/
data_add.py
executable file
·652 lines (599 loc) · 20.3 KB
/
data_add.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""CLI Tasks for (de)populating the database - most useful in development"""
from datetime import timedelta
from typing import Dict, List, Optional
import json
import pandas as pd
import pytz
from flask import current_app as app
from flask.cli import with_appcontext
import click
import getpass
from sqlalchemy.exc import IntegrityError
import timely_beliefs as tb
from flexmeasures.data import db
from flexmeasures.data.services.forecasting import create_forecasting_jobs
from flexmeasures.data.services.users import create_user
from flexmeasures.data.models.user import Account, AccountRole, RolesAccounts
from flexmeasures.data.models.time_series import Sensor, TimedBelief
from flexmeasures.data.schemas.sensors import SensorSchema
from flexmeasures.data.schemas.generic_assets import (
GenericAssetSchema,
GenericAssetTypeSchema,
)
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType
from flexmeasures.data.models.weather import WeatherSensor
from flexmeasures.data.schemas.weather import WeatherSensorSchema
from flexmeasures.data.models.data_sources import (
get_or_create_source,
get_source_or_none,
)
from flexmeasures.utils.time_utils import server_now
@click.group("add")
def fm_add_data():
"""FlexMeasures: Add data."""
@fm_add_data.command("account-role")
@with_appcontext
@click.option("--name", required=True)
@click.option("--description")
def new_account_role(name: str, description: str):
"""
Create an account role.
"""
role = AccountRole.query.filter_by(name=name).one_or_none()
if role is not None:
click.echo(f"Account role '{name}' already exists.")
raise click.Abort
role = AccountRole(name=name, description=description)
db.session.add(role)
db.session.commit()
print(f"Account role '{name}' (ID: {role.id}) successfully created.")
@fm_add_data.command("account")
@with_appcontext
@click.option("--name", required=True)
@click.option("--roles", help="e.g. anonymous,Prosumer,CPO")
def new_account(name: str, roles: str):
"""
Create an account for a tenant in the FlexMeasures platform.
"""
account = db.session.query(Account).filter_by(name=name).one_or_none()
if account is not None:
click.echo(f"Account '{name}' already exists.")
raise click.Abort
account = Account(name=name)
db.session.add(account)
if roles:
for role_name in roles.split(","):
role = AccountRole.query.filter_by(name=role_name).one_or_none()
if role is None:
print(f"Adding account role {role_name} ...")
role = AccountRole(name=role_name)
db.session.add(role)
db.session.flush()
db.session.add(RolesAccounts(role_id=role.id, account_id=account.id))
db.session.commit()
print(f"Account '{name}' (ID: {account.id}) successfully created.")
@fm_add_data.command("user")
@with_appcontext
@click.option("--username", required=True)
@click.option("--email", required=True)
@click.option("--account-id", type=int, required=True)
@click.option("--roles", help="e.g. anonymous,Prosumer,CPO")
@click.option(
"--timezone",
"timezone_optional",
help="timezone as string, e.g. 'UTC' or 'Europe/Amsterdam' (defaults to FLEXMEASURES_TIMEZONE config setting)",
)
def new_user(
username: str,
email: str,
account_id: int,
roles: List[str],
timezone_optional: Optional[str],
):
"""
Create a FlexMeasures user.
The `users create` task from Flask Security Too is too simple for us.
Use this to add email, timezone and roles.
"""
if timezone_optional is None:
timezone = app.config.get("FLEXMEASURES_TIMEZONE", "UTC")
print(
f"Setting user timezone to {timezone} (taken from FLEXMEASURES_TIMEZONE config setting)..."
)
else:
timezone = timezone_optional
try:
pytz.timezone(timezone)
except pytz.UnknownTimeZoneError:
print(f"Timezone {timezone} is unknown!")
raise click.Abort
account = db.session.query(Account).get(account_id)
if account is None:
print(f"No account with id {account_id} found!")
raise click.Abort
pwd1 = getpass.getpass(prompt="Please enter the password:")
pwd2 = getpass.getpass(prompt="Please repeat the password:")
if pwd1 != pwd2:
print("Passwords do not match!")
raise click.Abort
created_user = create_user(
username=username,
email=email,
password=pwd1,
account_name=account.name,
timezone=timezone,
user_roles=roles,
check_email_deliverability=False,
)
db.session.commit()
print(f"Successfully created user {created_user}")
@fm_add_data.command("sensor")
@with_appcontext
@click.option("--name", required=True)
@click.option("--unit", required=True, help="e.g. °C, m/s, kW/m²")
@click.option(
"--event-resolution",
required=True,
type=int,
help="Expected resolution of the data in minutes",
)
@click.option(
"--timezone",
required=True,
help="timezone as string, e.g. 'UTC' or 'Europe/Amsterdam'",
)
@click.option(
"--asset-id",
"generic_asset_id",
required=True,
type=int,
help="Generic asset to assign this sensor to",
)
@click.option(
"--attributes",
required=False,
type=str,
default="{}",
help='Additional attributes. Passed as JSON string, should be a dict. Hint: Currently, for sensors that measures power, use {"capacity_in_mw": 10} to set a capacity of 10 MW',
)
def add_sensor(**args):
"""Add a sensor."""
check_timezone(args["timezone"])
try:
attributes = json.loads(args["attributes"])
except json.decoder.JSONDecodeError as jde:
print(f"Error decoding --attributes. Please check your JSON: {jde}")
raise click.Abort()
del args["attributes"] # not part of schema
check_errors(SensorSchema().validate(args))
args["event_resolution"] = timedelta(minutes=args["event_resolution"])
sensor = Sensor(**args)
if not isinstance(attributes, dict):
print("Attributes should be a dict.")
raise click.Abort()
sensor.attributes = attributes
if sensor.measures_power:
if "capacity_in_mw" not in sensor.attributes:
print("A sensor which measures power needs a capacity (see --attributes).")
raise click.Abort
db.session.add(sensor)
db.session.commit()
print(f"Successfully created sensor with ID {sensor.id}")
print(f"You can access it at its entity address {sensor.entity_address}")
@fm_add_data.command("asset-type")
@with_appcontext
@click.option("--name", required=True)
@click.option(
"--description",
type=str,
help="Description (useful to explain acronyms, for example).",
)
def add_asset_type(**args):
"""Add an asset type."""
check_errors(GenericAssetTypeSchema().validate(args))
generic_asset_type = GenericAssetType(**args)
db.session.add(generic_asset_type)
db.session.commit()
print(f"Successfully created asset type with ID {generic_asset_type.id}.")
print("You can now assign assets to it.")
@fm_add_data.command("asset")
@with_appcontext
@click.option("--name", required=True)
@click.option(
"--latitude",
type=float,
help="Latitude of the asset's location",
)
@click.option(
"--longitude",
type=float,
help="Longitude of the asset's location",
)
@click.option("--account-id", type=int, required=True)
@click.option(
"--asset-type-id",
"generic_asset_type_id",
required=True,
type=int,
help="Asset type to assign to this asset",
)
def add_asset(**args):
"""Add an asset."""
check_errors(GenericAssetSchema().validate(args))
generic_asset = GenericAsset(**args)
db.session.add(generic_asset)
db.session.commit()
print(f"Successfully created asset with ID {generic_asset.id}.")
print("You can now assign sensors to it.")
@fm_add_data.command("weather-sensor")
@with_appcontext
@click.option("--name", required=True)
@click.option("--weather-sensor-type-name", required=True)
@click.option("--unit", required=True, help="e.g. °C, m/s, kW/m²")
@click.option(
"--event-resolution",
required=True,
type=int,
help="Expected resolution of the data in minutes",
)
@click.option(
"--latitude",
required=True,
type=float,
help="Latitude of the sensor's location",
)
@click.option(
"--longitude",
required=True,
type=float,
help="Longitude of the sensor's location",
)
@click.option(
"--timezone",
default="UTC",
help="timezone as string, e.g. 'UTC' (default) or 'Europe/Amsterdam'",
)
def add_weather_sensor(**args):
"""
Add a weather sensor.
This is legacy, after we moved to the new data model.
Adding necessary GenericAsset and Sensor(s) should be done by the (to be built) OWM plugin.
"""
check_timezone(args["timezone"])
check_errors(WeatherSensorSchema().validate(args))
args["event_resolution"] = timedelta(minutes=args["event_resolution"])
sensor = WeatherSensor(**args)
db.session.add(sensor)
db.session.commit()
print(f"Successfully created weather sensor with ID {sensor.id}")
print(f" You can access it at its entity address {sensor.entity_address}")
@fm_add_data.command("structure")
@with_appcontext
def add_initial_structure():
"""Initialize structural data like asset types, market types and weather sensor types."""
from flexmeasures.data.scripts.data_gen import populate_structure
populate_structure(db)
@fm_add_data.command("beliefs")
@with_appcontext
@click.argument("file", type=click.Path(exists=True))
@click.option(
"--sensor-id",
required=True,
type=click.IntRange(min=1),
help="Sensor to which the beliefs pertain.",
)
@click.option(
"--source",
required=True,
type=str,
help="Source of the beliefs (an existing source id or name, or a new name).",
)
@click.option(
"--horizon",
required=False,
type=int,
help="Belief horizon in minutes (use positive horizon for ex-ante beliefs or negative horizon for ex-post beliefs).",
)
@click.option(
"--cp",
required=False,
type=click.FloatRange(0, 1),
help="Cumulative probability in the range [0, 1].",
)
@click.option(
"--resample/--do-not-resample",
default=True,
help="Resample the data to fit the sensor's event resolution.",
)
@click.option(
"--allow-overwrite/--do-not-allow-overwrite",
default=False,
help="Allow overwriting possibly already existing data.\n"
"Not allowing overwriting can be much more efficient",
)
@click.option(
"--skiprows",
required=False,
default=1,
type=int,
help="Number of rows to skip from the top. Set to >1 to skip additional headers.",
)
@click.option(
"--nrows",
required=False,
type=int,
help="Number of rows to read (from the top, after possibly skipping rows). Leave out to read all rows.",
)
@click.option(
"--datecol",
required=False,
default=0,
type=int,
help="Column number with datetimes (0 is 1st column, the default)",
)
@click.option(
"--valuecol",
required=False,
default=1,
type=int,
help="Column number with values (1 is 2nd column, the default)",
)
@click.option(
"--delimiter",
required=True,
type=str,
default=",",
help="[For csv files] Character to delimit columns per row, defaults to comma",
)
@click.option(
"--decimal",
required=False,
default=".",
type=str,
help="[For csv files] decimal character, e.g. '.' for 10.5",
)
@click.option(
"--thousands",
required=False,
default=None,
type=str,
help="[For csv files] thousands separator, e.g. '.' for 10.035,2",
)
@click.option(
"--sheet_number",
required=False,
type=int,
help="[For xls or xlsx files] Sheet number with the data (0 is 1st sheet)",
)
def add_beliefs(
file: str,
sensor_id: int,
source: str,
horizon: Optional[int] = None,
cp: Optional[float] = None,
resample: bool = True,
allow_overwrite: bool = False,
skiprows: int = 1,
nrows: Optional[int] = None,
datecol: int = 0,
valuecol: int = 1,
delimiter: str = ",",
decimal: str = ".",
thousands: Optional[str] = None,
sheet_number: Optional[int] = None,
**kwargs, # in-code calls to this CLI command can set additional kwargs for use in pandas.read_csv or pandas.read_excel
):
"""Add sensor data from a csv file (also accepts xls or xlsx).
To use default settings, structure your csv file as follows:
- One header line (will be ignored!)
- UTC datetimes in 1st column
- values in 2nd column
For example:
Date,Inflow (cubic meter)
2020-12-03 14:00,212
2020-12-03 14:10,215.6
2020-12-03 14:20,203.8
In case no --horizon is specified, the moment of executing this CLI command is taken
as the time at which the beliefs were recorded.
"""
sensor = Sensor.query.filter(Sensor.id == sensor_id).one_or_none()
if sensor is None:
print(f"Failed to create beliefs: no sensor found with id {sensor_id}.")
return
if source.isdigit():
_source = get_source_or_none(int(source), source_type="CLI script")
if not _source:
print(f"Failed to find source {source}.")
return
else:
_source = get_or_create_source(source, source_type="CLI script")
# Set up optional parameters for read_csv
if file.split(".")[-1].lower() == "csv":
kwargs["infer_datetime_format"] = True
kwargs["delimiter"] = delimiter
kwargs["decimal"] = decimal
kwargs["thousands"] = thousands
if sheet_number is not None:
kwargs["sheet_name"] = sheet_number
if horizon is not None:
kwargs["belief_horizon"] = timedelta(minutes=horizon)
else:
kwargs["belief_time"] = server_now().astimezone(pytz.timezone(sensor.timezone))
bdf = tb.read_csv(
file,
sensor,
source=_source,
cumulative_probability=cp,
resample=resample,
header=None,
skiprows=skiprows,
nrows=nrows,
usecols=[datecol, valuecol],
parse_dates=True,
**kwargs,
)
try:
TimedBelief.add(
bdf,
expunge_session=True,
allow_overwrite=allow_overwrite,
bulk_save_objects=True,
commit_transaction=True,
)
print(f"Successfully created beliefs\n{bdf}")
except IntegrityError as e:
db.session.rollback()
print(f"Failed to create beliefs due to the following error: {e.orig}")
if not allow_overwrite:
print("As a possible workaround, use the --allow-overwrite flag.")
@fm_add_data.command("forecasts")
@with_appcontext
@click.option(
"--asset-type",
type=click.Choice(["Asset", "Market", "WeatherSensor"]),
help="The generic asset type for which to generate forecasts."
" Follow up with Asset, Market or WeatherSensor.",
)
@click.option(
"--asset-id",
help="Populate (time series) data for a single asset only. Follow up with the asset's ID. "
"We still need --asset-type, as well, so we know where to look this ID up.",
)
@click.option(
"--from-date",
"from_date_str",
default="2015-02-08",
help="Forecast from date (inclusive). Follow up with a date in the form yyyy-mm-dd.",
)
@click.option(
"--to-date",
"to_date_str",
default="2015-12-31",
help="Forecast to date (inclusive). Follow up with a date in the form yyyy-mm-dd.",
)
@click.option(
"--resolution",
type=int,
help="Resolution of forecast in minutes. If not set, resolution is determined from the asset to be forecasted",
)
@click.option(
"--horizon",
"horizons_as_hours",
multiple=True,
type=click.Choice(["1", "6", "24", "48"]),
default=["1", "6", "24", "48"],
help="Forecasting horizon in hours. This argument can be given multiple times. Defaults to all possible horizons.",
)
@click.option(
"--as-job",
is_flag=True,
help="Whether to queue a forecasting job instead of computing directly."
" Useful to run locally and create forecasts on a remote server. In that case, just point the redis db in your"
" config settings to that of the remote server. To process the job, run a worker to process the forecasting queue. Defaults to False.",
)
def create_forecasts(
asset_type: str = None,
asset_id: int = None,
from_date_str: str = "2015-02-08",
to_date_str: str = "2015-12-31",
horizons_as_hours: List[str] = ["1"],
resolution: Optional[int] = None,
as_job: bool = False,
):
"""
Create forecasts.
For example:
--from_date 2015-02-02 --to_date 2015-02-04 --horizon_hours 6
This will create forecast values from 0am on May 2nd to 0am on May 5th,
based on a 6 hour horizon.
"""
# make horizons
horizons = [timedelta(hours=int(h)) for h in horizons_as_hours]
# apply timezone and set forecast_end to be an inclusive version of to_date
timezone = app.config.get("FLEXMEASURES_TIMEZONE")
forecast_start = pd.Timestamp(from_date_str).tz_localize(timezone)
forecast_end = (pd.Timestamp(to_date_str) + pd.Timedelta("1D")).tz_localize(
timezone
)
event_resolution: Optional[timedelta]
if resolution is not None:
event_resolution = timedelta(minutes=resolution)
else:
event_resolution = None
if as_job:
for horizon in horizons:
# Note that this time period refers to the period of events we are forecasting, while in create_forecasting_jobs
# the time period refers to the period of belief_times, therefore we are subtracting the horizon.
create_forecasting_jobs(
old_sensor_id=asset_id,
horizons=[horizon],
start_of_roll=forecast_start - horizon,
end_of_roll=forecast_end - horizon,
)
else:
from flexmeasures.data.scripts.data_gen import populate_time_series_forecasts
populate_time_series_forecasts(
db=app.db,
horizons=horizons,
forecast_start=forecast_start,
forecast_end=forecast_end,
event_resolution=event_resolution,
old_sensor_class_name=asset_type,
old_sensor_id=asset_id,
)
@fm_add_data.command("external-weather-forecasts")
@with_appcontext
@click.option(
"--region",
type=str,
default="",
help="Name of the region (will create sub-folder if you store json files, should later probably tag the forecast in the DB).",
)
@click.option(
"--location",
type=str,
required=True,
help='Measurement location(s). "latitude,longitude" or "top-left-latitude,top-left-longitude:'
'bottom-right-latitude,bottom-right-longitude." The first format defines one location to measure.'
" The second format defines a region of interest with several (>=4) locations"
' (see also the "method" and "num_cells" parameters for this feature).',
)
@click.option(
"--num_cells",
type=int,
default=1,
help="Number of cells on the grid. Only used if a region of interest has been mapped in the location parameter. Defaults to 1.",
)
@click.option(
"--method",
default="hex",
type=click.Choice(["hex", "square"]),
help="Grid creation method. Only used if a region of interest has been mapped in the location parameter.",
)
@click.option(
"--store-in-db/--store-as-json-files",
default=False,
help="Store forecasts in the database, or simply save as json files. (defaults to json files)",
)
def collect_weather_data(region, location, num_cells, method, store_in_db):
"""
Collect weather forecasts from the OpenWeatherMap API
This function can get weather data for one location or for several locations within
a geometrical grid (See the --location parameter).
This should move to a FlexMeasures plugin for OWM integration.
"""
from flexmeasures.data.scripts.grid_weather import get_weather_forecasts
get_weather_forecasts(app, region, location, num_cells, method, store_in_db)
app.cli.add_command(fm_add_data)
def check_timezone(timezone):
try:
pytz.timezone(timezone)
except pytz.UnknownTimeZoneError:
print("Timezone %s is unknown!" % timezone)
raise click.Abort
def check_errors(errors: Dict[str, List[str]]):
if errors:
print(
f"Please correct the following errors:\n{errors}.\n Use the --help flag to learn more."
)
raise click.Abort