Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

2D interpolating function from numpy arrays to spherical-plot3d

I need to plot an antenna emission pattern. I am importing data from a tabbed .txt file (theta[i,j],phi[i,j],r[i,j]) as numpy arrays. I then employ the same data to build a 2D interpolating function with the scipy module scipy.interpolate.interp2d. The interpolating function successfully works in plot3d, but somehow breaks spherical_plot3d. Is there anyway to build a working 2D interpolating function for spherical_plot3D?

------------Code-------------

#importing Libraries
import numpy as np
import scipy
import scipy.interpolate

#Importing data
data=np.loadtxt('/home/giova/data/work/electrodynamics/gmm/shell/maps/test.msc')

#Numpy arrays to python lists
l_theta=theta.tolist();
l_phi=phi.tolist();
l_r=r.tolist();

#building interpolating 2D function
f=scipy.interpolate.interp2d(l_phi,l_theta,l_r)

#plotting the function
x,y = var('x,y')
plot3d(f,(x,0,2*pi),(y,0,pi))

#Spherical Plotting
x,y = var('x,y')
spherical_plot3d(f,(x,0,2*pi),(y,0,pi)) Does not work
click to hide/show revision 2
updated code - problem solved with a workaround but original problem still there

2D interpolating function from numpy arrays to spherical-plot3d

I need to plot an antenna emission pattern. I am importing data from a tabbed .txt file (theta[i,j],phi[i,j],r[i,j]) as numpy arrays. I then employ the same data to build a 2D interpolating function with the scipy module scipy.interpolate.interp2d. The interpolating function successfully works in plot3d, but somehow breaks spherical_plot3d. Is there anyway to build a working 2D interpolating function for spherical_plot3D?

-----Toy Data---------

v_phi=array([ 0.        ,  1.57079637,  3.14159274,  4.71238911,  6.28318548])
v_theta=array([ 0.        ,  0.78539819,  1.57079637,  2.35619456,  3.14159274])
m_r=array([[ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422]])

------Traceback from spherical_plot3d---------

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_326.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" +     _support_.preparse_worksheet_cell(base64.b64decode("c3BoZXJpY2FsX3Bsb3QzZChmLCh4LDAsMipwaSksKHksMCxwaSksY21hcD0ncGluaycsYWRhcHRpdmU9VHJ1ZSkuc2hvdyhhc3BlY3RfcmF0aW89KDEsMSwxKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpR1mM9J/___code___.py", line 3, in <module>
    exec compile(u"spherical_plot3d(f,(x,_sage_const_0 ,_sage_const_2 *pi),    (y,_sage_const_0 ,pi),cmap='pink',adaptive=True).show(aspect_ratio=(_sage_const_1 ,_sage_const_1 ,_sage_const_1 ))" + '\n', '', 'single')
  File "", line 1, in <module>

  File "base.pyx", line 1052, in sage.plot.plot3d.base.Graphics3d.show     (sage/plot/plot3d/base.c:9313)
  File "base.pyx", line 957, in     sage.plot.plot3d.base.Graphics3d._process_viewing_options     (sage/plot/plot3d/base.c:9112)
  File "base.pyx", line 198, in    sage.plot.plot3d.base.Graphics3d._determine_frame_aspect_ratio (sage/plot/plot3d/base.c:3404)
  File "base.pyx", line 214, in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:3521)
  File "parametric_surface.pyx", line 318, in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:2991)
  File "parametric_surface.pyx", line 366, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3521)
  File "parametric_surface.pyx", line 361, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3451)
  File "parametric_surface.pyx", line 576, in sage.plot.plot3d.parametric_surface.ParametricSurface.eval_grid (sage/plot/plot3d/parametric_surface.c:5152)
  File "<string>", line 4, in <lambda>
  File "expression.pyx", line 3501, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:15529)
  File "expression.pyx", line 2089, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:10788)
  File "parent_old.pyx", line 229, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3420)
  File "parent.pyx", line 1047, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7734)
TypeError: no canonical coercion from <type 'numpy.ndarray'> to Symbolic Ring

------------Code-------------

#importing Libraries
import numpy as np
import scipy
import scipy.interpolate

#Importing data
data=np.loadtxt('/home/giova/data/work/electrodynamics/gmm/shell/maps/test.msc')

