PtyLab / PtyLab.py

Python implementation of ptylab
Other
18 stars 8 forks source link

Circ is convolved with gaussian which leads for small diameters to unexpected behaviour #31

Closed wfeschen closed 9 months ago

wfeschen commented 1 year ago

I recently tried to reconstruct a dataset with a really small probe (20 pixel diameter with a detector size of 256x256). Unfortunately, the circ function does not work for small diameters. The issue is the gaussian filter. I suggest making the gaussian filter an option or even remove it.

Here the problematic code from the initializationFunctions.py file Here

if type_of_init == "circ":
        try:
            # BUG: This only works for the probe, not for the object
            pupil = circ(data.Xp, data.Yp, data.data.entrancePupilDiameter)

            # pupil = circ(data.Xp, data.Yp, data.Xp.max()/2)
            # soften the edges a bit
            from scipy import ndimage

            pupil = ndimage.gaussian_filter(
                pupil.astype(np.float64), 0.05 * data.Xp.shape[-1]
            )
            return np.ones(shape, dtype=np.complex64) * pupil + 0.001 * np.random.rand(
                *shape
            )
dboonz commented 1 year ago

Ah yes, I put that in, as I usually get circles in my images which take forever to solve. For me this worked like a charm. We could change it to make it optional, but most likely if we make the width a function of entrancePupilDiameter it should be fixed as well, right? Can you show what the problem is if you have a tiny probe? You can just drag and drop an image here.

I also see that there's a bug in the implementation, at the moment the random numbers are uniformly distributed over the entire image, also outside of the entrancepupildiameter.

I'll change these two things, can you take a look afterwards?

dboonz commented 1 year ago

image I updated the code. It solves the bug of having uniform imaginary noise over the entire probe and it convolves with a width that is dependent on the width of the illumination. I tried to run it with simulated data with a probe width of 5 pixels and that seems to work just fine. Can you take a look?

dboonz commented 1 year ago

image And this is after running

dboonz commented 1 year ago

Also see test_initializationFunctions.py for an idea

dboonz commented 1 year ago

I also noticed when doing this that changing entrancePupilDiameter at the moment is extremely unintuitive. As long as you never change it it can be fine but simply changing it in the script has no effect. That's a pretty serious bug and I think we should actually rewrite initializationFunctions.py.

wfeschen commented 1 year ago

Hi Dirk,

changing entrancePupilDiameter works for me, but I have to use the property of the experimentalData object.

# Type of the initial probe
reconstruction.initialProbe = 'circ'
experimentalData.entrancePupilDiameter = 10e-6

# Use simple ones for the initial object
reconstruction.initialObject = 'ones'
reconstruction.initializeObjectProbe()

Best, Wilhelm

dboonz commented 1 year ago

@wfeschen says:

Hi Dirk, I did some testing with the new function and for me the new implementation was unfortunately not convenient. For the test script with the really small probe any smoothing of the probe leads to a bad reconstruction. While the hard edge of the probe definitely shows a better reconstruction. I would suggest having both options available in the code

dboonz commented 1 year ago

If this is the case, let's make it an option indeed.

dboonz commented 1 year ago

@larsloetgering : Did you ever try to smoothen the edges of the probe? I noticed that it gave me better reconstructions if the parameters are not entirely correct, aka when you're building a new setup. With the sharp edge I saw a lot of circles being drawn in the object which only slowly disappear. However, apparently if you have a very small probe it will make it worse.