From fb7043df6807b81faeb59f227d44a770dc264389 Mon Sep 17 00:00:00 2001 From: Stig Ofstad Date: Mon, 8 Apr 2024 09:55:19 +0200 Subject: [PATCH] fix: P-value calculation off by one. Added interpolation. --- .github/workflows/master.yaml | 2 +- docker-compose.yaml | 3 +-- web/package.json | 2 +- web/src/Utils.ts | 33 ++++++++++++++++++++++++--------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml index 48b2d6e..6729aa3 100644 --- a/.github/workflows/master.yaml +++ b/.github/workflows/master.yaml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@master - name: "Create report" - run: docker-compose build api && docker-compose run -u 0 api python tests/optimizer_test.py + run: docker-compose build api && docker-compose run -u 0 api python src/tests/optimizer_test.py - name: Archive results uses: actions/upload-artifact@v2 diff --git a/docker-compose.yaml b/docker-compose.yaml index 66c39be..2ce7ca4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -25,8 +25,7 @@ services: web: build: context: ./web - target: prod -# target: development + target: development restart: unless-stopped stdin_open: true volumes: diff --git a/web/package.json b/web/package.json index 38a3c81..f4f5133 100644 --- a/web/package.json +++ b/web/package.json @@ -10,7 +10,7 @@ "react": "^18.2.0", "react-bootstrap": "^2.9.1", "react-dom": "^18.2.0", - "react-oauth2-code-pkce": "^1.15.2", + "react-oauth2-code-pkce": "^1.17.2", "react-router-dom": "^6.20.1", "react-scripts": "^5.0.1", "react-toastify": "^9.1.3", diff --git a/web/src/Utils.ts b/web/src/Utils.ts index d0d4cdd..ea8f4c9 100644 --- a/web/src/Utils.ts +++ b/web/src/Utils.ts @@ -17,27 +17,42 @@ export function findGraphData(sizeFractions: number[], bridges: Bridge): GraphDa sizeFractions.forEach((fraction, sizeIndex) => { let temp: GraphData = { size: fraction } Object.entries(bridges).forEach(([name, cumulative]) => { - temp[name] = cumulative[sizeIndex + 1] + temp[name] = cumulative[sizeIndex] }) newGraphData.push(temp) }) return newGraphData } +function linearInterpolation(yMin: number, yMax: number, xMin: number, xMax: number, targetY: number): number { + const increase = (yMax - yMin) / (xMax - xMin) + return Number((increase * (targetY - xMin) + yMin).toFixed(1)) +} + export function findDValue(graphData: GraphData[], goalYValue: number, bridgeName: string): number { - //A D value is defined as a bridge graph's x-value at a specific y value - //example: D90 (goalYValue = 90) is the bridge graph's x-value at at y = 90 + // A D value is defined as a bridge graph's x-value at a specific y value + // Example: D90 (goalYValue = 90) is the bridge graph's x-value at y = 90 let bridgeYValues: number[] = [] graphData.map(graph => bridgeYValues.push(graph[bridgeName])) // find the bridge's y-value that is closest to the goal y-value - var closestToGoalYvalue = bridgeYValues.reduce(function (prev: number, curr: number) { - return Math.abs(curr - goalYValue) < Math.abs(prev - goalYValue) ? curr : prev + let indexOfClosestHigherYValue = 0 + bridgeYValues.some((accumulativePercentage, index) => { + if (accumulativePercentage > goalYValue) { + indexOfClosestHigherYValue = index + return true + } + return false }) + if (!indexOfClosestHigherYValue) throw new Error('Failed to find D-value of bridge') - let indexOfClosestYValue = bridgeYValues.indexOf(closestToGoalYvalue) - let DValue = graphData[indexOfClosestYValue].size //size contains the bridge's x-value - - return DValue + // interpolate the values to get an approx value for the exact D requested + return linearInterpolation( + graphData[indexOfClosestHigherYValue - 1].size, + graphData[indexOfClosestHigherYValue].size, + bridgeYValues[indexOfClosestHigherYValue - 1], + bridgeYValues[indexOfClosestHigherYValue], + goalYValue + ) }