Esempio n. 1
0
def plotHist(data,
             title,
             countTools,
             xlabel="hour",
             ylabel="count of queries",
             log=False):
    if not os.path.exists(title[:title.rfind("/")]):
        os.makedirs(title[:title.rfind("/")])
    fig = plt.figure(1)
    ax = fig.add_subplot(111)
    plt.grid(True)

    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    axes = plt.axes()
    axes.xaxis.set_major_locator(ticker.MultipleLocator(1))

    if log:
        axes.set_yscale('log')

    colormap = cm.nipy_spectral(np.linspace(0, 1, countTools))
    color = iter(colormap)

    minMetricValue = data[0][1]["Y"][0]
    maxMetricValue = 0

    for dataPoint in data:
        c = next((color))
        XY = dataPoint[1]

        if (XY["Y"][0] < minMetricValue):
            minMetricValue = XY["Y"][0]

        if (XY["X"][0] > maxMetricValue):
            maxMetricValue = XY["Y"][0]

        try:
            ax.bar(XY["X"], XY["Y"], align='center', color=c, edgecolor=c)
        except ValueError:
            pass

    scalarMappaple = cm.ScalarMappable(cmap=cm.nipy_spectral,
                                       norm=plt.Normalize(vmin=minMetricValue,
                                                          vmax=maxMetricValue))
    scalarMappaple._A = []
    plt.colorbar(scalarMappaple)

    if xlabel is 'hour':
        plt.xlim(-1, 24)

    if xlabel is 'day':
        plt.xlim(0, 32)

    plt.xticks(fontsize=9)

    plt.savefig(title + ".png", bbox_inches='tight')

    plt.close()
Esempio n. 2
0
def plot_clustering(X_red, X, labels, title=None):
    x_min, x_max = np.min(X_red, axis=0), np.max(X_red, axis=0)
    X_red = (X_red - x_min) / (x_max - x_min)

    plt.figure(figsize=(6, 4))
    for i in range(X_red.shape[0]):
        plt.text(X_red[i, 0], X_red[i, 1], str(Y[i]),
                 color=cm.nipy_spectral(labels[i] / 10.),
                 fontdict={'weight': 'bold', 'size': 9})

    plt.xticks([])
    plt.yticks([])
    if title is not None:
        plt.title(title, size=17)
    plt.axis('off')
    plt.tight_layout()
Esempio n. 3
0
def PlotAttributes(component1,
                   component2,
                   attribute='acronym',
                   proportionShown='All',
                   pointSize=20,
                   X=X_pca,
                   samples=samples,
                   colors=None):
    """ 
    Plot each sample as a point, colored by attribute, relative to two principal components (component1 and component2)
    """
    title = attribute
    # Get the attributes
    y = samples[attribute]
    # Optional: Subset the data to make it easier to see what's going on (7,000 is a bit
    # too many points for this scatter plot)
    if proportionShown is not 'All':
        X_show, X_removed, y_show, y_removed = train_test_split(
            X,
            y,
            test_size=1 - proportionShown,
            stratify=y,
            random_state=RandomSeed)
    else:
        X_show = X
        y_show = y
    # Get a list of the attributes
    attributes = np.unique(y_show)
    # Create the plot
    fig = plt.figure(figsize=(5, 5))
    labels = attributes
    if colors:
        colors = colors
    else:
        colors = iter(cm.nipy_spectral(np.linspace(0, 1, len(attributes))))
    ax = fig.add_subplot(1, 1, 1)
    markers = ['o', 's', 'D', '>']
    for attribute, label, color in zip(attributes, labels, colors):
        ax.scatter(X_show[np.where(y_show == attribute), component1],
                   X_show[np.where(y_show == attribute), component2],
                   marker=markers[list(attributes).index(attribute) %
                                  len(markers)],
                   label=attribute,
                   color=color,
                   linewidth='0.2',
                   s=pointSize,
                   alpha=0.9)
        ax.legend(loc='best')
    plt.xlabel("Principal Component %s " % str(component1 + 1))
    plt.ylabel("Principal Component %s " % str(component2 + 1))
    plt.title("{} (PC{} and PC{})".format(title, component1 + 1,
                                          component2 + 1))
    if len(attributes) > 12:
        numberColumns = 2
    else:
        numberColumns = 1
    plt.legend(bbox_to_anchor=(1.05, 1),
               loc=2,
               ncol=numberColumns,
               borderaxespad=0.)
    plt.show()
Esempio n. 4
0
T = T[T['cell_line'] != 'BR1']
T = T[T['cell_line'] != 'PC9AZR']
T = T[T['MCMC_converge'] == 1]
T = T[T['model_level'] > 4]
T = T[T['R2'] > .5]

