Skip to content

Commit

Permalink
Merge branch 'main' into update-piptools
Browse files Browse the repository at this point in the history
  • Loading branch information
nhoening committed Sep 11, 2023
2 parents 4873e03 + 2841e61 commit e9b1e69
Show file tree
Hide file tree
Showing 51 changed files with 2,194 additions and 313 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PYV = $(shell python -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_

# Note: use tabs
# actions which are virtual, i.e. not a script
.PHONY: install install-for-dev install-for-test install-deps install-flexmeasures run-local test freeze-deps upgrade-deps update-docs update-docs-pdf show-file-space show-data-model clean-db
.PHONY: install install-for-dev install-for-test install-deps install-flexmeasures run-local test freeze-deps upgrade-deps update-docs update-docs-pdf show-file-space show-data-model clean-db cli-autocomplete


# ---- Development ---
Expand Down Expand Up @@ -115,4 +115,7 @@ ensure-deps-folder:
mkdir -p requirements/${PYV}

clean-db:
./flexmeasures/data/scripts/clean_database.sh ${db_name} ${db_user}
./flexmeasures/data/scripts/clean_database.sh ${db_name} ${db_user}

cli-autocomplete:
./flexmeasures/cli/scripts/add_scripts_path.sh ${extension}
27 changes: 27 additions & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
FlexMeasures Changelog
**********************

v0.16.0 | September XX, 2023
============================

New features
-------------

* Introduce new reporter to compute profit/loss due to electricity flows: `ProfitOrLossReporter` [see `PR #808 <https://github.com/FlexMeasures/flexmeasures/pull/808>`_]
* Charts visible in the UI can be exported to PNG or SVG formats in a more automated fashion, using the new CLI command flexmeasures show chart [see `PR #833 <https://github.com/FlexMeasures/flexmeasures/pull/833>`_]

Infrastructure / Support
----------------------

* Allow additional datetime conversions to quantitative time units, specifically, from timezone-naive and/or dayfirst datetimes, which can be useful when importing data [see `PR #831 <https://github.com/FlexMeasures/flexmeasures/pull/831>`_]
* Add a new tutorial to explain the use of the `AggregatorReporter` to compute the headroom and the `ProfitOrLossReporter` to compute the cost of running a process [see `PR #825 <https://github.com/FlexMeasures/flexmeasures/pull/825>`_]

v0.15.1 | August 28, 2023
============================

Bugfixes
-----------

* Fix infeasible problem due to floating point error in :abbr:`SoC (state of charge)` targets [see `PR #832 <https://github.com/FlexMeasures/flexmeasures/pull/832>`_]
* Use the `source` to filter beliefs in the `AggregatorReporter` and fix the case of having multiple sources [see `PR #819 <https://github.com/FlexMeasures/flexmeasures/pull/819>`_]
* Disable HiGHS logs on the standard output when `LOGGING_LEVEL=INFO` [see `PR #824 <https://github.com/FlexMeasures/flexmeasures/pull/824>`_ and `PR #826 <https://github.com/FlexMeasures/flexmeasures/pull/826>`_]
* Fix showing sensor data on the asset page of public assets, and searching for annotations on public assets [see `PR #830 <https://github.com/FlexMeasures/flexmeasures/pull/830>`_]
* Make the command `flexmeasures add schedule for-storage` to pass the soc-target timestamp to the flex model as strings instead of `pd.Timestamp` [see `PR #834 <https://github.com/FlexMeasures/flexmeasures/pull/834>`_]

v0.15.0 | August 9, 2023
============================

Expand Down
11 changes: 9 additions & 2 deletions documentation/cli/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
FlexMeasures CLI Changelog
**********************

since v0.15.0 | August 9, 2023
=================================
since v0.16.0 | September XX, 2023
=======================================
* Add command ``flexmeasures add sources`` to add the base `DataSources` for the `DataGenerators`.
* Add command ``flexmeasures show chart`` to export sensor and asset charts in PNG or SVG formats.
* Add ``--kind reporter`` option to ``flexmeasures add toy-account`` to create the asset and sensors for the reporter tutorial.
* Add ``--id`` option to ``flexmeasures show data-sources`` to show just one ``DataSource``.
* Add ``--show-attributes`` flag to ``flexmeasures show data-sources`` to select whether to show the attributes field or not.

since v0.15.0 | August 9, 2023
================================
* Allow deleting multiple sensors with a single call to ``flexmeasures delete sensor`` by passing the ``--id`` option multiple times.
* Add ``flexmeasures add schedule for-process`` to create a new process schedule for a given power sensor.
* Add support for describing ``config`` and ``parameters`` in YAML for the command ``flexmeasures add report``, editable in user's code editor using the flags ``--edit-config`` or ``--edit-parameters``.
Expand Down
1 change: 1 addition & 0 deletions documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ The platform operator of FlexMeasures can be an Aggregator.
tut/toy-example-from-scratch
tut/toy-example-expanded
tut/toy-example-process
tut/toy-example-reporter
tut/installation
tut/posting_data
tut/forecasting_scheduling
Expand Down
4 changes: 4 additions & 0 deletions documentation/tut/toy-example-process.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Finally, in the SHIFTABLE policy, the process is shifted to capture the best pri
Let's list the power price the policies achieved for each of the four blocks they scheduled:
.. _table-process:
+-------------------------+------------+-----------+-----------+
| Block | INFLEXIBLE | BREAKABLE | SHIFTABLE |
+=========================+============+===========+===========+
Expand All @@ -124,3 +126,5 @@ Let's list the power price the policies achieved for each of the four blocks the
+-------------------------+------------+-----------+-----------+
Quantitatively, comparing the total cost of running the process under each policy, the BREAKABLE policy achieves the best results. This is because it can fit much more consumption blocks in the cheapest hours.
This tutorial showed a quick way to optimize the activation of processes. In :ref:`tut_toy_schedule_reporter`, we'll turn to something different: using *reporters* to apply transformations to sensor data.
246 changes: 246 additions & 0 deletions documentation/tut/toy-example-reporter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
.. _tut_toy_schedule_reporter:

Toy example IV: Computing reports
=====================================

.. warning::
The reporting functionality is still in an early development stage. Beware that major changes might be introduced.

So far, we have worked on scheduling batteries and processes. Now, we are moving to one of the other three pillars of FlexMeasures: reporting.

In essence, reporters apply arbitrary transformations to data coming from some sensors (multiple inputs) and save the results to other sensors (multiple outputs). In practice, this allows to compute KPIs (such as profit and total daily energy production), to apply operations to beliefs (e.g. changing the sign of a power sensor for some time period), among other things.

.. note::
Currently, FlexMeasures comes with the following reporters:
- `PandasReporter`: applies arbitrary `Pandas <https://pandas.pydata.org>`_ methods to sensor data.
- `AggregatorReporter`: combines data from multiple sensors into one using any of the methods supported by the Pandas `aggregate` function (e.g. sum, average, max, min...).
- `ProfitLossReporter`: computes the profit/loss due to an energy flow under a specific tariff.

Moreover, it's possible to implement your custom reporters in plugins. Instructions for this to come.

Now, coming back to the tutorial, we are going to use the `AggregatorReporter` and the `ProfitLossReporter`. In the first part, we'll use the `AggregatorReporter` to compute the (discharge) headroom of the battery in :ref:`tut_toy_schedule_expanded`. That way, we can verify the maximum power at which the battery can discharge at any point of time. In the second part, we'll use the `ProfitLossReporter` to compute the costs of operating the process of Tut. Part III in the different policies.

Before getting to the meat of the tutorial, we need to set up up all the entities. Instead of having to do that manually (e.g. using commands such as ``flexmeasures add sensor``), we have prepared a command that does that automatically.

Setup
.....

Just as in previous sections, we need to run the command ``flexmeasures add toy-account``, but this time with a different value for *kind*:

.. code-block:: bash
$ flexmeasures add toy-account --kind reporter
Under the hood, this command is adding the following entities:
- A yearly sensor that stores the capacity of the grid connection.
- A power sensor, `headroom`, to store the remaining capacity for the battery. This is where we'll store the report.
- A `ProfitLossReporter` configured to use the prices that we set up in Tut. Part II.
- Three sensors to register the profits/losses from running the three different processes of Tut. Part III.

Let's check it out!

Run the command below to show the values for the `grid connection capacity`:

.. code-block:: bash
$ TOMORROW=$(date --date="next day" '+%Y-%m-%d')
$ flexmeasures show beliefs --sensor-id 7 --start ${TOMORROW}T00:00:00+02:00 --duration PT24H --resolution PT1H
Beliefs for Sensor 'grid connection capacity' (ID 7).
Data spans a day and starts at 2023-08-14 00:00:00+02:00.
The time resolution (x-axis) is an hour.
┌────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │ 1.0MW
│ │
│ │
│ │
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ 0.5MW
│ │
│ │
│ │
│▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁│ 0.0MW
│ │
│ │
│ │
│ │ -0.5MW
└────────────────────────────────────────────────────────────┘
5 10 15 20
██ grid connection capacity
Moreover, we can check the freshly created source `<Source id=6>` which defines the `ProfitLossReporter` with the required configuration. You'll notice that the `config` is under the `data_generator` field. That's because reporters belong to a bigger category of classes that also contains the `Schedulers` and `Forecasters`.

.. code-block:: bash
$ flexmeasures show data-sources --show-attributes --id 5
ID Name Type User ID Model Version Attributes
---- ------------ -------- --------- -------------- --------- -----------------------------------------
6 FlexMeasures reporter ProfitLossReporter {
"data_generator": {
"config": {
"consumption_price_sensor": 1
}
}
}
Compute headroom
-------------------

In this case, the discharge headroom is nothing but the difference between the grid connection capacity and the PV power. To compute that quantity, we can use the `AggregatorReporter` using the weights to make the PV to subtract the grid connection capacity.

In practice, we need to create the `config` and `parameters`:

.. code-block:: bash
$ echo "
$ {
$ 'weights' : {
$ 'grid connection capacity' : 1.0,
$ 'PV' : -1.0,
$ }
$ }" > headroom-config.json
.. code-block:: bash
$ echo "
$ {
$ 'input' : [{'name' : 'grid connection capacity','sensor' : 7},
$ {'name' : 'PV', 'sensor' : 3}],
$ 'output' : [{'sensor' : 8}]
$ }" > headroom-parameters.json
Finally, we can create the reporter with the following command:

.. code-block:: bash
$ TOMORROW=$(date --date="next day" '+%Y-%m-%d')
$ flexmeasures add report --reporter AggregatorReporter \
--parameters headroom-parameters.json --config headroom-config.json \
--start-offset DB,1D --end-offset DB,2D \
--resolution PT15M
Now we can visualize the headroom in the following `link <http://localhost:5000/sensor/8/>`_, which should resemble the following image.

.. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/sensor-data-headroom.png
:align: center
|
The graph shows that the capacity of the grid is at full disposal for the battery when there's no sun (thus no PV generation), while
at noon the battery can only discharge at 280kW max.

Process scheduler profit
-------------------------

For the second part of this tutorial, we are going to use the `ProfitLossReporter` to compute the losses (defined as `cost - revenue`) of operating the
process from Tut. Part III, under the three different policies: INFLEXIBLE, BREAKABLE and SHIFTABLE.

In addition, we'll explore another way to invoke reporters: data generators. Without going too much into detail, data generators
create new data. The thee main types are: `Reporters`, `Schedulers` and `Forecasters`. This will come handy as the three reports that
we are going to create share the same `config`. The `config` defines the price sensor to use and sets the reporter to work in **losses** mode which means
that it will return costs as positive values and revenue as negative values.

Still, we need to define the parameters. The three reports share the same structure for the parameters with the following fields:
* `input`: sensor that stores the power/energy flow. The number of sensors is limited to 1.
* `output`: sensor to store the report. We can provide sensors with different resolutions to store the same results at different time scales.

.. note::
It's possible to define the `config` and `parameters` in JSON or YAML formats.

After setting up `config` and `parameters`, we can invoke the reporter using the command ``flexmeasures add report``. The command takes the data source id,
the files containing the parameters and the timing parameters (start and end). For this particular case, we make use of the offsets to indicate that we want the
report to encompass the day of tomorrow.

Inflexible process
^^^^^^^^^^^^^^^^^^^

Define parameters in a JSON file:

.. code-block:: bash
$ echo "
$ {
$ 'input' : [{'sensor' : 4}],
$ 'output' : [{'sensor' : 9}]
$ }" > inflexible-parameters.json
Create report:

.. code-block:: bash
$ flexmeasures add report --source 6 \
--parameters inflexible-parameters.json \
--start-offset DB,1D --end-offset DB,2D
Check the results `here <http://localhost:5000/sensor/9/>`_. The image should be similar to the one below.

.. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/sensor-data-inflexible.png
:align: center
|

Breakable process
^^^^^^^^^^^^^^^^^^^
Define parameters in a JSON file:

.. code-block:: bash
$ echo "
$ {
$ 'input' : [{'sensor' : 5}],
$ 'output' : [{'sensor' : 10}]
$ }" > breakable-parameters.json
Create report:

.. code-block:: bash
$ flexmeasures add report --source 6 \
--parameters breakable-parameters.json \
--start-offset DB,1D --end-offset DB,2D
Check the results `here <http://localhost:5000/sensor/10/>`_. The image should be similar to the one below.


.. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/sensor-data-breakable.png
:align: center
|
Shiftable process
^^^^^^^^^^^^^^^^^^^

Define parameters in a JSON file:

.. code-block:: bash
$ echo "
$ {
$ 'input' : [{'sensor' : 6}],
$ 'output' : [{'sensor' : 11}]
$ }" > shiftable-parameters.json
Create report:

.. code-block:: bash
$ flexmeasures add report --source 6 \
--parameters shiftable-parameters.json \
--start-offset DB,1D --end-offset DB,2D
Check the results `here <http://localhost:5000/sensor/11/>`_. The image should be similar to the one below.


.. image:: https://github.com/FlexMeasures/screenshots/raw/main/tut/toy-schedule/sensor-data-shiftable.png
:align: center
|
Now, we can compare the results of the reports to the ones we computed manually in :ref:`this table <table-process>`). Keep in mind that the
report is showing the profit of each 15min period and adding them all shows that it matches with our previous results.
Loading

0 comments on commit e9b1e69

Please sign in to comment.