|
|
@ -6,13 +6,12 @@ import pickle |
|
|
|
import seaborn |
|
|
|
import seaborn |
|
|
|
import matplotlib.pyplot as pyplot |
|
|
|
import matplotlib.pyplot as pyplot |
|
|
|
|
|
|
|
|
|
|
|
from peakutils.plot import plot as peakplot |
|
|
|
|
|
|
|
from scipy.signal import savgol_filter |
|
|
|
from scipy.signal import savgol_filter |
|
|
|
|
|
|
|
|
|
|
|
from .imageproc import STATS_FUNCS, LEFT_GUARD, ROI_NAME, RIGHT_GUARD |
|
|
|
from .commons import ROI_STATISTIC_FUNCTIONS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def peakplot_iloc(x, y, ind): |
|
|
|
def peakplot_iloc(x, y, ix1, ix1_label="peaks", ix2=None, ix2_label="peaks"): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Plots the original data with the peaks that were identified |
|
|
|
Plots the original data with the peaks that were identified |
|
|
|
|
|
|
|
|
|
|
@ -29,15 +28,26 @@ def peakplot_iloc(x, y, ind): |
|
|
|
ind : array-like |
|
|
|
ind : array-like |
|
|
|
Indexes of the identified peaks |
|
|
|
Indexes of the identified peaks |
|
|
|
""" |
|
|
|
""" |
|
|
|
pyplot.plot(x, y, "--") |
|
|
|
pyplot.plot(x, y) |
|
|
|
|
|
|
|
num_items = len(ix1) |
|
|
|
pyplot.plot( |
|
|
|
pyplot.plot( |
|
|
|
x.iloc[ind], |
|
|
|
x.iloc[ix1], |
|
|
|
y.iloc[ind], |
|
|
|
y.iloc[ix1], |
|
|
|
"r+", |
|
|
|
"r+", |
|
|
|
ms=5, |
|
|
|
ms=5, |
|
|
|
mew=2, |
|
|
|
mew=2, |
|
|
|
label="{} peaks".format(len(ind)), |
|
|
|
label=f"{num_items} {ix1_label}", |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
if ix2 is not None: |
|
|
|
|
|
|
|
num_items = len(ix2) |
|
|
|
|
|
|
|
pyplot.plot( |
|
|
|
|
|
|
|
x.iloc[ix2], |
|
|
|
|
|
|
|
y.iloc[ix2], |
|
|
|
|
|
|
|
"g1", |
|
|
|
|
|
|
|
ms=5, |
|
|
|
|
|
|
|
mew=2, |
|
|
|
|
|
|
|
label=f"{num_items} {ix2_label}", |
|
|
|
|
|
|
|
) |
|
|
|
pyplot.legend() |
|
|
|
pyplot.legend() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -56,26 +66,31 @@ def set_plotting_styles(): |
|
|
|
seaborn.set_context("talk") |
|
|
|
seaborn.set_context("talk") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_pandas_data_frame(): |
|
|
|
def init_pandas_data_frame(parameters): |
|
|
|
columns = ["file", "frame"] |
|
|
|
columns = ["file", "frame"] |
|
|
|
for prefix in (LEFT_GUARD, ROI_NAME, RIGHT_GUARD): |
|
|
|
groups = ( |
|
|
|
for key in STATS_FUNCS: |
|
|
|
parameters.roi_name, |
|
|
|
columns.append(f"{prefix}.{key}") |
|
|
|
parameters.left_guard_name, |
|
|
|
|
|
|
|
parameters.right_guard_name, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
for group in groups: |
|
|
|
|
|
|
|
for func_name in ROI_STATISTIC_FUNCTIONS: |
|
|
|
|
|
|
|
columns.append(f"{group}.{func_name}") |
|
|
|
return pandas.DataFrame(columns=columns) |
|
|
|
return pandas.DataFrame(columns=columns) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def construct_data_frame(stats_results): |
|
|
|
def construct_data_frame(stats_results, parameters): |
|
|
|
data_frame = init_pandas_data_frame() |
|
|
|
data_frame = init_pandas_data_frame(parameters) |
|
|
|
data_frame = data_frame.append(stats_results, ignore_index=True) |
|
|
|
data_frame = data_frame.append(stats_results, ignore_index=True) |
|
|
|
return data_frame.sort_values(by=["frame"]).reset_index(drop=True) |
|
|
|
return data_frame.sort_values(by=["frame"]).reset_index(drop=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_guard_value(data_frame, settings): |
|
|
|
def find_guard_threshold(data_frame, parameters): |
|
|
|
left_avg = data_frame["left.average"] |
|
|
|
left_values = data_frame[parameters.left_guard_column] |
|
|
|
right_avg = data_frame["right.average"] |
|
|
|
right_values = data_frame[parameters.right_guard_column] |
|
|
|
guards_avg = left_avg.append(right_avg, ignore_index=True) |
|
|
|
guard_values = left_values.append(right_values, ignore_index=True) |
|
|
|
guard_data = numpy.histogram( |
|
|
|
guard_data = numpy.histogram( |
|
|
|
guards_avg, bins=settings.guard_histogram_bins |
|
|
|
guard_values, bins=parameters.guard_histogram_bins |
|
|
|
) |
|
|
|
) |
|
|
|
guard_counts = guard_data[0].astype(numpy.float16) |
|
|
|
guard_counts = guard_data[0].astype(numpy.float16) |
|
|
|
guard_edges = guard_data[1][1:] # edges enclose the counts |
|
|
|
guard_edges = guard_data[1][1:] # edges enclose the counts |
|
|
@ -85,13 +100,15 @@ def find_guard_value(data_frame, settings): |
|
|
|
pyplot.title("Histogram of Guard Avarages (not filtered)") |
|
|
|
pyplot.title("Histogram of Guard Avarages (not filtered)") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
path = settings.data_dir / "1-histogram-of-guard-avarages-not-filtered.png" |
|
|
|
path = ( |
|
|
|
|
|
|
|
parameters.data_dir / "1-histogram-of-guard-avarages-not-filtered.png" |
|
|
|
|
|
|
|
) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
guard_counts_filtered = savgol_filter( |
|
|
|
guard_counts_filtered = savgol_filter( |
|
|
|
guard_counts, |
|
|
|
guard_counts, |
|
|
|
settings.guard_filter_window, |
|
|
|
parameters.guard_filter_window, |
|
|
|
settings.guard_filter_polynom, |
|
|
|
parameters.guard_filter_polynom, |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
@ -99,7 +116,7 @@ def find_guard_value(data_frame, settings): |
|
|
|
pyplot.title("Histogram of Guard Avarages (Savitzky-Golay filter)") |
|
|
|
pyplot.title("Histogram of Guard Avarages (Savitzky-Golay filter)") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
path = settings.data_dir / "2-histogram-of-guard-avarages-filtered.png" |
|
|
|
path = parameters.data_dir / "2-histogram-of-guard-avarages-filtered.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
# Finding the first minima after the first peak |
|
|
|
# Finding the first minima after the first peak |
|
|
@ -114,47 +131,53 @@ def find_guard_value(data_frame, settings): |
|
|
|
inverted_series = max(guard_counts_filtered) - guard_counts_filtered |
|
|
|
inverted_series = max(guard_counts_filtered) - guard_counts_filtered |
|
|
|
indexes = peakutils.indexes( |
|
|
|
indexes = peakutils.indexes( |
|
|
|
inverted_series[first_peak_position:], |
|
|
|
inverted_series[first_peak_position:], |
|
|
|
min_dist=settings.guards_minima_min_dist, |
|
|
|
min_dist=parameters.guards_minima_min_dist, |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# since we shortened the data, we need to add the first peak position |
|
|
|
# since we shortened the data, we need to add the first peak position |
|
|
|
first_minima_position = indexes[0] + first_peak_position |
|
|
|
first_minima_position = indexes[0] + first_peak_position |
|
|
|
settings.guard_max_value = guard_edges[first_minima_position] |
|
|
|
parameters.guard_max_value = guard_edges[first_minima_position] |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
peakplot(guard_edges, guard_counts_filtered, [first_minima_position]) |
|
|
|
peakplot_iloc( |
|
|
|
|
|
|
|
pandas.Series(guard_edges), pandas.Series(guard_counts_filtered), [first_minima_position], "minima" |
|
|
|
|
|
|
|
) |
|
|
|
pyplot.title( |
|
|
|
pyplot.title( |
|
|
|
( |
|
|
|
( |
|
|
|
f"Histogram of Guard Avarages (Savitzky-Golay filter)," |
|
|
|
f"Histogram of Guard Avarages (Savitzky-Golay filter)," |
|
|
|
f" first minima at {int(settings.guard_max_value)} au" |
|
|
|
f" first minima at {int(parameters.guard_max_value)} au" |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
pyplot.ylabel("Number of Observations [1]") |
|
|
|
path = ( |
|
|
|
path = ( |
|
|
|
settings.data_dir |
|
|
|
parameters.data_dir |
|
|
|
/ "3-histogram-of-guard-avarages-filtered-with-first-minima.png" |
|
|
|
/ "3-histogram-of-guard-avarages-filtered-with-first-minima.png" |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_guards(data_frame, settings): |
|
|
|
def check_guards(data_frame, parameters): |
|
|
|
for what in (LEFT_GUARD, RIGHT_GUARD): |
|
|
|
for what in (parameters.left_guard_name, parameters.right_guard_name): |
|
|
|
ok_col = f"{what}.ok" |
|
|
|
ok_col = f"{what}.ok" |
|
|
|
data_frame[ok_col] = False |
|
|
|
data_frame[ok_col] = False |
|
|
|
mask = data_frame[f"{what}.average"] < settings.guard_max_value |
|
|
|
mask = ( |
|
|
|
|
|
|
|
data_frame[f"{what}.{parameters.guard_stats}"] |
|
|
|
|
|
|
|
< parameters.guard_max_value |
|
|
|
|
|
|
|
) |
|
|
|
data_frame.loc[mask, ok_col] = True |
|
|
|
data_frame.loc[mask, ok_col] = True |
|
|
|
data_frame["guards.ok"] = ( |
|
|
|
data_frame["guards.ok"] = ( |
|
|
|
data_frame[f"{LEFT_GUARD}.ok"] & data_frame[f"{RIGHT_GUARD}.ok"] |
|
|
|
data_frame[f"{parameters.left_guard_name}.ok"] |
|
|
|
|
|
|
|
& data_frame[f"{parameters.right_guard_name}.ok"] |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
mask = data_frame["guards.ok"] == True |
|
|
|
mask = data_frame["guards.ok"] == True # noqa: E712 |
|
|
|
guarded_df = data_frame[mask].copy() |
|
|
|
guarded_df = data_frame[mask].copy() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
x="frame", |
|
|
|
x="frame", |
|
|
|
y=f"{ROI_NAME}.average", |
|
|
|
y=parameters.roi_column, |
|
|
|
data=data_frame, |
|
|
|
data=data_frame, |
|
|
|
hue="guards.ok", |
|
|
|
hue="guards.ok", |
|
|
|
hue_order=[True, False], |
|
|
|
hue_order=[True, False], |
|
|
@ -163,13 +186,13 @@ def check_guards(data_frame, settings): |
|
|
|
pyplot.title("Selection based on guard values") |
|
|
|
pyplot.title("Selection based on guard values") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Frame Number [1]") |
|
|
|
path = settings.data_dir / "4-image-selection-based-on-guard-values.png" |
|
|
|
path = parameters.data_dir / "4-image-selection-based-on-guard-values.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
settings.charts_y_limit = ax.get_ylim() |
|
|
|
parameters.charts_y_limit = ax.get_ylim() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
x="frame", y=f"{ROI_NAME}.average", data=guarded_df |
|
|
|
x="frame", y=parameters.roi_column, data=guarded_df |
|
|
|
) |
|
|
|
) |
|
|
|
count_all_images = len(data_frame) |
|
|
|
count_all_images = len(data_frame) |
|
|
|
count_guarded_images = len(guarded_df) |
|
|
|
count_guarded_images = len(guarded_df) |
|
|
@ -181,81 +204,80 @@ def check_guards(data_frame, settings): |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
path = settings.data_dir / "5-selected-values-based-on-guard-values.png" |
|
|
|
path = parameters.data_dir / "5-selected-values-based-on-guard-values.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data_frame |
|
|
|
return data_frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_outliers(data_frame, settings): |
|
|
|
def find_outliers(data_frame, parameters): |
|
|
|
|
|
|
|
|
|
|
|
mask = data_frame["guards.ok"] == True |
|
|
|
mask = data_frame["guards.ok"] == True # noqa: E712 |
|
|
|
guarded_df = data_frame[mask].copy() |
|
|
|
guarded_df = data_frame[mask].copy() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
seaborn.boxplot(data=guarded_df, x=f"{ROI_NAME}.average") |
|
|
|
seaborn.boxplot(data=guarded_df, x=parameters.roi_column) |
|
|
|
pyplot.title(f"Boxblot of guarded values") |
|
|
|
pyplot.title(f"Boxblot of guarded values") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
path = settings.data_dir / "6-boxplot-of-guarded-values.png" |
|
|
|
path = parameters.data_dir / "6-boxplot-of-guarded-values.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
lower_quartil = guarded_df[f"{ROI_NAME}.average"].quantile(0.25) |
|
|
|
lower_quartil = guarded_df[parameters.roi_column].quantile(0.25) |
|
|
|
upper_quartil = guarded_df[f"{ROI_NAME}.average"].quantile(0.75) |
|
|
|
upper_quartil = guarded_df[parameters.roi_column].quantile(0.75) |
|
|
|
inter_quartil_range = upper_quartil - lower_quartil |
|
|
|
inter_quartil_range = upper_quartil - lower_quartil |
|
|
|
settings.outlier_upper_limit = upper_quartil + 1.5 * inter_quartil_range |
|
|
|
parameters.outlier_upper_limit = upper_quartil + 1.5 * inter_quartil_range |
|
|
|
|
|
|
|
|
|
|
|
data_frame["outlier.ok"] = ( |
|
|
|
data_frame["outlier.ok"] = ( |
|
|
|
data_frame[f"{ROI_NAME}.average"] < settings.outlier_upper_limit |
|
|
|
data_frame[parameters.roi_column] < parameters.outlier_upper_limit |
|
|
|
) |
|
|
|
) |
|
|
|
return data_frame |
|
|
|
return data_frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_value_on_guards_and_outliers(data_frame, settings): |
|
|
|
def select_on_guards_and_outliers(data_frame, parameters): |
|
|
|
data_frame["outlier_guards.ok"] = ( |
|
|
|
data_frame["outlier_guards.ok"] = ( |
|
|
|
data_frame["guards.ok"] & data_frame["outlier.ok"] |
|
|
|
data_frame["guards.ok"] & data_frame["outlier.ok"] |
|
|
|
) |
|
|
|
) |
|
|
|
mask = data_frame["outlier_guards.ok"] == True |
|
|
|
mask = data_frame["outlier_guards.ok"] == True # noqa: E712 |
|
|
|
selected_df = data_frame[mask].copy() |
|
|
|
selected_df = data_frame[mask].copy() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
x="frame", y=f"{ROI_NAME}.average", data=selected_df |
|
|
|
x="frame", y=parameters.roi_column, data=selected_df |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.title(f"Selected Images, outliers removed") |
|
|
|
pyplot.title(f"Selected Images, outliers removed") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
path = settings.data_dir / "7-selected-images-outliers-removed.png" |
|
|
|
path = parameters.data_dir / "7-selected-images-outliers-removed.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
return selected_df |
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def smooth_rolling_min(selected_df, settings): |
|
|
|
def smooth_rolling_min(selected_df, parameters): |
|
|
|
rm = ( |
|
|
|
rm = ( |
|
|
|
selected_df[f"{ROI_NAME}.average"] |
|
|
|
selected_df[parameters.roi_column] |
|
|
|
.rolling(settings.rolling_min_window) |
|
|
|
.rolling(parameters.rolling_min_window) |
|
|
|
.min() |
|
|
|
.min() |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# after a rolling window calculation, the first values will be NaN, |
|
|
|
# after a rolling window calculation, the first values will be NaN, |
|
|
|
# we need to fill them |
|
|
|
# we need to fill them |
|
|
|
selected_df[f"{ROI_NAME}.average.rolling.min"] = rm.fillna( |
|
|
|
selected_df[f"{parameters.roi_name}.rolling.min"] = rm.fillna( |
|
|
|
method="backfill" |
|
|
|
method="backfill" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
ax = seaborn.scatterplot( |
|
|
|
x="frame", y=f"{ROI_NAME}.average.rolling.min", data=selected_df |
|
|
|
x="frame", y=f"{parameters.roi_name}.rolling.min", data=selected_df |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.title(f"Selected Images, outliers removed, rolling min applied") |
|
|
|
pyplot.title(f"Selected Images, outliers removed, rolling min applied") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
path = ( |
|
|
|
path = ( |
|
|
|
settings.data_dir |
|
|
|
parameters.data_dir |
|
|
|
/ "8-selected-images-outliers-removed-rolling-min-applied.png" |
|
|
|
/ "8-selected-images-outliers-removed-rolling-min-applied.png" |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
@ -263,17 +285,17 @@ def smooth_rolling_min(selected_df, settings): |
|
|
|
return selected_df |
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def smooth_savgol_filter(selected_df, settings): |
|
|
|
def smooth_savgol_filter(selected_df, parameters): |
|
|
|
filtered = savgol_filter( |
|
|
|
filtered = savgol_filter( |
|
|
|
selected_df[f"{ROI_NAME}.average.rolling.min"], |
|
|
|
selected_df[f"{parameters.roi_name}.rolling.min"], |
|
|
|
settings.savgol_filter_window, |
|
|
|
parameters.savgol_filter_window, |
|
|
|
settings.savgol_filter_polynom, |
|
|
|
parameters.savgol_filter_polynom, |
|
|
|
) |
|
|
|
) |
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"] = filtered |
|
|
|
selected_df[f"{parameters.roi_name}.savgol"] = filtered |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
seaborn.lineplot( |
|
|
|
seaborn.lineplot( |
|
|
|
x="frame", y=f"{ROI_NAME}.average.savgol", data=selected_df |
|
|
|
x="frame", y=f"{parameters.roi_name}.savgol", data=selected_df |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.title( |
|
|
|
pyplot.title( |
|
|
|
( |
|
|
|
( |
|
|
@ -284,7 +306,7 @@ def smooth_savgol_filter(selected_df, settings): |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
path = ( |
|
|
|
path = ( |
|
|
|
settings.data_dir |
|
|
|
parameters.data_dir |
|
|
|
/ "9-selected-images-outliers-removed-rolling-min-savgol-filtered.png" |
|
|
|
/ "9-selected-images-outliers-removed-rolling-min-savgol-filtered.png" |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
@ -292,46 +314,50 @@ def smooth_savgol_filter(selected_df, settings): |
|
|
|
return selected_df |
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_extremas(selected_df, settings): |
|
|
|
def find_extremas(selected_df, parameters): |
|
|
|
|
|
|
|
|
|
|
|
indexes = peakutils.indexes( |
|
|
|
max_indexes = peakutils.indexes( |
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
thres=settings.peak_threshold, |
|
|
|
thres=parameters.peak_threshold, |
|
|
|
min_dist=settings.peak_min_distance, |
|
|
|
min_dist=parameters.peak_min_distance, |
|
|
|
) |
|
|
|
) |
|
|
|
maximas = selected_df.iloc[indexes].copy() |
|
|
|
maximas = selected_df.iloc[max_indexes].copy() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
peakplot_iloc( |
|
|
|
peakplot_iloc( |
|
|
|
selected_df["frame"], |
|
|
|
selected_df["frame"], |
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
indexes, |
|
|
|
max_indexes, |
|
|
|
|
|
|
|
"maxima", |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.title(f"Finding Maximas") |
|
|
|
pyplot.title(f"Finding Maximas") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
path = settings.data_dir / "10-finding-maximas.png" |
|
|
|
path = parameters.data_dir / "10-finding-maximas.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
inverted_series = ( |
|
|
|
inverted_series = ( |
|
|
|
max(selected_df[f"{ROI_NAME}.average.savgol"]) |
|
|
|
max(selected_df[f"{parameters.roi_name}.savgol"]) |
|
|
|
- selected_df[f"{ROI_NAME}.average.savgol"] |
|
|
|
- selected_df[f"{parameters.roi_name}.savgol"] |
|
|
|
) |
|
|
|
) |
|
|
|
indexes = peakutils.indexes( |
|
|
|
min_indexes = peakutils.indexes( |
|
|
|
inverted_series, min_dist=settings.peak_min_distance |
|
|
|
inverted_series, min_dist=parameters.peak_min_distance |
|
|
|
) |
|
|
|
) |
|
|
|
minimas = selected_df.iloc[indexes].copy() |
|
|
|
minimas = selected_df.iloc[min_indexes].copy() |
|
|
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
pyplot.clf() |
|
|
|
peakplot_iloc( |
|
|
|
peakplot_iloc( |
|
|
|
selected_df["frame"], |
|
|
|
selected_df["frame"], |
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
indexes, |
|
|
|
max_indexes, |
|
|
|
|
|
|
|
"maxima", |
|
|
|
|
|
|
|
min_indexes, |
|
|
|
|
|
|
|
"minima", |
|
|
|
) |
|
|
|
) |
|
|
|
pyplot.title(f"Finding Minimas") |
|
|
|
pyplot.title(f"Finding Minimas") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
path = settings.data_dir / "11-finding-minimas.png" |
|
|
|
path = parameters.data_dir / "11-finding-minimas.png" |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
maximas["is_maxima"] = True |
|
|
|
maximas["is_maxima"] = True |
|
|
@ -340,91 +366,38 @@ def find_extremas(selected_df, settings): |
|
|
|
return extremas_df |
|
|
|
return extremas_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_data(data_frame, selected_df, extremas_df, settings): |
|
|
|
def save_data(data_frame, selected_df, extremas_df, parameters): |
|
|
|
path = settings.data_dir / "numeric-data.xlsx" |
|
|
|
path = parameters.data_dir / "numeric-data.xlsx" |
|
|
|
writer = pandas.ExcelWriter(path, engine="xlsxwriter") |
|
|
|
writer = pandas.ExcelWriter(path, engine="xlsxwriter") |
|
|
|
|
|
|
|
|
|
|
|
extremas_df.to_excel(writer, sheet_name="extremas") |
|
|
|
extremas_df.to_excel(writer, sheet_name="extremas") |
|
|
|
selected_df.to_excel(writer, sheet_name="selected data") |
|
|
|
selected_df.to_excel(writer, sheet_name="selected data") |
|
|
|
data_frame.to_excel(writer, sheet_name="raw data") |
|
|
|
data_frame.to_excel(writer, sheet_name="raw data") |
|
|
|
|
|
|
|
|
|
|
|
ignore_settings = {"tif_list", "cuts_dir"} |
|
|
|
ignore_parameters = {"tif_list", "cuts_dir"} |
|
|
|
tmp_settings = { |
|
|
|
tmp_parameters = { |
|
|
|
k: [v] for k, v in settings.items() if k not in ignore_settings |
|
|
|
k: [v] for k, v in parameters.items() if k not in ignore_parameters |
|
|
|
} |
|
|
|
} |
|
|
|
tmp_setings_df = pandas.DataFrame(tmp_settings).T |
|
|
|
tmp_setings_df = pandas.DataFrame(tmp_parameters).T |
|
|
|
tmp_setings_df.to_excel(writer, sheet_name="parameters") |
|
|
|
tmp_setings_df.to_excel(writer, sheet_name="parameters") |
|
|
|
|
|
|
|
|
|
|
|
writer.save() |
|
|
|
writer.save() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def data_workflow(stats_results, settings): |
|
|
|
def save_temp(data_frame, parameters): |
|
|
|
print("3/4: Data analysis") |
|
|
|
csv_path = parameters.tif_dir / "_data.csv" |
|
|
|
set_plotting_styles() |
|
|
|
|
|
|
|
data_frame = construct_data_frame(stats_results) |
|
|
|
|
|
|
|
save_temp(data_frame, settings) |
|
|
|
|
|
|
|
find_guard_value(data_frame, settings) |
|
|
|
|
|
|
|
data_frame = check_guards(data_frame, settings) |
|
|
|
|
|
|
|
data_frame = find_outliers(data_frame, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
selected_df = select_value_on_guards_and_outliers(data_frame, settings) |
|
|
|
|
|
|
|
selected_df = smooth_rolling_min(selected_df, settings) |
|
|
|
|
|
|
|
selected_df = smooth_savgol_filter(selected_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extremas_df = find_extremas(selected_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
save_data(data_frame, selected_df, extremas_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return list(selected_df["file"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_temp(data_frame, settings): |
|
|
|
|
|
|
|
csv_path = settings.tif_dir / "_data.csv" |
|
|
|
|
|
|
|
data_frame.to_csv(csv_path, sep="\t") |
|
|
|
data_frame.to_csv(csv_path, sep="\t") |
|
|
|
xls_path = settings.tif_dir / "_data.xlsx" |
|
|
|
parameters_path = parameters.tif_dir / "_parameters.pickle" |
|
|
|
data_frame.to_excel(xls_path) |
|
|
|
with open(parameters_path, "wb") as fh: |
|
|
|
settings_path = settings.tif_dir / "_settings.pickle" |
|
|
|
pickle.dump(parameters, fh) |
|
|
|
with open(settings_path, "wb") as fh: |
|
|
|
|
|
|
|
pickle.dump(settings, fh) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_temp(folder): |
|
|
|
def load_temp(folder): |
|
|
|
dir_path = pathlib.Path(folder) |
|
|
|
dir_path = pathlib.Path(folder) |
|
|
|
csv_path = dir_path / "_data.csv" |
|
|
|
csv_path = dir_path / "_data.csv" |
|
|
|
data_frame = pandas.read_csv(csv_path, sep="\t", index_col=0) |
|
|
|
data_frame = pandas.read_csv(csv_path, sep="\t", index_col=0) |
|
|
|
settings_path = dir_path / "_settings.pickle" |
|
|
|
parameters_path = dir_path / "_parameters.pickle" |
|
|
|
with open(settings_path, "rb") as fh: |
|
|
|
with open(parameters_path, "rb") as fh: |
|
|
|
settings = pickle.load(fh) |
|
|
|
parameters = pickle.load(fh) |
|
|
|
|
|
|
|
|
|
|
|
settings.cut_pad_x = 50 |
|
|
|
return data_frame, parameters |
|
|
|
settings.cut_pad_y = 25 |
|
|
|
|
|
|
|
settings.guard_histogram_bins = 100 |
|
|
|
|
|
|
|
settings.guard_filter_window = 7 |
|
|
|
|
|
|
|
settings.guard_filter_polynom = 3 |
|
|
|
|
|
|
|
settings.guards_minima_min_dist = 10 |
|
|
|
|
|
|
|
settings.rolling_min_window = 5 |
|
|
|
|
|
|
|
settings.savgol_filter_window = 51 |
|
|
|
|
|
|
|
settings.savgol_filter_polynom = 1 |
|
|
|
|
|
|
|
settings.peak_min_distance = 5 |
|
|
|
|
|
|
|
settings.peak_threshold = 0.3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data_frame, settings |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cached_workflow(folder): |
|
|
|
|
|
|
|
print("3/4: Data analysis") |
|
|
|
|
|
|
|
set_plotting_styles() |
|
|
|
|
|
|
|
data_frame, settings = load_temp(folder) |
|
|
|
|
|
|
|
find_guard_value(data_frame, settings) |
|
|
|
|
|
|
|
data_frame = check_guards(data_frame, settings) |
|
|
|
|
|
|
|
data_frame = find_outliers(data_frame, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
selected_df = select_value_on_guards_and_outliers(data_frame, settings) |
|
|
|
|
|
|
|
selected_df = smooth_rolling_min(selected_df, settings) |
|
|
|
|
|
|
|
selected_df = smooth_savgol_filter(selected_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extremas_df = find_extremas(selected_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
save_data(data_frame, selected_df, extremas_df, settings) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return list(selected_df["file"]), settings |
|
|
|
|
|
|
|