abcd
abcd

Reputation: 10791

Different performance when same code is put in separate script and executed with execfile

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.

Upvotes: 1

Views: 49

Answers (0)

Related Questions