Ask Your Question
0

2D interpolating function from numpy arrays to spherical-plot3d

asked 2011-10-31 07:37:11 +0100

Giovanni gravatar image

updated 2015-01-17 22:35:31 +0100

FrédéricC gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2011-10-31 21:59:45 +0100

Jason Grout gravatar image

updated 2011-11-01 11:03:14 +0100

It's because your f function above is returning a numpy array, rather than a number.

sage: f(1,2)
array([[ 0.45712591]])
sage: f(1,2)[0,0]
0.45712590955624677

So you need to extract that number out. You did it in your last example above. Here's another way:

spherical_plot3d(lambda x,y: f(x,y)[0,0],(x,0,2*pi),(y,0,pi))

Here is yet another way, since numpy arrays with a single value like that can be cast to floats.

spherical_plot3d(lambda x,y: float(f(x,y)),(x,0,2*pi),(y,0,pi))
edit flag offensive delete link more

Comments

Thanks for the answer, the lambda form is especially useful. Nevertheless the fact that plot3d works and spherical_plot3d doesn't is odd

Giovanni gravatar imageGiovanni ( 2011-11-01 04:57:19 +0100 )edit
1

Yes, you're right. I just posted a patch to fix this: http://trac.sagemath.org/sage_trac/ticket/11974 The patch needs review now.

Jason Grout gravatar imageJason Grout ( 2011-11-01 11:01:01 +0100 )edit
1

answered 2011-10-31 10:56:30 +0100

kcrisman gravatar image

updated 2011-10-31 10:59:11 +0100

Hmm, this is a tiny bit odd, since the code for spherical_plot3d is just

return plot3d(f, urange, vrange, transformation=Spherical('radius', ['azimuth', 'inclination']), **kwds)

So probably the transformation is having trouble handling something nonsymbolic (the Scipy interpolation function).

Can you give a toy version of your data (since we obviously can't reproduce this faithfully as it stands) in an edit to your question? It makes looking at the traceback easier. (For sure do please include the full error message, though, as that will help a lot.)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-10-31 07:37:11 +0100

Seen: 1,433 times

Last updated: Nov 01 '11