#!/bin/zsh
# 依頼 363: 当てはめの本 (割り振り 1) を書き出し, 値の組を描いて測り, 採った組 (v2) で 比べる 3 本を描いて比べる
cd ~/r2r2r-scripts
source /opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh
V=${1:-v2}
if [ "$2" = "--tune" ]; then
  conda activate lerobot; python so101/blender/export_363.py --plan 1; conda deactivate
  blender -b --factory-startup --python so101/blender/render_363.py -- --src check_363/tune01 --out check_363/tune01 --var base --samples 32 --labels 2>&1 | grep -E "Error|Traceback"
  for v in b1 b2 b3 c1 c2 c3 v1 v2; do
    blender -b --factory-startup --python so101/blender/render_363.py -- --src check_363/tune01 --out check_363/tune01 --var $v --samples 32 2>&1 | grep -E "Error|Traceback"
  done
  conda activate lerobot
  for v in base b1 b2 b3 c1 c2 c3 v1 v2; do
    python so101/blender/post_363.py --fit-gain tune01 --var $v; python so101/blender/post_363.py --finish tune01 --var $v
  done
  python so101/blender/post_363.py --measure tune01 --vars base,b1,b2,b3 --tag t_b
  python so101/blender/post_363.py --measure tune01 --vars base,b2,c1,c2,c3 --tag t_c
  python so101/blender/post_363.py --measure tune01 --vars base,c2,v1,v2 --tag t_v
  conda deactivate
fi
if [ "$2" != "--ablation" ]; then
for ep in ep00 ep05 ep03; do
  blender -b --factory-startup --python so101/blender/render_363.py -- --src check_361/$ep --out check_363/$ep --var $V --samples 32 --labels 2>&1 | grep -E "^frame .* (0|100|200) |Error|Traceback"
done
conda activate lerobot
python so101/blender/post_363.py --finish ep00,ep05,ep03 --var $V
python so101/blender/post_363.py --measure ep00,ep05,ep03 --vars 361,$V --tag eval
python so101/blender/post_363.py --metrics ep00,ep05,ep03 --var $V
python so101/blender/post_363.py --box-edges ep00,ep05,ep03,tune01
python so101/blender/figs_363.py --var $V
~/venvs/feat334/bin/python so101/blender/features_363.py --eps ep00,ep05,ep03 --vars $V
python so101/blender/summary_363.py --var $V
conda deactivate
fi
if [ "$2" = "--ablation" ]; then  # 切り分け (3 本の 16 コマずつ): 箱の形だけ (base)・形を 95.7×89.4 に戻す (base95)・箱の材質まで (v2box)
  F=0,10,20,40,60,80,100,120,140,150,170,190,205,215,225,240
  blender -b --factory-startup --python so101/blender/render_363.py -- --src check_363/tune01 --out check_363/tune01 --var v2box --samples 32 2>&1 | grep -E "Error|Traceback"
  for ep in ep00 ep05 ep03; do for v in base base95 v2box; do
    blender -b --factory-startup --python so101/blender/render_363.py -- --src check_361/$ep --out check_363/$ep --var $v --samples 32 --frames $F 2>&1 | grep -E "Error|Traceback"
  done; done
  conda activate lerobot
  python so101/blender/post_363.py --fit-gain tune01 --var v2box
  cp check_363/gain_base.json check_363/gain_base95.json  # base95 は base の倍率をそのまま使う
  python so101/blender/post_363.py --finish ep00,ep05,ep03 --var base; python so101/blender/post_363.py --finish ep00,ep05,ep03 --var base95
  python so101/blender/post_363.py --finish ep00,ep05,ep03 --var v2box
  ~/venvs/feat334/bin/python so101/blender/features_363.py --eps ep00,ep05,ep03 --vars base95,base,v2box,$V --out features_363_ablation.csv
fi
echo done363
