def make_ica_maps(data, imgs, img_size_x, img_size_y, num_ica_colors, color_map, colors_ica):
    
    reference = data.seriesMean().pack()
    maps = Colorize(cmap=color_map, colors = colors_ica[0:np.size(imgs,0)], scale=num_ica_colors).transform(abs(imgs),background=reference, mixing=1.5)
        
    #Count number of unique colors in the images
    #Get number of planes based on map dimesnions
    if len(maps.shape)==3:
        num_planes = 1
    else:
        num_planes = np.size(maps,2)
        
    unique_clrs = []
    for ii in xrange(0, np.size(colors_ica[0:np.size(imgs,0)])):
        unique_clrs.append( np.round(np.array(webcolors.name_to_rgb(colors_ica[ii]), dtype=np.float)/255))
    
    #From maps get number of pixel matches with color for each plane
    matched_pixels = np.zeros((np.size(unique_clrs,0),num_planes))
    array_maps = np.round(maps.astype(np.float16))
    matched_pixels = np.zeros((np.size(unique_clrs,0),num_planes))
    if len(maps.shape) == 3:
        array_maps_plane = np.reshape(array_maps, (np.size(array_maps,0)*np.size(array_maps,1),3))
        matched_pixels[:,0] = [np.size(np.where((np.array(array_maps_plane) == match).all(axis=1))) for match in unique_clrs]
    else:     
        for ii in xrange(0,num_planes):
            array_maps_plane = np.reshape(array_maps[:,:,ii,:], (np.size(array_maps,0)*np.size(array_maps,1),3))
            matched_pixels[:,ii] = [np.size(np.where((np.array(array_maps_plane) == match).all(axis=1))) for match in unique_clrs]
                 
    
    return maps, matched_pixels
    def plot_all_together_inseperateplot(self,
                                         fig1,
                                         gs,
                                         regressors,
                                         rsq,
                                         b,
                                         colors,
                                         brightness_scale=3,
                                         gridspecs='[0, 0]'):

        b_pos = b * (b > 0)  # Get positive beta values
        # Set up colorize function
        c = Colorize(cmap='indexed', scale=brightness_scale, flag_scale=1)

        b_pos_list = []
        color_mat = []
        for ii in xrange(0, len(regressors)):
            b_pos_list.append(b_pos[ii, :, :])
            color_mat.append(colors[ii])
        ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')
        c.colors = color_mat
        img = c.transform(b_pos_list,
                          mask=rsq,
                          background=self.reference,
                          mixing=0.5)
        self.image(img)
    def plot_regressors_as_RGB(self, fig1, gs, regressors, rsq, b, colors, brightness_scale=3):

        b_pos = b * (b > 0)  # Get positive beta values
        # Set up colorize function
        c = Colorize(cmap="indexed", scale=brightness_scale, flag_scale=1)

        for ii in xrange(0, len(regressors)):
            ax1 = fig1.add_subplot(gs[0, ii])
            c.colors = [colors[ii]]
            img = c.transform([b_pos[ii, :, :]], mask=rsq, background=self.reference, mixing=0.5)
            self.image(img)

        # Plot the unique stimuli in subplots
        subplot_count = 0
        for ii in self.unique_stimuli:
            b_pos_list = []
            color_mat = []
            for keys in regressors.iterkeys():
                if ii in keys:
                    index = regressors.keys().index(keys)
                    b_pos_list.append(b_pos[index, :, :])
                    color_mat.append(colors[index])
            ax1 = fig1.add_subplot(gs[1, subplot_count])
            c.colors = color_mat
            img = c.transform(b_pos_list, mask=rsq, background=self.reference, mixing=0.5)
            self.image(img)
            subplot_count += 1

        # Plot the different stimuli parameters in subplots
        for ii in self.parameters:
            b_pos_list = []
            color_mat = []
            for keys in regressors.iterkeys():
                if ii in keys:
                    index = regressors.keys().index(keys)
                    b_pos_list.append(b_pos[index, :, :])
                    color_mat.append(colors[index])
            ax1 = fig1.add_subplot(gs[1, subplot_count])
            c.colors = color_mat
            img = c.transform(b_pos_list, mask=rsq, background=self.reference, mixing=0.5)
            self.image(img)
            subplot_count += 1

        # Plot all together
        b_pos_list = []
        color_mat = []
        for ii in xrange(0, len(regressors)):
            b_pos_list.append(b_pos[ii, :, :])
            color_mat.append(colors[ii])
        ax1 = fig1.add_subplot(gs[2:4, 1:3])
        c.colors = color_mat
        img = c.transform(b_pos_list, mask=rsq, background=self.reference, mixing=0.5)
        self.image(img)
        subplot_count += 1

        ax1 = fig1.add_subplot(gs[2, 0])
        self.create_colorbar(ax1, regressors, colors)
Esempio n. 4
0
def make_ica_maps(data, imgs, img_size_x, img_size_y, num_ica_colors,
                  color_map, colors_ica):

    reference = data.seriesMean().pack()
    maps = Colorize(cmap=color_map,
                    colors=colors_ica[0:np.size(imgs, 0)],
                    scale=num_ica_colors).transform(abs(imgs),
                                                    background=reference,
                                                    mixing=1.5)

    #Count number of unique colors in the images
    #Get number of planes based on map dimesnions
    if len(maps.shape) == 3:
        num_planes = 1
    else:
        num_planes = np.size(maps, 2)

    unique_clrs = []
    for ii in xrange(0, np.size(colors_ica[0:np.size(imgs, 0)])):
        unique_clrs.append(
            np.round(
                np.array(webcolors.name_to_rgb(colors_ica[ii]), dtype=np.float)
                / 255))

    #From maps get number of pixel matches with color for each plane
    matched_pixels = np.zeros((np.size(unique_clrs, 0), num_planes))
    array_maps = np.round(maps.astype(np.float16))
    matched_pixels = np.zeros((np.size(unique_clrs, 0), num_planes))
    if len(maps.shape) == 3:
        array_maps_plane = np.reshape(
            array_maps, (np.size(array_maps, 0) * np.size(array_maps, 1), 3))
        matched_pixels[:, 0] = [
            np.size(np.where(
                (np.array(array_maps_plane) == match).all(axis=1)))
            for match in unique_clrs
        ]
    else:
        for ii in xrange(0, num_planes):
            array_maps_plane = np.reshape(
                array_maps[:, :, ii, :],
                (np.size(array_maps, 0) * np.size(array_maps, 1), 3))
            matched_pixels[:, ii] = [
                np.size(
                    np.where(
                        (np.array(array_maps_plane) == match).all(axis=1)))
                for match in unique_clrs
            ]

    return maps, matched_pixels
    def plot_all_together_inseperateplot(
        self, fig1, gs, regressors, rsq, b, colors, brightness_scale=3, gridspecs="[0, 0]"
    ):

        b_pos = b * (b > 0)  # Get positive beta values
        # Set up colorize function
        c = Colorize(cmap="indexed", scale=brightness_scale, flag_scale=1)

        b_pos_list = []
        color_mat = []
        for ii in xrange(0, len(regressors)):
            b_pos_list.append(b_pos[ii, :, :])
            color_mat.append(colors[ii])
        ax1 = eval("fig1.add_subplot(gs" + gridspecs + ")")
        c.colors = color_mat
        img = c.transform(b_pos_list, mask=rsq, background=self.reference, mixing=0.5)
        self.image(img)
