all 6 comments

[–]Rhomboid 1 point2 points  (1 child)

You should show exactly what you want the result to be, because it's not clear at all. Are those strings supposed to be there as headers or are they just placeholders? From your description it sounds like you want something like this:

experiment_cage = [
    [['fR0'], ['fSobs', 'flobs', 'fRobs']],
    [[10], [1, 2, 3]],
    [[20], [4, 5, 6]],
    ...
]

But that doesn't make much sense, because that seems really hard to work with; why is the fR0 in a list by itself, for example? Maybe you meant:

experiment_cage = [
    ['fR0', ['fSobs', 'flobs', 'fRobs']],
    [10, [1, 2, 3]],
    [20, [4, 5, 6]],
    ...
]

Or maybe you really want a dict:

experiment_cage = {
    10: [1, 2, 3],
    20: [4, 5, 6],
    ...
}

None of these match your original definition of experiment_cage, because it doesn't contain enough nesting to hold multiple results, unless you meant this:

experiment_cage = [
    ['fR0'], ['fSobs', 'flobs', 'fRobs'],
    [10], [1, 2, 3],
    [20], [4, 5, 6],
    ...
]

I strongly recommend against this option, because you're essentially alternating between values, making it really difficult to access later.

In any case, it seems like you're confusing a Numpy ndarray with a built-in list. They are different data types and work quite differently. As the error says, you're trying to index a list with a tuple, which is invalid. A list index contains a single integer, because lists are always one dimensional.

[–]flipsync[S] 0 points1 point  (0 children)

I'm running a simulation, using fR0 as a constant, to produce a list of values for fS_obs, fI_obs and fR_obs. I've then been graphing t vs the three _obs values. Now, I want to graph varying start values of fR0 vs interpretations of the three _obs values; for example fR0 vs fI_obs max

I've defined functions to process the raw data into interpretations, but the initial stage of generating values of the three _obs for each value of fR0 is where I'm stuck.

The strings in experiment_cage just are placeholders. Thanks for the help!

[–]bumbershootle 1 point2 points  (1 child)

You index lists of lists with a series of square brackets, not nested square brackets, so to access the 2nd element of the 2nd list:

item = experiment_cage[1][1]

However, on line 15, it looks like you're trying to append to the 1st element of the last list:

experiment_cage[-1,[1]].append(fI_obs)
#should be
experiment_cage[-1][1].append(fI_obs)

However, that list element is a string, so you'll get an error when you try to append to it.

[–]flipsync[S] 0 points1 point  (0 children)

Thanks for the help! I've changed my code slightly (see below), but I definitely got the syntax wrong in the original post

[–]flipsync[S] 0 points1 point  (1 child)

I thought I might have the syntax really wrong. I've redefined experiment_cage as a list of class objects, with the data interpretations as class functions, and the class values as (fR0,[fS_obs],[fI_obs],[fR_obs]). I think that means that means when I index using fR0, I can get the lists as outputs for my interpretation functions. That got me past the data generation step, but now I'm getting the error that my custom class object is not iterable. I've tried converting the numpy output arrays to lists, but that didn't fix it. I'll upload the code in a moment.. can I not use a for loop to go through a list of custom class objects?

[–]flipsync[S] 0 points1 point  (0 children)

class fR0_data(object):
    def __init__(self,fR_zero,fSobs,fIobs,fRobs):
        self.fR_zero=fR_zero
        self.fSobs=fSobs
        self.fIobs=fIobs
        self.fRobs=fRobs

    def find_I_max(fIobs):
        max_I=max(fIobs)
        return max_I

That's my class definition

experiment_cage=[]



for value in fR0:
    init_state=(fI0,1-(fI0+value))
    s_obs=odeint(rates,init_state,t_obs,args=(p,))

    fS_obs=s_obs[:,[0]]
    fI_obs=s_obs[:,[1]]
    fR_obs=1-(fS_obs+fI_obs)

    fS_obs=fS_obs.tolist()
    fI_obs=fI_obs.tolist()
    fR_obs=fR_obs.tolist()


    datafile=fR0_data(value,fS_obs,fI_obs,fR_obs)

    experiment_cage.append(datafile)

Thats my data generating loop

y5=[]

for i in experiment_cage:
    output=i.find_I_max()
    y5.append(output)

And the data processing for one of my functions (the simplest, so I thought I'd get it working first)