Skip to content

Commit

Permalink
fix(packages/shared-components): ensure that sample solutions are alw…
Browse files Browse the repository at this point in the history
…ays visible on evaluation histogram (#4420)
  • Loading branch information
sjschlapbach authored Dec 30, 2024
1 parent 4bdc708 commit d73c976
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 11 additions & 5 deletions packages/shared-components/src/charts/ElementHistogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ function ElementHistogram({
const supportedElementTypes = [ElementType.Numerical]
const [numBins, setNumBins] = useState('20')

const showSolutionRanges = showSolution && solutionRanges
const showExactSolutions =
showSolution && exactSolutions && exactSolutions.length > 0
const processedData = useEvaluationHistogramData({
type,
responses,
solutionRanges: showSolutionRanges ? solutionRanges : undefined,
exactSolutions: showExactSolutions
? exactSolutions.map((solution) =>
typeof solution === 'number' ? solution : parseFloat(solution)
)
: undefined,
minValue,
maxValue,
binCount: parseInt(numBins),
Expand Down Expand Up @@ -214,8 +223,7 @@ function ElementHistogram({
/>
)}

{showSolution &&
solutionRanges &&
{showSolutionRanges &&
solutionRanges.map(
(
solutionRange: { min?: number | null; max?: number | null },
Expand Down Expand Up @@ -243,9 +251,7 @@ function ElementHistogram({
)
)}

{showSolution &&
exactSolutions &&
exactSolutions.length > 0 &&
{showExactSolutions &&
exactSolutions.map((solution, idx) => (
<ReferenceLine
key={`exact-solution-${idx}`}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-components/src/evaluation/NREvaluation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function NREvaluation({ options, evaluation, reference }: NREvaluationProps) {
exactSolutions={options.exactSolutions ?? undefined}
minValue={options.restrictions?.min}
maxValue={options.restrictions?.max}
showSolution={true}
textSize="md"
className={{ root: 'h-40' }}
reference={reference ? parseFloat(reference) : undefined}
showSolution
hideBins
basic
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ElementType } from '@klicker-uzh/graphql/dist/ops'
import {
ElementType,
type NumericalSolutionRange,
} from '@klicker-uzh/graphql/dist/ops'
import { maxBy, minBy, round, sumBy } from 'lodash'
import { useMemo } from 'react'

interface UseEvaluationHistogramDataProps {
type: ElementType
responses: { value: number; count: number }[]
solutionRanges?: NumericalSolutionRange[] | null
exactSolutions?: number[] | null
minValue?: number | null
maxValue?: number | null
binCount: number
Expand All @@ -13,6 +18,8 @@ interface UseEvaluationHistogramDataProps {
function useEvaluationHistogramData({
type,
responses,
solutionRanges,
exactSolutions,
minValue,
maxValue,
binCount,
Expand All @@ -22,15 +29,27 @@ function useEvaluationHistogramData({
return null
}

// use the minima defined for the solution ranges and exact solutions to compute the bounds for the histogram
const solutionRangesMin =
minBy(solutionRanges, 'min')?.min ?? Number.MAX_VALUE
const solutionRangesMax =
maxBy(solutionRanges, 'max')?.max ?? Number.MIN_VALUE
const exactSolutionsMin = minBy(exactSolutions) ?? Number.MAX_VALUE
const exactSolutionsMax = maxBy(exactSolutions) ?? Number.MIN_VALUE
const solutionsMin = Math.min(solutionRangesMin, exactSolutionsMin)
const solutionsMax = Math.max(solutionRangesMax, exactSolutionsMax)

// compute the lower and upper bounds of the histogram
const min: number =
minValue !== null && typeof minValue === 'number'
? minValue
: (minBy(responses, 'value')?.value ?? 0) - 10
: Math.min(minBy(responses, 'value')?.value ?? 0, solutionsMin) - 10
const max: number =
maxValue !== null && typeof maxValue === 'number'
? maxValue
: (maxBy(responses, 'value')?.value ?? 0) + 10
: Math.max(maxBy(responses, 'value')?.value ?? 0, solutionsMax) + 10

// organize the data into histogram bins based on the computed min and max values
let dataArray = Array.from({ length: binCount }, (_, i) => ({
value: min + (max - min) * (i / binCount) + (max - min) / (2 * binCount),
}))
Expand Down

0 comments on commit d73c976

Please sign in to comment.