Esempio n. 6
0
    def masks(self, dims=None, binary=True, outline=False, base=None):
        """
        Composite masks combined across sources as an iamge.

        Parameters
        ----------
        dims : list or tuple, optional, default = None
            Dimensions of image in which to create masks, must either provide
            these or provide a base image

        binary : boolean, optional, deafult = True
            Whether to incoporate values or only show a binary mask

        outline : boolean, optional, deafult = False
            Whether to only show outlines (derived using binary dilation)

        base : array-like, optional, deafult = None
            Base background image on which to put masks.
        """
        from thunder import Colorize

        if dims is None and base is None:
            raise Exception(
                "Must provide image dimensions for composite masks "
                "or provide a base image.")

        if base is not None and isinstance(base, SourceModel):
            outline = True

        if dims is None and base is not None:
            dims = asarray(base).shape

        combined = zeros(dims)
        for s in self.sources:
            combined = maximum(s.mask(dims, binary, outline), combined)

        if base is not None:
            if isinstance(base, SourceModel):
                base = base.masks(dims)
                baseColor = 'silver'
            else:
                baseColor = 'white'
            clr = Colorize(cmap='indexed', colors=[baseColor, 'deeppink'])
            combined = clr.transform([base, combined])

        return combined
Esempio n. 7
0
    def masks(self, dims=None, binary=True, outline=False, base=None):
        """
        Composite masks combined across sources as an iamge.

        Parameters
        ----------
        dims : list or tuple, optional, default = None
            Dimensions of image in which to create masks, must either provide
            these or provide a base image

        binary : boolean, optional, deafult = True
            Whether to incoporate values or only show a binary mask

        outline : boolean, optional, deafult = False
            Whether to only show outlines (derived using binary dilation)

        base : array-like, optional, deafult = None
            Base background image on which to put masks.
        """
        from thunder import Colorize

        if dims is None and base is None:
            raise Exception("Must provide image dimensions for composite masks "
                            "or provide a base image.")

        if base is not None and isinstance(base, SourceModel):
            outline = True

        if dims is None and base is not None:
            dims = asarray(base).shape

        combined = zeros(dims)
        for s in self.sources:
            combined = maximum(s.mask(dims, binary, outline), combined)

        if base is not None:
            if isinstance(base, SourceModel):
                base = base.masks(dims)
                baseColor = 'silver'
            else:
                baseColor = 'white'
            clr = Colorize(cmap='indexed', colors=[baseColor, 'deeppink'])
            combined = clr.transform([base, combined])

        return combined
Esempio n. 8
0
 def createbrainmap_withcmap(self, fig1, gs, colormap, img_labels, img_sim,
                             mixing_parameter, gridspecs):
     brainmap = Colorize(cmap=colormap).transform(img_labels,
                                                  mask=img_sim,
                                                  background=self.reference,
                                                  mixing=mixing_parameter)
     ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')
     ax1.imshow(brainmap)
     ax1.axis('off')
Esempio n. 9
0
    def make_NMF_maps(self, imgs, mixing_parameter, ignore_clusters=0):

        new_NMF_colors = copy(self.NMF_colors)
        if ignore_clusters != 0:
            new_NMF_colors[:, ignore_clusters] = 0, 0, 0

        maps = Colorize(cmap=self.color_map,
                        colors=new_NMF_colors,
                        scale=self.num_NMF_colors).transform(
                            imgs,
                            background=self.reference,
                            mixing=mixing_parameter)

        return maps
Esempio n. 10
0
    def mask(self, dims=None, binary=True, outline=False, color=None):
        """
        Construct a mask from a source, either locally or within a larger image.

        Parameters
        ----------
        dims : list or tuple, optional, default = None
            Dimensions of large image in which to draw mask. If none, will restrict
            to the bounding box of the region.

        binary : boolean, optional, deafult = True
            Whether to incoporate values or only show a binary mask

        outline : boolean, optional, deafult = False
            Whether to only show outlines (derived using binary dilation)

        color : str or array-like
            RGB triplet (from 0 to 1) or named color (e.g. 'red', 'blue')
        """
        from thunder import Colorize

        coords = self.coordinates

        if dims is None:
            extent = self.bbox[len(self.center):] - self.bbox[0:len(self.center
                                                                    )] + 1
            m = zeros(extent)
            coords = (coords - self.bbox[0:len(self.center)])
        else:
            m = zeros(dims)

        if hasattr(self,
                   'values') and self.values is not None and binary is False:
            m[coords.T.tolist()] = self.values
        else:
            m[coords.T.tolist()] = 1

        if outline:
            from skimage.morphology import binary_dilation
            m = binary_dilation(m, ones((3, 3))) - m

        if color is not None:
            m = Colorize(cmap='indexed', colors=[color]).transform([m])

        return m
