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 model = new HashMap(); private HashMap eval_pre = new HashMap(); private HashMap train_result_models = new HashMap(); public AlgsUtils() { } public HashMap createPredictHashmap(HashMap 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 createPredictHashmapplus(HashMap 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 reverseModels(HashMap 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 reverseResult(HashMap 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 result) { // 将result中的Double数组转为double数组 if (result.containsKey("result")) { HashMap resultValues = (HashMap) result.get("result"); for (Map.Entry 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 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 result) { if (result.containsKey("test")) { HashMap 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 map = new HashMap<>(); map.put("realValue", realValue1); map.put("predictValue", predictValue1); result.put("test", map); } } public void reverseEval(HashMap result) { if (result.containsKey("eval")) { HashMap eval = (HashMap) result.get("eval"); HashMap evalMap = new HashMap<>(eval.size()); for (HashMap.Entry entry : eval.entrySet()) { evalMap.put(entry.getKey(),String.valueOf(entry.getValue())); } result.put("eval",evalMap); } } private void reverseOptdParams(HashMap result) { if (result.containsKey("paramsopt")) { HashMap params = (HashMap) result.get("paramsopt"); HashMap newParams = new HashMap<>(params.size()); for (HashMap.Entry entry : params.entrySet()) { newParams.put(entry.getKey(),String.valueOf(entry.getValue())); } result.put("optdParams",newParams); } } }