Skip to content
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
39 changes: 39 additions & 0 deletions e2e/adult-map-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test } from '@playwright/test';

for (const rejected of [false, true]) {
test(`adult GPS notices remain visible with no usable points (rejected=${rejected})`, async ({
page
}) => {
await page.route('**/adults/__data.json*', async (route) => {
const response = await route.fetch();
const body = await response.json();
// SvelteKit's flattened data uses array indices as references. Append synthetic
// values so shared numbers in the original response are not changed in place.
const node = body.nodes.find(
(n: { data?: unknown[] } | null) => n?.data?.[0] && 'where' in (n.data[0] as object)
);
const data: unknown[] = node.data;
const root = data[0] as Record<string, number>;
const where = data[root.where] as Record<string, number>;
const add = (v: unknown) => data.push(v) - 1;
root.n = add(3);
where.points = add([]);
where.density = add(null);
where.missing = add(rejected ? 2 : 3);
const exclusion = add({ label: add('Synthetic adult'), reason: add('on land') });
where.setAside = add(rejected ? [exclusion] : []);
await route.fulfill({ response, json: body });
});
await page.goto('/');
await page
.locator('a[href="/adults"]')
.first()
.evaluate((a: HTMLAnchorElement) => a.click());
await expect(page.getByText('No usable GPS points are available for this map.')).toBeVisible();
await expect(
page.getByText(`${rejected ? 2 : 3} captures have no GPS point.`, { exact: false })
).toBeVisible();
if (rejected)
await expect(page.getByText('1 point left out (on land).', { exact: false })).toBeVisible();
});
}
63 changes: 59 additions & 4 deletions src/lib/components/InteractiveMap.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<script lang="ts" module>
import type { MapLine, MapPoint } from './MooreaMap.svelte';

/** A density surface as grid cells, coloured on the sequential scale by `bin` (1–5). */
export interface MapDensity {
name: string;
cells: { south: number; west: number; north: number; east: number; bin: number }[];
}

export interface MapLineLayer {
name: string;
/** CSS colour, applied through CSS so it follows the theme. */
Expand Down Expand Up @@ -52,6 +58,7 @@
layers,
lineLayers = [],
lineLabel = '',
density,
lineMax: fixedLineMax,
traveller,
label,
Expand All @@ -65,6 +72,8 @@
/** Lines drawn under the circles, width by value, one toggle per layer. */
lineLayers?: MapLineLayer[];
lineLabel?: string;
/** Drawn under everything else, with its own toggle. */
density?: MapDensity;
/** Value that gets the widest line; fix it so widths don't jump as lines are added. */
lineMax?: number;
/** A dot that glides from `from` to `to` whenever `key` changes (a Play button). */
Expand Down Expand Up @@ -207,6 +216,7 @@
const L = leaflet;
const current = layers;
const currentLines = lineLayers;
const currentDensity = density;
if (!m || !L || !el) return;
const hidden = new Set(
drawn.flatMap((d) => d.groups.filter((g) => !m.hasLayer(g.group)).map((g) => g.key))
Expand All @@ -219,6 +229,25 @@
const lineMax =
fixedLineMax ??
Math.max(1e-9, ...currentLines.flatMap((g) => g.lines.map((l) => l.value ?? 1)));
const densityGroups = currentDensity
? [
(() => {
const group = L.layerGroup();
for (const c of currentDensity.cells)
L.rectangle(
[
[c.south, c.west],
[c.north, c.east]
],
{ stroke: false, interactive: false, className: `ps-dens ps-dens-${c.bin}` }
).addTo(group);
const key = 'density';
if (!hidden.has(key)) group.addTo(m);
overlays[`<span class="ps-key ps-dens-key"></span>${currentDensity.name}`] = group;
return { key, group };
})()
]
: [];
const lineGroups = currentLines.map((g, i) => {
const group = L.layerGroup();
for (const l of g.lines)
Expand Down Expand Up @@ -270,11 +299,15 @@
return { key, group };
});
const control = L.control
.layers(bases, current.length + currentLines.length > 1 ? overlays : {}, {
collapsed: el.clientWidth < 560
})
.layers(
bases,
current.length + currentLines.length + densityGroups.length > 1 ? overlays : {},
{
collapsed: el.clientWidth < 560
}
)
.addTo(m);
drawn = [{ groups: [...lineGroups, ...groups], control }];
drawn = [{ groups: [...densityGroups, ...lineGroups, ...groups], control }];
});

