Import class in transfer function


#1

Hey guys,
what is the clean way to import a python class in a transfer function?
should it be installed as a python package in the nrp virtual env?

cheers,
camilo


#2

@kennysharma how do you do it for tensorflow?


#3

Hey Camilo,

You can add it anywhere in your PYTHONPATH, so either in the system installed pip packages or in your NRP virtualenv (which may not be a good idea if you want to keep your install clean).

For tensorflow (which requires a higher numpy version and can’t explicitly be added to the PYTHONPATH before launching), I use this in a transfer function to make its virtualenv accessible:

    import site
    site.addsitedir('/home/kenny/Desktop/tensorflow_env/lib/python2.7/site-packages')

It really just depends on how you want to do it for your experiment, I don’t think we have explicit guidance right now.

Kenny


#4

Hey Guys,

i have the folder containing my experiment in my PYTHONPATH and both files, the one containing the transfer function as well as the file to import, in the same folder (no subfolders). But i cant get the interpreter to find it, as it always runs into a “global name classname is not defined” error. What am i missing?

Thanks in advance,
Ben


#5

Hi Ben,

Is the transfer function trying to import the second file? If so, could you copy here the content of this function?

Best regards,
Luc


#6

Hi Luc,
yes, the transfer function is importing another file, the setup looks like this (simplified):

import numpy as np
from dvs_msgs.msg import EventArray
import mapping_generator


@nrp.MapRobotSubscriber('dvs', Topic('/dvs_camera/events', EventArray))
@nrp.MapRobotPublisher('dvs_mapped', Topic('/dvs_mapped', sensor_msgs.msg.Image))
@nrp.MapSpikeSource('input_neurons', nrp.map_neurons(
range(0,nrp.config.brain_root.input_shape[0] * nrp.config.brain_root.input_shape[1]),
lambda i: nrp.brain.sensors[i]), nrp.dc_source)
@nrp.Robot2Neuron()

def map(t, dvs, input_neurons, dvs_mapped):
#some unrelated code here

mapping = mapping_generator.from_img(event_msg.width, event_msg.heigth, 6)

and the imported file looks like this:

def from_img(width, height, layers):
# cropped the code

return mapping

Thanks in advance,
Ben


#7

@kennysharma Alternatively, you can actually activate the platform_venv, then upgrade pip, then install tensorflow and finally downgrade pip again.


#8

Hi Ben,

I can reproduce your problem very easily. It was quite a surprise to me that the import statements located above the decorator of the transfer function definition have no effect in the body of the function (but they can be used to declare decorator parameters).

I hope that Georg, or anyone acknowledgeable for the transfer function framework, will comment on that issue soon (many import statements in our examples are actually useless!).

In your case, the fix is simple:

....
def map(t, dvs, input_neurons, dvs_mapped):
#some unrelated code here
import mapping_generator
mapping = mapping_generator.from_img(event_msg.width, event_msg.heigth, 6)
...

Best regards,
Luc

PS: I created the Jira issue https://bbpteam.epfl.ch/project/issues/browse/NRRPLT-5653 in order to track this import problem.


#9

Thanks a lot Luc,

kind of weird that nobody had that problem before though… but well, better to find it now i guess.

Thanks again,
Ben


#10

hi there,
we have figured out different ways, as described here, to import user code in a transfer function.
but is there a recommended way?

cheers


#12

Hello,

I’d also like to know if there is a recommended way.


#13

Hello everyone,

As of now, there is a new tab in the editors widget called dependencies. There you can upload your custom python file and it gets automatically appended to the PYTHONPATH. Using it then from within a transfer function is straightforward as described in the examples above, by just using an import statement.

Regards,
Manos