Procházet zdrojové kódy

性能验证模块 开发 (二期) / 添加线性验证 - 评价斜率 结论模板内容

liyuan před 1 rokem
rodič
revize
bceac7238e

+ 69 - 60
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/model2/LinearRangeAverageSlope.java

@@ -1,10 +1,7 @@
 package com.lc.ibps.components.verification.model2;
 
 import com.lc.ibps.components.verification.model.InspectionConfigVO;
-import com.lc.ibps.components.verification.regression.PolynomialRegression;
 import com.lc.ibps.components.verification.report.TableDTO;
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.commons.math3.stat.regression.RegressionResults;
 import org.apache.commons.math3.stat.regression.SimpleRegression;
 
 import java.util.ArrayList;
@@ -12,89 +9,46 @@ import java.util.List;
 
 public class LinearRangeAverageSlope extends PVModel {
 
-    private final double[] means;
-    private final double[][] diffs;
-    private final double[][] dli;
-    private double maxDli;
-    private double mean;
-    private final double[] standardDeviations;
-
-    private double[] targetValues;
-    private boolean isTarget = true;
-
-
-    private final PolynomialRegression pr1 = new PolynomialRegression();
-
-    private double sdr;
-    private double cvr;
+    private double slope;
+    private double intercept;
+    private double slopeCI;//ConfidenceInterval
+    private double rSquare;
+    private double r;
 
     public LinearRangeAverageSlope(double[][] data, InspectionConfigVO configVO) {
         super(data, configVO);
-
-        this.means = new double[this.specimensNum];
-        this.standardDeviations = new double[this.specimensNum];
-        this.diffs = new double[5][this.specimensNum];
-        this.dli = new double[5][this.specimensNum];
-        this.targetValues = new double[this.specimensNum];
-        calcTarget(configVO.getTargetValue());
     }
 
     @Override
     public void calculate() {
         final SimpleRegression regression = new SimpleRegression();
         regression.addData(data);
-        RegressionResults results = regression.regress();
-//        results.
-
-    }
-
-    private void calcTarget(double[] targets) {
-        if (ArrayUtils.isEmpty(targets)) {
-            for (int i = 0; i < specimensNum; i++) {
-                targetValues[i] = i + 1;
-            }
-            isTarget = false;
-        } else if (targets.length == 2) {
-            targetValues[0] = targets[0];
-            targetValues[specimensNum - 1] = targets[1];
-            int range = specimensNum - 1;
-            for (int i = 1; i < range; i++) {
-                targetValues[i] = format(targets[0] * (range - i) / range + targets[1] * i / range, 2);
-            }
-        } else if (targets.length == specimensNum) {
-            targetValues = targets;
-        }
+        slope = regression.getSlope();
+        intercept = regression.getIntercept();
+        slopeCI = regression.getSlopeConfidenceInterval();
+        rSquare = regression.getRSquare();
+        r = regression.getR();
+
     }
 
+
     public TableDTO buildDataTableDTO() {
         List<String> header = new ArrayList<String>();
 
         header.add("标本号");
-        if (isTarget) header.add("目标值");
         for (int i = 1; i <= repeatNum; i++) {
             header.add(String.format("重复#%d", i));
         }
         TableDTO table = new TableDTO();
         table.buildHeader(header.toArray(new String[header.size()]));
 
-        String[][] r = new String[specimensNum][];
-        for (int i = 0; i < data.length; i++) {
-            List<String> rl = new ArrayList();
-            rl.add(String.valueOf(i + 1));
-            if (isTarget) rl.add(String.valueOf(targetValues[i]));
-
-            for (int j = 0; j < data[i].length; j++) {
-                rl.add(format(data[i][j]));
-            }
-            r[i] = rl.toArray(new String[rl.size()]);
-        }
-        table.buildData(r);
+        table.buildDataWithIndex(getData(),getScale());
         return table;
     }
 
     @Override
     public String generateResult() {
-        return renderReportTemplate(this, null);
+        return renderReportTemplate(this, "/report/linearRangeAverageSlope.ftl");
     }
 
 
@@ -108,4 +62,59 @@ public class LinearRangeAverageSlope extends PVModel {
         return new String[0];
     }
 
+    public String getFunction() {
+        double b0 = intercept;
+        double b1 = slope;
+
+        return "y =" +
+                (b0 >= 0 ? " " : " -") +
+                Math.abs(b0) +
+                (b1 > 0 ? " + " : " - ") +
+                Math.abs(b1) +
+                "x";
+    }
+
+    public String getSlopeCIFormat(){
+        return String.format("%s ~ %s)", slope - slopeCI,slope + slopeCI);
+    }
+
+    public double getSlope() {
+        return slope;
+    }
+
+    public void setSlope(double slope) {
+        this.slope = slope;
+    }
+
+    public double getIntercept() {
+        return intercept;
+    }
+
+    public void setIntercept(double intercept) {
+        this.intercept = intercept;
+    }
+
+    public double getSlopeCI() {
+        return slopeCI;
+    }
+
+    public void setSlopeCI(double slopeCI) {
+        this.slopeCI = slopeCI;
+    }
+
+    public double getrSquare() {
+        return rSquare;
+    }
+
+    public void setrSquare(double rSquare) {
+        this.rSquare = rSquare;
+    }
+
+    public double getR() {
+        return r;
+    }
+
+    public void setR(double r) {
+        this.r = r;
+    }
 }

