-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Sort run names with leading numbers differently #6664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+420
−29
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33b2bae
sort runs with numeric prefixes differently
rileyajones 91a36c7
add and update tests
rileyajones 8b3639d
add undefined handling strategy
rileyajones e453d0d
add license
rileyajones 1fa3f32
address pr comments
rileyajones 71d8ba1
add one more test case
rileyajones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = new Set(['string', 'number']); | ||
rileyajones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 orderFromLocalComparison(aValue, bValue); | ||
} | ||
|
||
if ( | ||
POTENTIALLY_NUMERIC.has(typeof aValue) && | ||
POTENTIALLY_NUMERIC.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 orderFromLocalComparison(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 orderFromLocalComparison(aPostfix, bPostfix, { | ||
insertUndefined: UndefinedStrategy.BEFORE, | ||
}); | ||
} | ||
|
||
return orderFromLocalComparison(aPrefix, bPrefix); | ||
} | ||
} | ||
|
||
return orderFromLocalComparison(aValue, bValue); | ||
}); | ||
return sortedItems; | ||
|
||
function orderFromLocalComparison( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand the name of this function. Maybe just call it compareValues? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this interface with a single attribute. Is there a reason you cannot just use the enum directly everywhere this interface is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty complicated function and I thought that using an object as a parameter would make the options provided more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am ok with that justification. I think I would prefer naming the enum something more readable....UndefinedValuePlacement?
I cant really think of something good. Maybe this is best.