Error on Vector.as_numpy()

Error on Vector.as_numpy()

Postby leonelmd » Thu Jun 08, 2017 10:37 pm

Hi all,
I installed Neuron release 7.4 (1370:16a7055d4a86) on my account in a cluster under Red Hat Enterprise Linux Server release 7.3. I followed carefully the steps suggested in https://www.neuron.yale.edu/neuron/down ... pile_linux to get Python+Neuron, i.e., I used the --with-nrnpython option for ./configure. The cluster has Python 2.7.10.

"nrniv -python" worked as expected, and no error doing "from neuron import h".

Next, to be able to import Neuron into Python, I did:
Code: Select all
python setup.py install --prefix=$HOME/local

and set the PYTHONPATH:
Code: Select all
export PYTHONPATH=$PYTHONPATH:$HOME/local/lib64/python/site-packages

(I first attempted "lib" instead of "lib64", as suggested, but I was not able to import neuron in Python. I noted there was a lib64 folder with a site-packages subfolder).

Again, everything seemed fine:
Code: Select all
python
Python 2.7.10 (default, Nov 10 2015, 18:09:20)
[GCC Intel(R) C++ gcc 4.8 mode] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from neuron import h
Warning: no DISPLAY environment variable.
--No graphics will be displayed.
NEURON -- Release 7.4 (1370:16a7055d4a86) 2015-11-09
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits


I noted I was not able to import well-known modules in Python, e.g., numpy, but I solved this by doing:

Code: Select all
module load python/2.7.10

(I got the module name from "module avail").

However, I get the following error when trying to use the Vector.as_numpy() function, which my code -that works fine on two other different machines- heavily relies on:

Code: Select all
>>> import numpy as np
>>> from neuron import h
Warning: no DISPLAY environment variable.
--No graphics will be displayed.
NEURON -- Release 7.4 (1370:16a7055d4a86) 2015-11-09
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits

>>> v = h.Vector(5).indgen()
>>> v.as_numpy()
NEURON: Vector.as_numpy() error
near line 0
objref hoc_obj_[2]
                   ^
        Vector[0].as_numpy()
oc_restore_code tobj_count=1 should be 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: hoc error
>>>


Looking in the Forum, I thought this may have to do: viewtopic.php?f=2&t=2434 because I realized that I could not import numpy from Neuron (i.e., nrniv -python). So I reinstalled everything using the ./configure options indicated there and I guessed my correct Python paths using "module show python/2.7.10". I was able now to import numpy in "nrniv -python". However, the error with Vector.as_numpy() persisted.

Someone appeared to have a similar problem (viewtopic.php?f=5&t=3548), but I'm hesitant to install an alpha version since the combination Python 2.7 + Neuron 7.4 works perfectly fine in two other machines (both Ubuntu though).

Any ideas on what the problem might be?

Thanks a lot!
leonelmd
 
Posts: 9
Joined: Tue Aug 19, 2014 3:12 pm

Re: Error on Vector.as_numpy()

Postby hines » Fri Jun 09, 2017 8:45 am

The system makes a connection between Vector and numpy via ctypes in the lib64/python/neuron/__init__.py module
What has failed is the try...except fragment in that file that contains the line
Code: Select all
  set_vec_as_numpy = nrn_dll_sym('nrnpy_set_vec_as_numpy')

More diagnostic information would be forthcoming if you replaced the try: and except: lines
with
Code: Select all
if True:

and
Code: Select all
else:

(then you don't need to change the indentation.
My guess is that the underlying reason for the failure is the failure of
def nrn_dll(printpath=False):
"""Return a ctypes object corresponding to the NEURON library.
More recent implementations of nrn_dll are more robust, in the context of launching python, through the fragment
Code: Select all
    try:
        #extended? if there is a __file__, then use that
        if printpath: print ("hoc.__file__ %s" % hoc.__file__)
        the_dll = ctypes.cdll[hoc.__file__]
        return the_dll
    except:
        pass

which you should put just after the
Code: Select all
    import glob

line in def nrn_dll
hines
Site Admin
 
Posts: 1420
Joined: Wed May 18, 2005 3:32 pm

Re: Error on Vector.as_numpy()

Postby leonelmd » Fri Jun 09, 2017 9:36 am

Thanks a lot for your reply, Michael.
After replacing the try-except with the if-else, I get the following:
Code: Select all
from neuron import h
Warning: no DISPLAY environment variable.
--No graphics will be displayed.
NEURON -- Release 7.4 (1370:16a7055d4a86) 2015-11-09
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lmedina/.local/lib64/python2.7/site-packages/neuron/__init__.py", line 479, in <module>
    set_vec_as_numpy = nrn_dll_sym('nrnpy_set_vec_as_numpy')
  File "/home/lmedina/.local/lib64/python2.7/site-packages/neuron/__init__.py", line 394, in nrn_dll_sym
    dll = nrn_dll()
  File "/home/lmedina/.local/lib64/python2.7/site-packages/neuron/__init__.py", line 454, in nrn_dll
    raise Exception('unable to connect to the NEURON library')
Exception: unable to connect to the NEURON library
>>> v=h.Vector()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'h' is not defined


So, it seems it does have to do with nrn_dll(). However, I'm not sure how to fix this since I don't have an "import glob" in my def nrn_dll (BTW, I noted that in the machine where I don't have issues, I have the same def nrn_dll):

Code: Select all
def nrn_dll(printpath=False):
    """Return a ctypes object corresponding to the NEURON library.

    .. warning::

        This provides access to the C-language internals of NEURON and should
   be used with care.
    """
    import ctypes
    import os
    import platform

    neuron_home = os.path.split(os.path.split(h.neuronhome())[0])[0]

    success = False
    base_path = os.path.join(neuron_home, 'lib' , 'python', 'neuron', 'hoc')
    for extension in ['', '.dll', '.so', '.dylib']:
        try:
            the_dll = ctypes.cdll[base_path + extension]
            if printpath : print base_path + extension
            success = True
        except:
            pass
        if success: break
    else:
   raise Exception('unable to connect to the NEURON library')
    return the_dll


I'm sorry I'm a bit uncertain on how to insert/update the lines you indicated.

Thanks
leonelmd
 
Posts: 9
Joined: Tue Aug 19, 2014 3:12 pm

Re: Error on Vector.as_numpy()

Postby hines » Fri Jun 09, 2017 10:39 am

put it after the last of the list of imports. in your case, import platform
hines
Site Admin
 
Posts: 1420
Joined: Wed May 18, 2005 3:32 pm

Re: Error on Vector.as_numpy()

Postby leonelmd » Fri Jun 09, 2017 11:11 am

Thank you very much, Michael! This solved the problem; I can now use Vector.as_numpy() :)
Saludos
leonelmd
 
Posts: 9
Joined: Tue Aug 19, 2014 3:12 pm


Return to UNIX/Linux

Who is online

Users browsing this forum: No registered users and 2 guests