Skip to content

Commit

Permalink
enhance: with MissingDataStrategy.auto, drop entities with single n…
Browse files Browse the repository at this point in the history
…on-error data point
  • Loading branch information
marcelgerber committed Aug 15, 2024
1 parent 2e42646 commit e2ef106
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
14 changes: 14 additions & 0 deletions packages/@ourworldindata/core-table/src/OwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
)
}

dropRowsWithAtLeastThisManyErrorValuesForColumns(
slugs: ColumnSlug[],
minErrorValues: number
): this {
return this.rowFilter(
(row) =>
slugs.filter((slug) => !isNotErrorValue(row[slug])).length >=
minErrorValues,
`Drop rows with at least ${minErrorValues} ErrorValues in every column: ${slugs.join(
", "
)}`
)
}

// Drop _all rows_ for an entity if there is any column that has no valid values for that entity.
dropEntitiesThatHaveNoDataInSomeColumn(columnSlugs: ColumnSlug[]): this {
const indexesByEntityName = this.rowIndicesByEntityName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ export class StackedDiscreteBarChart
{
base: React.RefObject<SVGGElement> = React.createRef()

private applyMissingDataStrategy(table: OwidTable): OwidTable {
if (this.missingDataStrategy === MissingDataStrategy.hide) {
// If MissingDataStrategy is explicitly set to hide, drop rows (= times) where one of
// the y columns has no data
return table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs)
} else if (this.missingDataStrategy === MissingDataStrategy.auto) {
// If MissingDataStrategy is set to auto, drop rows where there is only a single non-error value
if (this.yColumnSlugs.length > 1) {
return table.dropRowsWithAtLeastThisManyErrorValuesForColumns(
this.yColumnSlugs,
this.yColumnSlugs.length - 1
)
}
}
return table
}

transformTable(table: OwidTable): OwidTable {
if (!this.yColumnSlugs.length) return table

Expand All @@ -144,11 +161,7 @@ export class StackedDiscreteBarChart
table = table.interpolateColumnWithTolerance(slug)
})

// If MissingDataStrategy is explicitly set to hide, drop rows (= times) where one of
// the y columns has no data
if (this.missingDataStrategy === MissingDataStrategy.hide) {
table = table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs)
}
table = this.applyMissingDataStrategy(table)

if (this.manager.isRelativeMode) {
table = table.toPercentageFromEachColumnForEachEntityAndTime(
Expand All @@ -165,9 +178,7 @@ export class StackedDiscreteBarChart
.replaceNegativeCellsWithErrorValues(this.yColumnSlugs)
.dropRowsWithErrorValuesForAllColumns(this.yColumnSlugs)

if (this.missingDataStrategy === MissingDataStrategy.hide) {
table = table.dropRowsWithErrorValuesForAnyColumn(this.yColumnSlugs)
}
table = this.applyMissingDataStrategy(table)

return table
}
Expand Down

0 comments on commit e2ef106

Please sign in to comment.