scottprahl / pypolar

Python modules for polarization changes using Jones or Mueller calculus
MIT License
35 stars 8 forks source link

Draw Poincaré sphere #1

Open gyger opened 4 years ago

gyger commented 4 years ago

I think it would be nice if one could draw the Poincaré sphere. As an example the library py_pol: https://bitbucket.org/optbrea/py_pol/src/master/py_pol/drawings.py

Which is unfortunately GPL.

scottprahl commented 4 years ago

py_pol is a nice module! Bizarrely the module is MIT but the code explicitly says GPL?

I have wanted to add Poincaré sphere drawings to pypolar but just have not had time.

gyger commented 4 years ago

It is a nice module, i came from there, but I don't like the encapsulation and so. I prefer having the matrices and vectors (like your module). Migrating their Poincaré sphere to pypolar "style" is "easy", the question is if one should just contact them to ask for the license of this specific source file. Perhaps it is an oversight.

scottprahl commented 4 years ago

I just checked in basic support for plotting on a Poincaré sphere

import numpy as np
import matplotlib.pyplot as plt
import pypolar.jones as jones
import pypolar.mueller as mueller
import pypolar.visualization as vis

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111,projection='3d')
vis.draw_empty_sphere(ax)
for angle in range(0,90,30):
    J = jones.field_linear(np.radians(angle))
    vis.plot_jones_on_sphere(ax,J)
plt.show()

or

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111,projection='3d')
vis.draw_empty_sphere(ax)
for angle in range(0,90,30):
    S = mueller.stokes_linear(np.radians(angle))
    vis.plot_stokes_on_sphere(ax,S)
plt.show()

Things seem to work OK. Let me know if you have suggestions.

gyger commented 4 years ago

That sounds great. I think the set_box_aspect only came in matplotlib 3.3, I get an error and can not find it in the documentation of 3.2. I will try to get the new matplotlib installed.

One thing that would be super convenient in your library, is if the functions would expect only the last dimension to be the stokes vector, to e.g. calculate the intensity for a list of stokes vectors. As a short example that would be to calculate the polarization:

S[..., 0]/np.sqrt(S[..., 1]**2+S[..., 2]**2+S[..., 3]**2)

scottprahl commented 4 years ago

I tried to remove the dependence of matplotlib v3.3.0. I also changed the parameter order and names to match other parts of thevisualization.py code. So now to draw a point on a Poincaré sphere, one does

J = pypolar.jones.field_linear(np.pi/6)
pypolar.visualization.draw_jones_poincare(J)

Something more complicated is done like this

S1 = pypolar.mueller.stokes_left_circular()
S2 = pypolar.mueller.stokes_linear(np.radians(15))

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
pypolar.visualization.draw_empty_sphere(ax)
pypolar.visualization.draw_stokes_poincare(S1, ax, label='  S1')
pypolar.visualization.draw_stokes_poincare(S2, ax, label='  S2')
pypolar.visualization.join_stokes_poincare(S1, S2, ax, lw=2, ls=':', color='orange')

see also the example at https://pypolar.readthedocs.io/en/latest/05-Jones-Examples.html

scottprahl commented 4 years ago

One thing that would be super convenient in your library, is if the functions would expect only the last dimension to be the stokes vector, to e.g. calculate the intensity for a list of stokes vectors. As a short example that would be to calculate the polarization:

S[..., 0]/np.sqrt(S[..., 1]**2+S[..., 2]**2+S[..., 3]**2)

This is a great idea. I will look into it.

scottprahl commented 4 years ago

just checked in support for lists of Stokes vectors. Tested only for 1D lists.

gyger commented 3 years ago

Thanks a lot, would be super useful if one could also use a list of Stoke vectors to plot on the poincare sphere.

def draw_stokes_poincare(S, ax=None, label=None, **kwargs):

    SS = np.sqrt(S[..., 1]**2+S[..., 2]**2+S[..., 3]**2)

    x = S[...,1]/SS
    y = S[...,2]/SS
    z = S[...,3]/SS

    ax.plot([x], [y], [z], 'o', **plot_args)

I think something along this line should work, the ax.plot needs to be slightly changed I think, if you want I can also send a pull-request.

scottprahl commented 3 years ago

@gyger please submit a pull request and example of usage. Thanks