From dcc05dd1b89e7787ccd706575eb6a0ddb626a0fb Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Fri, 22 Feb 2019 16:18:27 +0100
Subject: [PATCH] Removed numpy from requirements

I think the inclusion of numpy headers was related to non used variables
forgotten from an intermediate implementation.
I just removed them. In this manner it is not necessary that numpy is
already installed when plumed extension is compiled.

Numpy would be anyway required to run our test.
---
 configure                  |  5 ++---
 configure.ac               |  3 +--
 macports/PortfilePython.in | 10 ++++++----
 python/README.rst          |  6 ++----
 python/plumed.pyx          | 12 +++++++-----
 python/setup.py            | 11 ++---------
 user-doc/Installation.md   |  6 ++----
 7 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/configure b/configure
index 113b119d9..cafcbc3fd 100755
--- a/configure
+++ b/configure
@@ -8760,8 +8760,8 @@ done
   then
     { $as_echo "$as_me:${as_lineno-$LINENO}: Python executable is $PYTHON_BIN" >&5
 $as_echo "$as_me: Python executable is $PYTHON_BIN" >&6;}
-    { $as_echo "$as_me:${as_lineno-$LINENO}: checking support for required python modules (setuptools, cython, numpy, subprocess, os, shutil)" >&5
-$as_echo_n "checking support for required python modules (setuptools, cython, numpy, subprocess, os, shutil)... " >&6; }
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking support for required python modules (setuptools, cython, subprocess, os, shutil)" >&5
+$as_echo_n "checking support for required python modules (setuptools, cython, subprocess, os, shutil)... " >&6; }
 testimport="
 from setuptools import setup
 from setuptools import Extension
@@ -8770,7 +8770,6 @@ import os
 import os.path
 import sys
 from shutil import copyfile
-import numpy
 from Cython.Build import cythonize
 "
     if echo "$testimport" | "$PYTHON_BIN" 1>/dev/null 2>/dev/null;  then
diff --git a/configure.ac b/configure.ac
index 73a717ce7..582cb0da1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -760,7 +760,7 @@ if test $python == true  ; then
   if test -n "$PYTHON_BIN"
   then
     AC_MSG_NOTICE([Python executable is $PYTHON_BIN])
-    AC_MSG_CHECKING([support for required python modules (setuptools, cython, numpy, subprocess, os, shutil)])
+    AC_MSG_CHECKING([support for required python modules (setuptools, cython, subprocess, os, shutil)])
 testimport="
 from setuptools import setup
 from setuptools import Extension
@@ -769,7 +769,6 @@ import os
 import os.path
 import sys
 from shutil import copyfile
-import numpy
 from Cython.Build import cythonize
 "
     if echo "$testimport" | "$PYTHON_BIN" 1>/dev/null 2>/dev/null;  then
