Skip to content

Commit

Permalink
new TimeSeries and TimeDataFrame Pydantic Models
Browse files Browse the repository at this point in the history
  • Loading branch information
JrtPec committed Jul 16, 2024
1 parent 93d0b6c commit fdb86ed
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 142 deletions.
176 changes: 88 additions & 88 deletions demo_energiedelen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"metadata": {},
"outputs": [],
"source": [
"from openenergyid import TimeSeries\n",
"from openenergyid import TimeDataFrame\n",
"from openenergyid.energysharing import EnergySharingInput, EnergySharingOutput, calculate, CalculationMethod"
]
},
Expand Down Expand Up @@ -386,9 +386,9 @@
"# This helps with validation and serialization\n",
"\n",
"input = EnergySharingInput.model_construct(\n",
" gross_injection=TimeSeries.from_pandas(gross_injection),\n",
" gross_offtake=TimeSeries.from_pandas(gross_offtake),\n",
" key=TimeSeries.from_pandas(key),\n",
" gross_injection=TimeDataFrame.from_pandas(gross_injection),\n",
" gross_offtake=TimeDataFrame.from_pandas(gross_offtake),\n",
" key=TimeDataFrame.from_pandas(key),\n",
")\n",
"\n",
"df = input.data_frame()"
Expand Down Expand Up @@ -449,18 +449,18 @@
" <tbody>\n",
" <tr>\n",
" <th>2024-01-01 12:00:00+01:00</th>\n",
" <td>70.0</td>\n",
" <td>10.0</td>\n",
" <td>20.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>30.0</td>\n",
" <td>22.0</td>\n",
" <td>30.0</td>\n",
" <td>18.0</td>\n",
" <td>20.0</td>\n",
" <td>70</td>\n",
" <td>10</td>\n",
" <td>20</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>30</td>\n",
" <td>22</td>\n",
" <td>30</td>\n",
" <td>18</td>\n",
" <td>20</td>\n",
" <td>0.0</td>\n",
" <td>0.1</td>\n",
" <td>0.225</td>\n",
Expand All @@ -470,18 +470,18 @@
" </tr>\n",
" <tr>\n",
" <th>2024-01-01 12:15:00+01:00</th>\n",
" <td>20.0</td>\n",
" <td>10.0</td>\n",
" <td>30.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>20.0</td>\n",
" <td>20.0</td>\n",
" <td>25.0</td>\n",
" <td>17.0</td>\n",
" <td>22.0</td>\n",
" <td>20</td>\n",
" <td>10</td>\n",
" <td>30</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>20</td>\n",
" <td>20</td>\n",
" <td>25</td>\n",
" <td>17</td>\n",
" <td>22</td>\n",
" <td>0.0</td>\n",
" <td>0.1</td>\n",
" <td>0.225</td>\n",
Expand All @@ -491,18 +491,18 @@
" </tr>\n",
" <tr>\n",
" <th>2024-01-01 12:30:00+01:00</th>\n",
" <td>100.0</td>\n",
" <td>10.0</td>\n",
" <td>40.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>10.0</td>\n",
" <td>30.0</td>\n",
" <td>35.0</td>\n",
" <td>19.0</td>\n",
" <td>24.0</td>\n",
" <td>100</td>\n",
" <td>10</td>\n",
" <td>40</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>10</td>\n",
" <td>30</td>\n",
" <td>35</td>\n",
" <td>19</td>\n",
" <td>24</td>\n",
" <td>0.0</td>\n",
" <td>0.1</td>\n",
" <td>0.225</td>\n",
Expand All @@ -512,18 +512,18 @@
" </tr>\n",
" <tr>\n",
" <th>2024-01-01 12:45:00+01:00</th>\n",
" <td>0.0</td>\n",
" <td>10.0</td>\n",
" <td>50.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>40.0</td>\n",
" <td>10.0</td>\n",
" <td>25.0</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>10</td>\n",
" <td>50</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>40</td>\n",
" <td>10</td>\n",
" <td>25</td>\n",
" <td>26</td>\n",
" <td>0.0</td>\n",
" <td>0.1</td>\n",
" <td>0.225</td>\n",
Expand All @@ -536,26 +536,26 @@
"</div>"
],
"text/plain": [
" Gross Injection \\\n",
" P1 P2 P3 P4 P5 P6 \n",
"2024-01-01 12:00:00+01:00 70.0 10.0 20.0 0.0 0.0 0.0 \n",
"2024-01-01 12:15:00+01:00 20.0 10.0 30.0 0.0 0.0 0.0 \n",
"2024-01-01 12:30:00+01:00 100.0 10.0 40.0 0.0 0.0 0.0 \n",
"2024-01-01 12:45:00+01:00 0.0 10.0 50.0 0.0 0.0 0.0 \n",
" Gross Injection Gross Offtake \\\n",
" P1 P2 P3 P4 P5 P6 P1 P2 \n",
"2024-01-01 12:00:00+01:00 70 10 20 0 0 0 0 30 \n",
"2024-01-01 12:15:00+01:00 20 10 30 0 0 0 0 20 \n",
"2024-01-01 12:30:00+01:00 100 10 40 0 0 0 0 10 \n",
"2024-01-01 12:45:00+01:00 0 10 50 0 0 0 0 0 \n",
"\n",
" Gross Offtake Key \\\n",
" P1 P2 P3 P4 P5 P6 P1 \n",
"2024-01-01 12:00:00+01:00 0.0 30.0 22.0 30.0 18.0 20.0 0.0 \n",
"2024-01-01 12:15:00+01:00 0.0 20.0 20.0 25.0 17.0 22.0 0.0 \n",
"2024-01-01 12:30:00+01:00 0.0 10.0 30.0 35.0 19.0 24.0 0.0 \n",
"2024-01-01 12:45:00+01:00 0.0 0.0 40.0 10.0 25.0 26.0 0.0 \n",
" Key \\\n",
" P3 P4 P5 P6 P1 P2 P3 P4 P5 \n",
"2024-01-01 12:00:00+01:00 22 30 18 20 0.0 0.1 0.225 0.225 0.225 \n",
"2024-01-01 12:15:00+01:00 20 25 17 22 0.0 0.1 0.225 0.225 0.225 \n",
"2024-01-01 12:30:00+01:00 30 35 19 24 0.0 0.1 0.225 0.225 0.225 \n",
"2024-01-01 12:45:00+01:00 40 10 25 26 0.0 0.1 0.225 0.225 0.225 \n",
"\n",
" \n",
" P2 P3 P4 P5 P6 \n",
"2024-01-01 12:00:00+01:00 0.1 0.225 0.225 0.225 0.225 \n",
"2024-01-01 12:15:00+01:00 0.1 0.225 0.225 0.225 0.225 \n",
"2024-01-01 12:30:00+01:00 0.1 0.225 0.225 0.225 0.225 \n",
"2024-01-01 12:45:00+01:00 0.1 0.225 0.225 0.225 0.225 "
" \n",
" P6 \n",
"2024-01-01 12:00:00+01:00 0.225 \n",
"2024-01-01 12:15:00+01:00 0.225 \n",
"2024-01-01 12:30:00+01:00 0.225 \n",
"2024-01-01 12:45:00+01:00 0.225 "
]
},
"execution_count": 9,
Expand Down Expand Up @@ -1155,6 +1155,12 @@
"text": [
"{\n",
" \"netInjection\": {\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"columns\": [\n",
" \"P1\",\n",
" \"P2\",\n",
Expand All @@ -1163,12 +1169,6 @@
" \"P5\",\n",
" \"P6\"\n",
" ],\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"data\": [\n",
" [\n",
" 0.0,\n",
Expand Down Expand Up @@ -1205,6 +1205,12 @@
" ]\n",
" },\n",
" \"netOfftake\": {\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"columns\": [\n",
" \"P1\",\n",
" \"P2\",\n",
Expand All @@ -1213,12 +1219,6 @@
" \"P5\",\n",
" \"P6\"\n",
" ],\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"data\": [\n",
" [\n",
" 0.0,\n",
Expand Down Expand Up @@ -1255,6 +1255,12 @@
" ]\n",
" },\n",
" \"sharedEnergy\": {\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"columns\": [\n",
" \"P1\",\n",
" \"P2\",\n",
Expand All @@ -1263,12 +1269,6 @@
" \"P5\",\n",
" \"P6\"\n",
" ],\n",
" \"index\": [\n",
" \"2024-01-01T12:00:00+01:00\",\n",
" \"2024-01-01T12:15:00+01:00\",\n",
" \"2024-01-01T12:30:00+01:00\",\n",
" \"2024-01-01T12:45:00+01:00\"\n",
" ],\n",
" \"data\": [\n",
" [\n",
" 0.0,\n",
Expand Down
14 changes: 7 additions & 7 deletions demo_energyid_download.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"source": [
"import json\n",
"import energyid\n",
"from openenergyid import TimeSeries, const"
"from openenergyid import TimeDataFrame, const"
]
},
{
Expand Down Expand Up @@ -98,9 +98,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Rename to standard metric names used in the Open Energy Library\n",
"\n",
"energy_balance.columns = [const.ELECTRICITY_DELIVERED, const.ELECTRICITY_PRODUCED, const.ELECTRICITY_EXPORTED]"
"energy_balance"
]
},
{
Expand All @@ -109,7 +107,9 @@
"metadata": {},
"outputs": [],
"source": [
"energy_balance.index.name = None"
"# Rename to standard metric names used in the Open Energy Library\n",
"\n",
"energy_balance.columns = [const.ELECTRICITY_DELIVERED, const.ELECTRICITY_PRODUCED, const.ELECTRICITY_EXPORTED]"
]
},
{
Expand All @@ -118,7 +118,7 @@
"metadata": {},
"outputs": [],
"source": [
"series = TimeSeries.from_pandas(energy_balance)"
"data = TimeDataFrame.from_pandas(energy_balance)"
]
},
{
Expand All @@ -127,7 +127,7 @@
"metadata": {},
"outputs": [],
"source": [
"series.to_json(path='data/energy_balance_PT15M.json', indent=2)"
"data.to_json(path='data/energy_balance_PT15M.json', indent=2)"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions openenergyid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
__version__ = "0.1.10"

from .enums import Granularity
from .models import TimeSeries
from .models import TimeDataFrame, TimeSeries

__all__ = ["Granularity", "TimeSeries"]
__all__ = ["Granularity", "TimeDataFrame", "TimeSeries"]
20 changes: 10 additions & 10 deletions openenergyid/energysharing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel, Field
import pandas as pd

from openenergyid import TimeSeries
from openenergyid import TimeDataFrame
from .data_formatting import create_multi_index_input_frame
from .const import NET_INJECTION, NET_OFFTAKE, SHARED_ENERGY

Expand All @@ -22,9 +22,9 @@ class CalculationMethod(Enum):
class EnergySharingInput(BaseModel):
"""Input data for energy sharing."""

gross_injection: Annotated[TimeSeries, Field(alias="grossInjection")]
gross_offtake: Annotated[TimeSeries, Field(alias="grossOfftake")]
key: Annotated[TimeSeries, Field(alias="key")]
gross_injection: Annotated[TimeDataFrame, Field(alias="grossInjection")]
gross_offtake: Annotated[TimeDataFrame, Field(alias="grossOfftake")]
key: Annotated[TimeDataFrame, Field(alias="key")]
timezone: str = Field(alias="timeZone", default="Europe/Brussels")

def data_frame(self) -> pd.DataFrame:
Expand All @@ -41,15 +41,15 @@ def data_frame(self) -> pd.DataFrame:
class EnergySharingOutput(BaseModel):
"""Output data for energy sharing."""

net_injection: TimeSeries = Field(alias="netInjection")
net_offtake: TimeSeries = Field(alias="netOfftake")
shared_energy: TimeSeries = Field(alias="sharedEnergy")
net_injection: TimeDataFrame = Field(alias="netInjection")
net_offtake: TimeDataFrame = Field(alias="netOfftake")
shared_energy: TimeDataFrame = Field(alias="sharedEnergy")

@classmethod
def from_calculation_result(cls, result: pd.DataFrame) -> "EnergySharingOutput":
"""Create an output model from a calculation result."""
return cls.model_construct(
net_injection=TimeSeries.from_pandas(result[NET_INJECTION]),
net_offtake=TimeSeries.from_pandas(result[NET_OFFTAKE]),
shared_energy=TimeSeries.from_pandas(result[SHARED_ENERGY]),
net_injection=TimeDataFrame.from_pandas(result[NET_INJECTION]),
net_offtake=TimeDataFrame.from_pandas(result[NET_OFFTAKE]),
shared_energy=TimeDataFrame.from_pandas(result[SHARED_ENERGY]),
)
Loading

0 comments on commit fdb86ed

Please sign in to comment.