@@ -95,6 +95,8 @@ def __init__(self):
9595 self .plot_parameters ["lineplots" ]["style" ] = None
9696 self .plot_parameters ["lineplots" ]["estimator" ] = "mean"
9797 self .plot_parameters ["lineplots" ]["errorbar" ] = "ci"
98+ self .plot_parameters ["general" ] = {}
99+ self .plot_parameters ["general" ]["na_as_zero" ] = True
98100 self .structures_info_list = self .experiment .structures_info_list
99101 # Use the directly loaded parameter_settings instead of experiment.param_settings
100102 # to ensure all parameter groups (including particle_defect) are available
@@ -753,6 +755,22 @@ def set_plot_parameters(self, plot_type, **kwargs):
753755 for key , val in kwargs .items ():
754756 self .plot_parameters [plot_type ][key ] = val
755757
758+ def set_na_as_zero_in_plots (self , na_as_zero : bool = True ):
759+ """
760+ Set whether to treat NaN values as zero in plots.
761+
762+ Parameters
763+ ----------
764+ :param na_as_zero: bool, optional
765+ If True, NaN values will be treated as zero in plots. Defaults to True.
766+ This does not affect the underlying data, only the visualization.
767+
768+ Returns
769+ -------
770+ None
771+ """
772+ self .plot_parameters ["general" ]["na_as_zero" ] = na_as_zero
773+
756774 def run_analysis (
757775 self ,
758776 save = True ,
@@ -818,6 +836,7 @@ def run_analysis(
818836 return_figure = True ,
819837 metric_name = metric_name ,
820838 filter_dictionary = None ,
839+ na_as_zero = self .plot_parameters ["general" ]["na_as_zero" ],
821840 )
822841 if save :
823842 self .save_analysis (
@@ -876,6 +895,7 @@ def generate_analysis_plots(
876895 decimals : int = None ,
877896 return_figure = True ,
878897 filter_dictionary = None ,
898+ na_as_zero = True ,
879899 ** kwargs ,
880900 ):
881901 """
@@ -918,6 +938,7 @@ def generate_analysis_plots(
918938 return_figure = return_figure ,
919939 decimals = decimals ,
920940 filter_dictionary = filter_dictionary ,
941+ na_as_zero = na_as_zero ,
921942 ** plot_params ,
922943 ** kwargs
923944 )
@@ -929,6 +950,7 @@ def generate_analysis_plots(
929950 decimals = decimals ,
930951 return_figure = return_figure ,
931952 filter_dictionary = filter_dictionary ,
953+ na_as_zero = na_as_zero ,
932954 ** plot_params ,
933955 ** kwargs
934956 )
@@ -945,6 +967,7 @@ def _gen_heatmaps(
945967 filter_dictionary = None ,
946968 annotations = False ,
947969 palette = None ,
970+ na_as_zero = False ,
948971 ** kwargs ,
949972 ):
950973 """
@@ -1016,6 +1039,7 @@ def _gen_heatmaps(
10161039 metric_name = metric_name ,
10171040 conditions_cmaps = [cmap_palette ]* nconditions ,
10181041 decimals = decimals ,
1042+ na_as_zero = na_as_zero ,
10191043 ** kwargs
10201044 )
10211045 return plot
@@ -1034,6 +1058,7 @@ def _gen_lineplots(
10341058 decimals = "%.4f" ,
10351059 return_figure = True ,
10361060 filter_dictionary = None ,
1061+ na_as_zero = False ,
10371062 ** kwargs ,
10381063 ):
10391064 """
@@ -1079,8 +1104,13 @@ def _gen_lineplots(
10791104 if style is None and len (self .parameters_with_set_values ) > 1 :
10801105 style = self .parameters_with_set_values [1 ]
10811106 fig , axes = plt .subplots (figsize = figsize )
1107+ if na_as_zero :
1108+ data_to_plot = data .copy (deep = True )
1109+ data_to_plot .fillna (0 , inplace = True )
1110+ else :
1111+ data_to_plot = data
10821112 sns .lineplot (
1083- data = data ,
1113+ data = data_to_plot ,
10841114 x = x_param ,
10851115 y = metric_name ,
10861116 hue = hue ,
@@ -1095,7 +1125,7 @@ def _gen_lineplots(
10951125 axes .xaxis .set_major_formatter (FormatStrFormatter (decimals ))
10961126 title = estimator + " " + metric_name + " for " + x_param
10971127 if style is not None :
1098- title = title + "per " + style
1128+ title = title + " per " + style
10991129 plt .title (title )
11001130 plt .close ()
11011131 return fig
@@ -1140,6 +1170,17 @@ def save_analysis(
11401170 if keyname == "dataframes" :
11411171 df = self .get_analysis_output (keyname )
11421172 df_name = output_name + "_dataframe.csv"
1173+ files_indir = os .listdir (output_directory )
1174+ if df_name in files_indir :
1175+ count = 1
1176+ df_name = (
1177+ output_name + "_dataframe_" + str (count ) + ".csv"
1178+ )
1179+ while df_name in files_indir :
1180+ count += 1
1181+ df_name = (
1182+ output_name + "_dataframe_" + str (count ) + ".csv"
1183+ )
11431184 df .to_csv (os .path .join (output_directory , df_name ), index = False )
11441185 elif keyname == "plots" :
11451186 plots_dictionary = self .get_analysis_output (keyname )
@@ -1153,6 +1194,31 @@ def save_analysis(
11531194 + plot_type
11541195 + ".png"
11551196 )
1197+ current_files = os .listdir (output_directory )
1198+ if figure_name in current_files :
1199+ count = 1
1200+ figure_name = (
1201+ output_name
1202+ + "_"
1203+ + metric
1204+ + "_"
1205+ + plot_type
1206+ + "_"
1207+ + str (count )
1208+ + ".png"
1209+ )
1210+ while figure_name in current_files :
1211+ dt_string = dt_string + "_1"
1212+ figure_name = (
1213+ output_name
1214+ + "_"
1215+ + metric
1216+ + "_"
1217+ + plot_type
1218+ + "_"
1219+ + str (count )
1220+ + ".png"
1221+ )
11561222 plot .savefig (
11571223 os .path .join (output_directory , figure_name )
11581224 )
@@ -1182,11 +1248,21 @@ def save_images(self, output_name=None, output_directory=None, floats_as=float):
11821248 - If `self.reference_image` is present, saves it as "reference.tiff" in the output directory.
11831249 - Saves acquisition parameters as a YAML file in the output directory.
11841250 """
1251+ now = datetime .now () # dd/mm/YY H:M:S
1252+ dt_string = now .strftime ("%Y%m%d" )
11851253 if output_name is None :
11861254 output_name = "vLab4mic_images_"
11871255 if output_directory is None :
1256+ foldername = "simulated_images_" + dt_string
1257+ filesindir = os .listdir (self .output_directory )
1258+ if foldername in filesindir :
1259+ count = 1
1260+ foldername = "simulated_images_" + dt_string + "_" + str (count )
1261+ while foldername in filesindir :
1262+ count += 1
1263+ foldername = "simulated_images_" + dt_string + "_" + str (count )
11881264 output_directory = os .path .join (
1189- self .output_directory , "simulated_images" , ""
1265+ self .output_directory , foldername
11901266 )
11911267 if not os .path .exists (output_directory ):
11921268 os .makedirs (output_directory )
@@ -1198,8 +1274,9 @@ def save_images(self, output_name=None, output_directory=None, floats_as=float):
11981274 image = replicates [0 ]
11991275 for i in range (1 , nreps ):
12001276 image = np .concatenate ((image , replicates [i ]))
1201- name = output_directory + param_combination_id + ".tiff"
1202- tiff .imwrite (name , image )
1277+ name = param_combination_id + ".tiff"
1278+ dir_name = os .path .join (output_directory , name )
1279+ tiff .imwrite (dir_name , image )
12031280 if floats_as is not None and callable (floats_as ):
12041281 copy_of_params = copy .deepcopy (self .acquisition_outputs_parameters )
12051282 for combination_id , list_of_parameters in copy_of_params .items ():
@@ -1221,8 +1298,10 @@ def save_images(self, output_name=None, output_directory=None, floats_as=float):
12211298 )
12221299 if self .reference_image is not None :
12231300 # save reference image
1224- name_ref = output_directory + "reference.tiff"
1225- tiff .imwrite (name_ref , self .reference_image )
1301+ name_ref = param_combination_id + ".tiff"
1302+ dir_name_ref = os .path .join (output_directory , name_ref )
1303+ #name_ref = output_directory + "reference.tiff"
1304+ tiff .imwrite (dir_name_ref , self .reference_image )
12261305
12271306
12281307def run_parameter_sweep (
@@ -1271,6 +1350,8 @@ def run_parameter_sweep(
12711350 psf_voxel_nm = None ,
12721351 depth_of_field_nm = None ,
12731352 exp_time = None ,
1353+ # for plot generation
1354+ na_as_zero = True ,
12741355 # Add more as needed for your sweep
12751356):
12761357 """
@@ -1369,7 +1450,7 @@ def run_parameter_sweep(
13691450 sweep_gen .select_modalities (modalities = modalities )
13701451 sweep_gen .set_output_directory (output_directory = output_directory )
13711452 sweep_gen .set_number_of_repetitions (sweep_repetitions )
1372- # number of particles accross sweep
1453+ # number of particles across sweep
13731454 if particle_positions is not None :
13741455 # set those positions
13751456 sweep_gen .experiment .set_virtualsample_params (
@@ -1416,6 +1497,7 @@ def run_parameter_sweep(
14161497 reference_structure = reference_structure ,
14171498 reference_probe = reference_probe ,
14181499 ** reference_parameters )
1500+ sweep_gen .set_na_as_zero_in_plots (na_as_zero = na_as_zero )
14191501 if run_analysis :
14201502 sweep_gen .run_analysis (
14211503 save = save_analysis_results ,
0 commit comments