Skip to content

Commit

Permalink
Sort run names with leading numbers differently (tensorflow#6664)
Browse files Browse the repository at this point in the history
## 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
rileyajones authored and bmd3k committed Nov 2, 2023
1 parent bd27895 commit 9b76dbc
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 29 deletions.
2 changes: 2 additions & 0 deletions tensorboard/webapp/runs/views/runs_table/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ tf_ng_module(
"runs_table_component.ts",
"runs_table_container.ts",
"runs_table_module.ts",
"sorting_utils.ts",
],
assets = [
":regex_edit_dialog_styles",
Expand Down Expand Up @@ -131,6 +132,7 @@ tf_ts_library(
"regex_edit_dialog_test.ts",
"runs_data_table_test.ts",
"runs_table_test.ts",
"sorting_utils_test.ts",
],
deps = [
":runs_table",
Expand Down
30 changes: 1 addition & 29 deletions tensorboard/webapp/runs/views/runs_table/runs_table_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import {
ColumnHeader,
FilterAddedEvent,
SortingInfo,
SortingOrder,
TableData,
} from '../../../widgets/data_table/types';
import {
Expand Down Expand Up @@ -101,6 +100,7 @@ import {
getPotentialHparamColumns,
} from '../../../metrics/views/main_view/common_selectors';
import {runsTableFullScreenToggled} from '../../../core/actions';
import {sortTableDataItems} from './sorting_utils';

const getRunsLoading = createSelector<
State,
Expand Down Expand Up @@ -182,34 +182,6 @@ function sortRunTableItems(
return sortedItems;
}

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 bValue === undefined ? -1 : 1;
}

return aValue < bValue === (sort.order === SortingOrder.ASCENDING) ? -1 : 1;
});
return sortedItems;
}

function matchFilter(
filter: DiscreteFilter | IntervalFilter,
value: number | DiscreteHparamValue | undefined
Expand Down
131 changes: 131 additions & 0 deletions tensorboard/webapp/runs/views/runs_table/sorting_utils.ts
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;
}
}
Loading

0 comments on commit 9b76dbc

Please sign in to comment.