ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
586 stars 161 forks source link

ENH: Threshold mask in images_to_matrix #646

Closed cookpa closed 1 month ago

cookpa commented 1 month ago

Prevents cryptic error if the mask is not binarized, because the threshold was being applied to the numpy matrix but not the mask itself.

Also add some more tests

cookpa commented 1 month ago

Related to #641

@ncullen93 the matrix here was all zero because it was sampling a corner of the image, by adding the spacing arg I make the downsampled mask cover all of the image domain

s = [65]img.dimension mask2 = ants.from_numpy(np.random.randn(s), spacing=[4.0, 4.0]) mask2 = mask2 > mask2.mean() imgmat = ants.images_to_matrix(imglist, mask=mask2)

Still not sure what the Windows seg faults were about, but will revisit if they happen again

ncullen93 commented 1 month ago

Great! My guess is the windows error was because there was a function within a function trying to access an image in the outer function environment.. perhaps something with the c++ references that is quit complicated. I removed that and it will hopefully be OK. Here is a simplified version:

def outer_fn(image, mask):
    def inner_fn(image):
        # mask not in "inner" environment but being accessed anyways
        return image + mask 
    return inner_fn(image)
coveralls commented 1 month ago

Coverage Status

coverage: 81.145% (+0.004%) from 81.141% when pulling 0796515a1cf2fad110e6fcd8a7d5f372be47fade on image_to_matrix_mask into 7d876d800b3a861ab8683b1a4119086cdde15435 on master.