-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* apply linear binning closes #8 * a test for contourDensity & linear binning * style Co-authored-by: Mike Bostock <mbostock@gmail.com>
- Loading branch information
Showing
5 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assert from "assert"; | ||
|
||
export function assertInDelta(actual, expected, delta = 1e-6) { | ||
assert(inDelta(actual, expected, delta)); | ||
} | ||
|
||
function inDelta(actual, expected, delta) { | ||
return (Array.isArray(expected) ? inDeltaArray : inDeltaNumber)(actual, expected, delta); | ||
} | ||
|
||
function inDeltaArray(actual, expected, delta) { | ||
const n = expected.length; | ||
if (actual.length !== n) return false; | ||
for (let i = 0; i < n; ++i) if (!inDelta(actual[i], expected[i], delta)) return false; | ||
return true; | ||
} | ||
|
||
function inDeltaNumber(actual, expected, delta) { | ||
return actual >= expected - delta && actual <= expected + delta; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import assert from "assert"; | ||
import {polygonCentroid} from "d3-polygon"; | ||
import {contourDensity} from "../src/index.js"; | ||
import {assertInDelta} from "./asserts.js"; | ||
|
||
it("density.size(…) validates the specified size", () => { | ||
assert.deepStrictEqual(contourDensity().size([1, 2]).size(), [1, 2]); | ||
assert.deepStrictEqual(contourDensity().size([0, 0]).size(), [0, 0]); | ||
assert.deepStrictEqual(contourDensity().size([1.5, 2.5]).size(), [1.5, 2.5]); | ||
assert.throws(() => void contourDensity().size([0, -1]), /invalid size/); | ||
}); | ||
|
||
it("contourDensity(data) returns the expected result for empty data", () => { | ||
const c = contourDensity(); | ||
assert.deepStrictEqual(c([]), []); | ||
}); | ||
|
||
it("contourDensity(data) returns contours centered on a point", () => { | ||
const c = contourDensity().thresholds([0.0001, 0.001]); | ||
for (const p of [[100, 100], [100.5, 102]]) { | ||
const contour = c([p]); | ||
assert.strictEqual(contour.length, 2); | ||
for (const b of contour) { | ||
const a = polygonCentroid(b.coordinates[0][0]); | ||
assertInDelta(a[0], p[0], 0.1); | ||
assertInDelta(a[1], p[1], 0.1); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters