def plot_3D_surfaces(inputfolder, outputfolder, points=True, gridsize=100): """ Plot 3D views of surfaces located in a given directory. Parameters ---------- inputfolder : str Input directory with surfaces. outputfolder : str Output directory to save the plots. points : bool, optional If True, surface points will be displayed. Default is True. gridsize : int, optional Dimension of the square grid to interpolate the surface points. Default is 100. """ files = filelib.list_subfolders(inputfolder, extensions=['csv']) for fn in files: s = Surface(filename=inputfolder + fn) s.centrate() s.to_spherical() s.Rgrid = s.interpolate(grid_size=gridsize) mesh = s.plot_surface(points=points) mesh.magnification = 3 filelib.make_folders([os.path.dirname(outputfolder + fn[:-4])]) mesh.save(outputfolder + fn[:-4] + '.png', size=(200, 200))
def compute_frequency_spectra(**kwargs): """ Compute frequency spectra for a given surface. Keyword arguments ----------------- *inputfolder* : str Directory with the input surface. *item* : str File name of the input surface. *outputfolder* : str Directory to save the computed spectra. *grid_size* : int Dimension of the square grid to interpolate the surface points. *normalize* : bool If True, the values of the spectrum will be normalized according to the `normalization_method`. *normalization_method* : str, optional If 'mean-radius', the grid values will be divided by the mean grid value prior to the SPHARM transform. If 'zero-component', all spectral components will be divided by the value of the first component (m=0, n=0). Default is 'zero-component'. """ inputfolder = kwargs.get('inputfolder') outputfolder = kwargs.get('outputfolder', inputfolder + '../spharm/') filename = kwargs.get('item') if not os.path.exists(outputfolder + filename): surface = Surface(filename=inputfolder + filename) surface.centrate() surface.to_spherical() surface.compute_spharm( grid_size=kwargs.get('grid_size'), normalize=kwargs.get('normalize'), normalization_method=kwargs.get('normalization_method')) surface.spharm.save_to_csv(outputfolder + filename)
def test_transforms(self): img = ImageStack(filename='', load=False) img.data = np.zeros([10, 100, 100]) img.data[2:7, 10:-10, 10:-10] = 1 voxel_size = [4, 0.3824, 0.3824] img.extract_surfaces('data/test_data/surfaces/', voxel_size=voxel_size, reconstruct=False) surf = Surface(filename='data/test_data/surfaces/_Cell00001.csv') surf.to_spherical() x, y, z = tr.spherical_to_cart(surf.R, surf.theta, surf.phi) self.assertAlmostEqual(np.sum(np.abs(surf.x - x)), 0, 7) self.assertAlmostEqual(np.sum(np.abs(surf.y - y)), 0, 7) self.assertAlmostEqual(np.sum(np.abs(surf.z - z)), 0, 7) shutil.rmtree('data/test_data/')
def test_spharm_transform(self): img = ImageStack(filename='', load=False) img.data = np.zeros([100, 100, 100]) img.data[48:52, 48:52, 48:52] = 1. voxel_size = 0.3 img.extract_surfaces('data/test_data/surfaces/', voxel_size=voxel_size, reconstruct=True) surf = Surface(filename='data/test_data/surfaces/_Cell00001.csv') surf.centrate() surf.to_spherical() grid = surf.interpolate(grid_size=10) surf.compute_spharm(grid_size=10) ngrid = surf.inverse_spharm() self.assertAlmostEqual(np.mean(np.abs(ngrid - grid)), 0, 1) shutil.rmtree('data/test_data/')
def test_read_surface_and_save_as_stack(self): img = ImageStack(filename='', load=False) img.data = np.zeros([10, 100, 100]) img.data[2:7, 10:-10, 10:-10] = 1 voxel_size = [4, 0.3824, 0.3824] img.extract_surfaces('data/test_data/surfaces/', voxel_size=voxel_size, reconstruct=False) surf = Surface(filename='data/test_data/surfaces/_Cell00001.csv') for coord in ['x', 'y', 'z']: self.assertIsNotNone(surf.__dict__[coord]) surf.save_as_stack(filename='data/test_data/stack.tif', voxel_size=0.5) self.assertEqual(os.path.exists('data/test_data/stack.tif'), True) self.assertEqual(os.path.exists('data/test_data/stack.txt'), True) shutil.rmtree('data/test_data/')
def test_add_spectrum_and_plotting(self): ms = MovingSurface() surf = Surface(grid=np.ones([10, 10])) for i in range(3): ms.add_surface(surf, timepoint=i * 20) surf = Surface(grid=np.ones([10, 10])) surf.Rgrid[3:4, 4:8] = 10 for i in range(2): ms.add_surface(surf, timepoint=i * 20 + 60) surf = Surface(grid=np.ones([10, 10])) for i in range(3): ms.add_surface(surf, timepoint=i * 20 + 100) self.assertEqual(len(ms.timespectrum.spectra), 0) ms.compute_timespectrum(gridsize=10) self.assertEqual(len(ms.timespectrum.spectra), 8) ms.plot_surfaces('data/test_data/surfaces/') files = os.listdir('data/test_data/surfaces/') self.assertEqual(len(files), 8) ms.plot_max_projections(outputfolder='data/test_data/maxprojections/', voxel_size=0.1) files = os.listdir('data/test_data/maxprojections/') self.assertEqual(len(files), 24) shutil.rmtree('data/test_data/')
def test_init_and_save(self, coords): os.makedirs('data/test_data') f = open('data/test_data/surface.txt', 'w') f.write(coords) f.close() surf = Surface(filename='data/test_data/surface.txt') for coord in ['x', 'y', 'z']: self.assertIsNotNone(surf.__dict__[coord]) shutil.rmtree('data/test_data/')
def convert_to_tiff(**kwargs): """ Save the surface as a 3D image stack. Keyword arguments ----------------- *inputfolder* : str Directory with the input surface. *item* : str File name of the input surface. *outputfolder* : str Directory to save the output 3D stack. """ inputfolder = kwargs.get('inputfolder') outputfolder = kwargs.get('outputfolder', inputfolder + '../stacks/') filename = kwargs.get('item') surface = Surface(filename=inputfolder + filename, **kwargs) surface.save_as_stack(outputfolder + filename + '.tif', voxel_size=kwargs.get('voxel_size'))
def convert_surfaces(**kwargs): """ Convert surface file from txt to a csv format. Keyword arguments ----------------- *inputfolder* : str Directory with the input surface. *item* : str File name of the input surface. *outputfolder* : str Directory to save the converted surface. """ inputfolder = kwargs.get('inputfolder') outputfolder = kwargs.get('outputfolder', inputfolder + '../surfaces/') filename = kwargs.get('item') surface = Surface(filename=inputfolder + filename, **kwargs) surface.save(outputfolder + filename + '.csv')
def test_interpolate(self, grid_size): img = ImageStack(filename='', load=False) img.data = np.zeros([10, 100, 100]) img.data[2:7, 10:-10, 10:-10] = 1 voxel_size = [4, 0.3824, 0.3824] img.extract_surfaces('data/test_data/surfaces/', voxel_size=voxel_size, reconstruct=False) surf = Surface(filename='data/test_data/surfaces/_Cell00001.csv') surf.centrate() surf.to_spherical() grid = surf.interpolate(grid_size=grid_size) self.assertEqual(len(grid), grid_size) shutil.rmtree('data/test_data/')
files = os.listdir(path + 'stacks/' + gr + '/') for fn in files: print(fn) stack = ImageStack(path + 'stacks/' + gr + '/' + fn) stack.filename = fn stack.extract_surfaces(path + 'surfaces/' + gr + '/', voxel_size=0.3) groups = os.listdir(path + 'surfaces/') for gr in groups: print(gr) files = os.listdir(path + 'surfaces/' + gr + '/') files.sort() for fn in files: print(fn) surface = Surface(filename=path + 'surfaces/' + gr + '/' + fn, voxel_size=0.3) mesh = mlab.points3d(surface.x, surface.y, surface.z, surface.x, scale_mode='none', scale_factor=0.5, mode='sphere', colormap='jet').scene mesh.background = (1, 1, 1) mesh.magnification = 10 filelib.make_folders([ os.path.dirname(path + 'surface_plots/' + gr + '_' + fn[:-4]) ]) mesh.save(path + 'surface_plots/' + gr + '_' + fn[:-4] +
def compute_spharm(**kwargs): """ Compute spherical harmonics spectra for a given surface. Keyword arguments ----------------- *inputfolder* : str Directory with the input surface. *item* : str File name of the input surface. *outputfolder* : str Directory to save the computed spectra. *grid_size* : int Dimension of the square grid to interpolate the surface points. *normalize* : bool If True, the values of the spectrum will be normalized according to the `normalization_method`. *normalization_method* : str, optional If 'mean-radius', the grid values will be divided by the mean grid value prior to the SPHARM transform. If 'zero-component', all spectral components will be divided by the value of the first component (m=0, n=0). Default is 'zero-component'. """ inputfolder = kwargs.get('inputfolder') outputfolder = kwargs.get('outputfolder', inputfolder + '../spharm/') filename = kwargs.get('item') combined_tracks = kwargs.get('combined_tracks', False) rotate = kwargs.get('rotate', False) filelib.make_folders([os.path.dirname(outputfolder[:-1] + '_kwargs.csv')]) pd.Series(kwargs).to_csv(outputfolder[:-1] + '_kwargs.csv', sep='\t', header=False) if not (os.path.exists(outputfolder + filename) or os.path.exists(outputfolder + filename[:-4] + '_Time_%03d.csv' % 1)): if combined_tracks: stat = pd.read_csv(inputfolder + filename, sep='\t', index_col=0) stat.at[:, 'Time'] = np.array(stat['Time']).astype(float).astype(int) stat = stat.sort_values('Time').reset_index() t_surface = MovingSurface() if not filename.endswith('.csv'): filename += '.csv' times = stat['Time'].unique() if len(times) > 2: for t in times: curstat = stat[stat['Time'] == t] if len(curstat) > 4: surface = Surface(data=curstat) surface.metadata[ 'Name'] = filename[:-4] + '_Time_%03d.csv' % t if 'TrackID' in curstat.columns: surface.metadata['TrackID'] = curstat.iloc[0][ 'TrackID'] surface.centrate() if len(t_surface.surfaces) > 0: x, y, z = surface.center - t_surface.surfaces[ -1].center # direction of the previous interval surface.migration_angles = tr.cart_to_spherical( x, y, z)[1:] t_surface.add_surface(surface) else: print(filename, times, t, len(curstat)) t_surface.surfaces[0].migration_angles = t_surface.surfaces[ 1].migration_angles for surface in t_surface.surfaces: if rotate: surface.rotate(surface.migration_angles[0], surface.migration_angles[1]) surface.to_spherical() surface.compute_spharm(grid_size=kwargs.get('grid_size'), normalize=kwargs.get('normalize'), normalization_method=kwargs.get( 'normalization_method', 'zero-component')) surface.spharm.save_to_csv(outputfolder + surface.metadata['Name']) else: surface = Surface(filename=inputfolder + filename) surface.centrate() surface.to_spherical() surface.compute_spharm(grid_size=kwargs.get('grid_size'), normalize=kwargs.get('normalize'), normalization_method=kwargs.get( 'normalization_method', 'zero-component')) if not filename.endswith('.csv'): filename += '.csv' surface.spharm.save_to_csv(outputfolder + filename)
if not path.endswith('/'): path += '/' path += 'output/' track_stat = pd.DataFrame() summary_stat = pd.DataFrame() groups = os.listdir(path + 'surfaces/') for gr in groups: print(gr) files = os.listdir(path + 'surfaces/' + gr + '/') for fn in files: stat = pd.read_csv(path + 'surfaces/' + gr + '/' + fn, sep='\t', index_col=0) t1 = stat['Time'].unique()[0] stat = stat[stat['Time'] == t1] print(fn, len(stat)) surface = Surface(data=stat) mesh = surface.plot_points(scale_factor=0.2) filelib.make_folders([os.path.dirname(path + 'surface_plots/' + gr + '_' + fn[:-4])]) mesh.save(path + 'surface_plots/' + gr + '_' + fn[:-4] + '_init.png', size=(100, 100)) mlab.clf() surface.centrate() surface.to_spherical() surface.compute_spharm(grid_size=120, normalize=True) mesh = surface.plot_surface(points=False) filelib.make_folders([os.path.dirname(path + 'surface_plots/' + gr + '_' + fn[:-4])]) mesh.save(path + 'surface_plots/' + gr + '_' + fn[:-4] + '_grid.png', size=(100, 100)) mlab.clf() surface.inverse_spharm(lmax=10) mesh = surface.plot_surface(points=False)
def plot_inverse_shapes(inputfile, outputfolder, group='Group'): """ Plot average cells shapes obtained by inverse SPHARM. Parameters ---------- inputfile : str Path to the file with spectral data. outputfolder : str Directory to save the plotted distributions. group : str, optional Column in the input data sheet to use for grouping. Default is 'Group'. """ filelib.make_folders([os.path.dirname(outputfolder)]) stat = pd.read_csv(inputfile, sep='\t', index_col=0) stat['value'] = stat['real'] + stat['imag']*1j if 'Group' not in stat.columns: for name in stat['Name'].unique(): group = name.split('/')[0] stat = stat.set_value(stat[stat['Name'] == name].index, 'Group', group) data = stat.groupby(['degree', 'order', 'Group']).mean().reset_index() groups = data[group].unique() for gr in groups: curdata = data[data[group] == gr] sp = Spectrum() sp.harmonics_csv = curdata sp.convert_to_shtools_array() surf = Surface() surf.spharm = sp maxdegree = np.max(sp.harmonics_csv['degree']) for lmax in np.arange(5, maxdegree + 1, 5): surf.inverse_spharm(lmax=lmax) surf.plot_surface(points=False).save(outputfolder + '_' + gr + '_inverse_lmax=' + str(lmax) + '.png', size=(200, 200)) surf.inverse_spharm(lmax=None) surf.plot_surface(points=False).save(outputfolder + '_' + gr + '_inverse_full.png', size=(200, 200))
def test07_spharm_inverse_less(self): path = site.getsitepackages()[0] + '/SPHARM/tests/' fn = path + 'data/synthetic_cell.txt' surf = Surface(filename=fn) surf.centrate() surf.to_spherical() surf.compute_spharm(grid_size=100) surf.inverse_spharm() os.makedirs('data/test_data') with warnings.catch_warnings(): warnings.simplefilter("ignore") surf.plot_surface(points=True).save( 'data/test_data/surface_inverse_all.png', size=(200, 200)) surf.inverse_spharm(lmax=30) surf.plot_surface(points=True).save( 'data/test_data/surface_inverse_30.png', size=(200, 200)) surf.inverse_spharm(lmax=10) surf.plot_surface(points=True).save( 'data/test_data/surface_inverse_10.png', size=(200, 200)) self.assertEqual( os.path.exists('data/test_data/surface_inverse_all.png'), True) self.assertEqual( os.path.exists('data/test_data/surface_inverse_30.png'), True) self.assertEqual( os.path.exists('data/test_data/surface_inverse_10.png'), True) shutil.rmtree('data/test_data/')
def test05_plot(self): path = site.getsitepackages()[0] + '/SPHARM/tests/' fn = path + 'data/synthetic_cell.txt' surf = Surface(filename=fn) with warnings.catch_warnings(): warnings.simplefilter("ignore") mesh = surf.plot_points() os.makedirs('data/test_data') mesh.save('data/test_data/points_3D.png', size=(200, 200)) surf.centrate() surf.to_spherical() surf.Rgrid = surf.interpolate(grid_size=100) with warnings.catch_warnings(): warnings.simplefilter("ignore") mesh = surf.plot_surface(points=False) mesh.save('data/test_data/surface_3D.png', size=(200, 200)) with warnings.catch_warnings(): warnings.simplefilter("ignore") mesh = surf.plot_surface(points=True) mesh.save('data/test_data/surface_with_points_3D.png', size=(200, 200)) self.assertEqual(os.path.exists('data/test_data/surface_3D.png'), True) self.assertEqual(os.path.exists('data/test_data/points_3D.png'), True) self.assertEqual( os.path.exists('data/test_data/surface_with_points_3D.png'), True) shutil.rmtree('data/test_data/')