-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPeri-Stimulus.py
186 lines (149 loc) · 8.24 KB
/
Peri-Stimulus.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import Utility_working as Utility #custom-made utility file, contains lengthy functions
from constant import *
import numpy as np
import sys
import os
import math
import subprocess
## This script extracts the data withint peri-stimulus window (output one file ending with "Peri-stim"),
## and calculate mean, median, 95% range for each frame (output another file ending with "avg Peri-stim")
## Input: Smoothed data, stim file, ROIsToInclude.
if debug:
print("--------------Peri-Stimulus.py---------------")
x=sys.argv[2].split()
print(x)
stim_index=[int(i) for i in x]
postfix = sys.argv[3]
## User input
interval = 20*fps #10*fps would mean 10sec pre and post the start of the stim
grace = 1.5*fps #exclude some time before and after stim start/end (this is useful when you're not so cofident about start/stop time stamps)
## Note to self
#
# [16,17,18,19] for pre 4880 scratch
# [20,21,22] for pre 4880 cold
# [32,33] for post 4880 scratch
# [34,35,36] for post 4880 cold
for plane in range(0,int(float(sys.argv[1]))):
# if want to customize some varibles, can enter them in terminal (see Unit Test.txt for example)
planeNumber = "P"+str(plane)
splPrefix = expNumber + "_" + planeNumber + "_"
pathToRaw = "../"+parentFolder+"/Data/"+parentFolder+"_"+planeNumber+"_"
print("running",planeNumber,": INPUT: ",pathToOutputData + splPrefix +"Smoothed.csv","OUTPUT:",pathToOutputData + expNumber+"_" + planeNumber)
# if have a csv. file with ROIs to remove, it will be included in plotting
try:
AllROIsToRemove = np.loadtxt(pathToData +"BadROIs.csv",delimiter=',',dtype=str)
print("found badROIs")
ROIsToRemove=Utility.getROIsToRemove(debug, AllROIsToRemove, plane = planeNumber)
print("ROIsToRemove=",ROIsToRemove)
except:
ROIsToRemove = np.zeros((1,1))
print("No Bad ROIs")
#if have a csv file with ROIs to include, only those ROIs will be analyzed. Otherwise, will include all ROIs in this plane
try:
AllROIsToInclude = np.loadtxt(pathToData +"ROIsToInclude.csv",delimiter=',',dtype=str)
print("found ROIsToInclude")
ROIsToInclude=Utility.getROIsToRemove(debug, AllROIsToInclude, plane = planeNumber)
print("for plane",planeNumber,", ROIsToInclude=",ROIsToInclude)
except:
ROIsToInclude = "all"
print("including all ROIs")
#import data
splPATH = pathToOutputData + splPrefix
smoothed = np.loadtxt(splPATH +"Smoothed.csv",delimiter=',',dtype=str)
smoothed = Utility.extractData(debug, smoothed, ROIsToRemove = ROIsToRemove, stimStart = 1, stimEnd = "all",ROIs=ROIsToInclude)
stimulus = np.loadtxt(pathToData +"Stimulus.csv",delimiter=',',dtype=str,usecols = (0,1,2,3))
starts = stimulus[...,1]
ends = stimulus [...,2]
stimName = stimulus[...,0]
#pull out all durations for all stim
durations = [float(xi) - float(yi) for xi, yi in zip(ends, starts)]
theseDurations = [durations[i] for i in stim_index] #contains a list of durations of only the selected stims
max_dur = max(theseDurations) # max duration amongst the chosen stimuli
# Extract data
for i in range(len(stim_index)): #for each stimulus start time
# extract the start and end for this specific stimulus
this_start = math.ceil(float(starts[stim_index[i]]))-grace
this_end = math.ceil(float(ends[stim_index[i]]))+grace
duration=max_dur
this_name= stimName[stim_index[i]]
# cropp out the time before and after stimulus (tihs includes header)
croppedData = Utility.extractData(debug,smoothed,stimStart=this_start-interval,stimEnd=this_start + interval + duration)
y,x=croppedData.shape
if debug:
print("cropping out data from frame",this_start-interval,"to frame",this_start + interval + duration,"shape of cropped data:", croppedData.shape)
# assemble header
# stim start,end,name
name = [str(ele) for ele in [this_name] for i in range(x-1)] #repeat this_start for each ROI
name = np.insert(name,0,"stim name")
start_head = [str(ele) for ele in [this_start] for i in range(x-1)] #repeat this_start for each ROI
start_head = np.insert(start_head,0,"start fr")
end_head = [str(ele) for ele in [this_end] for i in range(x-1)] #repeat this_start for each ROI
end_head = np.insert(end_head,0,"end fr")
# mean pre vs post stim
pre=Utility.getBaselineMean(debug,croppedData,baselineStart = 1,baselineEnd = interval-grace)
pre=np.transpose(pre)
pre=pre.astype(str)
pre = np.insert(pre,0,"pre_mean")
post = Utility.getBaselineMean(debug,croppedData,baselineStart = interval+duration+grace,baselineEnd=duration + 2*interval)
post=np.transpose(post)
post=post.astype(str)
post = np.insert(post,0,"post_mean")
if debug:
print("averaged frame 1-",interval-grace, "as pre, and ",interval+duration+grace, "-",duration + 2*interval,"as post")
print("for stimulus:",stimName[stim_index[i]],"at index",i)
#put them all together
if i==0:
# contains header (first column)
out = np.vstack((croppedData[0,:],name,start_head,end_head,pre,post,croppedData[1:,:]))
stripped = croppedData
else:
temp=np.vstack((croppedData[0,:],name,start_head,end_head,pre,post,croppedData[1:,:]))
out=np.hstack((out,temp[...,1:])) # remove header from temp
stripped = np.hstack((stripped,croppedData[...,1:]))
np.savetxt(splPATH +"full Peri-stim "+postfix+".csv",out, delimiter=',', comments='', fmt='%s')
#### calculate median,mean and 95 percentile (2.5%-97.5%) for stripped dataset
y,x=stripped.shape
summary_stripped = [postfix+"_mean_"+planeNumber,postfix+"_median_"+planeNumber,"lower 95 range","upper 95 range"]
for i in range(1,y):
data=stripped[i,1:] # remove the first column b/c it'll be the frame number
data=data.astype(float)
if i%1000 == 1:
# print out every 10000 frames
print("reaching i=",i,"out of",y)
print("max signal in this frame:", np.max(data))
#calculate upper and lower limit of 95 percentile (2.5%-97.5%)
l = np.percentile(data, 2.5)
u = np.percentile(data, 97.5)
mean = np.mean(data)
median = np.median(data)
summary_stripped=np.vstack((summary_stripped,[mean,median,l,u]))
np.savetxt(splPATH +"avg Peri-stim "+postfix+".csv",summary_stripped, delimiter=',', comments='', fmt='%s')
print("wrote to file",splPATH +"_avg Peri-stim "+postfix+".csv")
########### Finish running each individual planes##########
# Run Stitch Files to combine data from all planes
subprocess.run(["python", "StitchFiles.py", parentFolder, "full Peri-stim "+postfix])
# Load the stitched file and calculate mean, median, 95% range
stitched = np.loadtxt(pathToOutputData + parentFolder + "_"+"full Peri-stim "+postfix + "_Stitched.csv",delimiter=',',dtype=str,ndmin=2)
stitched = Utility.extractData(debug, stitched)
# calculate median, mean, 95%
print("opening",pathToOutputData + parentFolder + "_"+"full Peri-stim "+postfix + "_Stitched.csv"," with shape=",stitched.shape)
y,x=stitched.shape
summary_stitched = [postfix+"_mean_"+planeNumber,postfix+"_median_"+planeNumber,"lower 95range","upper 95range"]
# i starts at 6 b/c the first 6 columns are mean of pre, post stim, stim name etc.
for i in range(6,x):
# remove the first column b/c it'll be the frame number
# not sure why i had to remove the second column as well, but hey, it works
data=stitched[2:,i]
data=data.astype(float)
if i%500 == 1: # print out results every 100 frames
# print out every 500 frames
print("reaching i=",i,"out of",y)
print("max signal in this frame:", np.max(data))
#calculate upper and lower limit of 95 percentile (2.5%-97.5%)
l = np.percentile(data, 2.5)
u = np.percentile(data, 97.5)
mean = np.mean(data)
median = np.median(data)
summary_stitched=np.vstack((summary_stitched,[mean,median,l,u]))
np.savetxt(pathToOutputData+expNumber +"_allPlanes_avg Peri-stim "+postfix+".csv",summary_stitched, delimiter=',', comments='', fmt='%s')
print("wrote to file",splPATH +"full avg Peri-stim "+postfix+".csv")