From ccd76234217b2fb3bdf126c2e2618dae2d0dc7df Mon Sep 17 00:00:00 2001 From: Carolin Sachgau Date: Tue, 20 Nov 2018 17:27:35 +0100 Subject: [PATCH] changed for multiple repros --- analysis_graphs.py | 8 ++--- analysis_graphs_new.py | 53 ++++++++++++++++++++++++++++++ open_nixio.py | 3 +- open_nixio_new.py | 74 +++++++++++++++++++++--------------------- 4 files changed, 95 insertions(+), 43 deletions(-) create mode 100644 analysis_graphs_new.py diff --git a/analysis_graphs.py b/analysis_graphs.py index 6ef5ade..d10a791 100644 --- a/analysis_graphs.py +++ b/analysis_graphs.py @@ -10,7 +10,7 @@ import matplotlib.pyplot as plt from IPython import embed import sys from icr_analysis import * -from open_nixio import * +from open_nixio_new import * # Parameters sampling_rate = 20000 @@ -19,7 +19,7 @@ delay = 1.5 # delay in seconds after comb reaches one end, before commencing mo cell_name = sys.argv[1].split('/')[-2] # Open Nixio File -curr_comb, intervals_dict = open_nixio(sys.argv[1], sys.argv[2]) +curr_comb, intervals_dict = open_nixio_new(sys.argv[1], sys.argv[2]) # Kernel Density estimator: gaussian fit t = np.arange(-sigma*4, sigma*4, 1/sampling_rate) @@ -53,7 +53,7 @@ if sys.argv[2] == 'average': plt.close(fig) elif sys.argv[2] == 'nonaverage': - for (speed, direct, pos, comb) in intervals_dict: + for (repro, speed, direct, pos, comb) in intervals_dict: spike_train = intervals_dict[speed, direct, pos, comb] avg_convolve_spikes = gaussian_convolve(spike_train, fxn, sampling_rate) p, freq, std_four, mn_four = fourier_psd(avg_convolve_spikes, sampling_rate) @@ -73,7 +73,7 @@ elif sys.argv[2] == 'nonaverage': ax2.axhline(y=(mn_four+std_four), xmin=0, xmax=1, linestyle='--', color='red') # ax2.axvline(x=max_four,linestyle='--', color='green') - plt.savefig(('nonavg_' + '_' + str(cell_name) + '_' + str(speed) + '_' + str(pos) + plt.savefig(('nonavg_' + str(repro) +'_' + str(cell_name) + '_' + str(speed) + '_' + str(pos) + '_' + str(comb) + '_' + str(direct) + '.png')) plt.close(fig) diff --git a/analysis_graphs_new.py b/analysis_graphs_new.py new file mode 100644 index 0000000..631d5d8 --- /dev/null +++ b/analysis_graphs_new.py @@ -0,0 +1,53 @@ +# --------------------------------------------------------------------------------------------------------------------- +# Name: Firing Rate and Fourier Script (moving comb repro) +# Purpose: Takes nixio spike data from moving comb repro and plots firing rate and power spectrum density graph +# Usage: python3 analysis_graphs.py average +# Author: Carolin Sachgau, University of Tuebingen +# Created: 20/09/2018 +# --------------------------------------------------------------------------------------------------------------------- + +import matplotlib.pyplot as plt +from IPython import embed +import sys +from icr_analysis import * +from open_nixio_new import * + +# Parameters +sampling_rate = 20000 +sigma = 0.01 # for Gaussian +delay = 1.5 # delay in seconds after comb reaches one end, before commencing movement again +cell_name = sys.argv[1].split('/')[-2] + +# Open Nixio File +intervals_dict = open_nixio_new(sys.argv[1]) + +# Kernel Density estimator: gaussian fit +t = np.arange(-sigma*4, sigma*4, 1/sampling_rate) +fxn = np.exp(-0.5*(t/sigma)**2) / np.sqrt(2*np.pi) / sigma # gaussian function + + +for (repro, speed, direct, pos, comb) in intervals_dict: + spike_train = intervals_dict[speed, direct, pos, comb] + avg_convolve_spikes = gaussian_convolve(spike_train, fxn, sampling_rate) + p, freq, std_four, mn_four = fourier_psd(avg_convolve_spikes, sampling_rate) + + # Graphing + fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) + + # Firing Rate Graph + firing_times = np.arange(0, len(avg_convolve_spikes)) + ax1.plot((firing_times / sampling_rate), avg_convolve_spikes) + ax1.set_title('Firing Rate of trial ' + str((speed, pos)) + ' comb = ' + str(comb) + '\n') + ax1.set_xlabel('Time (s)') + ax1.set_ylabel('Firing rate (Hz)') + + # Fourier Graph + ax2.semilogy(freq[freq < 200], p[freq < 200]) + ax2.axhline(y=(mn_four+std_four), xmin=0, xmax=1, linestyle='--', color='red') + # ax2.axvline(x=max_four,linestyle='--', color='green') + + plt.savefig(('nonavg_' + str(repro) +'_' + str(cell_name) + '_' + str(speed) + '_' + str(pos) + + '_' + str(comb) + '_' + str(direct) + '.png')) + plt.close(fig) + +# --------------------------------------------------------------------------------------------------------------------- diff --git a/open_nixio.py b/open_nixio.py index edee585..1d92f14 100644 --- a/open_nixio.py +++ b/open_nixio.py @@ -39,6 +39,5 @@ def open_nixio(nix_file, avg_opt): # Spike data at baseline # comb_baseline = spikes[(spikes < comb_pos[0])] - embed() - quit() + return curr_comb, intervals_dict diff --git a/open_nixio_new.py b/open_nixio_new.py index cd20805..6e0c328 100644 --- a/open_nixio_new.py +++ b/open_nixio_new.py @@ -3,40 +3,40 @@ from IPython import embed from collections import defaultdict import numpy as np -nix_file = '/home/sachgau/Documents/combproject/comb_data/2018-11-16-ag/2018-11-16-ag.nix' - - -f = nix.File.open(nix_file, nix.FileMode.ReadOnly) -b = f.blocks[0] # first block -mt = b.multi_tags['moving object-1'] -comb_pos = mt.positions[:] -comb_ext = np.array(mt.extents[:]) -spikes = b.data_arrays["Spikes-1"][:] - -feature_dict = {} -for feat in mt.features: - feature_dict.update({feat.data.name[16:]: mt.features[feat.data.name].data[:]}) - -tags = b.tags -intervals_dict = defaultdict(list) - -for tag in tags: - if tag.name.startswith('Baseline'): - continue - curr_comb = tag.metadata["RePro-Info"]["settings"]["object"] - repro_pos, = tag.position - repro_ext, = tag.extent - idx_qry = np.logical_and(repro_pos < comb_pos, comb_pos < (repro_pos + repro_ext)) - tag_idx = np.flatnonzero(idx_qry) - tag_pos = comb_pos[idx_qry] - - for idx, position in zip(tag_idx, tag_pos): - if idx == (len(comb_pos)-1): - break - curr_speed = feature_dict['speed'][idx] - curr_pos = comb_pos[idx] - curr_dir = feature_dict['direction'][idx] - curr_spikes = spikes[(spikes < comb_pos[idx + 1]) & (spikes > comb_pos[idx])] - intervals_dict.update({(tag.name, curr_speed, curr_dir, curr_pos, curr_comb): curr_spikes}) -embed() -quit() \ No newline at end of file + +def open_nixio_new(nix_file): + + f = nix.File.open(nix_file, nix.FileMode.ReadOnly) + b = f.blocks[0] # first block + mt = b.multi_tags['moving object-1'] + comb_pos = mt.positions[:] + comb_ext = np.array(mt.extents[:]) + spikes = b.data_arrays["Spikes-1"][:] + + feature_dict = {} + for feat in mt.features: + feature_dict.update({feat.data.name[16:]: mt.features[feat.data.name].data[:]}) + + tags = b.tags + intervals_dict = defaultdict(list) + + for tag in tags: + if tag.name.startswith('Baseline'): + continue + curr_comb = tag.metadata["RePro-Info"]["settings"]["object"] + repro_pos, = tag.position + repro_ext, = tag.extent + idx_qry = np.logical_and(repro_pos < comb_pos, comb_pos < (repro_pos + repro_ext)) + tag_idx = np.flatnonzero(idx_qry) + tag_pos = comb_pos[idx_qry] + + for idx, position in zip(tag_idx, tag_pos): + if idx == (len(comb_pos)-1): + break + curr_speed = feature_dict['speed'][idx] + curr_pos = comb_pos[idx] + curr_dir = feature_dict['direction'][idx] + curr_spikes = spikes[(spikes < comb_pos[idx + 1]) & (spikes > comb_pos[idx])] + intervals_dict.update({(tag.name, curr_speed, curr_dir, curr_pos, curr_comb): curr_spikes}) + + return intervals_dict \ No newline at end of file