Exemplo n.º 1
0
# Get all the units from the hdf5 file
units = hf5.list_nodes('/sorted_units')

# Delete and remake a directory for storing the plots of the unit waveforms (if it exists)
try:
    shutil.rmtree("unit_waveforms_plots", ignore_errors=True)
except:
    pass
os.mkdir("unit_waveforms_plots")

# Now plot the waveforms from the units in this directory one by one
for unit in range(len(units)):
    waveforms = units[unit].waveforms[:]
    x = np.arange(waveforms.shape[1] / 10) + 1
    fig, ax = blech_waveforms_datashader.waveforms_datashader(waveforms, x)
    ax.set_xlabel('Sample (30 samples per ms)')
    ax.set_ylabel('Voltage (microvolts)')
    ax.set_title('Unit %i, total waveforms = %i' % (unit, waveforms.shape[0]) +
                 '\n' + 'Electrode: %i, Single Unit: %i, RSU: %i, FS: %i' %
                 (hf5.root.unit_descriptor[unit]['electrode_number'],
                  hf5.root.unit_descriptor[unit]['single_unit'],
                  hf5.root.unit_descriptor[unit]['regular_spiking'],
                  hf5.root.unit_descriptor[unit]['fast_spiking']))
    fig.savefig('./unit_waveforms_plots/Unit%i.png' % (unit))
    plt.close("all")

    # Also plot the mean and SD for every unit - downsample the waveforms 10 times to remove effects of upsampling during de-jittering
    fig = plt.figure()
    plt.plot(x, np.mean(waveforms[:, ::10], axis=0), linewidth=4.0)
    plt.fill_between(x,
# Get all the units from the hdf5 file
units = hf5.list_nodes('/sorted_units')

# Delete and remake a directory for storing the plots of the unit waveforms (if it exists)
try:
	shutil.rmtree("unit_waveforms_plots", ignore_errors = True)
except:
	pass
os.mkdir("unit_waveforms_plots")

# Now plot the waveforms from the units in this directory one by one
for unit in range(len(units)):
	waveforms = units[unit].waveforms[:]
	x = np.arange(waveforms.shape[1]/10) + 1
	fig, ax = blech_waveforms_datashader.waveforms_datashader(waveforms, x)
	ax.set_xlabel('Sample (30 samples per ms)')
	ax.set_ylabel('Voltage (microvolts)')
	ax.set_title('Unit %i, total waveforms = %i' % (unit, waveforms.shape[0]) + '\n' + 'Electrode: %i, Single Unit: %i, RSU: %i, FS: %i' % (hf5.root.unit_descriptor[unit]['electrode_number'], hf5.root.unit_descriptor[unit]['single_unit'], hf5.root.unit_descriptor[unit]['regular_spiking'], hf5.root.unit_descriptor[unit]['fast_spiking']))
	fig.savefig('./unit_waveforms_plots/Unit%i.png' % (unit))
	plt.close("all")
	
	# Also plot the mean and SD for every unit - downsample the waveforms 10 times to remove effects of upsampling during de-jittering
	fig = plt.figure()
	plt.plot(x, np.mean(waveforms[:, ::10], axis = 0), linewidth = 4.0)
	plt.fill_between(x, np.mean(waveforms[:, ::10], axis = 0) - np.std(waveforms[:, ::10], axis = 0), np.mean(waveforms[:, ::10], axis = 0) + np.std(waveforms[:, ::10], axis = 0), alpha = 0.4)
	plt.xlabel('Sample (30 samples per ms)')
	plt.ylabel('Voltage (microvolts)')
	plt.title('Unit %i, total waveforms = %i' % (unit, waveforms.shape[0]) + '\n' + 'Electrode: %i, Single Unit: %i, RSU: %i, FS: %i' % (hf5.root.unit_descriptor[unit]['electrode_number'], hf5.root.unit_descriptor[unit]['single_unit'], hf5.root.unit_descriptor[unit]['regular_spiking'], hf5.root.unit_descriptor[unit]['fast_spiking']))
	fig.savefig('./unit_waveforms_plots/Unit%i_mean_sd.png' % (unit))
	plt.close("all")
		g.fit(data)
	
		# Show the cluster plots if the solution converged
		if g.converged_:
			split_predictions = g.predict(data)
			x = np.arange(len(spike_waveforms[0])/10) + 1
			for cluster in range(n_clusters):
				split_points = np.where(split_predictions == cluster)[0]				
				# plt.figure(cluster)
				slices_dejittered = spike_waveforms[this_cluster, :]		# Waveforms and times from the chosen cluster
				times_dejittered = spike_times[this_cluster]
				times_dejittered = times_dejittered[split_points]		# Waveforms and times from the chosen split of the chosen cluster
				ISIs = np.ediff1d(np.sort(times_dejittered))/30.0
				violations1 = 100.0*float(np.sum(ISIs < 1.0)/split_points.shape[0])
				violations2 = 100.0*float(np.sum(ISIs < 2.0)/split_points.shape[0])
				fig, ax = blech_waveforms_datashader.waveforms_datashader(slices_dejittered[split_points, :], x)
				# plt.plot(x-15, slices_dejittered[split_points, :].T, linewidth = 0.01, color = 'red')
				ax.set_xlabel('Sample (30 samples per ms)')
				ax.set_ylabel('Voltage (microvolts)')
				ax.set_title("Split Cluster{:d}, 2ms ISI violations={:.1f} percent".format(cluster, violations2) + "\n" + "1ms ISI violations={:.1f}%, Number of waveforms={:d}".format(violations1, split_points.shape[0]))
		else:
			print("Solution did not converge - try again with higher number of iterations or lower convergence criterion")
			continue

		plt.show()
		# Ask the user for the split clusters they want to choose
		chosen_split = easygui.multchoicebox(msg = 'Which split cluster do you want to choose? Hit cancel to exit', choices = tuple([str(i) for i in range(n_clusters)]))
		try:
			chosen_split = int(chosen_split[0])
		except:
			continue
Exemplo n.º 4
0
            split_predictions = g.predict(data)
            x = np.arange(len(spike_waveforms[0]) / 10) + 1
            for cluster in range(n_clusters):
                split_points = np.where(split_predictions == cluster)[0]
                # plt.figure(cluster)
                slices_dejittered = spike_waveforms[
                    this_cluster, :]  # Waveforms and times from the chosen cluster
                times_dejittered = spike_times[this_cluster]
                times_dejittered = times_dejittered[
                    split_points]  # Waveforms and times from the chosen split of the chosen cluster
                ISIs = np.ediff1d(np.sort(times_dejittered)) / 30.0
                violations1 = 100.0 * float(
                    np.sum(ISIs < 1.0) / split_points.shape[0])
                violations2 = 100.0 * float(
                    np.sum(ISIs < 2.0) / split_points.shape[0])
                fig, ax = blech_waveforms_datashader.waveforms_datashader(
                    slices_dejittered[split_points, :], x)
                # plt.plot(x-15, slices_dejittered[split_points, :].T, linewidth = 0.01, color = 'red')
                ax.set_xlabel('Sample (30 samples per ms)')
                ax.set_ylabel('Voltage (microvolts)')
                ax.set_title(
                    "Split Cluster{:d}, 2ms ISI violations={:.1f} percent".
                    format(cluster, violations2) + "\n" +
                    "1ms ISI violations={:.1f}%, Number of waveforms={:d}".
                    format(violations1, split_points.shape[0]))
        else:
            print(
                "Solution did not converge - try again with higher number of iterations or lower convergence criterion"
            )
            continue

        plt.show()
            # Show the cluster plots if the solution converged
            if g.converged_:
                split_predictions = g.predict(data)
                x = np.arange(len(spike_waveforms[0])) + 1
                #fig, ax = gen_square_subplots(n_clusters,sharex=True,sharey=True)
                for cluster in range(n_clusters):
                    split_points = np.where(split_predictions == cluster)[0]
                    # Waveforms and times from the chosen cluster
                    slices_dejittered = spike_waveforms[this_cluster, :]            
                    times_dejittered = spike_times[this_cluster]
                    # Waveforms and times from the chosen split of the chosen cluster
                    times_dejittered = times_dejittered[split_points]               
                    ISIs = np.ediff1d(np.sort(times_dejittered))/30.0
                    violations1 = 100.0*float(np.sum(ISIs < 1.0)/split_points.shape[0])
                    violations2 = 100.0*float(np.sum(ISIs < 2.0)/split_points.shape[0])
                    fig, ax = blech_waveforms_datashader.waveforms_datashader(\
                            slices_dejittered[split_points, :], x, downsample = False)
                    ax.set_xlabel('Sample (30 samples per ms)')
                    ax.set_ylabel('Voltage (microvolts)')
                    print_str = (f'\nSplit Cluster {cluster} \n'
                        f'{violations2:.1f} % (<2ms),'
                        f'{violations1:.1f} % (<1ms),'
                        f'{split_points.shape[0]} total waveforms. \n') 
                    ax.set_title(print_str)
            else:
                print("Solution did not converge "\
                        "- try again with higher number of iterations "\
                        "or lower convergence criterion")
                continue

            plt.show()
Exemplo n.º 6
0
                f'clusters{i+2}/predictions.npy',
        predictions)

    # Create file, and plot spike waveforms for the different clusters.
    # Plot 10 times downsampled dejittered/smoothed waveforms.
    # Additionally plot the ISI distribution of each cluster
    os.mkdir(f'./Plots/{electrode_num:02}/{i+2}_clusters_waveforms_ISIs')
    x = np.arange(len(slices_dejittered[0])) + 1
    for cluster in range(i + 2):
        cluster_points = np.where(predictions[:] == cluster)[0]

        if len(cluster_points) > 0:
            fig, ax = \
                    blech_waveforms_datashader.waveforms_datashader(\
                        slices_dejittered[cluster_points, :],
                        x,
                        downsample = False,
                        dir_name = "datashader_temp_el" + str(electrode_num))
            ax.set_xlabel('Sample ({:d} samples per ms)'.\
                    format(int(sampling_rate/1000)))
            ax.set_ylabel('Voltage (microvolts)')
            ax.set_title('Cluster%i' % cluster)
            fig.savefig(f'./Plots/{electrode_num:02}/'\
                    f'{i+2}_clusters_waveforms_ISIs/Cluster{cluster}_waveforms')
            plt.close("all")

            fig = plt.figure()
            cluster_times = times_dejittered[cluster_points]
            ISIs = np.ediff1d(np.sort(cluster_times))
            ISIs = ISIs / 30.0
            max_ISI_val = 20
