1import logging 

2from grond.meta import GrondError, classes_with_have_get_plot_classes 

3from grond.environment import Environment, GrondEnvironmentError 

4from grond.plot.collection import PlotCollectionManager 

5from grond.plot.config import PlotConfigCollection 

6 

7logger = logging.getLogger('grond.plots') 

8 

9 

10def get_plot_names(env): 

11 plot_classes = env.get_plot_classes() 

12 return [plot_class.name for plot_class in plot_classes] 

13 

14 

15def get_all_plot_classes(): 

16 plot_classes = set() 

17 for cls in classes_with_have_get_plot_classes: 

18 plot_classes.update(cls.get_plot_classes()) 

19 

20 return sorted(list(plot_classes), key=lambda plot: plot.name) 

21 

22 

23def get_plot_config_collection(env=None, plot_names=None): 

24 

25 if env is None: 

26 plot_classes = get_all_plot_classes() 

27 else: 

28 plot_classes = env.get_plot_classes() 

29 

30 plots = [] 

31 for plot_class in plot_classes: 

32 if plot_names is None or plot_class.name in plot_names: 

33 plots.append(plot_class()) 

34 

35 if plot_names is not None: 

36 if set(plot_names) - set([p.name for p in plots]): 

37 logger.warning( 

38 'Plots %s not available!' 

39 % ', '.join(set(plot_names) - set([p.name for p in plots]))) 

40 

41 collection = PlotConfigCollection(plot_configs=plots) 

42 

43 return collection 

44 

45 

46def make_plots( 

47 env, 

48 plot_config_collection=None, 

49 plot_names=None, 

50 plots_path=None, 

51 show=False): 

52 

53 if plot_config_collection is None: 

54 plot_config_collection = get_plot_config_collection(env, plot_names) 

55 

56 if plots_path is None: 

57 plots_path = env.get_plots_path() 

58 

59 plots = plot_config_collection.plot_configs 

60 manager = PlotCollectionManager(plots_path, show=show) 

61 env.set_plot_collection_manager(manager) 

62 

63 for plot in plots: 

64 try: 

65 plot.make(env) 

66 except (GrondEnvironmentError, GrondError) as e: 

67 logger.warning('Cannot create plot %s: %s' % ( 

68 plot.name, str(e))) 

69 

70 

71def make_movie(dirname, xpar_name, ypar_name, movie_filename): 

72 env = Environment([dirname]) 

73 optimiser = env.get_optimiser() 

74 problem = env.get_problem() 

75 history = env.get_history() 

76 movie_maker = optimiser.get_movie_maker( 

77 problem, history, xpar_name, ypar_name, movie_filename) 

78 

79 movie_maker.render() 

80 

81 

82__all__ = [ 

83 'get_plot_names', 

84 'get_plot_config_collection', 

85 'get_all_plot_classes', 

86 'make_plots', 

87 'make_movie']