-
Notifications
You must be signed in to change notification settings - Fork 1
/
featStage1.py
73 lines (54 loc) · 2.3 KB
/
featStage1.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
import os
import shutil
from pathlib2 import Path
from fsl.data.image import Image
from fsl.data.featanalysis import loadFsf
from python_script_utils import get_subj_numbers
dataDir = '/vols/Scratch/bnc208/friend_request_task/'
designName = 'd8_noITI_ppi_stage1_fslanat' #mostly used for the fsf file
template_subj_name = 'S02'
#Grab a template
templatePath = dataDir+'/templateFSF/'+designName+'.fsf'
subj_number = get_subj_numbers()
for subj in subj_number:
subjName = 'S'+str(subj).zfill(2)
subjPath = dataDir+subjName+'/'
#copy stage1 design file to the subject folder
designFile = shutil.copyfile(templatePath, subjPath + designName+'.fsf')
# modify fsf file with subject specific paths
path = Path(designFile) # p here must be captial for the function.
text = path.read_text()
text = text.replace(template_subj_name, subjName)
path.write_text(text) #This is important to get values relevant to this subject
text = path.read_text()
# Now, get the total number of timepoints from the func data
fsfDict = loadFsf(designFile) # loading fsf file to get image path
func_path = fsfDict.get("feat_files(1)")
print(func_path)
old_npts = fsfDict.get("npts")
print("Old npts: "+str(old_npts))
#get npts of new image
img = Image(func_path)
new_npts = img.header.get("dim")[4]
print("New npts: " + str(new_npts))
#get old output dir and replace with new one
old_output = fsfDict.get('outputdir')
new_output = subjPath+designName
print("Old Output Path: "+old_output)
print("New Output Path: "+new_output)
text = text.replace(str(old_npts), str(new_npts))
text = text.replace(old_output, new_output)
path.write_text(text)
#we should now be ready to run feat
os.chdir(subjPath)
first_level_folder = new_output+".feat"
#Remove previous folder by the same name it already exists
if os.path.exists(first_level_folder):
print(f"Folder {first_level_folder} exists.")
# delete the folder
shutil.rmtree(first_level_folder)
print(f"Folder {first_level_folder} deleted.")
else:
print(f"Folder {first_level_folder} does not exist.")
os.system('fsl_sub -q verylong.q -T 360 -R 30 feat '+designName+'.fsf')
print('Started first level analysis for subject ' + subjName)