[Debian-astro-commits] [gyoto] 185/221: Provide casting from NumPy ndarray to gyoto.array_double. All Gyoto methods take the latter as argument.

Thibaut Jean-Claude Paumard thibaut at moszumanska.debian.org
Fri May 22 20:52:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

thibaut pushed a commit to branch master
in repository gyoto.

commit edeec3e193af94c5b948ce93d878022d7d01992b
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date:   Fri Jan 9 15:19:35 2015 +0100

    Provide casting from NumPy ndarray to gyoto.array_double. All Gyoto methods take the latter as argument.
---
 python/example-mpi.py |  6 ++--
 python/example.py     | 85 +++++++++++++++++++++++++++------------------------
 python/gyoto.i        | 53 ++++++++++++++++----------------
 3 files changed, 74 insertions(+), 70 deletions(-)

diff --git a/python/example-mpi.py b/python/example-mpi.py
index f69020f..afdabe7 100644
--- a/python/example-mpi.py
+++ b/python/example-mpi.py
@@ -27,9 +27,9 @@ distance=numpy.zeros((res, res), dtype=float)
 
 # Store array pointers in AstrobjProperties
 aop=gyoto.AstrobjProperties()
-aop.Intensity(intensity)
-aop.EmissionTime(time)
-aop.MinDistance(distance)
+aop.intensity=gyoto.array_double.fromnumpy2(intensity)
+aop.time=gyoto.array_double.fromnumpy2(time)
+aop.distance=gyoto.array_double.fromnumpy2(distance)
 
 # Prepare Coord2dSet to select what Photons to launch
 ii=gyoto.Range(1, res, 1)
diff --git a/python/example.py b/python/example.py
index 7e8c461..1c87742 100644
--- a/python/example.py
+++ b/python/example.py
@@ -23,22 +23,30 @@ ph=gyoto.Photon()
 ph.setInitialCondition(sc.metric(), sc.astrobj(), sc.screen(), 0., 0.)
 ph.hit()
 n=ph.get_nelements()
-t=gyoto.array_double(n)
-r=gyoto.array_double(n)
-theta=gyoto.array_double(n)
-phi=gyoto.array_double(n)
-ph.getCoord(t, r, theta, phi)
-
-t2=numpy.zeros(n)
-r2=numpy.zeros(n)
-theta2=numpy.zeros(n)
-phi2=numpy.zeros(n)
 
-for i in range(0, n):
-    t2[i]=t[i]
-    r2[i]=r[i]
-    theta2[i]=theta[i]
-    phi2[i]=phi[i]
+# Gyoto supports simple C-like arrays through the gyoto.array_double
+# and gyoto.array_unsigned_long classes. Beware that this type does
+# not provide any safeguards, it is quite easy to get it to SEGFAULT.
+#
+# To use NumPy arrays, create the arrays using numpy, then cast them
+# using the fromnumpyN static methods. The actual array will be
+# deleted with the NumPy variable: don't use the array_double()
+# variable (for anyting else that destroying it) past that time.
+
+# Create NumPy arrays
+t2=numpy.ndarray(n)
+r2=numpy.ndarray(n)
+theta2=numpy.ndarray(n)
+phi2=numpy.ndarray(n)
+
+# Cast them to array_double type
+t=gyoto.array_double.fromnumpy1(t2)
+r=gyoto.array_double.fromnumpy1(r2)
+theta=gyoto.array_double.fromnumpy1(theta2)
+phi=gyoto.array_double.fromnumpy1(phi2)
+
+# Call Gyoto method that takes pointers as argument:
+ph.getCoord(t, r, theta, phi)
 
 plt.plot(t2, r2)
 plt.show()
@@ -50,22 +58,18 @@ wl=gyoto_std.Star(sc.astrobj())
 wl.xFill(1000)
 
 n=wl.get_nelements()
-t=gyoto.array_double(n)
-r=gyoto.array_double(n)
-theta=gyoto.array_double(n)
-phi=gyoto.array_double(n)
-wl.getCoord(t, r, theta, phi)
 
