forked from CURENT/andes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeseries.py
269 lines (200 loc) 路 8.01 KB
/
timeseries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
140
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Model for metadata of timeseries.
"""
import os
import logging
from collections import OrderedDict
from andes.core.model import ModelData, Model # noaq
from andes.core.param import DataParam, IdxParam, NumParam # noqa
from andes.core.discrete import Switcher
from andes.shared import pd, np, tqdm
logger = logging.getLogger(__name__)
def str_list_iconv(x):
"""
Helper function to convert a string or a list of strings into a numpy array.
"""
if isinstance(x, str):
x = x.split(',')
x = [item.strip() for item in x]
return x
raise NotImplementedError
def str_list_oconv(x):
"""
Convert list into a list literal.
"""
return ','.join(x)
class TimeSeriesData(ModelData):
"""
Input data for metadata of timeseries.
"""
def __init__(self):
ModelData.__init__(self)
self.mode = NumParam(default='1',
info='Mode for applying timeseries. '
'1: exact time, '
'2: interpolated',
vrange=(1, 2),
)
self.path = DataParam(mandatory=True, info='Path to timeseries xlsx file.')
self.sheet = DataParam(mandatory=True, info='Sheet name to use')
self.fields = NumParam(mandatory=True,
info='comma-separated field names in timeseries data',
iconvert=str_list_iconv,
oconvert=str_list_oconv,
vtype=object,
)
self.tkey = DataParam(default='t', info='Key for timestamps')
self.model = DataParam(info='Model to link to', mandatory=True)
self.dev = IdxParam(info='Idx of device to link to', mandatory=True)
self.dests = NumParam(mandatory=True,
info='comma-separated device fields as destinations',
iconvert=str_list_iconv,
oconvert=str_list_oconv,
vtype=object,
)
class TimeSeriesModel(Model):
"""
Implementation of TimeSeries.
"""
def __init__(self, system, config):
Model.__init__(self, system, config)
# Notes:
# TimeSeries model is not used in power flow for now
self.group = 'DataSeries'
self.flags.tds = True
self.config.add(OrderedDict((('silent', 1),
)))
self.config.add_extra("_help",
silent="suppress output messages if is not zero",
)
self.config.add_extra("_alt",
silent=(0, 1),
)
self.SW = Switcher(self.mode, options=(0, 1, 2),
info='mode switcher', )
self._data = OrderedDict() # keys are the idx, and values are the dataframe
def list2array(self):
"""
Set internal storage for timeseries data.
Open file and read data into internal storage.
"""
# TODO: timeseries file must exist for setup to pass. Consider moving
# the file reading to a later stage so that adding sheets to xlsx file can work
# without the file existing.
Model.list2array(self)
# read and store data
for ii in range(self.n):
idx = self.idx.v[ii]
path = self.path.v[ii]
sheet = self.sheet.v[ii]
if not os.path.isabs(path):
path = os.path.join(self.system.files.case_path, path)
if not os.path.exists(path):
raise FileNotFoundError('<%s idx=%s>: File not found: "%s"',
self.class_name, idx, path)
# --- read supported formats ---
if path.endswith("xlsx") or path.endswith("xls"):
df = self._read_excel(path, sheet, idx)
elif path.endswith("csv"):
df = pd.read_csv(path)
for field in self.fields.v[ii]:
if field not in df.columns:
raise ValueError('Field {} not found in timeseries data'.format(field))
self._data[idx] = df
logger.info('Read timeseries data from "%s"', path)
def _read_excel(self, path, sheet, idx):
"""
Helper function to read excel file.
"""
try:
df = pd.read_excel(path, sheet_name=sheet)
return df
except ValueError as e:
logger.error('<%s idx=%s>: Sheet not found: "%s" in "%s"',
self.class_name, idx, sheet, path)
raise e
def get_times(self):
"""
Gather simulation stop-at times for mode = 1.
"""
Model.get_times(self)
# collect all time stamps
out = list()
for ii in range(self.n):
if self.SW.s1[ii] != 1:
continue
idx = self.idx.v[ii]
df = self._data[idx]
tkey = self.tkey.v[ii]
out.append(df[tkey].to_numpy())
return out
def apply_exact(self, t):
"""
Apply the timeseries data at the exact time.
Parameters
----------
t : float
the current time
"""
# convert from numpy scalar to float
t = t.tolist()
for ii in range(self.n):
# skip offline devices
if self.u.v[ii] == 0:
continue
# check mode
if self.SW.s1[ii] != 1:
continue
idx = self.idx.v[ii]
df = self._data[idx]
tkey = self.tkey.v[ii]
# check if current time is a valid time stamp
if t not in df[tkey].values:
continue
fields = self.fields.v[ii]
dests = self.dests.v[ii]
model = self.model.v[ii]
dev_idx = self.dev.v[ii]
# apply the value change
for field, dest in zip(fields, dests):
value = df.loc[df[tkey] == t, field].values
if len(value) == 0:
continue
value = value[0]
self.system.__dict__[model].set(dest, dev_idx, 'v', value)
if not self.config.silent:
tqdm.write("<TimeSeries %s> set %s=%g for %s.%s at t=%g" %
(idx, dest, value, model, dev_idx, t))
def apply_interpolate(self, t):
"""
Apply timeseries data at the interpolated time.
"""
raise NotImplementedError
def init(self, routine):
"""
Set values for the very first time step.
"""
Model.init(self, routine)
self.apply_exact(np.array(self.system.TDS.config.t0))
logger.debug('<%s>: Initialization done', self.class_name)
class TimeSeries(TimeSeriesData, TimeSeriesModel):
"""
Model for applying time-series data.
A TimeSeries device takes a `xlsx` data spreadsheet and applies the data to
the specified device. The spreadsheet can contain multiple sheets, each with
a column named ``t`` and multiple user-defined columns for the data. The
values will be applied at the exact time instant.
The ``xlsx`` data spreadsheet is assumed in the same folder as the case
file.
Regarding the parameters for the ``TimeSeries`` device:
- The column names in the ``xlsx`` data file need to be specified through
the ``fields`` parameter, separated by commas.
- The parameter/service names of the device which is to be updated need to
be specified through the ``dests`` parameter, separated by commas.
There are a few caveats with the current TimeSeries implementation:
- TimeSeries will not be applied power flow.
- The interpolation mode has yet to be implemented.
"""
def __init__(self, system, config):
TimeSeriesData.__init__(self)
TimeSeriesModel.__init__(self, system, config)