[ismrmrd] 235/281: add Numpy friendly accessor for getTraj

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:01:19 UTC 2015


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

ghisvail-guest pushed a commit to annotated tag ismrmrd0.5
in repository ismrmrd.

commit 876e6ec96e7fbfd658dad425f3ca537ea2860687
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Tue Jun 3 18:22:09 2014 +0100

    add Numpy friendly accessor for getTraj
    
    Allows to read the trajectory within Python using Numpy arrays. The
    implementation follows what has already been written for getData.
    Without this patch, getTraj() returns a valarray which Python fails
    to translate into a meaningful data structure.
---
 bindings/python/ismrmrd_python.i | 53 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/bindings/python/ismrmrd_python.i b/bindings/python/ismrmrd_python.i
index c564a1f..8753046 100644
--- a/bindings/python/ismrmrd_python.i
+++ b/bindings/python/ismrmrd_python.i
@@ -157,11 +157,62 @@
         memcpy(&(data[0]), raw, nbytes);
         $self->setData(data);
     }
+
+    PyObject* getTraj()
+    {
+        npy_intp dims[] = { $self->getTraj().size() };
+        PyObject *array = PyArray_New(&PyArray_Type, 1, dims, NPY_FLOAT,
+                NULL, NULL, 0, NPY_ARRAY_FARRAY, NULL);
+
+        char *raw = PyArray_BYTES((PyArrayObject*)array);
+        int data_size = PyArray_ITEMSIZE((PyArrayObject*)array);
+        npy_intp raw_size = PyArray_NBYTES((PyArrayObject*)array);
+
+        std::valarray<float> traj = $self->getTraj();
+        memcpy(raw, &traj[0], dims[0]*data_size);
+
+        return array;
+    }
+
+    void setTraj(PyObject *in_array)
+    {
+        if (!PyArray_Check((PyArrayObject*)in_array) || !PyArray_ISFLOAT((PyArrayObject*)in_array)) {
+            set_err("Argument to setData is not a numpy float array\n");
+            return;
+        } else if (!PyArray_ISBEHAVED_RO((PyArrayObject*)in_array)) {
+            set_err("Argument to setData must be aligned\n");
+            return;
+        } else if (!PyArray_ISONESEGMENT((PyArrayObject*)in_array)) {
+            set_err("Data is not one segment\n");
+            return;
+        }
+
+        PyObject *array = NULL;
+        /* if array is C-style contiguous, make a Fortran-style contiguous copy */
+        if (PyArray_ISCONTIGUOUS((PyArrayObject*)in_array)) {
+            array = PyArray_NewCopy((PyArrayObject*)in_array, NPY_FORTRANORDER);
+        } else {
+            array = in_array;
+        }
+
+        int ndim = PyArray_NDIM((PyArrayObject*)array);
+        int itemsize = PyArray_ITEMSIZE((PyArrayObject*)array);
+        npy_intp nbytes = PyArray_NBYTES((PyArrayObject*)array);
+        npy_intp nelements = PyArray_SIZE((PyArrayObject*)array);
+        npy_intp* dims = PyArray_DIMS((PyArrayObject*)array);
+        npy_intp* strides = PyArray_STRIDES((PyArrayObject*)array);
+        void *raw = PyArray_DATA((PyArrayObject*)array);
+
+        std::valarray<float> traj(0.0, nelements);
+        memcpy(&(traj[0]), raw, nbytes);
+        $self->setTraj(traj);
+    }
 }
 
 %ignore ISMRMRD::Acquisition::getData;
 %ignore ISMRMRD::Acquisition::setData;
-
+%ignore ISMRMRD::Acquisition::getTraj;
+%ignore ISMRMRD::Acquisition::setTraj;
 
 // IsmrmrdDataset
 

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



More information about the debian-science-commits mailing list