-
Notifications
You must be signed in to change notification settings - Fork 1
/
clusterperm_script.py
72 lines (57 loc) · 2.33 KB
/
clusterperm_script.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
"""A helper script to pass files to and from clusterperm.py.
Copy RSA results files to the directory where ``clusterperm.py`` is located,
then run ``clusterperm.py``,
then copy the results to another directory.
Repeat for each kind of RSA result to be tested.
"""
import datetime
import os
import shutil
import subprocess
from utils import BIDS_ROOT
def run_clusterperm(rsa_results_path):
"""Run clusterperm.py for a given results.csv file."""
# Where are we currently?
wd = os.getcwd()
# Make a temporary directory with clusterperm.py in it
time_now = datetime.datetime.now().isoformat()
tmpdir = os.path.join(BIDS_ROOT, "code", f"clusterperm_results_{time_now}")
os.makedirs(tmpdir)
clusterperm_py = os.path.join(BIDS_ROOT, "code", "clusterperm.py")
shutil.copyfile(clusterperm_py, os.path.join(tmpdir, "clusterperm.py"))
# Copy the results file to the new tmp clusterperm.py directory
dest = os.path.join(tmpdir, "rsa_results.csv")
shutil.copyfile(rsa_results_path, dest)
# call clusterperm as subprocess
# capture stdout and stderr in one stream and write that to file
# after completing the process
cmd = r"python clusterperm.py"
process = subprocess.run(
cmd, shell=True, cwd=tmpdir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
logfile = os.path.join(tmpdir, "log.out")
with open(logfile, "wb") as fout:
fout.write(process.stdout)
# copy over all results to the RSA results dir
results_dir, _ = os.path.split(rsa_results_path)
_, dirname = os.path.split(tmpdir)
shutil.copytree(tmpdir, os.path.join(results_dir, dirname))
# clean up the tmpdir
shutil.rmtree(tmpdir)
# change back to previous working directory
os.chdir(wd)
if __name__ == "__main__":
# RSA results to run clusterperm on
derivatives = os.path.join(BIDS_ROOT, "derivatives")
rsa_results_paths = [
os.path.join(
derivatives,
"rsa_9x9",
"cv-False_flip-False_rsa-pearson_dist-euclidean_half-both_exclude-Identity_mnn-FalseFalseFalse_ec-False_c-(0.6, 1.6)t--0.8s-150b-(None, 0)s-True_outcome",
"rsa_results.csv",
),
]
for rsa_results_path in rsa_results_paths:
print(f"\nstart work on: {rsa_results_path}")
run_clusterperm(rsa_results_path)
print("... finished")