-t2=numpy.zeros(n)
-r2=numpy.zeros(n)
-theta2=numpy.zeros(n)
-phi2=numpy.zeros(n)
+t2=numpy.ndarray(n)
+r2=numpy.ndarray(n)
+theta2=numpy.ndarray(n)
+phi2=numpy.ndarray(n)
 
-for i in range(0, n):
-    t2[i]=t[i]
-    r2[i]=r[i]
-    theta2[i]=theta[i]
-    phi2[i]=phi[i]
+t=gyoto.array_double.fromnumpy1(t2)
+r=gyoto.array_double.fromnumpy1(r2)
+theta=gyoto.array_double.fromnumpy1(theta2)
+phi=gyoto.array_double.fromnumpy1(phi2)
+
+wl.getCoord(t, r, theta, phi)
 
 plt.plot(r2*numpy.cos(phi2), r2*numpy.sin(phi2))
 plt.show()
@@ -77,9 +81,10 @@ intensity=numpy.zeros((res, res), dtype=float)
 time=numpy.zeros((res, res), dtype=float)
 distance=numpy.zeros((res, res), dtype=float)
 aop=gyoto.AstrobjProperties()
-aop.Intensity(intensity)
-aop.EmissionTime(time)
-aop.MinDistance(distance)
+
+aop.intensity=gyoto.array_double.fromnumpy2(intensity)
+aop.time=gyoto.array_double.fromnumpy2(time)
+aop.distance=gyoto.array_double.fromnumpy2(distance)
 
 ii=gyoto.Range(1, res, 1)
 jj=gyoto.Range(1, res, 1)
@@ -107,7 +112,7 @@ jj=gyoto.Range(1, res, 1)
 grid=gyoto.Grid(ii, jj, "\rj = ")
 
 aop=gyoto.AstrobjProperties()
-aop.Spectrum(spectrum)
+aop.spectrum=gyoto.array_double.fromnumpy3(spectrum)
 aop.offset=res*res
 
 sc.rayTrace(grid, aop)
@@ -148,7 +153,7 @@ grid=gyoto.Grid(ii, jj, "\rj = ")
 ipct=numpy.zeros((res, res, 16), dtype=float)
 
 aop=gyoto.AstrobjProperties()
-aop.ImpactCoords(ipct)
+aop.impactcoords=gyoto.array_double.fromnumpy3(ipct)
 aop.offset=res*res
 
 sc.rayTrace(grid, aop)
@@ -165,14 +170,14 @@ a=gyoto.Angles(buf)
 d=gyoto.RepeatAngle(screen.fieldOfView()*-0.5, N)
 bucket=gyoto.Bucket(a, d)
 
-ipct=numpy.zeros((N, 1, 16), dtype=float)
+ipct=numpy.zeros((N, 16), dtype=float)
 
 aop=gyoto.AstrobjProperties()
-aop.ImpactCoords(ipct)
+aop.impactcoords=gyoto.array_double.fromnumpy2(ipct)
 aop.offset=N
 
 sc.rayTrace(bucket, aop)
-plt.plot(buf, ipct[:,0,0])
+plt.plot(buf, ipct[:,0])
 plt.show()
 
 # Trace the diagonal of the above using i and j. The Range and Indices
@@ -185,15 +190,15 @@ ii=gyoto.Indices(ind)
 jj=gyoto.Range(1, res, 1)
 bucket=gyoto.Bucket(ii, jj)
 
-ipct=numpy.zeros((res, 1, 16), dtype=float)
+ipct=numpy.zeros((res, 16), dtype=float)
 
 aop=gyoto.AstrobjProperties()
-aop.ImpactCoords(ipct)
+aop.impactcoords=gyoto.array_double.fromnumpy2(ipct)
 aop.offset=res
 
 sc.rayTrace(bucket, aop)
 
-t=numpy.clip(ipct[:,0,0], a_min=-200, a_max=0)
+t=numpy.clip(ipct[:,0], a_min=-200, a_max=0)
 plt.plot(t)
 plt.show()
 
