-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tuning_curve.py
162 lines (136 loc) · 5.46 KB
/
main_tuning_curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 4 17:51:47 2018
This script will help you to compute the tuning curve of all the the data from head direction cells of one animal,
and it will save the data in a .hdf file located in ./data_output
You should run this script before main_autocorrelation,py and main.py
@author: grvite
"""
import pandas as pd
from functions_mt import *
import numpy as np
#define number of bins for the tuning curve computation
nbins = 60
#number of columns for subplots
col= 15
#indicate if it is the first time that you run this script
first_run = False
#Select directory where you have all the folders of your animals data
data_directory = '../data_read_t/'
# Define output directory for the plots
output = '../plots'
# Define directory to save the .hdf data
hdf_dir = '../data_output'
"""
1. Load and accomodate data
"""
#get information about the animals and the sessions
dic = getcodes(data_directory)
if first_run == True:
#Select directory where you have all the .ang and .pos files of your animals data
pos_dir = './data_read_t/positions'
#data handling -temporary-
files_managment(dic, data_directory, pos_dir)
"""
2. Compute the tuning curve for all the HD neurons
"""
#Create a dataframe with the information of the tuning curves of all the neurons
abins = np.linspace(0, 2*np.pi, nbins)
df_tuning = pd.DataFrame(index = abins[0:-1])
for mouse in dic.keys():
for ID in dic[mouse]:
path = data_directory + mouse + '/' + ID
print('the path is', path)
try:
spikes, shank, hd_spikes = data_hand (path, ID)
except KeyError:
print('problem with spikes for {0}'.format(ID))
break
wake_ep = loadEpoch(path, 'wake')
#Make a list of your neurons numbers
indx = list(hd_spikes.keys())
print(indx)
keys = list(map(str, indx))
name_cols = list(map(lambda x: ID + '-' + x, keys))
# read angular data
ang = np.genfromtxt(path + '/' + ID + '_ang.txt')
# transform it to Tsd
ang = nts.TsdFrame(d = ang[:,1], t = ang[:,0], time_units = 's')
for i, n in zip(indx, name_cols):
print (i,n)
tuning = tuneit (hd_spikes, ang, wake_ep, i, nbins)
df_tuning[n] = pd.Series(index = abins[0:-1], data = np.squeeze(tuning.values))
#Sort values by columns
df_tuning = df_tuning.sort_index(axis=1)
"""
3. Compute widths
"""
#smooth data to make easy to cumpute the width of the tuning curve
df_smooth = df_tuning.rolling(window =5, win_type='gaussian', center=True, min_periods = 1).mean(std = 5.0)
df_tun_widths = pd.DataFrame(index= name_cols, columns = ['width'])
for i in name_cols:
print(i)
array = df_smooth[i].values
df_tun_widths.loc[i, 'width'] = width_tun (nbins, array)
"""
4. Plot Results
"""
#A. Example
##Tuning curve
label= 'width = ' + str( df_tun_widths.loc[neuron].values[0])
neuron = 'Mouse12-120806-23'
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='linen', edgecolor = 'linen', alpha=0.5)
fig = df_smooth[neuron].plot(color='chocolate', figsize=(11,8))
fig.set_ylabel("Firing rate (spk/s)", fontsize=14);
fig.set_xlabel("Direction (radians)", fontsize=14)
fig.axhline(df_smooth[neuron].max()/2, color='red')
fig.set_title(neuron, fontsize=14)
fig.annotate(label, xytext=(1.0, 12), xy=(3.88, 25.9),
arrowprops=dict(arrowstyle="->"), bbox = props, fontsize=12)
plt.savefig(output + '/tun_plot_' + neuron + '.pdf', bbox_inches = 'tight')
##Polar plot
phase = df_smooth.index.values
figp = plt.figure(figsize=(8,8))
ax = figp.add_subplot(111, projection='polar')
ax.plot(phase, df_smooth[neuron], color ='darkorange')
ax.set_title(neuron)
plt.savefig(output + '/tun_polar_' + neuron + '.pdf', bbox_inches = 'tight')
plt.plot(phase, df_smooth[neuron], color ='darkorange', projection = 'polar')
#B. General
#Determine the number of raws
raws = int(np.ceil(len(df_smooth.columns)/col))
#Get columns names
name_cols = df_tuning.columns
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='linen', edgecolor = 'linen', alpha=0.5)
fig = plt.figure(figsize=(25,160))
for c,num in zip(name_cols, range(1,len(df_smooth.columns)+1)):
label= 'width = ' + str( df_tun_widths.loc[c].values[0])
ax = fig.add_subplot(raws,col,num)
ax.plot(df_smooth[c], color ='darkorange')
ax.axhline(df_smooth[c].max()/2, color='red')
#ax.set_xlabel('radians')
ax.set_title(c)
ax.text(0.03, 0.05, label, transform=ax.transAxes, fontsize=10,
verticalalignment='top', bbox = props)
plt.tight_layout()
plt.savefig( output + '/tuning_plot' + '.pdf')
#Polar plots
#smooth data, we change the parameters just for presentation since the smoothing does not allow to close the loop for polar plots. Consider that no measure is taken from polar plo
df_polar = df_tuning.rolling(window = 5, win_type='bartlett', center=True, min_periods = 1).mean(std = 8.0)
phase = df_polar.index.values
fig = plt.figure(figsize=(40,250))
for c,num in zip(name_cols, range(1,len(df_polar.columns)+1)):
ax = fig.add_subplot(raws, col, num, projection='polar')
ax.plot(phase, df_polar[c], color ='darkorange')
#ax.set_xlabel('radians')
ax.set_title(c)
#plt.tight_layout(h_pad=0.1)
plt.savefig(output + '/tuning_polar_' + '.pdf')
"""
5. Save data in .hdf format
"""
df_tuning.to_hdf(hdf_dir + '/df_tuning.hdf', 'tuning')
df_tun_widths.to_hdf(hdf_dir + '/df_tuning_widths.hdf', 'widhts_t')