def test_position_cell(self): stack = Stack() stack.image = np.zeros([10, 10, 10]) ind = np.where(np.ones([3, 3, 3]) > 0) cell = Cell(ind=ind) cell.position = (1, 1, 1) stack.position_cell(cell) self.assertEqual(np.sum(stack.image > 1), 27)
def test_position_cell3(self): stack = Stack() stack.image = np.zeros([10, 6, 6]) cell = Cell() cell.image = np.zeros([15, 8, 8]) cell.image[8:10, 2:-1, 2:-1] = 255 cell.position = ndimage.center_of_mass(np.ones_like(stack.image)) stack.position_cell(cell) self.assertEqual(np.sum(stack.image > 0), np.sum(cell.image > 0))
def __generate_stacks_batch_helper(item, outputfolder, input_voxel_size, stack_size_microns, **kwargs_to_ignore): filename, params = item if not outputfolder.endswith('/'): outputfolder += '/' stack = Stack(input_voxel_size=input_voxel_size, stack_size=stack_size_microns, cell_params=params) stack.save(outputfolder + filename[:-4] + '.tif') stack.metadata['StackID'] = filename[:-4].split('_')[-1] stack.metadata.save(outputfolder + filename[:-4] + '.csv')
def test_position_cell2(self): stack = Stack() stack.image = np.zeros([10, 10, 10]) ind = np.where(np.ones([3, 3, 3]) > 0) cell = Cell(ind=ind) cell.position = (1, 1, 1) stack.position_cell(cell) cell.position = (5, 5, 5) stack.position_cell(cell)
def __resize_batch_helper(item, inputfolder, outputfolder, order=1, append_resolution_to_filename=True, **kwargs_to_ignore): inputfile, voxel_size = item metadata = Metadata() metadata.set_voxel_size(voxel_size) stack = Stack(filename=inputfolder + inputfile) zoom = np.array(stack.metadata['Voxel size arr']) / np.array( metadata['Voxel size arr']) stack.resize(zoom=zoom, order=order) stack.metadata.set_voxel_size(metadata['Voxel size arr']) path = os.path.dirname(inputfile) strres = str(metadata['Voxel size']).replace(' ', ' ').replace( ' ', ' ').replace('[ ', '[').replace(' ', '_') if path == '': if append_resolution_to_filename: outputname = outputfolder + inputfile[: -4] + '_voxel_size_' + strres + '.tif' else: outputname = outputfolder + path + 'voxel_size_' + strres + '/' + inputfile.split( '/')[-1] else: outputname = outputfolder + path + '_voxel_size_' + strres + '/' + inputfile.split( '/')[-1] stack.save(outputname) stack.metadata.save(outputname[:-4] + '.csv')
def __convolve_batch_helper(item, inputfolder, psffolder, outputfolder, **kwargs_to_ignore): inputfile, psffile = item stack = Stack(filename=inputfolder + inputfile) psf = PSF(filename=psffolder + psffile) stack.convolve(psf) stack.save(outputfolder + psffile[:-4] + '/' + inputfile) for c in ['PSF sigma xy um', 'PSF aspect ratio']: stack.metadata[c] = psf.metadata[c] stack.metadata.save(outputfolder + psffile[:-4] + '/' + inputfile[:-4] + '.csv') psf.save(outputfolder + psffile) psf.metadata.save(outputfolder + psffile[:-4] + '.csv')
def __compute_accuracy_measures_batch_helper(item, inputfolder, reffolder, outputfolder, **kwargs_to_ignore): if not reffolder.endswith('/'): reffolder += '/' parts = item.split('/') name = parts[-1] if len(parts) > 1: base = parts[-2] else: base = '' stack = Stack(filename=inputfolder + item) if 'isPSF' not in stack.metadata.index or str( stack.metadata['isPSF']) == 'False': if os.path.exists(reffolder + item): refstack = Stack(filename=reffolder + item) elif os.path.exists(reffolder + name): refstack = Stack(filename=reffolder + name) elif os.path.exists(reffolder + base + '/' + name): refstack = Stack(filename=reffolder + base + '/' + name) elif os.path.exists(reffolder + name.split('_voxel_size')[0] + '.tif'): refstack = Stack(filename=reffolder + name.split('_voxel_size')[0] + '.tif') else: raise ValueError('No ground truth found for cell ' + item + '!') input_voxel_size = stack.metadata['Voxel size arr'] zoom = np.array(stack.metadata['Voxel size arr']) / np.array( refstack.metadata['Voxel size arr']) stack.resize(zoom=zoom) stats = stack.compute_accuracy_measures(refstack) stack.metadata.set_voxel_size(input_voxel_size) for c in stack.metadata.index: try: stats[c] = stack.metadata[c] except ValueError: stats[c] = str(stack.metadata[c]) stats['Name'] = item filelib.make_folders([os.path.dirname(outputfolder + item)]) stats.to_csv(outputfolder + item[:-4] + '.csv', sep='\t')
def test_from_stack_params2(self): params = pd.DataFrame({ 'size_x': [10, 10, 10], 'size_y': [9, 10, 8], 'size_z': [10, 11, 10], 'phi': [0, np.pi / 4, np.pi / 2], 'theta': [0, 0, 0], 'x': [0.1, 0.8, 0.2], 'y': [0.5, 0.5, 0.5], 'z': [0.2, 0.5, 0.8], 'spikiness': [0, 0, 100], 'spike_size': [0, 0, 1], 'spike_smoothness': [0.05, 0.05, 0.05] }) stack = Stack(cell_params=params, input_voxel_size=0.5, stack_size=[50, 50, 50]) self.assertIsNotNone(stack.image)
def __add_noise_batch_helper(item, inputfolder, outputfolder, **kwargs_to_ignore): inputfile, kind, snr = item stack = Stack(filename=inputfolder + inputfile) path = os.path.dirname(inputfile) if 'isPSF' not in stack.metadata.index or str( stack.metadata['isPSF']) == 'False': stack.add_noise(kind=kind, snr=snr) if path == '': outputname = outputfolder + inputfile else: outputname = outputfolder + path + '_noise' snr = np.array([snr]).flatten() kind = np.array([kind]).flatten() if len(snr) == 1: snr = np.array([snr[0]] * len(kind)) for i, k in enumerate(kind): outputname = outputname + '_' + k + '_snr=' + str(snr[i]) outputname = outputname + '/' + inputfile.split('/')[-1] stack.save(outputname) stack.metadata.save(outputname[:-4] + '.csv')
def test_range_for_number_of_cells(self): params = CellParams(number_of_stacks=3, number_of_cells=[5, 10]) stack = Stack(cell_params=params[params['stack'] == 2].reset_index(), input_voxel_size=0.5, stack_size=[10, 10, 10]) self.assertIsNotNone(stack.image)
def test_from_stack_params(self): params = CellParams(number_of_stacks=1, number_of_cells=5) stack = Stack(cell_params=params[params['stack'] == 0], input_voxel_size=0.5, stack_size=[10, 10, 10]) self.assertIsNotNone(stack.image)
def test_from_params(self, case): params, res, stacksize, volume = case stack = Stack() stack.generate(params, res, stacksize) self.assertAlmostEqual(np.sum(stack.image > 0) / volume, 1, 1)
def test_empty_arguments(self): img = Stack() for var in ['image', 'filename']: self.assertIn(var, img.__dict__) self.assertEqual(img.__dict__[var], None)