diff --git a/macports/PortfilePython.in b/macports/PortfilePython.in
index e4a5aad8a..31d990ef6 100644
--- a/macports/PortfilePython.in
+++ b/macports/PortfilePython.in
@@ -41,12 +41,14 @@ if {${name} ne ${subport}} {
     build.env-append plumed_default_kernel=${prefix}/lib/libplumedKernel.dylib \
                      plumed_macports=yes
 
-    depends_build-append port:py${python.version}-cython
+    depends_build-append port:py${python.version}-cython \
+                         port:py${python.version}-setuptools
 
-    depends_lib-append   port:py${python.version}-numpy \
-                         path:${prefix}/lib/libplumedKernel.dylib:plumed
+    depends_lib-append   path:${prefix}/lib/libplumedKernel.dylib:plumed
+
+    depends_test-append port:py${python.version}-nose \
+                        port:py${python.version}-numpy
 
-    depends_test-append port:py${python.version}-nose
     test.cmd            nosetests-${python.branch}
     test.target
     test.run            yes
diff --git a/python/README.rst b/python/README.rst
index 30a0244b3..1d9f989c2 100644
--- a/python/README.rst
+++ b/python/README.rst
@@ -1,11 +1,8 @@
 Python wrappers for plumed
 ==========================
 
-Install using the following commands::
+Install using the following command::
 
-     # install dependencies
-     python -m pip install numpy
-     # install plumed
      python -m pip install plumed
 
 WARNING: You will need to also build and install the plumed library (see http://www.plumed.org) and set the environment variable
@@ -16,3 +13,4 @@ WARNING: You will need to also build and install the plumed library (see http://
      >>> import plumed
      >>> p=plumed.Plumed()
 
+CHANGES: See the PLUMED documentation.
diff --git a/python/plumed.pyx b/python/plumed.pyx
index 1a897863a..6b3d6ac5a 100644
--- a/python/plumed.pyx
+++ b/python/plumed.pyx
@@ -25,8 +25,12 @@
 #
 
 cimport cplumed  # This imports information from pxd file - including contents of this file here causes name clashes
-import numpy as np
-cimport numpy as np
+
+try:
+     import numpy as np
+     HAS_NUMPY=True
+except:
+     HAS_NUMPY=False
 
 cdef class Plumed:
      cdef cplumed.Plumed c_plumed
@@ -59,8 +63,6 @@ cdef class Plumed:
          cdef bytes py_bytes = key.encode()
          cdef char* ckey = py_bytes
          cdef char* cval 
-         cdef np.int_t[:] ibuffer
-         cdef np.float64_t[:] dbuffer
          if val is None :
             self.c_plumed.cmd( ckey, NULL )
          elif isinstance(val, (int,long) ):
@@ -71,7 +73,7 @@ cdef class Plumed:
             if key=="getBias" :
                raise ValueError("when using cmd with getBias option value must be a size one ndarray")
             self.cmd_float(ckey, val) 
-         elif isinstance(val, np.ndarray) : 
+         elif HAS_NUMPY and isinstance(val, np.ndarray) : 
             if( val.dtype=="float64" ):
                self.cmd_ndarray_real(ckey, val)
             elif( val.dtype=="int64" ) : 
diff --git a/python/setup.py b/python/setup.py
index 3e9df6f9a..929c3af12 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -76,18 +76,11 @@ def readme():
     with open('README.rst') as f:
         return f.read()
 
-try:
-    import numpy
-except:
-    print('Error: building ' + plumedname + ' requires numpy. Please install it first with pip install numpy')
-    sys.exit(-1)
-
-include_dirs=[numpy.get_include()]
 
 try:
-    include_dirs.append(os.environ["plumed_include_dir"])
+    include_dirs=[os.environ["plumed_include_dir"]]
 except:
-    include_dirs.append(".")
+    include_dirs=["."]
 
 # allow one to force using cython with env var plumed_force_cython=yes
 USE_CYTHON = False
diff --git a/user-doc/Installation.md b/user-doc/Installation.md
index 8497f60bc..0b31786b2 100644
--- a/user-doc/Installation.md
+++ b/user-doc/Installation.md
@@ -690,7 +690,7 @@ There are two ways to install Python wrappers.
 
 \subsection installingpython-inside Installing Python wrappers within PLUMED
 
-If `./configure` finds a `python` executable that also has the modules `numpy` and `cython` available, Python wrappers will be installed within `/prefix/lib/plumed/python`. In order to access them, you should add this directory to the environment variable `PYTHONPATH`. Notice that if your python interpreter has a different name you might have to pass it to `./configure` with `PYTHON_BIN=python3.6`. The whole thing would then be:
+If `./configure` finds a `python` executable that also has the `cython` module available, Python wrappers will be installed within `/prefix/lib/plumed/python`. In order to access them, you should add this directory to the environment variable `PYTHONPATH`. Notice that if your python interpreter has a different name you might have to pass it to `./configure` with `PYTHON_BIN=python3.6`. The whole thing would then be:
 
 ````
 ./configure PYTHON_BIN=python3.6 --prefix=$HOME/opt
@@ -707,7 +707,6 @@ Notice that in this manner you will have to commit to a specific python version
 If you use multiple python versions, you might find it easier to install the Python wrappers separately from PLUMED. The simplest way is to do it with `pip`:
 
 ````
-pip3.6 install --user numpy
 pip3.6 install --user plumed
 ````
 
@@ -724,8 +723,7 @@ If you want to install using pip the development version of the wrappers you sho
 the following commands:
 
 ````
-pip3.6 install --user cython # also cython is required in this case
-pip3.6 install --user numpy
+pip3.6 install --user cython # cython is required in this case
 cd plumed2/python
 make pip
 pip3.6 install --user .
-- 
GitLab