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