Skip to content

Commit d92e894

Browse files
authored
Merge pull request #100 from HenriquesLab/checktypos
Check typos and output stability
2 parents 786cb15 + a7c8d3b commit d92e894

11 files changed

Lines changed: 165 additions & 38 deletions

File tree

src/vlab4mic/analysis/_plots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def sns_heatmap_pivots(
1414
return_figure=False,
1515
metric_name=None,
1616
decimals="%.4f",
17-
fillna = True,
17+
na_as_zero = True,
1818
**kwargs,
1919
):
2020
conditions = list(df_pivots.keys())
@@ -23,7 +23,7 @@ def sns_heatmap_pivots(
2323
annot_kws = {"size": 10, "rotation": 45}
2424
else:
2525
annot_kws = kwargs["annot_kws"]
26-
f, axes = plt.subplots(nconditions, 2, figsize=figsize)
26+
f, axes = plt.subplots(nconditions, 2, figsize=figsize, squeeze = False)
2727
plot_num = 0
2828
if cmaps_range == "same":
2929
# min and max here correspond to SSIM
@@ -37,7 +37,7 @@ def sns_heatmap_pivots(
3737
else:
3838
metric_n = "Metric"
3939
for n, cond in enumerate(conditions):
40-
if fillna:
40+
if na_as_zero:
4141
mask = df_pivots[cond][0].isna()
4242
df_pivots[cond][0][mask] = 0.0
4343
mask2 = df_pivots[cond][1].isna()

src/vlab4mic/analysis/sweep.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def pivot_dataframes_byCategory(
840840
)
841841
.reset_index()
842842
)
843-
# get mean and std accross parameter combinations of axes_param_names
843+
# get mean and std across parameter combinations of axes_param_names
844844
condition_mean_pivot = summarised_group.pivot(
845845
index=param1, columns=param2, values="Mean_Value"
846846
).round(4)

src/vlab4mic/generate/imaging.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self):
3838
self.field["reference_point"] = None
3939
self.fluorophore_params = (
4040
dict()
41-
) # contiain its photophysical parameters such as excitation and emission
41+
) # contain its photophysical parameters such as excitation and emission
4242
self.emitters_by_fluorophore = dict()
4343
self.emitters_by_channel = dict()
4444
self.writing_dir = ""
@@ -101,7 +101,7 @@ def _calculate_roi_ranges(self):
101101
]
102102
self.roi_params["ranges"] = [xrange, yrange, zrange]
103103

104-
def get_absoulte_reference_point(self):
104+
def get_absolute_reference_point(self):
105105
"""
106106
Get the absolute reference point in 3D (including focus plane).
107107
@@ -403,11 +403,11 @@ def _set_modality_channels(self, modality, fluorophores_in_channel, prints=False
403403
if prints:
404404
print(fluoname)
405405
channel_name = "ch" + str(ch)
406-
fluorophores_in_chanel = []
407-
fluorophores_in_chanel.append(
406+
fluorophores_in_channel = []
407+
fluorophores_in_channel.append(
408408
fluoname
409409
) # this is because a channel can have several fluorophores
410-
filter_dictionary[channel_name] = fluorophores_in_chanel
410+
filter_dictionary[channel_name] = fluorophores_in_channel
411411
ch += 1
412412
self.modalities[modality]["filters"] = filter_dictionary
413413
else:
@@ -441,7 +441,7 @@ def _set_modality_psf(
441441
if "depth" not in self.modalities[modality]["psf"]:
442442
depth_default = int((psf_stack.shape)[2] / 2)
443443
if prints:
444-
print("No depth parameter found for psf, asigning default")
444+
print("No depth parameter found for psf, assigning default")
445445
self.modalities[modality]["psf"]["depth"] = depth_default
446446

447447
def _set_modality_emission(self, modality, emission):
@@ -450,7 +450,7 @@ def _set_modality_emission(self, modality, emission):
450450
def _set_modality_detector(
451451
self,
452452
modality,
453-
image_size=[], # decpreciated, image size will only be taken from ROI
453+
image_size=[], # deprecated, image size will only be taken from ROI
454454
pixelsize=None,
455455
bits_pixel=None,
456456
noise_model=None,
@@ -480,7 +480,7 @@ def _set_modality_detector(
480480

481481
def _calculate_imsize_from_ROIranges(
482482
self, mod_pixelsize
483-
): # THere should be specified the scale of the pixelsize of modality
483+
): # There should be specified the scale of the pixelsize of modality
484484
roi_scale = self.get_roi_params("scale")
485485
xrange = self.get_roi_params("ranges")[0]
486486
yrange = self.get_roi_params("ranges")[1]
@@ -585,7 +585,7 @@ def generate_imaging(
585585
**kwargs,
586586
):
587587
"""
588-
Master funciton that generates image sequences depending on the modality
588+
Master function that generates image sequences depending on the modality
589589
This function is responsible for
590590
Preparing the coordinates according to ROI (DONE!)
591591
Creating the Photons per frame matrix
@@ -609,7 +609,7 @@ def generate_imaging(
609609
)
610610
else:
611611
fluonames = list(self.modalities[modality]["filters"][ch])
612-
# prepare a dictionary that conaints the emitters per channel defined
612+
# prepare a dictionary that contains the emitters per channel defined
613613
output_per_fluoname = dict()
614614
writing_notes = self.identifier + "_" + str(modality) + "_" + str(ch) + "_"
615615
for fluo in fluonames: # a channel could capture multiple fluorophores
@@ -869,8 +869,8 @@ def add_detector_noise(self, modality, stack):
869869

870870
def _adjust_to_pixel_depth(self, modality, stack):
871871
bits = self.modalities[modality]["detector"]["bits_pixel"]
872-
saturaton = (2**bits) - 1
873-
stack[stack > saturaton] == saturaton
872+
saturation = (2**bits) - 1
873+
stack[stack > saturation] == saturation
874874
return stack
875875

876876
def _save_timeseries_with_beads(self, timeseries_stack, beads_stack, notes):
@@ -1212,7 +1212,7 @@ def show_field(
12121212
ax.plot_surface(xx, yy, zz, alpha=0.2)
12131213
# show ROI reference point
12141214
if reference_pt:
1215-
ref_pt = self.get_absoulte_reference_point() * factor
1215+
ref_pt = self.get_absolute_reference_point() * factor
12161216
add_ax_scatter(ax, format_coordinates(ref_pt))
12171217
# EMITTERS PER FLUOROPHORE SPECIES
12181218
if self.emitters_by_fluorophore is not None:

src/vlab4mic/generate/molecular_structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def gen_targets_by_sequence(self, target_name, sequence, **kwargs):
597597
**kwargs
598598
Additional keyword arguments (e.g., fluorophore, labeling_efficiency, method).
599599
"""
600-
# only the first appearance of the epitope in every chain is retreived
600+
# only the first appearance of the epitope in every chain is retrieved
601601
# if a chain has more than one epitope, only the first one is returned
602602
target_seq = sequence
603603
method = "average"

src/vlab4mic/sweep_generator.py

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12281307
def 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,

src/vlab4mic/utils/data_format/configuration_format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def compile_modality_parameters(
7979
if prints:
8080
print(fluoname)
8181
channel_name = "ch" + str(ch)
82-
fluorophores_in_chanel = []
83-
fluorophores_in_chanel.append(
82+
fluorophores_in_channel = []
83+
fluorophores_in_channel.append(
8484
fluoname
8585
) # this is because a channel can have several fluorophores
86-
filter_dictionary[channel_name] = fluorophores_in_chanel
86+
filter_dictionary[channel_name] = fluorophores_in_channel
8787
ch += 1
8888
else:
8989
filter_dictionary = mod_pars["filters"]

0 commit comments

Comments
 (0)