PetroFit / petrofit

Python package for calculating Petrosian properties and fitting galaxy light profiles
https://petrofit.readthedocs.io
28 stars 7 forks source link

Centre of blended sub-masks affected by main structure mask. #201

Open lucatelli opened 1 year ago

lucatelli commented 1 year ago

I have been running some experiments on multi-structure sources. Consider that one structure is the brightest, while the others are smoother. I noticed that the centre of these 'secondary masks' are affected by the main one if they are too close (blended objects). For example, if a few bright pixels from the main mask are at the edge of a secondary mask, the centre will be attributed to those points, and not to the 'centre-of-light' of the actual sub-mask.

Here there is an example: 3 structures, ID1 is the brightest. image

When performing photometry on mask ID2, the centre seems not to be correct. image image image

I do not know if this is an issue, or supposed to be like this. Should the centre be at the centre of the mask?

Note: I am running another algorithm (SEP) for source detection, and feeding those masks into Petrofit. I am not interested on the full structure of the emission at first glance, just the most dominant parts (e.g. near the centre of each region). I am using petrofit to compute R50, I50, so that I feed those values as initial constraints for a multi-Sersic minimisation problem.

Thanks again for your hard work on petroFft!!!! Cheers Gef Lucatelli

robelgeda commented 1 year ago

Hi Gef, thank you for opening this issue, your feedback has been very useful and I really appreciate them!

The short answer for ID2 is that the plots seem to indicate that PF is doing the correct thing but that the mask seems a little off at first glance. For problems like these, you would need to use a deblender and mask out or fit and remove offending source (without doing so, the COG that is produced is severely contaminated). But the rest of my comments assumes its not possible/convenient to do that.

Just from the plots here it looks like PetroFit is centering roughly at the correct center. Unless you tell it too, PetroFit will center at the pixel with the max value (pf.source_photometry). I say this because ID1 looks like its at the center of the test image and your target (ID2) is slightly below, which is where the apertures are centered in the photometery image. It also looks like the x slice of the image (the last plot) is centered at something. Its hard to tell what is going on beyond this without seeing the function call so please post a snippet here if the discussion doesn't help.

My first impression is that the mask is not centered at ID2, it is centered at ID1 and includes all 3 sources in it. This is causing a few issues if you are interested in ID2. First, as you can see the ellipticity and PA of the apertures is not correct, its biased by the alignment of the sources in a ~45 degree angle. ID2 seems to also barely be within the mask's window. I say this because the y-slice (second to last plot) shows an abrupt cutoff of the profile right after the peak. So my conclusion is that the segmentation produced mask that is not good for measuring the curve of growth of ID2.

I assume you are already doing this but it would be appropriate to use pf.radial_photometry for this kind of situation because pf.source_photometry needs a PhotUtils segmentation Source object. In radial_photometry you can control where the center is along with the shape of the apertures. I realized that radial_photometry needs to be in the documents, I will open a ticket to do that!

Some questions I have for you that would help me further understand:

lucatelli commented 1 year ago

Hi Rob, sorry for this very late reply, I was busy with other things.

So, by main mask, I mean the mask region referent to the brightest part. But, you can have other sub-masks close to it, referring to regions with fainter brightness distribution, by being naturally deblended or from a deblending result.

I did not understand well your second question. Note that in this approximation, I am using as the mask the points inside the ellipse. This ellipse is centred at the centre of mass of the detected component. However, since it is close to the brightest component, some edge pixels can affect the centre, so when performing photometry only on the ID2, the centre can be biased towards those edge pixels which are more a part of ID1 than ID2.

To measure photometry, I am using source_photometry.

Notes: I am working with radio interferometric images. Across images, the noise structure/level can be different, but mostly Gaussian. So I am finding some issues when it comes to source detection and deblending. That is independent of the method, but I found that SEP is performing significantly better for that. So I am using SEP for source detection and pass the masks/objects/coordinates to PetroFit (photutils), to do photometry (with source_photometry). I need to work more on this to see if I can tune the detection and deblending parameters.

robelgeda commented 1 year ago

Sorry about my late reply as well and no worries!

This sounds like it would be too hard to get the segmentation right and then do the radial photometry. You would have to do the background subtraction yourself but using radial_photometry would give you much more control:

pf.radial_photometry(
    image,
    position,
    r_list,
    error=None,
    mask=None,
    elong=1.0,
    theta=0.0,
    plot=False,
    vmin=0,
    vmax=None,
)

Where image is background subtracted image and the rest is set by you instead of the segmentation map. position is the x and y pixel coordinate to center the apertures. This makes more sense to do because of the level of blending and the mask orientation. I would also truncate the r_list to a region that makes sense since at higher radii, the main bright source starts to dominate. This should help with the photometry but please let me know if we should discuss about how to improve deblending step (which is a related but separate goal).