num_classes = len(np.unique(T['cell_line']))
cell_lines = np.unique(T['cell_line'])

xmin, xmax = (T['log_alpha2'].min() - 0.5, T['log_alpha2'].max() + 0.5)
ymin, ymax = (T['beta_obs_norm'].min() - .05, T['beta_obs_norm'].max() + .1)
y_lim = (ymin, ymax)
x_lim = (xmin, xmax)

#Colors for each super class
col = cm.nipy_spectral(np.linspace(0, 1, len(cell_lines)))

fig = plt.figure(figsize=(8.5, 2.3))
ax = []
for i in range(num_classes):
    ax.append(plt.subplot2grid((1, num_classes), (0, i)))
plt.subplots_adjust(wspace=0, hspace=.4)

for i in range(num_classes):
    plt.sca(ax[i])
    x = T.loc[T['cell_line'] == cell_lines[i], 'log_alpha2'].values
    y = T.loc[T['cell_line'] == cell_lines[i], 'beta_obs_norm'].values
    plt.scatter(x, y, s=30, color=col[i], edgecolor='k', linewidth=1)
    plt.title(cell_lines[i])

for e in range(num_classes):
Esempio n. 5
0
genes = tpm_df.index.values

tpm_df = tpm_df.T

tpm_df = tpm_df[gnImpDf.GeneId]

tpm_df['barcode'] = tpm_df.index

tpm_df = tpm_df.merge(cl_df[['barcode','cancer_type']])

tpm_df = tpm_df.drop(columns=['barcode'])

DiseaseType = tpm_df.pop('cancer_type')

colors=cm.nipy_spectral(pd.np.linspace(0,1,DiseaseType.unique().size))
lut = dict(zip(DiseaseType.unique(), colors))

row_colors = DiseaseType.map(lut)
fig = sns.clustermap(tpm_df, row_colors=row_colors,
                     yticklabels=False,xticklabels=1)

fig.ax_heatmap.set_xticklabels(fig.ax_heatmap.get_xmajorticklabels(), 
                                fontsize = 2.5)

fig.ax_row_dendrogram.set_visible(False)
fig.ax_col_dendrogram.set_visible(False)

fig.cax.set_visible(False)

import matplotlib.patches as mpatches
Esempio n. 6
0
ax = plt.gca()
np1 = [101, 60, 83]
x, y = map(95.8853, 37.6199)

beach1 = beach(np1, xy=(x, y), width=700000)
ax.add_collection(beach1)

x = []
y = []
z = []
for i in result:
    x.append(i[:][2])
    y.append(i[:][1])
    z.append(int(i[:][3]))

colors = cm.nipy_spectral(np.linspace(0, 1, np.max(z) + 1))

x = np.asarray(x)
y = np.asarray(y)

x, y = map(x, y)
map.scatter(x, y, c=[colors[index] for index in z])
try:
    plt.text(x[0], y[0], 'r' + str(data[0, 0])[:], fontsize=12)
except:
    pass

radii = [1000, 5000, 10000]
for radius in radii:
    equi(map, lon_0, lat_0, radius, lw=2.)
