Skip to content

Commit

Permalink
Update visualizations to count distinct people
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Jennison committed Dec 5, 2024
1 parent 7696caf commit 5ebcd7a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 57 deletions.
10 changes: 1 addition & 9 deletions ui/src/cohortReview/newReviewDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@ export function NewReviewDialog(props: NewReviewDialogProps) {
cohort: props.cohort,
},
async () =>
(
await studySource.cohortCount(
studyId,
props.cohort.id,
undefined,
undefined,
[]
)
)?.[0]?.count ?? 0
(await studySource.cohortCount(studyId, props.cohort.id))?.[0]?.count ?? 0
);

const cohortCount = countState.data;
Expand Down
30 changes: 17 additions & 13 deletions ui/src/data/source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export type FilterCountValue = {
[x: string]: DataValue;
};

export type CohortCountOptions = {
groupSectionId?: string;
groupId?: string;
groupByAttributes?: string[];
entity?: string;
countDistinctAttribute?: string;
};

export type PropertyMap = {
[key: string]: string;
};
Expand Down Expand Up @@ -431,10 +439,7 @@ export interface StudySource {
cohortCount(
studyId: string,
cohortId: string,
groupSectionId?: string,
groupId?: string,
groupByAttributes?: string[],
entity?: string
options?: CohortCountOptions
): Promise<FilterCountValue[]>;

getFeatureSetMetadata(
Expand Down Expand Up @@ -1344,10 +1349,7 @@ export class BackendStudySource implements StudySource {
async cohortCount(
studyId: string,
cohortId: string,
groupSectionId?: string,
groupId?: string,
groupByAttributes?: string[],
entity?: string
options?: CohortCountOptions
): Promise<FilterCountValue[]> {
let pageMarker: string | undefined;
const instanceCounts: tanagra.InstanceCount[] = [];
Expand All @@ -1358,13 +1360,15 @@ export class BackendStudySource implements StudySource {
studyId,
cohortId,
cohortCountQuery: {
countDistinctAttribute: undefined,
countDistinctAttribute: options?.countDistinctAttribute,
groupByAttributes:
groupByAttributes == null ? [] : groupByAttributes,
criteriaGroupSectionId: groupSectionId,
criteriaGroupId: groupId,
options?.groupByAttributes == null
? []
: options?.groupByAttributes,
criteriaGroupSectionId: options?.groupSectionId,
criteriaGroupId: options?.groupId,
pageMarker,
entity,
entity: options?.entity,
limit: 1000000,
},
})
Expand Down
26 changes: 8 additions & 18 deletions ui/src/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,7 @@ function GroupList() {
const studySource = useStudySource();

const fetchCount = useCallback(async () => {
return (
(
await studySource.cohortCount(
studyId,
cohort.id,
undefined,
undefined,
[]
)
)?.[0]?.count ?? 0
);
return (await studySource.cohortCount(studyId, cohort.id))?.[0]?.count ?? 0;
}, [studyId, cohort]);

const countState = useSWRImmutable(
Expand Down Expand Up @@ -292,7 +282,9 @@ function ParticipantsGroupSection(props: {
}

return (
await studySource.cohortCount(studyId, cohort.id, backendGroupSection.id)
await studySource.cohortCount(studyId, cohort.id, {
groupSectionId: backendGroupSection.id,
})
)[0].count;
}, [studyId, cohort.id, cohort.underlayName, backendGroupSection]);

Expand Down Expand Up @@ -764,12 +756,10 @@ function ParticipantsGroup(props: {
}

return (
await studySource.cohortCount(
studyId,
cohort.id,
backendGroupSection.id,
backendGroup.id
)
await studySource.cohortCount(studyId, cohort.id, {
groupSectionId: backendGroupSection.id,
groupId: backendGroup.id,
})
)[0].count;
}, [
studyId,
Expand Down
17 changes: 7 additions & 10 deletions ui/src/viz/vizContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,18 @@ async function fetchVizData(
`Visualizations of ${vizSource.criteriaSelector} are not supported.`
);
}
if (vizSource.joins?.length) {
throw new Error("Joins are unsupported.");
if (vizSource.joins?.find((j) => j.entity !== "person" || j.aggregation)) {
throw new Error("Only unique joins to person are supported.");
}
if (!vizSource.attributes?.length || vizSource.attributes.length > 2) {
throw new Error("Only 1 or 2 attributes are supported.");
}

const fcvs = await studySource.cohortCount(
studyId,
cohort.id,
undefined,
undefined,
vizSource.attributes.map((a) => a.attribute),
entity
);
const fcvs = await studySource.cohortCount(studyId, cohort.id, {
groupByAttributes: vizSource.attributes.map((a) => a.attribute),
entity,
countDistinctAttribute: vizSource.joins?.length ? "person_id" : undefined,
});

return processFilterCountValues(vizConfig, fcvs);
}
8 changes: 5 additions & 3 deletions underlay/src/main/proto/viz/viz_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ message VizConfig {

message Aggregation {
enum AggregationType {
UNKNOWN = 0;
UNIQUE = 0;
MIN = 1;
MAX = 2;
AVERAGE = 3;
}

// The type of aggregation being performed.
AggregationType type = 1;
optional AggregationType type = 1;

// The output is always ids and values but aggregation may occur over
// another field (e.g. date to find the most recent value).
Expand All @@ -45,7 +45,9 @@ message VizConfig {
// values to a person), an aggregation is often required to make the data
// visualizable. For example, to visualize weight vs. race, each person
// needs to have a single weight value associated with them, such as the
// average or most recent.
// average or most recent. For simple cases, simply counting unique
// instances of a related entity may be sufficient (e.g. to count people
// with related condition occurrences).
optional Aggregation aggregation = 2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"sources": [
{
"criteriaSelector": "conditions",
"joins": [],
"joins": [
{
"entity": "person"
}
],
"attributes": [
{
"attribute": "condition",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"sources": [
{
"criteriaSelector": "ingredients",
"joins": [],
"joins": [
{
"entity": "person"
}
],
"attributes": [
{
"attribute": "ingredient",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"sources": [
{
"criteriaSelector": "measurements",
"joins": [],
"joins": [
{
"entity": "person"
}
],
"attributes": [
{
"attribute": "measurement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"sources": [
{
"criteriaSelector": "procedures",
"joins": [],
"joins": [
{
"entity": "person"
}
],
"attributes": [
{
"attribute": "procedure",
Expand Down

0 comments on commit 5ebcd7a

Please sign in to comment.