From e0a6d90f32c52b3e0148b5b530fa44c53ebf06ed Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 27 Mar 2024 16:18:33 -0700 Subject: [PATCH 01/11] add mock plot data to result file --- .../upy/simularium/simularium_helper.py | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/cellpack/autopack/upy/simularium/simularium_helper.py b/cellpack/autopack/upy/simularium/simularium_helper.py index 1e047e56..4aab49f9 100644 --- a/cellpack/autopack/upy/simularium/simularium_helper.py +++ b/cellpack/autopack/upy/simularium/simularium_helper.py @@ -18,6 +18,7 @@ ModelMetaData, CameraData, DisplayData, + ScatterPlotData, ) from simulariumio.cellpack import CellpackConverter, HAND_TYPE from simulariumio.constants import DISPLAY_TYPE, VIZ_TYPE @@ -1371,7 +1372,64 @@ def writeToFile(self, file_name, bb, recipe_name, version): time_units=UnitData("ns"), # nanoseconds spatial_units=UnitData("nm"), # nanometers ) - TrajectoryConverter(converted_data).save(file_name, False) + plot_data = ScatterPlotData( + title="test_plot", + xaxis_title="concentrations (uM)", + yaxis_title="time (s)", + xtrace=np.array( + [ + 0.0, + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 3.0, + 4.0, + 3.0, + 7.0, + 5.0, + 3.0, + 6.0, + 5.0, + 2.0, + 4.0, + ] + ), + ytraces={ + "agent1": np.array( + [ + 0.0, + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 3.0, + 4.0, + 3.0, + 7.0, + 5.0, + 3.0, + 6.0, + 5.0, + 2.0, + 4.0, + ] + ), + }, + ) + trajectory_converter = TrajectoryConverter(converted_data) + trajectory_converter.add_plot(plot_data, "scatter") + trajectory_converter.save(file_name, False) return file_name def raycast(self, **kw): From 750f72ad0e71a01378e211998717b1d901954fc1 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 1 May 2024 16:02:17 -0700 Subject: [PATCH 02/11] solution 1: using binary --- cellpack/autopack/upy/simularium/simularium_helper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cellpack/autopack/upy/simularium/simularium_helper.py b/cellpack/autopack/upy/simularium/simularium_helper.py index 4aab49f9..63cdb52c 100644 --- a/cellpack/autopack/upy/simularium/simularium_helper.py +++ b/cellpack/autopack/upy/simularium/simularium_helper.py @@ -1372,6 +1372,7 @@ def writeToFile(self, file_name, bb, recipe_name, version): time_units=UnitData("ns"), # nanoseconds spatial_units=UnitData("nm"), # nanometers ) + converter = TrajectoryConverter(converted_data) plot_data = ScatterPlotData( title="test_plot", xaxis_title="concentrations (uM)", @@ -1427,9 +1428,8 @@ def writeToFile(self, file_name, bb, recipe_name, version): ), }, ) - trajectory_converter = TrajectoryConverter(converted_data) - trajectory_converter.add_plot(plot_data, "scatter") - trajectory_converter.save(file_name, False) + converter.add_plot(plot_data, "scatter") + converter.save(file_name) return file_name def raycast(self, **kw): From a7acbb4016cfc9161584a30a945e17e6ae558d86 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 8 May 2024 11:11:53 -0700 Subject: [PATCH 03/11] add histogram --- .../upy/simularium/simularium_helper.py | 91 +++++++++++++------ 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/cellpack/autopack/upy/simularium/simularium_helper.py b/cellpack/autopack/upy/simularium/simularium_helper.py index 63cdb52c..30b8fa28 100644 --- a/cellpack/autopack/upy/simularium/simularium_helper.py +++ b/cellpack/autopack/upy/simularium/simularium_helper.py @@ -19,6 +19,7 @@ CameraData, DisplayData, ScatterPlotData, + HistogramPlotData, ) from simulariumio.cellpack import CellpackConverter, HAND_TYPE from simulariumio.constants import DISPLAY_TYPE, VIZ_TYPE @@ -1373,35 +1374,65 @@ def writeToFile(self, file_name, bb, recipe_name, version): spatial_units=UnitData("nm"), # nanometers ) converter = TrajectoryConverter(converted_data) - plot_data = ScatterPlotData( - title="test_plot", + # plot_data = ScatterPlotData( + # title="test_plot", + # xaxis_title="concentrations (uM)", + # yaxis_title="time (s)", + # xtrace=np.array( + # [ + # 0.0, + # 1.0, + # 2.0, + # 3.0, + # 4.0, + # 5.0, + # 6.0, + # 7.0, + # 8.0, + # 9.0, + # 3.0, + # 4.0, + # 3.0, + # 7.0, + # 5.0, + # 3.0, + # 6.0, + # 5.0, + # 2.0, + # 4.0, + # ] + # ), + # ytraces={ + # "agent1": np.array( + # [ + # 0.0, + # 1.0, + # 2.0, + # 3.0, + # 4.0, + # 5.0, + # 6.0, + # 7.0, + # 8.0, + # 9.0, + # 3.0, + # 4.0, + # 3.0, + # 7.0, + # 5.0, + # 3.0, + # 6.0, + # 5.0, + # 2.0, + # 4.0, + # ] + # ), + # }, + # ) + plot_data = HistogramPlotData( + title="Test Histogram 2", xaxis_title="concentrations (uM)", - yaxis_title="time (s)", - xtrace=np.array( - [ - 0.0, - 1.0, - 2.0, - 3.0, - 4.0, - 5.0, - 6.0, - 7.0, - 8.0, - 9.0, - 3.0, - 4.0, - 3.0, - 7.0, - 5.0, - 3.0, - 6.0, - 5.0, - 2.0, - 4.0, - ] - ), - ytraces={ + traces={ "agent1": np.array( [ 0.0, @@ -1428,8 +1459,8 @@ def writeToFile(self, file_name, bb, recipe_name, version): ), }, ) - converter.add_plot(plot_data, "scatter") - converter.save(file_name) + converter.add_plot(plot_data, "histogram") + converter.save(file_name, False) return file_name def raycast(self, **kw): From f3c2149b8090ba176c82a924f6a43668804132c3 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 23 May 2024 14:20:05 -0700 Subject: [PATCH 04/11] Co-authored-by: Megan Riel-Mehan --- cellpack/autopack/Analysis.py | 51 +++++++++- cellpack/autopack/Environment.py | 9 +- cellpack/autopack/upy/simularium/plots.py | 36 +++++++ .../upy/simularium/simularium_helper.py | 93 +------------------ cellpack/bin/pack.py | 1 + 5 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 cellpack/autopack/upy/simularium/plots.py diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index 03bfec63..f57652ba 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -57,7 +57,6 @@ def __init__( self.smallest = env.smallestProteinSize self.largest = env.largestProteinSize self.afviewer = viewer - self.helper = None if viewer: self.helper = self.afviewer.vi self.result_file = result_file @@ -86,6 +85,7 @@ def __init__( self.figures_path = self.output_path / "figures" self.figures_path.mkdir(parents=True, exist_ok=True) self.seed_to_results = {} + self.helper = autopack.helper autopack._colors = None @staticmethod @@ -643,6 +643,11 @@ def plot_position_distribution_total(self, all_positions): x_label=dim, y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title=dim, + traces={"count": all_pos[:, ind]}, + ) def plot_position_distribution(self, ingr): pos_xyz = numpy.array(self.env.ingredient_positions[ingr.name]) @@ -658,6 +663,9 @@ def plot_position_distribution(self, ingr): x_label=dim, y_label="count", ) + self.helper.plot_data.add_histogram( + title=ingr.name, xaxis_title=dim, traces={"count": all_pos[:, ind]} + ) def plot_occurence_distribution(self, ingr): occ = self.env.occurences[ingr.name] @@ -680,6 +688,11 @@ def plot_occurence_distribution(self, ingr): x_label="occurrences", y_label="count", ) + self.helper.plot_data.add_histogram( + title=ingr.name, + xaxis_title="occurrences", + traces={"count": numpy.array(occ)}, + ) def plot_distance_distribution(self, all_ingredient_distances): """ @@ -696,6 +709,11 @@ def plot_distance_distribution(self, all_ingredient_distances): x_label="pairwise distance", y_label="count", ) + self.helper.plot_data.add_histogram( + title=ingr_key, + xaxis_title="pairwise distance", + traces={"count": numpy.array(distances)}, + ) def correlation(self, ingr): basename = self.env.basename @@ -2181,7 +2199,6 @@ def pack_one_seed( self.afviewer.clearFill("Test_Spheres2D") else: self.env.reset() - self.env.saveResult = True numpy.random.seed(seed) self.build_grid() two_d = self.env.is_two_d() @@ -2331,6 +2348,7 @@ def pack_one_seed( grid_image_writer = gradient.create_voxelization(grid_image_writer) grid_image_writer.export_image() + Writer().save_as_simularium(self.env, self.seed_to_results) return ( center_distance_dict, pairwise_distance_dict, @@ -2498,9 +2516,6 @@ def doloop( self.writeJSON(ingredient_occurences_file, ingredient_occurence_dict) self.writeJSON(ingredient_key_file, ingredient_key_dict) - if number_of_packings > 1: - Writer().save_as_simularium(self.env, self.seed_to_results) - all_ingredient_positions = self.combine_results_from_seeds( ingredient_position_dict ) @@ -2559,6 +2574,11 @@ def doloop( x_label="center distance", y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title="center distance", + traces={"count": numpy.array(all_center_distance_array)}, + ) if len(all_center_distance_array) > 1: self.histogram( @@ -2569,6 +2589,11 @@ def doloop( x_label="pairwise distances", y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title="pairwise distances", + traces={"count": numpy.array(all_pairwise_distance_array)}, + ) # plot the angle if len(all_ingredient_angle_array) > 1: @@ -2579,6 +2604,11 @@ def doloop( x_label="angles X", y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title="angles X", + traces={"count": numpy.array(all_ingredient_angle_array[0])}, + ) self.histogram( all_ingredient_angle_array[1], self.figures_path / f"all_angles_Y_{self.env.basename}.png", @@ -2586,6 +2616,11 @@ def doloop( x_label="angles Y", y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title="angles Y", + traces={"count": numpy.array(all_ingredient_angle_array[1])}, + ) self.histogram( all_ingredient_angle_array[2], self.figures_path / f"all_angles_Z_{self.env.basename}.png", @@ -2593,3 +2628,9 @@ def doloop( x_label="angles Z", y_label="count", ) + self.helper.plot_data.add_histogram( + title="all_ingredients", + xaxis_title="angles Z", + traces={"count": numpy.array(all_ingredient_angle_array[2])}, + ) + Writer().save_as_simularium(self.env, self.seed_to_results) diff --git a/cellpack/autopack/Environment.py b/cellpack/autopack/Environment.py index b9dcd713..30fe06e3 100644 --- a/cellpack/autopack/Environment.py +++ b/cellpack/autopack/Environment.py @@ -143,7 +143,11 @@ def __init__(self, config=None, recipe=None): self.name = name self.version = recipe.get("version", "default") # saving/pickle option - self.saveResult = "out" in config + self.saveResult = ( + "out" in config + and not config["save_analyze_result"] + and not config["number_of_packings"] > 1 + ) self.out_folder = create_output_dir(config["out"], name, config["place_method"]) self.base_name = f"{self.name}_{config['name']}_{self.version}" self.grid_file_out = ( @@ -2174,8 +2178,9 @@ def pack_grid( pbar.close() self.log.info("time to fill %d", t2 - t1) all_objects = self.prep_molecules_for_save(distances, free_points, nbFreePoints) - + print("SELF.result", self.saveResult) if self.saveResult: + print("SAVING FROM ENV") self.save_result( free_points, distances=distances, diff --git a/cellpack/autopack/upy/simularium/plots.py b/cellpack/autopack/upy/simularium/plots.py new file mode 100644 index 00000000..8cd4477d --- /dev/null +++ b/cellpack/autopack/upy/simularium/plots.py @@ -0,0 +1,36 @@ +import numpy as np +from simulariumio import ScatterPlotData, HistogramPlotData + + +class PlotData: + def __init__(self): + self.plot_list = [] # list of tuples + + def _add_plot(self, plot): + self.plot_list.append(plot) + + def add_scatter(self, title, xaxis_title, yaxis_title, xtrace, ytraces): + self._add_plot( + ( + "scatter", + ScatterPlotData( + title=title, + xaxis_title=xaxis_title, + yaxis_title=yaxis_title, + xtrace=np.array(xtrace), + ytraces={key: np.array(value) for key, value in ytraces.items()}, + ), + ) + ) + + def add_histogram(self, title, xaxis_title, traces): + self._add_plot( + ( + "histogram", + HistogramPlotData( + title=title, + xaxis_title=xaxis_title, + traces={key: np.array(value) for key, value in traces.items()}, + ), + ) + ) diff --git a/cellpack/autopack/upy/simularium/simularium_helper.py b/cellpack/autopack/upy/simularium/simularium_helper.py index 257a03cd..a87ad5b0 100644 --- a/cellpack/autopack/upy/simularium/simularium_helper.py +++ b/cellpack/autopack/upy/simularium/simularium_helper.py @@ -17,13 +17,12 @@ ModelMetaData, CameraData, DisplayData, - ScatterPlotData, - HistogramPlotData, ) from simulariumio.cellpack import CellpackConverter, HAND_TYPE from simulariumio.constants import DISPLAY_TYPE, VIZ_TYPE from cellpack.autopack.upy import hostHelper +from cellpack.autopack.upy.simularium.plots import PlotData from cellpack.autopack.DBRecipeHandler import DBUploader, DBMaintenance from cellpack.autopack.interface_objects.database_ids import DATABASE_IDS import collada @@ -114,6 +113,7 @@ def __init__(self, master=None, vi=None): self.nogui = True self.hext = "dae" self.max_fiber_length = 0 + self.plot_data = PlotData() @staticmethod def format_rgb_color(color): @@ -1373,92 +1373,9 @@ def writeToFile(self, file_name, bb, recipe_name, version): spatial_units=UnitData("nm"), # nanometers ) converter = TrajectoryConverter(converted_data) - # plot_data = ScatterPlotData( - # title="test_plot", - # xaxis_title="concentrations (uM)", - # yaxis_title="time (s)", - # xtrace=np.array( - # [ - # 0.0, - # 1.0, - # 2.0, - # 3.0, - # 4.0, - # 5.0, - # 6.0, - # 7.0, - # 8.0, - # 9.0, - # 3.0, - # 4.0, - # 3.0, - # 7.0, - # 5.0, - # 3.0, - # 6.0, - # 5.0, - # 2.0, - # 4.0, - # ] - # ), - # ytraces={ - # "agent1": np.array( - # [ - # 0.0, - # 1.0, - # 2.0, - # 3.0, - # 4.0, - # 5.0, - # 6.0, - # 7.0, - # 8.0, - # 9.0, - # 3.0, - # 4.0, - # 3.0, - # 7.0, - # 5.0, - # 3.0, - # 6.0, - # 5.0, - # 2.0, - # 4.0, - # ] - # ), - # }, - # ) - plot_data = HistogramPlotData( - title="Test Histogram 2", - xaxis_title="concentrations (uM)", - traces={ - "agent1": np.array( - [ - 0.0, - 1.0, - 2.0, - 3.0, - 4.0, - 5.0, - 6.0, - 7.0, - 8.0, - 9.0, - 3.0, - 4.0, - 3.0, - 7.0, - 5.0, - 3.0, - 6.0, - 5.0, - 2.0, - 4.0, - ] - ), - }, - ) - converter.add_plot(plot_data, "histogram") + plot_list = self.plot_data.plot_list + for type, plot in plot_list: + converter.add_plot(plot, type) converter.save(file_name, False) return file_name diff --git a/cellpack/bin/pack.py b/cellpack/bin/pack.py index b1f7423e..fa003f12 100644 --- a/cellpack/bin/pack.py +++ b/cellpack/bin/pack.py @@ -48,6 +48,7 @@ def pack(recipe, config_path=None, analysis_config_path=None): packing_config_data["save_analyze_result"] or packing_config_data["number_of_packings"] > 1 ): + env.saveResult = False analyze = Analysis( env=env, viewer=afviewer, From a7e108655e5c9323b5dc5a750161289ba100c537 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 23 May 2024 15:21:51 -0700 Subject: [PATCH 05/11] remove unused flag and call --- cellpack/autopack/Analysis.py | 2 -- cellpack/bin/pack.py | 1 - 2 files changed, 3 deletions(-) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index f57652ba..09e50ee0 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -2347,8 +2347,6 @@ def pack_one_seed( ) grid_image_writer = gradient.create_voxelization(grid_image_writer) grid_image_writer.export_image() - - Writer().save_as_simularium(self.env, self.seed_to_results) return ( center_distance_dict, pairwise_distance_dict, diff --git a/cellpack/bin/pack.py b/cellpack/bin/pack.py index fa003f12..b1f7423e 100644 --- a/cellpack/bin/pack.py +++ b/cellpack/bin/pack.py @@ -48,7 +48,6 @@ def pack(recipe, config_path=None, analysis_config_path=None): packing_config_data["save_analyze_result"] or packing_config_data["number_of_packings"] > 1 ): - env.saveResult = False analyze = Analysis( env=env, viewer=afviewer, From 4183e2a182536bef400cba3bfada97af447251eb Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 23 May 2024 16:02:23 -0700 Subject: [PATCH 06/11] wip: comment out saveResult in pack_one_seed to avoid packing twice --- cellpack/autopack/Analysis.py | 2 ++ cellpack/autopack/Environment.py | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index 09e50ee0..aedbd5fd 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -2199,6 +2199,8 @@ def pack_one_seed( self.afviewer.clearFill("Test_Spheres2D") else: self.env.reset() + # TODO: why we set saveResult to True here? can we add a condition? + # self.env.saveResult = True numpy.random.seed(seed) self.build_grid() two_d = self.env.is_two_d() diff --git a/cellpack/autopack/Environment.py b/cellpack/autopack/Environment.py index 30fe06e3..10bc5d19 100644 --- a/cellpack/autopack/Environment.py +++ b/cellpack/autopack/Environment.py @@ -2178,9 +2178,7 @@ def pack_grid( pbar.close() self.log.info("time to fill %d", t2 - t1) all_objects = self.prep_molecules_for_save(distances, free_points, nbFreePoints) - print("SELF.result", self.saveResult) if self.saveResult: - print("SAVING FROM ENV") self.save_result( free_points, distances=distances, From 5eb664f912688405cb5e77f234c4e943bb03978c Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 28 May 2024 16:05:06 -0700 Subject: [PATCH 07/11] save each seed --- cellpack/autopack/writers/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cellpack/autopack/writers/__init__.py b/cellpack/autopack/writers/__init__.py index 1d1b57ce..5b439ecb 100644 --- a/cellpack/autopack/writers/__init__.py +++ b/cellpack/autopack/writers/__init__.py @@ -188,6 +188,8 @@ def save_as_simularium(self, env, seed_to_results_map): is_aggregate = len(seed_to_results_map) > 1 if is_aggregate: result_file_name = f"{env.result_file.split('_seed')[0]}_all" + else: + result_file_name = f"{env.result_file.split('_seed')[0]}_seed_{list(seed_to_results_map.keys())[0]}" file_name = env.helper.writeToFile( result_file_name, env.boundingBox, env.name, env.version ) From d98018224cbe6da859b61df0a70694a35accca0d Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 28 May 2024 17:06:05 -0700 Subject: [PATCH 08/11] merge add_histogram into histogram() --- cellpack/autopack/Analysis.py | 121 ++++++++++++++-------------------- 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index aedbd5fd..4ad33bc4 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -643,11 +643,6 @@ def plot_position_distribution_total(self, all_positions): x_label=dim, y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title=dim, - traces={"count": all_pos[:, ind]}, - ) def plot_position_distribution(self, ingr): pos_xyz = numpy.array(self.env.ingredient_positions[ingr.name]) @@ -663,9 +658,6 @@ def plot_position_distribution(self, ingr): x_label=dim, y_label="count", ) - self.helper.plot_data.add_histogram( - title=ingr.name, xaxis_title=dim, traces={"count": all_pos[:, ind]} - ) def plot_occurence_distribution(self, ingr): occ = self.env.occurences[ingr.name] @@ -688,11 +680,6 @@ def plot_occurence_distribution(self, ingr): x_label="occurrences", y_label="count", ) - self.helper.plot_data.add_histogram( - title=ingr.name, - xaxis_title="occurrences", - traces={"count": numpy.array(occ)}, - ) def plot_distance_distribution(self, all_ingredient_distances): """ @@ -709,11 +696,6 @@ def plot_distance_distribution(self, all_ingredient_distances): x_label="pairwise distance", y_label="count", ) - self.helper.plot_data.add_histogram( - title=ingr_key, - xaxis_title="pairwise distance", - traces={"count": numpy.array(distances)}, - ) def correlation(self, ingr): basename = self.env.basename @@ -1705,33 +1687,54 @@ def get_parametrized_representation( return all_spilr - def histogram(self, distances, filename, title_str="", x_label="", y_label=""): - plt.clf() - # calculate histogram - nbins = int(numpy.sqrt(len(distances))) - if nbins < 2: - return - y, bin_edges = numpy.histogram(distances, bins=nbins) - bincenters = 0.5 * (bin_edges[1:] + bin_edges[:-1]) - - # calculate standard error for values in each bin - bin_inds = numpy.digitize(distances, bin_edges) - x_err_vals = numpy.zeros(y.shape) - for bc in range(nbins): - dist_vals = distances[bin_inds == (bc + 1)] - if len(dist_vals) > 1: - x_err_vals[bc] = numpy.std(dist_vals) - else: - x_err_vals[bc] = 0 - y_err_vals = numpy.sqrt(y * (1 - y / numpy.sum(y))) - # set bin width - dbin = 0.9 * (bincenters[1] - bincenters[0]) - plt.bar(bincenters, y, width=dbin, color="r", xerr=x_err_vals, yerr=y_err_vals) - plt.title(title_str) - plt.xlabel(x_label) - plt.ylabel(y_label) - plt.savefig(filename) - plt.close() + def histogram( + self, + distances, + filename, + title_str="", + x_label="", + y_label="", + save_png=False, + add_to_result=True, + ): + if save_png: + # use matplotlib to plot histogram and save it as png file + plt.clf() + # calculate histogram + nbins = int(numpy.sqrt(len(distances))) + if nbins < 2: + return + y, bin_edges = numpy.histogram(distances, bins=nbins) + bincenters = 0.5 * (bin_edges[1:] + bin_edges[:-1]) + + # calculate standard error for values in each bin + bin_inds = numpy.digitize(distances, bin_edges) + x_err_vals = numpy.zeros(y.shape) + for bc in range(nbins): + dist_vals = distances[bin_inds == (bc + 1)] + if len(dist_vals) > 1: + x_err_vals[bc] = numpy.std(dist_vals) + else: + x_err_vals[bc] = 0 + y_err_vals = numpy.sqrt(y * (1 - y / numpy.sum(y))) + # set bin width + dbin = 0.9 * (bincenters[1] - bincenters[0]) + plt.bar( + bincenters, y, width=dbin, color="r", xerr=x_err_vals, yerr=y_err_vals + ) + plt.title(title_str) + plt.xlabel(x_label) + plt.ylabel(y_label) + plt.savefig(filename) + plt.close() + + if add_to_result: + # add histogrm to result file and display on the web page + self.helper.plot_data.add_histogram( + title=f"{title_str}: {x_label}", + xaxis_title=x_label, + traces={y_label: numpy.array(distances)}, + ) def plot(self, rdf, radii, file_name): plt.clf() @@ -2574,11 +2577,6 @@ def doloop( x_label="center distance", y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title="center distance", - traces={"count": numpy.array(all_center_distance_array)}, - ) if len(all_center_distance_array) > 1: self.histogram( @@ -2589,11 +2587,6 @@ def doloop( x_label="pairwise distances", y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title="pairwise distances", - traces={"count": numpy.array(all_pairwise_distance_array)}, - ) # plot the angle if len(all_ingredient_angle_array) > 1: @@ -2604,11 +2597,6 @@ def doloop( x_label="angles X", y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title="angles X", - traces={"count": numpy.array(all_ingredient_angle_array[0])}, - ) self.histogram( all_ingredient_angle_array[1], self.figures_path / f"all_angles_Y_{self.env.basename}.png", @@ -2616,11 +2604,6 @@ def doloop( x_label="angles Y", y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title="angles Y", - traces={"count": numpy.array(all_ingredient_angle_array[1])}, - ) self.histogram( all_ingredient_angle_array[2], self.figures_path / f"all_angles_Z_{self.env.basename}.png", @@ -2628,9 +2611,7 @@ def doloop( x_label="angles Z", y_label="count", ) - self.helper.plot_data.add_histogram( - title="all_ingredients", - xaxis_title="angles Z", - traces={"count": numpy.array(all_ingredient_angle_array[2])}, - ) + if number_of_packings > 1: + for seed, result in self.seed_to_results.items(): + Writer().save_as_simularium(self.env, {seed: result}) Writer().save_as_simularium(self.env, self.seed_to_results) From 36960038d4fd4e1337cba24d6a8877b7b63dbe1d Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 28 May 2024 17:07:43 -0700 Subject: [PATCH 09/11] remove unused code --- cellpack/autopack/Analysis.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index 4ad33bc4..aa0c8df8 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -2202,8 +2202,6 @@ def pack_one_seed( self.afviewer.clearFill("Test_Spheres2D") else: self.env.reset() - # TODO: why we set saveResult to True here? can we add a condition? - # self.env.saveResult = True numpy.random.seed(seed) self.build_grid() two_d = self.env.is_two_d() From c0d478647cd3e098a71fb13ae48fdaff76a46c01 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 29 May 2024 13:03:41 -0700 Subject: [PATCH 10/11] set save_png to true for pairwise distances --- cellpack/autopack/Analysis.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index aa0c8df8..f9fbdbbd 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -695,6 +695,7 @@ def plot_distance_distribution(self, all_ingredient_distances): title_str=ingr_key, x_label="pairwise distance", y_label="count", + save_png=True, ) def correlation(self, ingr): @@ -1727,6 +1728,7 @@ def histogram( plt.ylabel(y_label) plt.savefig(filename) plt.close() + print("saved", filename) if add_to_result: # add histogrm to result file and display on the web page From b1083ea137bcb3dd6b85161707b1e2d7f0d5763a Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Jun 2024 12:04:12 -0700 Subject: [PATCH 11/11] avoid early return in saving histogram --- cellpack/autopack/Analysis.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index 28b8c0f4..ddbf7343 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -588,7 +588,23 @@ def run_analysis_workflow( **analysis_config["create_report"], ) - def histogram(self, distances, filename, title_str="", x_label="", y_label="", save_png=True, add_to_result=True): + def histogram( + self, + distances, + filename, + title_str="", + x_label="", + y_label="", + add_to_result=True, + save_png=False, + ): + if add_to_result: + # add histogrm to result file and display on the web page + self.helper.plot_data.add_histogram( + title=f"{title_str}: {x_label}", + xaxis_title=x_label, + traces={y_label: numpy.array(distances)}, + ) if save_png: # use matplotlib to create histogram and save as png plt.clf() @@ -620,14 +636,6 @@ def histogram(self, distances, filename, title_str="", x_label="", y_label="", s plt.savefig(filename) plt.close() - if add_to_result: - # add histogrm to result file and display on the web page - self.helper.plot_data.add_histogram( - title=f"{title_str}: {x_label}", - xaxis_title=x_label, - traces={y_label: numpy.array(distances)}, - ) - def plot(self, rdf, radii, file_name): plt.clf() matplotlib.rc("font", size=14)