forked from tensorflow/tensorboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort run names with leading numbers differently (tensorflow#6664)
## Motivation for features / changes We have been treating all run names as strings even though some run names are (serialized) numbers and some begin with numbers. This means that sorting worked pretty unintuitively. Note: I've also changed the behavior around sorting `undefined` values. When sorting descending they should now appear at the top of the list. This was a recent change but it wasn't clear it was intentional and I found it made the code more complex. tensorflow#6651 Internal users see [b/278671226](http://b/278671226) ## Screenshots of UI changes (or N/A) Before: ![image](https://github.com/tensorflow/tensorboard/assets/78179109/5f805c37-283d-4f55-b1bf-3dfa4d9ea1da) After: ![image](https://github.com/tensorflow/tensorboard/assets/78179109/0d740b6e-2ed5-4762-aec0-22400eeb152d) ![image](https://github.com/tensorflow/tensorboard/assets/78179109/5283bade-b4da-401d-9893-932d9fb9d378)
- Loading branch information
1 parent
bd27895
commit 9b76dbc
Showing
4 changed files
with
420 additions
and
29 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
131 changes: 131 additions & 0 deletions
131
tensorboard/webapp/runs/views/runs_table/sorting_utils.ts
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,131 @@ | ||
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. | ||
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. | ||
==============================================================================*/ | ||
import { | ||
SortingInfo, | ||
SortingOrder, | ||
TableData, | ||
} from '../../../widgets/data_table/types'; | ||
import {ExperimentAlias} from '../../../experiments/types'; | ||
|
||
enum UndefinedStrategy { | ||
BEFORE, | ||
AFTER, | ||
} | ||
|
||
interface SortOptions { | ||
insertUndefined: UndefinedStrategy; | ||
} | ||
|
||
const POTENTIALLY_NUMERIC_TYPES = new Set(['string', 'number']); | ||
|
||
const DEFAULT_SORT_OPTIONS: SortOptions = { | ||
insertUndefined: UndefinedStrategy.AFTER, | ||
}; | ||
|
||
export function parseNumericPrefix(value: string | number) { | ||
if (typeof value === 'number') { | ||
return isNaN(value) ? undefined : value; | ||
} | ||
|
||
if (!isNaN(parseInt(value))) { | ||
return parseInt(value); | ||
} | ||
|
||
for (let i = 0; i < value.length; i++) { | ||
if (isNaN(parseInt(value[i]))) { | ||
if (i === 0) return; | ||
return parseInt(value.slice(0, i)); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
export function sortTableDataItems( | ||
items: TableData[], | ||
sort: SortingInfo | ||
): TableData[] { | ||
const sortedItems = [...items]; | ||
|
||
sortedItems.sort((a, b) => { | ||
let aValue = a[sort.name]; | ||
let bValue = b[sort.name]; | ||
|
||
if (sort.name === 'experimentAlias') { | ||
aValue = (aValue as ExperimentAlias).aliasNumber; | ||
bValue = (bValue as ExperimentAlias).aliasNumber; | ||
} | ||
|
||
if (aValue === bValue) { | ||
return 0; | ||
} | ||
|
||
if (aValue === undefined || bValue === undefined) { | ||
return compareValues(aValue, bValue); | ||
} | ||
|
||
if ( | ||
POTENTIALLY_NUMERIC_TYPES.has(typeof aValue) && | ||
POTENTIALLY_NUMERIC_TYPES.has(typeof bValue) | ||
) { | ||
const aPrefix = parseNumericPrefix(aValue as string | number); | ||
const bPrefix = parseNumericPrefix(bValue as string | number); | ||
// Show runs with numbers before to runs without numbers | ||
if ( | ||
(aPrefix === undefined || bPrefix === undefined) && | ||
aPrefix !== bPrefix | ||
) { | ||
return compareValues(aPrefix, bPrefix, { | ||
insertUndefined: UndefinedStrategy.BEFORE, | ||
}); | ||
} | ||
if (aPrefix !== undefined && bPrefix !== undefined) { | ||
if (aPrefix === bPrefix) { | ||
const aPostfix = | ||
aValue.toString().slice(aPrefix.toString().length) || undefined; | ||
const bPostfix = | ||
bValue.toString().slice(bPrefix.toString().length) || undefined; | ||
return compareValues(aPostfix, bPostfix, { | ||
insertUndefined: UndefinedStrategy.BEFORE, | ||
}); | ||
} | ||
|
||
return compareValues(aPrefix, bPrefix); | ||
} | ||
} | ||
|
||
return compareValues(aValue, bValue); | ||
}); | ||
return sortedItems; | ||
|
||
function compareValues( | ||
a: TableData[string] | undefined, | ||
b: TableData[string] | undefined, | ||
{insertUndefined}: SortOptions = DEFAULT_SORT_OPTIONS | ||
) { | ||
if (a === b) { | ||
return 0; | ||
} | ||
|
||
if (a === undefined) { | ||
return insertUndefined === UndefinedStrategy.AFTER ? 1 : -1; | ||
} | ||
if (b === undefined) { | ||
return insertUndefined === UndefinedStrategy.AFTER ? -1 : 1; | ||
} | ||
|
||
return a < b === (sort.order === SortingOrder.ASCENDING) ? -1 : 1; | ||
} | ||
} |
Oops, something went wrong.