utils
tit.pre.qsi.utils ¶
Utility functions for QSI integration.
This module provides path resolution, validation, and helper functions for the QSI Docker-out-of-Docker integration.
SdcPlan
dataclass
¶
How QSIPrep will correct susceptibility distortion for one subject.
mode is "fieldmap" (reverse-PE fmap/*_epi images, TOPUP),
"reverse-pe-dwi" (DWI series with opposite phase encoding, TOPUP) or
"syn" (fieldmap-less SyN, --use-syn-sdc warn). problems names
DWI fieldmaps that cannot be used and why.
resolve_host_project_path ¶
Resolve a container path to the corresponding host path for Docker mounts.
When running inside the SimNIBS container, project directories are mounted at /mnt/$PROJECT_DIR_NAME. However, sibling containers (QSIPrep/QSIRecon) need to mount the original host path, not the container path.
The LOCAL_PROJECT_DIR environment variable contains the host machine's absolute path to the project directory.
Parameters¶
container_path : str Path as seen from inside the SimNIBS container (e.g., /mnt/myproject).
Returns¶
str The corresponding host path for Docker volume mounts.
Raises¶
ValueError If LOCAL_PROJECT_DIR is not set or the path cannot be resolved.
Source code in tit/pre/qsi/utils.py
get_host_project_dir ¶
get_host_project_dir() -> str
Get the host machine's project directory path.
Returns¶
str Absolute path to the project directory on the host machine.
Raises¶
ValueError If LOCAL_PROJECT_DIR is not set.
Source code in tit/pre/qsi/utils.py
check_image_exists ¶
Check if a Docker image exists locally.
Parameters¶
image : str Docker image name (e.g., 'pennlinc/qsiprep'). tag : str Image tag (e.g., '26.0.0').
Returns¶
bool True if the image exists locally.
Source code in tit/pre/qsi/utils.py
pull_image_if_needed ¶
Pull a Docker image if it doesn't exist locally.
Parameters¶
image : str Docker image name. tag : str Image tag. logger : logging.Logger Logger for status messages.
Returns¶
bool True if image is available (either existed or was pulled successfully).
Source code in tit/pre/qsi/utils.py
validate_dood_environment ¶
validate_dood_environment(project_dir: str, *, require_gpu: bool = False, require_x86_64: bool = False) -> tuple[bool, str | None]
Validate Docker-outside-of-Docker prerequisites before QSI runs.
require_x86_64 rejects an arm64 Docker host (Apple Silicon), where QSIPrep
cannot run; the check reads docker info's Architecture line.
Source code in tit/pre/qsi/utils.py
nifti_stem ¶
Return path's name without its .nii or .nii.gz extension.
Source code in tit/pre/qsi/utils.py
read_nifti_dims ¶
Return the 8-element dim field of a NIfTI header, or None.
read_nifti_zooms ¶
Return the three spatial voxel sizes (mm) of a NIfTI header, or None.
Source code in tit/pre/qsi/utils.py
validate_bids_dwi ¶
Validate that usable DWI data exists for a subject in BIDS format.
Checks that every *_dwi.nii* under the subject's dwi/ folder has a
gradient table that matches it: same basename, parseable, one b-value and
one direction per volume, and enough distinct directions to fit a tensor.
Parameters¶
project_dir : str Path to the BIDS project root. subject_id : str Subject identifier (without 'sub-' prefix). logger : logging.Logger Logger for status messages.
Returns¶
tuple[bool, str | None] (is_valid, error_message). If valid, error_message is None.
See Also¶
ensure_total_readout_time : The sidecar metadata QSIPrep needs alongside this.
Source code in tit/pre/qsi/utils.py
ensure_total_readout_time ¶
ensure_total_readout_time(project_dir: str, subject_id: str, *, logger: Logger, repair: bool = True) -> tuple[bool, str | None]
Make sure every DWI sidecar carries the metadata QSIPrep dereferences.
QSIPrep formats TotalReadoutTime into an FSL acqp line for every
run, including ones with no fieldmap and no TOPUP, and its sidecar reader
has no fallback when the key is absent -- the run dies with
TypeError: must be real number, not NoneType only after the anatomical
workflow has finished, an hour in.
A missing value is derived from EstimatedTotalReadoutTime or from
EffectiveEchoSpacing and the phase-encode matrix size when the sidecar
carries them. Failing that, and only when the subject has no fieldmap and
a single phase-encoding direction, a conventional placeholder is written:
in that configuration no susceptibility correction is estimated, so the
readout time is a common scale factor that cancels. Anything else is
reported rather than guessed, because a wrong readout time does bias
distortion correction once a fieldmap is present.
Parameters¶
project_dir : str Path to the BIDS project root. subject_id : str Subject identifier (without 'sub-' prefix). logger : logging.Logger Logger for status messages. repair : bool, optional Write the derived value back into the sidecar. When False a missing value is reported as an error instead. Default: True.
Returns¶
tuple[bool, str | None] (is_ok, error_message). If ok, error_message is None.
Source code in tit/pre/qsi/utils.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
plan_distortion_correction ¶
plan_distortion_correction(project_dir: str, subject_id: str, *, logger: Logger, repair: bool = True) -> SdcPlan
Choose QSIPrep's susceptibility distortion correction for subject_id.
An fmap/*_epi image is treated as a DWI fieldmap when its IntendedFor
already names a DWI, or, with no IntendedFor, when its acq label says
dwi/dti or it has no acq label and the DWI's matrix. It is usable when its
sidecar has a PhaseEncodingDirection opposite to a DWI's on the same axis and
a TotalReadoutTime (derived like the DWI's when absent). With repair, a
usable fieldmap missing IntendedFor or TotalReadoutTime gets them written
into its own sidecar; nothing else is touched. Unusable DWI fieldmaps are
reported in problems, never guessed at.
Source code in tit/pre/qsi/utils.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
choose_unringing_method ¶
("rpg" | "mrdegibbs", reason) from the DWI sidecars' PartialFourier.
mrdegibbs assumes full k-space; TORTOISE's rpg handles partial-Fourier data.
Source code in tit/pre/qsi/utils.py
native_dwi_resolution ¶
The DWI's smallest voxel dimension rounded half-up to 0.1 mm, or None.
QSIPrep resamples to an isotropic grid; the finest native axis keeps every acquired sample without inventing resolution.
Source code in tit/pre/qsi/utils.py
validate_qsiprep_output ¶
Validate that QSIPrep output exists for a subject.
Parameters¶
project_dir : str Path to the project root. subject_id : str Subject identifier.
Returns¶
tuple[bool, str | None] (is_valid, error_message). If valid, error_message is None.
Source code in tit/pre/qsi/utils.py
format_memory_limit ¶
get_container_resource_limits ¶
Return (cpu_limit, mem_limit_bytes) for the current container.
- cpu_limit: integer number of CPUs available via cgroups/cpuset if limited, otherwise None.
- mem_limit_bytes: memory limit in bytes via cgroups if limited, otherwise None.
Source code in tit/pre/qsi/utils.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 | |
get_inherited_dood_resources ¶
Determine DooD resource defaults that match the current container.
Returns (cpus, memory_gb) with conservative rounding.