Skip to content

Commit

Permalink
fix multifilter when there is no filter applied to the variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bobular committed Nov 19, 2024
1 parent c29fde8 commit deff652
Showing 1 changed file with 71 additions and 74 deletions.
145 changes: 71 additions & 74 deletions packages/libs/eda/src/lib/core/components/filter/MultiFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,81 +193,78 @@ export function MultiFilter(props: Props) {
}, [_thisFilter, thisFilter]);

// Counts retrieved from the backend, used for the table display.
const leafSummariesPromise = useCachedPromise(
() => {
return Promise.all(
leaves.map((leaf) => {
const thisFilterWithoutLeaf = thisFilter && {
...thisFilter,
subFilters: thisFilter.subFilters.filter(
(f) => f.variableId !== leaf.term
),
};
return getDistribution(
{
entityId: entity.id,
variableId: leaf.term,
filters:
thisFilterWithoutLeaf == null ||
thisFilterWithoutLeaf.subFilters.length === 0 ||
thisFilterWithoutLeaf.operation === 'union'
? otherFilters
: [...(otherFilters || []), thisFilterWithoutLeaf],
},
(filters) =>
subsettingClient.getDistribution(
studyMetadata.id,
entity.id,
leaf.term,
{
filters,
valueSpec: 'count',
}
)
).then((distribution) => {
const fgValueByLabel = Object.fromEntries(
distribution.foreground.histogram.map(({ binLabel, value }) => [
binLabel,
value ?? 0,
])
);
const bgValueByLabel = Object.fromEntries(
distribution.background.histogram.map(({ binLabel, value }) => [
binLabel,
value ?? 0,
])
const leafSummariesPromise = useCachedPromise(() => {
return Promise.all(
leaves.map((leaf) => {
const thisFilterWithoutLeaf = thisFilter && {
...thisFilter,
subFilters: thisFilter.subFilters.filter(
(f) => f.variableId !== leaf.term
),
};
return getDistribution(
{
entityId: entity.id,
variableId: leaf.term,
filters:
thisFilterWithoutLeaf == null ||
thisFilterWithoutLeaf.subFilters.length === 0 ||
thisFilterWithoutLeaf.operation === 'union'
? otherFilters
: [...(otherFilters || []), thisFilterWithoutLeaf],
},
(filters) =>
subsettingClient.getDistribution(
studyMetadata.id,
entity.id,
leaf.term,
{
filters,
valueSpec: 'count',
}
)
).then((distribution) => {
const fgValueByLabel = Object.fromEntries(
distribution.foreground.histogram.map(({ binLabel, value }) => [
binLabel,
value ?? 0,
])
);
const bgValueByLabel = Object.fromEntries(
distribution.background.histogram.map(({ binLabel, value }) => [
binLabel,
value ?? 0,
])
);
const variable = variablesById[leaf.term];
if (variable == null || !isTableVariable(variable))
throw new Error(
`Could not find a categorical EDA variable associated with the leaf field "${leaf.term}".`
);
const variable = variablesById[leaf.term];
if (variable == null || !isTableVariable(variable))
throw new Error(
`Could not find a categorical EDA variable associated with the leaf field "${leaf.term}".`
);
return {
term: leaf.term,
display: leaf.display,
valueCounts: variable.vocabulary?.map((label) => ({
value: label,
count: bgValueByLabel[label],
filteredCount: fgValueByLabel[label] ?? 0,
})),
internalsCount:
distribution.background.statistics.numDistinctEntityRecords,
internalsFilteredCount:
distribution.foreground.statistics.numDistinctEntityRecords,
};
});
})
);
},
[
thisFilter,
otherFilters,
leaves,
entity.id,
studyMetadata.id,
variablesById,
] // used to have `subsettingClient`
);
return {
term: leaf.term,
display: leaf.display,
valueCounts: variable.vocabulary?.map((label) => ({
value: label,
count: bgValueByLabel[label],
filteredCount: fgValueByLabel[label] ?? 0,
})),
internalsCount:
distribution.background.statistics.numDistinctEntityRecords,
internalsFilteredCount:
distribution.foreground.statistics.numDistinctEntityRecords,
};
});
})
);
}, [
thisFilter ? thisFilter : { type: 'NO_FILTER' },
otherFilters,
leaves,
entity.id,
studyMetadata.id,
variablesById,
]);

// Sorted counts. This is done separately from retrieving the data so that
// updates to sorting don't incur backend requests.
Expand Down

0 comments on commit deff652

Please sign in to comment.