Skip to content

Commit

Permalink
Fix small number handling with threshold and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BielStela committed Feb 5, 2024
1 parent 7029db7 commit f42cbd8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 8 additions & 5 deletions api/src/modules/h3-data/h3-data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ export class H3DataRepository extends Repository<H3Data> {
// If map type is Impact Map, sum up values from previous subquery
if (mapType === IMPACT_MAP_TYPE.IMPACT_MAP) {
impactViewAggregationQueryBuilder.addSelect(
'round(sum(impactview.value * reduced.scaled_value)::numeric, 4)',
'sum(impactview.value * reduced.scaled_value)::numeric',
'v',
);
}
Expand All @@ -766,19 +766,21 @@ export class H3DataRepository extends Repository<H3Data> {
*/
private async calculateQuantiles(tmpTableName: string): Promise<number[]> {
const N_BINS: number = 7;
// TODO: make threshold configurable by unit
const DISPLAY_THRESHOLD: number = 0.01;
try {
//
const max: any[] = await this.dataSource.query(
const max_q: any[] = await this.dataSource.query(
`select percentile_cont(0.99) WITHIN GROUP (ORDER BY v) as value from "${tmpTableName}"; `,
);
// DISPLAY_THRESHOLD is the threshold for the smallest value to be displayed in the map and legend
const max = Math.max(max_q[0]["value"], DISPLAY_THRESHOLD)
let bins: number[] = [];
for (let i = 1; i <= N_BINS; i++) {

// Log scale binning value shifted with + 1
// to avoid values < 1, the zone where log behaves badly (rush to -inf for ->0).
// Subtract 1 to undo the shift in the final value.
let bin: number = Math.pow(10, (Math.log10(max[0]['value'] + 1 ) * i / N_BINS )) - 1;

let bin: number = Math.pow(10, (Math.log10(max + 1 ) * i / N_BINS )) - 1;
// Round small values with 2 significant digits, and bigger ones with just one.
// >=10 values look like: 0 50 400 3k 20k 1M...
// <10: 0.017 0.035 0.11...
Expand All @@ -796,6 +798,7 @@ export class H3DataRepository extends Repository<H3Data> {

/**
* This quantile calculation is meant to be used only for comparison maps
* TODO: Refactor this to use log scale (same approach as above in calculateQuantiles)
*/
private async calculateScenarioComparisonQuantiles(
tmpTableName: string,
Expand Down
11 changes: 7 additions & 4 deletions client/src/utils/number-format.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { format } from 'd3-format';

// for numbers bigger than 1
export const NUMBER_FORMAT = format('.3~s');
export const NUMBER_FORMAT = format('.2~s');

// for numbers smaller than 1
export const SMALL_NUMBER_FORMAT = format('.3~g');
export const SMALL_NUMBER_FORMAT = format('.2~g');

export function formatNumber(number: number): string {
if (number < 1) {
return SMALL_NUMBER_FORMAT(number);
if (Math.abs(number) < 1) {
if (Math.abs(number) < 0.001) {
return "< 0.001";
}
return SMALL_NUMBER_FORMAT(number)
}
return NUMBER_FORMAT(number);
}
Expand Down

0 comments on commit f42cbd8

Please sign in to comment.