提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mdk.predict.impl; |
潘 |
2 |
|
|
3 |
import com.alibaba.fastjson.JSONArray; |
|
4 |
import com.alibaba.fastjson.JSONObject; |
|
5 |
import com.iail.model.IAILModel; |
4f1717
|
6 |
import com.iailab.module.model.common.enums.CommonConstant; |
373ab1
|
7 |
import com.iailab.module.model.common.enums.OutResultType; |
69bd5e
|
8 |
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
7fd198
|
9 |
import com.iailab.module.model.mcs.pre.entity.MmModelArithSettingsEntity; |
潘 |
10 |
import com.iailab.module.model.mcs.pre.entity.MmPredictModelEntity; |
69bd5e
|
11 |
import com.iailab.module.model.mcs.pre.service.MmItemOutputService; |
7fd198
|
12 |
import com.iailab.module.model.mcs.pre.service.MmModelArithSettingsService; |
潘 |
13 |
import com.iailab.module.model.mdk.common.enums.TypeA; |
|
14 |
import com.iailab.module.model.mdk.common.exceptions.ModelInvokeException; |
|
15 |
import com.iailab.module.model.mdk.predict.PredictModelHandler; |
|
16 |
import com.iailab.module.model.mdk.sample.SampleConstructor; |
|
17 |
import com.iailab.module.model.mdk.sample.dto.SampleData; |
|
18 |
import com.iailab.module.model.mdk.vo.PredictResultVO; |
1a2b62
|
19 |
import com.iailab.module.model.mpk.common.utils.DllUtils; |
7fd198
|
20 |
import lombok.extern.slf4j.Slf4j; |
潘 |
21 |
import org.springframework.beans.factory.annotation.Autowired; |
|
22 |
import org.springframework.stereotype.Component; |
|
23 |
|
69bd5e
|
24 |
import java.util.*; |
7fd198
|
25 |
|
潘 |
26 |
/** |
|
27 |
* @author PanZhibao |
|
28 |
* @Description |
|
29 |
* @createTime 2024年09月01日 |
|
30 |
*/ |
|
31 |
@Slf4j |
|
32 |
@Component |
|
33 |
public class PredictModelHandlerImpl implements PredictModelHandler { |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private MmModelArithSettingsService mmModelArithSettingsService; |
|
37 |
|
|
38 |
@Autowired |
69bd5e
|
39 |
private MmItemOutputService mmItemOutputService; |
7fd198
|
40 |
|
潘 |
41 |
@Autowired |
|
42 |
private SampleConstructor sampleConstructor; |
|
43 |
|
4f1717
|
44 |
/** |
潘 |
45 |
* 根据模型预测,返回预测结果 |
|
46 |
* |
|
47 |
* @param predictTime |
|
48 |
* @param predictModel |
|
49 |
* @return |
|
50 |
* @throws ModelInvokeException |
|
51 |
*/ |
7fd198
|
52 |
@Override |
4f1717
|
53 |
public synchronized PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel) throws ModelInvokeException { |
7fd198
|
54 |
PredictResultVO result = new PredictResultVO(); |
潘 |
55 |
if (predictModel == null) { |
|
56 |
throw new ModelInvokeException("modelEntity is null"); |
|
57 |
} |
|
58 |
String modelId = predictModel.getId(); |
|
59 |
try { |
|
60 |
List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Predict.name(), modelId, predictTime); |
|
61 |
String modelPath = predictModel.getModelpath(); |
|
62 |
if (modelPath == null) { |
373ab1
|
63 |
log.info("模型路径不存在,modelId=" + modelId); |
7fd198
|
64 |
return null; |
潘 |
65 |
} |
|
66 |
IAILModel newModelBean = composeNewModelBean(predictModel); |
|
67 |
HashMap<String, Object> settings = getPredictSettingsByModelId(modelId); |
|
68 |
if (settings == null) { |
|
69 |
log.error("模型setting不存在,modelId=" + modelId); |
|
70 |
return null; |
|
71 |
} |
|
72 |
int portLength = sampleDataList.size(); |
|
73 |
Object[] param2Values = new Object[portLength + 2]; |
|
74 |
for (int i = 0; i < portLength; i++) { |
4f1717
|
75 |
param2Values[i] = sampleDataList.get(i).getMatrix(); |
7fd198
|
76 |
} |
潘 |
77 |
param2Values[portLength] = newModelBean.getDataMap().get("models"); |
4f1717
|
78 |
param2Values[portLength + 1] = settings; |
7fd198
|
79 |
|
潘 |
80 |
log.info("#######################预测模型 " + predictModel.getItemid() + " ##########################"); |
|
81 |
JSONObject jsonObjNewModelBean = new JSONObject(); |
|
82 |
jsonObjNewModelBean.put("newModelBean", newModelBean); |
|
83 |
log.info(String.valueOf(jsonObjNewModelBean)); |
|
84 |
JSONObject jsonObjParam2Values = new JSONObject(); |
|
85 |
jsonObjParam2Values.put("param2Values", param2Values); |
|
86 |
log.info(String.valueOf(jsonObjParam2Values)); |
|
87 |
|
|
88 |
//IAILMDK.run |
1a2b62
|
89 |
HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, predictModel.getMpkprojectid()); |
4f1717
|
90 |
if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) || |
潘 |
91 |
!modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) { |
b2aca2
|
92 |
throw new RuntimeException("模型结果异常:" + modelResult); |
D |
93 |
} |
4f1717
|
94 |
modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT); |
7fd198
|
95 |
//打印结果 |
51c1c2
|
96 |
log.info("预测模型计算完成:modelId=" + modelId + modelResult); |
7fd198
|
97 |
JSONObject jsonObjResult = new JSONObject(); |
1a2b62
|
98 |
jsonObjResult.put("result", modelResult); |
7fd198
|
99 |
log.info(String.valueOf(jsonObjResult)); |
潘 |
100 |
|
373ab1
|
101 |
List<MmItemOutputEntity> itemOutputList = mmItemOutputService.getByItemid(predictModel.getItemid()); |
潘 |
102 |
Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>(itemOutputList.size()); |
|
103 |
for (MmItemOutputEntity output : itemOutputList) { |
|
104 |
if (!modelResult.containsKey(output.getResultstr())) { |
|
105 |
continue; |
|
106 |
} |
|
107 |
OutResultType outResultType = OutResultType.getEumByCode(output.getResultType()); |
|
108 |
switch (outResultType) { |
|
109 |
case D1: |
|
110 |
double[] temp1 = (double[]) modelResult.get(output.getResultstr()); |
|
111 |
predictMatrixs.put(output, temp1); |
|
112 |
break; |
|
113 |
case D2: |
|
114 |
double[][] temp2 = (double[][]) modelResult.get(output.getResultstr()); |
|
115 |
double[] tempColumn = new double[temp2.length]; |
|
116 |
for (int i = 0; i < tempColumn.length; i++) { |
|
117 |
tempColumn[i] = temp2[i][output.getResultIndex()]; |
69bd5e
|
118 |
} |
373ab1
|
119 |
predictMatrixs.put(output, tempColumn); |
潘 |
120 |
break; |
|
121 |
default: |
|
122 |
break; |
1a2b62
|
123 |
} |
D |
124 |
} |
69bd5e
|
125 |
result.setPredictMatrixs(predictMatrixs); |
1a2b62
|
126 |
result.setModelResult(modelResult); |
7fd198
|
127 |
result.setPredictTime(predictTime); |
潘 |
128 |
} catch (Exception ex) { |
4f1717
|
129 |
log.error("调用发生异常,异常信息为:{}", ex); |
7fd198
|
130 |
ex.printStackTrace(); |
ead005
|
131 |
throw new ModelInvokeException(ex.getMessage()); |
7fd198
|
132 |
} |
潘 |
133 |
return result; |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* 构造IAILMDK.run()方法的newModelBean参数 |
|
138 |
* |
|
139 |
* @param predictModel |
|
140 |
* @return |
|
141 |
*/ |
|
142 |
private IAILModel composeNewModelBean(MmPredictModelEntity predictModel) { |
|
143 |
IAILModel newModelBean = new IAILModel(); |
|
144 |
newModelBean.setClassName(predictModel.getClassname().trim()); |
|
145 |
newModelBean.setMethodName(predictModel.getMethodname().trim()); |
|
146 |
//构造参数类型 |
|
147 |
String[] paArStr = predictModel.getModelparamstructure().trim().split(","); |
|
148 |
Class<?>[] paramsArray = new Class[paArStr.length]; |
|
149 |
for (int i = 0; i < paArStr.length; i++) { |
|
150 |
if ("[[D".equals(paArStr[i])) { |
|
151 |
paramsArray[i] = double[][].class; |
|
152 |
} else if ("Map".equals(paArStr[i]) || "java.util.HashMap".equals(paArStr[i])) { |
|
153 |
paramsArray[i] = HashMap.class; |
|
154 |
} |
|
155 |
} |
|
156 |
newModelBean.setParamsArray(paramsArray); |
|
157 |
HashMap<String, Object> dataMap = new HashMap<>(); |
|
158 |
HashMap<String, String> models = new HashMap<>(1); |
b2aca2
|
159 |
models.put("model_path", predictModel.getModelpath()); |
7fd198
|
160 |
dataMap.put("models", models); |
潘 |
161 |
newModelBean.setDataMap(dataMap); |
|
162 |
return newModelBean; |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* 根据模型id获取参数map |
|
167 |
* |
|
168 |
* @param modelId |
|
169 |
* @return |
|
170 |
*/ |
|
171 |
private HashMap<String, Object> getPredictSettingsByModelId(String modelId) { |
|
172 |
List<MmModelArithSettingsEntity> list = mmModelArithSettingsService.getByModelId(modelId); |
|
173 |
HashMap<String, Object> result = new HashMap<>(); |
|
174 |
for (MmModelArithSettingsEntity entry : list) { |
|
175 |
String valueType = entry.getValuetype().trim(); //去除两端空格 |
|
176 |
if ("int".equals(valueType)) { |
|
177 |
int value = Integer.parseInt(entry.getValue()); |
|
178 |
result.put(entry.getKey(), value); |
|
179 |
} else if ("double".equals(valueType)) { |
|
180 |
double value = Double.parseDouble(entry.getValue()); |
|
181 |
result.put(entry.getKey(), value); |
|
182 |
} else if ("string".equals(valueType)) { |
|
183 |
String value = entry.getValue(); |
|
184 |
result.put(entry.getKey(), value); |
|
185 |
} else if ("decimalArray".equals(valueType)) { |
|
186 |
JSONArray valueArray = JSONArray.parseArray(entry.getValue()); |
|
187 |
double[] value = new double[valueArray.size()]; |
|
188 |
for (int i = 0; i < valueArray.size(); i++) { |
|
189 |
value[i] = Double.parseDouble(valueArray.get(i).toString()); |
|
190 |
} |
|
191 |
result.put(entry.getKey(), value); |
|
192 |
} else if ("decimal".equals(valueType)) { |
|
193 |
double value = Double.parseDouble(entry.getValue()); |
|
194 |
result.put(entry.getKey(), value); |
|
195 |
} |
|
196 |
} |
|
197 |
return result; |
|
198 |
} |
|
199 |
} |