Esempio n. 11
0
    def createbrainmap_withcmap(self, fig1, gs, mixing_parameter, gridspecs,
                                **kwargs):
        if 'cmap' in kwargs:
            colormap = ListedColormap(list(kwargs['cmap']), name='braincmap')
        else:
            colormap = ListedColormap(list(self.centered_cmap),
                                      name='braincmap')

        brainmap = Colorize(cmap=colormap).transform(
            self.img_labels,
            mask=self.img_sim,
            background=self.reference_image,
            mixing=mixing_parameter)
        ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')
        ax1.imshow(brainmap)
        ax1.axis('off')
        ax1.get_xaxis().set_visible(False)
        ax1.get_yaxis().set_visible(False)

        return brainmap
Esempio n. 12
0
    def createbrainmap_withcmap(self, fig1, gs, colormap, img_labels, img_sim, mixing_parameter, gridspecs):
        """
        Create the spatial brainmap again using specified colormap

        Parameters
        ----------
        fig1 : Figure handle
        gs : Gridspecs handle
        colormap : Colormap to make spatial maps
        img_sim : obtained using the Similarity method of thunder's KMeansModel.
        img_labels : Predicted label for each pixel, acquired as an image of labels
        mixing_parameter : mixing controls relative scale of the background.
        gridspecs : Grid to plot in

        """
        brainmap = Colorize(cmap=colormap).transform(img_labels, mask=img_sim, background=self.reference,
                                                     mixing=mixing_parameter)
        ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')
        ax1.imshow(brainmap)
        ax1.axis('off')
Esempio n. 13
0
    def createbrainmap_withcmap_forcombining(self,
                                             fig1,
                                             gs,
                                             gridspecs='[0, :]',
                                             **kwargs):
        if 'cmap' in kwargs:
            colormap = ListedColormap(list(kwargs['cmap']), name='braincmap')
        else:
            colormap = ListedColormap(list(self.centered_cmap),
                                      name='braincmap')

        brainmap = Colorize(cmap=colormap).transform(self.img_labels,
                                                     mask=self.img_sim)

        ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')
        ax1.imshow(brainmap)
        ax1.axis('off')
        ax1.get_xaxis().set_visible(False)
        ax1.get_yaxis().set_visible(False)

        return brainmap
Esempio n. 14
0
 def colorize(self, data, cmap="rainbow", scale=1, vmin=0, vmax=30):
     if data is None or len(data) == 0:
         return None
     print "In colorize, data.shape: %s" % str(data.shape)
     return Colorize(cmap=cmap, scale=scale, vmin=vmin, vmax=vmax).transform(data)
Esempio n. 15
0
    def masks(self,
              dims=None,
              binary=True,
              outline=False,
              base=None,
              color=None,
              values=None,
              inds=None):
        """
        Composite masks combined across sources as an image.

        Parameters
        ----------
        dims : list or tuple, optional, default = None
            Dimensions of image in which to create masks, must either provide
            these or provide a base image

        binary : boolean, optional, deafult = True
            Whether to incoporate values or only show a binary mask

        outline : boolean, optional, deafult = False
            Whether to only show outlines (derived using binary dilation)

        base : SourceModel or array-like, optional, deafult = None
            Base background image on which to put masks,
            or another set of sources (usually for comparisons).

        color : str or LinearSegmentedColormap, optional, deafult = None
            Color to assign regions or colormap, will used named colormap
            or use the provided colormap, or assign randomly if 'random'
            
        values : array-like
            List of values to use with colormap

        inds : array-like, optional, deafult = None
            List of indices if only showing a subset
        """
        from thunder import Colorize
        from matplotlib.cm import get_cmap
        from matplotlib.colors import LinearSegmentedColormap

        if inds is None:
            inds = range(0, self.count)

        if dims is None and base is None:
            raise Exception(
                "Must provide image dimensions for composite masks "
                "or provide a base image.")

        if base is not None and isinstance(base, SourceModel):
            outline = True

        if dims is None and base is not None:
            dims = asarray(base).shape

        if isinstance(base, SourceModel):
            base = base.masks(dims, color='silver')

        elif isinstance(base, ndarray):
            base = Colorize(cmap='indexed', colors=['white']).transform([base])

        if base is not None and color is None:
            color = 'deeppink'

        if isinstance(color, LinearSegmentedColormap) and values is not None:
            combined = zeros(list(dims) + [3])
            colors = color(values)[:, 0:3]
            for i in inds:
                combined = maximum(
                    self.sources[i].mask(dims, binary, outline, colors[i]),
                    combined)

        if isinstance(color,
                      str) and values is not None and not color == 'random':
            combined = zeros(list(dims) + [3])
            colors = get_cmap(color, self.count)(values)[:, 0:3]
            for i in inds:
                combined = maximum(
                    self.sources[i].mask(dims, binary, outline, colors[i]),
                    combined)

        if color == 'random':
            combined = zeros(list(dims) + [3])
            ncolors = min(self.count, 20)
            colors = get_cmap('rainbow', ncolors)(range(0, ncolors, 1))[:, 0:3]
            for i in inds:
                combined = maximum(
                    self.sources[i].mask(dims, binary, outline,
                                         colors[i % len(colors)]), combined)
        elif values is None:
            combined = zeros(dims)
            for i in inds:
                combined = maximum(self.sources[i].mask(dims, binary, outline),
                                   combined)

        if isinstance(color, str) and color != 'random' and values is None:
            combined = Colorize(cmap='indexed',
                                colors=[color]).transform([combined])

        if base is not None:
            combined = maximum(base, combined)

        return combined
