Spaces:
Running
Running
File size: 33,607 Bytes
599f646 e23b66d 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 b0476f6 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 bac04f0 8bab137 bac04f0 8bab137 bac04f0 8bab137 bac04f0 8bab137 bac04f0 8bab137 bac04f0 8bab137 599f646 8bab137 a66466f 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 8bab137 599f646 b0476f6 599f646 8bab137 599f646 bac04f0 599f646 |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 |
/**
*
* Copyright 2023-2025 InspectorRAGet Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
'use client';
import { countBy, isEmpty } from 'lodash';
import cx from 'classnames';
import { useState, useMemo, useEffect, useRef } from 'react';
import { Tile, Button, Slider } from '@carbon/react';
import { WarningAlt } from '@carbon/icons-react';
import { ScatterChart } from '@carbon/charts-react';
import { useTheme } from '@/src/theme';
import { Model, Metric, TaskEvaluation } from '@/src/types';
import {
castToNumber,
AgreementLevels,
extractMetricDisplayName,
} from '@/src/utilities/metrics';
import { calculateFisherRandomization } from '@/src/utilities/significance';
import { areObjectsIntersecting } from '@/src/utilities/objects';
import { hash } from '@/src/utilities/strings';
import Filters from '@/src/components/filters/Filters';
import TasksTable from '@/src/views/tasks-table/TasksTable';
import ModelSelector from '@/src/components/selectors/ModelSelector';
import MetricSelector from '@/src/components/selectors/MetricSelector';
import { getModelColorPalette } from '@/src/utilities/colors';
import '@carbon/charts-react/styles.css';
import classes from './ModelComparator.module.scss';
// ===================================================================================
// TYPES
// ===================================================================================
type StatisticalInformation = {
p: number;
distributionA: number[];
meanA: number;
distributionB: number[];
meanB: number;
taskIds?: string[];
};
interface Props {
evaluationsPerMetric: { [key: string]: TaskEvaluation[] };
models: Model[];
metrics: Metric[];
filters: { [key: string]: string[] };
onTaskSelection: Function;
}
// ===================================================================================
// COMPUTE FUNCTIONS
// ===================================================================================
/**
* Build an array containing evaluations only for selected models for each task.
*
* Eligbility criteria:
*
* 1. Must have evaluations for both selected models
*
* 2. Each evaluation must have an agreement value for selected metric
*
*
* @param evaluations evaluations for all task
* @param modelA selected model
* @param modelB selected model
* @param metric selected metric
* @returns
*/
function extractEvaluationsPerTask(
evaluations: TaskEvaluation[],
modelA: Model,
modelB: Model,
metric: string,
selectedFilters: { [key: string]: string[] },
selectedMetricRange?: number[],
) {
// Step 1: Initiaze necessary variable
const modelEvaluationsPerTask: { [key: string]: TaskEvaluation[] } = {};
// Step 2: Add to model evaluations for a task, if evaluation meets eligbility criteria
evaluations.forEach((evaluation) => {
if (
(evaluation.modelId === modelA.modelId ||
evaluation.modelId === modelB.modelId) &&
evaluation[`${metric}_agg`].level !== AgreementLevels.NO_AGREEMENT &&
(!isEmpty(selectedFilters)
? areObjectsIntersecting(selectedFilters, evaluation)
: true)
) {
const modelEvaluationsForTask =
modelEvaluationsPerTask[evaluation.taskId];
if (modelEvaluationsForTask) {
modelEvaluationsForTask.push(evaluation);
} else {
modelEvaluationsPerTask[evaluation.taskId] = [evaluation];
}
}
});
// Step 3: Retain only those task which has evaluations for both models
// and one or more models have aggregate value in the selected range
return Object.values(modelEvaluationsPerTask).filter(
(entry) =>
entry.length == 2 &&
(selectedMetricRange
? (entry[0][`${metric}_agg`].value >= selectedMetricRange[0] &&
entry[0][`${metric}_agg`].value <= selectedMetricRange[1]) ||
(entry[1][`${metric}_agg`].value >= selectedMetricRange[0] &&
entry[1][`${metric}_agg`].value <= selectedMetricRange[1])
: true),
);
}
/**
* Run statistical significance test based on Fisher randomization method.
* @param evaluationsPerMetric evaluations per metric
* @param metrics metrics
* @param modelA selected model
* @param modelB selected model
* @param selectedMetric If `undefined`, run for all metrics in `evaluationsPerMetric` object
* @returns
*/
function runStatisticalSignificanceTest(
evaluationsPerMetric: { [key: string]: TaskEvaluation[] },
metrics: Metric[],
modelA: Model,
modelB: Model,
selectedMetric: Metric | undefined,
selectedFilters: { [key: string]: string[] },
selectedMetricRange?: number[],
) {
// Step 1: Initialize necessary variables
const evaluationsPerMetricPerTask: { [key: string]: TaskEvaluation[][] } = {};
// Step 2: Retain evaluations for tasks where both models have agreement value
if (selectedMetric) {
const evaluationsPerTask = extractEvaluationsPerTask(
evaluationsPerMetric[selectedMetric.name],
modelA,
modelB,
selectedMetric.name,
selectedFilters,
selectedMetricRange,
);
if (evaluationsPerTask.length !== 0) {
evaluationsPerMetricPerTask[selectedMetric.name] = evaluationsPerTask;
}
} else {
Object.keys(evaluationsPerMetric).forEach((metric) => {
const evaluationsPerTask = extractEvaluationsPerTask(
evaluationsPerMetric[metric],
modelA,
modelB,
metric,
selectedFilters,
selectedMetricRange,
);
if (evaluationsPerTask.length !== 0) {
evaluationsPerMetricPerTask[metric] = evaluationsPerTask;
}
});
}
// Step 3: Compute model value distribution for every metric
const distributionA: { [key: string]: number[] } = {};
const distributionB: { [key: string]: number[] } = {};
const taskIds: { [key: string]: string[] } = {};
Object.keys(evaluationsPerMetricPerTask).forEach((metric) => {
const metricValues = metrics.find((entry) => entry.name === metric)?.values;
taskIds[metric] = evaluationsPerMetricPerTask[metric].map(
(entry) => entry[0].taskId,
);
distributionA[metric] = evaluationsPerMetricPerTask[metric].map((entry) =>
castToNumber(
entry[0].modelId === modelA.modelId
? entry[0][`${metric}_agg`].value
: entry[1][`${metric}_agg`].value,
metricValues,
),
);
distributionB[metric] = evaluationsPerMetricPerTask[metric].map((entry) =>
castToNumber(
entry[1].modelId === modelB.modelId
? entry[1][`${metric}_agg`].value
: entry[0][`${metric}_agg`].value,
metricValues,
),
);
});
// Step 3: Compute p value and means for every metric by comparing distributions
const information: { [key: string]: StatisticalInformation } = {};
Object.keys(evaluationsPerMetricPerTask).forEach((metric) => {
const [p, meanA, meanB] = calculateFisherRandomization(
distributionA[metric],
distributionB[metric],
);
information[metric] = {
p: p,
distributionA: distributionA[metric],
meanA: meanA,
distributionB: distributionB[metric],
meanB: meanB,
taskIds: taskIds[metric],
};
});
return information;
}
// ===================================================================================
// RENDER FUNCTIONS
// ===================================================================================
function prepareScatterPlotData(
modelA: string,
distributionA: number[],
modelB: string,
distributionB: number[],
taskIds?: string[],
) {
if (distributionA.length !== distributionB.length) {
return [];
}
// Step 2: Collate model wise predictions per task
const distributions: { values: number[]; taskId: string }[] = [];
distributionA.forEach((valueA, index) => {
distributions.push({
taskId: taskIds ? taskIds[index] : `${index}`,
values: [valueA, distributionB[index]],
});
});
// Step 3: Primary sort based on model A's value
distributions.sort((a, b) => a.values[0] - b.values[0]);
// Step 4: Scondary sort based on Model B's value
distributions.sort((a, b) => a.values[1] - b.values[1]);
// Step 5: Prepare chart data
const chartData: { [key: string]: string | number }[] = [];
distributions.forEach((entry, idx) => {
// Model A record
chartData.push({
group: modelA,
key: idx,
value: entry.values[0],
...(taskIds && { taskId: entry.taskId }),
});
// Model B record
chartData.push({
group: modelB,
key: idx,
value: entry.values[1],
...(taskIds && { taskId: entry.taskId }),
});
});
return chartData;
}
function renderResult(
statisticalInformationPerMetric: { [key: string]: StatisticalInformation },
metric: Metric,
modelA: Model,
modelB: Model,
numEvaluations: number,
modelColors: { [key: string]: string },
modelOrder: string[],
theme?: string,
) {
if (statisticalInformationPerMetric.hasOwnProperty(metric.name)) {
return (
<div
key={'statisticalInformation-metric-' + metric.name}
className={classes.performanceInformation}
>
<h5>
<strong>{extractMetricDisplayName(metric)}</strong>
</h5>
<Tile className={classes.tile}>
<div className={classes.tileContent}>
<span className={classes.tileContentInformation}>p-value</span>
<span
className={classes.tileContentValue}
suppressHydrationWarning={true}
>
{statisticalInformationPerMetric[metric.name]['p'].toFixed(4)}
</span>
<span
className={classes.tileContentDecision}
suppressHydrationWarning={true}
>
{statisticalInformationPerMetric[metric.name]['p'] <= 0.05
? 'Significant'
: 'Not significant'}
</span>
</div>
</Tile>
<ScatterChart
data={prepareScatterPlotData(
modelA.name,
statisticalInformationPerMetric[metric.name].distributionA,
modelB.name,
statisticalInformationPerMetric[metric.name].distributionB,
statisticalInformationPerMetric[metric.name].taskIds,
)}
options={{
axes: {
left: {
mapsTo: 'value',
...(metric.type === 'numerical' &&
typeof metric.minValue === 'number' &&
typeof metric.maxValue === 'number' && {
domain: [metric.minValue, metric.maxValue],
}),
...(metric.type === 'categorical' &&
typeof metric.minValue !== 'number' &&
typeof metric.maxValue !== 'number' && {
domain: [
castToNumber(metric.minValue?.value || 0, metric.values),
castToNumber(metric.maxValue?.value || 4, metric.values),
],
}),
title: extractMetricDisplayName(metric),
},
bottom: {
mapsTo: 'key',
ticks: {
values: [],
},
title: `Tasks (${
statisticalInformationPerMetric[metric.name].distributionA
.length
}/${numEvaluations})`,
},
},
width: '500px',
height: '500px',
toolbar: {
enabled: false,
},
color: {
scale: modelColors,
},
legend: {
order: modelOrder,
},
theme: theme,
}}
></ScatterChart>
</div>
);
} else {
return null;
}
}
// ===================================================================================
// MAIN FUNCTION
// ===================================================================================
export default function ModelComparator({
evaluationsPerMetric,
models,
metrics,
filters,
onTaskSelection,
}: Props) {
// Step 1: Initialize state and necessary variables
const [WindowWidth, setWindowWidth] = useState<number>(
global?.window && window.innerWidth,
);
const [modelA, setModelA] = useState<Model>(models[0]);
const [modelB, setModelB] = useState<Model>(models[1]);
const [selectedMetric, setSelectedMetric] = useState<Metric | undefined>(
undefined,
);
const [selectedFilters, setSelectedFilters] = useState<{
[key: string]: string[];
}>({});
const [statisticalInformationPerMetric, setStatisticalInformationPerMetric] =
useState<{ [key: string]: StatisticalInformation } | undefined>(undefined);
const [modelColors, modelOrder] = getModelColorPalette(models);
const [selectedMetricRange, setSelectedMetricRange] = useState<number[]>();
const chartRef = useRef(null);
// Step 2: Run effects
// Step 2.a: Window resizing
useEffect(() => {
const handleWindowResize = () => {
setWindowWidth(window.innerWidth);
};
// Step: Add event listener
window.addEventListener('resize', handleWindowResize);
// Step: Cleanup to remove event listener
return () => {
window.removeEventListener('resize', handleWindowResize);
};
}, []);
// Step 2.a: Fetch theme
const { theme } = useTheme();
//Step 2.c: Bucket human and algoritmic metrics
const [humanMetrics, algorithmMetrics] = useMemo(() => {
const hMetrics: Metric[] = [];
const aMetrics: Metric[] = [];
Object.values(metrics).forEach((metric) => {
if (metric.author === 'human') {
hMetrics.push(metric);
} else if (metric.author === 'algorithm') {
aMetrics.push(metric);
}
});
return [hMetrics, aMetrics];
}, [metrics]);
// Step 2.d: Reset selected metric range, only applicable for numerical metrics
useEffect(() => {
if (
selectedMetric &&
selectedMetric.type === 'numerical' &&
selectedMetric.range
) {
setSelectedMetricRange([
selectedMetric.range[0],
selectedMetric.range[1],
]);
} else setSelectedMetricRange(undefined);
}, [selectedMetric]);
// Step 2.e: Identify visible evaluations
const filteredEvaluations = useMemo(() => {
if (selectedMetric) {
// Step 1: Identify evaluations for selected models
const evaluationsForSelectedModels = evaluationsPerMetric[
selectedMetric.name
].filter(
(evaluation) =>
(evaluation.modelId === modelA.modelId ||
evaluation.modelId === modelB.modelId) &&
(!isEmpty(selectedFilters)
? areObjectsIntersecting(selectedFilters, evaluation)
: true),
);
// Step 2: Collate evaluation per task id
const evaluationsPerTask: { [key: string]: { [key: string]: number } } =
{};
evaluationsForSelectedModels.forEach((evaluation) => {
const entry = evaluationsPerTask[evaluation.taskId];
if (entry) {
entry[evaluation.modelId] =
evaluation[`${selectedMetric.name}_agg`].value;
} else {
evaluationsPerTask[evaluation.taskId] = {
[evaluation.modelId]:
evaluation[`${selectedMetric.name}_agg`].value,
};
}
});
// Step 3: Only select evaluation tasks where models aggregate values differe
// and one or more models have aggregate value in the selected range
const visibleEvaluationTaskIds = Object.keys(evaluationsPerTask).filter(
(taskId) =>
Object.keys(countBy(Object.values(evaluationsPerTask[taskId])))
.length > 1 &&
(selectedMetricRange
? (Object.values(evaluationsPerTask[taskId])[0] >=
selectedMetricRange[0] &&
Object.values(evaluationsPerTask[taskId])[0] <=
selectedMetricRange[1]) ||
(Object.values(evaluationsPerTask[taskId])[1] >=
selectedMetricRange[0] &&
Object.values(evaluationsPerTask[taskId])[1] <=
selectedMetricRange[1])
: true),
);
// Step 4: Return evaluations for selected evaluation tasks where models aggregate values differe
return evaluationsForSelectedModels.filter((evaluation) =>
visibleEvaluationTaskIds.includes(evaluation.taskId),
);
}
return [];
}, [
evaluationsPerMetric,
selectedMetric,
modelA,
modelB,
selectedMetricRange,
]);
// Step 2.f: Reset statistical information, if either of model changes or filters are changed
useEffect(() => {
setStatisticalInformationPerMetric(undefined);
}, [modelA, modelB, selectedFilters]);
// Step 2.g: Recalculate statistical information, if metric changes
useEffect(() => {
if (
!selectedMetric &&
statisticalInformationPerMetric &&
Object.keys(statisticalInformationPerMetric).length == 1
) {
setStatisticalInformationPerMetric(
runStatisticalSignificanceTest(
evaluationsPerMetric,
metrics,
modelA,
modelB,
selectedMetric,
selectedFilters,
selectedMetricRange,
),
);
} else if (
selectedMetric &&
selectedMetricRange &&
statisticalInformationPerMetric &&
statisticalInformationPerMetric.hasOwnProperty(selectedMetric.name)
) {
setStatisticalInformationPerMetric(
runStatisticalSignificanceTest(
evaluationsPerMetric,
metrics,
modelA,
modelB,
selectedMetric,
selectedFilters,
selectedMetricRange,
),
);
}
}, [selectedMetric, selectedMetricRange]);
// Step 2.h: Compute computation complexity
const complexity = useMemo(() => {
let size = 0;
if (selectedMetric) {
size = evaluationsPerMetric[selectedMetric.name].length / models.length;
} else {
size = Object.values(evaluationsPerMetric)
.map((evaluations) => evaluations.length / models.length)
.reduce((a, b) => a + b, 0);
}
if (size > 1000) {
return 'high';
}
return 'low';
}, [evaluationsPerMetric, selectedMetric]);
// Step 2.i: Add chart event
useEffect(() => {
// Step 2.i.*: Local copy of reference
let ref = null;
// Step 2.i.**: Update reference and add event
if (chartRef && chartRef.current) {
ref = chartRef.current;
//@ts-ignore
ref.chart.services.events.addEventListener(
'scatter-click',
({ detail }) => {
onTaskSelection(detail.datum.taskId);
},
);
}
// Step 2.i.***: Cleanup function
return () => {
if (ref) {
//@ts-ignore
ref.chart.services.events.removeEventListener(
'scatter-click',
({ detail }) => {
onTaskSelection(detail.datum.taskId);
},
);
}
};
}, [chartRef, selectedMetric, statisticalInformationPerMetric]);
// Step 3: Render
return (
<div className={classes.page}>
<div className={classes.selectors}>
<div className={classes.modelSelector}>
<ModelSelector
id={'modelA-selector-excluding-model-' + modelB.modelId}
key={'modelA-selector-excluding-model-' + modelB.modelId}
models={models}
defaultValue={modelA}
onSelect={(modelId: string) => {
const selectedModel = models.find(
(model) => model.modelId === modelId,
);
if (selectedModel) {
setModelA(selectedModel);
}
}}
disabledModels={[modelB]}
/>
</div>
<div className={classes.modelSelector}>
<ModelSelector
id={'modelB-selector-excluding-model-' + modelA.modelId}
key={'modelB-selector-excluding-model-' + modelA.modelId}
models={models}
defaultValue={modelB}
onSelect={(modelId: string) => {
const selectedModel = models.find(
(model) => model.modelId === modelId,
);
if (selectedModel) {
setModelB(selectedModel);
}
}}
disabledModels={[modelA]}
/>
</div>
<div className={classes.metricSelector}>
<MetricSelector
metrics={metrics}
onSelect={(metric: Metric | undefined) => {
setSelectedMetric(metric);
}}
warn={!selectedMetric}
warnText={'You must select a single metric to view tasks. '}
/>
</div>
{selectedMetric &&
selectedMetric.type === 'numerical' &&
selectedMetric.range ? (
<div>
<Slider
ariaLabelInput="Lower bound"
unstable_ariaLabelInputUpper="Upper bound"
labelText={`Choose range`}
value={
selectedMetricRange
? selectedMetricRange[0]
: selectedMetric.range[0]
}
unstable_valueUpper={
selectedMetricRange
? selectedMetricRange[1]
: selectedMetric.range[1]
}
min={selectedMetric.range[0]}
max={selectedMetric.range[1]}
step={
selectedMetric.range.length === 3 ? selectedMetric.range[2] : 1
}
onChange={({
value,
valueUpper,
}: {
value: number;
valueUpper?: number;
}) => {
setSelectedMetricRange((prev) => [
value,
valueUpper
? valueUpper
: prev
? prev[1]
: selectedMetric.range
? selectedMetric.range[2]
: 100,
]);
}}
/>
</div>
) : null}
<div className={classes.calculateBtn}>
<Button
onClick={() => {
// Run statistical significance calculations
setStatisticalInformationPerMetric(
runStatisticalSignificanceTest(
evaluationsPerMetric,
metrics,
modelA,
modelB,
selectedMetric,
selectedFilters,
selectedMetricRange,
),
);
}}
>
Calculate
</Button>
</div>
</div>
{!isEmpty(filters) ? (
<Filters
keyPrefix="ModelComparator"
filters={filters}
selectedFilters={selectedFilters}
setSelectedFilters={setSelectedFilters}
/>
) : null}
{statisticalInformationPerMetric ? (
<div className={classes.row}>
<div className={classes.hypothesisContainer}>
<span className={classes.hypothesisStatement}>
H<sub>0</sub>: {modelA.name} and {modelB.name} scores are derived
from the same distribution.
</span>
<span className={classes.hypothesisValidityCondition}>
<span>{'Reject the null hypothesis if p < 0.05'}</span>
</span>
</div>
{!selectedMetric && humanMetrics.length ? (
<div className={classes.row}>
<h4>Human Evaluations</h4>
<div
className={cx(
humanMetrics.length > 3
? classes.graphsGrid
: classes.graphsFlex,
)}
>
{humanMetrics.map((metric) =>
renderResult(
statisticalInformationPerMetric,
metric,
modelA,
modelB,
evaluationsPerMetric[metric.name].length / models.length,
modelColors,
modelOrder,
theme,
),
)}
</div>
</div>
) : null}
{!selectedMetric && algorithmMetrics.length ? (
<div className={classes.row}>
<h4>Algorithmic Evaluations</h4>
<div
className={cx(
algorithmMetrics.length > 3
? classes.graphsGrid
: classes.graphsFlex,
)}
>
{algorithmMetrics.map((metric) =>
renderResult(
statisticalInformationPerMetric,
metric,
modelA,
modelB,
evaluationsPerMetric[metric.name].length / models.length,
modelColors,
modelOrder,
theme,
),
)}
</div>
</div>
) : null}
{selectedMetric &&
statisticalInformationPerMetric.hasOwnProperty(
selectedMetric.name,
) ? (
<div className={classes.row}>
<div
key={`statisticalInformation-metric-${selectedMetric.name}--${hash(JSON.stringify(statisticalInformationPerMetric[selectedMetric.name]))}`}
className={classes.performanceInformation}
>
<h5>
<strong>{extractMetricDisplayName(selectedMetric)}</strong>
</h5>
<Tile className={classes.tile}>
<div className={classes.tileContent}>
<span className={classes.tileContentInformation}>
p-value
</span>
<span
className={classes.tileContentValue}
suppressHydrationWarning={true}
>
{statisticalInformationPerMetric[selectedMetric.name][
'p'
].toFixed(4)}
</span>
<span
className={classes.tileContentDecision}
suppressHydrationWarning={true}
>
{statisticalInformationPerMetric[selectedMetric.name][
'p'
] <= 0.05
? 'Significant'
: 'Not significant'}
</span>
</div>
</Tile>
<ScatterChart
ref={chartRef}
data={prepareScatterPlotData(
modelA.name,
statisticalInformationPerMetric[selectedMetric.name]
.distributionA,
modelB.name,
statisticalInformationPerMetric[selectedMetric.name]
.distributionB,
statisticalInformationPerMetric[selectedMetric.name]
.taskIds,
)}
options={{
axes: {
left: {
mapsTo: 'value',
...(selectedMetric.type === 'numerical' &&
typeof selectedMetric.minValue === 'number' &&
typeof selectedMetric.maxValue === 'number' && {
domain: [
selectedMetric.minValue,
selectedMetric.maxValue,
],
}),
...(selectedMetric.type === 'categorical' &&
typeof selectedMetric.minValue !== 'number' &&
typeof selectedMetric.maxValue !== 'number' && {
domain: [
castToNumber(
selectedMetric.minValue?.value || 0,
selectedMetric.values,
),
castToNumber(
selectedMetric.maxValue?.value || 4,
selectedMetric.values,
),
],
}),
title: extractMetricDisplayName(selectedMetric),
},
bottom: {
mapsTo: 'key',
ticks: {
values: [],
},
title: `Tasks (${
statisticalInformationPerMetric[selectedMetric.name]
.distributionA.length
}/${
evaluationsPerMetric[selectedMetric.name].length /
models.length
})`,
},
},
width: `${Math.round(WindowWidth * 0.8)}px`,
height: '500px',
toolbar: {
enabled: false,
},
color: {
scale: modelColors,
},
legend: {
order: modelOrder,
},
theme: theme,
}}
></ScatterChart>
</div>
</div>
) : (
<>
<div className={classes.tasksContainerNotification}>
<span
className={classes.tasksContainerNotificationText}
>{`Press calculate to measure statistical significance ${selectedMetric ? 'for' : 'across'} "${selectedMetric ? extractMetricDisplayName(selectedMetric) : 'all'}" metric${selectedMetric ? '' : 's'}`}</span>
<span
className={classes.tasksContainerNotificationText}
>{`for "${modelA.name}" and "${modelB.name}" models.`}</span>
{complexity === 'high' ? (
<div className={classes.tasksContainerWarning}>
<WarningAlt
height={'24px'}
width={'24px'}
className={classes.tasksContainerWarningIcon}
/>
<span className={classes.tasksContainerWarningText}>
It might take few minutes to build this view.
</span>
</div>
) : null}
</div>
</>
)}
</div>
) : (
<>
<div className={classes.tasksContainerNotification}>
<span
className={classes.tasksContainerNotificationText}
>{`Press calculate to measure statistical significance ${selectedMetric ? 'for' : 'across'} "${selectedMetric ? extractMetricDisplayName(selectedMetric) : 'all'}" metric${selectedMetric ? '' : 's'}`}</span>
<span
className={classes.tasksContainerNotificationText}
>{`for "${modelA.name}" and "${modelB.name}" models.`}</span>
{complexity === 'high' ? (
<div className={classes.tasksContainerWarning}>
<WarningAlt
height={'24px'}
width={'24px'}
className={classes.tasksContainerWarningIcon}
/>
<span className={classes.tasksContainerWarningText}>
It might take few minutes to build this view.
</span>
</div>
) : null}
</div>
</>
)}
{selectedMetric &&
statisticalInformationPerMetric &&
statisticalInformationPerMetric.hasOwnProperty(selectedMetric.name) && (
<div className={classes.row}>
<h4>
Tasks{selectedMetric && filteredEvaluations && <sup>*</sup>}
</h4>
{filteredEvaluations ? (
<>
<TasksTable
metrics={[selectedMetric]}
evaluations={filteredEvaluations}
models={[modelA, modelB]}
filters={filters}
onClick={onTaskSelection}
/>
<span className={classes.tasksTableWarning}>
<sup>*</sup> Only tasks with different model aggregate scores
are shown in the above table.
</span>
</>
) : null}
</div>
)}
</div>
);
}
|