Exemple #1
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')
Exemple #2
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()
 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/')
    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)
Exemple #5
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 #6
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 = []
    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/')