def make_pca_maps(data, pca, imgs, required_pcs, img_size_x, img_size_y,
                  num_pca_colors, num_samples, thresh_pca, color_map):
    reference = data.seriesMean().pack()
    if required_pcs == 0:
        maps = Colorize(cmap=color_map,
                        scale=num_pca_colors).transform(imgs,
                                                        background=reference,
                                                        mixing=0.3)
    else:
        maps = Colorize(cmap=color_map,
                        scale=num_pca_colors).transform(imgs,
                                                        background=reference,
                                                        mixing=0.3)

    pts = pca.scores.subset(num_samples, thresh=thresh_pca, stat='norm')

    if required_pcs == 0:
        pca_pts = list()
        for ii in xrange(1, size(pca.comps.T, 1)):
            pca_pts.append(pts[:, ii][:, newaxis])
        clrs = Colorize(cmap=color_map,
                        scale=num_pca_colors).transform(pca_pts).squeeze()
    else:
        pca_pts = list()
        for ii in xrange(0, size(required_pcs)):
            pca_pts.append(pts[:, required_pcs[ii]][:, newaxis])
        clrs = Colorize(cmap=color_map,
                        scale=num_pca_colors).transform(pca_pts).squeeze()

    #Reconstruct the scores using the pca components
    if required_pcs == 0:
        recon = asarray(
            map(
                lambda x: (x[1] * pca.comps[1, :] + x[2] * pca.comps[2, :] + x[
                    3] * pca.comps[3, :]).tolist(), pts))
    else:
        pts_list = pts.tolist()
        recon = zeros((size(pts_list, 0), size(pca.comps, 1)))
        for ii in range(0, size(pts_list, 0)):
            for jj in range(0, size(required_pcs)):
                recon[ii, :] += pts_list[ii][required_pcs[jj]] * pca.comps[
                    required_pcs[jj], :]

    #Count number of unique colors in the images
    #Get number of planes based on map dimesnions
    if len(maps.shape) == 3:
        num_planes = 1
    else:
        num_planes = size(maps, 2)
    num_time = size(pca.comps.T, 0)

    #Get specific color matches across animals and get mean and standard deviation
    array1 = [map(int, single_dim)
              for single_dim in clrs]  #Convert the colors to RGB integers
    new_array = [tuple(row) for row in array1]
    unique_clrs = list(set(new_array))  #Get unique combination of colors
    unique_clrs.remove((0, 0, 0))
    matches = [
        where((array(array1) == match).all(axis=1)) for match in unique_clrs
    ]  #Match the colors with the original rows

    matches_black = [
        where((array(array1) == match).all(axis=1)) for match in [0]
    ]
    pts_nonblack = delete(pts, matches_black, axis=0)
    clrs_nonblack = delete(clrs, matches_black, axis=0)

    #From maps get number of pixel matches with color for each plane
    matched_pixels = zeros((size(unique_clrs, 0), num_planes))
    #    while sum(matched_pixels) == 0 :
    array_maps = round(maps.astype(float16))
    matched_pixels = zeros((size(unique_clrs, 0), num_planes))
    if len(maps.shape) == 3:
        array_maps_plane = reshape(
            array_maps, (size(array_maps, 0) * size(array_maps, 1), 3))
        matched_pixels[:, 0] = [
            size(where((array(array_maps_plane) == match).all(axis=1)))
            for match in unique_clrs
        ]
    else:
        for ii in xrange(0, num_planes):
            array_maps_plane = reshape(
                array_maps[:, :, ii, :],
                (size(array_maps, 0) * size(array_maps, 1), 3))
            matched_pixels[:, ii] = [
                size(where((array(array_maps_plane) == match).all(axis=1)))
                for match in unique_clrs
            ]

    #Find stats based on the color - but only use the subset of pixels in recon
    matched_signals = [
        structtype() for i in range(size(matches, 0) * num_planes)
    ]

    mean_signal = zeros((size(matches, 0), num_planes, num_time))
    sem_signal = zeros((size(matches, 0), num_planes, num_time))
    for ii in xrange(0, size(matches, 0)):
        temp_ele = array(matches[ii])
        matched_signals[ii].clr_grped_signal = [
            array(recon[ele]) for ele in temp_ele[0, :]
        ]  #Get signals from the reconstruction that match the colors
        mean_signal[ii, :] = mean(matched_signals[ii].clr_grped_signal, axis=0)
        sem_signal[ii, :] = stats.sem(matched_signals[ii].clr_grped_signal,
                                      axis=0)

    return maps, pts, pts_nonblack, clrs, clrs_nonblack, recon, unique_clrs, matched_pixels, matched_signals, mean_signal, sem_signal
    def plot_regressors_as_RGB(self,
                               fig1,
                               gs,
                               regressors,
                               rsq,
                               b,
                               colors,
                               brightness_scale=3):

        b_pos = b * (b > 0)  # Get positive beta values
        # Set up colorize function
        c = Colorize(cmap='indexed', scale=brightness_scale, flag_scale=1)

        for ii in xrange(0, len(regressors)):
            ax1 = fig1.add_subplot(gs[0, ii])
            c.colors = [colors[ii]]
            img = c.transform([b_pos[ii, :, :]],
                              mask=rsq,
                              background=self.reference,
                              mixing=0.5)
            self.image(img)

        # Plot the unique stimuli in subplots
        subplot_count = 0
        for ii in self.unique_stimuli:
            b_pos_list = []
            color_mat = []
            for keys in regressors.iterkeys():
                if ii in keys:
                    index = regressors.keys().index(keys)
                    b_pos_list.append(b_pos[index, :, :])
                    color_mat.append(colors[index])
            ax1 = fig1.add_subplot(gs[1, subplot_count])
            c.colors = color_mat
            img = c.transform(b_pos_list,
                              mask=rsq,
                              background=self.reference,
                              mixing=0.5)
            self.image(img)
            subplot_count += 1

        # Plot the different stimuli parameters in subplots
        for ii in self.parameters:
            b_pos_list = []
            color_mat = []
            for keys in regressors.iterkeys():
                if ii in keys:
                    index = regressors.keys().index(keys)
                    b_pos_list.append(b_pos[index, :, :])
                    color_mat.append(colors[index])
            ax1 = fig1.add_subplot(gs[1, subplot_count])
            c.colors = color_mat
            img = c.transform(b_pos_list,
                              mask=rsq,
                              background=self.reference,
                              mixing=0.5)
            self.image(img)
            subplot_count += 1

        # Plot all together
        b_pos_list = []
        color_mat = []
        for ii in xrange(0, len(regressors)):
            b_pos_list.append(b_pos[ii, :, :])
            color_mat.append(colors[ii])
        ax1 = fig1.add_subplot(gs[2:4, 1:3])
        c.colors = color_mat
        img = c.transform(b_pos_list,
                          mask=rsq,
                          background=self.reference,
                          mixing=0.5)
        self.image(img)
        subplot_count += 1

        ax1 = fig1.add_subplot(gs[2, 0])
        self.create_colorbar(ax1, regressors, colors)
    def make_pca_maps(self, pca, imgs, required_pcs, mixing_parameter):
        """
        Make maps and scatter plots of the pca scores with colormaps for plotting
        Parameters
        ----------
        pca: PCA model
        imgs : scores
        required_pcs: List contianing indices of required PCA components. Specify 0 if you need all
        mixing_parameter : mixing controls relative scale of the background.

        Returns
        -------
        maps: Images of PCA scores plotted using a colormap
        pts_nonblack: Pixels that are not black (not part of the background)
        clrs_nonblack: Colors that are not black
        recon: PCA reconstructed using user specified components
        unique_clrs: Colors that are unique from the images of the PCA scores
        matched_pixels: Pixels that correspond to the colors obtained in unique_clrs
        matched_signals: Individual time traces from the matched_pixels
        mean_signal: Mean of matched_signals
        sem_signal: Sem of matched_signals

        """

        maps = Colorize(cmap=self.color_map,
                        scale=self.num_pca_colors).transform(
                            imgs,
                            background=self.reference,
                            mixing=mixing_parameter)
        pts = pca.scores.subset(self.num_samples,
                                thresh=self.thresh_pca,
                                stat='norm')

        if required_pcs == 0:
            pca_pts = list()
            for ii in xrange(0, size(pca.comps.T, 1)):
                pca_pts.append(pts[:, ii][:, newaxis])
            clrs = Colorize(
                cmap=self.color_map,
                scale=self.num_pca_colors).transform(pca_pts).squeeze()
        else:
            pca_pts = list()
            for ii in xrange(0, size(required_pcs)):
                pca_pts.append(pts[:, required_pcs[ii]][:, newaxis])
            clrs = Colorize(
                cmap=self.color_map,
                scale=self.num_pca_colors).transform(pca_pts).squeeze()

        # Reconstruct the scores using the pca components
        if required_pcs == 0:
            recon = asarray(
                map(
                    lambda x: (x[0] * pca.comps[0, :] + x[1] * pca.comps[1, :]
                               + x[2] * pca.comps[2, :]).tolist(), pts))
        else:
            pts_list = pts.tolist()
            recon = zeros((size(pts_list, 0), size(pca.comps, 1)))
            for ii in range(0, size(pts_list, 0)):
                for jj in range(0, size(required_pcs)):
                    recon[ii, :] += pts_list[ii][required_pcs[jj]] * pca.comps[
                        required_pcs[jj], :]

        # Count number of unique colors in the images
        # Get number of planes based on map dimensions
        if len(maps.shape) == 3:
            num_planes = 1
        else:
            num_planes = size(maps, 2)
        num_time = size(pca.comps.T, 0)

        # Get specific color matches across animals and get mean and standard deviation
        array1 = [map(int, single_dim)
                  for single_dim in clrs]  # Convert the colors to RGB integers
        new_array = [tuple(row) for row in array1]
        unique_clrs = list(set(new_array))  # Get unique combination of colors

        if (0, 0, 0) in unique_clrs:
            unique_clrs.remove((0, 0, 0))
        matches = [
            where((array(array1) == match).all(axis=1))
            for match in unique_clrs
        ]  # Match the colors with the original rows

        matches_black = [
            where((array(array1) == match).all(axis=1)) for match in [0]
        ]
        pts_nonblack = delete(pts, matches_black, axis=0)
        clrs_nonblack = delete(clrs, matches_black, axis=0)

        # From maps get number of pixel matches with color for each plane
        array_maps = round(maps.astype(float16))
        matched_pixels = zeros((size(unique_clrs, 0), num_planes))
        array_maps_plane = reshape(
            array_maps, (size(array_maps, 0) * size(array_maps, 1), 3))
        matched_pixels[:, 0] = [
            size(where((array(array_maps_plane) == match).all(axis=1)))
            for match in unique_clrs
        ]

        # Find stats based on the color - but only use the subset of pixels in recon
        matched_signals = [
            structtype() for i in range(size(matches, 0) * num_planes)
        ]

        mean_signal = zeros((size(matches, 0), num_planes, num_time))
        sem_signal = zeros((size(matches, 0), num_planes, num_time))
        for ii in xrange(0, size(matches, 0)):
            temp_ele = array(matches[ii])
            matched_signals[ii].clr_grped_signal = [
                array(recon[ele]) for ele in temp_ele[0, :]
            ]
            mean_signal[ii, :] = mean(matched_signals[ii].clr_grped_signal,
                                      axis=0)
            sem_signal[ii, :] = stats.sem(matched_signals[ii].clr_grped_signal,
                                          axis=0)

        return maps, pts_nonblack, clrs_nonblack, recon, unique_clrs, matched_pixels, \
               matched_signals, mean_signal, sem_signal
