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). problems names
DWI fieldmaps that cannot be used and why.
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.1.1').
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
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 | |
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
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 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 | |
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
qsiprep_has_mni_transform ¶
Whether the subject's QSIPrep output holds the anat-to-MNI transform.
QSIPrep writes anat/*_to-MNI152NLin2009cAsym_*xfm.h5 only when its MNI
normalization ran (opt-in, or forced by SyN distortion correction); QSIRecon warps
its atlases through it (the guard in :func:tit.pre.qsi.qsirecon.run_qsirecon).
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
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 | |
get_inherited_dood_resources ¶
Determine DooD resource defaults that match the current container.
Returns (cpus, memory_gb) with conservative rounding.