david-hoffman / pyotf

python functions for simulating optical transfer functions
Apache License 2.0
70 stars 14 forks source link

Applying multiple named abberations #16

Closed mb1069 closed 3 years ago

mb1069 commented 3 years ago

Hi, thank you for writing this great package! I'm trying to apply multiple named aberrations to a PSF, but it seems like only the last aberration is applied.

    model_kwargs = dict(
        wl=525, na=1.27, ni=1.33, res=70, size=32, zrange=32, vec_corr="none", condition="none",
    )
    model = HanserPSF(**model_kwargs)
    model = apply_named_aberration('oblique astigmatism', 1)
    model = apply_named_aberration('piston', 0)

For example, the PSF generated from this code block is equivalent to an un-abberated PSF.

Is this expected behaviour? If not, is it possible to merge the mcoefs for various named abberations to produce a PSF aberrated using multiple Zernike polynomials?

david-hoffman commented 3 years ago

I don't think that code actually runs: can you supply a minimal working example with plotting?

model_kwargs = dict(
    wl=525, na=1.27, ni=1.33, res=70, size=32, zrange=32, vec_corr="none", condition="none",
)
model = HanserPSF(**model_kwargs)
model = apply_named_aberration('oblique astigmatism', 1)
model = apply_named_aberration('piston', 0)

gives

TypeError                                 Traceback (most recent call last)
<ipython-input-2-d4529ddce059> in <module>
      3 )
      4 model = HanserPSF(**model_kwargs)
----> 5 model = apply_named_aberration('oblique astigmatism', 1)
      6 model = apply_named_aberration('piston', 0)

TypeError: apply_named_aberration() missing 1 required positional argument: 'magnitude'

You can't apply apply_named_aberration more than once, it will overwrite any previously applied aberrations.

If you want to use more than one aberration see apply_aberration:

https://github.com/david-hoffman/pyOTF/blob/5cb240897dd3ff1e4bb17de5cf894fff79a2f961/pyotf/otf.py#L527-L539

If you think updating apply_named_aberration to accept multiple aberrations please open another issue.

mb1069 commented 3 years ago

Oop sorry about, here is a better example with layers plotted

import numpy as np
from skimage import img_as_ubyte
import matplotlib.pyplot as plt

from data.pyOTF.pyotf.otf import apply_named_aberration, HanserPSF

model_kwargs = dict(
    wl=525, na=1.27, ni=1.33, res=70, size=32, zsize=32, vec_corr="none", condition="none",
)
model = HanserPSF(**model_kwargs)
psf1 = apply_named_aberration(model, 'oblique astigmatism', 1)
psf2 = apply_named_aberration(psf1, 'piston', 0)

sub_psf = np.concatenate((psf1.PSFi, psf2.PSFi), axis=2)

axial_psf = np.concatenate(sub_psf[slice(5, sub_psf.shape[0]-5, 3)], axis=0)
axial_psf = axial_psf / axial_psf.max()
plt.imshow(axial_psf)
plt.show()

I think I've solved the issue though;

I'd be happy to open a pull request if you think this may be useful to share?

david-hoffman commented 3 years ago

Sure, go for it.

On Feb 21, 2021, at 00:44, Miguel Boland notifications@github.com wrote:

 Oop sorry about, here is a better example with layers plotted

import numpy as np from skimage import img_as_ubyte import matplotlib.pyplot as plt

from data.pyOTF.pyotf.otf import apply_named_aberration, HanserPSF

model_kwargs = dict( wl=525, na=1.27, ni=1.33, res=70, size=32, zsize=32, vec_corr="none", condition="none", ) model = HanserPSF(**model_kwargs) psf1 = apply_named_aberration(model, 'oblique astigmatism', 1) psf2 = apply_named_aberration(psf1, 'piston', 0)

sub_psf = np.concatenate((psf1.PSFi, psf2.PSFi), axis=2)

axial_psf = np.concatenate(sub_psf[slice(5, sub_psf.shape[0]-5, 3)], axis=0) axial_psf = axial_psf / axial_psf.max() plt.imshow(axial_psf) plt.show() I think I've solved the issue though;

Convert each named aberration to an array of pcoefs Sum arrays of Pcoefs to obtain coefficients containing multiple abberations Apply as un-named abberation to PSF I'd be happy to open a pull request if you think this may be useful to share?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

david-hoffman commented 3 years ago

@mb1069 one extra thing to note, piston is just a constant offset so has no effect on the PSF when applied to the phase.