def make_pca_maps(data,pca, imgs, required_pcs, img_size_x, img_size_y,  num_pca_colors, num_samples, thresh_pca, color_map):
    reference = data.seriesMean().pack()
    maps = Colorize(cmap=color_map, scale=num_pca_colors).transform(imgs,background=reference, mixing=2)
    pts = pca.scores.subset(num_samples, thresh=thresh_pca, stat='norm')
    if required_pcs == 0:
        clrs = Colorize(cmap=color_map, scale=num_pca_colors).transform([pts[:,0][:,newaxis], pts[:,1][:,newaxis]]).squeeze()
    else:
        clrs = Colorize(cmap=color_map, scale=num_pca_colors).transform([pts[:,required_pcs[0]][:,newaxis], pts[:,required_pcs[1]][:,newaxis]]).squeeze()

    #Reconstruct the scores using the pca components
    if required_pcs == 0:
        recon = asarray(map(lambda x: (x[1] * pca.comps[1, :] + x[2] * pca.comps[2, :] + x[3] * pca.comps[3, :]).tolist(), pts))
    else:
        pts_list = pts.tolist()
        recon = zeros((size(pts_list,0),size(pca.comps,1)))
        for ii in range(0,size(pts_list,0)):
            for jj in range(0, size(required_pcs)):
                recon[ii,:] += pts_list[ii][required_pcs[jj]]*pca.comps[required_pcs[jj],:] 
            
    
    #Count number of unique colors in the images
    #Get number of planes based on map dimesnions
    if len(maps.shape)==3:
        num_planes = 1
    else:
        num_planes = size(maps,2)
    num_time = size(pca.comps.T,0)
    
    #Get specific color matches across animals and get mean and standard deviation       
    array1 = [map(int,single_dim) for single_dim in clrs] #Convert the colors to RGB integers
    new_array = [tuple(row) for row in array1] 
    unique_clrs = list(set(new_array))    #Get unique combination of colors   
    unique_clrs.remove((0,0,0))
    matches = [where((array(array1) == match).all(axis=1)) for match in unique_clrs] #Match the colors with the original rows
    
    matches_black = [where((array(array1) == match).all(axis=1)) for match in [0]]  
    pts_nonblack = delete(pts, matches_black, axis=0)
    clrs_nonblack = delete(clrs, matches_black, axis=0)
    
    #From maps get number of pixel matches with color for each plane
    matched_pixels = zeros((size(unique_clrs,0),num_planes))
