Coverage for backend/django/flowsheetInternals/unitops/config/objects/machine_learning_block_config.py: 99%
63 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-05-13 02:47 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-05-13 02:47 +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.PropertyValue import PropertyValue
8 from idaes_factory.adapters.property_info_adapter import serialise_property_info
10 result = {}
11 # TODO: This doesn't really handle the controlled variables, etc, and doesn't work
12 # for dynamics
13 # We should deprecate config files and then everntually this can be handled
14 # in a similar way to other unit models.
15 for item in simulationObject.properties.ContainedProperties.all():
16 if item is not None: 16 ↛ 15line 16 didn't jump to line 15 because the condition on line 16 was always true
17 prop_value = PropertyValue.objects.get(property=item)
18 if not (prop_value.is_enabled()):
19 prop_value.value = None
20 result[item.key] = serialise_property_info(ctx,item, is_tear=False, is_indexed=False)
21 return result
24class JSONModelAdapter:
25 def serialise(self, ctx, simulationObject):
26 from core.auxiliary.models import MLModel
28 return ValueArgSchema(
29 value=MLModel.objects.get(simulationObject=simulationObject).surrogate_model
30 )
33class IDAdapter:
34 def serialise(self, ctx, simulationObject):
35 from core.auxiliary.enums.generalEnums import PropertyType
36 from core.auxiliary.models import MLModel
37 from core.auxiliary.models.MLColumnMapping import custom_property_port_index
38 from flowsheetInternals.unitops.models.Port import Port
40 ml_model = MLModel.objects.get(simulationObject=simulationObject)
41 result = {
42 "input": [],
43 "output": []
44 }
45 for item in ml_model.MLColumnMappings.all():
46 direction = ConType.Inlet if item.propertyType == PropertyType.InletProperty else ConType.Outlet
47 if item.portIndex == custom_property_port_index:
48 # can get directly from unitop properties
49 propertyInfo = ml_model.simulationObject.properties.get_property(item.propertyKey)
50 else:
51 # these are on streams, so we need to get from the ports
52 port = Port.objects.get(unitOp=ml_model.simulationObject, index=item.portIndex, direction=direction)
53 propertyInfo = port.stream.properties.get_property(item.propertyKey)
54 if item.propertyType == PropertyType.InletProperty:
55 result["input"].append(propertyInfo.values.first().id)
56 else:
57 result["output"].append(propertyInfo.values.first().id)
58 return ValueArgSchema(value=result)
61class unitopNamesAdapter:
62 def serialise(self, ctx, simulationObject):
63 from core.auxiliary.models import MLModel
64 from core.auxiliary.models.MLColumnMapping import custom_property_port_index
66 ml_model = MLModel.objects.get(simulationObject=simulationObject)
67 result = []
68 for item in ml_model.MLColumnMappings.filter(portIndex=custom_property_port_index):
69 result.append(item.column)
71 return ValueArgSchema(value=result)
73class MLPortListAdapter(PortListAdapter):
74 """
75 Handles the serialisation and reloading of the ports of a mixer.
76 """
77 def __init__(self):
78 pass
80 def serialise(self, ctx, unit_model) -> dict:
81 inlets = get_all_ports(unit_model, "inlet")
82 outlets = get_all_ports(unit_model, "outlet")
84 portList = {}
85 id = 1
87 for inlet in inlets:
88 portList[f"inlet_{id}"] = {
89 "id": inlet.pk,
90 "properties": serialise_stream(ctx, inlet.stream, is_inlet=True)
91 }
92 id += 1
94 id = 1
96 for outlet in outlets:
97 portList[f"outlet_{id}"] = {
98 "id": inlet.pk,
99 "properties": serialise_stream(ctx, outlet.stream, is_inlet=False)
100 }
101 id += 1
103 return portList
106machine_learning_block_config: ObjectType = ObjectType(
107 displayType="Machine Learning",
108 displayName="Machine Learning",
109 graphicObject=unitop_graphic(),
110 propertyPackagePorts=default_property_package_ports(),
111 ports={
112 "inlet": PortType(
113 many=True,
114 displayName="Inlet",
115 type=ConType.Inlet,
116 default=1, # default number of inlets
117 minimum=1, # minimum number of inlets
118 streamName="S"
119 ),
120 "outlet": PortType(
121 many=True,
122 displayName="Outlet",
123 type=ConType.Outlet,
124 default=1, # default number of inlets
125 minimum=1, # minimum number of inlets
126 streamName="S"
127 )
128 },
129 properties=PropertiesType({
130 }),
131 propertySetGroups={
132 "default": {
133 "type": "All",
134 "displayName": "Properties",
135 },
136 },
137 keyProperties=[],
138 idaes_adapter=UnitModelAdapter(
139 args=ArgAdapter({
140 "property_package": PropertyPackageAdapter(),
141 "model": JSONModelAdapter(),
142 "ids": IDAdapter(),
143 "unitopNames": unitopNamesAdapter(),
144 "num_inlets": NumInletsAdapter(),
145 "num_outlets": NumOutletsAdapter(),
146 }),
147 properties=MLPropetiesAdapter(),
148 ports=MLPortListAdapter()
149 ),
150)