Beispiel #1
0
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 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/')
                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])])
                mesh.save(path + 'surface_plots/' + gr + '_' + fn[:-4] + '_inverse.png', size=(100, 100))
                mlab.clf()