Skip to content

ANTsPy Segmentation Fault in mask_image Function #838

@AlanYWu

Description

@AlanYWu

Hi! I am using ANTsPy to register the brain, and I found I keep getting segmentation errors. Below is a reproducible issue report. Thank you!

Issue Description

The ants.mask_image() function causes a segmentation fault on macOS ARM64 (Apple Silicon) when used with certain image data. This issue prevents the use of this function in production workflows.

Environment Information

  • Platform: macOS-15.5-arm64-arm-64bit (Apple Silicon)
  • Python: 3.10.13 (conda-forge)
  • ANTsPy: 0.5.4 (pypi)
  • ANTs: 2.6.2 (conda-forge)
  • NumPy: 2.0.1 (pypi)
  • Matplotlib: 3.10.3 (pypi)

Steps to Reproduce

  1. Create a conda environment with ANTsPy:
conda create -n ants python=3.10
conda activate ants
conda install -c conda-forge ants
pip install antspyx
  1. Run the provided reproduction script:
python ants_segmentation_fault_with_real_data.py

Expected Behavior

The ants.mask_image() function should apply the mask to the image and return a new masked image without causing a segmentation fault.

Actual Behavior

The function causes a segmentation fault (exit code 139) when called with certain image data, particularly with:

  • Large images (1024x512x218)
  • Images that have been standardized (origin, spacing, direction modified)
  • Specific mask values and parameters

Minimal Reproduction Example

import numpy as np
import ants

# Load and standardize images (this triggers the issue)
fixed_image = ants.image_read("JFRCtemplate2010.nrrd") # This can be downloaded from https://github.com/VirtualFlyBrain/DrosAdultBRAINdomains/blob/master/template/JFRCtemplate2010.nrrd
mask = ants.image_read("'JFRCtempate2010.mask130819_Original.nrrd'") # This can be downloaded from https://github.com/VirtualFlyBrain/DrosAdultBRAINdomains/blob/master/combinedIndexFiles/JFRCtempate2010.mask130819_Original.nrrd

# Standardize images
fixed_image = ants.image_clone(fixed_image, pixeltype='unsigned char')
fixed_image.set_origin((0, 0, 0))
fixed_image.set_spacing((1.0, 1.0, 1.0))
fixed_image.set_direction(np.eye(3))

mask = ants.image_clone(mask, pixeltype='unsigned char')
mask.set_origin((0, 0, 0))
mask.set_spacing((1.0, 1.0, 1.0))
mask.set_direction(np.eye(3))

# This line causes segmentation fault
masked_image = ants.mask_image(image=fixed_image, mask=mask, level=1, binarize=False)

Workaround

A manual numpy-based masking approach works without issues:

# Manual masking workaround
fixed_np = fixed_image.numpy()
mask_np = mask.numpy()
binary_mask = (mask_np == 1).astype(np.uint8)
masked_np = fixed_np * binary_mask

masked_image = ants.from_numpy(
    masked_np,
    origin=fixed_image.origin,
    spacing=fixed_image.spacing,
    direction=fixed_image.direction
)

Additional Notes

  • The issue appears to be specific to certain image properties or sizes
  • Simple synthetic data (64x64x32) does not reproduce the issue
  • The issue occurs consistently with real neuroimaging data
  • All other ANTsPy functions work correctly
  • The workaround provides identical functionality without the segmentation fault

Impact

This issue affects users working with neuroimaging data on macOS ARM64 systems, particularly when using the mask_image function in automated workflows or scripts.

Files Attached

  • ants_segmentation_fault_with_real_data.py: Complete reproduction script
  • ants_segmentation_fault_reproducer.py: Minimal reproduction attempt (doesn't trigger the issue)

Request

Please investigate this segmentation fault and provide a fix for the mask_image function on macOS ARM64 systems. The workaround, while functional, is not ideal for production use.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions