def test_fourier(self):
     sp = Spectrum()
     sp.from_surface(surface=np.ones([10, 10]))
     sp.convert_to_csv()
     tsp = TimeSpectrum()
     for i in range(10):
         tsp.add_spectrum(sp)
     tsp.fourier_analysis()
     tsp.save_frequencies_to_csv('data/test_data/time_spectrum_freq.csv')
     self.assertEqual(os.path.exists('data/test_data/time_spectrum_freq.csv'), True)
     pl = tsp.frequency_heatmap()
     pl.savefig('data/test_data/frequency_heatmap.png')
     self.assertEqual(os.path.exists('data/test_data/frequency_heatmap.png'), True)
     shutil.rmtree('data/test_data/')
Exemple #2
0
def plot_individual_time_heatmaps(inputfile, outputfolder, group='Group', cutoff=None,
                                  logscale=False, id_col='TrackID'):
    """
    Plot the amplitude of spectral components over time for different groups as a heatmap.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.
    outputfolder : str
        Directory to save the plotted heat maps.
    group : str, optional
        Column in the input data sheet to use for grouping.
        Default is 'Group'.
    cutoff : int, optional
        The number of degrees to display.
        If None, all degrees will be displayed.
    logscale : bool, optional
        If True, the natural logarithm of the value will be displayed.
        Default is False.
    id_col : str
        Column in the input data sheet to group connected time points.
        Default is 'TrackID'
    """
    filelib.make_folders([os.path.dirname(outputfolder)])
    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    stat['Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10
    if cutoff is not None:
        stat = stat[stat['degree'] <= cutoff]

    for gr in stat[group].unique():
        substat = stat[stat[group] == gr]
        for id in substat[id_col].unique():
            subsubstat = substat[substat[id_col] == id]
            subsubstat = subsubstat.sort_values('Time').reset_index()
            time_spectrum = TimeSpectrum()
            for t in subsubstat['Time'].unique():
                sp = Spectrum()
                sp.harmonics_csv = subsubstat[subsubstat['Time'] == t]
                time_spectrum.add_spectrum(sp, timepoint=t)
            pl = time_spectrum.time_heatmap(value='amplitude', logscale=logscale)
            if pl is not None:
                pl.savefig(outputfolder + '_' + gr + '_' + 'track_' + str(id) + '.png')
Exemple #3
0
 def __init__(self, name=None):
     """
     Initiate the time series for the surface.
     
     Parameters
     ----------
     name : str, optional
         Name of the surface to add to the output file when saving results. 
         Default is None.
     """
     if name is None:
         self.name = ''
     else:
         self.name = name
     self.surfaces = []
     self.times = []
     self.timespectrum = TimeSpectrum(name=name)
     self.minmax = np.array([[1000., -1000.], [1000., -1000.],
                             [1000., -1000.]])
     self.centers = []
Exemple #4
0
def plot_mean_abs_derivative(inputfile, outputfolder, group='Group', cutoff=None, id_col='TrackID'):
    """
    Plot the derivative of each spectral component of different groups over time.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.
    outputfolder : str
        Directory to save the plotted derivative.
    group : str, optional
        Column in the input data sheet to use for grouping.
        Default is 'Group'.
    cutoff : int, optional
        The number of degrees to display.
        If None, all degrees will be displayed.
    id_col : str
        Column in the input data sheet to group connected time points.
        Default is 'TrackID'
    """
    filelib.make_folders([os.path.dirname(outputfolder)])
    if not os.path.exists(inputfile[:-4] + '_mean_abs_derivative.csv'):
        stat = pd.read_csv(inputfile, sep='\t', index_col=0)
        if id_col == 'CellID':
            stat.loc[:, 'Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10
        nstat = pd.DataFrame()
        if cutoff is not None:
            stat = stat[stat['degree'] <= cutoff]
        for gr in stat[group].unique():
            substat = stat[stat[group] == gr]
            for id in substat[id_col].unique():
                subsubstat = substat[substat[id_col] == id]
                subsubstat = subsubstat.sort_values('Time')
                time_spectrum = TimeSpectrum()
                for t in subsubstat['Time'].unique():
                    sp = Spectrum()
                    sp.harmonics_csv = subsubstat[subsubstat['Time'] == t]
                    time_spectrum.add_spectrum(sp, timepoint=t)

                time_spectrum.compute_derivative()
                meanderivstat = time_spectrum.mean_abs_derivative
                meanderivstat['Group'] = gr
                meanderivstat['TrackID'] = id
                nstat = pd.concat([nstat, meanderivstat], ignore_index=True)

        nstat.to_csv(inputfile[:-4] + '_mean_abs_derivative.csv', sep='\t')
    nstat = pd.read_csv(inputfile[:-4] + '_mean_abs_derivative.csv', sep='\t', index_col=0)
    nstat = nstat.sort_values(['harmonic', group])
    plt.clf()
    plt.figure(figsize=(20, 5))
    sns.barplot(x='harmonic', y='absolute amplitude', data=nstat, hue=group)
    plt.ylabel('Mean absolute derivative of amplitude')
    labels = nstat['harmonic'].unique()
    plt.xticks(np.arange(len(labels)) + 0.6, labels, rotation='vertical')
    margins = {'left': 0.07, 'right': 0.98, 'top': 0.93, 'bottom': 0.25}
    plt.subplots_adjust(**margins)
    plt.savefig(outputfolder + 'mean_abs_derivative.png')
    plt.close()
Exemple #5
0
class MovingSurface(object):
    """
    Class for storing the dynamics of an object surface.
    """
    def __init__(self, name=None):
        """
        Initiate the time series for the surface.
        
        Parameters
        ----------
        name : str, optional
            Name of the surface to add to the output file when saving results. 
            Default is None.
        """
        if name is None:
            self.name = ''
        else:
            self.name = name
        self.surfaces = []
        self.times = []
        self.timespectrum = TimeSpectrum(name=name)
        self.minmax = np.array([[1000., -1000.], [1000., -1000.],
                                [1000., -1000.]])
        self.centers = []

    def add_surface(self, surface, timepoint=None):
        """
        Add new surface to the time series.
        
        Parameters
        ----------
        surface : Surface
            Object surface at one time point.
        timepoint : scalar, optional
            Time point to assign to the surface.
            If None, the number of previously added surfaces will be used to label the time point 
             (e.g. 0 if none were added).
            Default is None.
        """
        self.surfaces.append(surface)
        if timepoint is None:
            self.times.append(len(self.surfaces))
        else:
            self.times.append(timepoint)
        self.minmax[0, 0] = min(self.minmax[0, 0], np.min(surface.z))
        self.minmax[1, 0] = min(self.minmax[1, 0], np.min(surface.y))
        self.minmax[2, 0] = min(self.minmax[2, 0], np.min(surface.x))
        self.minmax[0, 1] = max(self.minmax[0, 1], np.max(surface.z))
        self.minmax[1, 1] = max(self.minmax[1, 1], np.max(surface.y))
        self.minmax[2, 1] = max(self.minmax[2, 1], np.max(surface.x))
        self.centers.append(surface.center)

    def compute_timespectrum(self, gridsize):
        """
        Compute a SPHARM spectrum for each surface and generate a TimeSpectrum object.
        
        Parameters
        ----------
        gridsize : int, optional
            Dimension of the square grid to interpolate the surface points.
            Will be used to interpolate the surface coordinates if self.Rgrid is None 
             (in this case it is a mandatory parameter).
            Default is None.
        """
        for i, surface in enumerate(self.surfaces):
            surface.compute_spharm(grid_size=gridsize)
            self.timespectrum.add_spectrum(surface.spharm,
                                           timepoint=self.times[i])

    def plot_surfaces(self, outputfolder, points=False):
        """
        Plot 3D views of all surfaces with mayavi and save to png files.
        
        Parameters
        ----------
        outputfolder : str
            Directory to save the plots.
        points : bool, optional
            If True, surface points will be displayed.
            Default is False.
        """
        filelib.make_folders([outputfolder])
        extent = np.array([self.minmax[2], self.minmax[1],
                           self.minmax[0]]).flatten()
        for i, surface in enumerate(self.surfaces):
            mesh = surface.plot_surface(points=points, extent=extent)
            mesh.magnification = 3
            mesh.save(outputfolder + self.name + '_%03d.png' % self.times[i],
                      size=(200, 200))

    def plot_max_projections(self, outputfolder, voxel_size):
        """
        Plot maxium projections of all surfaces and save to png files.
        
        Parameters
        ----------
        outputfolder : str
            Directory to save the plots.
        voxel_size : scalar or sequence of scalars
            Voxel size of the image. 
            Specified either by individual value for each axis, or by one value for all axes.
        """
        filelib.make_folders([outputfolder])
        voxel_size = np.array([voxel_size]).flatten()
        if len(voxel_size) == 1:
            voxel_size = np.ones(3) * voxel_size
        for i, surface in enumerate(self.surfaces):
            stack = surface.as_stack(voxel_size=voxel_size, minmax=self.minmax)

            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                io.imsave(
                    outputfolder + 'xy_' + self.name +
                    '_%03d.png' % self.times[i],
                    stack.max(0).astype(np.uint8))
                io.imsave(
                    outputfolder + 'xz_' + self.name +
                    '_%03d.png' % self.times[i],
                    stack.max(1).astype(np.uint8))
                io.imsave(
                    outputfolder + 'yz_' + self.name +
                    '_%03d.png' % self.times[i],
                    stack.max(2).astype(np.uint8))
Exemple #6
0
def plot_average_frequency_heatmaps(inputfile, outputfolder, group='Group', cutoff=None,
                                    logscale=False, id_col='TrackID'):
    """
    Plot the Fourier frequencies of spectral components of different groups as a heatmap.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.
    outputfolder : str
        Directory to save the plotted heat maps.
    group : str, optional
        Column in the input data sheet to use for grouping.
        Default is 'Group'.
    cutoff : int, optional
        The number of degrees to display.
        If None, all degrees will be displayed.
    logscale : bool, optional
        If True, the natural logarithm of the value will be displayed.
        Default is False.
    id_col : str
        Column in the input data sheet to group connected time points.
        Default is 'TrackID'
    """
    filelib.make_folders([os.path.dirname(outputfolder)])
    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    stat.loc[:, 'Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10
    if cutoff is not None:
        stat = stat[stat['degree'] <= cutoff]
    frequency_stat = pd.DataFrame()
    for gr in stat[group].unique():
        substat = stat[stat[group] == gr]
        for id in substat[id_col].unique():
            subsubstat = substat[substat[id_col] == id]
            time_spectrum = TimeSpectrum()
            for t in subsubstat['Time'].unique():
                sp = Spectrum()
                sp.harmonics_csv = subsubstat[subsubstat['Time'] == t]
                time_spectrum.add_spectrum(sp, timepoint=t)

            time_spectrum.fourier_analysis(value='amplitude')
            time_spectrum.frequencies['Group'] = gr
            frequency_stat = pd.concat([frequency_stat, time_spectrum.frequencies], ignore_index=True)

    frequency_stat = frequency_stat.groupby(['Group', 'frequency', 'harmonic']).mean().reset_index()
    for gr in stat[group].unique():
        time_spectrum = TimeSpectrum()
        time_spectrum.frequencies = frequency_stat[frequency_stat['Group'] == gr]

        pl = time_spectrum.frequency_heatmap(value='amplitude', logscale=logscale)
        if pl is not None:
            pl.savefig(outputfolder + gr + '.png')
    def test_feature_vector(self):
        surf = np.ones([10, 10])
        sp = Spectrum()
        sp.from_surface(surface=surf)
        sp.convert_to_csv()

        surf[3:4, 4:8] = 10
        sp2 = Spectrum()
        sp2.from_surface(surface=surf)
        sp2.convert_to_csv()

        tsp = TimeSpectrum()
        tsp.add_spectrum(sp)
        tsp.add_spectrum(sp2)
        tsp.add_spectrum(sp)
        tsp.compute_derivative()
        self.assertEqual(len(tsp.return_feature_vector(cutoff=2)), 3)
    def test_add_spectrum_and_plotting(self):
        tsp = TimeSpectrum()

        sp = Spectrum()
        sp.from_surface(surface=np.ones([10, 10]))
        sp.convert_to_csv()
        for i in range(3):
            tsp.add_spectrum(sp, timepoint=i*20)
        sp = Spectrum()
        surf = np.ones([10, 10])
        surf[3:4, 4:8] = 10
        sp.from_surface(surface=surf)
        sp.convert_to_csv()
        for i in range(2):
            tsp.add_spectrum(sp, timepoint=i*20+60)

        sp = Spectrum()
        sp.from_surface(surface=np.ones([10, 10]))
        sp.convert_to_csv()
        for i in range(3):
            tsp.add_spectrum(sp, timepoint=i*20+100)

        self.assertEqual(len(tsp.spectra), 8)
        tsp.save_to_csv('data/test_data/time_spectrum.csv')
        self.assertEqual(os.path.exists('data/test_data/time_spectrum.csv'), True)
        pl = tsp.time_heatmap()
        pl.savefig('data/test_data/time_heatmap.png')
        self.assertEqual(os.path.exists('data/test_data/time_heatmap.png'), True)

        tsp.compute_derivative()
        pl = tsp.derivative_heatmap()
        pl.savefig('data/test_data/derivative_heatmap.png')
        self.assertEqual(os.path.exists('data/test_data/derivative_heatmap.png'), True)

        pl = tsp.plot_mean_abs_derivative()
        pl.savefig('data/test_data/mean_abs_derivative.png')
        self.assertEqual(os.path.exists('data/test_data/mean_abs_derivative.png'), True)
        shutil.rmtree('data/test_data/')