潘志宝
4 天以前 042be316746210a681a21c3ecca6b7a44e793db0
提交 | 用户 | 时间
c4422a 1 package iail.mdk.model.utils;
D 2
3 import java.util.HashMap;
4 import java.util.Map;
5
6
7 /**
8  * @description:
9  * @author: dzd 
10  * @date: 2024/10/12 9:19
11  **/
12 public class AlgsUtils {
13     private HashMap<String, Object> model = new HashMap();
14     private HashMap<String, Object> eval_pre = new HashMap();
15     private HashMap<String, Object> train_result_models = new HashMap();
16
17     public AlgsUtils() {
18     }
19
20     public HashMap<String, Object> createPredictHashmap(HashMap<String, Object> models) {
21         if (models.containsKey("model")) {
22             String aaa;
23             if (((String) ((HashMap) models.get("model")).get("param1")).isEmpty()) {
24                 aaa = "error";
25                 this.model.put("param1", aaa);
26             } else {
27                 aaa = (String) ((HashMap) models.get("model")).get("param1");
28                 this.model.put("param1", aaa);
29             }
30         } else {
31             this.model = models;
32         }
33
34         return this.model;
35     }
36
37     public HashMap<String, Object> createPredictHashmapplus(HashMap<String, Object> models) {
38         if (models.containsKey("models")) {
39             String aaa;
40             if (((String) ((HashMap) models.get("models")).get("paramFile")).isEmpty()) {
41                 aaa = "error";
42                 this.model.put("param1", aaa);
43             } else {
44                 aaa = (String) ((HashMap) models.get("models")).get("paramFile");
45                 this.model.put("paramFile", aaa);
46                 if (((HashMap) models.get("models")).containsKey("dim")) {
47                     Object dim = ((HashMap) models.get("models")).get("dim");
48                     this.model.put("dim", dim);
49                 }
50             }
51         } else {
52             this.model = models;
53         }
54
55         return this.model;
56     }
57
58     public HashMap<String, Object> reverseModels(HashMap<String, Object> train_result) {
59         if (train_result.containsKey("models")) {
60             this.train_result_models = (HashMap) train_result.get("models");
61             if (((HashMap) train_result.get("models")).containsKey("dim")) {
62                 double dim = Double.parseDouble((String) ((HashMap) train_result.get("models")).get("dim"));
63                 this.train_result_models.put("dim", dim);
64             }
65
66             train_result.put("models", this.train_result_models);
67         }
68
69         return train_result;
70     }
71
72     public int[] getColAndRow(double[][] arr) {
73         int row = arr.length;
74         int col = arr[0].length;
75         int[] result = new int[]{row, col};
76         return result;
77     }
78
79     public double[][] getMathergeArr(double[][] data, double[][] refs) {
80         int[] dataRowAndCol = this.getColAndRow(data);
81         int rowData = dataRowAndCol[0];
82         int colData = dataRowAndCol[1];
83         int[] refsRowAndCol = this.getColAndRow(refs);
84         int rowrefs = refsRowAndCol[0];
85         int colrefs = refsRowAndCol[1];
86         double[][] newData = new double[rowData + rowrefs][colData];
87
88         int i;
89         int j;
90         for (i = 0; i < rowData; ++i) {
91             for (j = 0; j < colData; ++j) {
92                 newData[i][j] = data[i][j];
93             }
94         }
95
96         for (i = 0; i < rowrefs; ++i) {
97             for (j = 0; j < colrefs; ++j) {
98                 newData[i + rowData][j] = refs[i][j];
99             }
100         }
101
102         return newData;
103     }
104
105     public HashMap<String, Object> reverseResult(HashMap<String, Object> result) {
106         if (null == result) {
107             return result;
108         }
109         String code = reverseResultCode(result);
110         if (!"100".equals(code)) {
111             return result;
112         }
113         reverseResultValues(result);
114         reverseTest(result);
115         reverseEval(result);
116         reverseOptdParams(result);
117         return result;
118     }
119
120     private void reverseResultValues(HashMap<String, Object> result) {
121         // 将result中的Double数组转为double数组
122         if (result.containsKey("result")) {
123             HashMap<String, Object> resultValues = (HashMap) result.get("result");
124             for (Map.Entry<String, Object> entry : resultValues.entrySet()) {
125                 if (entry.getValue() instanceof Double[]) {
126                     //一维数组
127                     Double[] value = (Double[]) entry.getValue();
128                     double[] value1 = new double[value.length];
129                     for (int i = 0; i < value.length; i++) {
130                         Double d = value[i];
131                         if (Double.isNaN(d)) {
132                             value1[i] = new Double(0.0).doubleValue();
133                         } else {
134                             value1[i] = d.doubleValue();
135                         }
136                     }
137                     resultValues.put(entry.getKey(),value1);
138                 }
139
140                 if (entry.getValue() instanceof Double[][]) {
141                     //二维数组
142                     Double[][] value = (Double[][]) entry.getValue();
143                     double[][] value1 = new double[value.length][];
144                     for (int i = 0; i < value.length; i++) {
145                         value1[i] = new double[value[i].length];
146                         for (int j = 0; j < value[i].length; j++) {
147                             Double d = value[i][j];
148                             if (Double.isNaN(d)) {
149                                 value1[i][j] = new Double(0.0).doubleValue();
150                             } else {
151                                 value1[i][j] = d.doubleValue();
152                             }
153                         }
154                     }
155                     resultValues.put(entry.getKey(),value1);
156                 }
157             }
158         }
159     }
160
161
162     private String reverseResultCode(HashMap<String, Object> result) {
163         String code = result.containsKey("status_code") ? String.valueOf(result.get("status_code")) : "400";
164         result.put("status_code", code);
165         return code;
166     }
167
168     private void reverseTest(HashMap<String, Object> result) {
169         if (result.containsKey("test")) {
170             HashMap<String, Object> test = (HashMap) result.get("test");
171             Double[][] realValue = (Double[][]) test.get("realValue");
172             Double[][] predictValue = (Double[][]) test.get("predictValue");
173             double[][] realValue1 = new double[realValue.length][2];
174             double[][] predictValue1 = new double[predictValue.length][2];
175             for (int i = 0; i < realValue.length; i++) {
176                 for (int j = 0; j < realValue[i].length; j++) {
177                     Double d = (Double) realValue[i][j];
178                     if (Double.isNaN(d)) {
179                         realValue1[i][j] = new Double(0.0).doubleValue();
180                     } else {
181                         realValue1[i][j] = d.doubleValue();
182                     }
183
184                 }
185             }
186             for (int i = 0; i < predictValue.length; i++) {
187                 for (int j = 0; j < predictValue[i].length; j++) {
188                     Double d = (Double) predictValue[i][j];
189                     if (Double.isNaN(d)) {
190                         predictValue1[i][j] = new Double(0.0).doubleValue();
191                     } else {
192                         predictValue1[i][j] = d.doubleValue();
193                     }
194
195                 }
196             }
197             HashMap<String, double[][]> map = new HashMap<>();
198             map.put("realValue", realValue1);
199             map.put("predictValue", predictValue1);
200
201             result.put("test", map);
202         }
203     }
204
205     public void reverseEval(HashMap<String, Object> result) {
206         if (result.containsKey("eval")) {
207             HashMap<String,Object> eval = (HashMap<String,Object>) result.get("eval");
208             HashMap<String, String> evalMap = new HashMap<>(eval.size());
209
210             for (HashMap.Entry<String, Object> entry : eval.entrySet()) {
211                 evalMap.put(entry.getKey(),String.valueOf(entry.getValue()));
212             }
213
214             result.put("eval",evalMap);
215         }
216     }
217
218     private void reverseOptdParams(HashMap<String, Object> result) {
219         if (result.containsKey("paramsopt")) {
220             HashMap<String,Object> params = (HashMap<String,Object>) result.get("paramsopt");
221             HashMap<String, String> newParams = new HashMap<>(params.size());
222
223             for (HashMap.Entry<String, Object> entry : params.entrySet()) {
224                 newParams.put(entry.getKey(),String.valueOf(entry.getValue()));
225             }
226
227             result.put("optdParams",newParams);
228         }
229     }
230 }