Skip to content

Commit 03739f9

Browse files
committed
finsihed orchestrate.py, made automate changes to match.py, getting ready for testing
1 parent 41c7411 commit 03739f9

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ This repo is meant to house all of the code necessary to transfer actigraphy sou
77
This repo contains a minimal amount of hard-coding to allow for reproducibility for future studies, however, there are some parts specific to BOOST that are hard coded like Observational vs. Interventional subject numbers. If you are reproducing this, please check through some of the files to make sure they do not contain any hard codes.
88

99
## Tasks
10-
- [ ] Fix directory structure for Boost I/O folders on LSS -> Cannot because permissions
10+
- [x] Fix directory structure for Boost I/O folders on LSS -> Cannot because permissions
1111
- [x] add bids folder to observational -> see above
1212
- [x] Find out how to change output naming convention for GGIR
1313
- [x] Add .csvs to BIDS not sourcedata
1414
- [x] GGIR is formatted as GGIR-#.# with no more version numbers after the second. Full version number should be put into the readme
15-
- [ ] Finish adding all required code to move to linux
15+
- [x] Finish adding all required code to move to linux
1616
- [ ] move to linux?
1717

code/orchestrate.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import sys
1313

1414
OBSDIR = '/Volumes/vosslabhpc/Projects/BOOST/InterventionStudy/3-Experiment/data/bids'
15-
INTDIR = '/Volumes/vosslabhpc/Projects/BOOST/ObersvationalStudy/3-Experiment/data'
15+
INTDIR = '/Volumes/vosslabhpc/Projects/BOOST/ObersvationalStudy/3-Experiment/data/bids'
16+
RDSSDIR = '/Volumes/VossLab/Repositories/Accelerometer_Data/'
17+
TXT = './resources/files.txt'
18+
1619

1720
def parse_args():
1821
parser = argparse.ArgumentParser(description="move act files across locations and run GGIR on them")
1922
parser.add_argument('-i', type=str, required=True, help='Input Directory (RDSS)')
20-
parser.add_argument('-t', type=str, required=True, help='Text file of all the already ran files')
2123
parser.add_argument('-token', type=str, required=True, help='Token for RedCap API')
2224
args = parser.parse_args()
2325
return args
@@ -30,29 +32,28 @@ def init_servers():
3032
os.system("sudo mount -t cifs //itf-rs-store24.hpc.uiowa.edu/vosslabhpc /home/vosslab-svc/tmp/vosslabhpc -o uid=vosslab-svc,username=vosslab-svc,vers=3.0")
3133
except Exception as e:
3234
print(f'An error occured trying to connect to LSS: {e}')
33-
35+
sys.exit(1)
3436
try:
3537
os.system("sudo mount -t cifs //rdss.iowa.uiowa.edu/rdss_mwvoss/VossLab /mnt/nfs/rdss/rdss_vosslab -o user=vosslab-svc,uid=2418317,gid=900001021")
3638
except Exception as e:
3739
print(f'An error occured trying to connect to RDSS: {e}')
38-
40+
sys.exit(1)
3941
return None
4042

4143
def check_files():
4244
from src.match import get_files, compare
4345

44-
rdss_files = get_files(#placeholder_RDSS_dir
45-
)
46+
rdss_files = get_files(RDSSDIR)
4647

4748
if len(rdss_files) == 0:
4849
print("error: RDSS files were not grabbed - find out why")
4950

50-
need = compare(rdss_files, placeholder_text_file)
51+
need = compare(rdss_files, TXT)
5152

5253

5354
if len(need) == 0:
5455
print("no files need GGIR at the moment. quitting...")
55-
exit()
56+
sys.exit(1)
5657

5758
return need
5859

@@ -68,12 +69,12 @@ def create_comparable_dataframe(need):
6869
errors = []
6970
for index, row in lab_id_file.iterrows():
7071
#check if first column is string of four numbers
71-
if not isinstance(row[0], str) or not row[0].isdigit() or len(row[0]) != 4:
72-
errors.append(f"Row {index} has an invalid lab id: {row[0]}")
72+
if not isinstance(str(row[0]), str) or not str(row[0]).isdigit() or len(str(row[0])) != 4:
73+
errors.append(f"Row {index} has an invalid lab id: {str(row[0])}")
7374

7475
#check if second column is valid filepath
75-
if not os.path.isfile(row[1]):
76-
errors.append(f"Row {index} has an invalid filepath: {row[1]}")
76+
if not os.path.isfile(str(row[1])):
77+
errors.append(f"Row {index} has an invalid filepath: {str(row[1])}")
7778

