From c60307ef56dd018798cefe8606b559a47aa67baa Mon Sep 17 00:00:00 2001 From: "Houman.M_Dastjerdi" Date: Thu, 11 Jul 2024 17:07:58 +0200 Subject: [PATCH] Fixed bugs in histogram plot --- piscat/GUI/Projects/tab_localization.py | 51 ++++++++++++++- piscat/GUI/Projects/tab_plot_histogram.py | 75 +++++++++++++++++++++-- piscat/Trajectory/temporal_filtering.py | 40 +++++++----- 3 files changed, 143 insertions(+), 23 deletions(-) diff --git a/piscat/GUI/Projects/tab_localization.py b/piscat/GUI/Projects/tab_localization.py index 7fb7c81..60e2ad8 100644 --- a/piscat/GUI/Projects/tab_localization.py +++ b/piscat/GUI/Projects/tab_localization.py @@ -136,12 +136,14 @@ def createThirdExclusiveGroup(self): def createFourthExclusiveGroup(self): self.checkbox_filter_double_PSF = QtWidgets.QCheckBox("Filter dense PSFs", self) + self.checkbox_filter_border = QtWidgets.QCheckBox("Filter border PSFs", self) self.checkbox_filter_side_lobes_PSF = QtWidgets.QCheckBox("Filter side lobes PSFs", self) self.checkbox_filter_asymmetry_PSF = QtWidgets.QCheckBox("Filter asymmetry PSFs", self) self.checkbox_2DFitting = QtWidgets.QCheckBox("2D Gaussian Fitting", self) self.checkbox_crappy_frames = QtWidgets.QCheckBox("Filter outlier frames", self) self.checkbox_filter_asymmetry_PSF.toggled.connect(lambda: self.add_line_asymmetry_PSF()) + self.checkbox_filter_border.toggled.connect(lambda: self.add_line_border_margin_PSF()) self.checkbox_2DFitting.toggled.connect(lambda: self.add_line_2DFitting()) self.checkbox_crappy_frames.toggled.connect(lambda: self.add_line_crappy_frames()) @@ -155,7 +157,8 @@ def createFourthExclusiveGroup(self): self.grid_filters.addWidget(self.checkbox_filter_side_lobes_PSF, 2, 0) self.grid_filters.addWidget(self.checkbox_filter_asymmetry_PSF, 3, 0) self.grid_filters.addWidget(self.checkbox_2DFitting, 4, 0) - self.grid_filters.addWidget(self.btn_filtering, 5, 0) + self.grid_filters.addWidget(self.checkbox_filter_border, 5, 0) + self.grid_filters.addWidget(self.btn_filtering, 6, 0) self.groupBox_filters.setLayout(self.grid_filters) @@ -177,6 +180,21 @@ def add_line_crappy_frames(self): layout.widget().deleteLater() self.grid_filters.removeItem(layout) + def add_line_border_margin_PSF(self): + if self.checkbox_filter_border.isChecked(): + self.line_border_margin = QtWidgets.QLineEdit(self) + self.line_border_margin.setPlaceholderText("border margin") + self.line_border_margin_label = QtWidgets.QLabel("border margin (px):") + + self.grid_filters.addWidget(self.line_border_margin_label, 5, 1) + self.grid_filters.addWidget(self.line_border_margin, 5, 2) + + if not self.checkbox_filter_border.isChecked(): + for i_ in range(1, 5, 1): + layout = self.grid_filters.itemAtPosition(5, i_) + if layout is not None: + layout.widget().deleteLater() + self.grid_filters.removeItem(layout) def add_line_asymmetry_PSF(self): if self.checkbox_filter_asymmetry_PSF.isChecked(): self.line_asymmetry_PSF = QtWidgets.QLineEdit(self) @@ -524,6 +542,19 @@ def get_values_crappy_frames(self): self.empty_value_box_flag = False + def get_values_border_filtering(self): + try: + self.border_margin = int(self.line_border_margin.text()) + self.empty_value_box_flag = True + + except: + self.msg_box3 = QtWidgets.QMessageBox() + self.msg_box3.setWindowTitle("Warning!") + self.msg_box3.setText("Please filled all parameters!") + self.msg_box3.exec_() + + self.empty_value_box_flag = False + def get_values_asymmetry_filtering(self): try: self.scale = int(self.line_asymmetry_PSF.text()) @@ -802,6 +833,22 @@ def do_Filtering(self): ): pass + if self.checkbox_filter_border.isChecked(): + self.get_values_border_filtering() + if self.empty_value_box_flag: + self.df_PSFs_s_filter = self.df_PSFs_s_filter[(self.df_PSFs_s_filter['x'] > self.border_margin) & + (self.df_PSFs_s_filter['x'] < (self.input_video.shape[ + 2] - self.border_margin)) & + (self.df_PSFs_s_filter['y'] > self.border_margin) & + (self.df_PSFs_s_filter['y'] < (self.input_video.shape[ + 1] - self.border_margin))] + + self.setting_localization["Flag_border_PSFs_filter"] = True + self.setting_localization["border_margin"] = self.border_margin + + self.PSFs_Particels_num["#PSFs_after_border_PSFs_filter"] = self.df_PSFs_s_filter.shape[0] + self.empty_value_box_flag = False + if self.checkbox_2DFitting.isChecked(): self.get_values_2DFitting() if self.empty_value_box_flag: @@ -840,6 +887,8 @@ def do_Filtering(self): ] = self.df_PSFs_s_filter.shape[0] self.empty_value_box_flag = False + + self.update_localization.emit(self.df_PSFs_s_filter) self.output_setting_Tab_Localization.emit(self.setting_localization) self.output_number_PSFs_tracking.emit(self.PSFs_Particels_num) diff --git a/piscat/GUI/Projects/tab_plot_histogram.py b/piscat/GUI/Projects/tab_plot_histogram.py index a4fbb30..a298f2b 100644 --- a/piscat/GUI/Projects/tab_plot_histogram.py +++ b/piscat/GUI/Projects/tab_plot_histogram.py @@ -1,6 +1,8 @@ from PySide6 import QtCore, QtWidgets from piscat.Analysis.plot_protein_histogram import PlotProteinHistogram +import os +import time class Histogram_GUI(QtWidgets.QWidget): @@ -29,6 +31,10 @@ def __init__(self, parent=None): self.plot_hist.clicked.connect(self.do_hist) self.plot_hist.setFixedWidth(150) + self.save_hist = QtWidgets.QPushButton("save_histogram") + self.save_hist.clicked.connect(self.save_histogram) + self.save_hist.setFixedWidth(150) + self.le_lower_contrast_trim = QtWidgets.QLineEdit() self.le_lower_contrast_trim.setPlaceholderText("lower_limitation") self.le_lower_contrast_trim_label = QtWidgets.QLabel("lower limitation (Contrast):") @@ -69,6 +75,7 @@ def __init__(self, parent=None): self.grid.addWidget(self.createFirstExclusiveGroup(), 0, 0) self.grid.addWidget(self.createSecondExclusiveGroup(), 1, 0) self.grid.addWidget(self.plot_hist, 2, 0) + self.grid.addWidget(self.save_hist, 3, 0) self.setLayout(self.grid) @@ -114,8 +121,8 @@ def do_hist(self): self.get_values_histogram_plot() if self.empty_value_box_flag: if self.groupBox_gmm.isChecked(): - his_ = PlotProteinHistogram(intersection_display_flag=False) - his_( + self.his_ = PlotProteinHistogram(intersection_display_flag=False) + self.his_( folder_name="", particles=self.input_data, batch_size=self.batch_size, @@ -124,7 +131,7 @@ def do_hist(self): MinPeakProminence=0, ) - his_.plot_histogram( + self.his_.plot_histogram( bins=self.n_bins, upper_limitation=self.upper_limit, lower_limitation=self.lower_limit, @@ -134,9 +141,20 @@ def do_hist(self): Flag_GMM_fit=True, max_n_components=self.max_n_components, ) + + # + # self.his_. plot_fit_histogram(bins=self.n_bins, + # upper_limitation=self.upper_limit, + # lower_limitation=self.lower_limit, + # step_range=self.step_limit, + # face="g", + # edge="y", + # Flag_GMM_fit=True, + # max_n_components=self.max_n_components, + # ) else: - his_ = PlotProteinHistogram(intersection_display_flag=False) - his_( + self.his_ = PlotProteinHistogram(intersection_display_flag=False) + self.his_( folder_name="", particles=self.input_data, batch_size=self.batch_size, @@ -144,7 +162,7 @@ def do_hist(self): MinPeakWidth=self.minPeak_width, MinPeakProminence=0, ) - his_.plot_histogram( + self.his_.plot_histogram( bins=self.n_bins, upper_limitation=self.upper_limit, lower_limitation=self.lower_limit, @@ -154,8 +172,53 @@ def do_hist(self): Flag_GMM_fit=False, max_n_components=self.max_n_components, ) + + # self.his_.plot_fit_histogram(bins=self.n_bins, + # upper_limitation=self.upper_limit, + # lower_limitation=self.lower_limit, + # step_range=self.step_limit, + # face="g", + # edge="y", + # Flag_GMM_fit=False, + # max_n_components=self.max_n_components, + # ) + self.empty_value_box_flag = False + def save_histogram(self): + + self.file_path = False + self.file_path = QtWidgets.QFileDialog.getExistingDirectory( + self, "Select Folder", os.path.expanduser("~"), QtWidgets.QFileDialog.ShowDirsOnly + ) + timestr = time.strftime("%Y%m%d-%H%M%S") + name_mkdir = timestr + try: + dr_mk = os.path.join(self.file_path, name_mkdir) + os.mkdir(dr_mk) + print("Directory ", name_mkdir, " Created ") + except FileExistsError: + dr_mk = os.path.join(self.file_path, name_mkdir) + print("Directory ", name_mkdir, " already exists") + + self.file_path = dr_mk + + if self.file_path: + if self.groupBox_gmm.isChecked(): + self.his_.save_hist_data(self.file_path, + name='plot_hist_', + upper_limitation=self.upper_limit, + lower_limitation=self.lower_limit, + Flag_GMM_fit=True, + max_n_components=self.max_n_components) + else: + self.his_.save_hist_data(self.file_path, + name='plot_hist.h5', + upper_limitation=self.upper_limit, + lower_limitation=self.lower_limit, + Flag_GMM_fit=False, + max_n_components=self.max_n_components) + def get_values_histogram_plot(self): try: self.minPeak_width = float(self.min_peak_width.text()) diff --git a/piscat/Trajectory/temporal_filtering.py b/piscat/Trajectory/temporal_filtering.py index b0851c8..49248ae 100644 --- a/piscat/Trajectory/temporal_filtering.py +++ b/piscat/Trajectory/temporal_filtering.py @@ -184,28 +184,36 @@ def v_profile(self, df_PSFs, window_size=2000): start_frame = np.max([0, first_frame - window_size]) end_frame = np.min([self.video.shape[0], last_frame + window_size]) - - particle_center_intensity_follow_backward = self.video[ - int(start_frame) : int(first_frame), y_first_frame, x_first_frame - ] - particle_center_intensity_follow_forward = self.video[ - int(last_frame) : int(end_frame), y_last_frame, x_last_frame - ] - - particle_center_intensity_follow = np.concatenate( - ( - particle_center_intensity_follow_backward, - particle_center_intensity, - particle_center_intensity_follow_forward, - ), - axis=0, - ) + particle_center_intensity = [] + for f_, x_, y_ in zip(particle_frame, particle_X, particle_Y): + particle_center_intensity.append(self.video[int(f_), int(y_), int(x_)]) + + particle_center_intensity_follow = self.video[start_frame:end_frame, y_first_frame, x_first_frame] + particle_frame_follow = list(range(int(start_frame), int(end_frame))) + + + # particle_center_intensity_follow_backward = self.video[ + # int(start_frame) : int(first_frame), y_first_frame, x_first_frame + # ] + # particle_center_intensity_follow_forward = self.video[ + # int(last_frame) : int(end_frame), y_last_frame, x_last_frame + # ] + + # particle_center_intensity_follow = np.concatenate( + # ( + # particle_center_intensity_follow_backward, + # particle_center_intensity, + # particle_center_intensity_follow_forward, + # ), + # axis=0, + # ) trajectories = [] trajectories.append(intensity_horizontal) trajectories.append(intensity_vertical) trajectories.append(particle_center_intensity) trajectories.append(particle_center_intensity_follow) trajectories.append(particle_frame) + trajectories.append(particle_frame_follow) trajectories.append(particle_sigma) trajectories.append(particle_X) trajectories.append(particle_Y)