class CoordinateReader: """Generate a pims.FramesSquence-like object that draws features at given coordinates""" def __init__(self, f, shape, size, t=None, **kwargs): self._f = f.copy() self.pos_columns = ['z', 'y', 'x'][-len(shape):] self.shape = shape self.size = size self.kwargs = kwargs self.im = SimulatedImage(shape, size, **self.kwargs) if t is None: self._len = int(f['frame'].max() + 1) self._inds = range(self._len) else: self._len = len(t) self._inds = t def __len__(self): return self._len def __iter__(self): # this is actually a hack to get find_link working with float-typed indices return (self.get_frame(i) for i in self._inds) def __getitem__(self, key): return self.get_frame(key) def get_frame(self, ind): self.im.clear() pos = self._f.loc[self._f['frame'] == ind, self.pos_columns].values for _pos in pos: self.im.draw_feature(_pos) return TrackpyFrame(self.im(), frame_no=ind) @property def pixel_type(self): return self.im.dtype @property def frame_shape(self): return self.im.shape
class TestMultiple(unittest.TestCase): shape = (256, 256) pos_err = 3 diameter = 21 separation = 24 def setUp(self): self.signal = 200 if not hasattr(self, 'size'): if hasattr(self.diameter, '__iter__'): self.size = tuple([d / 4 for d in self.diameter]) else: self.size = self.diameter / 4 self.im = SimulatedImage(self.shape, self.size, dtype=np.uint8, signal=self.signal, feat_func=feat_gauss, noise=0) self.names = self.im.pos_columns + ['signal'] + self.im.size_columns self.bounds = dict() def test_multiple_simple_sparse(self): self.im.clear() self.im.draw_features(10, self.separation, 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_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) 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)
class TestMultiple(StrictTestCase): shape = (256, 256) pos_err = 3 diameter = 21 separation = 24 def setUp(self): self.signal = 200 if not hasattr(self, 'size'): if hasattr(self.diameter, '__iter__'): self.size = tuple([d / 4 for d in self.diameter]) else: self.size = self.diameter / 4 self.im = SimulatedImage(self.shape, self.size, dtype=np.uint8, signal=self.signal, feat_func=feat_gauss, noise=0) self.names = self.im.pos_columns + ['signal'] + self.im.size_columns self.bounds = dict() def test_multiple_simple_sparse(self): self.im.clear() self.im.draw_features(10, self.separation, 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_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) 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)