brandondube / prysm

physical optics: integrated modeling, phase retrieval, segmented systems, polynomials and fitting, sequential raytracing...
https://prysm.readthedocs.io/en/stable/
MIT License
267 stars 46 forks source link

zero division error in Image Simulation tutorial #114

Closed asebian closed 1 month ago

asebian commented 2 months ago

As I am working trought the Image Simulation notebook the OPD graph does not show anything. There seems to be an issue with the change from the Zernike to the Forbes polynomial base.

basis = list(polynomials.qpoly.Q2d_sequence(nms, r_aber, t))
print(basis)
C:\ProgramData\intelpython3\lib\site-packages\prysm\polynomials\qpoly.py:846: RuntimeWarning: divide by zero encountered in scalar divide
  Q0 = 1 / (2 * f0)

[array([[-2.        , -1.99334197, -1.98670119, ..., -1.98007763,
        -1.98670119, -1.99334197],
       [-1.99334197, -1.98669381, -1.98006288, ..., -1.97344916,
        -1.98006288, -1.98669381],
       [-1.98670119, -1.98006288, -1.9734418 , ..., -1.96683792,
        -1.9734418 , -1.98006288],
       ...,
       [-1.98007763, -1.97344916, -1.96683792, ..., -1.96024385,
        -1.96683792, -1.97344916],
       [-1.98670119, -1.98006288, -1.9734418 , ..., -1.96683792,
        -1.9734418 , -1.98006288],
       [-1.99334197, -1.98669381, -1.98006288, ..., -1.97344916,
        -1.98006288, -1.98669381]]), array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]]), array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]]), array([[-38.59533651, -38.33591823, -38.07806612, ..., -37.82177182,
        -38.07806612, -38.33591823],
       [-38.33591823, -38.07777985, -37.82120211, ..., -37.56617667,
        -37.82120211, -38.07777985],
       [-38.07806612, -37.82120211, -37.56589322, ..., -37.31213114,
        -37.56589322, -37.82120211],
       ...,
       [-37.82177182, -37.56617667, -37.31213114, ..., -37.05962696,
        -37.31213114, -37.56617667],
       [-38.07806612, -37.82120211, -37.56589322, ..., -37.31213114,
        -37.56589322, -37.82120211],
       [-38.33591823, -38.07777985, -37.82120211, ..., -37.56617667,
        -37.82120211, -38.07777985]]), array([[ 10.26547789,  10.21250896,  10.15975914, ..., -10.10722775,
        -10.15975914, -10.21250896],
       [ 10.21250896,  10.15972569,  10.10716103, ..., -10.05481427,
        -10.10716103, -10.15972569],
       [ 10.15975914,  10.10716103,  10.054781  , ..., -10.00261837,
        -10.054781  , -10.10716103],
       ...,
       [-10.10722775, -10.05481427, -10.00261837, ...,   9.95063936,
         10.00261837,  10.05481427],
       [-10.15975914, -10.10716103, -10.054781  , ...,  10.00261837,
         10.054781  ,  10.10716103],
       [-10.21250896, -10.15972569, -10.10716103, ...,  10.05481427,
         10.10716103,  10.15972569]]), array([[-1.88573770e-15, -1.13535432e-02, -2.26023842e-02, ...,
        -3.37470982e-02, -2.26023842e-02, -1.13535432e-02],
       [ 1.13535432e-02, -1.86631133e-15, -1.12489306e-02, ...,
        -2.23938235e-02, -1.12489306e-02,  6.22103777e-16],
       [ 2.26023842e-02,  1.12489306e-02, -1.84703330e-15, ...,
        -1.11449821e-02,  6.15677768e-16,  1.12489306e-02],
       ...,
       [ 3.37470982e-02,  2.23938235e-02,  1.11449821e-02, ...,
         6.09300932e-16,  1.11449821e-02,  2.23938235e-02],
       [ 2.26023842e-02,  1.12489306e-02, -1.84703330e-15, ...,
        -1.11449821e-02,  6.15677768e-16,  1.12489306e-02],
       [ 1.13535432e-02, -1.86631133e-15, -1.12489306e-02, ...,
        -2.23938235e-02, -1.12489306e-02,  6.22103777e-16]]), array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]]), array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]]), array([[ 65.90426621,  65.31331134,  64.72686369, ...,  64.14489416,
         64.72686369,  65.31331134],
       [ 65.53150744,  64.94341429,  64.35981073, ...,  63.78066777,
         64.35981073,  64.94341429],
       [ 65.16030394,  64.57506111,  63.99429032, ...,  63.41796265,
         63.99429032,  64.57506111],
       ...,
       [-64.79065295, -64.20824908, -63.63029975, ..., -63.05677614,
        -63.63029975, -64.20824908],
       [-65.16030394, -64.57506111, -63.99429032, ..., -63.41796265,
        -63.99429032, -64.57506111],
       [-65.53150744, -64.94341429, -64.35981073, ..., -63.78066777,
        -64.35981073, -64.94341429]]), array([[-65.90426621, -65.53150744, -65.16030394, ...,  64.79065295,
         65.16030394,  65.53150744],
       [-65.31331134, -64.94341429, -64.57506111, ...,  64.20824908,
         64.57506111,  64.94341429],
       [-64.72686369, -64.35981073, -63.99429032, ...,  63.63029975,
         63.99429032,  64.35981073],
       ...,
       [-64.14489416, -63.78066777, -63.41796265, ...,  63.05677614,
         63.41796265,  63.78066777],
       [-64.72686369, -64.35981073, -63.99429032, ...,  63.63029975,
         63.99429032,  64.35981073],
       [-65.31331134, -64.94341429, -64.57506111, ...,  64.20824908,
         64.57506111,  64.94341429]]), array([[-911.4641982 , -902.5427387 , -893.70299464, ..., -884.94427046,
        -893.70299464, -902.5427387 ],
       [-902.5427387 , -893.69319611, -884.92483152, ..., -876.23695309,
        -884.92483152, -893.69319611],
       [-893.70299464, -884.92483152, -876.22731226, ..., -867.60974866,
        -876.22731226, -884.92483152],
       ...,
       [-884.94427046, -876.23695309, -867.60974866, ..., -859.06197262,
        -867.60974866, -876.23695309],
       [-893.70299464, -884.92483152, -876.22731226, ..., -867.60974866,
        -876.22731226, -884.92483152],
       [-902.5427387 , -893.69319611, -884.92483152, ..., -876.23695309,
        -884.92483152, -893.69319611]])]

I am running prysm version 0.21.1. Thanks for your effort!

brandondube commented 1 month ago

Thanks for the bug report. This is a problem with requesting an invalid / nonsensical Q2D mode, which does not error nicely, but instead just fails. If you use Zernikes instead, there will be no error. This is (partially) fixed in the current master branch, although there is no nice error