// Glide a dot along the traveller's line; reduced motion (#32) just places it at the end.
Expand Down Expand Up @@ -394,6 +427,28 @@
font-weight: 650;
color: #1d5fb8;
}
.map :global(.ps-dens) {
fill-opacity: 0.6;
}
.map :global(.ps-dens-1) {
fill: var(--seq-2);
}
.map :global(.ps-dens-2) {
fill: var(--seq-3);
}
.map :global(.ps-dens-3) {
fill: var(--seq-4);
}
.map :global(.ps-dens-4) {
fill: var(--seq-5);
}
.map :global(.ps-dens-5) {
fill: var(--seq-6);
}
.map :global(.ps-dens-key) {
border-radius: 2px;
background: linear-gradient(90deg, var(--seq-2), var(--seq-6));
}
.map :global(.ps-traveller) {
stroke: #fff;
stroke-width: 2px;
Expand Down
57 changes: 57 additions & 0 deletions src/lib/server/analytics/density.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest';
import { BINS, MIN_POINTS, bandwidth, kde, kmFromCoast, onLand } from './density';

/** Deterministic pseudo-random normal draws (Box–Muller on a small LCG). */
function normals(n: number, seed = 7): number[] {
let s = seed;
const u = () => ((s = (s * 1103515245 + 12345) % 2 ** 31) + 1) / (2 ** 31 + 1);
return Array.from(
{ length: n },
() => Math.sqrt(-2 * Math.log(u())) * Math.cos(2 * Math.PI * u())
);
}

describe('kde', () => {
it('needs enough points', () => {
const pts = Array.from({ length: MIN_POINTS - 1 }, () => ({ lat: -17.5, lon: -149.8 }));
expect(kde(pts)).toBeNull();
});

it('peaks where the points cluster', () => {
const z = normals(80);
// 60 points tight around A, 20 spread around B 5 km east.
const pts = [
...z.slice(0, 60).map((v, i) => ({
lat: -17.5 + (v * 0.2) / 110.574,
lon: -149.85 + (z[(i + 7) % 80] * 0.2) / 106
})),
...z.slice(60).map((v, i) => ({
lat: -17.5 + v / 110.574,
lon: -149.85 + 5 / 106 + z[(i + 3) % 80] / 106
}))
];
const d = kde(pts)!;
expect(d.n).toBe(80);
const peak = d.cells.filter((c) => c.bin === BINS);
expect(peak.length).toBeGreaterThan(0);
for (const c of peak) expect(Math.abs((c.west + c.east) / 2 - -149.85)).toBeLessThan(0.02);
for (const c of d.cells) {
expect(c.north).toBeGreaterThan(c.south);
expect(c.east).toBeGreaterThan(c.west);
}
});

it('applies the bandwidth floor to coincident points', () => {
expect(bandwidth([0, 0, 0, 0], [0, 0, 0, 0])).toBe(0.3);
});
});

describe('coastline checks', () => {
it('puts the middle of the island on land and open ocean far from shore', () => {
const middle = { lat: -17.54, lon: -149.83 };
expect(onLand(middle)).toBe(true);
const ocean = { lat: -17.3, lon: -149.83 };
expect(onLand(ocean)).toBe(false);
expect(kmFromCoast(ocean)).toBeGreaterThan(15);
});
});
131 changes: 131 additions & 0 deletions src/lib/server/analytics/density.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Kernel density of capture locations (#174), on a local flat projection in km.
*
* Gaussian kernel, one bandwidth for both axes (Silverman's rule on the pooled spread, with a
* floor so a tight cluster still draws as an area). The grid covers the points plus three
* bandwidths; cells under 5% of the peak are dropped and the rest binned into `BINS` classes.
*/
import outline from '#lib/moorea-outline.json';

export const BINS = 5;
export const MIN_POINTS = 20;
const FLOOR_KM = 0.3;
const CELLS = 60;
const CUTOFF = 0.05;

export interface LatLon {
lat: number;
lon: number;
}

export interface DensityCell {
south: number;
west: number;
north: number;
east: number;
/** 1 (lowest) … BINS (peak). */
bin: number;
}

export interface Density {
n: number;
bandwidthKm: number;
cells: DensityCell[];
}

const KM_LAT = 110.574;
const kmLon = (lat: number) => 111.32 * Math.cos((lat * Math.PI) / 180);

function sd(xs: number[]): number {
const m = xs.reduce((a, b) => a + b, 0) / xs.length;
return Math.sqrt(xs.reduce((a, b) => a + (b - m) ** 2, 0) / Math.max(1, xs.length - 1));
}

function iqr(xs: number[]): number {
const s = [...xs].sort((a, b) => a - b);
const q = (p: number) => {
const i = (s.length - 1) * p;
const lo = Math.floor(i);
return s[lo] + (s[Math.ceil(i)] - s[lo]) * (i - lo);
};
return q(0.75) - q(0.25);
}

/** Silverman's rule of thumb, averaged over the two axes, in km. */
export function bandwidth(xs: number[], ys: number[]): number {
const n = xs.length;
const axis = (v: number[]) => {
const spread = Math.min(sd(v), iqr(v) / 1.34 || sd(v));
return 1.06 * spread * n ** -0.2;
};
return Math.max(FLOOR_KM, (axis(xs) + axis(ys)) / 2);
}

export function kde(points: LatLon[]): Density | null {
if (points.length < MIN_POINTS) return null;
const lat0 = points.reduce((a, p) => a + p.lat, 0) / points.length;
const lon0 = points.reduce((a, p) => a + p.lon, 0) / points.length;
const kx = kmLon(lat0);
const xs = points.map((p) => (p.lon - lon0) * kx);
const ys = points.map((p) => (p.lat - lat0) * KM_LAT);
const h = bandwidth(xs, ys);
const pad = 3 * h;
const x0 = Math.min(...xs) - pad;
const x1 = Math.max(...xs) + pad;
const y0 = Math.min(...ys) - pad;
const y1 = Math.max(...ys) + pad;
const step = Math.max(x1 - x0, y1 - y0) / CELLS;
const nx = Math.ceil((x1 - x0) / step);
const ny = Math.ceil((y1 - y0) / step);
const grid: number[] = [];
let max = 0;
for (let j = 0; j < ny; j++)
for (let i = 0; i < nx; i++) {
const cx = x0 + (i + 0.5) * step;
const cy = y0 + (j + 0.5) * step;
let v = 0;
for (let k = 0; k < xs.length; k++)
v += Math.exp(-((cx - xs[k]) ** 2 + (cy - ys[k]) ** 2) / (2 * h * h));
grid.push(v);
if (v > max) max = v;
}
const cells: DensityCell[] = [];
for (let j = 0; j < ny; j++)
for (let i = 0; i < nx; i++) {
const share = grid[j * nx + i] / max;
if (share < CUTOFF) continue;
const west = lon0 + (x0 + i * step) / kx;
const south = lat0 + (y0 + j * step) / KM_LAT;
cells.push({
south,
west,
north: south + step / KM_LAT,
east: west + step / kx,
bin: Math.min(BINS, Math.max(1, Math.ceil(share * BINS)))
});
}
return { n: points.length, bandwidthKm: h, cells };
}

const ring = outline.ring as [number, number][];

/** Point-in-polygon against Moorea's coastline (ray casting). */
export function onLand(p: LatLon): boolean {
let inside = false;
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const [xi, yi] = ring[i];
const [xj, yj] = ring[j];
if (yi > p.lat !== yj > p.lat && p.lon < ((xj - xi) * (p.lat - yi)) / (yj - yi) + xi)
inside = !inside;
}
return inside;
}

/** Distance in km to the nearest coastline vertex. */
export function kmFromCoast(p: LatLon): number {
const kx = kmLon(p.lat);
let best = Infinity;
for (const [lon, lat] of ring)
best = Math.min(best, Math.hypot((lon - p.lon) * kx, (lat - p.lat) * KM_LAT));
return best;
}
36 changes: 36 additions & 0 deletions src/routes/adults/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { memo } from '#lib/server/data';
import { round } from '#lib/server/analytics/common';
import { BINS, MIN_POINTS, kde, kmFromCoast, onLand } from '#lib/server/analytics/density';
import { countBy, histogram, linearFit, mean, pairedT, pairs } from '#lib/stats';
import { twoGroupText } from '#lib/server/analytics/tests';
import { num, pValue } from '#lib/format';
Expand All @@ -8,6 +9,40 @@ import type { PageServerLoad } from './$types';
export const load: PageServerLoad = () =>
memo('adults', (d) => {
const a = d.adults;
// Where adults were caught (#174): density of GPS points, with implausible ones set aside.
const located = a
.filter((x) => x.lat != null && x.lon != null)
.map((x, i) => ({
id: `adult-${i}`,
label: x.name ? `Adult blacktip “${x.name}”` : 'Adult blacktip',
lat: x.lat!,
lon: x.lon!,
sub: [x.date, x.site].filter(Boolean).join(' · ')
}));
const why = (p: { lat: number; lon: number }) =>
p.lon >= 0
? 'longitude has no minus sign'
: onLand(p)
? 'on land'
: kmFromCoast(p) > 5
? 'more than 5 km offshore'
: null;
const setAside = located.flatMap((p) => {
const w = why(p);
return w ? [{ label: p.label, reason: w }] : [];
});
const usable = located.filter((p) => !why(p));
const density = kde(usable);
const where = {
points: usable,
setAside,
missing: a.length - located.length,
density: density
? { n: density.n, bandwidthKm: round(density.bandwidthKm, 2), cells: density.cells }
: null,
minPoints: MIN_POINTS,
bins: BINS
};
const lactFit = linearFit(
pairs(
a,
Expand Down Expand Up @@ -64,6 +99,7 @@ export const load: PageServerLoad = () =>
: null
};
return {
where,
stats,
n: a.length,
sex: Object.fromEntries(countBy(a, (x) => x.sex)),
Expand Down
Loading
Loading