Example #1
0
 def test_reassignment_of_tracename(self):
     filename = "../resources/traces/Trace.txt"
     trace = TraceContainer(filename, loaded_from_ascii=True)
     trace.tracename = self.file_name
     self.assertEqual(trace.get_tracename(), self.file_name)
     self.assertEqual(trace.get_savename(dir_to_join=self.file_dir),
                      self.file_path)
 def test_load_trace_from_ascii_specified_bleach(self):
     filename = "../resources/traces/TraceNoAA.txt"
     trace = TraceContainer(filename)
     self.assertTrue(trace.load_successful)
     self.assertIsInstance(trace.acc.int, np.ndarray)
     self.assertIsInstance(trace.grn.int, np.ndarray)
     self.assertIsInstance(trace.red.int, np.ndarray)
     self.assertEqual(
         (57, None, 57), trace.get_bleaches()
     )  # expect bleach to propagate all channels
 def test_load_trace_from_dat(self):
     filename = "../resources/traces/kinsoftTrace.dat"
     trace = TraceContainer(filename)
     self.assertTrue(trace.load_successful)
     self.assertIsInstance(trace.acc.int, np.ndarray)
     self.assertIsInstance(trace.grn.int, np.ndarray)
     self.assertIsInstance(trace.red.int, np.ndarray)
     self.assertTrue(np.isnan(trace.red.int[0]))
 def newTraceFromVideo(self, n) -> TraceContainer:
     """
     Shortcut to obtain trace, and create a new one if it doesn't exist.
     """
     tracename = self.currName + "_" + str(n)
     if tracename not in self.data.traces:
         self.data.traces[tracename] = TraceContainer(filename=tracename,
                                                      video=self.currName,
                                                      n=n)
     return self.data.traces[tracename]
 def test_save_and_load_trace(self):
     self.addCleanup(os.remove, self.file_path)
     filename = "../resources/traces/fiddler_3dim_0.txt"
     trace = TraceContainer(filename)
     trace.tracename = self.file_path
     trace.export_trace_to_txt()
     trace2 = TraceContainer(self.file_path)
     np.testing.assert_array_almost_equal(trace.grn.int, trace2.grn.int)
     self.assertEqual(trace.first_bleach, trace2.first_bleach)
Example #6
0
 def test_save_and_load_trace(self):
     self.addCleanup(os.remove, self.file_path)
     filename = "../resources/traces/Trace.txt"
     trace = TraceContainer(filename, loaded_from_ascii=True)
     trace.tracename = self.file_name
     trace.export_trace_to_txt(dir_to_join=self.file_dir)
     trace2 = TraceContainer(self.file_path, loaded_from_ascii=True)
     self.assertTrue(trace2.load_successful)
     np.testing.assert_array_almost_equal(trace.grn.int, trace2.grn.int)
     self.assertEqual(trace.first_bleach, trace2.first_bleach)
 def test_save_and_load_trace_dat(self):
     self.addCleanup(os.remove, self.file_path)
     filename = "../resources/traces/kinsoftTrace.dat"
     trace = TraceContainer(filename)
     trace.tracename = self.file_path
     trace.export_trace_to_txt()
     trace2 = TraceContainer(self.file_path)
     np.testing.assert_allclose(trace.grn.int, trace2.grn.int, rtol=1e-06)
     np.testing.assert_allclose(trace.acc.int, trace2.acc.int, rtol=1e-06)
     np.testing.assert_allclose(trace.red.int, trace2.red.int, rtol=1e-06)
     self.assertEqual(trace.first_bleach, trace2.first_bleach)
     self.assertEqual(trace.n, trace2.n)
