Preprocessing API
Preprocessing utilities for image manipulation and sentinel value handling.
Image Resampling
pictologics.preprocessing.resample_image(image, new_spacing, interpolation='linear', boundary_mode='nearest', round_intensities=False, mask_threshold=None, source_mask=None, weight_threshold=0.5)
Resample image to new voxel spacing using IBSI-compliant 'Align grid centers' method.
The common cases (3D float64 image, 'nearest' boundary, nearest/linear interpolation) run on parallel numba kernels; everything else uses scipy.ndimage.affine_transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input Image object. |
required |
new_spacing
|
tuple[float, float, float]
|
Target spacing (x, y, z). Must be positive. |
required |
interpolation
|
str
|
Interpolation method. 'nearest': Nearest neighbour (order 0). 'linear': Trilinear (order 1). 'cubic': Tricubic spline (order 3). |
'linear'
|
boundary_mode
|
str
|
Padding mode for extrapolation. 'nearest' (default): Replicates edge values (aaaa|abcd|dddd). 'constant': Pads with constant value (0). 'reflect': Reflects at boundary. 'wrap': Wraps around. |
'nearest'
|
round_intensities
|
bool
|
If True, round resulting intensities to nearest integer. |
False
|
mask_threshold
|
Optional[float]
|
If provided, treat output as a binary mask. Values >= threshold become 1, others 0. Commonly 0.5 for partial volume correction. |
None
|
source_mask
|
Optional[Image | NDArray[bool_]]
|
Optional source validity mask. If provided (or if image.source_mask is set), only valid voxels are used for interpolation. This prevents sentinel values (e.g., -2048 in CT) from contaminating the resampled output. Can be an Image object or a boolean numpy array. |
None
|
weight_threshold
|
float
|
Only used with a source mask. Minimum fraction of interpolation weight that must come from valid voxels for an output voxel to be considered valid. Default 0.5 (majority). |
0.5
|
Returns:
| Type | Description |
|---|---|
Image
|
Resampled Image object. If source_mask was used, the output Image will have |
Image
|
its source_mask attribute set to the resampled validity mask. |
Note
When source_mask is active, the function uses normalized interpolation: the contribution of each input voxel is weighted by its validity, and the result is normalized by the sum of valid weights. This ensures that sentinel voxels do not affect the output.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any element of |
Example
Resample image to isotropic 1mm spacing using linear interpolation:
from pictologics.preprocessing import resample_image
# Resample to 1x1x1 mm
resampled_img = resample_image(
image,
new_spacing=(1.0, 1.0, 1.0),
interpolation="linear"
)
Resample with sentinel-value exclusion:
Source code in pictologics/preprocessing.py
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 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 656 657 658 659 660 661 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 | |
Discretisation
pictologics.preprocessing.discretise_image(image, method, roi_mask=None, n_bins=None, bin_width=None, min_val=None, max_val=None, cutoffs=None)
Discretise image intensities.
Supports IBSI-compliant Fixed Bin Number (FBN) and Fixed Bin Size (FBS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image | NDArray[Any]
|
Input Image object or numpy array. |
required |
method
|
str
|
'FBN' (Fixed Bin Number), 'FBS' (Fixed Bin Size), or 'FIXED_CUTOFFS'. |
required |
roi_mask
|
Image | NDArray[Any] | None
|
Optional mask to define the ROI for determining min/max values. |
None
|
n_bins
|
Optional[int]
|
Number of bins (required for FBN). |
None
|
bin_width
|
Optional[float]
|
Bin width (required for FBS). |
None
|
min_val
|
Optional[float]
|
Minimum value for discretisation. For FBS, defaults to ROI minimum (or global minimum). For FBN, defaults to ROI minimum. |
None
|
max_val
|
Optional[float]
|
Maximum value for discretisation (FBN only). Defaults to ROI maximum. |
None
|
cutoffs
|
Optional[list[float]]
|
List of cutoffs (required for FIXED_CUTOFFS). Values below the first cutoff map to bin 1; values >= the last cutoff map to bin len(cutoffs) + 1. |
None
|
Returns:
| Type | Description |
|---|---|
Image | NDArray[int32]
|
Discretised Image object or numpy array (depending on input). |
Image | NDArray[int32]
|
Values are 1-based int32 indices; 0 marks NaN (invalid) voxels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
Discretise image into 32 fixed bins (FBN):
Source code in pictologics/preprocessing.py
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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 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 | |
Mask Operations
pictologics.preprocessing.apply_mask(image, mask, mask_values=None)
Apply mask to image and return flattened array of voxel values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image | NDArray[Any]
|
Image object or numpy array. |
required |
mask
|
Image | NDArray[Any]
|
Image object (mask) or numpy array. |
required |
mask_values
|
int | list[int] | None
|
Optional value(s) in the mask to consider as ROI. When omitted, all nonzero mask values are considered ROI membership. Can be a single integer or a list of integers. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating[Any]]
|
1D numpy array of values within the mask. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the image and mask arrays have different shapes. |
Example
Extract intensities within a spherical mask:
Source code in pictologics/preprocessing.py
pictologics.preprocessing.resegment_mask(image, mask, range_min=None, range_max=None)
Update mask to exclude voxels where image intensity is outside the specified range. Used for IBSI re-segmentation (e.g. [-1000, 400] HU).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Image object. |
required |
mask
|
Image
|
Image object (mask). |
required |
range_min
|
Optional[float]
|
Minimum intensity value (inclusive). If None, no lower bound. |
None
|
range_max
|
Optional[float]
|
Maximum intensity value (inclusive). If None, no upper bound. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Updated Image object (mask) with re-segmentation applied. |
Example
Resegment mask to keep only values between -1000 and 400 (e.g. HU range):
Source code in pictologics/preprocessing.py
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | |
Outlier Filtering
pictologics.preprocessing.filter_outliers(image, mask, sigma=3.0)
Exclude outliers from the mask based on mean +/- sigma * std. IBSI 3.6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Image object. |
required |
mask
|
Image
|
Image object (mask). |
required |
sigma
|
float
|
Number of standard deviations. |
3.0
|
Returns:
| Type | Description |
|---|---|
Image
|
New Image object (mask) with outliers removed. |
Example
Remove outliers beyond 3 standard deviations from the mean:
Source code in pictologics/preprocessing.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 | |
Sentinel Value Handling
Utilities for detecting and masking sentinel values (e.g., -2048 HU for outside-FOV regions in CT).
pictologics.preprocessing.detect_sentinel_value(image, candidate_values=COMMON_SENTINEL_VALUES, min_presence_fraction=0.05, roi_mask=None)
Detect if image contains a common sentinel value outside the ROI.
A candidate is eligible as a sentinel if: 1. It occupies a significant fraction of the whole image (>= min_presence_fraction). 2. If roi_mask is provided, it appears primarily outside the ROI (ratio > 2:1 outside vs inside). This guard distinguishes a padding/fill value from a legitimate low-density tissue value (e.g. real air at ~-1000 HU in a raw, un-masked CT) that would otherwise be misdetected on proportion alone.
When several candidates are eligible, the one occupying the largest fraction of the image is returned; exact ties are broken by candidate_values order.
This is used by the pipeline's AUTO source_mode to automatically detect images that have been pre-masked with sentinel values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input Image object. |
required |
candidate_values
|
tuple[float, ...]
|
Values to check for sentinel patterns. Defaults to common medical imaging sentinels: -2048, -3024, -1024, -1000, 0, -32768. |
COMMON_SENTINEL_VALUES
|
min_presence_fraction
|
float
|
Minimum fraction of voxels that must equal the candidate to consider it a sentinel. Default is 5%. |
0.05
|
roi_mask
|
Optional[Image]
|
Optional ROI mask. If provided, checks that sentinel values are primarily outside the mask (ratio > 2:1 outside vs inside). |
None
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
The detected sentinel value, or None if not detected. |
Example
Source code in pictologics/preprocessing.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
pictologics.preprocessing.create_source_mask_from_sentinel(image, sentinel_value, tolerance=0.0)
Create a source validity mask by marking sentinel voxels as invalid.
The returned mask has value 1 for valid (non-sentinel) voxels and 0 for invalid (sentinel) voxels. This mask can be used with the Image.source_mask attribute to exclude sentinel voxels from resampling and filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input Image object. |
required |
sentinel_value
|
float
|
The value considered as sentinel (invalid data). |
required |
tolerance
|
float
|
Values within this tolerance of sentinel_value are also considered invalid. Default 0 means exact match only. |
0.0
|
Returns:
| Type | Description |
|---|---|
Image
|
Image object with binary mask (1 = valid, 0 = sentinel). |
Example
from pictologics.preprocessing import create_source_mask_from_sentinel
from pictologics.loader import load_image
image = load_image("ct_with_background.nii.gz")
# Create mask excluding -2048 sentinel values
source_mask = create_source_mask_from_sentinel(image, sentinel_value=-2048)
# Apply to image for sentinel-aware processing
image_with_mask = image.with_source_mask(source_mask)