Esempio n. 1
0
def plot_confusion_matrix(inputfolder, outputfolder, text_to_replace):
    filelib.make_folders([outputfolder])
    files = filelib.list_subfolders(inputfolder, extensions=['csv'])
    for fn in files:
        stat = pd.read_csv(inputfolder + fn, sep='\t', index_col=0)
        classes = stat['Group'].unique()
        cl_frame = pd.DataFrame({'Class name': classes})
        for i in range(len(cl_frame)):
            cl_frame.at[
                i, 'Class code'] = stat[stat['Group'] == cl_frame.iloc[i]
                                        ['Class name']]['Actual class'].iloc[0]
            cl_frame.at[i,
                        'Class name'] = cl_frame.iloc[i]['Class name'].replace(
                            'FB', 'FR')
            for text in text_to_replace:
                cl_frame.at[
                    i, 'Class name'] = cl_frame.iloc[i]['Class name'].replace(
                        text, '')
        cl_frame = cl_frame.sort_values('Class name')
        cl_frame['New class code'] = np.arange((len(cl_frame)))

        for i in range(len(cl_frame)):
            stat.at[stat[stat['Actual class'] ==
                         cl_frame.iloc[i]['Class code']].index,
                    'Actual class'] = cl_frame.iloc[i]['Class name']
            stat.at[stat[stat['Predicted class'] ==
                         cl_frame.iloc[i]['Class code']].index,
                    'Predicted class'] = cl_frame.iloc[i]['Class name']
        plot_confusion_matrix_from_data(stat['Actual class'],
                                        stat['Predicted class'],
                                        columns=cl_frame['Class name'],
                                        outputfile=outputfolder + fn[:-4] +
                                        '.png')
        plt.close()
Esempio n. 2
0
 def save_as_stack(self, filename, voxel_size):
     """
     Save the surface as a 3D stack.
     
     Parameters
     ----------
     filename : str
         Output file name.
     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.
     """
     if self.x is not None:
         voxel_size = np.array([voxel_size]).flatten()
         if len(voxel_size) == 1:
             voxel_size = np.ones(3) * voxel_size
         filelib.make_folders([os.path.dirname(filename)])
         img = self.as_stack(voxel_size)
         with warnings.catch_warnings():
             warnings.simplefilter("ignore")
             io.imsave(filename, img.astype(np.uint8))
         metadata = pd.Series({
             'voxel_size_xy': voxel_size[2],
             'voxel_size_z': voxel_size[0]
         })
         metadata['min_x'] = self.x.min() - 1
         metadata['min_y'] = self.y.min() - 1
         metadata['min_z'] = self.z.min() - 1
         metadata.to_csv(filename[:-4] + '.txt', sep='\t', header=False)
Esempio n. 3
0
    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))
Esempio n. 4
0
    def save(self, outputfile, normalize_output=False):
        """
        Saves the image to a given file.

        Parameters
        ----------
        outputfile: str
            Path to save the image.
        normalize_output : bool, optional
            If True, the output image will be normalized to 8 bit before saving.
            Default is False.
        """
        filelib.make_folders([os.path.dirname(outputfile)])
        if not outputfile.endswith('.tif'):
            outputfile += '.tif'
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            if normalize_output:
                io.imsave(
                    outputfile,
                    rescale_intensity(self.image,
                                      out_range=(0, 255)).astype(np.uint8))
            else:
                io.imsave(outputfile, self.image.astype(np.uint32))
        self.metadata.to_csv(outputfile[:-4] + '.csv', sep='\t', header=False)
        self.filename = outputfile
