-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sorting by label, metric (wip), year
- Loading branch information
jfrer
committed
Jul 2, 2024
1 parent
1fbbbac
commit b6dd235
Showing
6 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script setup lang="ts"> | ||
import type { DropdownOption, EvaluationResultsDocumentWide, GroundTruth } from "@/types" | ||
import { computed, ref, watch } from "vue" | ||
import Dropdown from "primevue/dropdown" | ||
import { GTTimelineSortingOptions, sortByOption } from "@/helpers/sorting" | ||
import { useI18n } from "vue-i18n" | ||
import { DropdownPassThroughStyles } from "@/helpers/pt" | ||
const { t } = useI18n() | ||
const props = defineProps<{ | ||
modelValue: GroundTruth[], | ||
selectedMetric: keyof EvaluationResultsDocumentWide | ||
}>() | ||
const emit = defineEmits<{ | ||
(event: 'update:modelValue', payload: GroundTruth[]): void | ||
}>() | ||
const sortOptions = computed<DropdownOption[]>(() => | ||
Object.keys(GTTimelineSortingOptions).map(key => | ||
({ value: GTTimelineSortingOptions[key as keyof typeof GTTimelineSortingOptions], | ||
label: t(GTTimelineSortingOptions[key as keyof typeof GTTimelineSortingOptions]) | ||
}) | ||
) | ||
) | ||
const selectedSortOption = ref<DropdownOption>(sortOptions.value[0]) | ||
watch(() => props.modelValue, () => { | ||
updateSortedList(selectedSortOption.value) | ||
}) | ||
watch(() => props.selectedMetric, () => { | ||
updateSortedList(selectedSortOption.value) | ||
}) | ||
function updateSortedList(event: any) { | ||
const sortedGtList = sortByOption(props.modelValue, event.value, props.selectedMetric) | ||
emit('update:modelValue', sortedGtList) | ||
} | ||
</script> | ||
<template> | ||
<div> | ||
<p class="font-semibold mb-2">Sort by:</p> | ||
<Dropdown | ||
v-model="selectedSortOption" | ||
@update:model-value="updateSortedList($event)" | ||
:options="sortOptions" | ||
:pt="DropdownPassThroughStyles" | ||
option-label="label" | ||
unstyled | ||
/> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import workflowsStore from "@/store/workflows-store" | ||
import type { EvaluationResultsDocumentWide, EvaluationRun, GroundTruth, TimeSpan } from "@/types" | ||
|
||
const GTTimelineSortingOptions = { | ||
LABEL_ASC: 'label_asc', | ||
LABEL_DESC: 'label_desc', | ||
METRIC_DESC: 'metric_desc', | ||
METRIC_ASC: 'metric_asc', | ||
YEAR_ASC: 'year_asc', | ||
YEAR_DESC: 'year_desc' | ||
} | ||
|
||
function sortByOption(gtList: GroundTruth[], sortingOption: string, metric: keyof EvaluationResultsDocumentWide): GroundTruth[] { | ||
if (sortingOption === GTTimelineSortingOptions.METRIC_DESC) return sortByMetric(gtList, true, metric) | ||
if (sortingOption === GTTimelineSortingOptions.METRIC_ASC) return sortByMetric(gtList, false, metric) | ||
if (sortingOption === GTTimelineSortingOptions.LABEL_DESC) return sortByLabel(gtList, true) | ||
if (sortingOption === GTTimelineSortingOptions.LABEL_ASC) return sortByLabel(gtList, false) | ||
if (sortingOption === GTTimelineSortingOptions.YEAR_DESC) return sortByYear(gtList, true) | ||
if (sortingOption === GTTimelineSortingOptions.YEAR_ASC) return sortByYear(gtList, false) | ||
return gtList | ||
} | ||
|
||
function sortByMetric(gtList: GroundTruth[], desc: boolean, metric: keyof EvaluationResultsDocumentWide): GroundTruth[] { | ||
return gtList.sort((left, right) => { | ||
const compareMetric = (left: GroundTruth, right: GroundTruth) => { | ||
const leftRuns = workflowsStore.getRuns(left.id) //assume getLatestRuns(id) TODO -> change to latestRuns | ||
const rightRuns = workflowsStore.getRuns(right.id) //same here | ||
|
||
const getAverageValue = (runs: EvaluationRun[]) => { | ||
if (runs.length === 0) return 0 | ||
return runs.reduce((acc, curr) => { | ||
const value = <number | null>curr.evaluation_results.document_wide[metric] | ||
return acc += value ?? 0 | ||
}, 0) / runs.length | ||
} | ||
|
||
return getAverageValue(leftRuns) - getAverageValue(rightRuns) | ||
} | ||
return desc ? compareMetric(right, left) : compareMetric(left, right) | ||
}) | ||
} | ||
|
||
function sortByLabel(gtList: GroundTruth[], desc: boolean): GroundTruth[] { | ||
return gtList.sort((left, right) => { | ||
const leftLabel = left.label.toLocaleLowerCase() | ||
const rightLabel = right.label.toLocaleLowerCase() | ||
return desc ? rightLabel.localeCompare(leftLabel) : leftLabel.localeCompare(rightLabel) | ||
}) | ||
} | ||
|
||
function sortByYear(gtList: GroundTruth[], desc: boolean): GroundTruth[] { | ||
return gtList.sort((left, right) => { | ||
const compareTimeSpan = (leftTime: TimeSpan, rightTime: TimeSpan) => { | ||
return (leftTime.notBefore > rightTime.notBefore) ? 1 : ((rightTime.notBefore > leftTime.notBefore) ? -1 : 0) | ||
} | ||
return desc ? compareTimeSpan(right.metadata.time, left.metadata.time) : compareTimeSpan(left.metadata.time, right.metadata.time) | ||
}) | ||
} | ||
|
||
|
||
|
||
export { | ||
GTTimelineSortingOptions, sortByOption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters