#!/bin/bash
# 2026-11-02: anchors trimmed to the control-point span (user: open tails past the outer CPs are not annotation;
# network_abs_windings2 used them, 18.7% of training anchors). Two R1-recipe lineages, seed 1, frozen code:
#   trim     = R1 inputs, trimmed anchors (s1_trim)            -> effect of dropping the tails
#   d69trim  = R1 inputs + extended dj-69, trimmed (s1_d69trim) -> effect of dj-69 on top
# Every stage also rescored on the trimmed records (scores_trim.json), as are R0/R1 seeds 1-2 for comparison.
#   chain_trim.sh FROZEN_DIR     stop file: work/chain_s1.stop
set -u -o pipefail
D=/media/djosey/nvme4/spiral_0332; F=${1:?frozen dir}; PY=/home/djosey/Documents/villa/spiral-fitting/.venv/bin/python
P=$(cat $D/work/chain_d69.pid 2>/dev/null); while kill -0 ${P:-0} 2>/dev/null; do sleep 30; done
rm -f $D/work/chain_s1.stop
echo $$ > $D/work/chain_trim.pid
source <(sed -e '/chain_s1.pid/d' -e '/chain_dt done/d' -e '/^for ARM_TAG/,$d' $F/work/s1/chain_s1.sh) $F
rescore() { R=$(rundir $1); [ -n "$R" ] && [ -f $R/checkpoint_fitted.ckpt ] || return 0
  [ -f $R/scores_trim.json ] || CUDA_VISIBLE_DEVICES= $PY $F/work/s1/s1_score.py $R --out $R/scores_trim.json \
    --train-records $D/work/s1_trim/anchors_train_records.json --held-records $D/work/s1_trim/anchors_held_records.json > $D/work/s1/scoretrim_$1.log 2>&1
  echo "$(date) rescored $1 on trimmed records"; }
for T in Vb_hub1030h_25k Vb_hub1030h_sheet_25k Vb_hub1030h_s2_25k Vb_hub1030h_sheet_s2_25k; do rescore $T; done
for A in trim d69trim; do
  stop && break
  stage Va_hub1030h_${A}_10k $A 10000 || continue
  stop && break
  stage Vb_hub1030h_${A}_25k $A 25000 Va_hub1030h_${A}_10k || continue
  rescore Vb_hub1030h_${A}_25k
done
echo "$(date) chain_trim done"