def test_combined_orbitals(gridsize, path):
    value = 'amplitude'
    filelib.make_folders(
        [path + 'surfaces/', path + 'heatmaps/', path + 'frequencies/'])
    combined = [[(0, 0, 1), (2, -1, 0.1)], [(0, 0, 1),
                                            (2, -1, 0.5), (4, 3, 1)],
                [(0, 0, 1), (4, 3, 0.5)], [(0, 0, 1), (1, 0, 1)]]
    for set in combined:
        set = np.array(set)
        m = set[:, 0]
        n = set[:, 1]
        amplitude = set[:, 2]

        orbital = Orbital(grid_shape=(gridsize, gridsize),
                          m=m,
                          n=n,
                          amplitude=amplitude)
        orbital.name = 'm=' + str(m) + '_n=' + str(n) + '_amplitude=' + str(
            amplitude)

        sp = orbital.compute_spharm(grid_size=gridsize)
        sp.heatmap(value=value, cutoff=5).savefig(path + 'heatmaps/' +
                                                  orbital.name + '.png')
        plt.clf()
        sp.frequency_plot(value=value).savefig(path + 'frequencies/' +
                                               orbital.name + '.png')
        plt.clf()

        orbital.Rgrid = orbital.Rgrid.real
        mesh = orbital.plot_surface(points=False)
        mesh.save(path + 'surfaces/' + orbital.name + '.png', size=(200, 200))
def test_single_orbitals(gridsize, path):
    value = 'amplitude'
    filelib.make_folders(
        [path + 'surfaces/', path + 'heatmaps/', path + 'frequencies/'])
    for m in range(5):
        for n in range(-m, m + 1):

            orbital = Orbital(grid_shape=(gridsize, gridsize),
                              m=m,
                              n=n,
                              amplitude=1)
            orbital.name = 'm=' + str(m) + '_n=' + str(n)

            sp = orbital.compute_spharm(grid_size=gridsize)
            sp.heatmap(value=value, cutoff=5).savefig(path + 'heatmaps/' +
                                                      orbital.name + '.png')
            plt.clf()
            sp.frequency_plot(value=value,
                              cutoff=5).savefig(path + 'frequencies/' +
                                                orbital.name + '.png')
            plt.clf()

            orbital.Rgrid = orbital.Rgrid.real
            mesh = orbital.plot_surface(points=False)
            mesh.save(path + 'surfaces/' + orbital.name + '.png',
                      size=(200, 200))
def extract_coordinates(inputfile, outputfile):
    """
    Extract cell coordinates from a given vrml file.
    
    Parameters
    ----------
    inputfile : str
        Path to a vrml or wrl file with cell coordinates.
    outputfile : str
        Path to save the extracted coordinates in a table form.
    """

    stat = pd.DataFrame()
    curcoords = []
    timepoint = 0
    node_id = 0

    nodes = extract_key_nodes(inputfile, key='children')
    for node in nodes:
        if node.children[0].name == 'Shape':
            timepoint += 1
            for subnode1 in node.children:
                for subnode in subnode1.children:
                    if subnode.name == 'IndexedFaceSet':
                        curstat, curcoords = subnode.extract_coordinates(
                            curcoords)
                        curstat['ID'] = node_id
                        curstat['Time'] = timepoint
                        stat = pd.concat([stat, curstat], ignore_index=True)
                        node_id += 1

    filelib.make_folders([os.path.dirname(outputfile)])
    stat.to_csv(outputfile, sep='\t')
Esempio n. 8
0
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 plot_boxplots(statname, outputfolder, **kwargs):

    # sns.set(style="ticks")
    filelib.make_folders([outputfolder])
    margins = kwargs.pop('margins', None)

    exclude = ['Image_name', 'Group']

    stat = pd.read_csv(statname, sep='\t')

    stat = stat.sort_values(['Group']).reset_index(drop=True)
    rotation = kwargs.pop('rotation', 0)

    columns = []
    for c in stat.columns:
        if c not in exclude:
            columns.append(c)

    for c in columns:
        substat = stat[stat[c] > -10000]

        pl = sns.catplot(x='Group', y=c, data=substat, **kwargs)
        pl.set_xticklabels(rotation=rotation)
        if margins is not None:
            plt.subplots_adjust(**margins)
        pl.savefig(outputfolder + c.replace(' ', '_') + '.png')
        plt.close()
