jrkerns / pylinac

An image analysis library for medical physics
https://pylinac.readthedocs.io/en/latest/
MIT License
153 stars 98 forks source link

Image array normalization uses significant amounts of memory #510

Open Quantico-Bullet opened 2 months ago

Quantico-Bullet commented 2 months ago

Description of the bug Normalization of image arrays using the normalize function in the array_utils.py (line: 57) uses a significant amount of memory due to conversion of the initial array dtype from np.int16 to np.float64 for the returned array.

To Reproduce/Check Simply amend the code to be as follows:

@validate(array=array_not_empty)
def normalize(array: np.ndarray, value: float | None = None) -> np.ndarray:
    """Normalize an array to the passed value. If not value is passed, normalize to the maximum value"""
    print("dtype before: ", array.dtype)
    if value is None:
        val = array.max()
    else:
        val = value
    array = array / val
    print("dtype after: ", array.dtype)
    return array

Possible solution Return the array with dtype np.float16 using:

return array.astype(np.float16)
jrkerns commented 1 month ago

I can add a parameter for the desired datatype to the call.