Esempio n. 1
0
def test_naive_fitter_single_fits():
    for fitter_class in profile_fitters.naive_fitters.values():
        ground = get_ground(fitter_class)
        p = np.random.poisson(fitter_class._model_function(ground, X))
        lp = rois.LineProfile(distance=X, profile=p)
        results = fitter_class.fit_single_profile(lp)
        np.testing.assert_allclose(results['fitResults'][0].tolist(), ground, atol=5, rtol=0.15)
Esempio n. 2
0
 def _load_profiles_from_list(self, ragged):
     for lp in ragged:
         if isinstance(lp, rois.LineProfile):
             self.add_line_profile(lp, update=False)  # we might not be using a GUI
         elif type(lp) is dict:  # recreate the line profile
             if lp['class'] == 'LineProfile':
                 input_lp = lp.copy()
                 del input_lp['class']
                 self.add_line_profile(rois.LineProfile(**input_lp), update=False)
Esempio n. 3
0
 def _load_profiles_from_array(self, parray):
     profile_count = parray.shape[0]
     for pi in range(profile_count):
         lp = rois.LineProfile(parray[pi]['r1'], parray[pi]['c1'], parray[pi]['r2'], parray[pi]['c2'],
                          parray[pi]['slice'], parray[pi]['width'], identifier=pi)
         lp.set_profile(parray[pi]['profile'])
         lp.set_distance(parray[pi]['distance'])
         # self.add_line_profile(lp)
         # skip GUI calls and just add profile
         self._rois.append(lp)
Esempio n. 4
0
def test_non_ensemble_fitters():
    for fitter_class in profile_fitters.non_ensemble_fitters.values():
        ground = get_ground(fitter_class)
        # leave out noise for non-ensemble fitters, otherwise results will be poor when fitting a single profile
        p = fitter_class._model_function(ground, X)
        lp = rois.LineProfile(distance=X, profile=p)
        h = handlers.LineProfileHandler()
        h.add_line_profile(lp, update=False)
        fitter = fitter_class(h)
        fitter.fit_profiles()
        np.testing.assert_allclose(fitter.results['fitResults'][0].tolist(), ground, atol=5, rtol=0.15)
Esempio n. 5
0
 def LineProfile_from_array(self, parray, table_name=None):
     lp = rois.LineProfile(parray['r1'],
                           parray['c1'],
                           parray['r2'],
                           parray['c2'],
                           parray['slice'],
                           parray['width'],
                           identifier=table_name)
     lp.set_profile(parray['profile'])
     lp.set_distance(parray['distance'])
     return lp
Esempio n. 6
0
def test_ensemble_fitters():
    for fitter_class in profile_fitters.ensemble_fitters.values():
        ground, ens = get_ground(fitter_class)
        # leave out poisson sampling since we're only making one profile and with noise the lumen fitters mix psf / diam
        p = fitter_class._model_function(ground, X, ens)
        lp = rois.LineProfile(distance=X, profile=p)
        h = handlers.LineProfileHandler()
        h.add_line_profile(lp, update=False)
        fitter = fitter_class(h)
        fitter.ensemble_fit(ens)
        np.testing.assert_allclose(fitter.results['fitResults'][0].tolist(), ground, rtol=0.1)
        np.testing.assert_allclose(fitter.results['ensemble_parameter'][0].tolist(), ens, rtol=0.1)
Esempio n. 7
0
    def open_line_profiles(self, hdfFile, gui=True):
        import tables
        from PYME.IO import ragged
        if type(hdfFile) == tables.file.File:
            hdf = hdfFile
        else:
            hdf = tables.open_file(hdfFile)

        for t in hdf.list_nodes('/'):
            #print t.name, t.__class__
            if isinstance(t, tables.vlarray.VLArray):
                profs = ragged.RaggedVLArray(hdf, t.name)
                for i in range(len(profs)):  # in profs[:]:
                    p = profs[i]
                    identifier = str(
                        i) if not p['identifier'] else p['identifier']
                    if p['class'] == 'LineProfile':
                        lp = rois.LineProfile(p['r1'],
                                              p['c1'],
                                              p['r2'],
                                              p['c2'],
                                              p['slice'],
                                              p['width'],
                                              identifier=identifier,
                                              image_name=p['image_name'])
                        lp.set_profile(p['profile'])
                        lp.set_distance(p['distance'])
                    else:  # p['class'] == 'MultiaxisProfile':
                        profiles = []
                        positions = []
                        n_prof = 0
                        for k in p.keys():
                            if len(k.split('~')) > 1:
                                n_prof = max(n_prof, int(k.split('~')[-1]) + 1)

                        for pi in range(n_prof):
                            profiles.append(np.asarray(p['profiles~%d' % pi]))
                            positions.append(np.asarray(p['positions~%d' %
                                                          pi]))
                        lp = rois.MultiaxisProfile(profiles,
                                                   positions,
                                                   p['widths'],
                                                   identifier=identifier,
                                                   image_name=p['image_name'])

                    self.add_line_profile(lp, False)

        if gui:
            self.update_names(relabel=True)
            self._on_list_changed()

            # TODO - check if we need to worry about multiple profiles with the same id
        hdf.close()
Esempio n. 8
0
    def _load_profiles_from_imagej(self, fn, constant_slice=None, gui=True):
        """
        Read line profiles from Fiji/ImageJ.

        Parameters
        ----------
            fn : str
                Filename containing line profiles.
            constant_slice : int
                Set slice to a constant value. Useful if ImageJ 
                output is inconsistent.
            gui: bool
                flag to update names / send signals to update the gui profile list.
        """
        import zipfile
        try:
            import read_roi
        except (ImportError):
            raise ImportError(
                'Please install the read_roi package (https://pypi.org/project/read-roi/).'
            )

        try:
            imagej_rois = read_roi.read_roi_zip(fn)
        except (zipfile.BadZipFile):
            imagej_rois = read_roi.read_roi_file(fn)

        for key in imagej_rois.keys():
            roi = imagej_rois[key]
            # Check for a slice val
            if constant_slice is None:
                try:
                    slice_val = roi['position'] - 1
                except KeyError:
                    slice_val = 0
            else:
                slice_val = constant_slice
            # x, y transposed in Fiji/ImageJ
            # Position is 1-indexed in Fiji/ImageJ
            self.add_line_profile(rois.LineProfile(roi['y1'],
                                                   roi['x1'],
                                                   roi['y2'],
                                                   roi['x2'],
                                                   slice=slice_val,
                                                   width=roi['width'],
                                                   identifier=roi['name'],
                                                   image_name=self.image_name),
                                  update=False)
        if gui:
            self.update_names(relabel=True)
            self._on_list_changed()