#    while sum(matched_pixels) == 0 :
    array_maps = round(maps.astype(float16))
    matched_pixels = zeros((size(unique_clrs,0),num_planes))
    if len(maps.shape) == 3:
        array_maps_plane = reshape(array_maps, (size(array_maps,0)*size(array_maps,1),3))
        matched_pixels[:,0] = [size(where((array(array_maps_plane) == match).all(axis=1))) for match in unique_clrs]
    else:     
        for ii in xrange(0,num_planes):
            array_maps_plane = reshape(array_maps[:,:,ii,:], (size(array_maps,0)*size(array_maps,1),3))
            matched_pixels[:,ii] = [size(where((array(array_maps_plane) == match).all(axis=1))) for match in unique_clrs]
             
    
    #Find stats based on the color - but only use the subset of pixels in recon
    matched_signals = [structtype() for i in range(size(matches,0)*num_planes)]
    
    mean_signal = zeros((size(matches,0), num_planes, num_time))
    sem_signal = zeros((size(matches,0), num_planes, num_time))
    for ii in xrange(0,size(matches,0)):
        temp_ele = array(matches[ii])
        matched_signals[ii].clr_grped_signal = [array(recon[ele]) for ele in temp_ele[0,:]] #Get signals from the reconstruction that match the colors                     
        mean_signal[ii,:] = mean(matched_signals[ii].clr_grped_signal,axis=0) 
        sem_signal[ii,:] = stats.sem(matched_signals[ii].clr_grped_signal,axis=0) 

    
    return maps, pts, pts_nonblack, clrs, clrs_nonblack, recon, unique_clrs, matched_pixels, matched_signals, mean_signal, sem_signal
Esempio n. 20
0
    def make_kmeans_maps(self,
                         kmeans_clusters,
                         img_labels,
                         img_sim,
                         mixing_parameter=0.8,
                         std_threshold=0.5,
                         ignore_clusters=0,
                         model_center=0):

        # Only plot those clusters where the standard deviation is greater than 0.1 - thus getting rid of noisy clusters

        interesting_clusters = array(
            where(
                logical_and(
                    std(kmeans_clusters, 0) > std_threshold,
                    max(kmeans_clusters, 0) > 0.01)))

        print 'Standard deviation of clusters is..', std(kmeans_clusters, 0)
        print 'Interesting clusters after STD are..', interesting_clusters

        if ignore_clusters != 0:
            for ii in ignore_clusters:
                index = where(squeeze(interesting_clusters) == ii)[0]
                interesting_clusters = delete(interesting_clusters, index)
            print 'Interesting clusters after user specified clusters..', interesting_clusters

        # Update kmeans clusters with those with higher standard deviation
        kmeans_clusters_updated = zeros((shape(kmeans_clusters)))
        kmeans_clusters_updated[:,
                                interesting_clusters] = kmeans_clusters[:,
                                                                        interesting_clusters]

        # Brewer colors
        newclrs_brewer = ListedColormap(sns.color_palette("Paired",
                                                          n_colors=size(
                                                              kmeans_clusters,
                                                              1)),
                                        name='from_list')
        # newclrs_brewer = (eval('palettable.colorbrewer.qualitative.' + string_cmap + '.mpl_colors'))
        # newclrs_brewer = ListedColormap(newclrs_brewer, name='from_list')
        newclrs_updated_brewer = self.update_colors(newclrs_brewer,
                                                    ignore_clusters,
                                                    interesting_clusters)

        # RGB colors

        newclrs_rgb = ListedColormap(sns.color_palette(
            "husl", n_colors=size(kmeans_clusters, 1) + 1)[:-1],
                                     name='from_list')
        newclrs_updated_rgb = self.update_colors(newclrs_rgb, ignore_clusters,
                                                 interesting_clusters)
        newclrs_updated_rgb.colors = round(newclrs_updated_rgb.colors)
        # Create maps
        brainmap = Colorize(cmap=newclrs_updated_brewer).transform(
            img_labels,
            mask=img_sim,
            background=self.reference,
            mixing=mixing_parameter)
        brainmap_for_finding_pixels = Colorize(
            cmap=newclrs_updated_rgb).transform(img_labels, mask=img_sim)

        # Count number of unique colors in the images
        # Get number of planes based on map dimensions
        if len(brainmap.shape) == 3:
            num_planes = 1
        else:
            num_planes = size(brainmap, 2)

        # Get specific color matches across animals and get mean and standard deviation
        round_clrs = round(newclrs_updated_rgb.colors)
        new_array = [tuple(row) for row in round_clrs]
        unique_clrs = (list(set(new_array))
                       )  # Get unique combination of colors
        ## remove black color if it exists
        elem = (0, 0, 0)
        unique_clrs = [
            value for key, value in enumerate(unique_clrs) if elem != value
        ]
        unique_clrs = round(unique_clrs)

        # From maps get number of pixel matches with color for each plane
        array_maps = brainmap_for_finding_pixels
        matched_pixels = zeros((size(unique_clrs, 0), num_planes))

        array_maps_plane = reshape(
            array_maps, (size(array_maps, 0) * size(array_maps, 1), 3))
        matched_pixels[:, 0] = [
            size(where((array(round(array_maps_plane)) == match).all(axis=1)))
            for match in unique_clrs
        ]

        # print matched_pixels, unique_clrs

        # Get brewer colors for plotting matched pixels
        elem = [0, 0, 0]
        a = unique_clrs.tolist()
        b = newclrs_updated_rgb.colors.tolist()
        c = newclrs_updated_brewer.colors.tolist()

        d1 = [value for key, value in enumerate(b) if elem != value]
        d2 = [value for key, value in enumerate(c) if elem != value]

        unique_clrs_brewer = zeros(shape(unique_clrs))
        for ii in xrange(0, size(unique_clrs, 0)):
            unique_clrs_brewer[ii, :] = d2[a.index(d1[ii])]

        return brainmap, unique_clrs_brewer, newclrs_updated_rgb, newclrs_updated_brewer, matched_pixels, \
               kmeans_clusters_updated
