MathOnco / valis

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

Issues with points - not transformed correctly? #160

Open lukasz-migas opened 2 months ago

lukasz-migas commented 2 months ago

Hi,

First, thanks a lot for creating this package - it has been fantastic for the non-rigid registration of MxIF, HE and CODEX data.

I am reaching out because I've encountered an issue when transforming xy coordinates and cannot figure out where the issue is coming from.

Let's say I have two serial sections - for each section, I have a number of modalities (section 1 - HE, AF, MxIF; section 2 - AF, CODEX, CSVs + GeoJSONs with segmentation, etc.). Within each section, the modalities are co-registered, so I only have to register any image of section 2 to any image of section 1 (HE is my target modality).

That works fairly well and I have a 'decent' registration. I then apply the established transformation on all the related modalities of section 2 images using slide.warp_slide(level=0, interp_method="bicubic", crop="reference", src_f=<PATH_TO_FILE>, non_rigid=True) and (CSVs, GeoJSONs) using slide.warp_xy(xy, crop="reference", non_rigid=True).

The images are transformed correctly; however, the CSV/GeoJSONs are slightly misaligned.

Below, you can see a zoom-in small region of the image where it's clear that the centroid position of a cell is offset by 5um on the x-axis and 2um on the y-axis (in the shown region).

Showing:

Before transformation - this is before the image + CSV is being transformed image

After transformation - this is after the image + CSV has been transformed image

Tested using:

Weirdly, this does not happen when I am not doing the micro-registration. I would normally also perform micro-registration with ~12.5% of image size to refine the registration like this:

registrar.register_micro(
    max_non_rigid_registration_dim_px=micro_reg_size,
    processor_dict=channel_kws,
    reference_img_f=str(reference) if reference else None,
    align_to_reference=reference is not None,
)

(micro_reg_size = size of the image; channel_kws = dictionary with image pre-processing parameters; reference = path to the target image, if specified)

Do you know whether this is a potential bug, or am I missing something?

lukasz-migas commented 1 month ago

Hi,

@cdgatenbee just checking in to see whether you have any ideas why this might be happening.

cdgatenbee commented 1 month ago

Hi @lukasz-migas, So sorry for the late response. Between accumulating a backlog of valis issues and being unable to work due to the extreme weather in Tampa, I haven't had a chance to work on this :( It is on the to-do list though, so I will let you know as soon as I'm able to make some progress on this. Thanks for you patience.

Best, Chandler

lukasz-migas commented 1 month ago

Hi @cdgatenbee,

Thanks for getting back to me, and I apologize for pinging you twice.

I didn't manage to solve the problem on my end but came up with a temporary solution/fix that gets me 99% of the way there. I simply convert the points/shapes into an image, transform it as an image and then convert it back to the original point cloud or shapes. It has a slight loss in precision, and it cuts off points outside of the image area, but it works fairly well. Would be happy to post the code here if somebody is interested.

Stay safe and all the best, Lukasz

cdgatenbee commented 2 weeks ago

Hi @lukasz-migas, No worries about pinging twice :) I do have an idea of why this is happening, but haven't yet had a chance to verify or work on the fix. I would like like to see you're code though, it could be a good fix until I'm able to sort out why the points aren't quite warping to the expected location when using micro-registration.

Best, -Chandler

lukasz-migas commented 1 week ago

Hey!

Thanks for getting back to me.

I've wrapped Valis in another application I am working on, so it has a CLI interface and a configuration file (perhaps it is of interest as a PR in the future). I will try to extract the relevant code and can also put the offending files in a Dropbox folder and share them.