# ScolioSim - install TotalSegmentator on this computer, to segment the vertebrae of a CT for ScolioSim. # # Run it in a PowerShell window (it needs no administrator rights): # irm https://scoliosim.com/segment/install.ps1 | iex # or from the Command Prompt (cmd): # powershell -ExecutionPolicy Bypass -c "irm https://scoliosim.com/segment/install.ps1 | iex" # # What it does, all inside %LOCALAPPDATA%\ScolioSim\segment (delete that folder to remove it all): # 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: the build for NVIDIA cards (CUDA) if there is one, else the one for the processor. # 3. scoliosim_segment.py, which picks the right series of a CT and runs TotalSegmentator on it. # 4. A file on the desktop to drop a CT folder or ZIP on, and the command scoliosim-segment. # Nothing of a patient is sent anywhere: the CT is read and segmented on this computer. $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $Source = 'https://scoliosim.com/segment' $Dir = Join-Path $env:LOCALAPPDATA 'ScolioSim\segment' $Venv = Join-Path $Dir 'venv' $Bin = Join-Path $Dir 'bin' function Say($text) { Write-Host $text -ForegroundColor Cyan } Say 'ScolioSim: installing TotalSegmentator on this computer.' if (-not [Environment]::Is64BitOperatingSystem) { throw 'TotalSegmentator needs 64-bit Windows.' } $drive = (Get-Item $env:LOCALAPPDATA).PSDrive $freeGB = [math]::Round((Get-PSDrive $drive.Name).Free / 1GB) if ($freeGB -lt 15) { Write-Warning "Only $freeGB GB free on drive $($drive.Name): it needs about 15 GB (programs and model weights)." } $ramGB = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB) if ($ramGB -lt 16) { Write-Warning "This computer has $ramGB GB of memory: a long CT may need more than 12 GB. 16 GB or more is advised." } # 1. uv $uv = (Get-Command uv -ErrorAction SilentlyContinue).Source if (-not $uv) { $uv = Join-Path $env:USERPROFILE '.local\bin\uv.exe' } if (-not (Test-Path $uv)) { Say 'Installing uv (Python manager, from astral.sh)...' # Saved to a file and run in its own PowerShell: it does not depend on how this one was started $uvSetup = Join-Path $env:TEMP 'scoliosim-uv-install.ps1' Invoke-WebRequest https://astral.sh/uv/install.ps1 -OutFile $uvSetup -UseBasicParsing & powershell -NoProfile -ExecutionPolicy Bypass -File $uvSetup Remove-Item $uvSetup -ErrorAction SilentlyContinue } if (-not (Test-Path $uv)) { throw "uv did not install where expected ($uv)." } # 2. Which PyTorch: CUDA only with an NVIDIA card; any other card (Intel, AMD) gets the processor build $nvidia = @(Get-CimInstance Win32_VideoController | Where-Object { $_.Name -match 'NVIDIA' }) if ($nvidia.Count -gt 0) { $backend = 'auto' Say "NVIDIA card found ($($nvidia[0].Name)): installing the CUDA build of PyTorch." Say ' Keep its driver up to date (nvidia.com/drivers): an old driver makes PyTorch fall back to the processor.' } else { $backend = 'cpu' Write-Warning 'No NVIDIA card found: TotalSegmentator will run on the processor. It works, but a whole spine takes a long time.' } New-Item -ItemType Directory -Force -Path $Dir, $Bin | Out-Null Say 'Creating its own Python 3.12...' & $uv venv --python 3.12 --allow-existing $Venv if ($LASTEXITCODE -ne 0) { throw 'Could not create the Python environment.' } $py = Join-Path $Venv 'Scripts\python.exe' Say 'Installing TotalSegmentator and PyTorch (a few GB; it takes a while)...' $env:UV_TORCH_BACKEND = $backend & $uv pip install --python $py --upgrade TotalSegmentator if ($LASTEXITCODE -ne 0) { throw 'Could not install TotalSegmentator.' } Remove-Item Env:UV_TORCH_BACKEND # 3. The series picker Invoke-WebRequest "$Source/scoliosim_segment.py" -OutFile (Join-Path $Dir 'scoliosim_segment.py') -UseBasicParsing # 4. The command, and the file on the desktop to drop a CT on $run = @" @echo off set "TOTALSEG_HOME_DIR=%LOCALAPPDATA%\ScolioSim\segment\totalseg" "%LOCALAPPDATA%\ScolioSim\segment\venv\Scripts\python.exe" -I "%LOCALAPPDATA%\ScolioSim\segment\scoliosim_segment.py" %* "@ Set-Content -Path (Join-Path $Bin 'scoliosim-segment.cmd') -Value $run -Encoding ASCII $drop = @" @echo off title ScolioSim - segment a CT if "%~1"=="" ( echo Drop the folder or ZIP of a CT on this file, from the CD or from where you saved it. echo It can be the whole study: the series for the spine is picked by itself. echo. pause exit /b ) call "%LOCALAPPDATA%\ScolioSim\segment\bin\scoliosim-segment.cmd" %* echo. pause "@ $desktop = [Environment]::GetFolderPath('Desktop') Set-Content -Path (Join-Path $desktop 'ScolioSim - segment a CT.cmd') -Value $drop -Encoding ASCII $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (($userPath -split ';') -notcontains $Bin) { [Environment]::SetEnvironmentVariable('Path', ($(if ($userPath) { "$userPath;" } else { '' }) + $Bin), 'User') } # Check: what PyTorch sees & $py -c "import torch; c = torch.cuda.is_available(); print('PyTorch', torch.__version__, '- graphics card in use:', torch.cuda.get_device_name(0) if c else 'none, the processor will do the work')" Write-Host '' Say 'Done. To segment a CT:' Write-Host " - drop its folder or ZIP on 'ScolioSim - segment a CT' on the desktop, or" Write-Host ' - in a new terminal: scoliosim-segment (add --list to see its series first)' Write-Host 'The first time it also downloads the model weights from TotalSegmentator''s authors.' Write-Host "To remove it all: delete $Dir and the file on the desktop."