开发者

How do i pass a generator yield statement to another function. -Python

i've been reading on this site and can't seem to find the specific answer i want. i've tried reading david beasly's slides on iteration and generators but still can't quite get the answer i'm looking for though the question seems simple. i'm running a clock-based simulation (Brian for neural networking) and i have a generator that is manipulating the outputs and adding them to a running sum (in order for there to be n exponential decay for a simple low-pass filter). i then want to take the outputs of these generators, at each time-step and then use them in another function to update some of the state variables, it says that since the item is of the generator type i cannot do this. code and explanation of code is as follows:

import numpy
our_range=numpy.arange(0*ms,duration+defaultclock.dt,defaultclock.dt)
a=our_range   
c=defaultclock.t   #this is a clock that is part of the program i'm running, it updates every #timestep and runs through the range given above

def sum_tau(neuron):            #this gives a running sum which i want to access (the alphas can be ignored as they are problem specific)
    for c in a:            #had to express it like this (with c and a) or it wouldn't run
        if c ==0:
            x=0
        elif defaultclock.still_running()==False:
            return
        else:
            x = x*(1-alpha) + p(neuron)*alpha
            print x
            yield x


#p(neuron) just takes some of the neurons variables and gives a number

b=sum_tau(DN)     #this is just to specify the neuron we're working on, problem specific

@network_operation
def x():
    b.next()

the @network_operation means that every clock timestep the function below will be executed, therefore updating the sum to it's required value. Now what i want to do here is update a value that is used for the simulation (where d is the output to another generator, not shown, but very similar to b) by typing:

ron= (d/(1-b))

However, it says i cannot use a generator object in this way, i have used print statements to determine that that b (and d) give the outputs i want every timestep (when the simulation is run) but I cannot seem to take these outputs and do anything with them. (more specifically unsupported operand type '-' for int and generator. i tried converting it to a number with float() but obviously this doesn't work for the same reason, i feel there must be a very simple solu开发者_开发知识库tion to my problem but i can't seem to find it. Thanks in advance.


"more specifically unsupported operand type '-' for int and generator" Take the hint.

You can't use a generator in a trivial formula. You have to "expand" it with a generator expression.

ron= (d/(1-b))

Has a generator b, right? b is not a "value". It's some kind of sequence of values.

So, you have to apply each value in the sequence to your formula.

ron = [ d/(1-x) for x in b ]

will get each value of the sequence and compute a new value.

(It's not clear if this is actually useful, since the original ron= (d/(1-b)) when b is a collection of values doesn't make much sense.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