diff --git a/cellpack/autopack/Analysis.py b/cellpack/autopack/Analysis.py index d0f09ad6..37b9a6e3 100644 --- a/cellpack/autopack/Analysis.py +++ b/cellpack/autopack/Analysis.py @@ -312,9 +312,6 @@ def run_distance_analysis( ] ) - df = pd.DataFrame() - df["Ingredient key"] = [] - df["Pairwise distance distribution"] = [] img_list = [] for ingr_key in all_pairwise_distances: ingr_distance_histo_path = figure_path.glob( @@ -327,11 +324,18 @@ def run_distance_analysis( path=f"{output_image_location}/{img_path.name}", ) ) + text_list = [ + "Ingredient key", + "Pairwise distance distribution", + *[ + val + for pair in zip(all_pairwise_distances.keys(), img_list) + for val in pair + ], + ] + + md_object.add_table(header="Pairwise Distance Distributions", table=pd.DataFrame([text_list])) - df = pd.DataFrame() - df["Ingredient key"] = all_pairwise_distances.keys() - df["Pairwise distance distribution"] = img_list - md_object.add_table(header="", table=df) def get_ingredient_key_from_object_or_comp_name( self, search_name, ingredient_key_dict @@ -409,6 +413,7 @@ def run_partner_analysis( if len(partner_pair_dict): md_object.add_header(header="Partner Analysis") + val_list = [] paired_keys = [] touching_radii = [] binding_probabilities = [] @@ -422,18 +427,38 @@ def run_partner_analysis( numpy.count_nonzero(pairwise_distances < padded_radius) / partner_values["num_packed"] ) - paired_keys.append(paired_key) - touching_radii.append(partner_values["touching_radius"]) - binding_probabilities.append(partner_values["binding_probability"]) - close_fractions.append(close_fraction) + val_list.extend( + [ + paired_key, + partner_values["touching_radius"], + partner_values["binding_probability"], + close_fraction, + ] + ) - df = pd.DataFrame() - df["Paired keys"] = paired_keys - df["Touching radii"] = touching_radii - df["Binding probabilities"] = binding_probabilities - df["Close packing fractions"] = close_fractions + text_list = [ + "Partner pair", + "Touching radius", + "Binding probability", + "Close packed fraction", + *val_list, + ] + md_object.add_table( + header="", + text=text_list, + ) + # paired_keys.append(paired_key) + # touching_radii.append(partner_values["touching_radius"]) + # binding_probabilities.append(partner_values["binding_probability"]) + # close_fractions.append(close_fraction) - md_object.add_table(header="", table=df) + # df = pd.DataFrame() + # df["Paired keys"] = paired_keys + # df["Touching radii"] = touching_radii + # df["Binding probabilities"] = binding_probabilities + # df["Close packing fractions"] = close_fractions + + # md_object.add_table(header="", table=df) def create_report( self, @@ -460,7 +485,23 @@ def create_report( run_*_analysis: bool whether to run specific analysis """ - self.ingredient_key_dict = self.read_dict_from_glob_file("ingredient_key_*") + if report_output_path is None: + report_output_path = self.output_path + report_output_path = Path(report_output_path) + + # initialize MarkdownWriter object + md_object = MarkdownWriter( + title="Packing analysis report", + output_path=self.output_path, + output_image_location=output_image_location, + report_name="analysis_report", + ) + + md_object.add_header( + header=f"Analysis for packing results located at {self.packing_results_path}", level=2 + ) + if not hasattr(self, "ingredient_key_dict"): + self.ingredient_key_dict = self.read_dict_from_glob_file("ingredient_key_*") if ingredient_keys is None: ingredient_keys = list(self.ingredient_key_dict.keys()) @@ -469,38 +510,33 @@ def create_report( ingredient_keys=ingredient_keys ) ingredient_radii = self.get_ingredient_radii(recipe_data=recipe_data) - pairwise_distance_dict = self.read_dict_from_glob_file( - "pairwise_distances_*.json" - ) - combined_pairwise_distance_dict = self.combine_results_from_seeds( - pairwise_distance_dict - ) + + if not hasattr(self, "pairwise_distance_dict"): self.pairwise_distance_dict = self.read_dict_from_glob_file( "pairwise_distances_*.json" ) - df = pd.DataFrame() - df["Ingredient name"] = list(ingredient_keys) - df["Encapsulating radius"] = list(ingredient_radii.values()) - df["Average number packed"] = list(avg_num_packed.values()) - - # path to save report and other outputs - if output_image_location is None: - output_image_location = self.output_path - - md_object = MarkdownWriter( - title="Packing analysis report", - output_path=self.output_path, - output_image_location=output_image_location, - report_name="analysis_report", + combined_pairwise_distance_dict = self.combine_results_from_seeds( + self.pairwise_distance_dict ) - md_object.add_header( - header=f"Analysis for packing results located at {self.packing_results_path}" - ) + val_list = [] + for key, radius, num_packed in zip(ingredient_keys, ingredient_radii.values(), avg_num_packed.values()): + val_list.extend([key, radius, num_packed]) + text_list = [ + "Ingredient name", + "Encapsulating radius", + "Average number packed", + *val_list, + ] + # table_data = pd.DataFrame({ + # "Ingredient name": list(ingredient_keys), + # "Encapsulating radius": list(ingredient_radii.values()), + # "Average number packed": list(avg_num_packed.values()) + # }) + md_object.add_table(header="Ingredient Analysis", table=pd.DataFrame([text_list])) - # TODO: check if this is needed # path to save report and other outputs if output_image_location is None: output_image_location = self.output_path @@ -509,6 +545,7 @@ def create_report( packing_results_path = self.packing_results_path figure_path = packing_results_path / "figures" + # add packing image to report md_object.add_images( header="Packing image", image_text=["Packing image"],