diff --git a/src/containers/ColorByMenu/index.tsx b/src/containers/ColorByMenu/index.tsx index d699b9549..8f8f8a217 100644 --- a/src/containers/ColorByMenu/index.tsx +++ b/src/containers/ColorByMenu/index.tsx @@ -1,6 +1,6 @@ import { Checkbox, Col, Collapse, Row } from "antd"; import { CheckboxChangeEvent } from "antd/es/checkbox"; -import { filter, includes } from "lodash"; +import { includes } from "lodash"; import React from "react"; import { connect } from "react-redux"; import type { ActionCreator } from "redux"; @@ -23,12 +23,13 @@ import { BoolToggleAction, ChangeDownloadConfigAction, ChangeGroupByCategory, - ChangeSelectionAction, ColorForPlot, DeselectGroupOfPointsAction, + DiscreteFilterData, DownloadConfig, SelectAxisAction, SetColorOverrideAction, + SetFilterAction, } from "../../state/selection/types"; import { State } from "../../state/types"; import { getColorByDisplayOptions, getGroupByDisplayOptions } from "../MainPlotContainer/selectors"; @@ -57,7 +58,7 @@ interface PropsFromState { downloadUrls: string[]; downloadConfig: DownloadConfig; downloadRoot: string; - filtersToExclude: string[]; + groupByFilter: DiscreteFilterData | undefined; interactivePanelData: PanelData[]; selectionSetsPanelData: PanelData[]; isInIndeterminateState: boolean; @@ -74,7 +75,7 @@ interface DispatchProps { handleChangeAxis: ActionCreator; handleChangeGroupByCategory: ActionCreator; handleCloseSelectionSet: ActionCreator; - handleFilterByCategoryName: ActionCreator; + handleSetFilter: ActionCreator; handleChangeDownloadSettings: ActionCreator; handleSetColorOverride: ActionCreator; } @@ -107,12 +108,24 @@ class ColorByMenu extends React.Component { this.onSelectionSetDownloadButtonClicked.bind(this); } + private getDiscreteFilterCopy(filter: DiscreteFilterData | undefined): DiscreteFilterData { + return filter + ? { ...filter, excludedValues: new Set(filter.excludedValues) } + : { discrete: true, excludedValues: new Set() }; + } + public onBarClicked({ target }: CheckboxChangeEvent) { - const { handleFilterByCategoryName, filtersToExclude } = this.props; - const newFilterList = includes(filtersToExclude, target.value) - ? filter(filtersToExclude, (e) => e !== target.value) - : [...filtersToExclude, target.value]; - handleFilterByCategoryName(newFilterList); + const { groupByFilter, groupBy, handleSetFilter: handleSetFeatureFilter } = this.props; + const filter = this.getDiscreteFilterCopy(groupByFilter); + // Toggle whether the category is excluded from the filter + if (filter.discrete) { + if (target.checked) { + filter.excludedValues.delete(target.value); + } else { + filter.excludedValues.add(target.value); + } + } + handleSetFeatureFilter(groupBy.toString(), filter); } public onCategorySetDownloadButtonClicked(categoryName: string) { @@ -132,12 +145,22 @@ class ColorByMenu extends React.Component { } public allOnOff({ target }: CheckboxChangeEvent) { - const { handleFilterByCategoryName, interactivePanelData } = this.props; - if (target.checked) { - return handleFilterByCategoryName([]); + const { + groupByFilter, + groupBy, + handleSetFilter: handleSetFeatureFilter, + interactivePanelData, + } = this.props; + const filter = this.getDiscreteFilterCopy(groupByFilter); + if (filter.discrete) { + if (target.checked) { + filter.excludedValues.clear(); + } else { + const keys = interactivePanelData.map((ele: PanelData) => ele.id); + keys.forEach((key) => filter?.excludedValues.add(key)); + } } - const keys = interactivePanelData.map((ele: PanelData) => ele.id); - handleFilterByCategoryName(keys); + handleSetFeatureFilter(groupBy.toString(), filter); } public onActivePanelChange(value: string | string[]) { @@ -205,7 +228,6 @@ class ColorByMenu extends React.Component { public renderTaggedStructuresPanel() { const { - filtersToExclude, isInIndeterminateState, interactivePanelData, downloadUrls, @@ -262,7 +284,7 @@ class ColorByMenu extends React.Component {
Show/Hide all @@ -353,7 +375,7 @@ function mapStateToProps(state: State): PropsFromState { downloadConfig: selectionStateBranch.selectors.getDownloadConfig(state), downloadUrls: createUrlFromListOfIds(state), downloadRoot: getDownloadRoot(state), - filtersToExclude: selectionStateBranch.selectors.getFiltersToExclude(state), + groupByFilter: selectionStateBranch.selectors.getGroupByFilter(state), groupByTitle: getGroupByTitle(state), interactivePanelData: getInteractivePanelData(state), selectionSetsPanelData: getSelectionPanelData(state), @@ -367,7 +389,7 @@ const dispatchToPropsMap: DispatchProps = { handleChangeGroupByCategory: selectionStateBranch.actions.changeGroupByCategory, handleChangeDownloadSettings: selectionStateBranch.actions.changeDownloadSettings, handleCloseSelectionSet: selectionStateBranch.actions.deselectGroupOfPoints, - handleFilterByCategoryName: selectionStateBranch.actions.toggleFilterByCategoryName, + handleSetFilter: selectionStateBranch.actions.setFilter, handleSetColorOverride: selectionStateBranch.actions.setColorOverride, }; export default connect( diff --git a/src/containers/ColorByMenu/selectors.ts b/src/containers/ColorByMenu/selectors.ts index 4bb0a6403..5f3dc3526 100644 --- a/src/containers/ColorByMenu/selectors.ts +++ b/src/containers/ColorByMenu/selectors.ts @@ -1,4 +1,4 @@ -import { chunk, includes, map, reduce, values } from "lodash"; +import { chunk, map, reduce, values } from "lodash"; import { createSelector } from "reselect"; import { @@ -23,7 +23,6 @@ import { getColorBySelection, getDownloadConfig, getDownloadRoot, - getFiltersToExclude, getSelectedGroupKeys, getSelectedGroups, getSelectedSetTotals, @@ -37,8 +36,13 @@ import { getYValues, getXValues, getColorOverrides, + getGroupByFilter, } from "../../state/selection/selectors"; -import { ColorForPlot, LassoOrBoxSelectPointData } from "../../state/selection/types"; +import { + ColorForPlot, + DiscreteFilterData, + LassoOrBoxSelectPointData, +} from "../../state/selection/types"; import { convertSingleImageIdToDownloadId } from "../../state/util"; import { PanelData } from "./types"; @@ -65,9 +69,14 @@ export const getGroupByTitle = createSelector( ); export const getCheckAllCheckboxIsIntermediate = createSelector( - [getFiltersToExclude, getGroupingCategoryNames], - (filtersToExclude, categoryNames: string[]): boolean => { - return filtersToExclude.length > 0 && filtersToExclude.length !== categoryNames.length; + [getGroupByFilter, getGroupingCategoryNames], + (groupByFilter, categoryNames: string[]): boolean => { + const excludedValues = groupByFilter?.excludedValues; + return ( + excludedValues !== undefined && + excludedValues.size > 0 && + excludedValues.size !== categoryNames.length + ); } ); @@ -126,7 +135,7 @@ export const getInteractivePanelData = createSelector( getGroupByCategory, getColorBySelection, getGroupByFeatureOptionsAsList, - getFiltersToExclude, + getGroupByFilter, getDisplayableGroups, getGroupingCategoryNamesAsArray, getColorOverrides, @@ -135,7 +144,7 @@ export const getInteractivePanelData = createSelector( categoryToGroupBy: keyof MappingOfMeasuredValuesArrays, categoryToColorBy: keyof MappingOfMeasuredValuesArrays, categories: MeasuredFeaturesOption[], - filtersToExclude: string[], + groupByFilter: DiscreteFilterData | undefined, displayableGroups: string[], categoryNamesArray: string[], colorOverrides: (string | undefined)[] @@ -168,15 +177,16 @@ export const getInteractivePanelData = createSelector( const id = category.key ?? category.name; const total: number = countsByCategory[id] || category.count || 0; const disabled = !displayableGroups.includes(id); + const excluded = groupByFilter?.excludedValues.has(id) ?? false; const color = getColorForCategory( categoryToColorBy === categoryToGroupBy, - includes(filtersToExclude, id), + excluded, disabled, category.color, colorOverrides[index] ); return { - checked: !includes(filtersToExclude, id), + checked: !excluded, color: color, id: id, name: names[index], diff --git a/src/containers/ColorByMenu/style.module.css b/src/containers/ColorByMenu/style.module.css index 4a8752aad..bbd58a27e 100644 --- a/src/containers/ColorByMenu/style.module.css +++ b/src/containers/ColorByMenu/style.module.css @@ -34,4 +34,11 @@ .feature-select-row :global(.ant-select) { width: 100%; -} \ No newline at end of file +} + +/* Make intermediate checkbox visible */ +.interactive-legend-container + :global(.ant-checkbox.ant-checkbox-indeterminate) + > :global(.ant-checkbox-inner)::after { + background-color: var(--dimgray); +} diff --git a/src/containers/MainPlotContainer/index.tsx b/src/containers/MainPlotContainer/index.tsx index d4d965f6f..11da4499c 100644 --- a/src/containers/MainPlotContainer/index.tsx +++ b/src/containers/MainPlotContainer/index.tsx @@ -63,7 +63,6 @@ interface PropsFromState { annotations: PlotlyAnnotation[]; categoricalFeatures: string[]; clickedPoints: string[]; - filtersToExclude: string[]; galleryCollapsed: boolean; hoveredPointData: SelectedPointData | null; hoveredXValue: string; @@ -188,17 +187,14 @@ class MainPlotContainer extends React.Component { - if ( - point.data.name === SCATTER_PLOT_NAME && - !includes(filtersToExclude, point.fullData.name) - ) { + if (point.data.name === SCATTER_PLOT_NAME) { window.clearTimeout(this.thumbnailTimeout); changeHoveredPoint({ [CELL_ID_KEY]: point.id, @@ -382,7 +378,6 @@ function mapStateToProps(state: State): PropsFromState { annotations: getAnnotations(state), clickedPoints: selectionStateBranch.selectors.getClickedScatterPoints(state), categoricalFeatures: metadataStateBranch.selectors.getCategoricalFeatureKeys(state), - filtersToExclude: selectionStateBranch.selectors.getFiltersToExclude(state), galleryCollapsed: selectionStateBranch.selectors.getGalleryCollapsed(state), hoveredPointData: getDataForOverlayCard(state), hoveredXValue: getFormattedHoveredXValue(state), diff --git a/src/containers/MainPlotContainer/selectors.ts b/src/containers/MainPlotContainer/selectors.ts index 477d73e2a..c521a5486 100644 --- a/src/containers/MainPlotContainer/selectors.ts +++ b/src/containers/MainPlotContainer/selectors.ts @@ -50,8 +50,9 @@ import { getLineMovingAverageWindow, getConnectByFeature, getConnectByCategory, - getFiltersToExclude, + getNumericFilters, } from "../../state/selection/selectors"; +import { isIdExcludedByFilters } from "../../state/util"; import { ColorForPlot, MainPlotSettings, @@ -296,19 +297,17 @@ const getAnnotationData = createSelector( [ getPerCellDataForPlot, getClickedCellsFileInfo, - getFiltersToExclude, + getNumericFilters, getPlotByOnX, getPlotByOnY, - getGroupingCategoryNamesAsArray, getHoveredCardId, ], ( allCellData: DataForPlot, clickedCellsFileInfo: FileInfo[], - filtersToExclude: string[], + numericFilters, xAxis, yAxis, - groupByCategoryNames, currentHoveredCellId ): AnnotationData[] => { if (isEmpty(allCellData.values) || isEmpty(allCellData.labels)) { @@ -332,8 +331,7 @@ const getAnnotationData = createSelector( const y = allCellData.values[yAxis][pointIndex] ?? null; // Apply filtering - const groupName = groupByCategoryNames[pointIndex] ?? null; - if (filtersToExclude.includes(groupName)) { + if (isIdExcludedByFilters(pointIndex, allCellData, numericFilters)) { return acc; } diff --git a/src/containers/MainPlotContainer/test/selectors.test.ts b/src/containers/MainPlotContainer/test/selectors.test.ts index 7dcf22993..dafb55a92 100644 --- a/src/containers/MainPlotContainer/test/selectors.test.ts +++ b/src/containers/MainPlotContainer/test/selectors.test.ts @@ -14,7 +14,7 @@ import { } from "../selectors"; import type { PlotlyAnnotation } from "../../../components/MainPlot"; import { CELL_ID_KEY, PALETTE } from "../../../constants"; -import type { ColorForPlot } from "../../../state/selection/types"; +import type { ColorForPlot, FilterData } from "../../../state/selection/types"; describe("MainPlotContainer selectors", () => { const newMockState = mockState; @@ -76,7 +76,15 @@ describe("MainPlotContainer selectors", () => { // 2 cells are selected, but the first (cell ID 1) will be // excluded by filtering selectedPoints: selectedCellFileInfo, - filterExclude: ["Paxillin"], + filters: new Map([ + [ + newMockState.selection.groupBy, + { + discrete: true, + excludedValues: new Set(["Paxillin"]), + }, + ], + ]), }, }; const result: PlotlyAnnotation[] = getAnnotations(state); diff --git a/src/state/selection/actions.ts b/src/state/selection/actions.ts index 34a2616aa..89cb3643b 100755 --- a/src/state/selection/actions.ts +++ b/src/state/selection/actions.ts @@ -16,7 +16,6 @@ import { SET_MOUSE_POSITION, SYNC_STATE_WITH_URL, TOGGLE_APPLY_SELECTION_SET_COLOR, - TOGGLE_FILTER_BY_CATEGORY_NAME, TOGGLE_GALLERY_OPEN_CLOSE, CHANGE_DATASET, RECEIVE_FILE_INFO, @@ -37,6 +36,9 @@ import { SET_CONNECT_LINE_WIDTH, SET_CONNECT_LINE_DEFAULT_COLOR, SET_CONNECT_LINE_OPACITY, + SET_FILTER_ACTIVE, + SET_FILTER, + CLEAR_FILTERS, } from "./constants"; import { BoolToggleAction, @@ -49,6 +51,7 @@ import { DeselectGroupOfPointsAction, DeselectPointAction, DownloadConfig, + FilterData, LassoOrBoxSelectAction, LassoOrBoxSelectPointData, MousePosition, @@ -62,6 +65,8 @@ import { SetColorOverrideAction, SetColorOverridesAction, SetCsvUrlAction, + SetFilterActiveAction, + SetFilterAction, SetLineAverageWindowAction, SetLineDefaultColorAction, SetLineOpacityAction, @@ -69,6 +74,7 @@ import { SetPointOpacityAction, SetPointRadiusAction, SyncStateWithURLAction, + ClearFiltersAction, } from "./types"; export function changeAxis(axisId: string, payload: string): SelectAxisAction { @@ -204,10 +210,40 @@ export function setPointOpacity(payload: number): SetPointOpacityAction { }; } -export function toggleFilterByCategoryName(payload: string): ChangeSelectionAction { +/** + * Sets a filter for the specified feature. If the feature is selected as the + * groupBy feature, or if the feature is marked as active for filtering (see + * `setFilterActive`), points that are excluded by/not in range of this filter + * will be excluded from the plot. + * @param feature The key of the feature to set the filter for. + * @param filter The filter data to apply to the feature. + */ +export function setFilter(feature: string, filter: FilterData | null): SetFilterAction { return { - payload, - type: TOGGLE_FILTER_BY_CATEGORY_NAME, + payload: { feature, filter }, + type: SET_FILTER, + }; +} + +/** + * Sets whether the specified feature's filter should be applied to points in + * the plot. + * @param feature The key of the feature to set the filter active state for. + * @param active Whether the filter should be applied to points in the plot. + */ +export function setFilterActive(feature: string, active: boolean): SetFilterActiveAction { + return { + payload: { + feature, + active, + }, + type: SET_FILTER_ACTIVE, + }; +} + +export function clearFilters(): ClearFiltersAction { + return { + type: CLEAR_FILTERS, }; } diff --git a/src/state/selection/constants.ts b/src/state/selection/constants.ts index 75347fc40..d7d62b8ab 100755 --- a/src/state/selection/constants.ts +++ b/src/state/selection/constants.ts @@ -25,9 +25,9 @@ export const SET_COLOR_OVERRIDE = makeSelectionConstant("set-color-override"); export const SET_COLOR_OVERRIDES = makeSelectionConstant("set-color-overrides"); export const SET_POINT_RADIUS = makeSelectionConstant("set-point-radius"); export const SET_POINT_OPACITY = makeSelectionConstant("set-point-opacity"); -export const TOGGLE_FILTER_BY_CATEGORY_NAME = makeSelectionConstant( - "toggle-filter-by-category-name" -); +export const SET_FILTER = makeSelectionConstant("set-filter"); +export const CLEAR_FILTERS = makeSelectionConstant("clear-filters"); +export const SET_FILTER_ACTIVE = makeSelectionConstant("set-filter-active"); export const OPEN_CELL_IN_3D = makeSelectionConstant("open-cell-in-3d"); export const TOGGLE_APPLY_SELECTION_SET_COLOR = makeSelectionConstant("apply-selection-set-color"); export const DESELECT_GROUP_OF_POINTS = makeSelectionConstant("deselect-group"); diff --git a/src/state/selection/logics.ts b/src/state/selection/logics.ts index fa0225d47..c14c576cd 100755 --- a/src/state/selection/logics.ts +++ b/src/state/selection/logics.ts @@ -26,6 +26,7 @@ import { changeConnectByCategory, changeConnectByFeature, changeGroupByCategory, + clearFilters, } from "./actions"; import { FileInfo } from "../metadata/types"; import { DatasetMetaData } from "../image-dataset/types"; @@ -119,6 +120,7 @@ const changeDatasetLogic = createLogic({ if (!featureKeys.has(state.selection.colorBy) && selections.defaultColorBy) { actions.push(changeAxis(COLOR_BY_SELECTOR, selections.defaultColorBy)); } + actions.push(clearFilters()); dispatch(batchActions(actions)); dispatch({ type: SET_DATASET, diff --git a/src/state/selection/reducer.ts b/src/state/selection/reducer.ts index d5dfa3b65..a9e82cefd 100755 --- a/src/state/selection/reducer.ts +++ b/src/state/selection/reducer.ts @@ -23,7 +23,6 @@ import { SET_DOWNLOAD_CONFIG, SET_MOUSE_POSITION, TOGGLE_APPLY_SELECTION_SET_COLOR, - TOGGLE_FILTER_BY_CATEGORY_NAME, TOGGLE_GALLERY_OPEN_CLOSE, SET_DATASET, RECEIVE_FILE_INFO_FOR_ALBUM_CELLS, @@ -44,6 +43,9 @@ import { SET_CONNECT_LINE_WIDTH, SET_CONNECT_LINE_DEFAULT_COLOR, SET_CONNECT_LINE_OPACITY, + SET_FILTER, + SET_FILTER_ACTIVE, + CLEAR_FILTERS, } from "./constants"; import { BoolToggleAction, @@ -54,7 +56,6 @@ import { ChangeHoveredPointAction, ChangeMousePositionAction, ChangeSelectedDatasetAction, - ChangeSelectionAction, ClearDatasetAction, DeselectGroupOfPointsAction, DeselectPointAction, @@ -69,6 +70,8 @@ import { SetColorOverrideAction, SetColorOverridesAction, SetCsvUrlAction, + SetFilterActiveAction, + SetFilterAction, SetLineAverageWindowAction, SetLineDefaultColorAction, SetLineOpacityAction, @@ -76,6 +79,7 @@ import { SetPointOpacityAction, SetPointRadiusAction, SetShowConnectLines, + ClearFiltersAction, } from "./types"; export const initialState: SelectionStateBranch = { @@ -88,7 +92,8 @@ export const initialState: SelectionStateBranch = { key: "", type: "", }, - filterExclude: [] as string[], + filters: new Map(), + activeFilters: new Set(), galleryCollapsed: true, hoveredCardId: "", hoveredPointData: null, @@ -349,12 +354,43 @@ const actionToConfigMap: TypeToDescriptionMap = { colorOverrides: action.payload, }), }, - [TOGGLE_FILTER_BY_CATEGORY_NAME]: { - accepts: (action: Action): action is ChangeSelectionAction => - action.type === TOGGLE_FILTER_BY_CATEGORY_NAME, - perform: (state: SelectionStateBranch, action: ChangeSelectionAction) => ({ + [SET_FILTER]: { + accepts: (action: Action): action is SetFilterAction => action.type === SET_FILTER, + perform: (state: SelectionStateBranch, action: SetFilterAction) => { + const newFilters = new Map(state.filters); + if (action.payload.filter === null) { + newFilters.delete(action.payload.feature); + } else { + newFilters.set(action.payload.feature, action.payload.filter); + } + return { + ...state, + filters: newFilters, + }; + }, + }, + [SET_FILTER_ACTIVE]: { + accepts: (action: Action): action is SetFilterActiveAction => + action.type === SET_FILTER_ACTIVE, + perform: (state: SelectionStateBranch, action: SetFilterActiveAction) => { + const newActiveFilters = new Set(state.activeFilters); + if (action.payload.active) { + newActiveFilters.add(action.payload.feature); + } else { + newActiveFilters.delete(action.payload.feature); + } + return { + ...state, + activeFilters: newActiveFilters, + }; + }, + }, + [CLEAR_FILTERS]: { + accepts: (action: Action): action is ClearFiltersAction => action.type === CLEAR_FILTERS, + perform: (state: SelectionStateBranch) => ({ ...state, - filterExclude: action.payload, + filters: new Map(), + activeFilters: new Set(), }), }, [TOGGLE_APPLY_SELECTION_SET_COLOR]: { diff --git a/src/state/selection/selectors.ts b/src/state/selection/selectors.ts index d8d0afa2f..949d91a21 100755 --- a/src/state/selection/selectors.ts +++ b/src/state/selection/selectors.ts @@ -25,15 +25,25 @@ import { PerCellLabels, } from "../metadata/types"; import { NumberOrString, SelectedGroups, State } from "../types"; -import { findFeature, getCategoryString, getFileInfoDatumFromCellId, sortNumeric } from "../util"; +import { + findFeature, + getCategoryString, + getFileInfoDatumFromCellId, + getNumericFilterFromFeature, + isIdExcludedByFilters, + sortNumeric, +} from "../util"; import { MISSING_CATEGORY_COLOR, MISSING_CATEGORY_LABEL } from "./constants"; import { ColorForPlot, + DiscreteFilterData, DownloadConfig, + FilterData, LassoOrBoxSelectPointData, MainPlotSettings, MousePosition, + NumericFilter, SelectedPointData, } from "./types"; @@ -64,7 +74,8 @@ export const getPointRadius = (state: State): number => state.selection.pointRad export const getPointOpacity = (state: State): number => state.selection.pointOpacity; export const getSelectionSetColors = (state: State): { [key: string]: string } => state.selection.selectedGroupColors; -export const getFiltersToExclude = (state: State): string[] => state.selection.filterExclude; +export const getActiveFilters = (state: State): Set => state.selection.activeFilters; +export const getFilters = (state: State): Map => state.selection.filters; export const getSelected3DCell = (state: State): string => state.selection.cellSelectedFor3D; export const getApplyColorToSelections = (state: State): boolean => state.selection.applySelectionSetColoring; @@ -373,6 +384,43 @@ export const getColorByCategoryCounts = createSelector( // FILTERED DATA // ============= +export const getGroupByFilter = createSelector( + [getFilters, getGroupByCategory], + (filters, groupByCategory): DiscreteFilterData | undefined => { + const groupByFilter = filters.get(groupByCategory); + if (!groupByFilter || !groupByFilter.discrete) { + return undefined; + } + return groupByFilter; + } +); + +export const getNumericFilters = createSelector( + [getActiveFilters, getFilters, getMeasuredFeaturesDefs, getGroupByCategory], + ( + activeFilters, + filters, + featureDefs, + groupByCategory + ): Map => { + // Add the group by category to the set of active filters. + activeFilters = new Set(activeFilters); + activeFilters.add(groupByCategory); + + const numericFilters: Map = new Map(); + for (const featureDef of featureDefs) { + if (activeFilters.has(featureDef.key)) { + const numericFilter = getNumericFilterFromFeature( + featureDef, + filters.get(featureDef.key) + ); + numericFilters.set(featureDef.key, numericFilter); + } + } + return numericFilters; + } +); + export const getFilteredCellData = createSelector( /** * Takes the full data and filters it based on the selected checkboxes and @@ -380,19 +428,27 @@ export const getFilteredCellData = createSelector( */ [ getMeasuredFeaturesKeys, - getFiltersToExclude, getPerCellDataForPlot, getGroupByCategory, getGroupingCategoryNamesAsArray, + getNumericFilters, ], ( measuredFeatureKeys, - filtersToExclude, - perCellDataForPlot: DataForPlot, + perCellDataForPlot, categoryKey: string, - groupingNames + groupingNames, + numericFilters ): DataForPlot => { - if (!filtersToExclude.length) { + const hasNoFilters = + numericFilters.size === 0 || + Array.from(numericFilters.values()).every( + (filter) => + filter === undefined || + (!filter.range && !filter.excludedIndices) || + (filter.excludedIndices && filter.excludedIndices.size === 0) + ); + if (hasNoFilters) { return { ...perCellDataForPlot, labels: { @@ -407,26 +463,27 @@ export const getFilteredCellData = createSelector( const thumbnails: string[] = []; const srcPaths: string[] = []; const indices: number[] = []; - for (let i = 0; i < perCellDataForPlot.labels.cellIds.length; i++) { - const categoryName: string = groupingNames[i]; - if (!includes(filtersToExclude, categoryName)) { - const cellId = perCellDataForPlot.labels.cellIds[i]; - cellIds.push(cellId); - categoryNameArray.push(categoryName); - thumbnails.push(perCellDataForPlot.labels.thumbnailPaths[i]); - srcPaths.push(perCellDataForPlot.labels.sourcePaths?.[i] ?? ""); - indices.push(perCellDataForPlot.indices[i]); - - measuredFeatureKeys.forEach((featureKey) => { - if (!dataToReturn[featureKey]) { - dataToReturn[featureKey] = []; - } - - dataToReturn[featureKey].push( - perCellDataForPlot.values[featureKey][i] as never - ); - }); + const numValues = perCellDataForPlot.labels?.cellIds?.length; + for (let i = 0; i < (numValues ?? 0); i++) { + const isExcluded = isIdExcludedByFilters(i, perCellDataForPlot, numericFilters); + if (isExcluded) { + continue; } + const categoryName: string = groupingNames[i]; + const cellId = perCellDataForPlot.labels.cellIds[i]; + cellIds.push(cellId); + categoryNameArray.push(categoryName); + thumbnails.push(perCellDataForPlot.labels.thumbnailPaths[i]); + srcPaths.push(perCellDataForPlot.labels.sourcePaths?.[i] ?? ""); + indices.push(perCellDataForPlot.indices[i]); + + measuredFeatureKeys.forEach((featureKey) => { + if (!dataToReturn[featureKey]) { + dataToReturn[featureKey] = []; + } + + dataToReturn[featureKey].push(perCellDataForPlot.values[featureKey][i] as never); + }); } return { indices: indices, diff --git a/src/state/selection/types.ts b/src/state/selection/types.ts index e17d260cc..ac0d5b3f3 100755 --- a/src/state/selection/types.ts +++ b/src/state/selection/types.ts @@ -3,6 +3,44 @@ import { CELL_ID_KEY, GENERAL_PLOT_SETTINGS } from "../../constants"; import type { URLSearchParamMap } from "../../util/UrlState"; import type { FileInfo } from "../metadata/types"; +export type DiscreteFilterData = { + discrete: true; + excludedValues: Set; +}; + +export type ContinuousFilterData = { + discrete: false; + /** Inclusive range for the filter. */ + range: [number, number]; +}; + +/** + * A filter on a feature, which excludes using string values for discrete data + * or with an (inclusive) range for continuous data. + * + * Can be parsed as a `NumericFilter` for applying against raw numeric feature + * data. + */ +export type FilterData = DiscreteFilterData | ContinuousFilterData; + +/** + * A precomputed filter that can be applied directly to raw numeric feature + * values. + */ +export type NumericFilter = { + featureKey: string; + /** + * For some discrete feature data `data`, the ID `i` is excluded by the + * filter if `excludedIndices.has(data[i]) === true`. + */ + excludedIndices?: Set; + /** + * For some continuous feature data `data`, the ID `i` is excluded by the + * filter if `data[i] < range[0]` or `range[1] < data[i]`. + */ + range?: [number, number]; +}; + export interface SelectionStateBranch { applySelectionSetColoring: boolean; cellSelectedFor3D: string; @@ -11,7 +49,8 @@ export interface SelectionStateBranch { /** Source of the URL for a CSV file. */ csvUrl: string; downloadConfig: DownloadConfig; - filterExclude: string[]; + filters: Map; + activeFilters: Set; galleryCollapsed: boolean; hoveredCardId: string; hoveredPointData: SelectedPointData | null; @@ -107,6 +146,26 @@ export interface ChangeSelectionAction { type: string; } +export interface ClearFiltersAction { + type: string; +} + +export interface SetFilterAction { + payload: { + feature: string; + filter: FilterData | null; + }; + type: string; +} + +export interface SetFilterActiveAction { + payload: { + feature: string; + active: boolean; + }; + type: string; +} + export interface ChangeDownloadConfigAction { payload: DownloadConfig; type: string; diff --git a/src/state/test/mocks.ts b/src/state/test/mocks.ts index 7af97804f..499f6ec3c 100644 --- a/src/state/test/mocks.ts +++ b/src/state/test/mocks.ts @@ -13,6 +13,7 @@ import { initialState as initialMetaData } from "../metadata/reducer"; import { DataForPlot, FileInfo, MeasuredFeatureDef } from "../metadata/types"; import { INITIAL_COLORS } from "../selection/constants"; import { initialState as initialSelectionState } from "../selection/reducer"; +import type { FilterData } from "../selection/types"; export const selectedCellFileInfo: FileInfo[] = [ { @@ -177,7 +178,6 @@ export const mockState = { }, downloadRoot: "dlurl/", groupBy: INITIAL_COLOR_AND_GROUP_BY, - filterExclude: [], plotByOnX: "apical-proximity", plotByOnY: "anaphase-segmentation-complete", defaultColors: INITIAL_COLORS, @@ -186,5 +186,6 @@ export const mockState = { selectedGroups: {}, selectedPoints: selectedCellFileInfo, displayableGroups: displayableGroups, + filters: new Map(), }, }; diff --git a/src/state/util.ts b/src/state/util.ts index 11fcef1e8..da8db71f1 100755 --- a/src/state/util.ts +++ b/src/state/util.ts @@ -3,8 +3,17 @@ import type { Action, Reducer } from "redux"; import { APP_ID, CELL_ID_KEY } from "../constants"; -import { DiscreteMeasuredFeatureDef, FileInfo, MeasuredFeatureDef } from "./metadata/types"; +import { + DataForPlot, + DiscreteMeasuredFeatureDef, + FileInfo, + MeasuredFeatureDef, +} from "./metadata/types"; import { BatchedAction, TypeToDescriptionMap } from "./types"; +import { FilterData, NumericFilter } from "./selection/types"; +import { + MISSING_CATEGORY_KEY +} from "./selection/constants"; const HTTP_REGEX = /^https?:\/\//; @@ -137,3 +146,94 @@ export function sortNumeric(arr: T[], toString: (item: T) => string): T[] { arr.sort((a, b) => collator.compare(toString(a), toString(b))); return arr; } + +/** + * Transforms a FilterData object into a NumericFilter object based on the + * feature definition, which can be compared against raw (numeric) feature + * values. + */ +export const getNumericFilterFromFeature = ( + featureDef: MeasuredFeatureDef, + filter: FilterData | undefined +): NumericFilter | undefined => { + if (!filter) { + return undefined; + } + if (featureDef.discrete && filter.discrete) { + // For discrete features, values are marked as excluded based on the + // string key, but the actual feature data is stored as an integer. Map + // the excluded string values to their corresponding integer value. + const featureOptions = { ...featureDef.options }; + const optionValues: (number | null)[] = Object.keys(featureOptions).map((key) => + Number.parseInt(key) + ); + const optionStringKey = Object.values(featureOptions).map( + (option) => option.key ?? option.name + ); + + // In the data, missing values are represented as `null`. If the user is + // filtering out missing values (MISSING_CATEGORY_KEY), + // `excludedIndices` will include `null`. + optionStringKey.push(MISSING_CATEGORY_KEY); + optionValues.push(null); + + const excludedIndices = new Set(); + for (let i = 0; i < optionValues.length; i++) { + if (filter.excludedValues.has(optionStringKey[i])) { + excludedIndices.add(optionValues[i]); + } + } + return { + featureKey: featureDef.key, + excludedIndices: excludedIndices, + }; + } else if (!featureDef.discrete && !filter.discrete) { + return { + featureKey: featureDef.key, + range: filter.range, + }; + } else { + console.error( + `Feature '${featureDef.displayName}' (key: '${featureDef.key}') type and filter type are mismatched and will be skipped.`, + featureDef, + filter + ); + return undefined; + } +}; + +/** + * Returns whether the specified cell index is excluded by the provided filters, + * using the current feature data. + * @param id The cell index to check for exclusion. + * @param cellData The current feature data for all cells. + * @param numericFilters The set of numeric filters to apply. + * @returns whether the cell is excluded by the provided filters. + */ +export function isIdExcludedByFilters( + id: number, + cellData: DataForPlot, + numericFilters: Map +): boolean { + let isExcluded = false; + for (const [featureKey, numericFilter] of numericFilters) { + const value = cellData.values[featureKey][id] ?? null; + if (numericFilter) { + if (numericFilter.range) { + if (value === null) { + isExcluded = true; + break; + } else if (value < numericFilter.range[0] || value > numericFilter.range[1]) { + isExcluded = true; + break; + } + } else if (numericFilter.excludedIndices) { + if (numericFilter.excludedIndices.has(value)) { + isExcluded = true; + break; + } + } + } + } + return isExcluded; +}