Example #8
0
 def test_save_and_load_trace_dat(self):
     self.addCleanup(os.remove, self.file_path)
     filename = "../resources/traces/kinsoftTrace.dat"
     trace = TraceContainer(filename, loaded_from_ascii=True)
     trace.tracename = self.file_name
     trace.export_trace_to_txt(dir_to_join=self.file_dir)
     trace2 = TraceContainer(self.file_path, loaded_from_ascii=True)
     self.assertTrue(trace2.load_successful)
     self.assertEqual(type(trace.grn.int), type(trace2.grn.int))
     self.assertEqual(type(trace.acc.int), type(trace2.acc.int))
     self.assertEqual(type(trace.red.int), type(trace2.red.int))
     np.testing.assert_allclose(trace.grn.int, trace2.grn.int, rtol=1e-06)
     np.testing.assert_allclose(trace.acc.int, trace2.acc.int, rtol=1e-06)
     np.testing.assert_allclose(trace.red.int, trace2.red.int, rtol=1e-06)
     self.assertEqual(trace.first_bleach, trace2.first_bleach)
     self.assertEqual(trace.n, trace2.n)
    def test_reducing_trace_save_and_load(self):
        self.addCleanup(os.remove, self.file_path)
        filename = "../resources/traces/fiddler_3dim_0.txt"
        trace = TraceContainer(filename)
        trace.red.int[:] = None
        trace.red.bg[:] = None
        trace.stoi[:] = None
        trace.tracename = self.file_path
        trace.export_trace_to_txt()
        trace2 = TraceContainer(self.file_path)

        self.assertEqual(trace.first_bleach, trace2.first_bleach)

        np.testing.assert_allclose(trace.grn.int, trace2.grn.int)
        np.testing.assert_allclose(trace.grn.bg, trace2.grn.bg)
        np.testing.assert_allclose(trace.acc.int, trace2.acc.int)
        np.testing.assert_allclose(trace.acc.bg, trace2.acc.bg)
        np.testing.assert_allclose(trace.red.int, trace2.red.int)
        np.testing.assert_allclose(trace.red.bg, trace2.red.bg)
        np.testing.assert_allclose(trace.stoi, trace2.stoi)
Example #10
0
    def test_reducing_trace_save_and_load(self):
        self.addCleanup(os.remove, self.file_path)
        filename = "../resources/traces/Trace.txt"
        trace = TraceContainer(filename, loaded_from_ascii=True)
        trace.red.int[:] = None
        trace.red.bg[:] = None
        trace.stoi[:] = None
        trace.tracename = self.file_name
        trace.export_trace_to_txt(dir_to_join=self.file_dir)
        trace2 = TraceContainer(self.file_path, loaded_from_ascii=True)
        self.assertTrue(trace2.load_successful)
        self.assertEqual(trace.first_bleach, trace2.first_bleach)

        np.testing.assert_allclose(trace.grn.int, trace2.grn.int)
        np.testing.assert_allclose(trace.grn.bg, trace2.grn.bg)
        np.testing.assert_allclose(trace.acc.int, trace2.acc.int)
        np.testing.assert_allclose(trace.acc.bg, trace2.acc.bg)
        np.testing.assert_allclose(trace.red.int, trace2.red.int)
        np.testing.assert_allclose(trace.red.bg, trace2.red.bg)
        np.testing.assert_allclose(trace.stoi, trace2.stoi)
    def test_reducing_trace_calculating_fret(self):
        self.addCleanup(os.remove, self.file_path)
        filename = "../resources/traces/fiddler_3dim_0.txt"
        trace = TraceContainer(filename)
        trace.red.int[:] = None
        trace.red.bg[:] = None
        trace.stoi[:] = None
        df = trace.get_export_df()
        trace.fret[:] = None
        trace.savename = self.file_path
        trace.export_trace_to_txt(keep_nan_columns=False)
        trace2 = TraceContainer(self.file_path)
        df2 = trace2.get_export_df()

        np.testing.assert_array_almost_equal(df["E"], df2["E"])
Example #12
0
    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()
Example #13
0
    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)
Example #14
0
 def test_save_to_right_path(self):
     filename = "../resources/traces/Trace.txt"
     trace = TraceContainer(filename, loaded_from_ascii=True)
     trace.tracename = self.file_name
     trace.export_trace_to_txt(dir_to_join=self.file_dir)
     self.assertTrue(os.path.exists(self.file_path))
Example #15
0
    def test_reducing_trace_calculating_fret(self):
        self.addCleanup(os.remove, self.file_path)
        filename = "../resources/traces/Trace.txt"
        trace = TraceContainer(filename, loaded_from_ascii=True)
        trace.red.int[:] = None
        trace.red.bg[:] = None
        trace.stoi[:] = None
        df = trace.get_export_df()
        trace.fret[:] = None
        trace.tracename = self.file_name
        trace.export_trace_to_txt(dir_to_join=self.file_dir,
                                  keep_nan_columns=False)
        trace2 = TraceContainer(self.file_path, loaded_from_ascii=True)
        self.assertTrue(trace2.load_successful)
        df2 = trace2.get_export_df()

        np.testing.assert_array_almost_equal(df["E"], df2["E"])
Example #16
0
 def test_loading_nonALEX_iSMS_trace(self):
     filename = "../resources/traces/iSMSNoALEX.txt"
     trace = TraceContainer(filename, loaded_from_ascii=True)
     self.assertTrue(trace.load_successful)