Esempio n. 10
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 extract_surfaces(self,
                         outputfolder,
                         voxel_size=1,
                         reconstruct=True,
                         min_coord=None):
        """
        Extract surface coordinates of each connected region.
        
        Parameters
        ----------
        outputfolder : str
            Path to a directory to save the surfaces.
        voxel_size : scalar or sequence of scalars, optional
            Voxel size of the image. 
            Specified either by individual value for each axis, or by one value for all axes.
            Default is 1.
        reconstruct : bool, optional
            If True, surfaces will be reconstructed by the marching cube algorithm, 
              and coordiantes of the vertices will be extracted.
            If False, coordinates of the voxels connected to the background will be extracted.
            Default is True.
        min_coord : sequence of scalars, optional
            Starting coordinates of the surface.
            Three values: for z, y, and x are expected.
            In not None, these values will be added to all surface coordinates.
        """

        voxel_size = np.array([voxel_size]).flatten()
        if len(voxel_size) < 3:
            voxel_size = [voxel_size[0]] * 3
        filelib.make_folders([outputfolder + os.path.dirname(self.filename)])

        llist = np.unique(self.data)[1:]

        if not reconstruct:
            border = find_boundaries(self.data) * self.data

        for i, l in enumerate(llist):
            if reconstruct:
                mask = np.where(self.data == l, 1, 0)
                verts = np.array(
                    measure.marching_cubes_lewiner(
                        mask, 0, spacing=tuple(voxel_size))[0])
                verts = verts.transpose()
            else:
                verts = np.array(np.where(border == l))
                for iv in range(3):
                    verts[iv] = verts[iv] * voxel_size[iv]
            if min_coord is not None:
                for i in range(3):
                    verts[i] += min_coord[i]
            stat = pd.DataFrame({'Z': verts[0], 'Y': verts[1], 'X': verts[2]})
            stat['Cell_ID'] = l
            stat['Image_name'] = self.filename
            if len(stat) > 0:
                stat.to_csv(outputfolder + self.filename[:-4] +
                            '_Cell%05d.csv' % l,
                            sep='\t')
Esempio n. 12
0
def plot_heatmaps(size, rotation, gridsize, path, value='power', cutoff=None, normalize=False, ri=False):

    filelib.make_folders([path + 'heatmaps/', path + 'frequencies/'])
    ell = Ellipsoid(grid_shape=(gridsize, gridsize), size=size, rotation=rotation)
    sp = ell.compute_spharm(grid_size=gridsize, normalize=normalize, normalization_method='zero-component', ri=ri)
    sp.save_to_csv(path + 'heatmaps/' + ell.name + '.csv')
    sp.heatmap(value=value, cutoff=cutoff).savefig(path + 'heatmaps/' + ell.name + '.png')
    plt.clf()
    sp.frequency_plot(value=value).savefig(path + 'frequencies/' + ell.name + '.png')
    plt.clf()
def split_parameters(filename, outputfolder):
    filelib.make_folders([outputfolder])
    stat = pd.read_csv(filename, sep='\t', index_col=0)
    for pw in stat['PosWeight'].unique():
        for nw in stat['NWeight'].unique():
            curstat = stat[(stat['PosWeight'] == pw) & (stat['NWeight'] == nw)].reset_index()
            curstat.to_csv(outputfolder + 'PosWeight=' + str(pw) + '_NWeight=' + str(nw) + '.csv', sep='\t')

    for fb in stat['FrontBack'].unique():
        curstat = stat[stat['FrontBack'] == fb].reset_index()
        curstat.to_csv(outputfolder + 'FrontBack=' + str(fb) + '.csv', sep='\t')
Esempio n. 14
0
    def save(self, outputfile):
        """
        Saves the cell parameters to a csv file

        Parameters
        ----------
        outputfile: str
            The path used to save the cell parameters.
        """
        filelib.make_folders([os.path.dirname(outputfile)])
        self.to_csv(outputfile, sep='\t')