diff --git a/python/gyoto.i b/python/gyoto.i
index 469cdad..97b8d69 100644
--- a/python/gyoto.i
+++ b/python/gyoto.i
@@ -244,19 +244,41 @@ GyotoSmPtrTypeMapClassDerived(Astrobj, Properties);
 // Non-Gyoto typemaps:
 // Handle std::string
 %include "std_string.i";
+
 // Handle std::vector<double> and <unsigned long int>
 %include "std_vector.i";
 %template(vector_double) std::vector<double>;
 %template(vector_unsigned_long) std::vector<unsigned long>;
-// Handle generic C arrays using a class-like interface
-%include "carrays.i"
-%array_class(double, array_double);
-%array_class(double, array_unsigned_long);
+
 // Handle some arrays as NumPy arrays
 %include "numpy.i";
 %numpy_typemaps(size_t, NPY_ULONG , size_t);
 %numpy_typemaps(double, NPY_DOUBLE, size_t);
 
+// Handle generic C arrays using a class-like interface
+%include "carrays.i"
+%array_class(double, array_double);
+%array_class(double, array_unsigned_long);
+// Provide conversion between generic C arrays and NumPy ndarrays
+%define ExtendArrayNumPy(name, type)
+%extend name {
+  static name* fromnumpy1(type* IN_ARRAY1, size_t DIM1) {
+    return static_cast< name * >(IN_ARRAY1);
+  }
+  static name* fromnumpy2(type* IN_ARRAY2, size_t DIM1, size_t DIM2) {
+    return static_cast< name * >(IN_ARRAY2);
+  }
+  static name* fromnumpy3(type* IN_ARRAY3, size_t DIM1, size_t DIM2, size_t DIM3) {
+    return static_cast< name * >(IN_ARRAY3);
+  }
+  static name* fromnumpy4(type* IN_ARRAY4, size_t DIM1, size_t DIM2, size_t DIM3, size_t DIM4) {
+    return static_cast< name * >(IN_ARRAY4);
+  }
+};
+%enddef
+
+ExtendArrayNumPy(array_double, double);
+
 // ******** INTERFACE ******** //
 // Here starts the actual parsing of the various header files
 
@@ -330,16 +352,6 @@ GyotoSmPtrClassGeneric(Astrobj)
 
 GyotoSmPtrClassDerived(Astrobj, ThinDisk)
 
-%define _PAccessor2(member, setter)
-  void setter(double *IN_ARRAY2, size_t DIM1, size_t DIM2) {
-    $self->member = IN_ARRAY2;
-  }
-%enddef
-%define _PAccessor3(member, setter)
-void setter(double *IN_ARRAY3, size_t DIM1, size_t DIM2, size_t DIM3) {
-    $self->member = IN_ARRAY3;
-  }
-%enddef
 %define _PConverter(member, method)
   Gyoto::Units::Converter * method() {
   Gyoto::Units::Converter * res = $self->member;
@@ -349,19 +361,6 @@ void setter(double *IN_ARRAY3, size_t DIM1, size_t DIM2, size_t DIM3) {
   }
 %enddef
 %extend Gyoto::Astrobj::Properties{
-  _PAccessor2(intensity, Intensity)
-  _PAccessor3(binspectrum, BinSpectrum)
-  _PAccessor2(distance, MinDistance)
-  _PAccessor2(first_dmin, FirstDistMin)
-  _PAccessor3(impactcoords, ImpactCoords)
-  _PAccessor2(redshift, Redshift)
-  _PAccessor3(spectrum, Spectrum)
-  _PAccessor2(time, EmissionTime)
-  _PAccessor2(user1, User1)
-  _PAccessor2(user2, User2)
-  _PAccessor2(user3, User3)
-  _PAccessor2(user4, User4)
-  _PAccessor2(user5, User5)
   _PConverter(binspectrum_converter_, binSpectrumConverter)
   _PConverter(intensity_converter_, intensityConverter)
   _PConverter(spectrum_converter_, spectrumConverter)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-astro/packages/gyoto.git



More information about the Debian-astro-commits mailing list