#Numpy arrays to python lists
l_theta=theta.tolist();
l_phi=phi.tolist();
l_r=r.tolist();
#getting the data
theta=data[:,0]
phi=data[:,1]
r=data[:,2];
#reshaping the data
side=sqrt(len(r));m_r=r.reshape(side,side);m_r=m_r.T;
v_theta=theta[::side]
v_phi=phi[0:side]

#building interpolating 2D function
f=scipy.interpolate.interp2d(l_phi,l_theta,l_r)
f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)

#plotting the function
x,y = var('x,y')
plot3d(f,(x,0,2*pi),(y,0,pi))

#Spherical Plotting
x,y = var('x,y')
spherical_plot3d(f,(x,0,2*pi),(y,0,pi)) Does not work
work!!!!!

#Workaround
def g(s,t):
    out1=f(s,t).tolist()
    out2=out1[0][0]
    return out2
spherical_plot3d(g,(x,0,2*pi),(y,0,pi)) Does not work!!!!!

2D interpolating function from numpy arrays to spherical-plot3d

I need to plot an antenna emission pattern. I am importing data from a tabbed .txt file (theta[i,j],phi[i,j],r[i,j]) as numpy arrays. I then employ the same data to build a 2D interpolating function with the scipy module scipy.interpolate.interp2d. The interpolating function successfully works in plot3d, but somehow breaks spherical_plot3d. Is there anyway to build a working 2D interpolating function for spherical_plot3D?

-----Toy Data---------

v_phi=array([ 0.        ,  1.57079637,  3.14159274,  4.71238911,  6.28318548])
v_theta=array([ 0.        ,  0.78539819,  1.57079637,  2.35619456,  3.14159274])
m_r=array([[ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422]])

------Traceback from spherical_plot3d---------

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_326.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" +     _support_.preparse_worksheet_cell(base64.b64decode("c3BoZXJpY2FsX3Bsb3QzZChmLCh4LDAsMipwaSksKHksMCxwaSksY21hcD0ncGluaycsYWRhcHRpdmU9VHJ1ZSkuc2hvdyhhc3BlY3RfcmF0aW89KDEsMSwxKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpR1mM9J/___code___.py", line 3, in <module>
    exec compile(u"spherical_plot3d(f,(x,_sage_const_0 ,_sage_const_2 *pi),    (y,_sage_const_0 ,pi),cmap='pink',adaptive=True).show(aspect_ratio=(_sage_const_1 ,_sage_const_1 ,_sage_const_1 ))" + '\n', '', 'single')
  File "", line 1, in <module>

  File "base.pyx", line 1052, in sage.plot.plot3d.base.Graphics3d.show     (sage/plot/plot3d/base.c:9313)
  File "base.pyx", line 957, in     sage.plot.plot3d.base.Graphics3d._process_viewing_options     (sage/plot/plot3d/base.c:9112)
  File "base.pyx", line 198, in    sage.plot.plot3d.base.Graphics3d._determine_frame_aspect_ratio (sage/plot/plot3d/base.c:3404)
  File "base.pyx", line 214, in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:3521)
  File "parametric_surface.pyx", line 318, in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:2991)
  File "parametric_surface.pyx", line 366, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3521)
  File "parametric_surface.pyx", line 361, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3451)
  File "parametric_surface.pyx", line 576, in sage.plot.plot3d.parametric_surface.ParametricSurface.eval_grid (sage/plot/plot3d/parametric_surface.c:5152)
  File "<string>", line 4, in <lambda>
  File "expression.pyx", line 3501, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:15529)
  File "expression.pyx", line 2089, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:10788)
  File "parent_old.pyx", line 229, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3420)
  File "parent.pyx", line 1047, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7734)
TypeError: no canonical coercion from <type 'numpy.ndarray'> to Symbolic Ring

------------Code-------------

#importing Libraries
import numpy as np
import scipy
import scipy.interpolate

#Importing data
data=np.loadtxt('/home/giova/data/work/electrodynamics/gmm/shell/maps/test.msc')

#getting the data
theta=data[:,0]
phi=data[:,1]
r=data[:,2];
#reshaping the data
side=sqrt(len(r));m_r=r.reshape(side,side);m_r=m_r.T;
v_theta=theta[::side]
v_phi=phi[0:side]

#building interpolating 2D function
f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)

#plotting the function
x,y = var('x,y')
plot3d(f,(x,0,2*pi),(y,0,pi))

