1

I have a script network.py that includes the following:

from brian import *
. . .
simulation_clock = Clock(dt=dt)
. . .
pop_vector = NeuronGroup(1, model=eqs_pop_vector)

This script raises a TypeError, because simulation_clock is not passed as an argument to NeuronGroup.

However, if I split up my code as follows

$ cat simclock.py
simulation_clock = Clock(dt=dt)
$ cat network.py
from brian import *
. . .
execfile('simclock.py')
. . .
pop_vector = NeuronGroup(1, model=eqs_pop_vector)

it runs without a hitch -- i.e., no TypeError.

The answer to my question may in part have to do with the particulars of NeuronGroup, but it's also more general than that: Why, in general, would the same exact code -- in this case, simulation_clock = Clock(dt=dt) -- have a different effect when separated from the rest of the code and called with execfile than when in the same script as the rest of the code?

With regard to the particulars of NeuronGroup, the following function is called (with the default keyword arguments) when no Clock is supplied as an argument, in order to find defined instances of Clock:

def find_instances(instancetype, startlevel=1, all=False):
    """Find first instances of a given class in the stack

    See documentation for module Brian.magic
    """
    # Note that we start from startlevel+1 because startlevel means from the calling function's point of view 
    for level in range(startlevel + 1, len(getouterframes(currentframe()))):
        objs, names = get_instances(instancetype, level, all=all)
        if len(objs):
            return (objs, names)
    return ([], [])

For some reason this function cannot find instances of Clock defined in separate files called with execfile. So I guess this is a bug in brian that raises a general question. (I'll delete this if the question seems to the community to be too specific.)

abcd
  • 10,215
  • 15
  • 51
  • 85
  • Is it possible that there is more than one `Clock` class. When you import everything using `*` instead of being explicit, sometimes you override other classes. I would try only importing the things you need from the `brian` module and any other module you are using. – Alejandro Mar 21 '15 at 23:55
  • Totally agree. I never import everything; this is code I was given by someone else. There is only one `Clock` class, so that doesn't explain this. The problem seems to have to do with `NeuronGroup`'s ability to find instances of `Clock`. There is a function `find_instances` that `NeuronGroup`'s `__init__` calls that searches the stack for instances. I'll post an answer once I've gotten to the bottom of this (if someone else hasn't beaten me to it). – abcd Mar 22 '15 at 00:12

0 Answers0