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 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 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/')
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/')
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 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)
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) filelib.make_folders([os.path.dirname(path + 'surface_plots/' + gr + '_' + fn[:-4])]) mesh.save(path + 'surface_plots/' + gr + '_' + fn[:-4] + '_inverse_lmax=10.png', size=(100, 100)) mlab.clf() surface.inverse_spharm(lmax=None) mesh = surface.plot_surface(points=False) filelib.make_folders([os.path.dirname(path + 'surface_plots/' + gr + '_' + fn[:-4])])