From df13d1b0802c47d8f2440508d042f16408c1626b Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Sat, 21 Oct 2023 17:13:46 +0100 Subject: [PATCH 1/3] Use ascending param to decide how to order infinite values --- optuna_dashboard/ts/components/DataGrid.tsx | 5 +++-- optuna_dashboard/ts/components/TrialTable.tsx | 16 ++++++++-------- standalone_app/src/components/DataGrid.tsx | 5 +++-- standalone_app/src/components/TrialTable.tsx | 16 ++++++++-------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/optuna_dashboard/ts/components/DataGrid.tsx b/optuna_dashboard/ts/components/DataGrid.tsx index 2d6a5670..d19ec9d9 100644 --- a/optuna_dashboard/ts/components/DataGrid.tsx +++ b/optuna_dashboard/ts/components/DataGrid.tsx @@ -28,7 +28,7 @@ interface DataGridColumn { field: keyof T label: string sortable?: boolean - less?: (a: T, b: T) => number + less?: (a: T, b: T, ascending: boolean) => number filterable?: boolean toCellValue?: (rowIndex: number) => string | React.ReactNode padding?: "normal" | "checkbox" | "none" @@ -358,7 +358,8 @@ function stableSort( const stabilizedThis = array.map((el, index) => [el, index] as [T, number]) stabilizedThis.sort((a, b) => { if (less) { - const result = order == "asc" ? -less(a[0], b[0]) : less(a[0], b[0]) + const ascending = order == "asc" + const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending) if (result !== 0) return result } else { const result = comparator(a[0], b[0]) diff --git a/optuna_dashboard/ts/components/TrialTable.tsx b/optuna_dashboard/ts/components/TrialTable.tsx index 8fcce63c..6090ee85 100644 --- a/optuna_dashboard/ts/components/TrialTable.tsx +++ b/optuna_dashboard/ts/components/TrialTable.tsx @@ -28,7 +28,7 @@ export const TrialTable: FC<{ field: "values", label: "Value", sortable: true, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, ascending): number => { const firstVal = firstEl.values?.[0] const secondVal = secondEl.values?.[0] @@ -36,9 +36,9 @@ export const TrialTable: FC<{ return 0 } if (firstVal === undefined) { - return -1 + return ascending ? -1 : 1 } else if (secondVal === undefined) { - return 1 + return ascending ? 1 : -1 } if (firstVal === "-inf" || secondVal === "inf") { return 1 @@ -63,7 +63,7 @@ export const TrialTable: FC<{ ? objectiveNames[objectiveId] : `Objective ${objectiveId}`, sortable: true, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, ascending): number => { const firstVal = firstEl.values?.[objectiveId] const secondVal = secondEl.values?.[objectiveId] @@ -71,9 +71,9 @@ export const TrialTable: FC<{ return 0 } if (firstVal === undefined) { - return -1 + return ascending ? -1 : 1 } else if (secondVal === undefined) { - return 1 + return ascending ? 1 : -1 } if (firstVal === "-inf" || secondVal === "inf") { return 1 @@ -106,7 +106,7 @@ export const TrialTable: FC<{ ?.param_external_value || null, sortable: sortable, filterable: filterable, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, _): number => { const firstVal = firstEl.params.find( (p) => p.name === s.name )?.param_internal_value @@ -146,7 +146,7 @@ export const TrialTable: FC<{ ?.value || null, sortable: attr_spec.sortable, filterable: !attr_spec.sortable, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, _): number => { const firstVal = firstEl.user_attrs.find( (attr) => attr.key === attr_spec.key )?.value diff --git a/standalone_app/src/components/DataGrid.tsx b/standalone_app/src/components/DataGrid.tsx index 2d6a5670..d19ec9d9 100644 --- a/standalone_app/src/components/DataGrid.tsx +++ b/standalone_app/src/components/DataGrid.tsx @@ -28,7 +28,7 @@ interface DataGridColumn { field: keyof T label: string sortable?: boolean - less?: (a: T, b: T) => number + less?: (a: T, b: T, ascending: boolean) => number filterable?: boolean toCellValue?: (rowIndex: number) => string | React.ReactNode padding?: "normal" | "checkbox" | "none" @@ -358,7 +358,8 @@ function stableSort( const stabilizedThis = array.map((el, index) => [el, index] as [T, number]) stabilizedThis.sort((a, b) => { if (less) { - const result = order == "asc" ? -less(a[0], b[0]) : less(a[0], b[0]) + const ascending = order == "asc" + const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending) if (result !== 0) return result } else { const result = comparator(a[0], b[0]) diff --git a/standalone_app/src/components/TrialTable.tsx b/standalone_app/src/components/TrialTable.tsx index 58d2d101..9e890c83 100644 --- a/standalone_app/src/components/TrialTable.tsx +++ b/standalone_app/src/components/TrialTable.tsx @@ -25,7 +25,7 @@ export const TrialTable: FC<{ field: "values", label: "Value", sortable: true, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, ascending): number => { const firstVal = firstEl.values?.[0] const secondVal = secondEl.values?.[0] @@ -33,9 +33,9 @@ export const TrialTable: FC<{ return 0 } if (firstVal === undefined) { - return -1 + return ascending ? -1 : 1 } else if (secondVal === undefined) { - return 1 + return ascending ? 1 : -1 } if (firstVal === "-inf" || secondVal === "inf") { return 1 @@ -57,7 +57,7 @@ export const TrialTable: FC<{ field: "values", label: `Objective ${objectiveId}`, sortable: true, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, ascending): number => { const firstVal = firstEl.values?.[objectiveId] const secondVal = secondEl.values?.[objectiveId] @@ -65,9 +65,9 @@ export const TrialTable: FC<{ return 0 } if (firstVal === undefined) { - return -1 + return ascending ? -1 : 1 } else if (secondVal === undefined) { - return 1 + return ascending ? 1 : -1 } if (firstVal === "-inf" || secondVal === "inf") { return 1 @@ -96,7 +96,7 @@ export const TrialTable: FC<{ null, sortable: true, filterable: false, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, _): number => { const firstVal = firstEl.params.find( (p) => p.name === s.name )?.param_internal_value @@ -126,7 +126,7 @@ export const TrialTable: FC<{ ?.value || null, sortable: attr_spec.sortable, filterable: false, - less: (firstEl, secondEl): number => { + less: (firstEl, secondEl, _): number => { const firstVal = firstEl.user_attrs.find( (attr) => attr.key === attr_spec.key )?.value From a4ff5c61afd4e98cd94b0d94b5620940c07446c2 Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:14:02 +0100 Subject: [PATCH 2/3] Suppress no-unused-vars --- optuna_dashboard/ts/components/TrialTable.tsx | 2 ++ standalone_app/src/components/TrialTable.tsx | 2 ++ 2 files changed, 4 insertions(+) diff --git a/optuna_dashboard/ts/components/TrialTable.tsx b/optuna_dashboard/ts/components/TrialTable.tsx index 6090ee85..c708f6ee 100644 --- a/optuna_dashboard/ts/components/TrialTable.tsx +++ b/optuna_dashboard/ts/components/TrialTable.tsx @@ -106,6 +106,7 @@ export const TrialTable: FC<{ ?.param_external_value || null, sortable: sortable, filterable: filterable, + // eslint-disable-next-line @typescript-eslint/no-unused-vars less: (firstEl, secondEl, _): number => { const firstVal = firstEl.params.find( (p) => p.name === s.name @@ -146,6 +147,7 @@ export const TrialTable: FC<{ ?.value || null, sortable: attr_spec.sortable, filterable: !attr_spec.sortable, + // eslint-disable-next-line @typescript-eslint/no-unused-vars less: (firstEl, secondEl, _): number => { const firstVal = firstEl.user_attrs.find( (attr) => attr.key === attr_spec.key diff --git a/standalone_app/src/components/TrialTable.tsx b/standalone_app/src/components/TrialTable.tsx index 9e890c83..5a0efc96 100644 --- a/standalone_app/src/components/TrialTable.tsx +++ b/standalone_app/src/components/TrialTable.tsx @@ -96,6 +96,7 @@ export const TrialTable: FC<{ null, sortable: true, filterable: false, + // eslint-disable-next-line @typescript-eslint/no-unused-vars less: (firstEl, secondEl, _): number => { const firstVal = firstEl.params.find( (p) => p.name === s.name @@ -126,6 +127,7 @@ export const TrialTable: FC<{ ?.value || null, sortable: attr_spec.sortable, filterable: false, + // eslint-disable-next-line @typescript-eslint/no-unused-vars less: (firstEl, secondEl, _): number => { const firstVal = firstEl.user_attrs.find( (attr) => attr.key === attr_spec.key From 31360a4597863da09019edbbfbec6060e8c584c1 Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Fri, 27 Oct 2023 08:21:49 +0100 Subject: [PATCH 3/3] Lint --- optuna_dashboard/ts/components/DataGrid.tsx | 4 +++- standalone_app/src/components/DataGrid.tsx | 4 +++- standalone_app/src/components/TrialTable.tsx | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/optuna_dashboard/ts/components/DataGrid.tsx b/optuna_dashboard/ts/components/DataGrid.tsx index d19ec9d9..24e45dc7 100644 --- a/optuna_dashboard/ts/components/DataGrid.tsx +++ b/optuna_dashboard/ts/components/DataGrid.tsx @@ -359,7 +359,9 @@ function stableSort( stabilizedThis.sort((a, b) => { if (less) { const ascending = order == "asc" - const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending) + const result = ascending + ? -less(a[0], b[0], ascending) + : less(a[0], b[0], ascending) if (result !== 0) return result } else { const result = comparator(a[0], b[0]) diff --git a/standalone_app/src/components/DataGrid.tsx b/standalone_app/src/components/DataGrid.tsx index d19ec9d9..24e45dc7 100644 --- a/standalone_app/src/components/DataGrid.tsx +++ b/standalone_app/src/components/DataGrid.tsx @@ -359,7 +359,9 @@ function stableSort( stabilizedThis.sort((a, b) => { if (less) { const ascending = order == "asc" - const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending) + const result = ascending + ? -less(a[0], b[0], ascending) + : less(a[0], b[0], ascending) if (result !== 0) return result } else { const result = comparator(a[0], b[0]) diff --git a/standalone_app/src/components/TrialTable.tsx b/standalone_app/src/components/TrialTable.tsx index 5a0efc96..97a04f3c 100644 --- a/standalone_app/src/components/TrialTable.tsx +++ b/standalone_app/src/components/TrialTable.tsx @@ -65,7 +65,7 @@ export const TrialTable: FC<{ return 0 } if (firstVal === undefined) { - return ascending ? -1 : 1 + return ascending ? -1 : 1 } else if (secondVal === undefined) { return ascending ? 1 : -1 }