Esempio n. 21
0
    def make_kmeans_maps(self, kmeans_clusters, img_labels, img_sim, mixing_parameter=0.8, std_threshold=0.5,
                         ignore_clusters=0, model_center=0):

        """
        Make color maps using the clusters and labels and calculate number of pixels corresponding to each cluster
        Parameters
        ----------
        kmeans_clusters : Clusters obtained from kmeans
        img_sim : obtained using the Similarity method of thunder's KMeansModel.
        img_labels : Predicted label for each pixel, acquired as an image of labels
        mixing_parameter : mixing controls relative scale of the background.
        std_threshold : If the standard deviation of the cluster trace is below the threshold, it is not used to make maps
        ignore_clusters : List of clusters to ignore. Ignored clusters are colored black in the map
        model_center : Whether to color clusters and maps by similarity of cluster centers

        Returns
        -------
        brainmap : Color map of kmean clusters, this is produced using brewer colors
        unique_clrs_brewer: Using brewer colors
        newclrs_updated_rgb: Using rgb colors, updated to exclude ignore clusters
        newclrs_updated_brewer: Using brewer colors, updated to exclude ignore clusters
        matched_pixels: Pixels that correspond to the colors obtained in unique_clrs
        kmeans_clusters_updated: Updted clusters, updated to exclude the ignore clusters

        """
        interesting_clusters = array(where(logical_and(std(kmeans_clusters, 0) > std_threshold,
                                                       max(kmeans_clusters, 0) > 0.01)))

        print 'Standard deviation of clusters is..', std(kmeans_clusters, 0)
        print 'Interesting clusters after STD are..', interesting_clusters

        if ignore_clusters != 0:
            for ii in ignore_clusters:
                index = where(squeeze(interesting_clusters) == ii)[0]
                interesting_clusters = delete(interesting_clusters, index)
            print 'Interesting clusters after user specified clusters..', interesting_clusters

        # Update kmeans clusters with those with higher standard deviation
        kmeans_clusters_updated = zeros((shape(kmeans_clusters)))
        kmeans_clusters_updated[:, interesting_clusters] = kmeans_clusters[:, interesting_clusters]

        # Brewer colors
        string_cmap = 'Paired_' + str(size(kmeans_clusters, 1))
        newclrs_brewer = (eval('palettable.colorbrewer.qualitative.' + string_cmap + '.mpl_colors'))
        newclrs_brewer = ListedColormap(newclrs_brewer, name='from_list')
        newclrs_updated_brewer = self.update_colors(newclrs_brewer, ignore_clusters, interesting_clusters)

        # RGB colors
        newclrs_rgb = ListedColormap(sns.color_palette("bright", size(kmeans_clusters, 1)), name='from_list')
        newclrs_updated_rgb = self.update_colors(newclrs_rgb, ignore_clusters, interesting_clusters)
        newclrs_updated_rgb.colors = round(newclrs_updated_rgb.colors)

        # Create maps
        brainmap = Colorize(cmap=newclrs_updated_brewer).transform(img_labels, mask=img_sim, background=self.reference,
                                                                   mixing=mixing_parameter)
        brainmap_for_finding_pixels = Colorize(cmap=newclrs_updated_rgb).transform(img_labels, mask=img_sim)

        # Count number of unique colors in the images
        # Get number of planes based on map dimensions
        if len(brainmap.shape) == 3:
            num_planes = 1
        else:
            num_planes = size(brainmap, 2)

        # Get specific color matches across animals and get mean and standard deviation
        round_clrs = round(newclrs_updated_rgb.colors)
        new_array = [tuple(row) for row in round_clrs]
        unique_clrs = (list(set(new_array)))  # Get unique combination of colors
        ## remove black color if it exists
        elem = (0, 0, 0)
        unique_clrs = [value for key, value in enumerate(unique_clrs) if elem != value]
        unique_clrs = round(unique_clrs)

        # From maps get number of pixel matches with color for each plane
        array_maps = brainmap_for_finding_pixels
        matched_pixels = zeros((size(unique_clrs, 0), num_planes))

        array_maps_plane = reshape(array_maps, (size(array_maps, 0) * size(array_maps, 1), 3))
        matched_pixels[:, 0] = [size(where((array(round(array_maps_plane)) == match).all(axis=1))) for match
                                in unique_clrs]

        # Get brewer colors for plotting matched pixels
        elem = [0, 0, 0]
        a = unique_clrs.tolist()
        b = newclrs_updated_rgb.colors.tolist()
        c = newclrs_updated_brewer.colors.tolist()

        d1 = [value for key, value in enumerate(b) if elem != value]
        d2 = [value for key, value in enumerate(c) if elem != value]

        unique_clrs_brewer = zeros(shape(unique_clrs))
        for ii in xrange(0, size(unique_clrs, 0)):
            unique_clrs_brewer[ii, :] = d2[a.index(d1[ii])]

        return brainmap, unique_clrs_brewer, newclrs_updated_rgb, newclrs_updated_brewer, matched_pixels, \
               kmeans_clusters_updated
