Magic in Brian

brian.magic_return(f)

Decorator to ensure that the returned object from a function is recognised by magic functions

Usage example:

@magic_return
def f():
    return PulsePacket(50*ms, 100, 10*ms)

Explanation

Normally, code like the following wouldn’t work:

def f():
    return PulsePacket(50*ms, 100, 10*ms)
pp = f()
M = SpikeMonitor(pp)
run(100*ms)
raster_plot()
show()

The reason is that the magic function run() only recognises objects created in the same execution frame that it is run from. The magic_return() decorator corrects this, it registers the return value of a function with the magic module. The following code will work as expected:

@magic_return
def f():
    return PulsePacket(50*ms, 100, 10*ms)
pp = f()
M = SpikeMonitor(pp)
run(100*ms)
raster_plot()
show()

Technical details

The magic_return() function uses magic_register() with the default level=1 on just the object returned by a function. See details for magic_register().

brian.magic_register(*args, **kwds)

Declare that a magically tracked object should be put in a particular frame

Standard usage

If A is a tracked class (derived from InstanceTracker), then the following wouldn’t work:

def f():
    x = A('x')
    return x
objs = f()
print get_instances(A,0)[0]

Instead you write:

def f():
    x = A('x')
    magic_register(x)
    return x    
objs = f()
print get_instances(A,0)[0]

Definition

Call as:

magic_register(...[,level=1])

The ... can be any sequence of tracked objects or containers of tracked objects, and each tracked object will have its instance id (the execution frame in which it was created) set to that of its parent (or to its parent at the given level). This is equivalent to calling:

x.set_instance_id(level=level)

For each object x passed to magic_register().

See also

Projects with multiple files or functions
Describes difficulties and solutions for using magic functions on projects with multiple files or functions.