BodenmillerGroup / steinbock

A toolkit for processing multiplexed tissue images
https://bodenmillergroup.github.io/steinbock
MIT License
49 stars 14 forks source link

CellProfiler error during SaveImages (within Docker): Images of type float must be between -1 and 1 #75

Closed a-rayford closed 2 years ago

a-rayford commented 2 years ago

Hi again,

I have had issues with CellProfiler saving images, both multichannel and single-channel tiffs. Each time I get the error "Images of type float must be between -1 and 1". I can't seem to find much help for this online, and I have the settings for NamesAndTypes and SaveImages exactly as they are in 3_measure_mask_basic.ccpipe from the IMCPreprocessingPipeline (CP4_pipelines).

I made a CellProfiler project within Docker that simply loads all the multichannel tiffs from the data/img folder and tries to save them (in data/test), and I get the error. I can't seem to attach the .ccproj file but I put screenshots of the settings below. Can you reproduce this and if so, is there something I'm doing wrong? I get a similar error when using CellProfiler locally.

From NamesAndTypes: image

From SaveImages: image

Error Message: image

Thanks again for your help! -austin

jwindhager commented 2 years ago

Hi Austin, sorry that you're experiencing this! I'll take a look and try to reproduce this tomorrow. Keeping you posted.

votti commented 2 years ago

Usually this happens if either float images used as a image source or a rescaling operation is used that brings an image outside the -1/1 range.

A quick fix is to add an "ImageMath" step that multiplies the image with 1 and has the setting "set values bigger than 1 to 1 set. An example is Module 7 here: https://github.com/BodenmillerGroup/ImcSegmentationPipeline/blob/main/cp4_pipelines/1_prepare_ilastik.cppipe

Cheers!

On Thu, Oct 28, 2021, 21:41 Jonas Windhager @.***> wrote:

Hi Austin, sorry that you're experiencing this! I'll take a look and try to reproduce this tomorrow. Keeping you posted.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/BodenmillerGroup/steinbock/issues/75#issuecomment-954145358, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4R2KH4SITO4YPHOKSTEMDUJGYQFANCNFSM5G4FYZBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

a-rayford commented 2 years ago

Hi Vito,

Thanks for the tips and nice to hear from you! Is it normal for images to be out of the -1 to 1 range when importing multipage tiffs (generated from IMC data using Steinbock, etc) into cellprofiler?

I am doing an analysis on spheroids, therefore I'm trying to construct a distance-to-border image (using transform binary and an ilastik-trained binary mask), append it to the stack of IMC images, and resave the stack before measuring the cell mask intensities for each channel with steinbock.

