Skip to content

Commit

Permalink
feat: Add density 1d max normalize option.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Nov 27, 2024
1 parent 3cfb158 commit 0952091
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 9 additions & 2 deletions packages/plot/src/marks/Density1DMark.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toDataColumns } from '@uwdata/mosaic-core';
import { binLinear1d, isBetween } from '@uwdata/mosaic-sql';
import { sum } from 'd3';
import { max, sum } from 'd3';
import { Transient } from '../symbols.js';
import { binExpr } from './util/bin-expr.js';
import { dericheConfig, dericheConv1d } from './util/density.js';
Expand Down Expand Up @@ -78,7 +78,7 @@ export class Density1DMark extends Mark {
const b = this.channelField(dim).as;
const b0 = +lo;
const delta = (hi - b0) / (bins - 1);
const scale = 1 / (delta * (normalize ? sum(grid) : 1));
const scale = 1 / (delta * norm(grid, result, normalize));

const _b = new Float64Array(bins);
const _v = new Float64Array(bins);
Expand All @@ -100,3 +100,10 @@ export class Density1DMark extends Mark {
return [{ type, data: { length }, options }];
}
}

function norm(grid, smoothed, type) {
const value = type === true || type === 'sum' ? sum(grid)
: type === 'max' ? max(smoothed)
: 1;
return value || 1;
}
8 changes: 5 additions & 3 deletions packages/spec/src/spec/marks/Density.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export interface Density1DOptions {
bins?: number | ParamRef;

/**
* Flag indicating if density estimates should be normalized by dividing
* by the total point mass. Defaults to false.
* Normalization method for density estimates. If `false` or `'none'` (the
* default), the density estimates are smoothed weighted counts. If `true`
* or `'sum'`, density estimates are divided by the sum of the total point
* mass. If `'max'`, estimates are divided by the maximum smoothed value.
*/
normalize?: boolean | ParamRef;
normalize?: boolean | 'max' | 'sum' | 'none' | ParamRef;
}

export interface DensityAreaXOptions extends Omit<AreaXOptions, 'x' | 'x1' | 'x2'> {
Expand Down

0 comments on commit 0952091

Please sign in to comment.