TissueImageAnalytics / tiatoolbox

Computational Pathology Toolbox developed by TIA Centre, University of Warwick.
https://warwick.ac.uk/tia
Other
371 stars 75 forks source link

Image reader `read_region` returns read-only np.ndarray #837

Closed ziyuanzhao2000 closed 4 weeks ago

ziyuanzhao2000 commented 2 months ago

Description

This is more like a question rather than reporting a bug.

I wonder if there's any reason that read_region and similar methods seem to return a read-only array? I understand that it might be for avoiding accidental tampering with research data, but it seems inefficient to have to copy the array, for example, to use the stain normalizers (like Vahadane) on the image.

For example, this excerpt

stain_normalizer = stainnorm.VahadaneNormalizer()
stain_normalizer.fit(target_image)

normed_sample = stain_normalizer.transform(sample.copy())

is from the stain normalizer notebook. I wonder if it's possible to load images with read-write access on the array, so we don't have to do that copy. Thanks for hearing out this suggestion!

mostafajahanifar commented 4 weeks ago

Hi there,

You can use the stain normalizer without copying the input image and most probably that wouldn't change the input image in any way.

normed_sample = stain_normalizer.transform(sample.copy())

Regarding the read_region output, being immutable (read-only), things are a little bit more complex and I appreciate you have brought this to our attention. I don't think it is by design. We assume that created Numpy arrays created from PIL images are mutable by default, however, it seems that after Numpy v1.16.0, doing np.asarray(pil_image) will return an immutable Numpy array. Gladly, there is an easy fix for this, we should use np.array() instead of np.asarray. I will make a PR as quick fix for this issue, to also check if this change does not break other functionalities of TIAToolbox.