Exemplo n.º 7
0
    # Additionally plot the ISI distribution of each cluster
    os.mkdir('./Plots/%i/Plots/%i_clusters_waveforms_ISIs' %
             (electrode_num, i + 2))
    x = np.arange(len(slices_dejittered[0]) / 10) + 1
    for cluster in range(i + 2):
        cluster_points = np.where(predictions[:] == cluster)[0]

        #for point in cluster_points:
        #	plot_wf = np.zeros(len(slices_dejittered[0])/10)
        #	for time in range(len(slices_dejittered[point])/10):
        #		plot_wf[time] = slices_dejittered[point, time*10]
        #	plt.plot(x-15, plot_wf, linewidth = 0.1, color = 'red')
        #	plt.hold(True)
        # plt.plot(x - int((sampling_rate/1000.0)*spike_snapshot_before), slices_dejittered[cluster_points, ::10].T, linewidth = 0.01, color = 'red')
        fig, ax = blech_waveforms_datashader.waveforms_datashader(
            slices_dejittered[cluster_points, :],
            x,
            dir_name="datashader_temp_el" + str(electrode_num))
        ax.set_xlabel('Sample ({:d} samples per ms)'.format(
            int(sampling_rate / 1000)))
        ax.set_ylabel('Voltage (microvolts)')
        ax.set_title('Cluster%i' % cluster)
        fig.savefig(
            './Plots/%i/Plots/%i_clusters_waveforms_ISIs/Cluster%i_waveforms' %
            (electrode_num, i + 2, cluster))
        plt.close("all")

        fig = plt.figure()
        cluster_times = times_dejittered[cluster_points]
        ISIs = np.ediff1d(np.sort(cluster_times))
        ISIs = ISIs / 30.0
        plt.hist(ISIs,
	
	# Create file, and plot spike waveforms for the different clusters. Plot 10 times downsampled dejittered/smoothed waveforms.
	# Additionally plot the ISI distribution of each cluster 
	os.mkdir('./Plots/%i/Plots/%i_clusters_waveforms_ISIs' % (electrode_num, i+2))
	x = np.arange(len(slices_dejittered[0])/10) + 1
	for cluster in range(i+2):
		cluster_points = np.where(predictions[:] == cluster)[0]

		#for point in cluster_points:
		#	plot_wf = np.zeros(len(slices_dejittered[0])/10)
		#	for time in range(len(slices_dejittered[point])/10):
		#		plot_wf[time] = slices_dejittered[point, time*10]
		#	plt.plot(x-15, plot_wf, linewidth = 0.1, color = 'red')
		#	plt.hold(True)
		# plt.plot(x - int((sampling_rate/1000.0)*spike_snapshot_before), slices_dejittered[cluster_points, ::10].T, linewidth = 0.01, color = 'red')
		fig, ax = blech_waveforms_datashader.waveforms_datashader(slices_dejittered[cluster_points, :], x, dir_name = "datashader_temp_el" + str(electrode_num))
		ax.set_xlabel('Sample ({:d} samples per ms)'.format(int(sampling_rate/1000)))
		ax.set_ylabel('Voltage (microvolts)')
		ax.set_title('Cluster%i' % cluster)
		fig.savefig('./Plots/%i/Plots/%i_clusters_waveforms_ISIs/Cluster%i_waveforms' % (electrode_num, i+2, cluster))
		plt.close("all")
		
		fig = plt.figure()
		cluster_times = times_dejittered[cluster_points]
		ISIs = np.ediff1d(np.sort(cluster_times))
		ISIs = ISIs/30.0
		plt.hist(ISIs, bins = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, np.max(ISIs)])
		plt.xlim([0.0, 10.0])
		plt.title("2ms ISI violations = %.1f percent (%i/%i)" %((float(len(np.where(ISIs < 2.0)[0]))/float(len(cluster_times)))*100.0, len(np.where(ISIs < 2.0)[0]), len(cluster_times)) + '\n' + "1ms ISI violations = %.1f percent (%i/%i)" %((float(len(np.where(ISIs < 1.0)[0]))/float(len(cluster_times)))*100.0, len(np.where(ISIs < 1.0)[0]), len(cluster_times)))
		fig.savefig('./Plots/%i/Plots/%i_clusters_waveforms_ISIs/Cluster%i_ISIs' % (electrode_num, i+2, cluster))
		plt.close("all")