#!/bin/sh # ScolioSim - install TotalSegmentator on this computer (macOS or Linux), to segment the vertebrae of a CT for ScolioSim. # # Run it in a terminal (it needs no administrator rights): # curl -fsSL https://scoliosim.com/segment/install.sh | sh # # What it does, all inside ~/.scoliosim/segment (delete that folder and ~/.local/bin/scoliosim-segment to remove it): # 1. uv, Astral's Python manager (from astral.sh), if it is not here yet; it brings its own Python 3.12. # 2. TotalSegmentator and PyTorch: on Linux the CUDA build if there is an NVIDIA card, else the processor one; # on a Mac the standard build, which uses the graphics of Apple silicon (M1 and later). # 3. scoliosim_segment.py, which picks the right series of a CT and runs TotalSegmentator on it. # 4. The command scoliosim-segment. # Nothing of a patient is sent anywhere: the CT is read and segmented on this computer. set -eu SOURCE=https://scoliosim.com/segment DIR="$HOME/.scoliosim/segment" BIN="$HOME/.local/bin" say() { printf '\033[36m%s\033[0m\n' "$1"; } warn() { printf '\033[33mWarning: %s\033[0m\n' "$1"; } say "ScolioSim: installing TotalSegmentator on this computer." case "$(uname -s)-$(uname -m)" in Darwin-arm64) BACKEND="" ; say "Mac with Apple silicon: TotalSegmentator will use its graphics." ;; Darwin-*) BACKEND="" ; warn "Mac with an Intel processor: it runs on the processor only, slowly, and is not tested." ;; Linux-x86_64|Linux-aarch64) if command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi >/dev/null 2>&1; then BACKEND=auto; say "NVIDIA card found: installing the CUDA build of PyTorch." else BACKEND=cpu; warn "No NVIDIA card found: TotalSegmentator will run on the processor. It works, but a whole spine takes a long time." fi ;; *) echo "Not supported here: $(uname -s) $(uname -m)."; exit 1 ;; esac FREE_GB=$(df -Pk "$HOME" | awk 'NR==2 {print int($4/1048576)}') [ "$FREE_GB" -lt 15 ] && warn "Only $FREE_GB GB free: it needs about 15 GB (programs and model weights)." # 1. uv UV=$(command -v uv || true) [ -z "$UV" ] && [ -x "$BIN/uv" ] && UV="$BIN/uv" if [ -z "$UV" ]; then say "Installing uv (Python manager, from astral.sh)..." curl -LsSf https://astral.sh/uv/install.sh | sh UV="$BIN/uv" fi mkdir -p "$DIR" "$BIN" say "Creating its own Python 3.12..." "$UV" venv --python 3.12 --allow-existing "$DIR/venv" # 2. TotalSegmentator and PyTorch say "Installing TotalSegmentator and PyTorch (a few GB; it takes a while)..." if [ -n "$BACKEND" ]; then UV_TORCH_BACKEND=$BACKEND "$UV" pip install --python "$DIR/venv/bin/python" --upgrade TotalSegmentator else "$UV" pip install --python "$DIR/venv/bin/python" --upgrade TotalSegmentator fi # 3. The series picker curl -fsSL "$SOURCE/scoliosim_segment.py" -o "$DIR/scoliosim_segment.py" # 4. The command cat > "$BIN/scoliosim-segment" <<'EOF' #!/bin/sh export TOTALSEG_HOME_DIR="$HOME/.scoliosim/segment/totalseg" exec "$HOME/.scoliosim/segment/venv/bin/python" -I "$HOME/.scoliosim/segment/scoliosim_segment.py" "$@" EOF chmod +x "$BIN/scoliosim-segment" "$DIR/venv/bin/python" -c "import torch; m = getattr(torch.backends, 'mps', None); c = torch.cuda.is_available(); print('PyTorch', torch.__version__, '- graphics in use:', torch.cuda.get_device_name(0) if c else ('Apple silicon' if m and m.is_available() else 'none, the processor will do the work'))" echo say "Done. To segment a CT, in a new terminal:" echo " scoliosim-segment (type it, then drag the CT folder or ZIP onto the terminal; add --list to see its series first)" case ":$PATH:" in *":$BIN:"*) ;; *) echo " If the command is not found, open a new terminal, or run it as $BIN/scoliosim-segment" ;; esac echo "The first time it also downloads the model weights from TotalSegmentator's authors." echo "To remove it all: rm -rf $DIR $BIN/scoliosim-segment"