FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MultiTimeFieldAnalysis/tcMultiTestMAT.py at main · BhallaLab/MultiTimeFieldAnalysis · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
BhallaLab
/
MultiTimeFieldAnalysis
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
MultiTimeFieldAnalysis
/
tcMultiTestMAT.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
240 lines (204 loc) · 10.2 KB
Breadcrumbs
MultiTimeFieldAnalysis
/
tcMultiTestMAT.py
Copy path
File metadata and controls
240 lines (204 loc) · 10.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Test script for multi-peak detection on G394 .mat files.
Directory structure expected:
root/<date>/<sub>/*.mat
Reports per-session peak counts, a table of all cells with at least one
significant peak, and a combined heatmap (R2B | TI) across all sessions.
"""
import
os
import
glob
import
sys
import
numpy
as
np
import
scipy
.
io
import
natsort
import
matplotlib
.
pyplot
as
plt
sys
.
path
.
insert
(
0
,
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
import
pyBindMat
as
pb
ROOT
=
"/home/bhalla/homework/Hrishi/2025/DATA/G394_TenFiles_13Oct"
def
load_params
(
data
):
td
=
data
[
"trialDetails"
]
tcpy
=
data
[
"ops"
][
"tcpy"
]
sr
=
float
(
td
[
"samplingRate"
])
frameDt
=
1.0
/
sr
csOnset
=
int
(
round
(
float
(
td
[
"preDuration"
])
*
sr
))
usOnset
=
int
(
round
(
csOnset
+
float
(
td
[
"csDuration"
])
*
sr
+
float
(
td
[
"traceDuration"
])
*
sr
))
circPad
=
int
(
tcpy
[
"circPadFrames"
])
binFrames
=
int
(
tcpy
[
"binFrames"
])
numShuffle
=
int
(
tcpy
[
"numShuffle"
])
r2bThresh
=
float
(
tcpy
[
"r2bThresh"
])
r2bPercentile
=
float
(
tcpy
[
"r2bPercentile"
])
transThresh
=
float
(
tcpy
[
"transientThresh"
])
tiPercentile
=
float
(
tcpy
[
"tiPercentile"
])
fracThresh
=
float
(
tcpy
[
"fracTrialsFiredThresh"
])
return
(
csOnset
,
usOnset
,
circPad
,
binFrames
,
numShuffle
,
r2bThresh
,
r2bPercentile
,
transThresh
,
tiPercentile
,
fracThresh
,
frameDt
)
def
peak_times_s
(
indices
,
circPad
,
frameDt
,
binFrames
=
1
):
return
[(
idx
*
binFrames
-
circPad
)
*
frameDt
for
idx
in
indices
]
def
session_summary
(
results
,
method
):
counts
=
{
0
:
0
,
1
:
0
,
2
:
0
,
3
:
0
}
for
r
in
results
:
counts
[
min
(
len
(
r
[
'allPkIndices'
]),
3
)]
+=
1
print
(
f"
{
method
}
: 0 peaks=
{
counts
[
0
]
}
1 peak=
{
counts
[
1
]
}
"
f"2 peaks=
{
counts
[
2
]
}
3+ peaks=
{
counts
[
3
]
}
"
)
def
_heatmap_ax
(
ax
,
traces
,
pk_indices
,
time_axis
,
method
,
us_time
):
"""Render one normalised heatmap into ax. Returns False if no data."""
if
not
traces
:
ax
.
text
(
0.5
,
0.5
,
f'No significant
\n
{
method
}
cells'
,
ha
=
'center'
,
va
=
'center'
,
transform
=
ax
.
transAxes
,
fontsize
=
11
)
ax
.
set_title
(
method
)
return
False
order
=
np
.
argsort
(
pk_indices
)
mat
=
np
.
array
([
traces
[
i
]
for
i
in
order
],
dtype
=
float
)
rmin
=
mat
.
min
(
axis
=
1
,
keepdims
=
True
)
rmax
=
mat
.
max
(
axis
=
1
,
keepdims
=
True
)
denom
=
rmax
-
rmin
denom
[
denom
==
0
]
=
1.0
mat
=
(
mat
-
rmin
)
/
denom
ncells
=
mat
.
shape
[
0
]
dt
=
time_axis
[
1
]
-
time_axis
[
0
]
im
=
ax
.
imshow
(
mat
,
aspect
=
'auto'
,
origin
=
'upper'
,
cmap
=
'hot'
,
vmin
=
0
,
vmax
=
1
,
extent
=
[
time_axis
[
0
]
-
dt
/
2
,
time_axis
[
-
1
]
+
dt
/
2
,
ncells
,
0
])
ax
.
axvline
(
0.0
,
color
=
'cyan'
,
lw
=
1.0
,
ls
=
'--'
,
label
=
'CS onset'
)
ax
.
axvline
(
us_time
,
color
=
'lime'
,
lw
=
1.0
,
ls
=
'--'
,
label
=
'US onset'
)
ax
.
legend
(
loc
=
'upper right'
,
fontsize
=
7
)
ax
.
set_xlabel
(
'Time re CS onset (s)'
)
ax
.
set_ylabel
(
f'Cell (sorted by 1st peak) n=
{
ncells
}
'
)
ax
.
set_title
(
method
)
plt
.
colorbar
(
im
,
ax
=
ax
,
label
=
'Norm. ΔF/F'
,
fraction
=
0.03
,
pad
=
0.02
)
return
True
def
plot_heatmaps
(
r2b_traces
,
r2b_pk
,
r2b_time
,
ti_traces
,
ti_pk
,
ti_time
,
r2b_traces_mp
,
r2b_pk_mp
,
ti_traces_mp
,
ti_pk_mp
,
us_time
,
title
):
n_all
=
max
(
len
(
r2b_traces
),
len
(
ti_traces
),
1
)
n_mp
=
max
(
len
(
r2b_traces_mp
),
len
(
ti_traces_mp
),
1
)
fig
,
axes
=
plt
.
subplots
(
2
,
2
,
figsize
=
(
14
,
n_all
*
0.12
+
n_mp
*
0.12
+
3.0
),
gridspec_kw
=
{
'height_ratios'
: [
n_all
,
n_mp
]})
fig
.
suptitle
(
title
)
_heatmap_ax
(
axes
[
0
,
0
],
r2b_traces
,
r2b_pk
,
r2b_time
,
'R2B — all significant'
,
us_time
)
_heatmap_ax
(
axes
[
0
,
1
],
ti_traces
,
ti_pk
,
ti_time
,
'TI — all significant'
,
us_time
)
_heatmap_ax
(
axes
[
1
,
0
],
r2b_traces_mp
,
r2b_pk_mp
,
r2b_time
,
'R2B — 2+ peaks'
,
us_time
)
_heatmap_ax
(
axes
[
1
,
1
],
ti_traces_mp
,
ti_pk_mp
,
ti_time
,
'TI — 2+ peaks'
,
us_time
)
plt
.
tight_layout
()
plt
.
show
()
def
main
():
dates
=
natsort
.
natsorted
(
d
for
d
in
os
.
listdir
(
ROOT
)
if
os
.
path
.
isdir
(
os
.
path
.
join
(
ROOT
,
d
))
)
total_cells
=
0
total_r2b
=
{
0
:
0
,
1
:
0
,
2
:
0
,
3
:
0
}
total_ti
=
{
0
:
0
,
1
:
0
,
2
:
0
,
3
:
0
}
table_rows
=
[]
# Accumulated across all sessions for the combined heatmap
r2b_traces
,
r2b_pk
,
r2b_time
=
[], [],
None
ti_traces
,
ti_pk
,
ti_time
=
[], [],
None
r2b_traces_mp
,
r2b_pk_mp
=
[], []
# 2+ peaks only
ti_traces_mp
,
ti_pk_mp
=
[], []
us_time_s
=
None
for
date
in
dates
:
for
sub
in
[
"1"
,
"2"
]:
mat_path
=
os
.
path
.
join
(
ROOT
,
date
,
sub
)
if
not
os
.
path
.
isdir
(
mat_path
):
continue
matfiles
=
glob
.
glob
(
os
.
path
.
join
(
mat_path
,
"*.mat"
))
if
not
matfiles
:
continue
try
:
data
=
scipy
.
io
.
loadmat
(
matfiles
[
0
],
simplify_cells
=
True
)
except
Exception
as
e
:
print
(
f" Could not load
{
matfiles
[
0
]
}
:
{
e
}
"
)
continue
dfbf
=
data
[
"dfbf_clean"
]
(
csOnset
,
usOnset
,
circPad
,
binFrames
,
numShuffle
,
r2bThresh
,
r2bPercentile
,
transThresh
,
tiPercentile
,
fracThresh
,
frameDt
)
=
load_params
(
data
)
circShuff
=
dfbf
.
shape
[
2
]
-
(
csOnset
-
circPad
)
print
(
f"
\n
{
date
}
/
{
sub
}
[
{
dfbf
.
shape
[
0
]
}
cells,
{
dfbf
.
shape
[
1
]
}
trials, "
f"
{
dfbf
.
shape
[
2
]
}
frames] csOnset=
{
csOnset
}
usOnset=
{
usOnset
}
"
f"frameDt=
{
frameDt
:.4f
}
"
)
r2b_res
=
pb
.
runR2Banalysis
(
dfbf
,
csOnset
,
usOnset
,
circPad
,
circShuff
,
binFrames
,
numShuffle
,
r2bThresh
,
r2bPercentile
,
frameDt
=
frameDt
)
ti_res
=
pb
.
runTIanalysis
(
dfbf
,
csOnset
,
usOnset
,
circPad
,
circShuff
,
binFrames
,
numShuffle
,
transThresh
,
tiPercentile
,
fracThresh
,
frameDt
)
session_summary
(
r2b_res
,
"R2B"
)
session_summary
(
ti_res
,
"TI "
)
total_cells
+=
dfbf
.
shape
[
0
]
for
r
in
r2b_res
:
total_r2b
[
min
(
len
(
r
[
'allPkIndices'
]),
3
)]
+=
1
for
r
in
ti_res
:
total_ti
[
min
(
len
(
r
[
'allPkIndices'
]),
3
)]
+=
1
# Table rows
for
cell_idx
, (
rr
,
tt
)
in
enumerate
(
zip
(
r2b_res
,
ti_res
)):
r2b_pks
=
list
(
zip
(
peak_times_s
(
rr
[
'allPkIndices'
],
circPad
,
frameDt
,
binFrames
=
1
),
rr
[
'allPkScores'
],
rr
[
'allPkPvalues'
]))
ti_pks
=
list
(
zip
(
peak_times_s
(
tt
[
'allPkIndices'
],
circPad
,
frameDt
,
binFrames
=
binFrames
),
tt
[
'allPkScores'
],
tt
[
'allPkPvalues'
]))
if
r2b_pks
or
ti_pks
:
table_rows
.
append
((
date
,
sub
,
cell_idx
,
r2b_pks
,
ti_pks
))
# Accumulate traces for combined heatmap
this_r2b_time
=
np
.
array
([(
ff
-
circPad
)
*
frameDt
for
ff
in
range
(
circShuff
)])
numBins
=
circShuff
//
binFrames
this_ti_time
=
np
.
array
([(
bb
*
binFrames
-
circPad
)
*
frameDt
for
bb
in
range
(
numBins
)])
if
r2b_time
is
None
:
r2b_time
=
this_r2b_time
ti_time
=
this_ti_time
us_time_s
=
(
usOnset
-
csOnset
)
*
frameDt
for
r
in
r2b_res
:
n
=
len
(
r
[
'allPkIndices'
])
if
n
>=
1
:
trace
=
np
.
array
(
r
[
'meanTrace'
])
if
len
(
trace
)
==
len
(
r2b_time
):
r2b_traces
.
append
(
trace
)
r2b_pk
.
append
(
r
[
'allPkIndices'
][
0
])
if
n
>=
2
:
r2b_traces_mp
.
append
(
trace
)
r2b_pk_mp
.
append
(
r
[
'allPkIndices'
][
0
])
for
r
in
ti_res
:
n
=
len
(
r
[
'allPkIndices'
])
if
n
>=
1
:
trace
=
np
.
array
(
r
[
'meanTrace'
])
if
len
(
trace
)
==
len
(
ti_time
):
ti_traces
.
append
(
trace
)
ti_pk
.
append
(
r
[
'allPkIndices'
][
0
])
if
n
>=
2
:
ti_traces_mp
.
append
(
trace
)
ti_pk_mp
.
append
(
r
[
'allPkIndices'
][
0
])
# ── Summary ──────────────────────────────────────────────────────────────
print
(
f"
\n
=== Totals across all sessions ==="
)
print
(
f" Cells analysed :
{
total_cells
}
"
)
print
(
f" R2B — 0 peaks:
{
total_r2b
[
0
]
}
1 peak:
{
total_r2b
[
1
]
}
"
f"2 peaks:
{
total_r2b
[
2
]
}
3+ peaks:
{
total_r2b
[
3
]
}
"
)
print
(
f" TI — 0 peaks:
{
total_ti
[
0
]
}
1 peak:
{
total_ti
[
1
]
}
"
f"2 peaks:
{
total_ti
[
2
]
}
3+ peaks:
{
total_ti
[
3
]
}
"
)
# ── Per-cell table ────────────────────────────────────────────────────────
if
not
table_rows
:
print
(
"
\n
No cells with significant peaks found."
)
else
:
hdr
=
(
f"
{
'date'
:<10
}
{
'sub'
:<4
}
{
'cell'
:>4
}
"
f"
{
'method'
:<5
}
{
'pk#'
:>3
}
{
'time(s)'
:>8
}
{
'score'
:>8
}
{
'p-val'
:>7
}
"
)
print
(
f"
\n
=== Cells with significant peaks ==="
)
print
(
hdr
)
print
(
"-"
*
len
(
hdr
))
for
date
,
sub
,
cell_idx
,
r2b_pks
,
ti_pks
in
table_rows
:
for
method
,
pks
in
[(
"R2B"
,
r2b_pks
), (
"TI"
,
ti_pks
)]:
for
pk_num
, (
t
,
score
,
pval
)
in
enumerate
(
pks
,
1
):
print
(
f"
{
date
:<10
}
{
sub
:<4
}
{
cell_idx
:>4
}
"
f"
{
method
:<5
}
{
pk_num
:>3
}
{
t
:>8.3f
}
{
score
:>8.4f
}
{
pval
:>7.4f
}
"
)
# ── Combined heatmap ─────────────────────────────────────────────────────
if
r2b_time
is
not
None
:
plot_heatmaps
(
r2b_traces
,
r2b_pk
,
r2b_time
,
ti_traces
,
ti_pk
,
ti_time
,
r2b_traces_mp
,
r2b_pk_mp
,
ti_traces_mp
,
ti_pk_mp
,
us_time_s
,
"All sessions — G394"
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL