Uncategorized

tuples – Python: Iterate through a series of lists to create custom configs for loading into my model inputs


I am unable to find an elegant way to iterate through my model input sensitivity cases to create configurations which can be passed into my model for sensitivity analysis.

fairly easy to accomplish but I have been struggling.

I have a model which takes a series of inputs. To deeper understand the technology I am modeling, I want to run a P10, P90 sensitivity analysis.

The setup:
I have my model input sensitivity cases in named tuples of p10, p50 & p90:

Sensitivity_Case = namedtuple('Sensitivity_Case', [
    'a',  
    'b',  
    'c',  
])

#p50 case  
p50 = Sensitivity_Case(  
    5,  
    50,  
    'medium',  
)

#p10 case
p10 = Sensitivity_Case(
    1,
    5,
    'low',
)


#p90 case
p90 = Sensitivity_Case(
    10,
    500,
    'high',
)

In order to get the right data generated for the sensitivity analysis, I need to run my model by manipulating one variable at a time while holding every other variable at the p50 (base) case. To do this I would like to create a series of configs (to then loop input into my model) which looks in the following way”

config1 = [**a.p10**, b.p50, c.p50]
config2 = [**a.p90**, b.p50, c.p50]
config3 = [a.p50, **b.p10**, c.p50]
config4 = [a.p50, **b.p90**, c.p50]
config5 = [a.p50, b.p50, **c.p10**]
config6 = [a.p50, b.p50, **c.p90**]

Obviously it would be preferable if whatever iterator which builds these configs is flexible to adaptively take the input lists and figure out (via len() or whatnot) to make as many config permutations as necessary for n items in a sensitivity case list. Otherwise I would just manually make the configs (which would be laborious for 100 variables)

I am fairly new to python and coding in general. I believe what I am trying to do ~should~ be
Below was a first stab at trying to make the config generator but there are obvious flaws. The primary flaw is that it cannot generate configs past config2. The function also creates an intermediate config (base config of all variables p50s).

def generate_combinations(lists):
    result = []

    # all lists have the same length
    list_length = len(lists[0])

    for i in range(list_length):
        current_combination = [lists[0][i]] + [lists[j][1] for j in range(1, len(lists))]
        result.append(current_combination)

    return result

# Example usage:
list1 = [a.p10, a.p50, a.p90]
list2 = [b.p10, b.p50, b.p90]
list3 = [c.p10, c.p50, c.p90]

combined_lists = generate_combinations([list1, list2, list3])

for combination in combined_lists:
    print(combination)



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *