def refine_com(self, pos_diff=None, signal_dev=None, size_dev=None, noise=None, denoise_size=1, smoothing_size=None, **kwargs): """ Parameters ---------- noise : integer noise level pos_diff : pixels deviation of p0 from true feature location signal_dev : deviation of feature signal with respect to p0 size_dev : deviation of feature size with respect to p0 """ if pos_diff is None: pos_diff = self.pos_diff if signal_dev is None: signal_dev = self.signal_dev if size_dev is None: size_dev = self.size_dev if noise is None: noise = self.noise # generate image with array of features and deviating signal and size image, expected = self.get_image(noise, signal_dev, size_dev, denoise_size=denoise_size, smoothing_size=smoothing_size) expected_pos, expected_signal, expected_size = expected p0_pos = self.gen_p0_coords(expected_pos, pos_diff) actual = refine_com(image, image, self.radius, p0_pos, **kwargs) actual = self.from_tp_ndarray(actual) # actual = self.sort(actual, expected_pos) return self.compute_deviations(actual, expected)
def test_refine(self): coords_v0 = self.expected_find_bp image_bp = tp.bandpass(self.v0_inverted, **self.bandpass_params) df = tp.refine_com(self.v0_inverted, image_bp, coords=coords_v0, **self.refine_params) actual = df[df['mass'] >= self.minmass][self.pos_columns].values assert_allclose(actual, self.expected_refine)
pos_columns = ['y', 'x'] char_columns = ['mass', 'size', 'ecc', 'signal', 'raw_mass', 'ep'] testpath = os.path.join(os.path.dirname(tp.__file__), 'tests') impath = os.path.join(testpath, 'video', 'image_sequence', '*.png') npzpath = os.path.join(testpath, 'data', 'reproducibility_v{}.npz'.format(version)) v = pims.ImageSequence(impath) # take reader that provides uint8! assert np.issubdtype(v.dtype, np.uint8) v0 = tp.invert_image(v[0]) v0_bp = tp.bandpass(v0, lshort=1, llong=9) expected_find = tp.grey_dilation(v0, separation=9) expected_find_bandpass = tp.grey_dilation(v0_bp, separation=9) expected_refine = tp.refine_com(v0, v0_bp, radius=4, coords=expected_find_bandpass) expected_refine = expected_refine[expected_refine['mass'] >= 140] expected_refine_coords = expected_refine[pos_columns].values expected_locate = tp.locate(v0, diameter=9, minmass=140) expected_locate_coords = expected_locate[pos_columns].values df = tp.locate(v0, diameter=9) df = df[(df['x'] < 64) & (df['y'] < 64)] expected_characterize = df[pos_columns + char_columns].values f = tp.batch(tp.invert_image(v), 9, minmass=140) f_crop = f[(f['x'] < 320) & (f['x'] > 280) & (f['y'] < 280) & (f['x'] > 240)] f_linked = tp.link(f_crop, search_range=5, memory=0) f_linked_memory = tp.link(f_crop, search_range=5, memory=2) link_coords = f_linked[pos_columns + ['frame']].values expected_linked = f_linked['particle'].values
pos_columns = ['y', 'x'] char_columns = ['mass', 'size', 'ecc', 'signal', 'raw_mass', 'ep'] testpath = os.path.join(os.path.dirname(tp.__file__), 'tests') impath = os.path.join(testpath, 'video', 'image_sequence', '*.png') npzpath = os.path.join(testpath, 'data', 'reproducibility_v{}.npz'.format(version)) v = pims.ImageSequence(impath) # take reader that provides uint8! assert np.issubdtype(v.dtype, np.uint8) v0 = tp.invert_image(v[0]) v0_bp = tp.bandpass(v0, lshort=1, llong=9) expected_find = tp.grey_dilation(v0, separation=9) expected_find_bandpass = tp.grey_dilation(v0_bp, separation=9) expected_refine = tp.refine_com(v0, v0_bp, radius=4, coords=expected_find_bandpass) expected_refine = expected_refine[expected_refine['mass'] >= 140] expected_refine_coords = expected_refine[pos_columns].values expected_locate = tp.locate(v0, diameter=9, minmass=140) expected_locate_coords = expected_locate[pos_columns].values df = tp.locate(v0, diameter=9) df = df[(df['x'] < 64) & (df['y'] < 64)] expected_characterize = df[pos_columns + char_columns].values f = tp.batch(tp.invert_image(v), 9, minmass=140) f_crop = f[(f['x'] < 320) & (f['x'] > 280) & (f['y'] < 280) & (f['x'] > 240)] f_linked = tp.link(f_crop, search_range=5, memory=0) f_linked_memory = tp.link(f_crop, search_range=5, memory=2) link_coords = f_linked[pos_columns + ['frame']].values expected_linked = f_linked['particle'].values expected_linked_memory = f_linked_memory['particle'].values
def train_leastsq(f, reader, diameter, separation, fit_function, param_mode=None, tol=1e-7, pos_columns=None, **kwargs): """Obtain fit parameters from an image of features with equal size. Different signal intensities per feature are allowed.""" try: ndim = len(reader.frame_shape) except AttributeError: ndim = reader.ndim reader_tp = [reader] if 'frame' in f: assert np.all(f['frame'].nunique() == 0) else: f['frame'] = 0 diameter = validate_tuple(diameter, ndim) radius = tuple([d // 2 for d in diameter]) if pos_columns is None: pos_columns = guess_pos_columns(f) isotropic = is_isotropic(diameter) size_columns = obtain_size_columns(isotropic, pos_columns) # first, refine using center-of-mass for frame_no, f_frame in f.groupby('frame'): coords = f_frame[pos_columns].values image = reader_tp[frame_no] tp_result = refine_com(image, image, radius, coords) pos = tp_result[:, ndim-1:None:-1] if isotropic: size = tp_result[:, ndim + 1] signal = tp_result[:, ndim + 3] else: size = tp_result[:, ndim + 1:2*ndim + 1] signal = tp_result[:, 2*ndim + 2] f.loc[f_frame.index, pos_columns] = pos f.loc[f_frame.index, 'signal'] = signal f.loc[f_frame.index, size_columns] = size if param_mode is None: param_mode = dict() ff = FitFunctions(fit_function, ndim, isotropic) for param in ff.params: if param in param_mode: continue if param in pos_columns + ['signal', 'background']: param_mode[param] = 'const' else: param_mode[param] = 'global' bounds = kwargs.pop('bounds', dict()) if bounds is None: bounds = dict() for size_col in size_columns: if size_col + '_rel_diff' not in bounds: bounds[size_col + '_rel_diff'] = (0.9, 9) # - 90%, +900% f = refine_leastsq(f, reader, diameter, separation, fit_function=fit_function, param_mode=param_mode, tol=tol, bounds=bounds, **kwargs) assert np.isfinite(f['cost']).all() return {p: f[p].mean() for p in param_mode if param_mode[p] == 'global'}