7879
if errors:
7980
for error in errors:
@@ -85,12 +86,12 @@ def create_comparable_dataframe(need):
8586
return lab_id_file
8687

8788

88-
def get_redcap_list_and_compare(token, files, list):
89+
def get_redcap_list_and_compare(token, files):
8990

9091
from src.match import get_list, compare_ids, add_sub_to_sublist, evaluate_run
9192
sub_lab_id = get_list(token)
9293

93-
matched_df = compare_ids(files, list)
94+
matched_df = compare_ids(files, sub_lab_id)
9495

9596
add_sub_to_sublist(matched_df)
9697

@@ -106,21 +107,31 @@ def save_n_rename(matched, indir):
106107

107108
return outputs
108109

109-
def main():
110-
args = parse_args
111-
110+
def GGIR(outs):
111+
#this is where we run GGIR using the GGIR function in matched
112+
from src.match import GGIR
113+
GGIR(outs)
114+
return None
112115

113116

114-
return None
117+
def main():
118+
args = parse_args()
115119

116-
if __name__ == 'main':
117-
main()
120+
init_servers()
118121

122+
need = check_files()
119123

124+
labid_df = create_comparable_dataframe(need)
120125

126+
matched_df = get_redcap_list_and_compare(args.token, labid_df)
121127

128+
outputs = save_n_rename(matched_df, args.i)
122129

130+
GGIR(outputs)
123131

132+
return None
124133

134+
if __name__ == 'main':
135+
main()
125136

126137

code/src/match.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,47 @@ def save_n_rename_files(matched, dir):
176176
outputs.append(dest)
177177

178178
return outputs
179-
180-
def GGIR(matched):
181179

180+
def GGIR(matched):
181+
import subprocess
182182
os.system('R') #start R term
183183

184-
for index, row in matched.iterrows():
185-
# Use regex to extract the year from the file name
186-
match = re.search(r'\((\d{4})-\d{2}-\d{2}\)', row['raw_file'])
187-
188-
if row['subject_id']<7000:
189-
outdir = os.path.join(OBS_DIR, 'derivatives', 'GGIR-3.1.4', f"sub-{row['subject_id']")
190-
else:
191-
outdir = os.path.join(INT_DIR, 'derivatives', 'GGIR-3.1.4', f"sub-{row['subject_id']")
192-
#run the GGIR script
193-
os.system(f"Rscript code/src/accel.R --project_dir {outdir} --project_deriv_dir {outdir}/derivatives --files {row['raw_file']} --verbose TRUE")
184+
if isinstance(matched, list):
185+
for item in matched:
186+
if os.path.exists(item):
187+
try:
188+
result = subprocess.run(
189+
[f"Rscript code/src/accel.R --project_dir {item.split('/')[:-1]} --project_deriv_dir {item.split('/')[:-1]}/derivatives --files {item} --verbose TRUE"],
190+
check=True,
191+
stdout=subprocess.PIPE,
192+
stderr=subprocess.PIPE
193+
)
194+
print(f"Command output: {result.stdout.decode().strip()}")
195+
except subprocess.CalledProcessError as e:
196+
print(f"Command failed with exit status: {e.returncode}")
197+
print(f"Error output: {e.stderr.decode().strip()}")
198+
199+
elif isinstance(matched, pd.DataFrame):
200+
for index, row in matched.iterrows():
201+
# Use regex to extract the year from the file name
202+
match = re.search(r'\((\d{4})-\d{2}-\d{2}\)', row['raw_file'])
203+
if row['subject_id']<7000:
204+
outdir = os.path.join(OBS_DIR, 'derivatives', 'GGIR-3.1.4', f"sub-{row['subject_id']}")
205+
else:
206+
outdir = os.path.join(INT_DIR, 'derivatives', 'GGIR-3.1.4', f"sub-{row['subject_id']}")
207+
try:
208+
result = subprocess.run(
209+
[f"Rscript code/src/accel.R --project_dir {outdir} --project_deriv_dir {outdir}/derivatives --files {row['raw_file']} --verbose TRUE"],
210+
check=True,
211+
stdout=subprocess.PIPE,
212+
stderr=subprocess.PIPE
213+
)
214+
print(f"Command output: {result.stdout.decode().strip()}")
215+
216+
except subprocess.CalledProcessError as e:
217+
print(f"Command failed with exit status: {e.returncode}")
218+
print(f"Error output: {e.stderr.decode().strip()}")
219+
os.system(f"Rscript code/src/accel.R --project_dir {outdir} --project_deriv_dir {outdir}/derivatives --files {row['raw_file']} --verbose TRUE")
194220
os.system('q()')
195221
return None
196222

0 commit comments

Comments
 (0)