def get_expected_filenames(self, file_format=".png"): expected_files = [] for output in self.outputs: fname = "reveal_generated_plot_{}_against_{}_and_{}{}" fname = fname.format(output, self.param1, self.param2, file_format) fname = string2filename(fname) expected_files.append(fname) return expected_files
def plot_sim_group_performances(group_data, param1, param2, show_plots=True, save_plots=False, file_format=".png", **kwargs): """ Plot all performance parameters as a function of 2 parameters scanned. Parameters ---------- group_data : DataFrame SimulationGroup output data. param1, param2 : str Scanned parameters to plot the performances against. First parameter display along the index, second along the columns. show_plots : bool Plot all outputs as an interactive Matplotlib window? save_plots : bool Save all plots into a (separate) file? file_format : str File format to save the plots into. Supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff. Ignored if save_to_file=False. kwargs : dict Additional plotting parameters. See :func:`plot_as_heatmap` for details. """ _convert_df_dtypes(group_data) output_map = extract_output_data(group_data, [param1, param2]) num_figs = plot_output_as_heatmaps(output_map, **kwargs) if save_plots: for output_name, fig_num in num_figs.items(): plt.figure(fig_num) fname = "reveal_generated_plot_{}_against_{}_and_{}{}" fname = fname.format(output_name, param1, param2, file_format) fname = string2filename(fname) msg = "Saving output {} into file {}".format(output_name, fname) logger.warning(msg) print(msg) plt.savefig(fname) if show_plots: plt.show()
def to_folder(self, **kwargs): """ Export all plots as separate images files PNG, JPEG, .... """ if not isdir(self.target_dir): os.makedirs(self.target_dir) plot_list = self.df_plotter.contained_plots if self.export_data == EXPORT_YES: if not self.export_each_plot_data: self._export_data_source_to_file(target=self.target_dir) else: fname = self.data_filename + self.data_format data_path = join(self.target_dir, fname) self._export_plot_data_to_file(plot_list, data_path=data_path) if self.filename_from_title: filename_patt = "{i}_{title}.{ext}" else: filename_patt = "plot_{i}.{ext}" for i, desc in enumerate(plot_list): if self.skip_hidden and not desc.visible: continue if self.filename_from_title: title = string2filename(desc.plot_title) else: title = "" filename = filename_patt.format(i=i, ext=self.image_format, title=title) filepath = join(self.target_dir, filename) if isfile(filepath) and not self.overwrite_file_if_exists: msg = "Target image file path specified already exists" \ ": {}. Move the file or select the 'overwrite' checkbox." msg = msg.format(filepath) logger.exception(msg) raise IOError(msg) save_plot_to_file(desc.plot, filepath=filepath, dpi=self.image_dpi, **kwargs)
def setUpClass(cls): input_file = io_data_path('ChromExampleDataV2.xlsx') cls.study = load_study_from_excel(input_file, allow_gui=False) # Building and running simulation for only 1 experiment expt = cls.study.experiments[0] output_file = "{}_{}_{}.h5".format(cls.study.name, expt.name, 'cadet_simulation') output_file = string2filename(output_file) sim = build_simulation_from_experiment(expt) cls.study.simulations.append(sim) # create the CADET inputs cadet_input = build_cadet_input(sim) # write the CADET inputs write_to_h5(output_file, cadet_input, root='/input', overwrite=True) # run simulation to generate outputs run_cadet_simulator(output_file) update_simulation_results(sim, output_file)
def plot_output_as_heatmaps(output_map, filepath='', **kwargs): """ Print the dictionary of outputs as 2D heatmaps. Returns the mapping between the figure created and the performance plotted on it, so that they can be recovered and saved as image files. Parameters ---------- output_map : dict Dictionary of dataframes containing output parameters as a function of 2 scanned parameters. filepath : str Folder path to save all produced plots into. Filename will be generated from each output plotted. kwargs : dict Additional plotting parameters. See :func:`plot_as_heatmap` for details. Returns ------- Dict Mapping between the performance plotted and the figure number. """ fig_map = {} for fig_num, (output, data) in enumerate(output_map.items()): title = output.replace("_", " ") if filepath: fname = string2filename(output) + ".jpg" kwargs["filepath"] = join(filepath, fname) try: plot_as_heatmap(data, title=title, **kwargs) except Exception as e: logger.exception(e) else: fig_map[title] = fig_num return fig_map
def test_export_multi_renderer_plot_export_source_and_plot_data(self): model = DataFramePlotManager(contained_plots=[self.desc4], data_source=TEST_DF) for fmt in [".h5", ".csv", ".xlsx"]: exporter = NonInteractiveExporter( df_plotter=model, target_file=self.target_file, export_data=EXPORT_YES, data_format=fmt, export_each_plot_data=True ) export_func = getattr(exporter, self.converter) export_func() content = os.listdir(self.target_dir) self.assertEqual(len(content), 2) if fmt in [".h5", ".xlsx"]: expected = {self.target_filename, EXTERNAL_DATA_FNAME + fmt} else: expected = {self.target_filename, EXTERNAL_DATA_FNAME + ".zip"} self.assertEqual(set(content), expected) self.assert_valid_pptx(exporter) # Check data file content: if fmt == ".csv": zip_path = join(self.target_dir, EXTERNAL_DATA_FNAME + ".zip") with ZipFile(zip_path) as f: f.extractall(join(self.target_dir, "data")) source_fname = string2filename(DEFAULT_DATASET_NAME) + ".csv" data_file = join(self.target_dir, "data", source_fname) else: data_file = join(self.target_dir, EXTERNAL_DATA_FNAME + fmt) num_d = len(set(TEST_DF["d"])) self.assert_data_in_file(data_file, num_dataset=1 + num_d) # clean up at every loop: self.tearDown()
def _export_plot_data_to_file(self, plot_list, data_path, **kwargs): """ Export the plots' PlotData to a file. Supported formats include zipped .csv, multi-tab .xlsx and multi-key HDF5. Parameters ---------- plot_list : list List of PlotDescriptor instances, containing the plot whose data need to be exported. data_path : str Path to the data file to be generated. """ data_format = self.data_format if not splitext(data_path)[1]: data_path += data_format if isfile(data_path): msg = "Target data path specified already exists: {}. It will be" \ " overwritten.".format(data_path) logger.warning(msg) data_dir = dirname(data_path) if data_format == ".xlsx": writer = pd.ExcelWriter(data_path) elif data_format == ".h5": writer = pd.HDFStore(data_path) try: if data_format == ".csv": data_path = join( data_dir, string2filename(DEFAULT_DATASET_NAME) + ".csv") self._export_data_source_to_file(target=data_path) elif data_format == ".xlsx": writer = pd.ExcelWriter(data_path) self._export_data_source_to_file(target=writer, key=DEFAULT_DATASET_NAME) else: self._export_data_source_to_file(target=writer, key=DEFAULT_DATASET_NAME) created_csv_files = [data_path] for i, desc in enumerate(plot_list): df_dict = plot_data2dataframes(desc) for name, df in df_dict.items(): key = "plot_{}_{}".format(i, name) if data_format == ".csv": target_fpath = join(data_dir, string2filename(key) + ".csv") df.to_csv(target_fpath) created_csv_files.append(target_fpath) elif data_format == ".xlsx": df.to_excel(writer, sheet_name=key, **kwargs) elif data_format == ".h5": df.to_hdf(data_path, key=key, **kwargs) else: msg = "Data format {} not implemented. Please report" \ " this issue.".format(data_format) logger.exception(msg) raise NotImplementedError(msg) finally: if data_format in [".xlsx", ".h5"]: writer.close() if data_format == ".csv" and len(created_csv_files) > 1: # zip up all csv files: data_path = join(data_dir, self.data_filename + ".zip") with ZipFile(data_path, "w") as f: for f_path in created_csv_files: f.write(f_path, basename(f_path)) for f_path in created_csv_files: os.remove(f_path) if self.interactive: msg = "Plot data is stored in: {}".format(data_path) information(None, msg)
def run_chromatography_simulation(input_file, output_file=None, expt_id="Run_1", skip_cadet=False, skip_plot=False, skip_animation=False, binding_model=None, transport_model=None, allow_gui=True): """ Run chromatography simulation for the inputs given in `input_file` and plot the results. Parameters ---------- input_file : str (file path) The file path for the excel file containing the experiment/simulation input data. output_file : str (file path) The file path for the CADET output h5 file. If None, then a default name is chosen based on the study and experiment ids. expt_id : str The name of the exeperiment to use for intializing the CADET simulation. If None, the first experiment in the study is chosen. skip_cadet : bool If True, the CADET simulation is not run but instead the output file is directly loaded (assuming one already exists). The main usecase here is for testing. skip_plot : bool If True, the chromatogram plot is not generated. This is useful for testing. Returns ------- study : Study The Study instance containing the experiment and the simulation data. """ study = load_study_from_excel(input_file, allow_gui=allow_gui) # lets just pick a single experiment if expt_id is None: expt = study.experiments[0] else: expt = study.search_experiment_by_name(expt_id) logger.info('Running simulation for experiment : {!r}'.format(expt_id)) if output_file is None: output_file = ("{}_{}_{}.h5".format(study.name, expt.name, 'cadet_simulation')) output_file = string2filename(output_file) # create a simulation object from an experiment # FIXME: how to identify related expt and simulations ! sim = build_simulation_from_experiment(expt, binding_model=binding_model, transport_model=transport_model) study.simulations.append(sim) # create the CADET inputs cadet_input = build_cadet_input(sim) # NOTE: This is primarily used for testing/debugging workflow when the # CADET simulation doesn't need to be run everytime. if not skip_cadet: # write the CADET inputs write_to_h5(output_file, cadet_input, root='/input', overwrite=True) logger.info("Output file {} was generated".format(output_file)) # run simulation to generate outputs run_cadet_simulator(output_file) update_simulation_results(sim, output_file) if not skip_plot: plot_chromatogram(expt, sim) if not skip_animation: column_animation(sim) return study