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