-
Notifications
You must be signed in to change notification settings - Fork 1
/
alohaDriver
executable file
·527 lines (433 loc) · 21.8 KB
/
alohaDriver
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
#!/bin/bash
#
# -A generate the Atrophy report
#
# alohaDriver -v -o none 66e4737e1b4b58bc3236d6d1
# where 66e4737e1b4b58bc3236d6d1 is a subject id
# just generats the AlohaInputs.json file
#
# alohaDriver -v -o site 66e4737e1b4b58bc3236d6d1
# generates the AlohaInputs.json file and uploads to the flywheel subject
#
# alohaDriver -v -u 66e4737e1b4b58bc3236d61d
# regenerate the json files which take a long time to dig out of flywheel
#
# alohaDriver -v -d 66e4737e1b4b58bc3236d61d
# Just download the aloha input files
#
# alohaDriver -v -r 66e4737e1b4b58bc3236d61d
# Run the aloha script
#
# alohaDriver -R 66e4737e1b4b58bc3236d61d
# Run the report
#
# -a download AlohaInputs.json from the flywheel subject container
# -d download input files
#
# *** The ignore/run/Fake logic is not correct.
#
# -f Comma separated list of portions json collection to fake
# IgnoreBaselineAshsPmcT1JobId|IgnoreBaselineAshsPmcT2JobId
# IgnoreBaselineT1DicomJson|IgnoreBaselineT1LeftSegmentationJson|IgnoreBaselineT1NiftiTrimmedJson|IgnoreBaselineT1RightSegmentationJson
# IgnoreBaselineT2DicomJson|IgnoreBaselineT2LeftSegmentationJson|IgnoreBaselineT2NiftiJson|IgnoreBaselineT2RightSegmentationJson
# IgnoreFollowupT1DicomJson|IgnoreFollowupT1NiftiTrimmedJson|IgnoreFollowupT2DicomJson|IgnoreFollowupT2NiftiJson
# IgnoreAlohaRun
#
# RunAshsPmcT1Gear|RunBaselineAshsPmcT2Gear
# RunBaselineT1Dcm2NiixGear|RunBaselineT1NecktrimGear
# RunBaselineT2Dcm2NiixGear|RunFollowupT1Dcm2NiixGear|RunFollowupT1NecktrimGear|RunFollowupT2Dcm2NiixGear
#
# alohaDriver -f RunBaselineT1Dcm2NiixGear,RunBaselineT2Dcm2NiixGear -o stdout 66e4737e1b4b58bc3236d61d
#
# -j JobId
# -m MachineTag - tag to use change the default machine size
# -n no-op
# -o [site|stdout|none]
# create AlohaInputs.json and upload it to the flywheel site, print to stdout, neither
# the AlohaInputs.json file will not be created unless -o is part of the command line
# -r run aloha code
# -t print atrophy rates in table form
# -u update files that take a long time to get
# -v verbose
CmdName=$(basename "$0")
FwDir=$(dirname $(dirname $(which fwget)))
FwLibDir="${FwDir}/lib"
function sys {
[ -n "${opt_n}${opt_v}" ] && echo "$@" 1>&2
[ -n "$opt_n" ] || "$@"
}
echo "${CmdName} $@" 1>&2
while getopts Aadf:j:m:no:rtuv arg
do
case "$arg" in
A|a|d|f|j|m|n|o|r|t|u|v)
eval "opt_${arg}='${OPTARG:=1}'"
;;
esac
done
shift $(( "$OPTIND" - 1 ))
[ -n "$opt_u" ] && UpdateAll=1
TmpDir="/tmp/Aloha"
[ -e "$TmpDir" ] || mkdir -p "$TmpDir"
AlohaInputsJsonFile="${TmpDir}/AlohaInputs.json"
AlohaWorkDir="${TmpDir}/Work"
[ -e "$AlohaWorkDir" ] || mkdir -p "$AlohaWorkDir"
SubjectId="$1"
SubjectJsonFile="${TmpDir}/Subject.json"
if [ ! \( \( -e "$SubjectJsonFile" \) -a \( -s "$SubjectJsonFile" \) \) ] || [ -n "$UpdateAll" ]
then
sys fwget -1 -raGz "$SubjectId" > "$SubjectJsonFile"
if [ "$?" != 0 ]
then
echo "${CmdName} : failed getting subject '$SubjectId'" 1>&2
exit 1
fi
fi
SubjectLabel=$(jq -r '.label' "$SubjectJsonFile")
Group=$(jq -r '.parents.group' "$SubjectJsonFile")
Id2LabelsJsonFile="${TmpDir}/Id2Labels.json"
echo -n > "$Id2LabelsJsonFile"
jq -r '[.sessions[] | { (._id) : .label }] | add ' "$SubjectJsonFile" >> "$Id2LabelsJsonFile"xo
if [ -n "$opt_m" ]
then
GearTags="$opt_m"
else
GearTags='extra-large'
fi
FollowupDir="${TmpDir}/Followups"
BaselineJsonFile="${TmpDir}/Baseline.json"
BaselineDir="${TmpDir}/Baseline"
# BaselineJobsJsonFile is created by alohaFindSegementation
BaselineJobsJsonFile="${TmpDir}/BaselineJobs.json"
[ -d "$BaselineDir" ] || mkdir -p "$BaselineDir"
SortedSessionsJsonFile="${TmpDir}/SortedSessions.json"
jq -r -L "$FwLibDir" -f alohaSortSessions.jq "$SubjectJsonFile" > "$SortedSessionsJsonFile"
# *** Don't need acquisition info as that will be in the T1/T2 stanzas
BaselineJson=$(jq -r '.Baseline' "$SortedSessionsJsonFile")
if [ -z "$BaselineJson" ] || [ "$BaselineJson" = 'null' ]
then
echo "${CmdName} : Missing Baseline session" 1>&2
exit 2
fi
BaselineSessionLabel=$(echo "$BaselineJson" | jq -r '.SessionLabel')
BaselineSessionId=$(echo "$BaselineJson" | jq -r '.SessionId')
BaselineSessionJsonFile="${TmpDir}/BaselineSession.json"
jq -r '..|select(._id == "'"$BaselineSessionId"'")?' "$SubjectJsonFile" > "$BaselineSessionJsonFile"
if [ -n "$opt_o" ]
then
FollowupCount=$(jq -r '[.Followups[] | select(.)] | length' "$SortedSessionsJsonFile")
# *** should make sure not all the followup sessions are null
if [ 0 -ge "$FollowupCount" ]
then
echo "${CmdName} : Missing Followup sessions" 1>&2
exit 3
fi
BaselineT1NiftiTrimmedJson=$(alohaT2NiftiJson -v -a BaselineT1NiftiTrimmed "$BaselineSessionJsonFile")
if [ -z "$BaselineT1NiftiTrimmedJson" ] || [ "$BaselineT1NiftiTrimmedJson" = "null" ]
then
echo "${CmdName} : Could not find BaselineT1NiftiTrimmed file in ${BaselineSessionJsonFile}" 1>&2
exit 2
fi
BaselineT1NiftiTrimmedFileId=$(echo "$BaselineT1NiftiTrimmedJson" | jq -r '.FileId')
BaselineT2NiftiJson=$(alohaT2NiftiJson -v -a BaselineT2Nifti "$BaselineSessionJsonFile")
if [ -z "$BaselineT2NiftiJson" ] || [ "$BaselineT2NiftiJson" = "null" ]
then
echo "${CmdName} : Could not find BaselineT2Nifti file in ${BaselineSessionJsonFile}" 1>&2
exit 3
fi
BaselineT2NiftiFileId=$(echo "$BaselineT2NiftiJson" | jq -r '.FileId')
alohaFindSegmentationArgs=()
[ -n "$opt_f" ] && alohaFindSegmentationArgs+=( -f "$opt_f")
BaselineT1Json=$(alohaFindSegmentationJson -v -c 'T1' -1 "$BaselineT1NiftiTrimmedFileId" -2 "$BaselineT2NiftiFileId" -t "$GearTags" "${alohaFindSegmentationArgs[@]}" "$BaselineSessionJsonFile")
if [ -z "$BaselineT1Json" ]
then
echo "${CmdName} : Baseline T1 Segmentation failed. Exiting" 1>&2
exit 4
fi
BaselineT2Json=$(alohaFindSegmentationJson -v -c 'T2' -1 "$BaselineT1NiftiTrimmedFileId" -2 "$BaselineT2NiftiFileId" -t "$GearTags" "${alohaFindSegmentationArgs[@]}" "$BaselineSessionJsonFile")
if [ -z "$BaselineT2Json" ]
then
echo "${CmdName} : Baseline T2 Segmentation failed. Exiting" 1>&2
exit 4
fi
BaselineT1LeftSegmentationJson=$(echo "$BaselineT1Json" | jq -r '.T1LeftSegmentation')
BaselineT1RightSegmentationJson=$(echo "$BaselineT1Json" | jq -r '.T1RightSegmentation')
BaselineT2LeftSegmentationJson=$(echo "$BaselineT2Json" | jq -r '.T2LeftSegmentation')
BaselineT2RightSegmentationJson=$(echo "$BaselineT2Json" | jq -r '.T2RightSegmentation')
#
# Followup Session Info should include session data
#
JobDate=$(date +%Y-%m-%dT%H:%M:%S%:z)
JobLabel='Aloha'
JobId=
[ -n "$opt_j" ] && JobId="$opt_j"
JobJson="{\"JobLabel\": \"$JobLabel\", \"JobId\": \"$JobId\", \"JobDate\": \"$JobDate\"}"
jq -r -n --argjson JobInfo "$JobJson" \
--argjson Baseline "$BaselineJson" \
--argjson BaselineT1 "$BaselineT1NiftiTrimmedJson" \
--argjson BaselineT2 "$BaselineT2NiftiJson" \
--argjson BaselineT1LeftSegmentation "$BaselineT1LeftSegmentationJson" \
--argjson BaselineT1RightSegmentation "$BaselineT1RightSegmentationJson" \
--argjson BaselineT2LeftSegmentation "$BaselineT2LeftSegmentationJson" \
--argjson BaselineT2RightSegmentation "$BaselineT2RightSegmentationJson" \
' { "JobInfo": $JobInfo} + {"Baseline": (
$Baseline
+ {
"T1": $BaselineT1
, "T2": $BaselineT2
, "T1LeftSegmentation": $BaselineT1LeftSegmentation
, "T1RightSegmentation": $BaselineT1RightSegmentation
, "T2LeftSegmentation": $BaselineT2LeftSegmentation
, "T2RightSegmentation": $BaselineT2RightSegmentation
})
}' \
> "$BaselineJsonFile"
FollowupsJsonFile="${TmpDir}/Followups.json"
rm -f "$FollowupsJsonFile"
for FollowupSessionId in $(jq -r '.Followups[] | .SessionId' "$SortedSessionsJsonFile")
do
#
# Single Followup Session
#
FollowupSessionJsonFile="${TmpDir}/FollowupSession.json"
jq -r '..|select(._id == "'"$FollowupSessionId"'")?' "$SubjectJsonFile" > "$FollowupSessionJsonFile"
if [ -S "$FollowupSessionJsonFile" ]
then
echo "${CmdName}: No followup session found" 1>&2
exit 11
fi
#
# SessionId, SessionLabel, SessionScanDateTime
#
FollowupJson=$(jq -r -L "$FwLibDir" 'include "FwLib"; ..|select(._id == "'"$FollowupSessionId"'")? | ({"SessionId": ._id, "SessionLabel": .label, "SessionScanDateTime": sessionScanDateTime(.)})' "$SubjectJsonFile")
FollowupSessionLabel=$(echo "$FollowupJson" | jq -r '.SessionLabel')
[ -n "$opt_v" ] && echo -n "${CmdName}: FollowupSession = '${FollowupSessionLabel}(${FollowupSessionId})': " 1>&2
AlohaArgFlagName='"FollowupT1NiftiTrimmed"'
FollowupT1NiftiTrimmedJson=$(alohaT2NiftiJson -v -a FollowupT1NiftiTrimmed "$FollowupSessionJsonFile")
if [ -z "$FollowupT1NiftiTrimmedJson" ] || [ "$FollowupT1NiftiTrimmedJson" = 'null' ]
then
echo "${CmdName} : Could not find FollowupT1NiftiTrimmed file in ${FollowupSessionJsonFile} for ${FollowupSessionLabel}($FollowupSessionId). Skipping" 1>&2
continue
fi
FollowupT2NiftiJson=$(alohaT2NiftiJson -v -a FollowupT2Nifti "$FollowupSessionJsonFile")
if [ -z "$FollowupT2NiftiJson" ] || [ "$FollowupT2NiftiJson" = "null" ]
then
echo "${CmdName} : Could not find FollowupT2Nifti file in ${FollowupSessionJsonFile} for ${FollowupSessionLabel}($FollowupSessionId). Skipping" 1>&2
continue
fi
if [ -n "$opt_v" ] && (echo "$FollowupT1NiftiTrimmedJson" "$FollowupT2NiftiJson" | jq -r -s '[ ..|.Missing? ] | any(.)' | grep -q 'false')
then
echo "Found T1 and T2 Niftis. Processing" 1>&2
fi
jq -r -n --argjson Followup "$FollowupJson" \
--argjson FollowupT1 "$FollowupT1NiftiTrimmedJson" \
--argjson FollowupT2 "$FollowupT2NiftiJson" \
'$Followup + {"T1": $FollowupT1, "T2": $FollowupT2}' >> "$FollowupsJsonFile"
done
jq -r -n --slurpfile Baseline "$BaselineJsonFile" --slurpfile Followups "$FollowupsJsonFile" '($Baseline[]) + ({ "Followups": $Followups })' > "$AlohaInputsJsonFile"
if echo "$opt_o" | grep -qi 'stdout'
then
cat "$AlohaInputsJsonFile"
fi
if (echo "$opt_o" | grep -qi 'site')
then
if [ -s "$AlohaInputsJsonFile" ]
then
sys fwuploadfile -p "$SubjectId" "$AlohaInputsJsonFile"
else
echo "${CmdName} : zero length $AlohaInputs file. Not uploading" 1>&2
fi
fi
fi
if [ -n "$opt_a" ]
then
AlohaInputsJsonFilename=$(basename "$AlohaInputsJsonFile")
AlohaInputsJsonFileId=$(fwget -1 -raz "$SubjectId" | jq -r '.files[] | select(.name == "'"$AlohaInputsJsonFilename"'") | .file_id')
sys fwget -v -f --download --download-dir "$TmpDir" "$AlohaInputsJsonFileId"
fi
DownloadedAllFiles=true
if [ -n "$opt_d" ]
then
for FileId in $(jq -r '.Baseline | ..|.FileId? | select(.)' $AlohaInputsJsonFile)
do
sys fwget -v -f --download --download-dir "$BaselineDir" "$FileId"
if [ "$?" != 0 ]
then
echo "${CmdName} : downloading file '$FileId' failed" 1>&2
DownloadAllFiles=false
fi
done
for FollowupSessionId in $(jq -r '.Followups[].SessionId' "$AlohaInputsJsonFile")
do
[ -n "$opt_v" ] && echo "FollowupSessionId = '$FollowupSessionId'" 1>&2
FollowupSessionLabel=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .SessionLabel' "$AlohaInputsJsonFile")
FollowupSessionDir="${FollowupDir}/${FollowupSessionLabel}"
[ -e "$FollowupSessionDir" ] || mkdir -p "$FollowupSessionDir"
FollowupSessionJsonFile="${FollowupSessionDir}/FollowupSession.json"
jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'")' "$AlohaInputsJsonFile" > "$FollowupSessionJsonFile"
for FileId in $(jq -r '..|.FileId? | select(.)' "$FollowupSessionJsonFile")
do
sys fwget -v -f --download --download-dir "$FollowupSessionDir" "$FileId"
if [ "$?" != 0 ]
then
echo "${CmdName} : downloading file '$FileId' failed" 1>&2
DownloadAllFiles=false
fi
done
done
fi
if [ -n "$opt_r" ]
then
AlohaBaselineArgs=()
AlohaCliBaselineArgs=()
BaselineSessionScanDateTime=$(jq -r '.Baseline.SessionScanDateTime' "$AlohaInputsJsonFile")
BaselineSessionScanDate=$(date -d "$BaselineSessionScanDateTime" "+%Y-%m-%d")
# *** Should verify aspect ratios for T1/T2 are acceptable
#skip the session info keys
for k in $(jq -r '.Baseline | keys[] as $k | select((.[$k]|type) == "object") | $k' "$AlohaInputsJsonFile")
do
AlohaArgFlag=$(jq -r '.Baseline.'"$k"' | .AlohaArgFlag' "$AlohaInputsJsonFile")
AlohaArgFlagName=$(jq -r '.Baseline.'"$k"' | .AlohaArgFlagName' "$AlohaInputsJsonFile")
FileName=$(jq -r '.Baseline.'"$k"' | .FileName' "$AlohaInputsJsonFile")
FileId=$(jq -r '.Baseline.'"$k"' | .FileId' "$AlohaInputsJsonFile")
InputFilePath="${BaselineDir}/${FileName}"
AlohaBaselineArgs+=( '-i' "$AlohaArgFlagName" "$FileId" )
AlohaCliBaselineArgs+=( "$AlohaArgFlag" "$InputFilePath" )
done
AlohaFollowupArgs=()
AlohaCliFollowupArgs=()
for FollowupSessionId in $(jq -r '.Followups[].SessionId' "$AlohaInputsJsonFile")
do
RunAloha=true
FollowupSessionScanDateTime=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .SessionScanDateTime' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionScanDateTime" ] || [ "$FollowupSessionScanDateTime" = 'null' ]
then
echo "${CmdName} : No FollowupSessionScanDateTime for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
fi
FollowupSessionScanDate=$(date -d "$FollowupSessionScanDateTime" "+%Y-%m-%d")
FollowupSessionLabel=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .SessionLabel' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionLabel" ] || [ "$FollowupSessionLabel" = 'null' ]
then
echo "${CmdName} : No FollowupSessionLabel for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
RunAloha=false
fi
[ -n "$opt_v" ] && echo "${CmdName} : FollowupSessionLabel = '$FollowupSessionLabel'" 1>&2
FollowupSessionT1AlohaArgFlag=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T1.AlohaArgFlag' "$AlohaInputsJsonFile")
FollowupSessionT1AlohaArgFlagName=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T1.AlohaArgFlagName' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionT1AlohaArgFlag" ] || [ "$FollowupSessionT1AlohaArgFlag" = 'null' ]
then
echo "${CmdName} : No FollowupSessionT1AlohaFlag for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
RunAloha=false
fi
FollowupSessionT1FileName=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T1.FileName' "$AlohaInputsJsonFile")
FollowupSessionT1FileId=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T1.FileId' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionT1FileName" ] || [ "$FollowupSessionT1FileName" = 'null' ]
then
echo "${CmdName} : No FollowupSessionT1FileName for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
RunAloha=false
fi
FollowupSessionT2AlohaArgFlag=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T2.AlohaArgFlag' "$AlohaInputsJsonFile")
FollowupSessionT2AlohaArgFlagName=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T2.AlohaArgFlagName' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionT2AlohaArgFlag" ] || [ "$FollowupSessionT2AlohaArgFlag" = 'null' ]
then
echo "${CmdName} : No FollowupSessionT2AlohaArgFlag for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
RunAloha=false
fi
FollowupSessionT2FileName=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T2.FileName' "$AlohaInputsJsonFile")
FollowupSessionT2FileId=$(jq -r '.Followups[] | select(.SessionId == "'"$FollowupSessionId"'") | .T2.FileId' "$AlohaInputsJsonFile")
if [ -z "$FollowupSessionT2FileName" ] || [ "$FollowupSessionT2FileName" = 'null' ]
then
echo "${CmdName} : No FollowupSessionT2FileName for '${FollowupSessionLabel}(${FollowupSessionId})'" 1>&2
RunAloha=false
fi
[ -n "$opt_v" ] && echo "${CmdName} : RunAloha = '$RunAloha'" 1>&2
if [ "$RunAloha" = 'true' ]
then
FollowupSessionDir="${FollowupDir}/${FollowupSessionLabel}"
FollowupSessionT1FilePath="${FollowupSessionDir}/${FollowupSessionT1FileName}"
FollowupSessionT2FilePath="${FollowupSessionDir}/${FollowupSessionT2FileName}"
FollowupSessionT1FileId="${FollowupSessionT1FileId}"
FollowupSessionT2FileId="${FollowupSessionT2FileId}"
AlohaFollowupArgs+=( -i "$FollowupSessionT1AlohaArgFlagName" "$FollowupSessionT1FileId" )
AlohaFollowupArgs+=( -i "$FollowupSessionT2AlohaArgFlagName" "$FollowupSessionT2FileId" )
AlohaCliFollowupArgs+=( "$FollowupSessionT1AlohaArgFlag" "$FollowupSessionT1FilePath" )
AlohaCliFollowupArgs+=( "$FollowupSessionT2AlohaArgFlag" "$FollowupSessionT2FilePath" )
# *** there is one last aloha job, which may or may not have been for these sessions!
# Need to check if there is a completed aloha job which has these session ids as inputs
CompletedBaselineSessionJobsJsonFile="${TmpDir}/CompletedBaselineSessionJobs.json"
LastAlohaBackendJobId=$(sys alohaFindCompletedJobs -v -b "$BaselineSessionId" -f "$FollowupSessionId" -c "CompletedBaselineSessionJobsJsonFile" -g 'alohabackend' -j "$BaselineJobsJsonFile")
# Run the backend if opt_f says to, or there isn't enough info from a previous alohabackend run, unless we're told to fake the backend
if (
# LastAlohaBackendJobId is way easier to track in bash -x than Json
( [ -n "$LastAlohaBackendJobId" ] ) &&
(
( [ -z "$opt_f" ] ) ||
( (echo "$opt_f" | grep -q 'FakeAlohaBackendGear')) ||
( (echo "$opt_f" | grep -E -v -q 'IgnoreAlohaBackendJson|RunAlohaBackendGear') )
)
)
then
echo "aloha_main.sh" "${AlohaCliBaselineArgs[@]}" "${AlohaCliFollowupArgs[@]}"
AlohaBackendJobId="$LastAlohaBackendJobId"
echo "${CmdName} : Found AlohaBackend Job Id, ${AlohaBackendJobId}, for ${FollowupSessionLabel}($FollowupSessionId)" 1>&2
else
AlohaBackendJobId=$(sys fwRunGear -w -t "$GearTags" -g alohabackend "${AlohaBaselineArgs[@]}" "${AlohaFollowupArgs[@]}")
ExitCode="$?"
if [ "$ExitCode" != 0 ]
then
echo "${CmdName} : aloha_main.sh failed '${ExitCode}'" 1>&2
continue
fi
fi
if [ -n "$AlohaBackendJobId" ]
then
AlohaBackendOutputJson=$(fwget -1 -j "$AlohaBackendJobId" | jq -r '.outputs[]')
AlohaLeftVolumeTxtFileId=$(echo "$AlohaBackendOutputJson" | jq -r 'select(.name == "volumes_left.txt").file_id')
AlohaRightVolumeTxtFileId=$(echo "$AlohaBackendOutputJson" | jq -r 'select(.name == "volumes_right.txt").file_id')
if [ -n "$AlohaLeftVolumeTxtFileId" ] && [ -n "$AlohaRightVolumeTxtFileId" ]
then
fwget -1 -f --download --download-dir "${AlohaWorkDir}" "$AlohaLeftVolumeTxtFileId"
fwget -1 -f --download --download-dir "${AlohaWorkDir}" "$AlohaRightVolumeTxtFileId"
AlohaLeftVolumeTxtFile="${AlohaWorkDir}/volumes_left.txt"
AlohaRightVolumeTxtFile="${AlohaWorkDir}/volumes_right.txt"
SubjectLeftVolumeTxtFile="${TmpDir}/${BaselineSessionLabel}-${FollowupSessionLabel}-LeftVolume.txt"
SubjectRightVolumeTxtFile="${TmpDir}/${BaselineSessionLabel}-${FollowupSessionLabel}-RightVolume.txt"
(echo "SessionScanDate,$BaselineSessionScanDate,$FollowupSessionScanDate"
cat "$AlohaLeftVolumeTxtFile") | tee "$SubjectLeftVolumeTxtFile"
(echo "SessionScanDate,$BaselineSessionScanDate,$FollowupSessionScanDate"
cat "$AlohaRightVolumeTxtFile") | tee "$SubjectRightVolumeTxtFile"
sys fwuploadfile -v -t 'AlohaOutput,HippocampalVolume,Left' -p "$SubjectId" "$SubjectLeftVolumeTxtFile"
sys fwuploadfile -v -t 'AlohaOutput,HippocampalVolume,Right' -p "$SubjectId" "$SubjectRightVolumeTxtFile"
else
echo "${CmdName} : Missing VolumeTxtFileId. Skipping" 1>&2
fi
else
echo "${CmdName} : No AlohaBackendJobId found. Skipping" 1>&2
fi
else
echo "${CmdName} : Invalid session aloha arguments for '${FollowupSessionLabel}(${FollowupSessionId})'. Skipping" 1>&2
fi
done
fi
if [ -n "$opt_A" ]
then
LeftVolumeFileIds=( $( sys fwget -1 -r "$SubjectId" | jq -r '.files[] | select( (.tags | any("HippocamplVolume")) and (.tags | any(. == "Left")) ) | .file_id' ) )
RightVolumeFileIds=( $( sys fwget -1 -r "$SubjectId" | jq -r '.files[] | select( (.tags | any("HippocamplVolume")) and (.tags | any(. == "Right")) ) | .file_id' ) )
LeftVolumeFilenames=( $( sys fwget -1 -f --download --download-dir "${TmpDir}" "${LeftVolumeFileIds[@]}" | sed "s/^.*=> //; s/ / -l /g; s/^/-l /") )
RightVolumeFilenames=( $( sys fwget -1 -f --download --download-dir "${TmpDir}" "${RightVolumeFileIds[@]}" | sed "s/^.*=> //; s/ / -r /g; s/^/-r /" ) )
AlohaSubjectAtrophyRatesCsvFile="${TmpDir}/${SubjectLabel}AtrophyRates.csv"
sys alohaCalculateAtrophyRates "${AtrophyArgs[@]}" "${LeftVolumeFilenames[@]}" "${RightVolumeFilenames[@]}" > "$AlohaSubjectAtrophyRatesCsvFile"
sys fwuploadfile -v -t 'AlohaOutput,HippocampalAtrophyRates' -p "$SubjectId" "$AlohaSubjectAtrophyRatesCsvFile"
if [ -n "$opt_t" ]
then
sys alohaCalculateAtrophyRates -t "${AtrophyArgs[@]}" "${LeftVolumeFilenames[@]}" "${RightVolumeFilenames[@]}"
else
cat "$AlohaSubjectAtrophyRatesCsvFile"
fi
fi
exit
#
# Dict to CSV jq code from
# https://stackoverflow.com/questions/32960857/how-to-convert-arbitrary-simple-json-to-csv-using-jq
#
jq -r -f alohaSessionReport.jq /tmp/aloharun.json | jq -f alohaFlattenDict.jq | jq -s -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv'