Пример #1
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()
Пример #2
0
def plot_average_heatmaps(inputfile, outputfolder, **kwargs):
    """
    Plot heatmaps for group-averaged SPHARM spectra.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.    
    outputfolder : str
        Output directory to save the heatmaps.
    kwargs : key, value pairings
            Arbitrary keyword arguments to pass to the Spectrum.heatmap function.
    """
    filelib.make_folders([os.path.dirname(outputfolder), outputfolder + 'timepoints/'])
    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    stat.loc[:, 'Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10
    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()
    for gr in data['Group'].unique():
        curdata = data[data['Group'] == gr]
        s = Spectrum()
        s.harmonics_csv = curdata
        pl = s.heatmap(title=gr + ' average', **kwargs)
        pl.savefig(outputfolder + gr + '.png')
        pl.clf()

    # plot separate time points
    stat = stat.groupby(['Time', 'Group', 'degree', 'order']).mean().reset_index()

    for t in stat['Time'].unique():
        for gr in stat['Group'].unique():
            curdata = stat[(stat['Group'] == gr) & (stat['Time'] == t)]
            if len(curdata) > 0:
                s = Spectrum()
                s.harmonics_csv = curdata
                pl = s.heatmap(title=gr + ' average, time point ' + str(t), **kwargs)
                pl.savefig(outputfolder + 'timepoints/' + gr + '_time=' + str(t) + '.png')
                pl.clf()
Пример #3
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')
Пример #4
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')
Пример #5
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))