Esempio n. 15
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 split_to_surfaces(inputfile,
                      outputfolder,
                      combine_tracks=False,
                      adjust_frame_rate=False,
                      metadata_file=None):
    """
    Split one surface file into separate files for surfaces of individual cells.
    
    Parameters
    ----------
    inputfile : str
        Input surface file.
    outputfolder : str
        Directory to save the output surface files.
    combine_tracks : bool, optional
        If True, connected time points will be combined into one file.
        Default is False.
    """
    filelib.make_folders([outputfolder])
    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    framerate = None
    if adjust_frame_rate:
        parts = inputfile.split('/')[-1].split('_')
        stem = parts[0] + '_' + parts[1]
        metadata = pd.read_csv(metadata_file, sep='\t')
        ind = metadata[metadata['Sample'] == stem].index[0]
        framerate = metadata.loc[ind, 'time']

    for track_id in stat['TrackID'].unique():
        combined_stat = pd.DataFrame()
        ntime = 1
        for t in stat['Time'].unique():
            curstat = stat[(stat['TrackID'] == track_id)
                           & (stat['Time'] == t)].reset_index()
            if adjust_frame_rate and framerate == 20:
                if (t - 1) % 3:
                    curstat = pd.DataFrame()
                else:
                    curstat['Time'] = ntime
                    ntime += 1
            if combine_tracks:
                combined_stat = pd.concat([combined_stat, curstat],
                                          ignore_index=True)
            else:
                if len(curstat) > 0:
                    curstat.to_csv(outputfolder + 'Track_' +
                                   str(int(track_id)) + '_Time_%03d.csv' % t,
                                   sep='\t')
        if combine_tracks and len(combined_stat) > 0:
            combined_stat.to_csv(outputfolder + 'Track_' + str(int(track_id)) +
                                 '.csv',
                                 sep='\t')