plt.show()
Esempio n. 7
0
    def plotSpectrogramByDepth(self,
                               nchannels=384,
                               nseconds=100,
                               maxFreq=125,
                               **kwargs):
        """
        Plots a heat map spectrogram of the LFP for each channel.

        Line plots of power per frequency band and power on a subset
        of channels are also displayed to the right and above the main plot.

        Parameters
        ----------
        nchannels : int
            The number of channels on the probe
        nseconds : int, optional
            How long in seconds from the start of the trial to do the
            spectrogram for (for speed).
            Default 100
        maxFreq : int
            The maximum frequency in Hz to plot the spectrogram out to.
            Maximum 1250.
            Default 125
        kwargs: legal values are: 'frequencies' and 'channels'; both are lists
                that denote which frequencies to show the mean power of
                along the length of the probe and,
                which channels to show the frequency spectra of.

        Notes
        -----
        Should also allow kwargs to specify exactly which channels and / or
        frequency bands to do the line plots for
        """
        import os

        lfp_file = os.path.join(self.path2LFPdata, "continuous.dat")
        status = os.stat(lfp_file)
        nsamples = int(status.st_size / 2 / nchannels)
        mmap = np.memmap(lfp_file,
                         np.int16,
                         "r",
                         0, (nchannels, nsamples),
                         order="F")
        # Load the channel map
        #  Assumes this is in the AP data location and that kilosort was run
        # channel_map = np.squeeze(np.load(os.path.join(
        #     self.path2APdata, 'channel_map.npy')))
        channel_map = np.arange(nchannels)
        lfp_sample_rate = 2500
        data = np.array(mmap[channel_map, 0:nseconds * lfp_sample_rate])
        from ephysiopy.common.ephys_generic import EEGCalcsGeneric

        E = EEGCalcsGeneric(data[0, :], lfp_sample_rate)
        E.calcEEGPowerSpectrum()
        # Select a subset of the full amount of data to display later
        spec_data = np.zeros(shape=(data.shape[0], len(E.sm_power[0::50])))
        for chan in range(data.shape[0]):
            E = EEGCalcsGeneric(data[chan, :], lfp_sample_rate)
            E.calcEEGPowerSpectrum()
            spec_data[chan, :] = E.sm_power[0::50]

        x, y = np.meshgrid(E.freqs[0::50], channel_map)
        import matplotlib.colors as colors
        from matplotlib.pyplot import cm
        from mpl_toolkits.axes_grid1 import make_axes_locatable

        _, spectoAx = plt.subplots()
        if "cmap" in kwargs:
            cmap = kwargs["cmap"]
        else:
            cmap = "bone"
        spectoAx.pcolormesh(
            x,
            y,
            spec_data,
            edgecolors="face",
            cmap=cmap,
            norm=colors.LogNorm(),
            shading="nearest",
        )
        if "minFreq" in kwargs:
            minFreq = kwargs["minFreq"]
        else:
            minFreq = 0
        spectoAx.set_xlim(minFreq, maxFreq)
        spectoAx.set_ylim(channel_map[0], channel_map[-1])
        spectoAx.set_xlabel("Frequency (Hz)")
        spectoAx.set_ylabel("Channel")
        divider = make_axes_locatable(spectoAx)
        channel_spectoAx = divider.append_axes("top",
                                               1.2,
                                               pad=0.1,
                                               sharex=spectoAx)
        meanfreq_powerAx = divider.append_axes("right",
                                               1.2,
                                               pad=0.1,
                                               sharey=spectoAx)
        plt.setp(
            channel_spectoAx.get_xticklabels() +
            meanfreq_powerAx.get_yticklabels(),
            visible=False,
        )

        mn_power = np.mean(spec_data, 0)
        if "channels" in kwargs:
            channels = kwargs["channels"]
            cols = iter(
                cm.rainbow(np.linspace(0, 1, (len(kwargs["channels"])))))
        else:
            channels = np.arange(0, spec_data.shape[0], 60)
            cols = iter(cm.rainbow(np.linspace(0, 1, (nchannels // 60) + 1)))
        for i in channels:
            c = next(cols)
            channel_spectoAx.plot(
                E.freqs[0::50],
                10 * np.log10(spec_data[i, :] / mn_power),
                c=c,
                label=str(i),
            )

        channel_spectoAx.set_ylabel("Channel power(dB)")
        channel_spectoAx.legend(
            bbox_to_anchor=(0.0, 1.02, 1.0, 0.102),
            loc="lower left",
            mode="expand",
            fontsize="x-small",
            ncol=4,
        )

        if "frequencies" in kwargs:
            lower_freqs = kwargs["frequencies"]
            upper_freqs = lower_freqs[1::]
            inc = np.diff([lower_freqs[-2], lower_freqs[-1]])
            upper_freqs = np.append(upper_freqs, lower_freqs[-1] + inc)
        else:
            freq_inc = 6
            lower_freqs = np.arange(1, maxFreq - freq_inc, freq_inc)
            upper_freqs = np.arange(1 + freq_inc, maxFreq, freq_inc)

        cols = iter(cm.nipy_spectral(np.linspace(0, 1, len(upper_freqs))))
        mn_power = np.mean(spec_data, 1)
        print(f"spec_data shape = {np.shape(spec_data)}")
        print(f"mn_power shape = {np.shape(mn_power)}")
        for freqs in zip(lower_freqs, upper_freqs):
            freq_mask = np.logical_and(E.freqs[0::50] > freqs[0],
                                       E.freqs[0::50] < freqs[1])
            mean_power = 10 * np.log10(
                np.mean(spec_data[:, freq_mask], 1) / mn_power)
            c = next(cols)
            meanfreq_powerAx.plot(
                mean_power,
                channel_map,
                c=c,
                label=str(freqs[0]) + " - " + str(freqs[1]),
            )
        meanfreq_powerAx.set_xlabel("Mean freq. band power(dB)")
        meanfreq_powerAx.legend(
            bbox_to_anchor=(0.0, 1.02, 1.0, 0.102),
            loc="lower left",
            mode="expand",
            fontsize="x-small",
            ncol=1,
        )
        if "saveas" in kwargs:
            saveas = kwargs["saveas"]
            plt.savefig(saveas)
        plt.show()
Esempio n. 8
0
	def plotSpectrogramByDepth(self, nchannels=384, nseconds=100, maxFreq=125, **kwargs):
		"""
		Plots a heat map spectrogram of the LFP for each channel.

		Line plots of power per frequency band and power on a subset of channels are 
		also displayed to the right and above the main plot.

		Parameters
		----------
		nchannels : int
			The number of channels on the probe
		nseconds : int, optional
			How long in seconds from the start of the trial to do the spectrogram for (for speed).
			Default 100
		maxFreq : int
			The maximum frequency in Hz to plot the spectrogram out to. Maximum 1250.
			Default 125
		
		Notes
		-----
		Should also allow kwargs to specify exactly which channels and / or frequency
		bands to do the line plots for
		"""
		import os
		lfp_file = os.path.join(self.path2LFPdata, 'continuous.dat')
		status = os.stat(lfp_file)
		nsamples = int(status.st_size / 2 / nchannels)
		mmap = np.memmap(lfp_file, np.int16, 'r', 0, (nchannels, nsamples), order='F')
		# Load the channel map NB assumes this is in the AP data location and that kilosort was run there
		channel_map = np.squeeze(np.load(os.path.join(self.path2APdata, 'channel_map.npy')))
		lfp_sample_rate = 2500
		data = np.array(mmap[channel_map, 0:nseconds*lfp_sample_rate])
		from ephysiopy.common.ephys_generic import EEGCalcsGeneric
		E = EEGCalcsGeneric(data[0, :], lfp_sample_rate)
		E.calcEEGPowerSpectrum()
		spec_data = np.zeros(shape=(data.shape[0], len(E.sm_power[0::50])))
		for chan in range(data.shape[0]):
			E = EEGCalcsGeneric(data[chan, :], lfp_sample_rate)
			E.calcEEGPowerSpectrum()
			spec_data[chan, :] = E.sm_power[0::50]

		x, y = np.meshgrid(E.freqs[0::50], channel_map)
		import matplotlib.colors as colors
		from matplotlib.pyplot import cm
		from mpl_toolkits.axes_grid1 import make_axes_locatable
		_, spectoAx = plt.subplots()
		spectoAx.pcolormesh(x, y, spec_data, edgecolors='face', cmap='bone',norm=colors.LogNorm())
		spectoAx.set_xlim(0, maxFreq)
		spectoAx.set_ylim(channel_map[0], channel_map[-1])
		spectoAx.set_xlabel('Frequency (Hz)')
		spectoAx.set_ylabel('Channel')
		divider = make_axes_locatable(spectoAx)
		channel_spectoAx = divider.append_axes("top", 1.2, pad = 0.1, sharex=spectoAx)
		meanfreq_powerAx = divider.append_axes("right", 1.2, pad = 0.1, sharey=spectoAx)
		plt.setp(channel_spectoAx.get_xticklabels() + meanfreq_powerAx.get_yticklabels(), visible=False)

		mn_power = np.mean(spec_data, 0)
		cols = iter(cm.rainbow(np.linspace(0,1,(nchannels//60)+1)))
		for i in range(0, spec_data.shape[0], 60):
			c = next(cols)
			channel_spectoAx.plot(E.freqs[0::50], 10*np.log10(spec_data[i, :]/mn_power), c=c, label=str(i))

		channel_spectoAx.set_ylabel('Channel power(dB)')
		channel_spectoAx.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', mode='expand',
			fontsize='x-small', ncol=4)

		freq_inc = 6
		lower_freqs = np.arange(1, maxFreq-freq_inc, freq_inc)
		upper_freqs = np.arange(1+freq_inc, maxFreq, freq_inc)
		cols = iter(cm.nipy_spectral(np.linspace(0,1,len(upper_freqs))))
		mn_power = np.mean(spec_data, 1)
		for freqs in zip(lower_freqs, upper_freqs):
			freq_mask = np.logical_and(E.freqs[0::50]>freqs[0], E.freqs[0::50]<freqs[1])
			mean_power = 10*np.log10(np.mean(spec_data[:, freq_mask],1)/mn_power)
			c = next(cols)
			meanfreq_powerAx.plot(mean_power, channel_map, c=c, label=str(freqs[0]) + " - " + str(freqs[1]))
		meanfreq_powerAx.set_xlabel('Mean freq. band power(dB)')
		meanfreq_powerAx.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', mode='expand',
			fontsize='x-small', ncol=1)
		if 'saveas' in kwargs:
			saveas = kwargs['saveas']
			plt.savefig(saveas)
		plt.show()