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

Refactor module and forecast imports #149

Merged
merged 26 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ to a microgrid:
```python
import numpy as np
from pymgrid import Microgrid
from pymgrid.microgrid.modules import GensetModule, BatteryModule, LoadModule, RenewableModule
from pymgrid.modules import GensetModule, BatteryModule, LoadModule, RenewableModule


genset = GensetModule(running_min_production=10,
Expand Down
4 changes: 2 additions & 2 deletions notebooks/modular_microgrid_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"source": [
"from pymgrid.MicrogridGenerator import MicrogridGenerator\n",
"from pymgrid import Microgrid\n",
"from pymgrid.microgrid.modules import *\n",
"from pymgrid.modules import *\n",
"import numpy as np\n"
]
},
Expand Down Expand Up @@ -1010,7 +1010,7 @@
"text": [
"\n",
"_modules\n",
"{'fixed': {'sources': {'genset': [<pymgrid.microgrid.modules.genset_module.GensetModule object at 0x7fedac51b250>]}, 'sinks': {'load': [<pymgrid.microgrid.modules.load_module.LoadModule object at 0x7fed600e2390>]}, 'source_and_sinks': {'battery': [<pymgrid.microgrid.modules.battery_module.BatteryModule object at 0x7fedac51b150>]}}, 'sources': {'fixed': {'genset': [<pymgrid.microgrid.modules.genset_module.GensetModule object at 0x7fedac51b250>]}, 'flex': {'PV': [<pymgrid.microgrid.modules.renewable_module.RenewableModule object at 0x7fed5821d090>]}}, 'sinks': {'fixed': {'load': [<pymgrid.microgrid.modules.load_module.LoadModule object at 0x7fed600e2390>]}, 'flex': {}}, 'source_and_sinks': {'fixed': {'battery': [<pymgrid.microgrid.modules.battery_module.BatteryModule object at 0x7fedac51b150>]}, 'flex': {'unbalanced_energy': [<pymgrid.microgrid.modules.unbalanced_energy_module.UnbalancedEnergyModule object at 0x7fed600e2410>]}}, 'flex': {'sources': {'PV': [<pymgrid.microgrid.modules.renewable_module.RenewableModule object at 0x7fed5821d090>]}, 'sinks': {}, 'source_and_sinks': {'unbalanced_energy': [<pymgrid.microgrid.modules.unbalanced_energy_module.UnbalancedEnergyModule object at 0x7fed600e2410>]}}}\n",
"{'fixed': {'sources': {'genset': [<pymgrid.modules.genset_module.GensetModule object at 0x7fedac51b250>]}, 'sinks': {'load': [<pymgrid.modules.load_module.LoadModule object at 0x7fed600e2390>]}, 'source_and_sinks': {'battery': [<pymgrid.modules.battery_module.BatteryModule object at 0x7fedac51b150>]}}, 'sources': {'fixed': {'genset': [<pymgrid.modules.genset_module.GensetModule object at 0x7fedac51b250>]}, 'flex': {'PV': [<pymgrid.modules.renewable_module.RenewableModule object at 0x7fed5821d090>]}}, 'sinks': {'fixed': {'load': [<pymgrid.modules.load_module.LoadModule object at 0x7fed600e2390>]}, 'flex': {}}, 'source_and_sinks': {'fixed': {'battery': [<pymgrid.modules.battery_module.BatteryModule object at 0x7fedac51b150>]}, 'flex': {'unbalanced_energy': [<pymgrid.modules.unbalanced_energy_module.UnbalancedEnergyModule object at 0x7fed600e2410>]}}, 'flex': {'sources': {'PV': [<pymgrid.modules.renewable_module.RenewableModule object at 0x7fed5821d090>]}, 'sinks': {}, 'source_and_sinks': {'unbalanced_energy': [<pymgrid.modules.unbalanced_energy_module.UnbalancedEnergyModule object at 0x7fed600e2410>]}}}\n",
"\n",
"_balance_logger\n",
"{}\n"
Expand Down
5 changes: 3 additions & 2 deletions src/pymgrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

PROJECT_PATH = Path(__file__).parent

from .non_modular_microgrid import NonModularMicrogrid
from .microgrid import Microgrid
from ._deprecated.non_modular_microgrid import NonModularMicrogrid
from .modular_microgrid import Microgrid
from .MicrogridGenerator import MicrogridGenerator

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC
import gym, logging, numpy as np, pandas as pd
from gym import Env
from pymgrid.non_modular_microgrid import NonModularMicrogrid
from pymgrid._deprecated.non_modular_microgrid import NonModularMicrogrid
from pymgrid.MicrogridGenerator import MicrogridGenerator
from copy import deepcopy
from pymgrid.algos.saa.saa import SampleAverageApproximation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
import pandas as pd
import numpy as np
from copy import copy
import cvxpy as cp
import operator
import sys
from plotly.offline import init_notebook_mode, iplot
import matplotlib.pyplot as plt
import cufflinks as cf
from IPython.display import display
from IPython import get_ipython

Expand Down Expand Up @@ -1263,10 +1259,10 @@ def penalty(self, coef = 1):

@classmethod
def from_modular(cls, modular):
from pymgrid.microgrid.convert.convert import to_nonmodular
from pymgrid.convert.convert import to_nonmodular
return to_nonmodular(modular)

def to_modular(self):
from pymgrid.microgrid.convert.convert import to_modular
from pymgrid.convert.convert import to_modular
return to_modular(self)

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pymgrid.non_modular_microgrid import NonModularMicrogrid
from pymgrid.microgrid.modular_microgrid.modular_microgrid import Microgrid
from pymgrid.microgrid.convert.get_module import get_module
from pymgrid.microgrid.convert.to_nonmodular_ops import check_viability, add_params_from_module, get_empty_params, finalize_params
from pymgrid._deprecated.non_modular_microgrid import NonModularMicrogrid
from pymgrid.modular_microgrid.modular_microgrid import Microgrid
from pymgrid.convert.get_module import get_module
from pymgrid.convert.to_nonmodular_ops import check_viability, add_params_from_module, get_empty_params, finalize_params


def to_modular(nonmodular, raise_errors=False):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pandas as pd
from pymgrid.microgrid.modules import LoadModule, RenewableModule, BatteryModule, GridModule, GensetModule, UnbalancedEnergyModule
from pymgrid.modules import LoadModule, RenewableModule, BatteryModule, GridModule, GensetModule, UnbalancedEnergyModule


def get_module(component, nonmodular, raise_errors):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pymgrid.microgrid.modules import LoadModule, RenewableModule, BatteryModule, GridModule, GensetModule, UnbalancedEnergyModule
from pymgrid.modules import LoadModule, RenewableModule, BatteryModule, GridModule, GensetModule, UnbalancedEnergyModule
from copy import deepcopy
import pandas as pd
import numpy as np
Expand Down
2 changes: 2 additions & 0 deletions src/pymgrid/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .discrete.discrete import DiscreteMicrogridEnv
from .continuous.continuous import ContinuousMicrogridEnv
1 change: 1 addition & 0 deletions src/pymgrid/envs/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base import BaseMicrogridEnv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import abstractmethod

from pymgrid import NonModularMicrogrid, Microgrid
from pymgrid.microgrid.envs.base.skip_init import skip_init
from pymgrid.envs.base.skip_init import skip_init


class BaseMicrogridEnv(Microgrid, Env):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from gym.spaces import Dict, Tuple, flatten_space
from warnings import warn

from pymgrid.microgrid.envs.base.base import BaseMicrogridEnv
from pymgrid.envs.base import BaseMicrogridEnv


class ContinuousMicrogridEnv(BaseMicrogridEnv):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from gym.spaces import Discrete
from math import isclose

from pymgrid.microgrid.envs.base.base import BaseMicrogridEnv
from pymgrid.microgrid.utils.logger import ModularLogger
from pymgrid.envs.base import BaseMicrogridEnv
from pymgrid.utils.logger import ModularLogger


class DiscreteMicrogridEnv(BaseMicrogridEnv):
Expand Down
1 change: 1 addition & 0 deletions src/pymgrid/forecast/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .forecaster import get_forecaster, OracleForecaster, GaussianNoiseForecaster, UserDefinedForecaster, NoForecaster
1 change: 0 additions & 1 deletion src/pymgrid/microgrid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
DEFAULT_HORIZON = 23

from .modular_microgrid.modular_microgrid import Microgrid
2 changes: 0 additions & 2 deletions src/pymgrid/microgrid/envs/__init__.py

This file was deleted.

2 changes: 0 additions & 2 deletions src/pymgrid/microgrid/modules/base/__init__.py

This file was deleted.

1 change: 1 addition & 0 deletions src/pymgrid/modular_microgrid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .modular_microgrid import Microgrid
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from warnings import warn

from pymgrid.microgrid import DEFAULT_HORIZON
from pymgrid.microgrid.modules import *
from pymgrid.microgrid.modules.module_container import ModuleContainer
from pymgrid.microgrid.utils.logger import ModularLogger
from pymgrid.microgrid.utils.step import MicrogridStep
from pymgrid.microgrid.utils.serialize import add_numpy_pandas_representers, add_numpy_pandas_constructors, dump_data
from pymgrid.modules import ModuleContainer, UnbalancedEnergyModule
from pymgrid.utils.logger import ModularLogger
from pymgrid.modular_microgrid.utils.step import MicrogridStep
from pymgrid.utils.serialize import add_numpy_pandas_representers, add_numpy_pandas_constructors, dump_data


class Microgrid(yaml.YAMLObject):
Expand Down Expand Up @@ -279,11 +278,11 @@ def serialize(self, dumper_stream):

@classmethod
def from_nonmodular(cls, nonmodular):
from pymgrid.microgrid.convert.convert import to_modular
from pymgrid.convert.convert import to_modular
return to_modular(nonmodular)

def to_nonmodular(self):
from pymgrid.microgrid.convert.convert import to_nonmodular
from pymgrid.convert.convert import to_nonmodular
return to_nonmodular(self)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .battery_module import BatteryModule
from .renewable_module import RenewableModule
from .genset_module import GensetModule
from .grid_module import GridModule
from .load_module import LoadModule
from .renewable_module import RenewableModule
from .unbalanced_energy_module import UnbalancedEnergyModule
from .grid_module import GridModule
from .genset_module import GensetModule

from .module_container import ModuleContainer
2 changes: 2 additions & 0 deletions src/pymgrid/modules/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .base_module import BaseMicrogridModule
from .timeseries.base_timeseries_module import BaseTimeSeriesMicrogridModule
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from gym.spaces import Box
from warnings import warn

from pymgrid.microgrid.utils.logger import ModularLogger
from pymgrid.microgrid.utils.normalize import Normalize, IdentityNormalize
from pymgrid.microgrid.utils.serialize import add_numpy_pandas_representers, add_numpy_pandas_constructors, dump_data
from pymgrid.utils.logger import ModularLogger
from pymgrid.modules.utils.normalize import Normalize, IdentityNormalize
from pymgrid.utils.serialize import add_numpy_pandas_representers, add_numpy_pandas_constructors, dump_data


script_logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abc import ABC, abstractmethod
from abc import abstractmethod

import numpy as np
from pymgrid.microgrid import DEFAULT_HORIZON
from pymgrid.microgrid.modules.base import BaseMicrogridModule
from pymgrid.microgrid.modules.base.timeseries.forecaster import get_forecaster
from pymgrid.modules.base import BaseMicrogridModule
from pymgrid.forecast.forecaster import get_forecaster


class BaseTimeSeriesMicrogridModule(BaseMicrogridModule):
Expand Down Expand Up @@ -107,7 +107,7 @@ def forecast_horizon(self, value):
self._forecast_horizon = value
else:
from warnings import warn
from pymgrid.microgrid.modules.base.timeseries.forecaster import OracleForecaster
from pymgrid.forecast.forecaster import OracleForecaster
warn("Setting forecast_horizon requires a non-null forecaster. Implementing OracleForecaster.")
self.forecaster = OracleForecaster()
self._forecast_horizon = value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pymgrid.microgrid.modules.base import BaseMicrogridModule
from pymgrid.modules.base import BaseMicrogridModule
import numpy as np
import yaml
from warnings import warn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from warnings import warn

from pymgrid.microgrid.modules.base import BaseMicrogridModule
from pymgrid.modules.base import BaseMicrogridModule


class GensetModule(BaseMicrogridModule):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import yaml

from pymgrid.microgrid import DEFAULT_HORIZON
from pymgrid.microgrid.modules.base import BaseTimeSeriesMicrogridModule
from pymgrid.modules.base import BaseTimeSeriesMicrogridModule


class GridModule(BaseTimeSeriesMicrogridModule):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import yaml

from pymgrid.microgrid import DEFAULT_HORIZON
from pymgrid.microgrid.modules.base import BaseTimeSeriesMicrogridModule
from pymgrid.modules.base import BaseTimeSeriesMicrogridModule


class LoadModule(BaseTimeSeriesMicrogridModule):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from collections import UserDict, UserList
from pymgrid.microgrid.modules.base import BaseMicrogridModule
from pymgrid.modules.base import BaseMicrogridModule


class ModuleContainer(UserDict):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import yaml

from pymgrid.microgrid import DEFAULT_HORIZON
from pymgrid.microgrid.modules.base import BaseTimeSeriesMicrogridModule
from pymgrid.modules.base import BaseTimeSeriesMicrogridModule


class RenewableModule(BaseTimeSeriesMicrogridModule):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import yaml

from pymgrid.microgrid.modules.base import BaseMicrogridModule
from pymgrid.modules.base import BaseMicrogridModule


class UnbalancedEnergyModule(BaseMicrogridModule):
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions tests/control/test_mpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_init(self):
self.assertEqual(mpc.horizon, 1)

def test_run_with_load_pv_battery_grid(self):
from pymgrid.microgrid.modules import RenewableModule, LoadModule
from pymgrid.modules import RenewableModule, LoadModule

forecast_steps = 10
pv_const = 50
Expand All @@ -35,7 +35,7 @@ def test_run_with_load_pv_battery_grid(self):
)

def test_run_with_load_pv_battery_genset(self):
from pymgrid.microgrid.modules import RenewableModule, LoadModule
from pymgrid.modules import RenewableModule, LoadModule

forecast_steps = 10
pv_const = 50
Expand All @@ -56,7 +56,7 @@ def test_run_with_load_pv_battery_genset(self):
[10.] * mpc_output.shape[0])

def test_run_twice_with_load_pv_battery_genset(self):
from pymgrid.microgrid.modules import RenewableModule, LoadModule
from pymgrid.modules import RenewableModule, LoadModule

forecast_steps = 10
pv_const = 50
Expand All @@ -83,7 +83,7 @@ def test_run_twice_with_load_pv_battery_genset(self):
self.assertEqual(mpc_output[("genset", 0, "genset_production")].values, [10.] * mpc_output.shape[0])

def test_run_with_load_pv_battery_grid_different_names(self):
from pymgrid.microgrid.modules import RenewableModule, LoadModule
from pymgrid.modules import RenewableModule, LoadModule

forecast_steps = 10
pv_const = 50
Expand Down
2 changes: 1 addition & 1 deletion tests/envs/discrete.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tests.helpers.test_case import TestCase
from tests.helpers.modular_microgrid import get_modular_microgrid

from pymgrid.microgrid.envs import DiscreteMicrogridEnv
from pymgrid.envs import DiscreteMicrogridEnv


class TestDiscreteEnv(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/genset_module_testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pymgrid.microgrid.modules import GensetModule
from pymgrid.modules import GensetModule


default_params = dict(running_min_production=10,
Expand Down
12 changes: 10 additions & 2 deletions tests/helpers/modular_microgrid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from pymgrid.microgrid.modular_microgrid.modular_microgrid import Microgrid
from pymgrid.microgrid.modules import *
import numpy as np

from pymgrid import Microgrid

from pymgrid.modules import (
BatteryModule,
GensetModule,
GridModule,
LoadModule,
RenewableModule
)


def get_modular_microgrid(remove_modules=(), additional_modules=None, add_unbalanced_module=True):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def setUp(self) -> None:
self.strong_grid_and_genset = [microgrid for microgrid in mgen.microgrids if
microgrid.architecture["genset"] and self.is_strong_grid(microgrid)]


@staticmethod
def is_weak_grid(microgrid):
return microgrid.architecture["grid"] and microgrid._grid_status_ts.min().item() < 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from tests.helpers.test_case import TestCase
from pymgrid.microgrid.modules.base.timeseries.forecaster import (
from pymgrid.forecast import (
get_forecaster, OracleForecaster, GaussianNoiseForecaster, UserDefinedForecaster, NoForecaster)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from gym.spaces import Box

from pymgrid.microgrid.modules.renewable_module import RenewableModule
from pymgrid.modules import RenewableModule
from tests.helpers.test_case import TestCase


Expand Down