def test_constraint_global_noisy(self): hard_radius = 1. constraints = dimer_global(1, self.im.ndim) self.im.clear() self.im.draw_clusters(10, 2, hard_radius, 2*self.separation, 2*self.diameter) f0 = self.im.f(noise=self.pos_err) f0['signal'] = 180 f0['size'] = 6. result = refine_leastsq(f0, self.im.noisy_image(0.2*self.signal), self.diameter, self.separation, constraints=constraints, param_mode=dict(signal='global', size='global'), options=dict(maxiter=1000)) dists = [] for _, f_cl in result.groupby('cluster'): pos = result[['y', 'x']].values dists.append(np.sqrt(np.sum(((pos[0] - pos[1])/np.array(self.im.size))**2))) assert_allclose(dists, 2*hard_radius, atol=0.1) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1) assert_allclose(result['signal'].values, 200, atol=2) assert_allclose(result['size'].values, 5.25, atol=1)
def test_constraint_global_noisy(self): hard_radius = 1. constraints = dimer_global(1, self.im.ndim) self.im.clear() self.im.draw_clusters(10, 2, hard_radius, 2 * self.separation, 2 * self.diameter) f0 = self.im.f(noise=self.pos_err) f0['signal'] = 180 f0['size'] = 6. result = refine_leastsq(f0, self.im.noisy_image(0.2 * self.signal), self.diameter, self.separation, constraints=constraints, param_mode=dict(signal='global', size='global'), options=dict(maxiter=1000)) dists = [] for _, f_cl in result.groupby('cluster'): pos = result[['y', 'x']].values dists.append( np.sqrt(np.sum( ((pos[0] - pos[1]) / np.array(self.im.size))**2))) assert_allclose(dists, 2 * hard_radius, atol=0.1) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1) assert_allclose(result['signal'].values, 200, atol=2) assert_allclose(result['size'].values, 5.25, atol=1)
def test_constraint_global_dimer(self): hard_radius = 1. constraints = dimer_global(1, self.im.ndim) self.im.clear() self.im.draw_clusters(10, 2, hard_radius, 2 * self.separation, 2 * self.diameter) f0 = self.im.f(noise=self.pos_err) result = refine_leastsq(f0, self.im(), self.diameter, self.separation, constraints=constraints, options=dict(maxiter=1000)) dists = [] for _, f_cl in result.groupby('cluster'): pos = result[['y', 'x']].values dists.append( np.sqrt(np.sum( ((pos[0] - pos[1]) / np.array(self.im.size))**2))) assert_allclose(dists, 2 * hard_radius, atol=0.01) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1)
def refine_cluster(self, cluster_size, hard_radius, pos_diff=None, signal_dev=None, size_dev=None, noise=None, param_mode=None, angle=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 param_mode is None: param_mode = self.param_mode else: param_mode = dict(self.param_mode, **param_mode) 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, clusters = self.get_image_clusters(cluster_size, hard_radius, noise, signal_dev, size_dev, angle) expected_pos, expected_signal, expected_size = expected expected_center, expected_angle = clusters p0_pos = self.gen_p0_coords(expected_pos, pos_diff) f0 = self.to_dataframe(p0_pos, self.signal, self.size, cluster_size) # Set an estimate for the background value. Helps convergence, # especially for 3D tests with high noise (tested here: S/N = 3) f0['background'] = noise / 2 actual = refine_leastsq(f0, image, self.diameter, separation=None, param_mode=dict(self.param_mode, **param_mode), param_val=self.param_val, pos_columns=self.pos_columns, t_column='frame', fit_function=self.fit_func, bounds=self.bounds, **kwargs) assert not np.any(np.isnan(actual['cost'])) assert np.all(actual['cluster_size'] <= cluster_size) actual = self.from_dataframe(actual) actual_pos, actual_signal, actual_size, pos_err = actual deviations = self.get_deviations(actual_pos, expected_pos, cluster_size, expected_center, expected_angle) return self.compute_deviations_cluster(actual, expected, deviations)
def refine(self, pos_diff=None, signal_dev=None, size_dev=None, noise=None, param_mode=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 param_mode is None: param_mode = self.param_mode else: param_mode = dict(self.param_mode, **param_mode) 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) expected_pos, expected_signal, expected_size = expected p0_pos = self.gen_p0_coords(expected_pos, pos_diff) f0 = self.to_dataframe(p0_pos, self.signal, self.size) # Set an estimate for the background value. Helps convergence, # especially for 3D tests with high noise (tested here: S/N = 3) f0['background'] = noise / 2 actual = refine_leastsq(f0, image, self.diameter, separation=None, param_mode=dict(self.param_mode, **param_mode), param_val=self.param_val, pos_columns=self.pos_columns, t_column='frame', fit_function=self.fit_func, bounds=self.bounds, **kwargs) assert not np.any(np.isnan(actual['cost'])) actual = self.from_dataframe(actual) return self.compute_deviations(actual, expected)
def test_multiple_overlapping(self): self.im.clear() self.im.draw_features(100, 15, self.diameter) f0 = self.im.f(noise=self.pos_err) result = refine_leastsq(f0, self.im(), self.diameter, self.separation) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1)
def test_var_global(self): self.im.clear() self.im.draw_features(100, 15, self.diameter) f0 = self.im.f(noise=self.pos_err) f0['signal'] = 180 result = refine_leastsq(f0, self.im(), self.diameter, self.separation, param_mode=dict(signal='global'), options=dict(maxiter=10000)) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1) assert (result['signal'].values[1:] == result['signal'].values[:-1]).all() assert_allclose(result['signal'].values, 200, atol=5)
def test_constraint_global_dimer(self): hard_radius = 1. constraints = dimer_global(1, self.im.ndim) self.im.clear() self.im.draw_clusters(10, 2, hard_radius, 2*self.separation, 2*self.diameter) f0 = self.im.f(noise=self.pos_err) result = refine_leastsq(f0, self.im(), self.diameter, self.separation, constraints=constraints, options=dict(maxiter=1000)) dists = [] for _, f_cl in result.groupby('cluster'): pos = result[['y', 'x']].values dists.append(np.sqrt(np.sum(((pos[0] - pos[1])/np.array(self.im.size))**2))) assert_allclose(dists, 2*hard_radius, atol=0.01) assert_coordinates_close(result[self.im.pos_columns].values, self.im.coords, 0.1)