Coverage for backend/idaes_factory/adapters/unit_models/ml_adapter.py: 99%

65 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-11-06 23:27 +0000

1from ..core_adapters import * 

2from ...idaes_factory_context import IdaesFactoryContext 

3from flowsheetInternals.unitops.models import * 

4from core.auxiliary.models import MLModel 

5from core.auxiliary.enums.generalEnums import PropertyType 

6from core.auxiliary.models.PropertyValue import PropertyValue 

7from core.auxiliary.enums import ConType 

8from flowsheetInternals.unitops.models.Port import Port 

9from flowsheetInternals.unitops.models.SimulationObject import SimulationObject 

10from ..stream_properties import serialise_stream 

11from ...queryset_lookup import get_all_ports 

12from idaes_factory.adapters.generic_adapters import NumInletsAdapter, NumOutletsAdapter 

13from core.auxiliary.models.MLColumnMapping import custom_property_port_index 

14 

15class MLPropetiesAdapter: 

16 def serialise(self, ctx: IdaesFactoryContext, simulationObject: SimulationObject): 

17 result = {} 

18 # TODO: This doesn't really handle the controlled variables, etc, and doesn't work 

19 # for dynamics 

20 # We should deprecate config files and then everntually this can be handled 

21 # in a similar way to other unit models. 

22 for item in simulationObject.properties.ContainedProperties.all(): 

23 if item is not None: 23 ↛ 22line 23 didn't jump to line 22 because the condition on line 23 was always true

24 prop_value = PropertyValue.objects.get(property=item) 

25 value = prop_value.value 

26 if not (prop_value.is_enabled()): 

27 value = None 

28 

29 result[item.key] = { 

30 "data": [{ 

31 "id": prop_value.pk, 

32 "value": value, 

33 }], 

34 "unit": "dimensionless" 

35 } 

36 return result 

37 

38 

39class JSONModelAdapter: 

40 def serialise(self, ctx: IdaesFactoryContext, simulationObject: SimulationObject): 

41 return MLModel.objects.get(simulationObject=simulationObject).surrogate_model 

42 

43 

44class IDAdapter: 

45 def serialise(self, ctx: IdaesFactoryContext, simulationObject: SimulationObject): 

46 ml_model = MLModel.objects.get(simulationObject=simulationObject) 

47 result = { 

48 "input": [], 

49 "output": [] 

50 } 

51 for item in ml_model.MLColumnMappings.all(): 

52 direction = ConType.Inlet if item.propertyType == PropertyType.InletProperty else ConType.Outlet 

53 if item.portIndex == custom_property_port_index: 

54 # can get directly from unitop properties 

55 propertyInfo = ml_model.simulationObject.properties.get_property(item.propertyKey) 

56 else: 

57 # these are on streams, so we need to get from the ports 

58 port = Port.objects.get(unitOp=ml_model.simulationObject, index=item.portIndex, direction=direction) 

59 propertyInfo = port.stream.properties.get_property(item.propertyKey) 

60 if item.propertyType == PropertyType.InletProperty: 

61 result["input"].append(propertyInfo.values.first().id) 

62 else: 

63 result["output"].append(propertyInfo.values.first().id) 

64 return result 

65 

66 

67class unitopNamesAdapter: 

68 def serialise(self, ctx: IdaesFactoryContext, simulationObject: SimulationObject): 

69 ml_model = MLModel.objects.get(simulationObject=simulationObject) 

70 result = [] 

71 for item in ml_model.MLColumnMappings.filter(portIndex=custom_property_port_index): 

72 result.append(item.column) 

73 

74 return result 

75 

76class MLPortListAdapter(PortListAdapter): 

77 """ 

78 Handles the serialisation and reloading of the ports of a mixer. 

79 """ 

80 def __init__(self): 

81 pass 

82 

83 def serialise(self, ctx, unit_model: SimulationObject) -> dict: 

84 inlets = get_all_ports(unit_model, "inlet") 

85 outlets = get_all_ports(unit_model, "outlet") 

86 

87 portList = {} 

88 id = 1 

89 

90 for inlet in inlets: 

91 portList[f"inlet_{id}"] = { 

92 "id": inlet.pk, 

93 "properties": serialise_stream(ctx, inlet.stream, is_inlet=True) 

94 } 

95 id += 1 

96 

97 id = 1 

98 

99 for outlet in outlets: 

100 portList[f"outlet_{id}"] = { 

101 "id": inlet.pk, 

102 "properties": serialise_stream(ctx, outlet.stream, is_inlet=False) 

103 } 

104 id += 1 

105 

106 return portList 

107 

108 

109ml_adapter = UnitModelAdapter( 

110 args=ArgAdapter({ 

111 "property_package": PropertyPackageAdapter(), 

112 "model": JSONModelAdapter(), 

113 "ids": IDAdapter(), 

114 'unitopNames': unitopNamesAdapter(), 

115 "num_inlets": NumInletsAdapter(), 

116 "num_outlets": NumOutletsAdapter() 

117 }), 

118 properties=MLPropetiesAdapter(), 

119 ports=MLPortListAdapter() 

120)