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: move RateType into common module #253

Merged
merged 1 commit into from
Oct 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/simple_python_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"execution_count": null,
"outputs": [],
"source": [
"from libecalc.dto.types import RateType, ConsumptionType, EnergyUsageType, ConsumerType, EnergyModelType\n",
"from libecalc.dto.types import ConsumptionType, EnergyUsageType, ConsumerType, EnergyModelType\n",
"from libecalc.common.utils.rates import RateType\n",
"from libecalc.dto.base import ConsumerUserDefinedCategoryType, ComponentType\n",
"from libecalc.expression import Expression\n",
"from libecalc.core.graph_result import GraphResult\n",
Expand Down
7 changes: 6 additions & 1 deletion src/libecalc/common/utils/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from abc import ABC, abstractmethod
from collections import defaultdict
from datetime import datetime
from enum import Enum
from typing import (
Any,
DefaultDict,
Expand All @@ -27,7 +28,6 @@
from libecalc.common.string_utils import to_camel_case
from libecalc.common.time_utils import Frequency, Period, calculate_delta_days
from libecalc.common.units import Unit
from libecalc.dto.types import RateType
from numpy.typing import NDArray
from pydantic import Extra, validator
from pydantic.fields import ModelField
Expand All @@ -37,6 +37,11 @@
TimeSeriesValue = TypeVar("TimeSeriesValue", bound=Union[int, float, bool, str])


class RateType(str, Enum):
STREAM_DAY = "STREAM_DAY"
CALENDAR_DAY = "CALENDAR_DAY"


class Rates:
@staticmethod
def to_stream_day(calendar_day_rates: NDArray[np.float64], regularity: List[float]) -> NDArray[np.float64]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from libecalc import dto
from libecalc.common.units import Unit
from libecalc.common.utils.rates import Rates
from libecalc.common.utils.rates import Rates, RateType
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
ConsumerFunctionResult,
Expand All @@ -15,7 +15,6 @@
get_power_loss_factor_from_expression,
)
from libecalc.core.models.results import EnergyFunctionGenericResult
from libecalc.dto.types import RateType
from libecalc.dto.variables import VariablesMap


Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/core/graph_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
compute_emission_intensity_by_yearly_buckets,
)
from libecalc.common.utils.rates import (
RateType,
TimeSeriesBoolean,
TimeSeriesFloat,
TimeSeriesInt,
Expand All @@ -36,7 +37,6 @@
PumpModelResult,
TurbineModelResult,
)
from libecalc.dto.types import RateType
from libecalc.dto.utils.aggregators import aggregate_emissions, aggregate_is_valid
from libecalc.expression import Expression
from pydantic import BaseModel, parse_obj_as
Expand Down
3 changes: 2 additions & 1 deletion src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from libecalc.common.temporal_model import TemporalExpression, TemporalModel
from libecalc.common.units import Unit
from libecalc.common.utils.rates import (
RateType,
TimeSeriesFloat,
TimeSeriesRate,
TimeSeriesStreamDayRate,
Expand All @@ -31,7 +32,7 @@
)
from libecalc.dto.models.compressor import CompressorModel
from libecalc.dto.models.pump import PumpModel
from libecalc.dto.types import ConsumptionType, EnergyUsageType, FuelType, RateType
from libecalc.dto.types import ConsumptionType, EnergyUsageType, FuelType
from libecalc.dto.utils.validators import (
ComponentNameStr,
EmissionNameStr,
Expand Down
3 changes: 2 additions & 1 deletion src/libecalc/dto/models/direct.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any, Dict, Literal, Optional

from libecalc.common.utils.rates import RateType
from libecalc.dto.models.base import ConsumerFunction
from libecalc.dto.types import ConsumerType, RateType
from libecalc.dto.types import ConsumerType
from libecalc.dto.utils.validators import convert_expression
from libecalc.expression import Expression
from pydantic import root_validator, validator
Expand Down
5 changes: 0 additions & 5 deletions src/libecalc/dto/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class EnergyUsageType(str, Enum):
POWER = "POWER"


class RateType(str, Enum):
STREAM_DAY = "STREAM_DAY"
CALENDAR_DAY = "CALENDAR_DAY"


class ConsumerType(str, Enum):
DIRECT = "DIRECT"
COMPRESSOR = "COMPRESSOR"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from libecalc import dto
from libecalc.common.utils.rates import RateType
from libecalc.dto.base import (
ComponentType,
ConsumerUserDefinedCategoryType,
Expand Down Expand Up @@ -527,7 +528,7 @@ def deh(regularity) -> dto.ElectricityConsumer:
condition=Expression.setup_from_expression(value="SIM1;GAS_LIFT > 0"),
power_loss_factor=Expression.setup_from_expression(value=0.05),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=RateType.STREAM_DAY,
),
},
)
Expand Down Expand Up @@ -996,7 +997,7 @@ def flare(fuel_gas, regularity) -> dto.FuelConsumer:
datetime(1900, 1, 1): dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="FLARE;FLARE_RATE"),
energy_usage_type=dto.types.EnergyUsageType.FUEL,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=RateType.STREAM_DAY,
)
},
user_defined_category={datetime(1900, 1, 1): ConsumerUserDefinedCategoryType.FLARE},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from libecalc import dto
from libecalc.common.time_utils import calculate_delta_days
from libecalc.common.units import Unit
from libecalc.common.utils.rates import RateType
from libecalc.dto.base import (
ConsumerUserDefinedCategoryType,
)
Expand Down Expand Up @@ -214,7 +215,7 @@ def simple_direct_el_consumer() -> dto.ElectricityConsumer:
date1: dto.DirectConsumerFunction(
load=Expression.setup_from_expression(value=power_usage_mw),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=RateType.STREAM_DAY,
),
},
regularity=regularity_temporal_consumer,
Expand All @@ -230,7 +231,7 @@ def simple_direct_el_consumer_mobile() -> dto.ElectricityConsumer:
date1: dto.DirectConsumerFunction(
load=Expression.setup_from_expression(value=power_usage_mw),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=RateType.STREAM_DAY,
),
},
regularity=regularity_temporal_consumer,
Expand Down
3 changes: 2 additions & 1 deletion src/libecalc/input/mappers/consumer_function_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from libecalc import dto
from libecalc.common.logger import logger
from libecalc.common.time_utils import Period, define_time_model_for_period
from libecalc.common.utils.rates import RateType
from libecalc.dto import (
CompressorWithTurbine,
VariableSpeedCompressorTrainMultipleStreamsAndPressures,
Expand Down Expand Up @@ -195,7 +196,7 @@ def _direct_mapper(energy_usage_model: Dict, references: References = None) -> d
condition=_map_condition(energy_usage_model),
power_loss_factor=energy_usage_model.get(EcalcYamlKeywords.power_loss_factor),
consumption_rate_type=energy_usage_model.get(EcalcYamlKeywords.direct_consumer_consumption_rate_type)
or dto.types.RateType.STREAM_DAY,
or RateType.STREAM_DAY,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
compute_emission_intensity_by_yearly_buckets,
compute_emission_intensity_yearly,
)
from libecalc.common.utils.rates import Rates, TimeSeriesRate, TimeSeriesVolumes
from libecalc.dto.types import RateType
from libecalc.common.utils.rates import (
Rates,
RateType,
TimeSeriesRate,
TimeSeriesVolumes,
)