This is the pixel intensity histogram for one of the distance-to-border images. (I'm not entirely sure I did it right...)

MicrosoftTeams-image (6)

How would I scale this appropriately to fit within the -1 to 1 range before saving? It seems tricky if the intensity range varies between different image sets, since I believe these are quantitative values.

Thanks, Austin

votti commented 2 years ago

Hi, The distance image looks fine to me. If I recall it correctly I rescaled images like this manually with a large constant value eg 1/65565. just don't forget to reapply this scaling in the final analysis. As distance images include negative values, I would save them as floating point, not uint16 images.

Best, Vito

On Fri, Oct 29, 2021, 19:18 a-rayford @.***> wrote:

Hi Vito,

Thanks for the tips and nice to hear from you! Is it normal for images to be out of the -1 to 1 range when importing multipage tiffs (generated from IMC data using Steinbock, etc) into cellprofiler?

I am doing an analysis on spheroids, therefore I'm trying to construct a distance-to-border image (using transform binary and an ilastik-trained binary mask), append it to the stack of IMC images, and resave the stack before measuring the cell mask intensities for each channel with steinbock.

This is the pixel intensity histogram for one of the distance-to-border images. (I'm not entirely sure I did it right...)

[image: MicrosoftTeams-image (6)] https://user-images.githubusercontent.com/44172212/139475828-604df580-4491-4830-8a08-b5b8999150a2.png

How would I scale this appropriately to fit within the -1 to 1 range before saving? It seems tricky if the intensity range varies between different image sets, since I believe these are quantitative values.

Thanks, Austin

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/BodenmillerGroup/steinbock/issues/75#issuecomment-954911797, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4R2KF3SYOZIMDF2QIEI23UJLJN7ANCNFSM5G4FYZBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

a-rayford commented 2 years ago

Hi Vito,

Thanks for this, I was able to save the distance image as a tiff after rescaling by 1/65565 using ImageMath. However, applying the same ImageMath function to the FullStack (multichannel) IMC image (loaded from the steinbock/img folder, also has issues with -1 to 1 range when I simply try to load/resave it in CP) only works when I turn the "eye" off for that step. Combining the scaled distance image and the scaled FullStack image using StackImages does not produce an error but the resulting saved tiff (regardless of 8- 16- or 32-bit floating integer) is not the correct dimensions (too skinny) and has 365 channels instead of ~30. I'm not sure if I'm doing something wrong in CellProfiler or if these set of commands just won't work when starting with a multichannel image.

The original goal was to add the distance image as a channel to the IMC image stack so they could all be measured simultaneously using steinbock measure intensities. Since I currently can only save the distance images alone and not the full stack, I tried to measure them independently by swapping them with the full stack IMC tiffs in the steinbock/img folder. When I try to measure these distance images I get the following error for each ROI/.tiff in the img folder:

Error measuring intensities in img/9_HCC827-SV80_HCC827-SV80_5_6.tiff Traceback (most recent call last): File "/app/steinbock/steinbock/measurement/intensities.py", line 57, in try_measure_intensities_from_disk intensities = measure_intensites( File "/app/steinbock/steinbock/measurement/intensities.py", line 37, in measure_intensites data = { File "/app/steinbock/steinbock/measurement/intensities.py", line 39, in <dictcomp> img[i], labels=mask, index=object_ids IndexError: index 1 is out of bounds for axis 0 with size 1

Any idea what this error means? Does steinbock measure intensities have any other dependencies besides the tiffs in the masks and img folder? The names of the distance .tiff files are identical to the multipage IMC tiffs that I swapped out in the img folder.

I know that this is a rather specific issue, however I think that it will be useful to work out these kinks with distance transforms within Steinbock since this is a common goal in many pipelines to measure distances to binary tissue compartment masks. -austin

jwindhager commented 2 years ago

Hi @a-rayford

just to keep you in the loop: I'm currently fixing some issues with steinbock (new release expected for today or tomorrow). Not sure these are related to this issue, but I will look into it tomorrow the latest. Apologies for the delay and thanks for the patience :)

Cheers

a-rayford commented 2 years ago

Hi Jonas,

No rush, just wanted to follow up with how far I got on this until I got stuck again. Looking forward to the new release. :) -austin

jwindhager commented 2 years ago

Hi @a-rayford, apologies again for the delay. Just released v0.10.0, which should at least (probably) fix the issue of loading images in cellprofiler after steinbock measure cellprofiler prepare. Will try to reproduce your pipeline and take care of the rest next week. Have a good weekend!

jwindhager commented 2 years ago

Assuming this issue to be resolved - please reopen if not! Thanks

a-rayford commented 2 years ago

I fixed this issue via a workaround in CellProfiler as follows:

See CellProfiler issue #4495 and #4496. Potentially more flexible workaround using image metadata to come.

jwindhager commented 2 years ago

Hi @a-rayford, thanks so much for sharing this! Indeed, as you've found in @nilseling's issues on the CellProfiler repository, CellProfiler seems to have problems with writing multi-channel images as of now.

Few comments on the steinbock side that may simplify things a bit for you:

(while also adding my distance transform image as an additional channel at the end, no need to rescale)

In case you are using the distance transform to identify object (e.g. cell) neighbors, have a look at measuring object neighbors using pixel expansion, which thresholds on Euclidean distance transforms to "expand" regions to identify neighboring objects.

Run steinbock measure intensities, on the output files (rename output folder to "img")

No need to rename the folder here: any steinbock command can be customized to run with arbitrary input/output folder/file names, see e.g. steinbock measure intensities --help. You can just run steinbock measure intensities --img YOUR_FOLDER.