MathOnco / valis

Virtual Alignment of pathoLogy Image Series
https://valis.readthedocs.io/en/latest/
MIT License
119 stars 29 forks source link

numpy.AxisError: axis 2 is out of bounds for array of dimension 2 #8

Closed MNM-TB closed 2 years ago

MNM-TB commented 2 years ago

Hi,

Thank you for a very interesting tool. I just installed the rc7 version and I'm trying to get the first example to run.

It runs well until after the Rigid registration but then fails regardless of what images I feed it. Below is the error output.

Thank you for the help!

` ======== Rigid registration complete in 18.376 seconds

/home/jovyan/venvs/valis/lib/python3.8/site-packages/valis/valtils.py:75: UserWarning: axis 2 is out of bounds for array of dimension 2 warnings.warn(warning_msg, warning_type)

Traceback (most recent call last): File "/home/jovyan/venvs/valis/lib/python3.8/site-packages/valis/registration.py", line 3064, in register rigid_registrar = self.rigid_register() File "/home/jovyan/venvs/valis/lib/python3.8/site-packages/valis/registration.py", line 2324, in rigid_register slide_obj.get_bg_color_px_pos() File "/home/jovyan/venvs/valis/lib/python3.8/site-packages/valis/registration.py", line 551, in get_bg_color_px_pos sum_img = self.image.sum(axis=2) File "/home/jovyan/venvs/valis/lib/python3.8/site-packages/numpy/core/_methods.py", line 48, in _sum return umr_sum(a, axis, dtype, out, keepdims, initial, where) numpy.AxisError: axis 2 is out of bounds for array of dimension 2

`

MNM-TB commented 2 years ago

Running with debug output I see that it shows the following error:

DEBUG:pyvips.error:Error image: no property named `affine'

I have tested this with vips version 8.12.2 and 8.11.4 and the error is the same.

The final debug line is:

DEBUG:pyvips.voperation:VipsOperation.call: result = None /home/jovyan/venvs/valis/lib/python3.8/site-packages/valis/valtils.py:75: UserWarning: axis 2 is out of bounds for array of dimension 2 warnings.warn(warning_msg, warning_type)

the "nnn_original_overlap.png" in the output dir image looks fine by the way so the parts this far have worked well.

cdgatenbee commented 2 years ago

Hello @MNM-TB ,

Thank you for bringing this to my attention. Could you tell me a bit more about the images you are working with? Are they by chance single channel images, maybe greyscale/processed versions of RGB images, or DAPI channels? It looks like maybe what is happening is that valis is trying to determine the background color of a multichannel image, but the image itself is single channel (hence why it doesn't have a 3rd axis, i.e. axis=2). I was able to recreate this error when registering greyscale images, and so it's something I'll need to fix. But please let me know if your images are multichannel, since that fix might not address your issue.

Best, -Chandler

MNM-TB commented 2 years ago

Hi, You are absolutely right! They are monochrome grayscale images. It is Spatial sequencing samples where we use the cDNA fluorescence in each sequencing cycle and I'm looking at Valis as means to coregister them to remove drift. It would be excellent if it would be possible to use with single-channel images as well.

cdgatenbee commented 2 years ago

Hi @MNM-TB , Relieved to hear that was the probably issue :) I've updated the code to be compatible with single channel images (1.0.0rc8). Sounds like a very interesting dataset and looking forward to hearing how it works out. If there are issues, please let me know. I'd love to get VALIS to work with this sort of data.

Best, -Chandler

MNM-TB commented 2 years ago

Hi, Thank you for fixing this. Now it completes the registration but I'm wondering if I'm doing something wrong as my registrations are way off. My test images are resonably well alignes from the start. They are off my 40 µm or so and rotation 0.9°. However after the VALIS registration one is rotated 45° or so. The files I'm trying to register is an inverted H&E to cDNA labeled with Cy3.

I'm using pretty much your standard code i.e.,:

slide_src_dir = export_path + "TestHE"
results_dst_dir = export_path + "TestHEout"
registered_slide_dst_dir = export_path + "intermediates"
reference_slide = "MNM_509_D1_cDNA_Crop_coReg.ome.tif"

# Create a Valis object and use it to register the slides in slide_src_dir, aligning towards the reference slide.
registrar = registration.Valis(slide_src_dir, results_dst_dir, reference_img_f=reference_slide)
rigid_registrar, non_rigid_registrar, error_df = registrar.register()

# Perform micro-registration on higher resolution images, aligning directly to the reference image
registrar.register_micro(max_non_rigid_registartion_dim_px=2000, align_to_reference=True)

I cannot attach my original files as they are about 4gb each but I attach a couple of the output thumbnails. Let me know if you need any additional information. Thanks'

MNM_509_D1_cDNA_Crop_coReg MNM_509_D1_HE-Invert_Crop

TestHE_original_overlap TestHE_rigid_overlap

cdgatenbee commented 2 years ago

Hi @MNM-TB ,

Glad to hear Valis is at least kind of working with your single channel images. I think maybe the problem with these registrations has to do with the preprocessing. In the thumbnails above, one is white on black (cDNA?) and the other black on white (inverted H&E?). They're essentially opposites, which I think might be the problem.

Do you happen to have the original RGB H&E image? Valis can register brightfield images to IF images, so it should automatically process the original RGB image to look like the cDNA image. The code is setup to use the default processing methods, but it would also be possible to do some color deconvolution on the H&E image to make it more closely resemble the cDNA image. Either way, the processed H&E image used for registration would be white on a black background, which should look more like the cDNA image. However, I'm assuming that 1st thumbnail is the cDNA image, and the 2nd is the inverted H&E, so please correct me if I'm wrong.

Valis also does adaptive histogram equalization on the IF images, but in this case it may be that it's too much, as the images are turning out pretty noisy. You could also try skipping that EQ step by passing {"adaptive_eq": False} to the "if_processing_kwargs" argument when calling registrar.register() and registrar.register_micro().

Finally, if you have small thumbnails of the original images you could share I wouldn't mind playing around a bit to see if I can get this to work better. They wouldn't have to be large, as an image with max dimension less than 1000px would be enough to test with. These could either be png (or similar), or you could use valis to read the slide (see the README for an example), rescale it ( warp_tools.rescale_img() ), and then save it as a png (warp_tools.save_img).

Let me know if any of these sound like good options and/or work out for you. If not, let me know and maybe we can try to come up with something else.

Best, -Chandler