-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatchscrub.py
61 lines (50 loc) · 2.28 KB
/
batchscrub.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
import os
import sys
import subprocess as sp
start_dir = ""
subject = ""
visit = ""
#scrubbed_root = "/Users/andrei/code/work/babylab/addscrubtier/scrubbed"
scrubbed_root = "/Users/andrei/code/work/babylab/addscrubtier/scrubbed12"
def walk_tree():
#Initialize variables "files_processed_count" and "base"
files_processed_count = 0
base = ["python", "addscrub.py"]
#Open file for writing in binary mode (wb)
with open("processed.txt", "wb") as processed:
for root, dirs, files in os.walk(start_dir):
for file in files:
print file
#if "newclan_merged.cha" in file or "_final.cha" in file:
#Call helper function to see if the subject in question has already been visited
if check_subject_visit(file):
command = base + [os.path.join(root, file),
os.path.join(scrubbed_root, file.replace(".cha", "_scrubbed.cha"))]
abbrev_command = [os.path.split(element)[1] for element in command]
print "command: {}".format(abbrev_command)
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
pipe.communicate() # blocks until the subprocess in complete
#Write to file processed
processed.write(file[0:5]+":\tcommand: {}\n".format(abbrev_command))
#Increment variable files_processed_count to keep track of number of fiels processed
files_processed_count += 1
#Print the count
print "{} files processed".format(files_processed_count)
#Helper function called in "walk_tree" function - returns boolean
def check_subject_visit(file):
#Conditions that would indicate a subject has already been visited; therefore returns "True"
if subject == "all" and visit == "all":
return True
if subject == "all" and visit == file[3:5]:
return True
if subject == file[0:2] and visit == "all":
return True
if subject == file[0:2] and visit == file[3:5]:
return True
#Returns "False" only if none of the above conditions are met
return False
if __name__ == "__main__":
start_dir = sys.argv[1]
subject = sys.argv[2]
visit = sys.argv[3]
walk_tree()