Esempio n. 22
0
    def plot_kmeans_components(self,
                               fig1,
                               gs,
                               kmeans_clusters,
                               uncentered_clrs,
                               plot_title='Hb',
                               num_subplots=1,
                               flag_separate=1,
                               gridspecs=[0, 0],
                               model_center=0,
                               removeclusters=0):

        with sns.axes_style('darkgrid'):
            if flag_separate:
                ax1 = fig1.add_subplot(2, 1, num_subplots)
            else:
                ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')

            if model_center == 1:  # Models colors according to trace
                clrs_cmap = Colorize.optimize(kmeans_clusters.T, asCmap=True)
                clrs = clrs_cmap.colors
            else:
                clrs_cmap = uncentered_clrs
                clrs = clrs_cmap.colors

            # Update kmeans if clusters were removed
            kmeans_clusters_new = copy(kmeans_clusters)
            if removeclusters != 0:
                newclrs_updated = copy(clrs_cmap)
                for index, value in enumerate(clrs_cmap.colors):
                    if index in removeclusters:
                        newclrs_updated.colors[index] = [0, 0, 0]
                clrs_cmap = newclrs_updated
                clrs = clrs_cmap.colors

                for ii in removeclusters:
                    print ii
                    kmeans_clusters_new[:,
                                        ii] = zeros(size(kmeans_clusters, 0))

            plt.gca().set_color_cycle(clrs)
            # remove those clusters that are ignored before plotting
            for ii in xrange(0, size(kmeans_clusters_new, 1)):
                plt.plot(kmeans_clusters_new[:, ii], lw=4, label=str(ii))

            plt.locator_params(axis='y', nbins=4)
            ax1.set(xlabel="Time (seconds)", ylabel="a.u")
            ax1.legend(prop={'size': 14},
                       loc='center left',
                       bbox_to_anchor=(1, 0.5),
                       ncol=1,
                       fancybox=True,
                       shadow=True)

            plt.title(plot_title, fontsize=14)

            plt.ylim((min(kmeans_clusters_new) - 0.0001,
                      max(kmeans_clusters_new) + 0.0001))
            plt.xlim((0, size(kmeans_clusters_new, 0)))
            plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
            self.plot_vertical_lines_onset()
            self.plot_vertical_lines_offset()
            self.plot_stimulus_patch(ax1)

        return clrs_cmap
def make_kmeans_maps(data, kmeans_cluster_centers, img_labels, img_sim,
                     img_size_x, img_size_y, ignore_clusters):

    reference = data.seriesMean().pack()

    #Only plot those clusters where the standard deviation is greater than 0.1 - thus getting rid of noisy clusters

    interesting_clusters = np.array(np.where(np.logical_and(np.std(kmeans_cluster_centers,0)>0.0001,\
    np.max(kmeans_cluster_centers,0)>0.00001)))

    if ignore_clusters != 0:
        for ii in ignore_clusters:
            index = np.where(np.squeeze(interesting_clusters) == ii)[0]
            interesting_clusters = np.delete(interesting_clusters, index)


#    newclrs_rgb, newclrs_brewer = Colorize.optimize(kmeans_cluster_centers.T, asCmap=True)

#Brewer colors
    string_cmap = 'Set1_' + str(np.size(kmeans_cluster_centers, 1))
    newclrs_brewer = eval('palettable.colorbrewer.qualitative.' + string_cmap +
                          '.mpl_colors')
    newclrs_brewer = ListedColormap(newclrs_brewer, name='from_list')
    newclrs_updated_brewer = update_colors(newclrs_brewer,
                                           interesting_clusters)

    #RGB colors
    newclrs_rgb = ListedColormap(sns.color_palette(
        "bright", np.size(kmeans_cluster_centers, 1)),
                                 name='from_list')
    newclrs_updated_rgb = update_colors(newclrs_rgb, interesting_clusters)
    newclrs_updated_rgb.colors = np.round(newclrs_updated_rgb.colors)

    #Update kmeans clusters with those with higher standard deviation
    kmeans_cluster_centers_updated = np.zeros(
        (np.shape(kmeans_cluster_centers)))
    kmeans_cluster_centers_updated[:,
                                   interesting_clusters] = kmeans_cluster_centers[:,
                                                                                  interesting_clusters]

    #Create maps
    brainmap = Colorize(cmap=newclrs_updated_brewer).transform(
        img_labels, mask=img_sim, background=reference, mixing=1.0)
    brainmap_for_finding_pixels = Colorize(cmap=newclrs_updated_rgb).transform(
        img_labels, mask=img_sim)

    #Count number of unique colors in the images
    #Get number of planes based on map dimesnions
    if len(brainmap.shape) == 3:
        num_planes = 1
    else:
        num_planes = np.size(brainmap, 2)

    #Get specific color matches across animals and get mean and standard deviation
    round_clrs = np.round(newclrs_updated_rgb.colors)
    new_array = [tuple(row) for row in round_clrs]
    unique_clrs = (list(set(new_array)))  #Get unique combination of colors

    ## remove black color if it exists
    elem = (0, 0, 0)
    X = unique_clrs.index(elem) if elem in unique_clrs else -1
    if X != -1:
        unique_clrs.remove(elem)
        unique_clrs = np.round(unique_clrs)

    #From maps get number of pixel matches with color for each plane
    matched_pixels = np.zeros((np.size(unique_clrs, 0), num_planes))
    #    while sum(matched_pixels) == 0 :
    array_maps = brainmap_for_finding_pixels
    matched_pixels = np.zeros((np.size(unique_clrs, 0), num_planes))
    if len(brainmap.shape) == 3:
        array_maps_plane = np.reshape(
            array_maps, (np.size(array_maps, 0) * np.size(array_maps, 1), 3))
        matched_pixels[:, 0] = [
            np.size(
                np.where((np.array(
                    np.round(array_maps_plane)) == match).all(axis=1)))
            for match in unique_clrs
        ]
    else:
        for ii in xrange(0, num_planes):
            array_maps_plane = np.reshape(
                array_maps[:, :, ii, :],
                (np.size(array_maps, 0) * np.size(array_maps, 1), 3))
            matched_pixels[:, ii] = [
                np.size(
                    np.where((np.array(
                        np.round(array_maps_plane)) == match).all(axis=1)))
                for match in unique_clrs
            ]

    return brainmap, unique_clrs, newclrs_updated_rgb, newclrs_updated_brewer, matched_pixels, kmeans_cluster_centers_updated