Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beliefs with belief timing #501

Merged
merged 5 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -9,6 +9,7 @@ New features
-------------

* Hit the replay button to replay what happened, available on the sensor and asset pages [see `PR #463 <http://www.github.com/FlexMeasures/flexmeasures/pull/463>`_]
* Improved import of time series data from CSV file: 1) drop duplicate records with warning, and 2) allow configuring which column contains explicit recording times for each data point (use case: import forecasts) [see `PR #501 <http://www.github.com/FlexMeasures/flexmeasures/pull/501>`_]

Bugfixes
-----------
Expand Down
22 changes: 18 additions & 4 deletions flexmeasures/cli/data_add.py
Expand Up @@ -354,6 +354,12 @@ def add_initial_structure():
type=int,
help="Column number with values (1 is 2nd column, the default)",
)
@click.option(
"--beliefcol",
required=False,
type=int,
help="Column number with datetimes",
)
@click.option(
"--delimiter",
required=True,
Expand Down Expand Up @@ -395,6 +401,7 @@ def add_beliefs(
nrows: Optional[int] = None,
datecol: int = 0,
valuecol: int = 1,
beliefcol: Optional[int] = None,
delimiter: str = ",",
decimal: str = ".",
thousands: Optional[str] = None,
Expand All @@ -416,8 +423,8 @@ def add_beliefs(
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.
In case no --horizon is specified and no beliefcol 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:
Expand All @@ -441,7 +448,7 @@ def add_beliefs(
kwargs["sheet_name"] = sheet_number
if horizon is not None:
kwargs["belief_horizon"] = timedelta(minutes=horizon)
else:
elif beliefcol is None:
kwargs["belief_time"] = server_now().astimezone(pytz.timezone(sensor.timezone))

bdf = tb.read_csv(
Expand All @@ -453,11 +460,18 @@ def add_beliefs(
header=None,
skiprows=skiprows,
nrows=nrows,
usecols=[datecol, valuecol],
usecols=[datecol, valuecol]
if beliefcol is None
else [datecol, beliefcol, valuecol],
parse_dates=True,
na_values=na_values,
**kwargs,
)
duplicate_rows = bdf.index.duplicated(keep="first")
if any(duplicate_rows) > 0:
print("Duplicates found. Dropping duplicates for the following records:")
print(bdf[duplicate_rows])
bdf = bdf[~duplicate_rows]
if unit is not None:
bdf["event_value"] = convert_units(
bdf["event_value"],
Expand Down
2 changes: 1 addition & 1 deletion requirements/app.in
Expand Up @@ -28,7 +28,7 @@ tldextract
pyomo>=5.6
tabulate
timetomodel>=0.7.1
timely-beliefs>=1.11.5
timely-beliefs>=1.12
python-dotenv
# a backport, not needed in Python3.8
importlib_metadata
Expand Down