Esempio n. 17
0
def plot_pairplots(inputfile, outputfolder, group='Group', cutoff=None):
    """
    Plot pairwise distributions of different spherical components in different groups.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.
    outputfolder : str
        Directory to save the plotted distributions.
    cutoff : int, optional
        The number of degrees to display.
        If None, all degrees will be displayed.
    group : str, optional
        Column in the input data sheet to use for grouping.
        Default is 'Group'.
    """
    filelib.make_folders([os.path.dirname(outputfolder), outputfolder + 'timepoints/'])

    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    if cutoff:
        stat = stat[stat.degree < cutoff]
    else:
        stat = stat[stat['degree'] < 7]

    stat['Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10

    nstat = pd.DataFrame()

    times = stat.Time.unique()
    times.sort()
    for t in times:
        curstat = stat[stat.Time == t]

        pivoted = curstat.pivot(index='Name', columns='degree', values='amplitude')
        pivoted['Name'] = pivoted.index

        for i in pivoted['Name'].unique():
            pivoted.at[pivoted[pivoted['Name'] == i].index, group] = stat[stat['Name'] == i][group].iloc[0]

        pivoted = pivoted.drop('Name', 1)
        if len(pivoted) > 0:
            f = sns.pairplot(pivoted, hue=group)
            f.savefig(outputfolder + 'timepoints/time=' + str(t) + 's.png')
            plt.close()

        nstat = pd.concat([nstat, pivoted], ignore_index=True)

    if len(nstat) > 0:
        f = sns.pairplot(nstat, hue=group)
        f.savefig(outputfolder + 'all_time_points.png')
        plt.close()
def distributions(inputfile,
                  outputfolder,
                  condition='Group',
                  exclude=None,
                  sort=False,
                  sort_key=None,
                  bins=10,
                  normed=False,
                  maxper=100,
                  separate_plots=False,
                  **kwargs):
    filelib.make_folders([outputfolder])

    extension = '.' + kwargs.pop('extension', 'png')
    if exclude is None:
        exclude = [condition]

    stat = pd.read_csv(inputfile, sep='\t')

    if sort:
        if sort_key is None:
            sort_key = [condition]
        stat = stat.sort(sort_key).reset_index(drop=True)

    ncols = kwargs.get('ncols', 7)

    for value in stat.columns:
        if value not in exclude:
            r = (stat[value].min(), np.nanpercentile(stat[value], maxper))
            if separate_plots:
                for c in stat[condition].unique():
                    curstat = stat[stat[condition] == c]
                    plt.hist(curstat[value],
                             bins=bins,
                             density=normed,
                             range=r)
                    plt.xlabel(value + ' (' + c + ')')
                    name = value.replace(' ', '_').replace('$', '').replace('/', '').replace('\\', '').replace(',', '') \
                           + '_' + c
                    plt.savefig(outputfolder + name + extension)
                    plt.close()

            else:
                g = sns.FacetGrid(stat, col=condition, col_wrap=ncols)
                g = g.map(plt.hist, value, bins=bins, density=normed, range=r)
                g.set_titles(col_template="{col_name}", fontsize=18)

                name = value.replace(' ', '_').replace('$', '').replace(
                    '/', '').replace('\\', '').replace(',', '')
                g.fig.savefig(outputfolder + name + extension)
                plt.close()
Esempio n. 19
0
def plot_average_spectra(inputfile, outputfolder, value='amplitude', group='Group', norm=False, cutoff=None):
    """
    Plot frequency spectra of spherical components.

    Parameters
    ----------
    inputfile : str
        Path to the file with spectral data.
    outputfolder : str
        Directory to save the plotted spectra.
    value : str, optional
        Part of the complex spectrum to plot.
        Valid values: 'amplitude', 'power', 'real', 'imag'.
        Default is 'amplitude'.
    norm : bool, optional
            If True, each component of the frequency spectrum will be divided by the value of the zero frequency.
            Default is False.        
    cutoff : int, optional
        The number of degrees to display.
        If None, all degrees will be displayed.
    group : str, optional
        Column in the input data sheet to use for grouping.
        Default is 'Group'.
    """

    filelib.make_folders([os.path.dirname(outputfolder), outputfolder + 'timepoints/'])

    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    if norm:
        stat.loc[:, value] = np.array(stat[value]) / stat[value].iloc[0]
    if cutoff:
        stat = stat[stat.degree < cutoff]

    stat.loc[:, 'Time'] = np.int_(np.round_(stat['Time'] / 10.)) * 10

    sns.catplot(x='degree', y=value, data=stat, kind='bar', hue=group, estimator=np.nanmedian, ci=90)

    plt.savefig(outputfolder + '_All_time_points.png')
    plt.close()

    # plot separate time points

    times = stat.Time.unique()
    times.sort()

    for t in times:
        curstat = stat[stat.Time == t]
        sns.catplot(x='degree', y=value, data=curstat, kind='bar', hue=group, estimator=np.nanmedian, ci=90)

        plt.savefig(outputfolder + 'timepoints/time=' + str(t) + 's.png')
        plt.close()
Esempio n. 20
0
 def save_to_csv(self, filename):
     """
     Save the table form of the spectrum to a csv file.
     
     Parameters
     ----------
     filename : str
         Path to the output file.
     """
     if filename is not None:
         filelib.make_folders([os.path.dirname(filename)])
         for col in self.metadata.index:
             self.harmonics_csv[col] = self.metadata[col]
         self.harmonics_csv.to_csv(filename, sep='\t')
 def save(self, outputfile):
     """
     Plot the profile and save to a given file.
     
     Parameters
     ----------
     outputfile : str
         Output file name to save the plotted profile.
     """
     make_folders([os.path.dirname(outputfile)])
     plt.clf()
     ax = plt.subplot(111, projection='polar')
     ax.plot(self.theta, self.R, color='r', linewidth=3)
     plt.savefig(outputfile)
Esempio n. 22
0
    def save(self, filename):
        """
        Save the surface coordinates to a csv file.
        
        Parameters
        ----------
        filename : str
            Output file name.
        """
        if self.x is not None:
            filelib.make_folders([os.path.dirname(filename)])
            stat = pd.DataFrame({'X': self.x, 'Y': self.y, 'Z': self.z})
            stat['Name'] = self.filename

            stat.to_csv(filename, sep='\t')
Esempio n. 23
0
    def save_projection(self, outputfile, axis=1):
        """
        Saves the maximum intensity projection of the stack.

        Parameters
        ----------
        outputfile : str
            The path used to save the maximum intensity projection.
        axis : int, optional
            Axis along which the projection should be made.
            Default is 1 (xz).
        """
        filelib.make_folders([os.path.dirname(outputfile)])
        maxproj = np.max(self.image, axis=axis)
        io.imsave(outputfile, maxproj.astype(np.uint8))
def extract_node_names(inputfile, outputfile=None):
    """
    Extract the names of the nodes from vrml file.
    
    Parameters
    ----------
    inputfile : str
        Path to the vrml or wrl file.
    outputfile : str, optional
        Path to the output file to save the node names.
    """
    f = open(inputfile)
    st = f.readlines()

    st = ''.join(st).replace('\t', ' ').replace('\n', ' ').replace('\r', ' ')
    pairs = {'{': '}', '[': ']', '(': ')'}
    root = Node(('root', ''))
    stack = []
    k = 0
    for j, s in enumerate(st):
        if s in pairs:
            parts = st[k:j].split(' ')
            name = ''
            for i in range(1, len(parts)):
                if len(parts[-i]) > 0:
                    name = parts[-i]
                    break
            node = Node((name, pairs[s]))
            if len(stack) > 0:
                stack[-1].add_text(st[k + 1:j])
            stack.append(node)
            k = j

        elif len(stack) > 0 and s == stack[-1].bracket:
            node = stack.pop()
            node.add_text(st[k + 1:j])
            if len(stack) > 0:
                stack[-1].add_child(node)
            else:
                root.add_child(node)
            k = j
    if outputfile is None:
        outputfile = inputfile[:-4] + '_nodes.txt'
    filelib.make_folders([os.path.dirname(outputfile)])
    f = open(outputfile, 'w')
    root.print_children(outputfile=f)

    f.close()
Esempio n. 25
0
def save_fiji_version(outputfolder):
    """
    Save the version of the Fiji software into a `version.txt` file in a give directory.

    Parameters
    ----------
    outputfolder : str
        Directory to store the Fiji version.

    """
    filelib.make_folders([outputfolder])
    if not outputfolder.endswith('/'):
        outputfolder += '/'
    os.system(get_fiji_path() +
              ' --headless --console -macro get_fiji_version.ijm "' +
              outputfolder + '"')
def combine_with_track_data(inputfile, trackfile, outputfile=None):
    """
    Add track IDs to the extracted coordinates.
    
    Parameters
    ----------
    inputfile : str
        Path to a file with extracted cell coordinates.
    trackfile : str
        Path to a file with track IDs.
    outputfile : str
        Path to the output file.
    """
    stat = pd.read_csv(inputfile, sep='\t', index_col=0)
    if 'ID' not in stat.columns:
        stat['ID'] = stat['Cell_ID']
    summary = stat.groupby(['ID', 'Time']).mean().reset_index()
    trackstat = pd.read_excel(trackfile, sheet_name='Position', header=1)
    if 'Time' not in trackstat.columns:
        trackstat['Time'] = trackstat['Death [s]']

    for t in trackstat['Time'].unique():
        curstat = summary[summary['Time'] == t].reset_index()
        curtrackstat = trackstat[trackstat['Time'] == t].reset_index()
        for i in range(len(curtrackstat)):
            dist = np.sqrt((curstat['X'] -
                            np.array(curtrackstat.iloc[i]['Position X']))**2 +
                           (curstat['Y'] -
                            np.array(curtrackstat.iloc[i]['Position Y']))**2 +
                           (curstat['Z'] -
                            np.array(curtrackstat.iloc[i]['Position Z']))**2)
            track_id = curtrackstat.iloc[i]['TrackID']
            if len(dist) > 0:
                ind = np.argmin(dist)
                stat.at[stat[(stat['ID'] == curstat.iloc[ind]['ID'])
                             & (stat['Time'] == t)].index,
                        'TrackID'] = track_id
            else:
                print(trackfile, t, track_id)

    if outputfile is None:
        outputfile = inputfile[:-4] + '_tracked.csv'
    filelib.make_folders([os.path.dirname(outputfile)])
    stat.to_csv(outputfile, sep='\t')
 def save_max_projection(self, filename, axis=0):
     """
     Save maximum projection of the image on a given axis.
     
     Parameters
     ----------
     filename : str 
         Path to save the maximum projection.
     axis : int, optional
         Axis for the maximum projection.
         Default is 0.
     """
     maxproj = np.max(self.data, axis=axis)
     filelib.make_folders([os.path.dirname(filename)])
     if maxproj.max() > 255:
         maxproj = rescale_intensity(maxproj, out_range=(0, 255))
     with warnings.catch_warnings():
         warnings.simplefilter("ignore")
         io.imsave(filename, maxproj.astype(np.uint8))
Esempio n. 28
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')
Esempio n. 29
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()
Esempio n. 30
0
 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))