Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fractional bandwidth, using d3.blur #60

Merged
merged 3 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"sideEffects": false,
"dependencies": {
"d3-array": "2 - 3"
"d3-array": "^3.2.0"
},
"devDependencies": {
"d3-axis": "3",
Expand Down
43 changes: 0 additions & 43 deletions src/blur.js

This file was deleted.

36 changes: 14 additions & 22 deletions src/density.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {max, ticks} from "d3-array";
import {blur2, max, ticks} from "d3-array";
import {slice} from "./array.js";
import {blurX, blurY} from "./blur.js";
import constant from "./constant.js";
import Contours from "./contours.js";

Expand Down Expand Up @@ -30,35 +29,28 @@ export default function() {
threshold = constant(20);

function grid(data) {
var values0 = new Float32Array(n * m),
values1 = new Float32Array(n * m),
pow2k = Math.pow(2, -k);
var values = new Float32Array(n * m),
pow2k = Math.pow(2, -k),
i = -1;

data.forEach(function(d, i, data) {
var xi = (x(d, i, data) + o) * pow2k,
for (const d of data) {
var xi = (x(d, ++i, data) + o) * pow2k,
yi = (y(d, i, data) + o) * pow2k,
wi = +weight(d, i, data);
if (xi >= 0 && xi < n && yi >= 0 && yi < m) {
var x0 = Math.floor(xi),
y0 = Math.floor(yi),
xt = xi - x0 - 0.5,
yt = yi - y0 - 0.5;
values0[x0 + y0 * n] += (1 - xt) * (1 - yt) * wi;
values0[x0 + 1 + y0 * n] += xt * (1 - yt) * wi;
values0[x0 + 1 + (y0 + 1) * n] += xt * yt * wi;
values0[x0 + (y0 + 1) * n] += (1 - xt) * yt * wi;
values[x0 + y0 * n] += (1 - xt) * (1 - yt) * wi;
values[x0 + 1 + y0 * n] += xt * (1 - yt) * wi;
values[x0 + 1 + (y0 + 1) * n] += xt * yt * wi;
values[x0 + (y0 + 1) * n] += (1 - xt) * yt * wi;
}
});

// TODO Optimize.
blurX({width: n, height: m, data: values0}, {width: n, height: m, data: values1}, r >> k);
blurY({width: n, height: m, data: values1}, {width: n, height: m, data: values0}, r >> k);
blurX({width: n, height: m, data: values0}, {width: n, height: m, data: values1}, r >> k);
blurY({width: n, height: m, data: values1}, {width: n, height: m, data: values0}, r >> k);
blurX({width: n, height: m, data: values0}, {width: n, height: m, data: values1}, r >> k);
blurY({width: n, height: m, data: values1}, {width: n, height: m, data: values0}, r >> k);
}

return values0;
blur2({data: values, width: n, height: m}, r * pow2k);
return values;
}

function density(data) {
Expand Down Expand Up @@ -150,7 +142,7 @@ export default function() {
density.bandwidth = function(_) {
if (!arguments.length) return Math.sqrt(r * (r + 1));
if (!((_ = +_) >= 0)) throw new Error("invalid bandwidth");
return r = Math.round((Math.sqrt(4 * _ * _ + 1) - 1) / 2), resize();
return r = (Math.sqrt(4 * _ * _ + 1) - 1) / 2, resize();
};

return density;
Expand Down
2 changes: 1 addition & 1 deletion test/density-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ it("contourDensity(data) returns nice default thresholds", async () => {
.bandwidth(30)
(faithful);

assert.deepStrictEqual(contour.map(c => c.value), ticks(0.0002, 0.006, 30));
assert.deepStrictEqual(contour.map(c => c.value), ticks(0.0002, 0.0059, 30));
});

it("contourDensity.contours(data) preserves the specified threshold exactly", async () => {
Expand Down
59 changes: 29 additions & 30 deletions test/output/faithfulContour.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 29 additions & 30 deletions test/output/faithfulContours.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ cssstyle@^2.3.0:
dependencies:
cssom "~0.3.6"

"d3-array@2 - 3", "[email protected] - 3", "[email protected] - 3":
version "3.1.6"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-3.1.6.tgz#0342c835925826f49b4d16eb7027aec334ffc97d"
integrity sha512-DCbBBNuKOeiR9h04ySRBMW52TFVc91O9wJziuyXw6Ztmy8D3oZbmCkOO3UHKC7ceNJsN2Mavo9+vwV8EAEUXzA==
"d3-array@2 - 3", "[email protected] - 3", "[email protected] - 3", d3-array@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-3.2.0.tgz#15bf96cd9b7333e02eb8de8053d78962eafcff14"
integrity sha512-3yXFQo0oG3QCxbF06rMPFyGRMGJNS7NvsV1+2joOjbBE+9xvWQ8+GcMJAjRCzw06zQ3/arXeJgbPYcjUCuC+3g==
dependencies:
internmap "1 - 2"

Expand Down