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

Update pyciemss-service intervention converters #119 #120

Merged
merged 1 commit into from
Sep 25, 2024
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
6 changes: 4 additions & 2 deletions service/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ class InterventionSelection(BaseModel):
class HMIStaticIntervention(BaseModel):
timestep: float
value: float
applied_to: str
type: str


class HMIDynamicIntervention(BaseModel):
parameter: str
threshold: float
value: float
applied_to: str
type: str


class HMIInterventionPolicy(BaseModel):
Expand All @@ -60,8 +64,6 @@ class HMIInterventionPolicy(BaseModel):

class HMIIntervention(BaseModel):
name: str
applied_to: str
type: str
static_interventions: Optional[list[HMIStaticIntervention]] = Field(default=None)
dynamic_interventions: Optional[list[HMIDynamicIntervention]] = Field(default=None)

Expand Down
16 changes: 6 additions & 10 deletions service/models/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def fetch_and_convert_static_interventions(policy_intervention_id, job_id):
for inter in policy_intervention["interventions"]:
intervention = HMIIntervention(
name=inter["name"],
applied_to=inter["applied_to"],
type=inter["type"],
static_interventions=inter["static_interventions"],
dynamic_interventions=inter["dynamic_interventions"],
)
Expand All @@ -32,8 +30,6 @@ def fetch_and_convert_dynamic_interventions(policy_intervention_id, job_id):
for inter in policy_intervention["interventions"]:
intervention = HMIIntervention(
name=inter["name"],
applied_to=inter["applied_to"],
type=inter["type"],
static_interventions=inter["static_interventions"],
dynamic_interventions=inter["dynamic_interventions"],
)
Expand All @@ -50,11 +46,11 @@ def convert_static_interventions(interventions: list[HMIIntervention]):
for inter in interventions:
for static_inter in inter.static_interventions:
time = torch.tensor(float(static_inter.timestep))
parameter_name = inter.applied_to
parameter_name = static_inter.applied_to
value = torch.tensor(float(static_inter.value))
if inter.type == "parameter":
if static_inter.type == "parameter":
static_param_interventions[time][parameter_name] = value
if inter.type == "state":
if static_inter.type == "state":
static_state_interventions[time][parameter_name] = value
return static_param_interventions, static_state_interventions

Expand Down Expand Up @@ -83,17 +79,17 @@ def convert_dynamic_interventions(interventions: list[HMIIntervention]):
] = defaultdict(dict)
for inter in interventions:
for dynamic_inter in inter.dynamic_interventions:
parameter_name = inter.applied_to
parameter_name = dynamic_inter.applied_to
threshold_value = torch.tensor(float(dynamic_inter.threshold))
to_value = torch.tensor(float(dynamic_inter.value))
threshold_func = make_var_threshold(
dynamic_inter.parameter, threshold_value
)
if inter.type == "parameter":
if dynamic_inter.type == "parameter":
dynamic_parameter_interventions[threshold_func].update(
{parameter_name: to_value}
)
if inter.type == "state":
if dynamic_inter.type == "state":
dynamic_state_interventions[threshold_func].update(
{parameter_name: to_value}
)
Expand Down
7 changes: 4 additions & 3 deletions tests/examples/simulate/input/intervention.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
"interventions":[
{
"name":"New_Intervention",
"applied_to":"β",
"static_interventions":[
{
"timestep":0,
"value":0
"value":0,
"applied_to":"β"
},
{
"timestep":1,
"value":2
"value":2,
"applied_to":"β"
}
],
"dynamic_interventions":[
Expand Down
Loading