Skip to content
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

Adds common selectors for shared hparams #6732

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tensorboard/webapp/hparams/_redux/hparams_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ export const getDashboardDefaultHparamFilters = createSelector(
);

export const getDashboardDisplayedHparamColumns = createSelector(
getDashboardHparamsAndMetricsSpecs,
getHparamsState,
(state) => state.dashboardDisplayedHparamColumns
({hparams}, state) => {
const hparamSet = new Set(hparams.map((hparam) => hparam.name));
return state.dashboardDisplayedHparamColumns.filter((column) =>
hparamSet.has(column.name)
);
}
);

export const getDashboardHparamFilterMap = createSelector(
Expand Down
70 changes: 52 additions & 18 deletions tensorboard/webapp/hparams/_redux/hparams_selectors_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.

import {ColumnHeaderType} from '../../widgets/data_table/types';
import {DomainType} from '../types';
import {State} from './types';
import * as selectors from './hparams_selectors';
import {
buildHparamSpec,
Expand Down Expand Up @@ -114,30 +115,63 @@ describe('hparams/_redux/hparams_selectors_test', () => {
});

describe('#getDashboardDisplayedHparamColumns', () => {
it('returns dashboard displayed hparam columns', () => {
const fakeColumns = [
{
type: ColumnHeaderType.HPARAM,
name: 'conv_layers',
displayName: 'Conv Layers',
enabled: true,
},
{
type: ColumnHeaderType.HPARAM,
name: 'conv_kernel_size',
displayName: 'Conv Kernel Size',
enabled: true,
},
];
it('returns no columns if no hparam specs', () => {
const state = buildStateFromHparamsState(
buildHparamsState({
dashboardDisplayedHparamColumns: fakeColumns,
dashboardSpecs: {
hparams: [],
},
dashboardDisplayedHparamColumns: [
{
type: ColumnHeaderType.HPARAM,
name: 'conv_layers',
displayName: 'Conv Layers',
enabled: true,
},
{
type: ColumnHeaderType.HPARAM,
name: 'conv_kernel_size',
displayName: 'Conv Kernel Size',
enabled: true,
},
],
})
);

expect(selectors.getDashboardDisplayedHparamColumns(state)).toEqual(
fakeColumns
expect(selectors.getDashboardDisplayedHparamColumns(state)).toEqual([]);
});

it('returns only hparam columns that have specs', () => {
const state = buildStateFromHparamsState(
buildHparamsState({
dashboardSpecs: {
hparams: [buildHparamSpec({name: 'conv_layers'})],
},
dashboardDisplayedHparamColumns: [
{
type: ColumnHeaderType.HPARAM,
name: 'conv_layers',
displayName: 'Conv Layers',
enabled: true,
},
{
type: ColumnHeaderType.HPARAM,
name: 'conv_kernel_size',
displayName: 'Conv Kernel Size',
enabled: true,
},
],
})
);

expect(selectors.getDashboardDisplayedHparamColumns(state)).toEqual([
{
type: ColumnHeaderType.HPARAM,
name: 'conv_layers',
displayName: 'Conv Layers',
enabled: true,
},
]);
});
});
});
1 change: 1 addition & 0 deletions tensorboard/webapp/metrics/views/main_view/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tf_ts_library(
"//tensorboard/webapp/metrics/views:utils",
"//tensorboard/webapp/metrics/views/card_renderer:scalar_card_types",
"//tensorboard/webapp/runs:types",
"//tensorboard/webapp/runs/store:selectors",
"//tensorboard/webapp/runs/views/runs_table:types",
"//tensorboard/webapp/util:matcher",
"//tensorboard/webapp/util:memoize",
Expand Down
43 changes: 42 additions & 1 deletion tensorboard/webapp/metrics/views/main_view/common_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 {createSelector} from '@ngrx/store';
import {createSelector, Selector} from '@ngrx/store';
import {State} from '../../../app_state';
import {
getCurrentRouteRunSelection,
Expand All @@ -33,6 +33,7 @@ import {
getDashboardHparamsAndMetricsSpecs,
getDashboardHparamFilterMap,
getDashboardDefaultHparamFilters,
getDashboardDisplayedHparamColumns,
} from '../../../hparams/_redux/hparams_selectors';
import {
DiscreteFilter,
Expand All @@ -44,6 +45,7 @@ import {
RunTableItem,
RunTableExperimentItem,
} from '../../../runs/views/runs_table/types';
import {getRunsTableHeaders} from '../../../runs/store/runs_selectors';
import {matchRunToRegex} from '../../../util/matcher';
import {isSingleRunPlugin, PluginType} from '../../data_source';
import {getNonEmptyCardIdsWithMetadata, TagMetadata} from '../../store';
Expand Down Expand Up @@ -287,10 +289,49 @@ export const getPotentialHparamColumns = createSelector(
sortable: true,
movable: true,
filterable: true,
hidable: true,
}));
}
);

export const getSelectableColumns = createSelector(
getPotentialHparamColumns,
getDashboardDisplayedHparamColumns,
(potentialColumns, currentColumns) => {
const currentColumnNames = new Set(currentColumns.map(({name}) => name));
return potentialColumns.filter((columnHeader) => {
return !currentColumnNames.has(columnHeader.name);
});
}
);

/** Returns a list of columns that have been sorted into logical groups.
*
* Column order: | RUN | experimentAlias | HPARAMs | other |
*/
export const getGroupedColumns = (
headersSelector: Selector<object, ColumnHeader[]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my opinion, feel free to push back.

We don't use the pattern of passing a selector to a selector anywhere else in the TB codebase. Due to this I expect this to confuse future devs coming to the project.
I would rather see this selectors projector moved to a utility function and shared by a series of selectors (not selector factories) created to select each type.

The result might look something like this

function groupHeaders(tableHeaders: ColumnHeader[], hparamHeaders: []) {
   // The projector function from the getGroupedColumns selector
}

export const selectGroupedRunTableHeaders = createSelector(
  getRunsTableHeaders,
  getDashboardDisplayedHparamColumns,
  groupHeaders
);

export const selectGroupedDataTableHeaders = createSelector(
  getDataTableHeaders,
  getDashboardDisplayedHparamColumns,
  groupHeaders
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather see this selectors projector moved to a utility function and shared by a series of selectors

This is indeed a much simpler abstraction than passing around selectors as function arguments. I created a groupColumns helper similar to what you described. I put it in /widgets/data_table/utils.ts because it doesn't really belong in the /metrics directory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved the runs and metrics selectors to their respective directories, and added a simple selector test for each.

) =>
createSelector(
headersSelector,
getDashboardDisplayedHparamColumns,
(tableHeaders, hparamHeaders): ColumnHeader[] => {
return [
...tableHeaders.filter((header) => header.type === 'RUN'),
...tableHeaders.filter(
(header) =>
header.type === 'CUSTOM' && header.name === 'experimentAlias'
),
...hparamHeaders,
...tableHeaders.filter(
(header) =>
header.type !== 'RUN' &&
!(header.type === 'CUSTOM' && header.name === 'experimentAlias')
),
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this approach of running a series of filters, then dumping everything which doesn't match at the end will scale if you end up with a handful of additional confitions.

Suggested change
return [
...tableHeaders.filter((header) => header.type === 'RUN'),
...tableHeaders.filter(
(header) =>
header.type === 'CUSTOM' && header.name === 'experimentAlias'
),
...hparamHeaders,
...tableHeaders.filter(
(header) =>
header.type !== 'RUN' &&
!(header.type === 'CUSTOM' && header.name === 'experimentAlias')
),
];
const headersGroups: Record<string, ColumnHeader[]> = {
RUN: [],
EXPERIMENT_ALIAS: [],
OTHER: [],
};
tableHeaders.forEach((header) => {
if (header.type === 'RUN') {
headersGroups['RUN'].push(header);
return;
}
if (header.type === 'CUSTOM' && header.name === 'experimentAlias') {
headerGroups['EXPERIMENT_ALIAS'].push(header);
return;
}
headersGroups['OTHER'].push(header);
})
return [
...headerGroups['RUN'],
...headerGroups['EXPERIMENT_ALIAS'],
...hparamHeaders,
...headerGroups['OTHER'],

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also wondering about a more robust way to do this and you gave me an idea. I implemented something along these lines in /widgets/data_table/utils.ts:groupColumns, PTAL!

}
);

export const getAllPotentialColumnsForCard = memoize((cardId: string) => {
return createSelector(
getColumnHeadersForCard(cardId),
Expand Down
Loading
Loading