-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_z_ts_fr.sh
171 lines (134 loc) · 4.99 KB
/
runner_z_ts_fr.sh
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
#!/bin/bash
process_directory()
{
# -----------------------------------------------------------------------------------------------------
# Preprocessing
directory="$1"
disease="$2"
start_time=$(date +%s)
rm -rf ./dirs/segmentorviewer
mkdir -p dirs/segmentorviewer
dirname=$(basename "$directory")
final_directory="./dirs/segmentorviewer/$dirname"
mkdir -p "$final_directory"
cp -r "$directory"/* "$final_directory"
for file in "$final_directory"/*; do
base_name=$(basename "$file")
if [[ $base_name =~ ^I[0-9]+$ ]]; then
mv "$file" "$file.dcm"
else
rm -rf "$file"
fi
done
echo "$final_directory"
TotalSegmentator -i "$final_directory" -o "./dirs/segmentations/$dirname" --roi_subset vertebrae_L4
python3 ./generic/get_z_of_vertebrae.py "$dirname"
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
python3 ./generic/find_white_z.py "$dirname" $elapsed_time $disease
# -----------------------------------------------------------------------------------------------------
# Total Segmentator
start_time=$(date +%s)
TotalSegmentator -i "$directory" -o "./dirs/segmentations/$dirname" -ta tissue_types
python3 ./totalsegmentator/ts.get_area_of_slices.py "$dirname" --use_mean
python3 ./totalsegmentator/ts.get_area_of_slices.py "$dirname" --use_minmax
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
json_file="./ts.area_values.json"
if [ -f "$json_file" ]; then
jq --arg dirname "$dirname" --arg elapsed_time "$elapsed_time" '.[$dirname].elapsed_time = ($elapsed_time | tonumber)' "$json_file" > tmp.json && mv tmp.json "$json_file"
else
echo "JSON file not found!"
fi
# -----------------------------------------------------------------------------------------------------
# Algorithm
python3 "./fatratio/fr.dicom2niftii.py" "$dirname"
start_time=$(date +%s)
python3 "./fatratio/fr.binarise.py" "$dirname" # ./dirs/output/$Dir/fat_thresh.nii.gz
python3 "./fatratio/fr.algorithm.py" "$dirname" --use_mean # ./dirs/output/$Dir/--use_mean_combined_result.nii.gz
python3 "./fatratio/fr.algorithm.py" "$dirname" --use_minmax # ./dirs/output/$Dir/--use_mean_combined_result.nii.gz
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
json_file="./fr.area_values.json"
if [ -f "$json_file" ]; then
jq --arg dirname "$dirname" --arg elapsed_time "$elapsed_time" '.[$dirname].elapsed_time = ($elapsed_time | tonumber)' "$json_file" > tmp.json && mv tmp.json "$json_file"
else
echo "JSON file not found!"
fi
# -----------------------------------------------------------------------------------------------------
# Merge Excel files
python3 ./generic/mergejsons.py "$dirname"
# -----------------------------------------------------------------------------------------------------
# Compare segmentations via Units and all...
# -----------------------------------------------------------------------------------------------------
# Ending this case and moving on to another
echo "Processed $directory" >> ./processed.txt
echo ' =============================='
# pause_for_debug
}
# Function to suppress output
suppress_output() {
exec 3>&1 4>&2 # Save current stdout and stderr
exec 1>/dev/null 2>&1 # Redirect stdout and stderr to /dev/null
}
# Function to enable output
enable_output() {
exec 1>&3 2>&4 # Restore stdout and stderr
exec 3>&- 4>&- # Close temporary file descriptors
}
# Function to add a breakpoint
pause_for_debug() {
echo "$1"
read -n 1 -s -r -p "Press any key to continue..."
echo ""
}
# Activate Python environment
source /tbcrohn_py39/bin/activate
# suppress output
# suppress_output
# Set the superdirectory
superdirectory="/home/medicalai/cd_itb/dataset"
rm -rfv ./z_values.json
rm -rfv ./dcmhead.json
rm -rfv ./fr.area_values.json
rm -rfv ./ts.area_values.json
rm -rfv ./processed.txt
# Process directories in each category
# enable_output
echo 'Working on iTB'
# suppress_output
for dirpath in "$superdirectory/itb"/*/; do
if [ -d "$dirpath" ]; then
echo "$dirpath"
process_directory "$dirpath" "iTB"
# enable_output
echo "Processed $dirpath"
# suppress_output
fi
done
# enable_output
echo 'Working on Crohns:'
# suppress_output
for dirpath in "$superdirectory/crohn"/*/; do
if [ -d "$dirpath" ]; then
echo "$dirpath"
process_directory "$dirpath" "CD"
# enable_output
echo "Processed $dirpath"
# suppress_output
fi
done
# enable_output
echo 'Working on Normal'
# suppress_output
for dirpath in "$superdirectory/normal"/*/; do
if [ -d "$dirpath" ]; then
echo "$dirpath"
process_directory "$dirpath" "N"
# enable_output
echo "Processed $dirpath"
# suppress_output
fi
done
# enable_output
echo 'done with generating z ranges'