[python-arrayfire] 142/250: FEAT: Adding interop module

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:41 UTC 2016


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

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 6e105e163226c5d0d7073eb482c0b88c7cd6ddf2
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Nov 11 12:13:19 2015 -0500

    FEAT: Adding interop module
---
 arrayfire/__init__.py |  2 ++
 arrayfire/interop.py  | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/arrayfire/__init__.py b/arrayfire/__init__.py
index e719c13..1c65fb7 100644
--- a/arrayfire/__init__.py
+++ b/arrayfire/__init__.py
@@ -52,9 +52,11 @@ from .vision     import *
 from .graphics   import *
 from .bcast      import *
 from .index      import *
+from .interop    import *
 
 # do not export default modules as part of arrayfire
 del ct
 del inspect
 del numbers
 del os
+del np
diff --git a/arrayfire/interop.py b/arrayfire/interop.py
new file mode 100644
index 0000000..aa27ac6
--- /dev/null
+++ b/arrayfire/interop.py
@@ -0,0 +1,59 @@
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+"""
+Interop with other python packages.
+
+This module provides interoperability with the following python packages.
+
+     1. numpy
+"""
+
+from .array import *
+from .data import reorder
+
+try:
+    import numpy as np
+    AF_NP_FOUND=True
+
+    def np_to_af_array(np_arr):
+        """
+        Convert numpy.ndarray to arrayfire.Array.
+
+        Parameters
+        ----------
+        np_arr  : numpy.ndarray()
+
+        Returns
+        ---------
+        af_arry  : arrayfire.Array()
+        """
+        if (np_arr.flags['F_CONTIGUOUS']):
+            return Array(np_arr.ctypes.data, np_arr.shape, np_arr.dtype.char)
+        elif (np_arr.flags['C_CONTIGUOUS']):
+            if np_arr.ndim == 1:
+                return Array(np_arr.ctypes.data, np_arr.shape, np_arr.dtype.char)
+            elif np_arr.ndim == 2:
+                shape = (np_arr.shape[1], np_arr.shape[0])
+                res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
+                return reorder(res, 1, 0)
+            elif np_arr.ndim == 3:
+                shape = (np_arr.shape[2], np_arr.shape[1], np_arr.shape[0])
+                res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
+                return reorder(res, 2, 1, 0)
+            elif np_arr.ndim == 4:
+                shape = (np_arr.shape[3], np_arr.shape[2], np_arr.shape[1], np_arr.shape[0])
+                res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
+                return reorder(res, 3, 2, 1, 0)
+            else:
+                raise RuntimeError("Unsupported ndim")
+        else:
+            return np_to_af_array(np.ascontiguousarray(np_arr))
+except:
+    AF_NP_FOUND=False

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



More information about the debian-science-commits mailing list