def _setup_intensity_testcase(
Expand Down
2 changes: 1 addition & 1 deletion src/tests/libecalc/common/utils/test_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from libecalc.common.units import Unit
from libecalc.common.utils.rates import (
Rates,
RateType,
TimeSeriesBoolean,
TimeSeriesFloat,
TimeSeriesRate,
TimeSeriesVolumes,
TimeSeriesVolumesCumulative,
)
from libecalc.dto.types import RateType


def test_compute_stream_day_rate():
Expand Down
9 changes: 5 additions & 4 deletions src/tests/libecalc/core/consumers/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

import libecalc.common.utils.rates
import pytest
from libecalc import dto
from libecalc.dto import VariablesMap
Expand Down Expand Up @@ -83,22 +84,22 @@ def direct_el_consumer() -> dto.ElectricityConsumer:
datetime(2020, 1, 1): dto.DirectConsumerFunction(
load=Expression.setup_from_expression(value=1),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
),
datetime(2021, 1, 1): dto.DirectConsumerFunction( # Run above capacity
load=Expression.setup_from_expression(value=2),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
),
datetime(2022, 1, 1): dto.DirectConsumerFunction( # Run above capacity
load=Expression.setup_from_expression(value=10),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
),
datetime(2023, 1, 1): dto.DirectConsumerFunction( # Ensure we handle 0 load as well.
load=Expression.setup_from_expression(value=0),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
),
},
regularity={datetime(1900, 1, 1): Expression.setup_from_expression(1)},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

import libecalc.common.utils.rates
import numpy as np
import pytest
from libecalc import dto
Expand Down Expand Up @@ -190,7 +191,7 @@ def test_direct_expression_consumer_function_consumption_rate_type(direct_variab
stream_day_function = DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
load=Expression.setup_from_expression(stream_day_consumption),
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
energy_usage_type=dto.types.EnergyUsageType.POWER,
),
)
Expand All @@ -199,7 +200,7 @@ def test_direct_expression_consumer_function_consumption_rate_type(direct_variab
calendar_day_function = DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
load=Expression.setup_from_expression(calendar_day_consumption),
consumption_rate_type=dto.types.RateType.CALENDAR_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.CALENDAR_DAY,
energy_usage_type=dto.types.EnergyUsageType.POWER,
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from libecalc import dto
from libecalc.common.units import Unit
from libecalc.common.utils.rates import (
RateType,
TimeSeriesBoolean,
TimeSeriesRate,
TimeSeriesStreamDayRate,
Expand All @@ -15,7 +16,6 @@
ConsumerFunctionResult,
)
from libecalc.core.result import EcalcModelResult
from libecalc.dto.types import RateType


def test_compute_consumer_rate():
Expand Down
3 changes: 1 addition & 2 deletions src/tests/libecalc/core/models/test_fuel_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import numpy as np
from libecalc import dto
from libecalc.common.units import Unit
from libecalc.common.utils.rates import TimeSeriesRate
from libecalc.common.utils.rates import RateType, TimeSeriesRate
from libecalc.core.models.fuel import FuelModel
from libecalc.dto import VariablesMap
from libecalc.dto.types import RateType
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
from datetime import datetime

import libecalc.common.utils.rates
import pytest
from libecalc import dto
from libecalc.common.time_utils import Period
Expand Down Expand Up @@ -134,7 +135,7 @@
load=Expression.setup_from_expression(value="4.1"),
condition=Expression.setup_from_expression(value="SIM2;GAS_LIFT > 0"),
energy_usage_type=dto.types.EnergyUsageType.POWER,
consumption_rate_type=dto.types.RateType.STREAM_DAY,
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
),
None,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import pytest
from libecalc import dto
from libecalc.common.units import Unit
from libecalc.common.utils.rates import TimeSeriesRate
from libecalc.common.utils.rates import RateType, TimeSeriesRate
from libecalc.core.ecalc import EnergyCalculator
from libecalc.core.graph_result import GraphResult
from libecalc.dto.types import RateType


@pytest.fixture
Expand Down