#Spherical Plotting
x,y = var('x,y')
spherical_plot3d(f,(x,0,2*pi),(y,0,pi)) Does not work!!!!!

#Workaround
def g(s,t):
    out1=f(s,t).tolist()
    out2=out1[0][0]
    return out2
spherical_plot3d(g,(x,0,2*pi),(y,0,pi)) Does not work!!!!!
click to hide/show revision 4
retagged

2D interpolating function from numpy arrays to spherical-plot3d

I need to plot an antenna emission pattern. I am importing data from a tabbed .txt file (theta[i,j],phi[i,j],r[i,j]) as numpy arrays. I then employ the same data to build a 2D interpolating function with the scipy module scipy.interpolate.interp2d. The interpolating function successfully works in plot3d, but somehow breaks spherical_plot3d. Is there anyway to build a working 2D interpolating function for spherical_plot3D?

-----Toy Data---------

v_phi=array([ 0.        ,  1.57079637,  3.14159274,  4.71238911,  6.28318548])
v_theta=array([ 0.        ,  0.78539819,  1.57079637,  2.35619456,  3.14159274])
m_r=array([[ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422],
   [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 
0.55282422],
   [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 
0.55282422]])

------Traceback from spherical_plot3d---------

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_326.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" +     _support_.preparse_worksheet_cell(base64.b64decode("c3BoZXJpY2FsX3Bsb3QzZChmLCh4LDAsMipwaSksKHksMCxwaSksY21hcD0ncGluaycsYWRhcHRpdmU9VHJ1ZSkuc2hvdyhhc3BlY3RfcmF0aW89KDEsMSwxKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpR1mM9J/___code___.py", line 3, in <module>
    exec compile(u"spherical_plot3d(f,(x,_sage_const_0 ,_sage_const_2 *pi),    (y,_sage_const_0 ,pi),cmap='pink',adaptive=True).show(aspect_ratio=(_sage_const_1 ,_sage_const_1 ,_sage_const_1 ))" + '\n', '', 'single')
  File "", line 1, in <module>

  File "base.pyx", line 1052, in sage.plot.plot3d.base.Graphics3d.show     (sage/plot/plot3d/base.c:9313)
  File "base.pyx", line 957, in     sage.plot.plot3d.base.Graphics3d._process_viewing_options     (sage/plot/plot3d/base.c:9112)
  File "base.pyx", line 198, in    sage.plot.plot3d.base.Graphics3d._determine_frame_aspect_ratio (sage/plot/plot3d/base.c:3404)
  File "base.pyx", line 214, in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:3521)
  File "parametric_surface.pyx", line 318, in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:2991)
  File "parametric_surface.pyx", line 366, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3521)
  File "parametric_surface.pyx", line 361, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3451)
  File "parametric_surface.pyx", line 576, in sage.plot.plot3d.parametric_surface.ParametricSurface.eval_grid (sage/plot/plot3d/parametric_surface.c:5152)
  File "<string>", line 4, in <lambda>
  File "expression.pyx", line 3501, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:15529)
  File "expression.pyx", line 2089, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:10788)
  File "parent_old.pyx", line 229, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3420)
  File "parent.pyx", line 1047, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7734)
TypeError: no canonical coercion from <type 'numpy.ndarray'> to Symbolic Ring

------------Code-------------

#importing Libraries
import numpy as np
import scipy
import scipy.interpolate

#Importing data
data=np.loadtxt('/home/giova/data/work/electrodynamics/gmm/shell/maps/test.msc')

#getting the data
theta=data[:,0]
phi=data[:,1]
r=data[:,2];
#reshaping the data
side=sqrt(len(r));m_r=r.reshape(side,side);m_r=m_r.T;
v_theta=theta[::side]
v_phi=phi[0:side]

#building interpolating 2D function
f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)

#plotting the function
x,y = var('x,y')
plot3d(f,(x,0,2*pi),(y,0,pi))

#Spherical Plotting
x,y = var('x,y')
spherical_plot3d(f,(x,0,2*pi),(y,0,pi)) Does not work!!!!!

#Workaround
def g(s,t):
    out1=f(s,t).tolist()
    out2=out1[0][0]
    return out2
spherical_plot3d(g,(x,0,2*pi),(y,0,pi)) Does work!!!!!