+ 1 - 1
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/model2/PVModel.java

@@ -118,7 +118,7 @@ public abstract class PVModel {
 
     public static double getTValue(double degreesOfFreedom, double p) {
         TDistribution t = new TDistribution(degreesOfFreedom);
-        double value = t.inverseCumulativeProbability(1 - p / 2);
+        double value = t.inverseCumulativeProbability(1d - p / 2d);
 //        double value = t.inverseCumulativeProbability(1 - 0.01/2);
         return format(value, 2);
     }

+ 15 - 4
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/model2/TruenessEP15Patient.java

@@ -2,6 +2,7 @@ package com.lc.ibps.components.verification.model2;
 
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.baomidou.mybatisplus.extension.api.R;
 import com.lc.ibps.components.verification.model.InspectionConfigVO;
 import com.lc.ibps.components.verification.report.ChartDTO;
 import com.lc.ibps.components.verification.report.TableDTO;
@@ -11,6 +12,7 @@ import org.apache.commons.math3.stat.StatUtils;
 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 
 import java.util.HashMap;
+import java.util.StringJoiner;
 
 public class TruenessEP15Patient extends PVModel {
     //多个样本均值 y
@@ -178,9 +180,15 @@ public class TruenessEP15Patient extends PVModel {
     @Override
     public TableDTO buildDataTableDTO() {
         TableDTO table = new TableDTO();
-        String[] header = {"次数", "Y (实验方法)", "X (比较方法)", "Y<sub>i</sub>-X<sub>i</sub>", "(Y<sub>i</sub>-X<sub>i</sub>-B)"};
+        String[] header = {"次数", "Y", "X", "b<sub>i</sub>", "b<sub>i</sub>-b"};
         table.buildHeader(header);
         table.buildData(getReportData(), getScale());
+        final String note = new StringJoiner("<br />")
+                .add("Y -- 实验方法结果")
+                .add("X -- 比较方法结果")
+                .add("b<sub>i</sub> -- 每个样本测量结果在两个方法间的绝对偏移")
+                .add("b -- 两个方法间的绝对偏移").toString();
+        table.setNote(note);
         return table;
     }
 
@@ -212,7 +220,7 @@ public class TruenessEP15Patient extends PVModel {
 
     @Override
     public String[] tableNames() {
-        return new String[]{"表1"};
+        return new String[]{"表1: 正确度验证数据表"};
     }
 
     @Override
@@ -229,10 +237,13 @@ public class TruenessEP15Patient extends PVModel {
         }
 //        chartDTO.setData(data);
         final HashMap<String, Object> config = new HashMap<>();
-        config.put("yAxisUp", sdClaim);
-        config.put("yAxisLow", -sdClaim);
+        config.put("yAxisUp", bias);
+        config.put("xAxisName","比较方法");
+        config.put("yAxisName","偏倚(实验-比较)");
+//        config.put("yAxisLow", -sdClaim);
         config.put("data", JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect));
         chartDTO.setOption(renderChartTemplate(config, 0, "/scatter/linesForRange.ftl"));
+        chartDTO.setNote(String.format("%s%d %s","图中实线为平均偏移值",format(bias),getUnits()));
         return chartDTO;
     }
 }

+ 14 - 5
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/report/ChartDTO.java

@@ -2,21 +2,30 @@ package com.lc.ibps.components.verification.report;
 
 public class ChartDTO {
 
-    private double[][] data;
+//    private double[][] data;
     private String option;
 
     private String name;
+    private String note;
 
     public ChartDTO(String name) {
         this.name = name;
     }
 
-    public double[][] getData() {
-        return data;
+//    public double[][] getData() {
+//        return data;
+//    }
+//
+//    public void setData(double[][] data) {
+//        this.data = data;
+//    }
+
+    public String getNote() {
+        return note;
     }
 
-    public void setData(double[][] data) {
-        this.data = data;
+    public void setNote(String note) {
+        this.note = note;
     }
 
     public String getOption() {

+ 4 - 0
ibps-provider-root/modules/provider-business/src/main/resources/pv/report/linearRangeAverageSlope.ftl

@@ -0,0 +1,4 @@
+<h3>结论判断:</h3>
+线性回归方程式 ${function},R<sub>2</sub>=${rSquare}, r=${r},
+斜率的95%可信区间为(${slopeCIFormat})
+R>0.975

+ 0 - 0
ibps-provider-root/modules/provider-business/src/main/resources/pv/report/linearRangeEP6A.ftl


+ 0 - 0
ibps-provider-root/modules/provider-business/src/main/resources/pv/report/linearRangeRecoveryMethod.ftl


+ 53 - 0
ibps-provider-root/modules/provider-business/src/test/java/com/lc/ibps/components/verification/model2/LinearRangeAverageSlopeTest.java

@@ -0,0 +1,53 @@
+package com.lc.ibps.components.verification.model2;
+
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.lc.ibps.components.verification.model.InspectionConfigVO;
+import com.lc.ibps.components.verification.report.ReportDataDTO;
+import com.lc.ibps.components.verification.report.ReportFactory;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.commons.math3.linear.MatrixUtils;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.linear.RealMatrixFormat;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.DecimalFormat;
+import java.time.LocalDate;
+
+import static org.junit.Assert.*;
+
+public class LinearRangeAverageSlopeTest {
+
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    @Test
+    public void calculate() {
+        InspectionConfigVO config = new InspectionConfigVO("平均斜率评价法", 1, 6,
+                null, 2, LocalDate.now(), true);
+
+        config.setDecimal(3);
+        config.setUnits("mmg/L");
+        config.setKey(ModelEnum.LinearRangeAverageSlope.getKey());
+        RandomUtils.nextDouble();
+        double[][] data = new double[config.getSpecimensNum()][config.getRepeatNum()];
+        for (int i = 0; i < config.getSpecimensNum(); i++) {
+            for (int j = 0; j < config.getRepeatNum(); j++) {
+                data[i][j] = RandomUtils.nextDouble(3.12+i*2, 3.75+i*2);
+            }
+        }
+        RealMatrix matrix = MatrixUtils.createRealMatrix(data);
+        RealMatrixFormat matrixFormat = new RealMatrixFormat("", "", "", "\n",
+                "", ",", new DecimalFormat("0.000"));
+        System.out.println(matrixFormat.format(matrix));
+        PVItemBuilder pvItemBuilder = PVItemBuilder.getInstance(config);
+        pvItemBuilder.buildPVModel(data);
+        ReportDataDTO dataDTO = ReportFactory.build(pvItemBuilder);
+        String s = JSONObject.toJSONString(dataDTO, SerializerFeature.DisableCircularReferenceDetect);
+        String s1 = pvItemBuilder.getPvModel().generateResult();
+        System.out.println(s);
+        System.out.println(s1);
+    }
+}