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

tested code. replaced dataclass with hashable objects. updated depend… #50

Open
wants to merge 1 commit into
base: feature/generator_new
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions source/package/adaptation_pathways/action_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
An action instance is a synonym for policy action, intervention, and measure, for example
which has a unique set of preceding actions. The instance signifies that the timing of an action
can be different depending on which measures have been implemented before
"""

from .app.model.action import Action
from .app.model.metric import Metric, MetricValue


class ActionInstance:
def __init__(
self,
action: Action,
instance: int,
tipping_point: float,
metric_data: dict[Metric, MetricValue | None],
):
self.action = action
self.instance = instance
self.tipping_point = tipping_point
self.metric_data = metric_data

def __eq__(self, other):
if not isinstance(other, ActionInstance):
return NotImplemented
return (
self.action == other.action
and self.instance == other.instance
and self.tipping_point == other.tipping_point
)

def __hash__(self):
return hash((self.action, self.instance, self.tipping_point))

def __repr__(self):
return (
f"ActionInstance(action={self.action}, instance={self.instance}, "
f"tipping_point={self.tipping_point}, metric_data={self.metric_data})"
)
83 changes: 68 additions & 15 deletions source/package/adaptation_pathways/app/model/action.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,75 @@
import dataclasses
from .comparisons import SequenceComparison
from .metric import Metric, MetricValue

from comparisons import SequenceComparison
from metric import Metric, MetricValue

class ActionDesign:
def __init__(self, color: str, icon: str):
self.color = color
self.icon = icon

def __eq__(self, other):
if not isinstance(other, ActionDesign):
return False
return self.color == other.color and self.icon == other.icon

def __hash__(self):
return hash((self.color, self.icon))


@dataclasses.dataclass
class Action:
id: str
name: str
color: str
icon: str
metric_data: dict[Metric, MetricValue | None]
def __init__(
self,
identifier: str,
name: str,
design: ActionDesign,
metric_data: dict[Metric, MetricValue],
):
self.id = identifier
self.name = name
self.design = design
self.metric_data = metric_data # dict[Metric, MetricValue | None]

def __eq__(self, other):
if not isinstance(other, Action):
return NotImplemented
return self.name == other.name if self.name else self.id == other.id

def __hash__(self):
return hash(self.name) if self.name else hash(self.id)

def __repr__(self):
return (
f"Action(id={self.id}, name={self.name}, design={self.design}"
f"metric_data={self.metric_data})"
)


@dataclasses.dataclass
class ActionDependency:
id: str
action: Action
relation: SequenceComparison
other_actions: list[Action]
actions_in_order: bool
def __init__(
self,
identifier: str,
action: Action,
relation: SequenceComparison,
other_actions: list,
# actions_in_order: bool,
):
self.id = identifier
self.action = action
self.relation = relation
self.other_actions = other_actions # list[Action]
# self.actions_in_order = actions_in_order

def __eq__(self, other):
if not isinstance(other, ActionDependency):
return NotImplemented
return self.id == other.id

def __hash__(self):
return hash(self.id)

def __repr__(self):
return (
f"ActionDependency(id={self.id}, action={self.action}, "
f"relation={self.relation}, other_actions={self.other_actions}, "
# f"actions_in_order={self.actions_in_order})"
)
5 changes: 5 additions & 0 deletions source/package/adaptation_pathways/app/model/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class SequenceComparison(Enum):
DOESNT_CONTAIN = 4
ENDS_WITH = 5
DOESNT_END_WITH = 6
BLOCKS = 7
AFTER = 8
DIRECTLY_AFTER = 9
BEFORE = 10
DIRECTLY_BEFORE = 11


class NumberComparison(Enum):
Expand Down
100 changes: 83 additions & 17 deletions source/package/adaptation_pathways/app/model/filter.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,92 @@
import dataclasses
from .action import Action
from .comparisons import NumberComparison, SequenceComparison
from .metric import Metric

from action import Action
from comparisons import NumberComparison, SequenceComparison
from metric import Metric


@dataclasses.dataclass
class ActionFilter:
relation: SequenceComparison
actions: list[Action]
actions_in_order: bool
def __init__(
self,
relation: SequenceComparison,
actions: list[Action],
actions_in_order: bool,
):
self.relation = relation
self.actions = actions
self.actions_in_order = actions_in_order

def __eq__(self, other):
if not isinstance(other, ActionFilter):
return NotImplemented
return (
self.relation == other.relation
and self.actions == other.actions
and self.actions_in_order == other.actions_in_order
)

def __hash__(self):
return hash((self.relation, tuple(self.actions), self.actions_in_order))

def __repr__(self):
return (
f"ActionFilter(relation={self.relation}, actions={self.actions}, "
f"actions_in_order={self.actions_in_order})"
)


@dataclasses.dataclass
class MetricFilter:
metric: Metric
relation: NumberComparison
value: float
def __init__(self, metric: Metric, relation: NumberComparison, value: float):
self.metric = metric
self.relation = relation
self.value = value

def __eq__(self, other):
if not isinstance(other, MetricFilter):
return NotImplemented
return (
self.metric == other.metric
and self.relation == other.relation
and self.value == other.value
)

def __hash__(self):
return hash((self.metric, self.relation, self.value))

def __repr__(self):
return f"MetricFilter(metric={self.metric}, relation={self.relation}, value={self.value})"


@dataclasses.dataclass
class GenerationConstraints:
action_constraints: list[ActionFilter]
metric_constraints: list[MetricFilter]
max_sequence_length: int | None = None
def __init__(
self,
action_constraints: list[ActionFilter],
metric_constraints: list[MetricFilter],
max_sequence_length: int | None = None,
):
self.action_constraints = action_constraints
self.metric_constraints = metric_constraints
self.max_sequence_length = max_sequence_length

def __eq__(self, other):
if not isinstance(other, GenerationConstraints):
return NotImplemented
return (
self.action_constraints == other.action_constraints
and self.metric_constraints == other.metric_constraints
and self.max_sequence_length == other.max_sequence_length
)

def __hash__(self):
return hash(
(
tuple(self.action_constraints),
tuple(self.metric_constraints),
self.max_sequence_length,
)
)

def __repr__(self):
return (
f"GenerationConstraints(action_constraints={self.action_constraints}, "
f"metric_constraints={self.metric_constraints}, "
f"max_sequence_length={self.max_sequence_length})"
)
78 changes: 64 additions & 14 deletions source/package/adaptation_pathways/app/model/metric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dataclasses
from enum import Enum


Expand All @@ -11,23 +10,74 @@ class MetricEstimate(Enum):
LAST = 6


@dataclasses.dataclass
class MetricUnit:
symbol: str
place_after_value: bool
value_format: str
def __init__(self, symbol: str, place_after_value: bool, value_format: str):
self.symbol = symbol
self.place_after_value = place_after_value
self.value_format = value_format

def __eq__(self, other):
if not isinstance(other, MetricUnit):
return NotImplemented
return (
self.symbol == other.symbol
and self.place_after_value == other.place_after_value
and self.value_format == other.value_format
)

def __hash__(self):
return hash((self.symbol, self.place_after_value, self.value_format))

def __repr__(self):
return (
f"MetricUnit(symbol={self.symbol}, place_after_value={self.place_after_value}, "
f"value_format={self.value_format})"
)


@dataclasses.dataclass
class Metric:
id: str
name: str
unit: MetricUnit
current_value: float
estimate: MetricEstimate
def __init__(
self,
identifier: str,
name: str,
unit: MetricUnit,
# current_value: float,
estimate,
):
self.id = identifier
self.name = name
self.unit = unit
# self.current_value = current_value
self.estimate = estimate

def __eq__(self, other):
if not isinstance(other, Metric):
return NotImplemented
return self.name == other.name if self.name else self.id == other.id

def __hash__(self):
return hash(self.name) if self.name else hash(self.id)

def __repr__(self):
return (
f"Metric(id={self.id}, name={self.name}, unit={self.unit}, "
# f"current_value={self.current_value}, "
f"estimate={self.estimate})"
)


@dataclasses.dataclass
class MetricValue:
value: float
is_estimate: bool
def __init__(self, value: float, is_estimate: bool):
self.value = value
self.is_estimate = is_estimate

def __eq__(self, other):
if not isinstance(other, MetricValue):
return NotImplemented
return self.value == other.value and self.is_estimate == other.is_estimate

def __hash__(self):
return hash((self.value, self.is_estimate))

def __repr__(self):
return f"MetricValue(value={self.value}, is_estimate={self.is_estimate})"
Loading