Coverage for backend/django/flowsheetInternals/unitops/config/objects/machine_learning_block_config.py: 100%
68 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-06-23 21:51 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-06-23 21:51 +0000
1from common.config_types import *
2from common.config_utils import *
3from ..spec_helpers import *
5class MLPropetiesAdapter:
6 def serialise(self, ctx, simulationObject):
7 from core.auxiliary.models import MLModel
9 # TODO: This doesn't really handle the controlled variables, etc, and doesn't work
10 # for dynamics
11 # We should deprecate config files and then everntually this can be handled
12 # in a similar way to other unit models.
13 ml_model = MLModel.objects.get(simulationObject=simulationObject)
14 return get_ml_properties(ctx, ml_model)
18def get_ml_properties(ctx, mlModel):
19 from idaes_factory.adapters.property_info_adapter import serialise_property_info
21 result = {}
22 for column_mapping in mlModel.MLColumnMappings.all():
23 propertyInfo = column_mapping.propertyInfo
24 if propertyInfo is not None:
25 result[column_mapping.propertyKey] = serialise_property_info(ctx, propertyInfo, is_tear=False, is_indexed=False)
26 return result
28class JSONModelAdapter:
29 def serialise(self, ctx, simulationObject):
30 from core.auxiliary.models import MLModel
32 return ValueArgSchema(
33 value=MLModel.objects.get(simulationObject=simulationObject).surrogate_model
34 )
37class IDAdapter:
38 def serialise(self, ctx, simulationObject):
39 from core.auxiliary.models import MLModel
40 ml_model = MLModel.objects.get(simulationObject=simulationObject)
41 return get_id_mappings(ml_model)
44def get_id_mappings(ml_model) -> ValueArgSchema:
45 from core.auxiliary.models.MLColumnMapping import custom_property_port_index
46 from flowsheetInternals.unitops.models.Port import Port
47 from core.auxiliary.enums.generalEnums import PropertyType
49 result = {
50 "input": [],
51 "output": []
52 }
53 for mapping in ml_model.MLColumnMappings.all():
54 direction = ConType.Inlet if mapping.propertyType == PropertyType.InletProperty else ConType.Outlet
55 if mapping.portIndex == custom_property_port_index:
56 propertyInfo = mapping.propertyInfo
57 else:
58 # these are on streams, so we need to get from the ports
59 port = Port.objects.get(unitOp=ml_model.simulationObject, index=mapping.portIndex, direction=direction)
60 propertyInfo = port.stream.properties.get_property(mapping.propertyKey)
61 if mapping.propertyType == PropertyType.InletProperty:
62 result["input"].append(propertyInfo.values.first().id)
63 else:
64 result["output"].append(propertyInfo.values.first().id)
65 return ValueArgSchema(value=result)
68class unitopNamesAdapter:
69 def serialise(self, ctx, simulationObject):
70 from core.auxiliary.models import MLModel
72 ml_model = MLModel.objects.get(simulationObject=simulationObject)
73 return get_unitop_names(ml_model)
75def get_unitop_names(ml_model) -> ValueArgSchema:
76 from core.auxiliary.models.MLColumnMapping import custom_property_port_index
78 result = []
79 for item in ml_model.MLColumnMappings.filter(portIndex=custom_property_port_index):
80 result.append(item.column)
82 return ValueArgSchema(value=result)
84class MLPortListAdapter(PortListAdapter):
85 """
86 Handles the serialisation and reloading of the ports of a mixer.
87 """
88 def __init__(self):
89 pass
91 def serialise(self, ctx, unit_model) -> dict:
92 inlets = get_all_ports(unit_model, "inlet")
93 outlets = get_all_ports(unit_model, "outlet")
95 portList = {}
96 id = 1
98 for inlet in inlets:
99 portList[f"inlet_{id}"] = {
100 "id": inlet.pk,
101 "properties": serialise_stream(ctx, inlet.stream, is_inlet=True)
102 }
103 id += 1
105 id = 1
107 for outlet in outlets:
108 portList[f"outlet_{id}"] = {
109 "id": inlet.pk,
110 "properties": serialise_stream(ctx, outlet.stream, is_inlet=False)
111 }
112 id += 1
114 return portList
117machine_learning_block_config: ObjectType = ObjectType(
118 displayType="Machine Learning",
119 displayName="Machine Learning",
120 graphicObject=unitop_graphic(),
121 propertyPackagePorts=default_property_package_ports(),
122 ports={
123 "inlet": PortType(
124 many=True,
125 displayName="Inlet",
126 type=ConType.Inlet,
127 default=1, # default number of inlets
128 minimum=1, # minimum number of inlets
129 streamName="S"
130 ),
131 "outlet": PortType(
132 many=True,
133 displayName="Outlet",
134 type=ConType.Outlet,
135 default=1, # default number of inlets
136 minimum=1, # minimum number of inlets
137 streamName="S"
138 )
139 },
140 properties=PropertiesType({
141 }),
142 propertySetGroups={
143 "default": {
144 "type": "All",
145 "displayName": "Properties",
146 },
147 },
148 keyProperties=[],
149 idaes_adapter=UnitModelAdapter(
150 args=ArgAdapter({
151 "property_package": PropertyPackageAdapter(),
152 "model": JSONModelAdapter(),
153 "ids": IDAdapter(),
154 "unitopNames": unitopNamesAdapter(),
155 "num_inlets": NumInletsAdapter(),
156 "num_outlets": NumOutletsAdapter(),
157 }),
158 properties=MLPropetiesAdapter(),
159 ports=MLPortListAdapter()
160 ),
161)