0

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 solution to my problem but i can't seem to find it. Thanks in advance.

4
  • 1
    1-b cannot work as b is a generator. You need to do 1-b.next() or value = b.next(); 1-value. Commented Sep 27, 2011 at 3:38
  • Please learn to use the shift key. "I" refers to yourself. It's important to spell correctly. Commented Sep 27, 2011 at 4:10
  • @StevenRumbalski Thanks Steven, this is indeed what I was looking for. unfortunately I now have a new problem as the network_operation decorator I need to use to apply this update step to the generator in sync with the simulation clock does not allow a return value (it show's up as "None") is there any way to return a value to the highest order namespace in order to get this value and not allow the generator to get out of sync with the simulation (so that in your notation I can pass the "value" up to the parent namespace and take it outside of the namespace of the decorator and function? Commented Sep 27, 2011 at 5:28
  • figured out the problem using globals, thanks to everyone for the help Commented Sep 27, 2011 at 6:01

1 Answer 1

2

"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.)

Sign up to request clarification or add additional context in comments.

2 Comments

yeah, at each clock "tick" I apply b.next() to get the next value, and at each clock tick I want to re-evaluate ron so I cannot evaluate it as a list using a for x in b method as you did. I then need to feed this ron back into the simulation before the next clock tick (so I cannot evaluate them all at once but need to do it step by step). Thank you for your answer though.
"so I cannot evaluate it as a list using a for x in b method as you did". You can say that. But it makes no sense. b is a list. A list. You must treat b as a list. If you want to use next(b) , then you must update your question to explain what you're doing. Your question says ron= (d/(1-b)) which does not use b as a list. I'm answering your question. If you don't like this answer, you must update the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.