def openFile(self): """ Open file to load in. """ if not self.getConfig(gvars.key_batchLoadingMode): directory = self.getLastOpenedDir() filenames, selectedFilter = QFileDialog.getOpenFileNames( self, caption="Open File", filter="Video files (*.tif *fits)", directory=directory, ) if len(filenames) > 0: progressbar = ProgressBar(loop_len=len(filenames), parent=self) for i, full_filename in enumerate(filenames): if progressbar.wasCanceled(): break # Make sure name is unique uniqueName = lib.utils.generate_unique_name( full_filename=full_filename, array=self.data.videos.keys(), ) self.data.load_video_data( path=full_filename, name=uniqueName, alex=self.getConfig(gvars.key_alexEnabled), view_setup=self.getConfig(gvars.key_viewSetup), donor_is_first=self.donor_first, donor_is_left=self.donor_is_left, bg_correction=self.bg_correction, ) item = QStandardItem(uniqueName) item.setCheckable(False) self.currName = uniqueName self.currDir = os.path.dirname(full_filename) self.listModel.appendRow(item) # refresh listView self.listView.repaint() progressbar.increment() # Select first video if loading into an empty listView if self.currRow is None: self.currRow = 0 self.selectListViewTopRow() index = self.listModel.index(self.currRow, 0) name = self.listModel.data(index) self.currName = name # Write most recently opened directory to getConfig file, # but only if a file was selected if self.currDir is not None: self.setConfig(gvars.key_lastOpenedDir, self.currDir) else: self.batchOpen()
def colocalizeSpotsAllVideos(self): """ Colocalizes spots for all videos, with the same threshold. Use this method instead for progress bar. """ progressbar = ProgressBar(loop_len=len(self.data.videos.keys()), parent=self) for name in self.data.videos.keys(): self.currName = name for c in "green", "red": self.colocalizeSpotsSingleVideo(c) progressbar.increment() self.resetCurrentName() self.refreshPlot()
def predict_batch(X, model, batch_size=256, progressbar: ProgressBar = None): """ Predicts on batches in a loop """ batches = (X.shape[0] // batch_size) + 1 y_pred = [] for i in range(batches): i1 = i * batch_size i2 = i1 + batch_size print(X[i1:i2].shape) y_pred.append(model.predict_on_batch(X[i1:i2])) if progressbar is not None: progressbar.increment() if progressbar.wasCanceled(): break return np.row_stack(y_pred)
def generateTraces(self, examples: bool): """Generate traces to show in the GUI (examples) or for export""" n_traces = self.n_examples if examples else self.n_traces if n_traces > 50: # every number of traces gets 20 updates in total # but closes if less update_every_nth = n_traces // 20 progressbar = ProgressBar( parent=self, loop_len=n_traces / update_every_nth ) else: update_every_nth = None progressbar = None if examples: self.data.example_traces.clear() else: self.data.simulated_traces.clear() df = lib.math.generate_traces( n_traces=n_traces, aa_mismatch=self.aa_mismatch, state_means=self.fret_means, min_state_diff=self.min_fret_diff, random_k_states_max=self.max_random_states, max_aggregate_size=self.max_aggregate_size, aggregation_prob=self.aggregate_prob, scramble_prob=self.scramble_prob, scramble_decouple_prob=self.scramble_decouple_prob, trace_length=self.trace_len, trans_prob=self.transition_prob, blink_prob=self.blinking_prob, bleed_through=self.bleed_through, noise=self.noise, D_lifetime=self.donor_lifetime, A_lifetime=self.acceptor_lifetime, au_scaling_factor=self.scaling_factor, falloff_prob=self.fall_off_prob, falloff_lifetime=self.fall_off_lifetime, discard_unbleached=False, progressbar_callback=progressbar, callback_every=update_every_nth, return_matrix=False, reduce_memory=False, run_headless_parallel=False, merge_state_labels=True, ) df.index = np.arange(0, len(df), 1) // int(self.trace_len) for n, (idx, trace_df) in enumerate(df.groupby(df.index)): self.data.simulated_traces[idx] = TraceContainer( filename="trace_{}.txt".format(idx), loaded_from_ascii=False, n=idx, ) self.getTrace(idx).set_from_df(df=trace_df) if progressbar is not None: progressbar.close()
def batchOpen(self): """Loads one video at a time and extracts traces, then clears the video from memory afterwards""" directory = self.getLastOpenedDir() trace_window = self.windows[gvars.TraceWindow] # type: TraceWindow filenames, selectedFilter = QFileDialog.getOpenFileNames( self, caption="Open File", filter="Video files (*.tif *.fits)", directory=directory, ) if len(filenames) > 0: self.processEvents() progressbar = ProgressBar(loop_len=len(filenames), parent=self) for i, full_filename in enumerate(filenames): if progressbar.wasCanceled(): break else: progressbar.increment() self.currName = os.path.basename(full_filename) if self.currName in self.data.videos: continue self.data.load_video_data( path=full_filename, name=os.path.basename(full_filename), donor_is_first=self.donor_first, donor_is_left=self.donor_is_left, bg_correction=self.bg_correction, ) channels = ("green", "red") for c in channels: if self.currentVideo().acc.exists: self.colocalizeSpotsSingleVideo(channel=c, find_npairs="auto") else: self.colocalizeSpotsSingleVideo(channel=c, find_npairs="spinbox") self.getTracesSingleVideo() self.currentVideo().vid = None for c in self.currentVideo().channels + ( self.currentVideo().acc, ): c.raw = None self.listModel.appendRow(QStandardItem(self.currName)) self.listView.repaint() self.currDir = os.path.dirname(full_filename) if self.currDir is not None: self.setConfig(gvars.key_lastOpenedDir, self.currDir) if len(self.data.traces) > 0: currently_loaded = trace_window.returnCurrentListViewNames() # Iterate over all filenames and add to list for name in self.data.traces.keys(): # If name is already in list, skip it if name in currently_loaded: continue item = QStandardItem(name) item.setCheckable(True) trace_window.listModel.appendRow(item) trace_window.selectListViewTopRow() trace_window.getCurrentListObject() trace_window.show() self.batchLoaded = True self.refreshInterface() self.selectListViewTopRow() self.refreshPlot()
def classifyTraces(self, single=False, checked_only=False): """ Classifies checked traces with deep learning model. """ self.processEvents() alpha = self.getConfig(gvars.key_alphaFactor) delta = self.getConfig(gvars.key_deltaFactor) if single: traces = [self.currentTrace()] if traces == [None]: traces.clear() elif checked_only: traces = [ trace for trace in self.data.traces.values() if trace.is_checked ] else: traces = [trace for trace in self.data.traces.values()] if len(traces) > 0: batch_size = 256 batches = (len(traces) // batch_size) + 1 progressbar = ProgressBar(loop_len=batches, parent=self) if not single: all_lengths_eq = lib.math.all_equal( [trace.frames_max for trace in traces]) all_features_eq = lib.math.all_equal( [lib.math.contains_nan(trace.red.int) for trace in traces]) else: all_lengths_eq = False all_features_eq = None if all((all_lengths_eq, all_features_eq)): # shape is (n_traces) if traces have uneven length X = np.array([ lib.math.correct_DA(trace.get_intensities(), alpha=alpha, delta=delta) for trace in traces ]) # Swap from (samples, features, time) to # (samples, time, features) X = np.swapaxes(X, 1, 2) X = (X[..., [1, 2]] if lib.math.contains_nan(X[..., -1]) else X[..., [1, 2, 3]]) # Normalize tensor X = lib.math.sample_max_normalize_3d(X) # Fix single sample dimension if len(X.shape) == 2: X = X[np.newaxis, :, :] model = (self.keras_two_channel_model if X.shape[-1] == 2 else self.keras_three_channel_model) Y = lib.math.predict_batch( X=X, model=model, progressbar=progressbar, batch_size=batch_size, ) else: Y = [] for n, trace in enumerate(traces): xi = np.column_stack( lib.math.correct_DA(trace.get_intensities(), alpha=alpha, delta=delta)) if lib.math.contains_nan(xi[..., -1]): model = self.keras_two_channel_model xi = xi[..., [1, 2]] else: model = self.keras_three_channel_model xi = xi[..., [1, 2, 3]] xi = lib.math.sample_max_normalize_3d(X=xi) yi = lib.math.predict_single(xi=xi, model=model) Y.append(yi) if n % batch_size == 0: progressbar.increment() for n, trace in enumerate(traces): self.setClassifications(trace=trace, yi_pred=Y[n]) self.resetCurrentName()
def openFile(self, *args): """ Loads ASCII files directly into the TraceWindow. """ directory = self.getLastOpenedDir() filenames, selectedFilter = QFileDialog.getOpenFileNames( self, caption="Open File", filter="Trace files (*.txt *.dat)", directory=directory, ) if len(filenames) != 0: select_top_row = True if len(self.data.traces) == 0 else False update_every_n = int(10) if len(filenames) > 10 else int(2) progressbar = ProgressBar(loop_len=len(filenames) / update_every_n, parent=self) rows = [] for n, full_filename in enumerate(filenames): if progressbar.wasCanceled(): break self.currDir = os.path.dirname(full_filename) try: newTrace = TraceContainer(filename=full_filename, loaded_from_ascii=True) except AttributeError: # if a non-trace file was selected warnings.warn( f"This file could not be read: \n{full_filename}", UserWarning, ) continue if (n % update_every_n) == 0: progressbar.increment() # If file wasn't loaded properly, skip if newTrace.load_successful is False: continue # Don't load duplicates if newTrace.name in self.data.traces.keys(): continue self.data.traces[newTrace.name] = newTrace item = QStandardItem(newTrace.name) rows.append(item) # Don't touch the GUI until loading is done [self.listModel.appendRow(row) for row in rows] [ self.listModel.item(i).setCheckable(True) for i in range(self.listModel.rowCount()) ] self.listView.repaint() progressbar.close() if select_top_row: self.selectListViewTopRow() self.getCurrentListObject() self.setConfig(gvars.key_lastOpenedDir, self.currDir)