Newer
Older
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 1 15:16:34 2011
@author: dave
__author__ = "David Verelst <dave@dtu.dk>"
__license__ = "GPL-2+"
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
from __future__ import absolute_import
from builtins import dict
from builtins import zip
from builtins import range
from builtins import str
from builtins import int
from future import standard_library
standard_library.install_aliases()
from builtins import object
# standard python library
import os
import subprocess as sproc
import copy
import zipfile
import shutil
import datetime
import math
import pickle
import re
# what is actually the difference between warnings and logging.warn?
# for which context is which better?
import warnings
import logging
from operator import itemgetter
from time import time
#import Queue
#import threading
#from multiprocessing import Pool
# numpy and scipy only used in HtcMaster._all_in_one_blade_tag
import numpy as np
import scipy
import scipy.interpolate as interpolate
#import matplotlib.pyplot as plt
import pandas as pd
import tables as tbl
# custom libraries
from wetb.prepost import misc
from wetb.prepost import windIO
from wetb.prepost import prepost
from wetb.dlc import high_level as dlc
from wetb.prepost.GenerateHydro import hydro_input
from wetb.utils.envelope import compute_envelope
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def load_pickled_file(source):
FILE = open(source, 'rb')
result = pickle.load(FILE)
FILE.close()
return result
def save_pickle(source, variable):
FILE = open(source, 'wb')
pickle.dump(variable, FILE, protocol=2)
FILE.close()
def write_file(file_path, file_contents, mode):
"""
INPUT:
file_path: path/to/file/name.csv
string : file contents is a string
mode : reading (r), writing (w), append (a),...
"""
FILE = open(file_path, mode)
FILE.write(file_contents)
FILE.close()
def create_multiloop_list(iter_dict, debug=False):
"""
Create a list based on multiple nested loops
============================================
Considerd the following example
>>> for v in range(V_start, V_end, V_delta):
... for y in range(y_start, y_end, y_delta):
... for c in range(c_start, c_end, c_delta):
... print v, y, c
Could be replaced by a list with all these combinations. In order to
replicate this with create_multiloop_list, iter_dict should have
the following structure
>>> iter_dict = dict()
>>> iter_dict['v'] = range(V_start, V_end, V_delta)
>>> iter_dict['y'] = range(y_start, y_end, y_delta)
>>> iter_dict['c'] = range(c_start, c_end, c_delta)
>>> iter_list = create_multiloop_list(iter_dict)
>>> for case in iter_list:
... print case['v'], case['y'], case['c']
Parameters
----------
iter_dict : dictionary
Key holds a valid tag as used in HtcMaster.tags. The corresponding
value shouuld be a list of values to be considered.
Output
------
iter_list : list
List containing dictionaries. Each entry is a combination of the
given iter_dict keys.
Example
-------
>>> iter_dict={'[wind]':[5,6,7],'[coning]':[0,-5,-10]}
>>> create_multiloop_list(iter_dict)
[{'[wind]': 5, '[coning]': 0},
{'[wind]': 5, '[coning]': -5},
{'[wind]': 5, '[coning]': -10},
{'[wind]': 6, '[coning]': 0},
{'[wind]': 6, '[coning]': -5},
{'[wind]': 6, '[coning]': -10},
{'[wind]': 7, '[coning]': 0},
{'[wind]': 7, '[coning]': -5},
{'[wind]': 7, '[coning]': -10}]
"""
iter_list = []
# fix the order of the keys
key_order = list(iter_dict.keys())
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
nr_keys = len(key_order)
nr_values,indices = [],[]
# determine how many items on each key
for key in key_order:
# each value needs to be an iterable! len() will fail if it isn't
# count how many values there are for each key
if type(iter_dict[key]).__name__ != 'list':
print('%s does not hold a list' % key)
raise ValueError('Each value in iter_dict has to be a list!')
nr_values.append(len(iter_dict[key]))
# create an initial indices list
indices.append(0)
if debug: print(nr_values, indices)
go_on = True
# keep track on which index you are counting, start at the back
loopkey = nr_keys -1
cc = 0
while go_on:
if debug: print(indices)
# Each entry on the list is a dictionary with the parameter combination
iter_list.append(dict())
# save all the different combination into one list
for keyi in range(len(key_order)):
key = key_order[keyi]
# add the current combination of values as one dictionary
iter_list[cc][key] = iter_dict[key][indices[keyi]]
# +1 on the indices of the last entry, the overflow principle
indices[loopkey] += 1
# cycle backwards thourgh all dimensions and propagate the +1 if the
# current dimension is full. Hence overflow.
for k in range(loopkey,-1,-1):
# if the current dimension is over its max, set to zero and change
# the dimension of the next. Remember we are going backwards
if not indices[k] < nr_values[k] and k > 0:
# +1 on the index of the previous dimension
indices[k-1] += 1
# set current loopkey index back to zero
indices[k] = 0
# if the previous dimension is not on max, break out
if indices[k-1] < nr_values[k-1]:
break
# if we are on the last dimension, break out if that is also on max
elif k == 0 and not indices[k] < nr_values[k]:
if debug: print(cc)
go_on = False
# fail safe exit mechanism...
if cc > 20000:
raise UserWarning('multiloop_list has already '+str(cc)+' items..')
go_on = False
cc += 1
return iter_list
Loading
Loading full blame...