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