Coverage for backend/core/auxiliary/models/MLWizard.py: 100%

46 statements  

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

1import json 

2from core.auxiliary.models.MLModel import MLModel 

3from core.auxiliary.models.MLColumnMapping import MLColumnMapping, custom_property_port_index 

4from core.auxiliary.enums.generalEnums import PropertyType 

5from django.db.models import Q 

6from core.auxiliary.models.PropertyInfo import PropertyInfo 

7from core.auxiliary.models.PropertyValue import PropertyValue 

8from flowsheetInternals.unitops.config.config_methods import get_property_fields 

9from common.config_types import PropertyType as PropertyTypeObj 

10from typing import TypedDict, List 

11from idaes_factory.endpoints import start_ml_training_event 

12 

13class ColumnMapping(TypedDict): 

14 portIndex: int 

15 propertyKey: str 

16 column: str 

17 

18 

19def create_column_mapping(flowsheet, model: int, inlet_mappings: List[ColumnMapping], outlet_mappings: List[ColumnMapping]): 

20 bulk_create_objects = [] 

21 

22 mappings = [ 

23 (inlet_mappings, PropertyType.InletProperty), 

24 (outlet_mappings, PropertyType.OutletProperty), 

25 ] 

26 

27 for mappings_list, property_type in mappings: 

28 for i, mapping in enumerate(mappings_list): 

29 propertyKey = mapping.get("propertyKey") 

30 portIndex = mapping.get("portIndex") 

31 column = mapping.get("column") 

32 ml_model = MLModel.objects.get(id=model) 

33 property_set = ml_model.simulationObject.properties 

34 

35 

36 # if unitop property 

37 if portIndex == custom_property_port_index: 

38 prop = PropertyTypeObj( 

39 displayName=propertyKey, 

40 value="", 

41 unitType="ratio", 

42 type="numeric", 

43 ) 

44 fields = get_property_fields(propertyKey, prop, property_set, 0) 

45 value = fields.pop("value") # since property value is separated from property info 

46 propertyInfo = PropertyInfo.objects.create(**fields, flowsheet=flowsheet) 

47 PropertyValue.objects.create(value=value, property=propertyInfo, enabled=property_type==PropertyType.InletProperty, flowsheet=flowsheet) 

48 

49 bulk_create_objects.append(MLColumnMapping( 

50 order=i, 

51 model=ml_model, 

52 column=column, 

53 portIndex=portIndex, 

54 propertyKey=propertyKey, 

55 propertyType=property_type, 

56 flowsheet=flowsheet 

57 )) 

58 

59 MLColumnMapping.objects.bulk_create(bulk_create_objects) 

60 MLModel.objects.update(progress=2) 

61 

62 

63def train(user, model_instance: MLModel): 

64 csv_data = model_instance.csv_data 

65 

66 input_columns = MLColumnMapping.objects.filter(model=model_instance, propertyType=PropertyType.InletProperty) 

67 output_columns = MLColumnMapping.objects.filter(model=model_instance, propertyType=PropertyType.OutletProperty) 

68 

69 data = json.loads(csv_data) 

70 input_labels = [item.column for item in input_columns] 

71 output_labels = [item.column for item in output_columns] 

72 headers = input_labels + output_labels 

73 reordered_json = [ 

74 {header: row[header] for header in headers} for row in data 

75 ] 

76 

77 columns = [key for key in reordered_json[0].keys() if key] 

78 datapoints = [[float(row[col]) for col in columns] for row in data] 

79 

80 return start_ml_training_event( 

81 datapoints=datapoints, 

82 columns=columns, 

83 input_labels=input_labels, 

84 output_labels=output_labels, 

85 user=user, 

86 flowsheet_id=model_instance.flowsheet.id, 

87 )