Skip to content

Commit

Permalink
Structure code in logically separated submodules (Akkudoktor-EOS#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelosthege authored Nov 19, 2024
1 parent 345e788 commit 5e2eae7
Show file tree
Hide file tree
Showing 31 changed files with 75 additions and 84 deletions.
6 changes: 3 additions & 3 deletions single_test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import numpy as np

from akkudoktoreos.class_numpy_encoder import NumpyEncoder
from akkudoktoreos.class_optimize import (
from akkudoktoreos.config import get_working_dir, load_config
from akkudoktoreos.optimization.genetic import (
OptimizationParameters,
OptimizeResponse,
optimization_problem,
)
from akkudoktoreos.config import get_working_dir, load_config
from akkudoktoreos.utils import NumpyEncoder
from akkudoktoreos.visualize import visualisiere_ergebnisse

start_hour = 0
Expand Down
24 changes: 0 additions & 24 deletions src/akkudoktoreos/class_numpy_encoder.py

This file was deleted.

Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field

from akkudoktoreos.class_akku import PVAkku
from akkudoktoreos.devices.battery import PVAkku


class WechselrichterParameters(BaseModel):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
from pydantic import BaseModel, Field, field_validator, model_validator
from typing_extensions import Self

from akkudoktoreos.class_akku import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.class_ems import (
from akkudoktoreos.config import AppConfig
from akkudoktoreos.devices.battery import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.devices.generic import Haushaltsgeraet, HaushaltsgeraetParameters
from akkudoktoreos.devices.inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.prediction.ems import (
EnergieManagementSystem,
EnergieManagementSystemParameters,
)
from akkudoktoreos.class_haushaltsgeraet import (
Haushaltsgeraet,
HaushaltsgeraetParameters,
)
from akkudoktoreos.class_inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.config import AppConfig
from akkudoktoreos.visualize import visualisiere_ergebnisse


Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from pydantic import BaseModel, Field, model_validator
from typing_extensions import Self

from akkudoktoreos.class_akku import PVAkku
from akkudoktoreos.class_haushaltsgeraet import Haushaltsgeraet
from akkudoktoreos.class_inverter import Wechselrichter
from akkudoktoreos.config import EOSConfig
from akkudoktoreos.devices.battery import PVAkku
from akkudoktoreos.devices.generic import Haushaltsgeraet
from akkudoktoreos.devices.inverter import Wechselrichter


class EnergieManagementSystemParameters(BaseModel):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
import requests
from pydantic import BaseModel, ValidationError

from akkudoktoreos.cachefilestore import cache_in_file
from akkudoktoreos.datetimeutil import to_datetime
from akkudoktoreos.logutil import get_logger
from akkudoktoreos.utils.cachefilestore import cache_in_file
from akkudoktoreos.utils.datetimeutil import to_datetime
from akkudoktoreos.utils.logutil import get_logger

logger = get_logger(__name__, logging_level="DEBUG")

Expand Down
20 changes: 10 additions & 10 deletions src/akkudoktoreos/server/fastapi_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
from fastapi import FastAPI, Query
from fastapi.responses import FileResponse, RedirectResponse

from akkudoktoreos.class_load import LoadForecast
from akkudoktoreos.class_load_container import Gesamtlast
from akkudoktoreos.class_load_corrector import LoadPredictionAdjuster
from akkudoktoreos.class_optimize import (
OptimizationParameters,
OptimizeResponse,
optimization_problem,
)
from akkudoktoreos.class_pv_forecast import ForecastResponse, PVForecast
from akkudoktoreos.class_strompreis import HourlyElectricityPriceForecast
from akkudoktoreos.config import (
SetupIncomplete,
get_start_enddate,
get_working_dir,
load_config,
)
from akkudoktoreos.optimization.genetic import (
OptimizationParameters,
OptimizeResponse,
optimization_problem,
)
from akkudoktoreos.prediction.load_container import Gesamtlast
from akkudoktoreos.prediction.load_corrector import LoadPredictionAdjuster
from akkudoktoreos.prediction.load_forecast import LoadForecast
from akkudoktoreos.prediction.price_forecast import HourlyElectricityPriceForecast
from akkudoktoreos.prediction.pv_forecast import ForecastResponse, PVForecast

app = FastAPI(
title="Akkudoktor-EOS",
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from datetime import date, datetime, time, timedelta
from typing import List, Optional, Union

from akkudoktoreos.datetimeutil import to_datetime, to_timedelta
from akkudoktoreos.logutil import get_logger
from akkudoktoreos.utils.datetimeutil import to_datetime, to_timedelta
from akkudoktoreos.utils.logutil import get_logger

logger = get_logger(__file__)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import json
import zoneinfo

import numpy as np


# currently unused
def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
Expand All @@ -16,6 +19,27 @@ def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
return dst_change


class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist() # Convert NumPy arrays to lists
if isinstance(obj, np.generic):
return obj.item() # Convert NumPy scalars to native Python types
return super(NumpyEncoder, self).default(obj)

@staticmethod
def dumps(data):
"""Static method to serialize a Python object into a JSON string using NumpyEncoder.
Args:
data: The Python object to serialize.
Returns:
str: A JSON string representation of the object.
"""
return json.dumps(data, cls=NumpyEncoder)


# # Example usage
# start_date = datetime.datetime(2024, 3, 31) # Date of the DST change
# if ist_dst_wechsel(start_date):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cachefilestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import pytest

from akkudoktoreos.cachefilestore import CacheFileStore, cache_in_file
from akkudoktoreos.datetimeutil import to_datetime
from akkudoktoreos.utils.cachefilestore import CacheFileStore, cache_in_file
from akkudoktoreos.utils.datetimeutil import to_datetime

# -----------------------------
# CacheFileStore
Expand Down
2 changes: 1 addition & 1 deletion tests/test_class_akku.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from akkudoktoreos.class_akku import PVAkku, PVAkkuParameters
from akkudoktoreos.devices.battery import PVAkku, PVAkkuParameters


class TestPVAkku(unittest.TestCase):
Expand Down
13 changes: 5 additions & 8 deletions tests/test_class_ems.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import numpy as np
import pytest

from akkudoktoreos.class_akku import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.class_ems import (
from akkudoktoreos.config import AppConfig
from akkudoktoreos.devices.battery import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.devices.generic import Haushaltsgeraet, HaushaltsgeraetParameters
from akkudoktoreos.devices.inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.prediction.ems import (
EnergieManagementSystem,
EnergieManagementSystemParameters,
)
from akkudoktoreos.class_haushaltsgeraet import (
Haushaltsgeraet,
HaushaltsgeraetParameters,
)
from akkudoktoreos.class_inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.config import AppConfig

prediction_hours = 48
optimization_hours = 24
Expand Down
13 changes: 5 additions & 8 deletions tests/test_class_ems_2.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import numpy as np
import pytest

from akkudoktoreos.class_akku import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.class_ems import (
from akkudoktoreos.config import AppConfig
from akkudoktoreos.devices.battery import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.devices.generic import Haushaltsgeraet, HaushaltsgeraetParameters
from akkudoktoreos.devices.inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.prediction.ems import (
EnergieManagementSystem,
EnergieManagementSystemParameters,
)
from akkudoktoreos.class_haushaltsgeraet import (
Haushaltsgeraet,
HaushaltsgeraetParameters,
)
from akkudoktoreos.class_inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.config import AppConfig

prediction_hours = 48
optimization_hours = 24
Expand Down
8 changes: 4 additions & 4 deletions tests/test_class_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import pytest

from akkudoktoreos.class_optimize import (
from akkudoktoreos.config import AppConfig
from akkudoktoreos.optimization.genetic import (
OptimizationParameters,
OptimizeResponse,
optimization_problem,
)
from akkudoktoreos.config import AppConfig

DIR_TESTDATA = Path(__file__).parent / "testdata"

Expand All @@ -37,7 +37,7 @@ def compare_dict(actual: dict[str, Any], expected: dict[str, Any]):
("optimize_input_2.json", "optimize_result_2_full.json", 400),
],
)
@patch("akkudoktoreos.class_optimize.visualisiere_ergebnisse")
@patch("akkudoktoreos.optimization.genetic.visualisiere_ergebnisse")
def test_optimize(
visualisiere_ergebnisse_patch,
fn_in: str,
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_optimize(
# Call the optimization function
ergebnis = opt_class.optimierung_ems(parameters=input_data, start_hour=start_hour, ngen=ngen)
# with open(f"new_{fn_out}", "w") as f_out:
# from akkudoktoreos.class_numpy_encoder import NumpyEncoder
# from akkudoktoreos.utils import NumpyEncoder
# json_data_str = NumpyEncoder.dumps(ergebnis)
# json.dump(json.loads(json_data_str), f_out, indent=4)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_datetimeutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from akkudoktoreos.datetimeutil import to_datetime, to_timedelta, to_timezone
from akkudoktoreos.utils.datetimeutil import to_datetime, to_timedelta, to_timezone

# -----------------------------
# to_datetime
Expand Down
2 changes: 1 addition & 1 deletion tests/test_heatpump.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from akkudoktoreos.heatpump import Heatpump
from akkudoktoreos.devices.heatpump import Heatpump


@pytest.fixture(scope="function")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_logutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from akkudoktoreos.logutil import get_logger
from akkudoktoreos.utils.logutil import get_logger

# -----------------------------
# get_logger
Expand Down
8 changes: 4 additions & 4 deletions tests/test_pv_forecast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test Module for PV Power Forecasting Module.
This test module is designed to verify the functionality of the `PVForecast` class
and its methods in the `class_pv_forecast` module. The tests include validation for
and its methods in the `prediction.pv_forecast` module. The tests include validation for
forecast data processing, updating AC power measurements, retrieving forecast data,
and caching behavior.
Expand All @@ -19,7 +19,7 @@
- test_cache_loading: Tests loading forecast data from a cached file to ensure caching works as expected.
Usage:
This test module uses `pytest` and requires the `akkudoktoreos.class_pv_forecast.py` module to be present.
This test module uses `pytest` and requires the `akkudoktoreos.prediction.pv_forecast.py` module to be present.
Run the tests using the command: `pytest test_pv_forecast.py`.
"""
Expand All @@ -31,8 +31,8 @@

import pytest

from akkudoktoreos.class_pv_forecast import PVForecast, validate_pv_forecast_data
from akkudoktoreos.datetimeutil import to_datetime
from akkudoktoreos.prediction.pv_forecast import PVForecast, validate_pv_forecast_data
from akkudoktoreos.utils.datetimeutil import to_datetime

DIR_TESTDATA = Path(__file__).absolute().parent.joinpath("testdata")

Expand Down

0 comments on commit 5e2eae7

Please sign in to comment.