This repository contains Python scripts for generating reports from XNAT instances. The scripts can be run directly with Python or using the provided Docker setup.
- Docker installed on your system
- Input files placed in your current working directory
- Clone this repository
- Navigate to the repository directory
- Run scripts using the provided wrapper:
./run.sh <script_name> [script_arguments]Run users per project report:
./run.sh users_per_project --xnat_url https://xnat.health-ri.nlRun users per project report for specific project:
./run.sh users_per_project --xnat_url https://xnat.health-ri.nl --project sandboxRun disk usage report:
./run.sh disk_usages --xnat_url https://xnat.health-ri.nl --report_path ./input_disk_usage_report.txt --study_overview ./studyoverview.csvThe run.sh script will automatically:
- Build the Docker image
- Mount your current directory into the container
- Execute the specified script with your arguments
- Output files will be created in your current directory
You can also run the scripts directly with Python if you have the dependencies installed.
Run in the terminal with:
python src/xnat_maintenance_monitoring_scripts/users_per_project.py --xnat_url https://xnat.health-ri.nlto query the whole XNAT. Run the following to query only 'sandbox'
python src/xnat_maintenance_monitoring_scripts/users_per_project.py --xnat_url https://xnat.health-ri.nl --project sandboxYou need to have 'Owner' or 'Site admin' priviliges to run this script on a project. This script returns a CSV file "./{today}_XNAT_users_per_project.csv", with the columns:
- project
- user_login_name
- user_first_name
- user_last_name
- user_email
- access_level
- group
- pi_firstname
- pi_lastname
- pi_title
- pi_email
- pi_institution
Run in the terminal with:
python src/xnat_maintenance_monitoring_scripts/disk_usages.py --xnat_url https://xnat.health-ri.nl --report_path ./input_disk_usage_report.txt --study_overview ./studyoverview.csvto query the whole XNAT.
report_path is the du output from the server.
study_overview is a CSV file with a column 'main_study' and 'substudy' to link main studies to their substudies.
This script returns a CSV file "./{today}_XNAT_disk_usage.csv", with the columns:
- project_id
- main_study
- project_path
- data_usage (MB)
- xnat_project_name
- xnat_project_id
- pi_firstname
- pi_lastname
- pi_title
- pi_email
- pi_institution
Removes XNAT prearchive uploads older than a configurable retention period, and verifies that each deleted session is actually gone from disk. Uploads with no associated project ("unassigned") are easy to forget about and can otherwise take up storage indefinitely.
Run in the terminal with:
python src/xnat_maintenance_monitoring_scripts/prearchive_cleanup.py \
--xnat_url https://xnat.health-ri.nl \
--project unassigned \
--retention_days 90 \
--project_root /data/xnat/prearchive--projectaccepts a specific project ID,unassigned(default) for uploads not associated with any project, orallto process every project includingunassigned.--retention_days(default 90) is how old (in days, based on upload timestamp) a prearchive session must be before it's removed.--project_rootmust be a filesystem path to the XNAT prearchive directory, reachable from wherever the script runs, so it can confirm the session folder was actually removed from disk after the API delete.
run.sh alone isn't enough for this script, because it only mounts the current directory as
/data — --project_root needs the real prearchive directory. Run it via docker run directly
instead, mounting the prearchive directory read-only (the script only reads it to verify deletion;
the delete itself happens server-side via the XNAT API):
docker build -t xnat-scripts .
docker run --rm -it \
-v /path/to/xnat/prearchive:/prearchive:ro \
xnat-scripts prearchive_cleanup \
--xnat_url https://xnat.health-ri.nl \
--project unassigned \
--retention_days 90 \
--project_root /prearchiveThe script prompts for your XNAT username and password, which requires -it. To run it
non-interactively (e.g. from cron), set XNAT_USERNAME and XNAT_PASSWORD instead and pass them
into the container with -e XNAT_USERNAME -e XNAT_PASSWORD (then -it can be dropped).
Removes files older than a configurable retention period from a given local directory (and any now-empty parent directories left behind), independent of XNAT.
python src/xnat_maintenance_monitoring_scripts/folder_cleanup.py --dir_path /path/to/folder --retention_days 90To add a new Python script to this repository:
-
Create your Python script following these guidelines:
- Use argparse for command-line arguments
- Include proper error handling
- Write output files to the current working directory
- Add dependencies to
requirements.txtif needed
-
Place your
.pyfile in thescripts/directory -
The script will automatically be available in the Docker image after rebuilding
-
Update this README.md with documentation for your script
- Must be a standalone Python file (
.pyextension) - Should handle authentication through user prompts (getpass)
- Input and output files should use the current working directory
- Follow the existing pattern of the current scripts