-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRFgroup.py
672 lines (569 loc) · 19.3 KB
/
PRFgroup.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import numpy as np
from os import path
from glob import glob
import re
import pandas as pd
from tqdm import tqdm
from . import PRF
class PRFgroup:
"""
This is a list class for PRFclass.
You can give study and masks for subjects/sessions/...
as regexp or it will load everything.
"""
def __init__(
self,
data_from,
study,
DF,
baseP,
orientation,
derivatives_path=None,
prfanalyze=None,
hemi=None,
fit=None,
method=None,
):
# set the variables
self.data_from = data_from
self.study = study
self.data = DF
self.baseP = baseP
self.orientation = orientation
if method:
self._method = method
if prfanalyze:
self._prfanalyze = prfanalyze
if hemi:
self._hemi = hemi
if fit:
self._fit = fit
if derivatives_path:
self._derivatives_path = derivatives_path
self._doROIMsk = True
self._doVarExpMsk = True
self._doBetaMsk = True
self._doEccMsk = True
self._doSigMsk = True
def __getitem__(self, item):
return self.data.loc[item]
def __len__(self):
return len(self.data)
# --------------------------ALTERNATIVE CONSTRUCTORS--------------------------#
@classmethod
def from_docker(
cls,
study,
subjects=".*",
sessions=".*",
tasks=".*",
runs=".*",
method="vista",
prfanalyze="01",
hemi="",
baseP=None,
orientation="VF",
):
if not baseP:
baseP = "/ceph/mri.meduniwien.ac.at/projects/physics/fmri/data"
derivatives_path = path.join(
baseP,
study,
"derivatives",
)
if not path.isdir(derivatives_path):
derivatives_path = path.join(
baseP,
study,
"BIDS" "derivatives",
)
if not path.isdir(derivatives_path):
raise FileNotFoundError(
f"could not find derivatives folder at {derivatives_path} or {path.join(baseP, study, 'derivatives')}"
)
anas = glob(
path.join(
derivatives_path,
f"prfanalyze-{method}",
f"analysis-{prfanalyze}",
"*",
"*",
"*_estimates.json",
)
)
su = [path.basename(a).split("_")[0].split("-")[-1] for a in anas]
se = [path.basename(a).split("_")[1].split("-")[-1] for a in anas]
ta = [path.basename(a).split("_")[2].split("-")[-1] for a in anas]
ru = [path.basename(a).split("_")[3].split("-")[-1] for a in anas]
anasDF = pd.DataFrame(
np.array([su, se, ta, ru]).T, columns=["subject", "session", "task", "run"]
)
if len(anasDF) == 0:
print("no analyses were found!")
return
anasDF = anasDF[
(anasDF["subject"].str.match(subjects))
& (anasDF["session"].str.match(sessions))
& (anasDF["task"].str.match(tasks))
& (anasDF["run"].str.match(runs))
]
anasDF = anasDF.drop_duplicates().reset_index(drop=True)
anasDF = anasDF.sort_values(
["subject", "session", "task", "run"], ignore_index=True
)
if len(anasDF) >= 10:
pbar = tqdm(total=len(anasDF))
for I, a in anasDF.iterrows():
try:
anasDF.loc[I, "prf"] = PRF.from_docker(
study=study,
subject=a["subject"],
session=a["session"],
task=a["task"],
run=a["run"],
method=method,
analysis=prfanalyze,
hemi=hemi,
baseP=baseP,
orientation=orientation,
)
except Exception as e:
print(
f'could not load sub-{a["subject"]}_ses-{a["session"]}_task-{a["task"]}_run-{a["run"]}!'
)
print(e)
anasDF.drop(I).reset_index(drop=True)
if len(anasDF) >= 10:
pbar.update(1)
if len(anasDF) >= 10:
pbar.close()
# check if something was actually found
if len(anasDF) == 0:
raise Warning("No analyses were found!")
return cls(
data_from="docker",
study=study,
DF=anasDF,
baseP=baseP,
derivatives_path=derivatives_path,
orientation=orientation,
hemi=hemi,
prfanalyze=prfanalyze,
method=method,
)
# --------------------------ALTERNATIVE CONSTRUCTORS--------------------------#
@classmethod
def from_samsrf(
cls,
study,
subjects=".*",
sessions=".*",
tasks=".*",
runs=".*",
prfanalyze="01",
baseP=None,
orientation="VF",
):
if not baseP:
baseP = "/ceph/mri.meduniwien.ac.at/projects/physics/fmri/data"
derivatives_path = path.join(
baseP,
study,
"derivatives",
)
if not path.isdir(derivatives_path):
derivatives_path = path.join(
baseP,
study,
"BIDS" "derivatives",
)
if not path.isdir(derivatives_path):
raise FileNotFoundError(
f"could not find derivatives folder at {derivatives_path} or {path.join(baseP, study, 'derivatives')}"
)
anas = glob(
path.join(
derivatives_path,
"samsrf",
f"analysis-{prfanalyze}",
"sub-*",
"ses-*",
"bi*_pRF-results.mat",
)
)
su = [path.basename(a).split("_")[1].split("-")[-1] for a in anas]
se = [path.basename(a).split("_")[2].split("-")[-1] for a in anas]
ta = [path.basename(a).split("_")[3].split("-")[-1] for a in anas]
ru = [path.basename(a).split("_")[4].split("-")[-1] for a in anas]
anasDF = pd.DataFrame(
np.array([su, se, ta, ru, anas]).T,
columns=["subject", "session", "task", "run", "path"],
)
anasDF = anasDF[
(anasDF["subject"].str.match(subjects))
& (anasDF["session"].str.match(sessions))
& (anasDF["task"].str.match(tasks))
& (anasDF["run"].str.match(runs))
]
if len(anasDF) == 0:
print("no analyses were found!")
return
anasDF = anasDF.drop_duplicates().reset_index(drop=True)
anasDF = anasDF.sort_values(
["subject", "session", "task", "run"], ignore_index=True
)
anasDF = anasDF[["hemi-R" not in a for a in anasDF["path"]]]
if len(anasDF) >= 10:
pbar = tqdm(total=len(anasDF))
for I, a in anasDF.iterrows():
try:
if "_hemi-L" in a["path"]:
he = ""
else:
he = None
anasDF.loc[I, "prf"] = PRF.from_samsrf(
study=study,
subject=a["subject"],
session=a["session"],
task=a["task"],
run=a["run"],
analysis=prfanalyze,
baseP=baseP,
orientation=orientation,
hemis=he,
)
except Exception as e:
print(
f'could not load sub-{a["subject"]}_ses-{a["session"]}_task-{a["task"]}_run-{a["run"]}!'
)
print(e)
anasDF.drop(I).reset_index(drop=True)
if len(anasDF) >= 10:
pbar.update(1)
if len(anasDF) >= 10:
pbar.close()
# check if something was actually found
if len(anasDF) == 0:
raise Warning("No analyses were found!")
return cls(
data_from="samsrf",
study=study,
DF=anasDF,
baseP=baseP,
derivatives_path=derivatives_path,
orientation=orientation,
prfanalyze=prfanalyze,
)
# ------------------------- ALTERNATIVE CONSTRUCTORS -------------------------#
@classmethod
def from_mrVista(
cls,
study,
subjects=".*",
sessions=".*",
analysis=".*",
baseP=None,
orientation="VF",
fit="fFit-fFit-fFit",
):
if not baseP:
baseP = "/ceph/mri.meduniwien.ac.at/projects/physics/fmri/data"
anas = glob(path.join(baseP, study, "subjects", "*", "*", "mrVista", "*"))
anas = [a for a in anas if len(glob(path.join(a, "Gray", "*", f"*{fit}.mat")))]
su = [a.split("/")[-4] for a in anas]
se = [a.split("/")[-3] for a in anas]
an = [a.split("/")[-1] for a in anas]
anasDF = pd.DataFrame(
np.array([su, se, an]).T, columns=["subject", "session", "analysis"]
)
anasDF = anasDF[
(anasDF["subject"].str.match(subjects))
& (anasDF["session"].str.match(sessions))
& (anasDF["analysis"].str.match(analysis))
]
anasDF = anasDF.drop_duplicates().reset_index()
anasDF = anasDF.sort_values(
["subject", "session", "analysis"], ignore_index=True
)
if len(anasDF) == 0:
print("no analyses were found!")
return
for I, a in anasDF.iterrows():
try:
anasDF.loc[I, "prf"] = PRF.from_mrVista(
study=study,
subject=a["subject"],
session=a["session"],
analysis=a["analysis"],
server="ceph",
forcePath=None,
orientation=orientation,
fit=fit,
)
except:
print(
f'could not load subject: {a["subject"]}; session: {a["session"]}; analysis: {a["analysis"]}!'
)
anasDF.drop(I).reset_index(drop=True)
# check if something was actually found
if len(anasDF) == 0:
raise Warning("No analyses were found!")
return cls(
data_from="mrVista",
study=study,
DF=anasDF,
baseP=baseP,
orientation=orientation,
fit=fit,
)
# ---------------------------------- MASKING ---------------------------------#
def maskROI(self, area="V1", atlas="benson", doV123=False, forcePath=False):
for I, a in self.data.iterrows():
a["prf"].maskROI(area, atlas, doV123, forcePath)
def maskVarExp(
self, varExpThresh, varexp_easy=False, highThresh=None, spmPath=None
):
for I, a in self.data.iterrows():
a["prf"].maskVarExp(varExpThresh, varexp_easy, highThresh, spmPath)
def maskEcc(self, rad):
for I, a in self.data.iterrows():
a["prf"].maskEcc(rad)
def maskSigma(self, s_min, s_max=None):
for I, a in self.data.iterrows():
a["prf"].maskSigma(s_min, s_max)
def maskBetaThresh(self, betaMax=50, doBorderThresh=False, doHighBetaThresh=True):
for I, a in self.data.iterrows():
a["prf"].maskBetaThresh(betaMax, doBorderThresh, doHighBetaThresh)
# ----------------------------------- QUERY ----------------------------------#
def query(self, s):
print("Warning, no full class will be returned!")
return PRFgroup(
data_from=self.data_from,
study=self.study,
DF=self.data.query(s).reset_index(drop=True),
baseP=self.baseP,
orientation=self.orientation,
prfanalyze=None,
hemi=None,
fit=None,
method=None,
)
# ----------------------------------- APPEND ----------------------------------#
def append(self, to_append):
print("Warning: just the data will be appended!")
self.data = pd.concat([self.data, to_append.data]).reset_index(drop=True)
# --------------------------------- PROPERTIS --------------------------------#
@property
def subject(self):
return self.data["subject"].unique()
@property
def session(self):
return self.data["session"].unique()
@property
def task(self):
if self.data_from == "docker" or self.data_from == "samsrf":
t = self.data["task"].unique()
return t if len(t) > 1 else t[0]
else:
print("No task field in mrVista data")
@property
def run(self):
if self.data_from == "docker" or self.data_from == "samsrf":
return self.data["run"].unique()
else:
print("No run field in mrVista data")
@property
def analysis(self):
if self.data_from == "mrVista":
return self.data["analysis"].unique()
else:
print("No analysis field in docker data")
@property
def x0(self):
return np.hstack([a["prf"].x0 for I, a in self.data.iterrows()])
@property
def y0(self):
return np.hstack([a["prf"].y0 for I, a in self.data.iterrows()])
@property
def s0(self):
return np.hstack([a["prf"].s0 for I, a in self.data.iterrows()])
@property
def sigma0(self):
return self.s0
@property
def r0(self):
return np.hstack([a["prf"].r0 for I, a in self.data.iterrows()])
@property
def ecc0(self):
return self.r0
@property
def phi0(self):
return np.hstack([a["prf"].phi0 for I, a in self.data.iterrows()])
@property
def pol0(self):
return self.phi0
@property
def varexp0(self):
return np.hstack([a["prf"].varexp0 for I, a in self.data.iterrows()])
@property
def varexp_easy0(self):
return np.hstack([a["prf"].varexp_easy0 for I, a in self.data.iterrows()])
@property
def beta0(self):
return np.hstack([a["prf"].beta0 for I, a in self.data.iterrows()])
@property
def x(self):
return np.hstack([a["prf"].x for I, a in self.data.iterrows()])
@property
def y(self):
return np.hstack([a["prf"].y for I, a in self.data.iterrows()])
@property
def s(self):
return np.hstack([a["prf"].s for I, a in self.data.iterrows()])
@property
def sigma(self):
return self.s
@property
def r(self):
return np.hstack([a["prf"].r for I, a in self.data.iterrows()])
@property
def ecc(self):
return self.s
@property
def phi(self):
return np.hstack([a["prf"].phi for I, a in self.data.iterrows()])
@property
def pol(self):
return self.phi
@property
def varexp(self):
return np.hstack([a["prf"].varexp for I, a in self.data.iterrows()])
@property
def varexp_easy(self):
return np.hstack([a["prf"].varexp_easy for I, a in self.data.iterrows()])
@property
def meanVarExp(self):
return np.mean([a["prf"].meanVarExp for I, a in self.data.iterrows()])
@property
def beta(self):
return np.hstack([a["prf"].beta for I, a in self.data.iterrows()])
# ------------------------- SUBJECT VARS as MATRIX -----------------------#
@property
def sub_x0(self):
if not hasattr(self, "_sub_x0"):
self._sub_x0 = {}
for s in self.subject:
self._sub_x0[s] = np.array(
[a["prf"].x0 for I, a in self.data.iterrows() if a["subject"] == s]
)
return self._sub_x0
@property
def sub_y0(self):
if not hasattr(self, "_sub_y0"):
self._sub_y0 = {}
for s in self.subject:
self._sub_y0[s] = np.array(
[a["prf"].y0 for I, a in self.data.iterrows() if a["subject"] == s]
)
return self._sub_y0
@property
def sub_s0(self):
if not hasattr(self, "_sub_s0"):
self._sub_s0 = {}
for s in self.subject:
self._sub_s0[s] = np.array(
[a["prf"].s0 for I, a in self.data.iterrows() if a["subject"] == s]
)
return self._sub_s0
@property
def sub_r0(self):
if not hasattr(self, "_sub_r0"):
self._sub_r0 = {}
for s in self.subject:
self._sub_r0[s] = np.array(
[a["prf"].r0 for I, a in self.data.iterrows() if a["subject"] == s]
)
return self._sub_r0
@property
def sub_x(self):
self._sub_x = {}
for s in self.sub_x0:
self._sub_x[s] = self.sub_x0[s][:, self.sub_mask[s]]
return self._sub_x
@property
def sub_y(self):
self._sub_y = {}
for s in self.sub_y0:
self._sub_y[s] = self.sub_y0[s][:, self.sub_mask[s]]
return self._sub_y
@property
def sub_s(self):
self._sub_s = {}
for s in self.sub_s0:
self._sub_s[s] = self.sub_s0[s][:, self.sub_mask[s]]
return self._sub_s
@property
def sub_r(self):
self._sub_r = {}
for s in self.sub_r0:
self._sub_r[s] = self.sub_r0[s][:, self.sub_mask[s]]
return self._sub_r
# --------------------------------- MASKS --------------------------------#
@property
def mask(self):
return np.hstack([a["prf"].mask for I, a in self.data.iterrows()])
@property
def varExpMsk(self):
return np.hstack([a["prf"].varExpMsk for I, a in self.data.iterrows()])
@property
def roiMsk(self):
return np.hstack([a["prf"].roiMsk for I, a in self.data.iterrows()])
@property
def sub_mask(self):
self._sub_mask = {}
for s in self.subject:
self._sub_mask[s] = np.all(
[a["prf"].mask for I, a in self.data.iterrows() if a["subject"] == s],
0,
)
return self._sub_mask
@property
def doROIMsk(self):
return self._doROIMsk
@doROIMsk.setter
def doROIMsk(self, value: bool):
for I, a in self.data.iterrows():
a["prf"].doROIMsk = value
self._doROIMsk = value
@property
def doVarExpMsk(self):
return self._doVarExpMsk
@doVarExpMsk.setter
def doVarExpMsk(self, value: bool):
for I, a in self.data.iterrows():
a["prf"].doVarExpMsk = value
self._doVarExpMsk = value
@property
def doBetaMsk(self):
return self._doBetaMsk
@doBetaMsk.setter
def doBetaMsk(self, value: bool):
for I, a in self.data.iterrows():
a["prf"].doBetaMsk = value
self._doBetaMsk = value
@property
def doEccMsk(self):
return self._doEccMsk
@doEccMsk.setter
def doEccMsk(self, value: bool):
for I, a in self.data.iterrows():
a["prf"].doEccMsk = value
self._doEccMsk = value
@property
def doSigMsk(self):
return self._doSigMsk
@doEccMsk.setter
def doSigMsk(self, value: bool):
for I, a in self.data.iterrows():
a["prf"].doSigMsk = value
self._doSigMsk = value