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)
Resample image to new voxel spacing using IBSI-compliant 'Align grid centers' method.
Uses scipy.ndimage.affine_transform for memory efficiency.
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
|
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.
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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 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 | |
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[floating[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[floating[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). |
None
|
Returns:
| Type | Description |
|---|---|
Image | NDArray[floating[Any]]
|
Discretised Image object or numpy array (depending on input). |
Image | NDArray[floating[Any]]
|
Values are 1-based indices. |
Example
Discretise image into 32 fixed bins (FBN):
Source code in pictologics/preprocessing.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 | |
Mask Operations
pictologics.preprocessing.apply_mask(image, mask, mask_values=1)
Apply mask to image and return flattened array of voxel values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image | NDArray[floating[Any]]
|
Image object or numpy array. |
required |
mask
|
Image | NDArray[floating[Any]]
|
Image object (mask) or numpy array. |
required |
mask_values
|
int | list[int] | None
|
Value(s) in the mask to consider as ROI. Default is 1. Can be a single integer or a list of integers. |
1
|
Returns:
| Type | Description |
|---|---|
NDArray[floating[Any]]
|
1D numpy array of values within the 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
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
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.01, roi_mask=None)
Detect if image contains a common sentinel value outside the ROI.
A sentinel value is detected if: 1. It appears in a significant fraction of voxels (>= min_presence_fraction) 2. If roi_mask is provided, the sentinel primarily appears outside the ROI
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, -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 1%. |
0.01
|
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
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)