PMEAL / porespy

A set of tools for characterizing and analying 3D images of porous materials
https://porespy.org
MIT License
308 stars 100 forks source link

Wrong porosity value on large images #814

Closed ivonindima closed 1 year ago

ivonindima commented 1 year ago

The function ps.metrics.porosity() returns an incorrect value if applied to a large image. This happens on Windows if the number of voxels in any phase exceeds the maximum allowable value for np.int32 (2_147_483_647).

Possible fix:

def porespy_porosity(im):
    im = np.array(im, dtype=int)
    Vp = np.sum(im == 1)
    Vs = np.sum(im == 0)
    e = Vp / (Vs + Vp)
    return e

def fixed_porosity(im):
    im = np.array(im, dtype=int)
    Vp = np.sum(im == 1, dtype = np.int64)
    Vs = np.sum(im == 0, dtype = np.int64)
    e = Vp / (Vs + Vp)
    return e

img = np.zeros((1500,1500,2000), dtype=int)
img[:,:,:1500] = 1
print(porespy_porosity(img))
print(fixed_porosity(img))
jgostick commented 1 year ago

Hi @ivonindima Thanks for this bug report. Your proposed fix looks good.

As for the triplicate issues, I think heard that github was having some problems this morning. I will delete the others.