Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·300 lines (265 loc) · 11.8 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·300 lines (265 loc) · 11.8 KB
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
#!/usr/bin/env bash
#
# SIH26038 - Explainable AI for DR Screening
# Demo launcher.
#
# ./start.sh launch the screening GUI (default - use this for judges)
# ./start.sh demo headless single-image run, prints the full decision trace
# ./start.sh numbers print the frozen operating point and the headline results
# ./start.sh tests run the full test suite
# ./start.sh check preflight only: verify MATLAB, model, calibration, data
#
# The Arch/Wayland environment variables below are documented in
# design doc section 14.2. Without them MATLAB either segfaults on the
# bundled libstdc++ or renders a blank grey window under Wayland.
set -uo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"
# ---------------------------------------------------------------- appearance
if [[ -t 1 ]]; then
BOLD=$'\033[1m'; DIM=$'\033[2m'; RED=$'\033[31m'
GREEN=$'\033[32m'; YELLOW=$'\033[33m'; BLUE=$'\033[34m'; RESET=$'\033[0m'
else
BOLD=""; DIM=""; RED=""; GREEN=""; YELLOW=""; BLUE=""; RESET=""
fi
say() { printf '%s\n' "$*"; }
ok() { printf '%s ok %s %s\n' "$GREEN" "$RESET" "$*"; }
bad() { printf '%s fail %s %s\n' "$RED" "$RESET" "$*"; }
warn() { printf '%s warn %s %s\n' "$YELLOW" "$RESET" "$*"; }
head2(){ printf '\n%s%s%s\n' "$BOLD" "$*" "$RESET"; }
# ------------------------------------------------------------------ env (14.2)
SIH_PRELOAD="/usr/lib/libstdc++.so.6:/usr/lib/libfreetype.so.6"
if [[ -n "${LD_PRELOAD:-}" ]]; then
export LD_PRELOAD="${LD_PRELOAD}:${SIH_PRELOAD}"
else
export LD_PRELOAD="$SIH_PRELOAD"
fi
export _JAVA_AWT_WM_NONREPARENTING=1
MATLAB_BIN="${MATLAB_BIN:-$(command -v matlab || true)}"
# --------------------------------------------------------- process control
# MATLAB installs its own SIGINT handler. Under "-nodesktop -r" a Ctrl+C is
# taken to mean "interrupt the current MATLAB statement", so it breaks out of
# uiwait, drops to the >> prompt, and leaves both the MATLAB process and the
# app window alive while this script is still blocked waiting on it. The
# terminal never comes back and the demo cannot be stopped.
#
# Running MATLAB as a background job and terminating it from a trap is what
# makes Ctrl+C actually stop the demo. Descendants are signalled too: MATLAB
# spawns helper and renderer processes that outlive a bare kill of the parent.
MATLAB_PID=""
kill_tree() {
local pid=$1 signal=$2 child
for child in $(pgrep -P "$pid" 2>/dev/null); do
kill_tree "$child" "$signal"
done
kill -"$signal" "$pid" 2>/dev/null || true
}
stop_matlab() {
[[ -z "$MATLAB_PID" ]] && return 0
kill -0 "$MATLAB_PID" 2>/dev/null || { MATLAB_PID=""; return 0; }
kill_tree "$MATLAB_PID" TERM
local waited=0
while kill -0 "$MATLAB_PID" 2>/dev/null && (( waited < 40 )); do
sleep 0.25
waited=$(( waited + 1 ))
done
if kill -0 "$MATLAB_PID" 2>/dev/null; then
kill_tree "$MATLAB_PID" KILL
fi
MATLAB_PID=""
}
on_interrupt() {
printf '\n'
warn "interrupted - stopping MATLAB"
stop_matlab
exit 130
}
trap on_interrupt INT TERM HUP
trap stop_matlab EXIT
run_matlab() {
# A background job in a non-interactive shell has no separate process
# group, so it stays in the terminal's foreground group and may read the
# tty without being stopped by SIGTTIN. The GUI needs that: MATLAB must
# stay in its interactive configuration or every blocking dialog,
# uigetfile included, refuses to open.
# -r only tests the device node's permission bits. A process with no
# controlling terminal passes that test and then fails on open with ENXIO,
# which is what happens under cron, CI, and other detached parents. Test
# that the device can actually be opened.
if { : < /dev/tty; } 2>/dev/null; then
"$MATLAB_BIN" "$@" < /dev/tty &
else
"$MATLAB_BIN" "$@" < /dev/null &
fi
MATLAB_PID=$!
local status=0
wait "$MATLAB_PID" || status=$?
MATLAB_PID=""
return $status
}
# ------------------------------------------------------------------ preflight
preflight() {
local failed=0
head2 "Environment"
if [[ -z "$MATLAB_BIN" ]]; then
bad "matlab not found on PATH (set MATLAB_BIN=/path/to/matlab)"
failed=1
else
ok "matlab: $MATLAB_BIN"
fi
if [[ "${XDG_SESSION_TYPE:-}" == "wayland" ]]; then
ok "Wayland detected - non-reparenting workaround active (14.2)"
fi
head2 "Frozen operating point (config/default.json)"
if [[ ! -f config/default.json ]]; then
bad "config/default.json is missing"
return 1
fi
# Read the frozen paths straight from config so the demo can never run a
# different model than the reported numbers describe.
local model calib
model=$(python3 -c "import json;print(json.load(open('config/default.json'))['operating_point']['model'])" 2>/dev/null)
calib=$(python3 -c "import json;print(json.load(open('config/default.json'))['operating_point']['calibration'])" 2>/dev/null)
if [[ -z "$model" ]]; then
bad "could not read operating_point.model from config/default.json"
failed=1
elif [[ -f "$model" ]]; then
ok "model checkpoint: $model"
else
bad "model checkpoint missing: $model"
failed=1
fi
if [[ -z "$calib" ]]; then
bad "could not read operating_point.calibration"
failed=1
elif [[ -f "$calib" || -f "$calib/temperature_fit.mat" ]]; then
ok "calibration: $calib"
else
bad "calibration missing: $calib"
failed=1
fi
# Which evidence channel the demo will actually run. The learned channel
# escalated 512 of 550 frames in 11.6 A9 and was kept off for that
# reason; A10 then showed the agreement check, not the channel, was
# causing it, and the learned channel was adopted on 31 August 2026. A
# presenter asked "which one is live?" should not have to guess, and a
# judge who asks is asking a good question.
local learned lesion_ckpt heads
learned=$(python3 -c "import json;print(json.load(open('config/default.json'))['pipeline'].get('learned_lesion_evidence', False))" 2>/dev/null)
if [[ "$learned" == "True" ]]; then
lesion_ckpt=$(python3 -c "import json;print(json.load(open('config/default.json'))['lesion_segmentation']['checkpoint'])" 2>/dev/null)
heads=$(python3 -c "import json;d=json.load(open('config/default.json'))['lesion_segmentation'];print('+'.join(d.get('evidence_heads') or d['lesion_types']))" 2>/dev/null)
if [[ -n "$lesion_ckpt" && -f "$lesion_ckpt" ]]; then
ok "lesion evidence: learned segmentation, heads $heads"
ok "lesion checkpoint: $lesion_ckpt"
else
bad "learned_lesion_evidence is on but its checkpoint is missing: $lesion_ckpt"
failed=1
fi
else
ok "lesion evidence: classical candidate detector (learned channel off)"
fi
head2 "Demo data"
local n
n=$(ls data/raw/aptos2019/train_images/*.png 2>/dev/null | wc -l)
if [[ "$n" -gt 0 ]]; then
ok "APTOS images available: $n"
else
bad "no APTOS images under data/raw/aptos2019/train_images/"
failed=1
fi
# The sealed set must never be touched by a demo. Report only that it is
# present and sealed; never read into it (design doc 10.4).
if [[ -d data/sealed ]]; then
ok "data/sealed/ present and NOT accessed by any demo path (10.4)"
fi
return $failed
}
# ------------------------------------------------------------------- actions
launch_gui() {
preflight || { bad "preflight failed - fix the above before demoing"; exit 1; }
head2 "Launching the screening GUI"
say "${DIM}Close the app window, or press Ctrl+C here, to return to this shell.${RESET}"
say ""
# The -r argument must be a single line. A multi-line string is silently
# dropped ("No MATLAB command specified for -r"), MATLAB then sits at the
# prompt and exits when stdin closes, so no window ever appears.
#
# This must NOT be -batch. -batch sets batchStartupOptionUsed, which puts
# MATLAB in a non-interactive configuration where every blocking dialog is
# refused, so "Select fundus image" dies with "Creating dialog boxes that
# block execution is not supported". The GUI needs the interactive
# configuration; Ctrl+C is handled by the trap above, not by the mode.
run_matlab -nodesktop -nosplash -r "addpath(genpath('src')); addpath('app'); a = ScreeningApp; uiwait(a.UIFigure); exit(0);"
}
run_demo() {
preflight || { bad "preflight failed"; exit 1; }
head2 "Single-image screening demo"
say "${DIM}Runs quality gate, grading, Grad-CAM, lesion evidence, decision policy,${RESET}"
say "${DIM}then exports the annotated report.${RESET}"
say ""
run_matlab -batch "addpath(genpath('src')); addpath('eval'); appSmoke()"
}
show_numbers() {
head2 "SIH26038 - headline results"
python3 - <<'PY'
import json
c = json.load(open('config/default.json'))
op = c.get('operating_point', {})
lesion = c.get('lesion_segmentation', {})
if c.get('pipeline', {}).get('learned_lesion_evidence'):
heads = lesion.get('evidence_heads') or lesion.get('lesion_types', [])
live = 'learned segmentation, heads ' + '+'.join(heads)
else:
live = 'classical candidate detector'
print(f"""
Frozen operating point {op.get('referable_threshold')} (frozen {op.get('frozen_on')})
Temperature (calibration) {op.get('temperature')}
Checkpoint {op.get('model')}
REFERABLE DR (ICDR >= 2), validation split, n = 550
Sensitivity {op.get('validation_sensitivity')} target > 0.90
Specificity {op.get('validation_specificity')} target > 0.85
Both targets are met at the frozen threshold on internal validation.
The sealed external set (Messidor-2) has NOT been opened.
LESION EVIDENCE CHANNEL
Live in this demo {live}
Learned channel, all four heads sensitivity 1.0000 / specificity 0.0000 (A6)
Learned channel, exudates only sensitivity 0.8072 / specificity 0.8257 (A8)
The learned channel is the better localiser (4.87x pointing game vs 1.32x
for Grad-CAM) and was still unusable as ICDR evidence until its thresholds
were re-selected on the calibration split. Restricting it to its one
trustworthy head fixed the channel and barely moved the pipeline, which
looked like the channel's fault and was not: the agreement check was
escalating it for reaching ICDR Level 2 at all. Repairing that (A10,
adopted 31 August 2026) took autonomous coverage from 0.0691 to 0.3273
with zero referable patients sent home. Both numbers are reported.
""")
PY
say "${DIM}Full metric set, confidence intervals and the ablation table are in${RESET}"
say "${DIM}docs/SIH26038_design.html sections 11.2, 11.3 and 11.6.${RESET}"
}
run_tests() {
head2 "Full test suite"
say "${DIM}This takes about 8 minutes.${RESET}"
say ""
run_matlab -batch "assertSuccess(runtests('tests','IncludeSubfolders',true))"
}
usage() {
cat <<EOF
${BOLD}SIH26038 - Explainable AI for DR Screening${RESET}
${BOLD}./start.sh${RESET} launch the screening GUI ${DIM}(use this for judges)${RESET}
${BOLD}./start.sh demo${RESET} headless single-image run with full decision trace
${BOLD}./start.sh numbers${RESET} print the frozen operating point and headline results
${BOLD}./start.sh tests${RESET} run the full test suite
${BOLD}./start.sh check${RESET} preflight only
EOF
}
case "${1:-gui}" in
gui|"") launch_gui ;;
demo) run_demo ;;
numbers|nums) show_numbers ;;
tests|test) run_tests ;;
check) preflight && ok "all preflight checks passed" ;;
-h|--help|help) usage ;;
*) bad "unknown option: $1"; usage; exit 1 ;;
esac