|
|
|
@ -6,13 +6,12 @@ import pickle
@@ -6,13 +6,12 @@ import pickle
|
|
|
|
|
import seaborn |
|
|
|
|
import matplotlib.pyplot as pyplot |
|
|
|
|
|
|
|
|
|
from peakutils.plot import plot as peakplot |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
@ -29,15 +28,26 @@ def peakplot_iloc(x, y, ind):
@@ -29,15 +28,26 @@ def peakplot_iloc(x, y, ind):
|
|
|
|
|
ind : array-like |
|
|
|
|
Indexes of the identified peaks |
|
|
|
|
""" |
|
|
|
|
pyplot.plot(x, y, "--") |
|
|
|
|
pyplot.plot(x, y) |
|
|
|
|
num_items = len(ix1) |
|
|
|
|
pyplot.plot( |
|
|
|
|
x.iloc[ind], |
|
|
|
|
y.iloc[ind], |
|
|
|
|
x.iloc[ix1], |
|
|
|
|
y.iloc[ix1], |
|
|
|
|
"r+", |
|
|
|
|
ms=5, |
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -56,26 +66,31 @@ def set_plotting_styles():
@@ -56,26 +66,31 @@ def set_plotting_styles():
|
|
|
|
|
seaborn.set_context("talk") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_pandas_data_frame(): |
|
|
|
|
def init_pandas_data_frame(parameters): |
|
|
|
|
columns = ["file", "frame"] |
|
|
|
|
for prefix in (LEFT_GUARD, ROI_NAME, RIGHT_GUARD): |
|
|
|
|
for key in STATS_FUNCS: |
|
|
|
|
columns.append(f"{prefix}.{key}") |
|
|
|
|
groups = ( |
|
|
|
|
parameters.roi_name, |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def construct_data_frame(stats_results): |
|
|
|
|
data_frame = init_pandas_data_frame() |
|
|
|
|
def construct_data_frame(stats_results, parameters): |
|
|
|
|
data_frame = init_pandas_data_frame(parameters) |
|
|
|
|
data_frame = data_frame.append(stats_results, ignore_index=True) |
|
|
|
|
return data_frame.sort_values(by=["frame"]).reset_index(drop=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_guard_value(data_frame, settings): |
|
|
|
|
left_avg = data_frame["left.average"] |
|
|
|
|
right_avg = data_frame["right.average"] |
|
|
|
|
guards_avg = left_avg.append(right_avg, ignore_index=True) |
|
|
|
|
def find_guard_threshold(data_frame, parameters): |
|
|
|
|
left_values = data_frame[parameters.left_guard_column] |
|
|
|
|
right_values = data_frame[parameters.right_guard_column] |
|
|
|
|
guard_values = left_values.append(right_values, ignore_index=True) |
|
|
|
|
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_edges = guard_data[1][1:] # edges enclose the counts |
|
|
|
@ -85,13 +100,15 @@ def find_guard_value(data_frame, settings):
@@ -85,13 +100,15 @@ def find_guard_value(data_frame, settings):
|
|
|
|
|
pyplot.title("Histogram of Guard Avarages (not filtered)") |
|
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
guard_counts_filtered = savgol_filter( |
|
|
|
|
guard_counts, |
|
|
|
|
settings.guard_filter_window, |
|
|
|
|
settings.guard_filter_polynom, |
|
|
|
|
parameters.guard_filter_window, |
|
|
|
|
parameters.guard_filter_polynom, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
@ -99,7 +116,7 @@ def find_guard_value(data_frame, settings):
@@ -99,7 +116,7 @@ def find_guard_value(data_frame, settings):
|
|
|
|
|
pyplot.title("Histogram of Guard Avarages (Savitzky-Golay filter)") |
|
|
|
|
pyplot.xlabel("Average Intensity [au]") |
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
# Finding the first minima after the first peak |
|
|
|
@ -114,47 +131,53 @@ def find_guard_value(data_frame, settings):
@@ -114,47 +131,53 @@ def find_guard_value(data_frame, settings):
|
|
|
|
|
inverted_series = max(guard_counts_filtered) - guard_counts_filtered |
|
|
|
|
indexes = peakutils.indexes( |
|
|
|
|
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 |
|
|
|
|
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() |
|
|
|
|
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( |
|
|
|
|
( |
|
|
|
|
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.ylabel("Number of Observations [1]") |
|
|
|
|
path = ( |
|
|
|
|
settings.data_dir |
|
|
|
|
parameters.data_dir |
|
|
|
|
/ "3-histogram-of-guard-avarages-filtered-with-first-minima.png" |
|
|
|
|
) |
|
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_guards(data_frame, settings): |
|
|
|
|
for what in (LEFT_GUARD, RIGHT_GUARD): |
|
|
|
|
def check_guards(data_frame, parameters): |
|
|
|
|
for what in (parameters.left_guard_name, parameters.right_guard_name): |
|
|
|
|
ok_col = f"{what}.ok" |
|
|
|
|
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["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() |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
ax = seaborn.scatterplot( |
|
|
|
|
x="frame", |
|
|
|
|
y=f"{ROI_NAME}.average", |
|
|
|
|
y=parameters.roi_column, |
|
|
|
|
data=data_frame, |
|
|
|
|
hue="guards.ok", |
|
|
|
|
hue_order=[True, False], |
|
|
|
@ -163,13 +186,13 @@ def check_guards(data_frame, settings):
@@ -163,13 +186,13 @@ def check_guards(data_frame, settings):
|
|
|
|
|
pyplot.title("Selection based on guard values") |
|
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
|
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)) |
|
|
|
|
settings.charts_y_limit = ax.get_ylim() |
|
|
|
|
parameters.charts_y_limit = ax.get_ylim() |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
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_guarded_images = len(guarded_df) |
|
|
|
@ -181,81 +204,80 @@ def check_guards(data_frame, settings):
@@ -181,81 +204,80 @@ def check_guards(data_frame, settings):
|
|
|
|
|
) |
|
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
|
path = settings.data_dir / "5-selected-values-based-on-guard-values.png" |
|
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
|
path = parameters.data_dir / "5-selected-values-based-on-guard-values.png" |
|
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
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.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)) |
|
|
|
|
|
|
|
|
|
lower_quartil = guarded_df[f"{ROI_NAME}.average"].quantile(0.25) |
|
|
|
|
upper_quartil = guarded_df[f"{ROI_NAME}.average"].quantile(0.75) |
|
|
|
|
lower_quartil = guarded_df[parameters.roi_column].quantile(0.25) |
|
|
|
|
upper_quartil = guarded_df[parameters.roi_column].quantile(0.75) |
|
|
|
|
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[f"{ROI_NAME}.average"] < settings.outlier_upper_limit |
|
|
|
|
data_frame[parameters.roi_column] < parameters.outlier_upper_limit |
|
|
|
|
) |
|
|
|
|
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["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() |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
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.xlabel("Frame Number [1]") |
|
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
|
path = settings.data_dir / "7-selected-images-outliers-removed.png" |
|
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
|
path = parameters.data_dir / "7-selected-images-outliers-removed.png" |
|
|
|
|
pyplot.savefig(str(path)) |
|
|
|
|
|
|
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def smooth_rolling_min(selected_df, settings): |
|
|
|
|
def smooth_rolling_min(selected_df, parameters): |
|
|
|
|
rm = ( |
|
|
|
|
selected_df[f"{ROI_NAME}.average"] |
|
|
|
|
.rolling(settings.rolling_min_window) |
|
|
|
|
selected_df[parameters.roi_column] |
|
|
|
|
.rolling(parameters.rolling_min_window) |
|
|
|
|
.min() |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
# after a rolling window calculation, the first values will be NaN, |
|
|
|
|
# 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" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
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.xlabel("Frame Number [1]") |
|
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
|
ax.set_ylim(settings.charts_y_limit) |
|
|
|
|
ax.set_ylim(parameters.charts_y_limit) |
|
|
|
|
path = ( |
|
|
|
|
settings.data_dir |
|
|
|
|
parameters.data_dir |
|
|
|
|
/ "8-selected-images-outliers-removed-rolling-min-applied.png" |
|
|
|
|
) |
|
|
|
|
pyplot.savefig(str(path)) |
|
|
|
@ -263,17 +285,17 @@ def smooth_rolling_min(selected_df, settings):
@@ -263,17 +285,17 @@ def smooth_rolling_min(selected_df, settings):
|
|
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def smooth_savgol_filter(selected_df, settings): |
|
|
|
|
def smooth_savgol_filter(selected_df, parameters): |
|
|
|
|
filtered = savgol_filter( |
|
|
|
|
selected_df[f"{ROI_NAME}.average.rolling.min"], |
|
|
|
|
settings.savgol_filter_window, |
|
|
|
|
settings.savgol_filter_polynom, |
|
|
|
|
selected_df[f"{parameters.roi_name}.rolling.min"], |
|
|
|
|
parameters.savgol_filter_window, |
|
|
|
|
parameters.savgol_filter_polynom, |
|
|
|
|
) |
|
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"] = filtered |
|
|
|
|
selected_df[f"{parameters.roi_name}.savgol"] = filtered |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
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( |
|
|
|
|
( |
|
|
|
@ -284,7 +306,7 @@ def smooth_savgol_filter(selected_df, settings):
@@ -284,7 +306,7 @@ def smooth_savgol_filter(selected_df, settings):
|
|
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
|
pyplot.ylabel("Average Intensity [au]") |
|
|
|
|
path = ( |
|
|
|
|
settings.data_dir |
|
|
|
|
parameters.data_dir |
|
|
|
|
/ "9-selected-images-outliers-removed-rolling-min-savgol-filtered.png" |
|
|
|
|
) |
|
|
|
|
pyplot.savefig(str(path)) |
|
|
|
@ -292,46 +314,50 @@ def smooth_savgol_filter(selected_df, settings):
@@ -292,46 +314,50 @@ def smooth_savgol_filter(selected_df, settings):
|
|
|
|
|
return selected_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_extremas(selected_df, settings): |
|
|
|
|
def find_extremas(selected_df, parameters): |
|
|
|
|
|
|
|
|
|
indexes = peakutils.indexes( |
|
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
|
thres=settings.peak_threshold, |
|
|
|
|
min_dist=settings.peak_min_distance, |
|
|
|
|
max_indexes = peakutils.indexes( |
|
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
|
thres=parameters.peak_threshold, |
|
|
|
|
min_dist=parameters.peak_min_distance, |
|
|
|
|
) |
|
|
|
|
maximas = selected_df.iloc[indexes].copy() |
|
|
|
|
maximas = selected_df.iloc[max_indexes].copy() |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
peakplot_iloc( |
|
|
|
|
selected_df["frame"], |
|
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
|
indexes, |
|
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
|
max_indexes, |
|
|
|
|
"maxima", |
|
|
|
|
) |
|
|
|
|
pyplot.title(f"Finding Maximas") |
|
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
inverted_series = ( |
|
|
|
|
max(selected_df[f"{ROI_NAME}.average.savgol"]) |
|
|
|
|
- selected_df[f"{ROI_NAME}.average.savgol"] |
|
|
|
|
max(selected_df[f"{parameters.roi_name}.savgol"]) |
|
|
|
|
- selected_df[f"{parameters.roi_name}.savgol"] |
|
|
|
|
) |
|
|
|
|
indexes = peakutils.indexes( |
|
|
|
|
inverted_series, min_dist=settings.peak_min_distance |
|
|
|
|
min_indexes = peakutils.indexes( |
|
|
|
|
inverted_series, min_dist=parameters.peak_min_distance |
|
|
|
|
) |
|
|
|
|
minimas = selected_df.iloc[indexes].copy() |
|
|
|
|
minimas = selected_df.iloc[min_indexes].copy() |
|
|
|
|
|
|
|
|
|
pyplot.clf() |
|
|
|
|
peakplot_iloc( |
|
|
|
|
selected_df["frame"], |
|
|
|
|
selected_df[f"{ROI_NAME}.average.savgol"], |
|
|
|
|
indexes, |
|
|
|
|
selected_df[f"{parameters.roi_name}.savgol"], |
|
|
|
|
max_indexes, |
|
|
|
|
"maxima", |
|
|
|
|
min_indexes, |
|
|
|
|
"minima", |
|
|
|
|
) |
|
|
|
|
pyplot.title(f"Finding Minimas") |
|
|
|
|
pyplot.xlabel("Frame Number [1]") |
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
maximas["is_maxima"] = True |
|
|
|
@ -340,91 +366,38 @@ def find_extremas(selected_df, settings):
@@ -340,91 +366,38 @@ def find_extremas(selected_df, settings):
|
|
|
|
|
return extremas_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_data(data_frame, selected_df, extremas_df, settings): |
|
|
|
|
path = settings.data_dir / "numeric-data.xlsx" |
|
|
|
|
def save_data(data_frame, selected_df, extremas_df, parameters): |
|
|
|
|
path = parameters.data_dir / "numeric-data.xlsx" |
|
|
|
|
writer = pandas.ExcelWriter(path, engine="xlsxwriter") |
|
|
|
|
|
|
|
|
|
extremas_df.to_excel(writer, sheet_name="extremas") |
|
|
|
|
selected_df.to_excel(writer, sheet_name="selected data") |
|
|
|
|
data_frame.to_excel(writer, sheet_name="raw data") |
|
|
|
|
|
|
|
|
|
ignore_settings = {"tif_list", "cuts_dir"} |
|
|
|
|
tmp_settings = { |
|
|
|
|
k: [v] for k, v in settings.items() if k not in ignore_settings |
|
|
|
|
ignore_parameters = {"tif_list", "cuts_dir"} |
|
|
|
|
tmp_parameters = { |
|
|
|
|
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") |
|
|
|
|
|
|
|
|
|
writer.save() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def data_workflow(stats_results, settings): |
|
|
|
|
print("3/4: Data analysis") |
|
|
|
|
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" |
|
|
|
|
def save_temp(data_frame, parameters): |
|
|
|
|
csv_path = parameters.tif_dir / "_data.csv" |
|
|
|
|
data_frame.to_csv(csv_path, sep="\t") |
|
|
|
|
xls_path = settings.tif_dir / "_data.xlsx" |
|
|
|
|
data_frame.to_excel(xls_path) |
|
|
|
|
settings_path = settings.tif_dir / "_settings.pickle" |
|
|
|
|
with open(settings_path, "wb") as fh: |
|
|
|
|
pickle.dump(settings, fh) |
|
|
|
|
parameters_path = parameters.tif_dir / "_parameters.pickle" |
|
|
|
|
with open(parameters_path, "wb") as fh: |
|
|
|
|
pickle.dump(parameters, fh) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_temp(folder): |
|
|
|
|
dir_path = pathlib.Path(folder) |
|
|
|
|
csv_path = dir_path / "_data.csv" |
|
|
|
|
data_frame = pandas.read_csv(csv_path, sep="\t", index_col=0) |
|
|
|
|
settings_path = dir_path / "_settings.pickle" |
|
|
|
|
with open(settings_path, "rb") as fh: |
|
|
|
|
settings = pickle.load(fh) |
|
|
|
|
|
|
|
|
|
settings.cut_pad_x = 50 |
|
|
|
|
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 |
|
|
|
|
parameters_path = dir_path / "_parameters.pickle" |
|
|
|
|
with open(parameters_path, "rb") as fh: |
|
|
|
|
parameters = pickle.load(fh) |
|
|
|
|
|
|
|
|
|
return data_frame, parameters |
|
|
|
|