def __init__(self, length, radius, curvature, pad_width=5, name=None): a2 = curvature xl = radius + pad_width xr = fsolve(calc_length, length, args=(xl, a2, length)).squeeze() a1 = -a2 * (xr + xl) r = radius xm = (xl + xr) / 2 y_mid = a1*xm + a2*xm**2 a0 = 4*radius - y_mid + pad_width y_max = a0 + a1 * xr + a2 * xr ** 2 shape = tuple(np.ceil([y_max + 10 + r, xr + 2 * r + 10]).astype(int)) coords = Coordinates(None, a0=a0, a1=a1, a2=a2, xl=xl, xr=xr, r=r, shape=shape, initialize=False) binary = coords.rc < r min1, max1, min2, max2 = mh.bbox(binary) min1p, max1p, min2p, max2p = min1 - pad_width, max1 + pad_width, min2 - pad_width, max2 + pad_width full = np.zeros((np.max([max1p, binary.shape[0]]), np.max([max2p, binary.shape[1]]))) full[0:binary.shape[0], 0:binary.shape[1]] = binary res = full[min1p:max1p, 0:max2p] data = Data() data.add_data(res.astype(int), 'binary') super(SynthCell, self).__init__(data, name=name) self.coords.a0 = a0 - min1p self.coords.a1 = a1 self.coords.a2 = a2 self.coords.xl = xl self.coords.xr = xr self.coords.r = r
def setUp(self): f_path = os.path.dirname(os.path.realpath(__file__)) self.cell_list = load( os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5')) self.cell = self.cell_list[0] data = Data() data.add_data(self.cell.data.binary_img, 'binary') self.empty_cell = Cell(data)
def load_data(dataset): dclasses = ['binary', 'brightfield', 'fluorescence'] f_path = os.path.dirname(os.path.realpath(__file__)) data = Data() for dclass in dclasses: files = sorted(listdir_fullpath( os.path.join(f_path, 'test_data', dataset, dclass.lower())), key=str) arr = np.empty((len(files), 512, 512)).astype('uint16') for i, f in enumerate(files): arr[i] = tifffile.imread(f) data.add_data(arr, dclass) return data
def setUp(self): f_path = os.path.dirname(os.path.realpath(__file__)) storm = load_thunderstorm( os.path.join(f_path, r'test_data/ds3/storm_table.csv')) binary = tifffile.imread( os.path.join(f_path, r'test_data/ds3/binary.tif')) fluorescence = tifffile.imread( os.path.join(f_path, r'test_data/ds3/flu.tif')) self.storm_ds = Data() self.storm_ds.add_data(binary, 'binary') self.storm_ds.add_data(fluorescence, 'fluorescence') self.storm_ds.add_data(storm, 'storm')
def setUp(self): f_path = os.path.dirname(os.path.realpath(__file__)) self.cell_list = load( os.path.join(f_path, 'test_data', 'test_synth_cell_storm.hdf5')) self.cell = self.cell_list[0] x = np.arange(20) y = np.exp(-x / 5) img_3d = self.cell.data.data_dict['fluorescence'][ np.newaxis, :, :] * y[:, np.newaxis, np.newaxis] self.cell.data.add_data(img_3d, 'fluorescence', 'flu_3d') data = Data() data.add_data(self.cell.data.binary_img, 'binary') self.empty_cell = Cell(data)
def load_escvdata(): binary = tifffile.imread(r'test_data/ds5/binary.tif') flu = tifffile.imread(r'test_data/ds5/flu.tif') storm = load_thunderstorm(r'test_data/ds5/storm_table.csv') data = Data() data.add_data(binary, 'binary') data.add_data(flu, 'fluorescence') data.add_data(storm, 'storm') return data
def load_stormdata(): f_path = os.path.dirname(os.path.realpath(__file__)) binary = tifffile.imread( os.path.join(f_path, r'test_data/ds2/binary_resized.tif')).astype('int') dtype = { 'names': ("id", "frame", "x", "y", "sigma", "intensity", "offset", "bkgstd", "chi2", "uncertainty_xy"), 'formats': (int, int, float, float, float, float, float, float, float, float) } storm_table = np.genfromtxt(os.path.join(f_path, r'test_data/ds2/storm_table.csv'), skip_header=1, dtype=dtype, delimiter=',') storm_table['x'] /= cfg.IMG_PIXELSIZE storm_table['y'] /= cfg.IMG_PIXELSIZE data = Data() data.add_data(binary, 'binary') data.add_data(storm_table, 'storm') return data
class TestDataToCells(ArrayTestCase): def setUp(self): f_path = os.path.dirname(os.path.realpath(__file__)) storm = load_thunderstorm( os.path.join(f_path, r'test_data/ds3/storm_table.csv')) binary = tifffile.imread( os.path.join(f_path, r'test_data/ds3/binary.tif')) fluorescence = tifffile.imread( os.path.join(f_path, r'test_data/ds3/flu.tif')) self.storm_ds = Data() self.storm_ds.add_data(binary, 'binary') self.storm_ds.add_data(fluorescence, 'fluorescence') self.storm_ds.add_data(storm, 'storm') def test_storm_data_to_cells(self): data = self.storm_ds cells = data_to_cells(data) self.assertEqual(sum([len(c.data.data_dict['storm']) for c in cells]), 40) data_copy = data.copy() storm_ds = data_copy.data_dict['storm'] storm_ds['frame'][storm_ds['frame'] == 4] = 5 cells = data_to_cells(data_copy) num_spots = [0, 10, 0, 6, 1, 14, 1, 0, 0, 0, 0, 0, 0] for c, spots in zip(cells, num_spots): self.assertEqual(len(c.data.data_dict['storm']), spots) self.assertIn('storm', c.data.names) self.assertIn('storm', c.data.dclasses)
def _load_cell(cell_grp): """Load the cell object from `cell_grp`""" data_obj = Data() data_grp = cell_grp['data'] for key in list(data_grp.keys()): grp = data_grp[key] data_arr = grp[key] dclass = grp.attrs.get('dclass').decode('UTF-8') data_obj.add_data(data_arr[:], dclass=dclass, name=key) c = Cell(data_obj, init_coords=False) attr_grp = cell_grp['attributes'] attr_dict = dict(attr_grp.attrs.items()) for a in ['r', 'xl', 'xr', 'coeff']: setattr(c.coords, a, attr_dict.get(a)) c.coords.shape = c.data.shape name = attr_dict.get('name').decode('UTF-8') c.name = name if name is not 'None' else None return c
def _load_deprecated(file_path): ext = os.path.splitext(file_path)[1] if ext == '.cc': with h5py.File(file_path, 'r') as f: data_obj = Data() data_grp = f['data'] for key in list(data_grp.keys()): grp = data_grp[key] data_arr = grp[key] dclass = grp.attrs.get('dclass').decode('UTF-8') data_obj.add_data(data_arr, dclass=dclass, name=key) c = Cell(data_obj) attr_grp = f['attributes'] attr_dict = dict(attr_grp.attrs.items()) for a in ['r', 'xl', 'xr', 'coeff']: setattr(c.coords, a, attr_dict.get(a)) c.name = attr_dict.get('label') return c else: raise ValueError('Invalid file type')
def setUp(self): self.data = load_testdata('ds1') f_path = os.path.dirname(os.path.realpath(__file__)) self.storm_cells_1 = load( os.path.join(f_path, 'test_data/test_single_spot_storm.hdf5')) self.storm_cells_2 = load( os.path.join(f_path, 'test_data/test_double_spot_storm.hdf5')) cells_no_flu = [] for c in self.storm_cells_2: d = Data() d.add_data(c.data.binary_img, 'binary') d.add_data(c.data.data_dict['storm_1'], 'storm', 'storm_1') d.add_data(c.data.data_dict['storm_2'], 'storm', 'storm_2') cell = Cell(d) cells_no_flu.append(cell) self.storm_cells_2_no_flu = CellList(cells_no_flu)
def test_add_data(self): testdata_int = np.round(np.random.rand(512, 512)).astype(int) testdata_float = np.round(np.random.rand(512, 512)) * 2**16 - 1 testdata_mov = np.round(np.random.rand(10, 512, 512)) * 2**16 - 1 data = Data() with self.assertRaises(TypeError): # Invalid dtype data.add_data(testdata_float, dclass='binary') data.add_data(testdata_int, dclass='binary') self.assertArrayEqual(testdata_int, data.data_dict['binary']) self.assertArrayEqual(testdata_int, data.binary_img) with self.assertRaises(ValueError): # Invalid shape data.add_data(testdata_float.reshape(256, -1), 'fluorescence') with self.assertRaises(ValueError): # Binary has to be unique data.add_data(testdata_int, dclass='binary', name='newbinaryname') data.add_data(testdata_float, dclass='brightfield') with self.assertRaises( ValueError ): # Same dclass data elements which will have the same name data.add_data(testdata_float, dclass='brightfield') self.assertEqual(testdata_float.shape, data.shape) data.add_data(testdata_mov, 'fluorescence', name='fluorescence_movie')