From 928ee14b533f217c19b0ce503abe6e9276009073 Mon Sep 17 00:00:00 2001 From: luen Date: Wed, 4 Nov 2026 14:09:47 +1000 Subject: [PATCH 1/2] Charts: compare sampled and available daily conditions (#124) --- AGENTS.md | 2 + e2e/chart-124.spec.ts | 20 +++ src/lib/components/SamplingConditions.svelte | 98 +++++++++++++++ .../analytics/samplingConditions.test.ts | 33 +++++ .../server/analytics/samplingConditions.ts | 114 ++++++++++++++++++ src/lib/server/import/index.ts | 2 +- src/routes/effort/+page.server.ts | 16 ++- src/routes/effort/+page.svelte | 2 + 8 files changed, 283 insertions(+), 4 deletions(-) create mode 100644 e2e/chart-124.spec.ts create mode 100644 src/lib/components/SamplingConditions.svelte create mode 100644 src/lib/server/analytics/samplingConditions.test.ts create mode 100644 src/lib/server/analytics/samplingConditions.ts diff --git a/AGENTS.md b/AGENTS.md index ace5450..803a74f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -93,6 +93,8 @@ samples/fixture/ synthetic workbooks for tests/CI (committed) ### Cyclones and heavy rain (#24) +- Sampling-condition distributions (#124) compare distinct dates within a selected nursery/calendar month. Both groups use identical daily environmental values; reference dates include sampled days. Never pool dates across nursery-month strata without explicit weights or test the overlapping groups as independent samples. + - `fetch-external.mjs --only=cyclones` downloads the IBTrACS South Pacific CSV (~36 MB) and keeps track points within 750 km of Moorea since 2013 (`external/cyclones.csv`, `cyclone_points` table). Analyses use 500 km. - Heavy-rain day = top 1% of ERA5 daily precipitation (the grid cell under-reads mountain rain). - After-event catches are compared with other sets in the same season and month (observed / expected, bootstrap interval), never with all sets: storms and downpours cluster in the wet season. diff --git a/e2e/chart-124.spec.ts b/e2e/chart-124.spec.ts new file mode 100644 index 0000000..492d918 --- /dev/null +++ b/e2e/chart-124.spec.ts @@ -0,0 +1,20 @@ +import { expect, test } from '@playwright/test'; +import AxeBuilder from '@axe-core/playwright'; +test('new charts render on mobile and pass both-theme accessibility', async ({ page }) => { + await page.setViewportSize({ width: 375, height: 800 }); + await page.goto('/effort'); + await expect( + page.getByRole('heading', { name: 'Which conditions did fieldwork sample?', exact: true }) + ).toBeVisible(); + for (const colorScheme of ['light', 'dark'] as const) { + await page.emulateMedia({ colorScheme }); + const results = await new AxeBuilder({ page }).include('main').analyze(); + expect( + results.violations.filter((v) => v.impact === 'serious' || v.impact === 'critical') + ).toEqual([]); + } + expect( + await page.evaluate(() => document.documentElement.scrollWidth - innerWidth) + ).toBeLessThanOrEqual(1); + await expect(page.getByRole('button', { name: /Download CSV/ }).first()).toBeVisible(); +}); diff --git a/src/lib/components/SamplingConditions.svelte b/src/lib/components/SamplingConditions.svelte new file mode 100644 index 0000000..877f762 --- /dev/null +++ b/src/lib/components/SamplingConditions.svelte @@ -0,0 +1,98 @@ + + +
+

Which conditions did fieldwork sample?

+

+ Compare distinct sampling dates with all available calendar days in one nursery-month. The + reference includes sampling dates, so these overlapping groups are descriptive, not independent + samples for a test. The regional weather and offshore SST are shared environmental proxies, not + nursery measurements. Differences do not establish why trips happened or how sharks responded. +

+ {#if data.site} +
+ + + +
+

+ {data.site.name}, {data.period}: {data.sampleDays} sampling dates in {data.calendarDays} calendar + days. Multiple sets on a date count once. Each plot uses the same daily quantity for both groups. + Changing nursery selects its latest sampled month if the chosen month is unavailable. No months + or nurseries are pooled. +

+
+ {#each data.metrics as m (m.key)} + + {#if m.all} + r.allPct!) + }, + ...(m.sampled + ? [ + { + name: 'Sampling days', + color: 'var(--series-2)', + counts: m.rows.map((r) => r.sampledPct!) + } + ] + : []) + ]} + mode="overlay" + xLabel={`${m.label} (${m.unit})`} + yLabel="Valid dates (%)" + /> + {#if !m.sampled}

No matched sampling dates; only the reference is shown.

{/if} + {:else}

No environmental data for this month.

{/if} +
+ {/each} +
+ {:else}

No nursery sampling dates available.

{/if} +
+ + diff --git a/src/lib/server/analytics/samplingConditions.test.ts b/src/lib/server/analytics/samplingConditions.test.ts new file mode 100644 index 0000000..c5c3ded --- /dev/null +++ b/src/lib/server/analytics/samplingConditions.test.ts @@ -0,0 +1,33 @@ +import { expect, it } from 'vitest'; +import type { Dataset } from '../data'; +import { samplingConditions } from './samplingConditions'; + +it('compares identical daily measures and deduplicates sampling dates within a nursery-month', () => { + const d = { + sites: [{ id: 'a', name: 'A', kind: 'nursery' }], + sets: [1, 2, 3].map((n) => ({ + siteId: 'a', + date: n === 3 ? '2020-02-29' : '2020-02-01', + gear: 'Gillnet' + })), + weather: [ + { date: '2020-02-01', precip: 0, windMax: 5 }, + { date: '2020-02-02', precip: 10, windMax: 5 } + ], + ocean: [] + } as unknown as Pick; + const r = samplingConditions(d, 'a', '2020-02'); + expect(r).toMatchObject({ sampleDays: 2, calendarDays: 29 }); + expect(r.metrics[0]).toMatchObject({ all: 2, sampled: 1, missingAll: 27, missingSample: 1 }); + expect(r.metrics[0].rows.reduce((n, b) => n + (b.allPct ?? 0), 0)).toBeCloseTo(100); + expect(r.metrics[0].rows[9].all).toBe(1); + expect(r.metrics[1].rows.reduce((n, b) => n + b.all, 0)).toBe(2); + expect(r.metrics[2].rows.every((b) => b.sampledPct === null)).toBe(true); + expect(samplingConditions(d, 'unknown', '1900-01').period).toBe('2020-02'); +}); +it('returns an explicit empty state without nursery fieldwork', () => { + expect(samplingConditions({ sites: [], sets: [], weather: [], ocean: [] })).toMatchObject({ + site: null, + metrics: [] + }); +}); diff --git a/src/lib/server/analytics/samplingConditions.ts b/src/lib/server/analytics/samplingConditions.ts new file mode 100644 index 0000000..b2a8618 --- /dev/null +++ b/src/lib/server/analytics/samplingConditions.ts @@ -0,0 +1,114 @@ +import type { Dataset } from '../data'; + +/** One nursery/calendar month at a time: no pooling weights or duplicated sampling dates. */ +export function samplingConditions( + d: Pick, + siteId?: string | null, + period?: string | null +) { + const siteIds = new Set(d.sites.filter((s) => s.kind === 'nursery').map((s) => s.id)); + const sets = d.sets.filter( + (s) => s.gear === 'Gillnet' && s.date && s.siteId && siteIds.has(s.siteId) + ); + const sites = d.sites + .filter((s) => sets.some((r) => r.siteId === s.id)) + .map((s) => ({ id: s.id, name: s.name })) + .sort((a, b) => a.name.localeCompare(b.name)); + const selectedSite = sites.find((s) => s.id === siteId) ?? sites[0]; + const own = sets.filter((s) => s.siteId === selectedSite?.id); + const periods = [...new Set(own.map((s) => s.date!.slice(0, 7)))].sort(); + const selectedPeriod = periods.includes(period ?? '') ? period! : periods.at(-1); + if (!selectedSite || !selectedPeriod) + return { + sites, + periods, + site: null, + period: null, + metrics: [], + sampleDays: 0, + calendarDays: 0 + }; + const sampleDates = new Set( + own.filter((s) => s.date!.startsWith(selectedPeriod)).map((s) => s.date!) + ); + const [year, month] = selectedPeriod.split('-').map(Number); + const calendarDays = new Date(Date.UTC(year, month, 0)).getUTCDate(); + const dates = Array.from( + { length: calendarDays }, + (_, i) => `${selectedPeriod}-${String(i + 1).padStart(2, '0')}` + ); + const weather = new Map(d.weather.map((r) => [r.date, r])); + const ocean = new Map(d.ocean.map((r) => [r.date, r])); + const variables = [ + { + key: 'rain', + label: 'Daily rainfall', + unit: 'mm', + get: (date: string) => weather.get(date)?.precip + }, + { + key: 'wind', + label: 'Daily maximum wind', + unit: 'km/h', + get: (date: string) => weather.get(date)?.windMax + }, + { + key: 'sst', + label: 'Daily offshore SST (OISST)', + unit: '°C', + get: (date: string) => ocean.get(date)?.oisst + } + ]; + const metrics = variables.map((v) => { + const all = dates + .map((date) => ({ date, value: v.get(date) })) + .filter( + (r): r is { date: string; value: number } => r.value != null && Number.isFinite(r.value) + ); + const sampled = all.filter((r) => sampleDates.has(r.date)); + const min = all.length ? Math.min(...all.map((r) => r.value)) : 0; + const max = all.length ? Math.max(...all.map((r) => r.value)) : 1; + const lower = min === max ? min - 0.5 : min; + const upper = min === max ? max + 0.5 : max; + const edges = Array.from({ length: 11 }, (_, i) => lower + ((upper - lower) * i) / 10); + const counts = (rs: typeof all) => + edges + .slice(0, -1) + .map( + (lo, i) => + rs.filter( + (r) => r.value >= lo && (i === 9 ? r.value <= edges[i + 1] : r.value < edges[i + 1]) + ).length + ); + const a = counts(all), + s = counts(sampled); + const rows = a.map((n, i) => ({ + from: edges[i], + to: edges[i + 1], + all: n, + sampled: s[i], + allPct: all.length ? (100 * n) / all.length : null, + sampledPct: sampled.length ? (100 * s[i]) / sampled.length : null + })); + return { + key: v.key, + label: v.label, + unit: v.unit, + edges, + rows, + all: all.length, + sampled: sampled.length, + missingAll: calendarDays - all.length, + missingSample: sampleDates.size - sampled.length + }; + }); + return { + sites, + periods, + site: selectedSite, + period: selectedPeriod, + metrics, + sampleDays: sampleDates.size, + calendarDays + }; +} diff --git a/src/lib/server/import/index.ts b/src/lib/server/import/index.ts index 3cfd073..6ce12c6 100644 --- a/src/lib/server/import/index.ts +++ b/src/lib/server/import/index.ts @@ -73,7 +73,7 @@ import { SITE_DEFS } from './sites'; import { readWorkbook } from './xlsx'; /** Bump when import logic changes so existing caches rebuild on the next start. */ -export const IMPORTER_VERSION = '8'; +export const IMPORTER_VERSION = '9'; const MIGRATIONS = import.meta.glob('/drizzle/*.sql', { query: '?raw', diff --git a/src/routes/effort/+page.server.ts b/src/routes/effort/+page.server.ts index 9a33446..69c8947 100644 --- a/src/routes/effort/+page.server.ts +++ b/src/routes/effort/+page.server.ts @@ -1,4 +1,5 @@ -import { memo } from '#lib/server/data'; +import { dataset, memo } from '#lib/server/data'; +import { samplingConditions } from '#lib/server/analytics/samplingConditions'; import { activeNurseries, cpueOf, @@ -11,8 +12,8 @@ import { histogram } from '#lib/stats'; import { rateSlopeText, rateTest, tableText } from '#lib/server/analytics/tests'; import type { PageServerLoad } from './$types'; -export const load: PageServerLoad = () => - memo('effort', (d) => { +export const load: PageServerLoad = ({ url }) => { + const summary = memo('effort', (d) => { const gill = d.sets.filter((s) => s.gear === 'Gillnet'); const eff = effortSets(d); const nurseries = activeNurseries(d); @@ -86,3 +87,12 @@ export const load: PageServerLoad = () => other: d.sets.filter((s) => s.siteId && d.siteById.get(s.siteId)?.kind !== 'nursery').length }; }); + return { + ...summary, + samplingConditions: samplingConditions( + dataset(), + url.searchParams.get('nursery'), + url.searchParams.get('period') + ) + }; +}; diff --git a/src/routes/effort/+page.svelte b/src/routes/effort/+page.svelte index 5d57ca8..1e8743a 100644 --- a/src/routes/effort/+page.svelte +++ b/src/routes/effort/+page.svelte @@ -1,4 +1,5 @@

Which conditions did fieldwork sample?

-

- Compare distinct sampling dates with all available calendar days in one nursery-month. The - reference includes sampling dates, so these overlapping groups are descriptive, not independent - samples for a test. The regional weather and offshore SST are shared environmental proxies, not - nursery measurements. Differences do not establish why trips happened or how sharks responded. +

+ The weather on the {plural(data.nights, 'night')} with a gillnet set, against every day of the same + {plural(data.months, 'month')} ({month(data.first)} to {month(data.last)}). Months with more + fieldwork count for more, so the comparison isn't just wet season against dry. The weather and + sea temperature are one series for the whole island, so a night counts once however many + nurseries were netted. +

+

+ The reference includes the fieldwork nights themselves. A difference shows which conditions the + data covers; it doesn't say why a trip went ahead or how the sharks responded.

- {#if data.site} -
- - - -
-

- {data.site.name}, {data.period}: {data.sampleDays} sampling dates in {data.calendarDays} calendar - days. Multiple sets on a date count once. Each plot uses the same daily quantity for both groups. - Changing nursery selects its latest sampled month if the chosen month is unavailable. No months - or nurseries are pooled. -

+ {#if data.nights}
{#each data.metrics as m (m.key)} - {#if m.all} - r.bin)} series={[ { - name: 'All available calendar days', + name: 'Fieldwork nights', color: 'var(--series-1)', - counts: m.rows.map((r) => r.allPct!) + values: m.rows.map((r) => r.sampledPct) }, - ...(m.sampled - ? [ - { - name: 'Sampling days', - color: 'var(--series-2)', - counts: m.rows.map((r) => r.sampledPct!) - } - ] - : []) + { + name: 'All days, same months', + color: 'var(--muted)', + values: m.rows.map((r) => r.referencePct) + } ]} - mode="overlay" - xLabel={`${m.label} (${m.unit})`} - yLabel="Valid dates (%)" + yLabel="Share (%)" + valueFormat={(v) => `${num(v, v < 10 ? 1 : 0)}%`} /> - {#if !m.sampled}

No matched sampling dates; only the reference is shown.

{/if} - {:else}

No environmental data for this month.

{/if} + {:else} +

No values for the fieldwork nights.

+ {/if}
{/each}
- {:else}

No nursery sampling dates available.

{/if} + {:else} +

No nursery gillnet sets to compare.

+ {/if}
- - diff --git a/src/lib/server/analytics/samplingConditions.test.ts b/src/lib/server/analytics/samplingConditions.test.ts index c5c3ded..711f843 100644 --- a/src/lib/server/analytics/samplingConditions.test.ts +++ b/src/lib/server/analytics/samplingConditions.test.ts @@ -2,32 +2,114 @@ import { expect, it } from 'vitest'; import type { Dataset } from '../data'; import { samplingConditions } from './samplingConditions'; -it('compares identical daily measures and deduplicates sampling dates within a nursery-month', () => { - const d = { - sites: [{ id: 'a', name: 'A', kind: 'nursery' }], - sets: [1, 2, 3].map((n) => ({ - siteId: 'a', - date: n === 3 ? '2020-02-29' : '2020-02-01', - gear: 'Gillnet' - })), - weather: [ - { date: '2020-02-01', precip: 0, windMax: 5 }, - { date: '2020-02-02', precip: 10, windMax: 5 } +type D = Pick; +const days = (month: string, n: number) => + Array.from({ length: n }, (_, i) => `${month}-${String(i + 1).padStart(2, '0')}`); +const make = ( + nights: { date: string; site?: string }[], + precip: (date: string) => number | null, + months: [string, number][] +) => + ({ + sites: [ + { id: 'a', name: 'A', kind: 'nursery' }, + { id: 'b', name: 'B', kind: 'nursery' }, + { id: 'x', name: 'Adults', kind: 'adult' } ], + sets: nights.map((n) => ({ siteId: n.site ?? 'a', date: n.date, gear: 'Gillnet' })), + weather: months.flatMap(([m, n]) => + days(m, n).map((date) => ({ date, precip: precip(date), windMax: 20 })) + ), ocean: [] - } as unknown as Pick; - const r = samplingConditions(d, 'a', '2020-02'); - expect(r).toMatchObject({ sampleDays: 2, calendarDays: 29 }); - expect(r.metrics[0]).toMatchObject({ all: 2, sampled: 1, missingAll: 27, missingSample: 1 }); - expect(r.metrics[0].rows.reduce((n, b) => n + (b.allPct ?? 0), 0)).toBeCloseTo(100); - expect(r.metrics[0].rows[9].all).toBe(1); - expect(r.metrics[1].rows.reduce((n, b) => n + b.all, 0)).toBe(2); - expect(r.metrics[2].rows.every((b) => b.sampledPct === null)).toBe(true); - expect(samplingConditions(d, 'unknown', '1900-01').period).toBe('2020-02'); + }) as unknown as D; + +it('counts a night once across sets and nurseries, and ignores other sites and gear', () => { + const d = make( + [ + { date: '2020-02-03' }, + { date: '2020-02-03' }, + { date: '2020-02-03', site: 'b' }, + { date: '2020-02-29', site: 'x' } + ], + () => 0, + [['2020-02', 29]] + ); + d.sets.push({ siteId: 'a', date: '2020-02-10', gear: 'Hook and line' } as D['sets'][number]); + const r = samplingConditions(d); + expect(r).toMatchObject({ nights: 1, months: 1, first: '2020-02', last: '2020-02' }); + expect(r.metrics[0].sampled).toBe(1); +}); + +it('weights each month by its fieldwork, so a dry month and a wet month do not mix', () => { + // January is wet (10 mm every day), March is dry (0 mm). Fieldwork: 3 January nights, 1 in March. + const d = make( + [ + { date: '2021-01-05' }, + { date: '2021-01-06' }, + { date: '2021-01-07' }, + { date: '2021-03-31' } + ], + (date) => (date.startsWith('2021-01') ? 10 : 0), + [ + ['2021-01', 31], + ['2021-03', 31] + ] + ); + const rain = samplingConditions(d).metrics[0]; + // Unweighted, the reference would average 5 mm; weighted 3:1 it matches the fieldwork exactly. + expect(rain.sampledMean).toBeCloseTo(7.5); + expect(rain.refMean).toBeCloseTo(7.5); + // Nights drawn from their own months always give the same mean, so p is 1. + expect(rain.p).toBe(1); + const pct = (k: 'sampledPct' | 'referencePct') => rain.rows.map((r) => r[k]); + expect(pct('sampledPct')).toEqual(pct('referencePct')); + expect(rain.rows.reduce((n, r) => n + r.referencePct, 0)).toBeCloseTo(100, 0); + expect(rain.rows.at(-1)!.bin).toBe('40+'); }); -it('returns an explicit empty state without nursery fieldwork', () => { + +it('keeps missing values missing and stays inside the months with fieldwork', () => { + // 2024-02 has 29 days; two have no rain value, one of them a fieldwork night. + const d = make( + [{ date: '2024-02-01' }, { date: '2024-02-29' }], + (date) => (date === '2024-02-29' || date === '2024-02-10' ? null : 1), + [ + ['2024-01', 31], + ['2024-02', 29] + ] + ); + const rain = samplingConditions(d).metrics[0]; + expect(rain).toMatchObject({ sampled: 1, nightsMissing: 1, refDays: 27, refMissing: 2 }); + // January had no fieldwork: none of its 31 days enter the reference. + expect(rain.rows.reduce((n, r) => n + r.reference, 0)).toBe(27); + // No SST at all: the metric is empty, not zero. + expect(samplingConditions(d).metrics[2]).toMatchObject({ sampled: 0, rows: [] }); +}); + +it('flags fieldwork that picks the dry days of each month', () => { + // Rain on odd days only; every fieldwork night is an even (dry) day. + const months: [string, number][] = [ + ['2019-01', 31], + ['2019-02', 28], + ['2019-03', 31], + ['2019-04', 30] + ]; + const nights = months.flatMap(([m]) => + [2, 4, 6, 8, 10].map((day) => ({ date: `${m}-${String(day).padStart(2, '0')}` })) + ); + const d = make(nights, (date) => (Number(date.slice(8)) % 2 ? 12 : 0), months); + const rain = samplingConditions(d).metrics[0]; + expect(rain.sampledMean).toBe(0); + expect(rain.refMean).toBeGreaterThan(5); + expect(rain.p).toBeLessThan(0.01); + expect(rain.stats).toMatch(/randomisation test, p [<=]/); + // Same seed, same answer on every render. + expect(samplingConditions(d).metrics[0].p).toBe(rain.p); +}); + +it('returns an empty comparison without nursery fieldwork', () => { expect(samplingConditions({ sites: [], sets: [], weather: [], ocean: [] })).toMatchObject({ - site: null, - metrics: [] + nights: 0, + months: 0, + first: null }); }); diff --git a/src/lib/server/analytics/samplingConditions.ts b/src/lib/server/analytics/samplingConditions.ts index b2a8618..f80df4b 100644 --- a/src/lib/server/analytics/samplingConditions.ts +++ b/src/lib/server/analytics/samplingConditions.ts @@ -1,114 +1,209 @@ +/** + * Conditions on fieldwork nights against the record (#124). The weather and SST series are one + * point for the whole island, so a night counts once however many nurseries or sets it had. + * + * Fieldwork clusters in some months, and the weather changes through the year, so nights are only + * compared with days of the months they fell in. Each calendar month with fieldwork is a stratum, + * and its days carry weight (fieldwork nights ÷ days with a value), so every month counts as much + * in the reference as in the fieldwork. The reference includes the fieldwork nights themselves: + * the two groups overlap, so instead of a two-sample test the statistic is checked against random + * draws of the same number of days from each month (a within-month randomisation test). + */ +import { num, pValue } from '#lib/format'; import type { Dataset } from '../data'; +import { round } from './common'; -/** One nursery/calendar month at a time: no pooling weights or duplicated sampling dates. */ -export function samplingConditions( - d: Pick, - siteId?: string | null, - period?: string | null -) { - const siteIds = new Set(d.sites.filter((s) => s.kind === 'nursery').map((s) => s.id)); - const sets = d.sets.filter( - (s) => s.gear === 'Gillnet' && s.date && s.siteId && siteIds.has(s.siteId) - ); - const sites = d.sites - .filter((s) => sets.some((r) => r.siteId === s.id)) - .map((s) => ({ id: s.id, name: s.name })) - .sort((a, b) => a.name.localeCompare(b.name)); - const selectedSite = sites.find((s) => s.id === siteId) ?? sites[0]; - const own = sets.filter((s) => s.siteId === selectedSite?.id); - const periods = [...new Set(own.map((s) => s.date!.slice(0, 7)))].sort(); - const selectedPeriod = periods.includes(period ?? '') ? period! : periods.at(-1); - if (!selectedSite || !selectedPeriod) - return { - sites, - periods, - site: null, - period: null, - metrics: [], - sampleDays: 0, - calendarDays: 0 - }; - const sampleDates = new Set( - own.filter((s) => s.date!.startsWith(selectedPeriod)).map((s) => s.date!) +/** Draws for the randomisation test; p can't go below 1/(DRAWS + 1). */ +export const DRAWS = 5000; + +/** Seeded uniform draws (mulberry32), so the page shows the same p on every render. */ +function rng(seed: number) { + return () => { + seed = (seed + 0x6d2b79f5) | 0; + let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }; +} + +const daysOf = (month: string) => { + const [y, m] = month.split('-').map(Number); + const n = new Date(Date.UTC(y, m, 0)).getUTCDate(); + return Array.from({ length: n }, (_, i) => `${month}-${String(i + 1).padStart(2, '0')}`); +}; + +interface Variable { + key: string; + label: string; + unit: string; + dp: number; + get: (date: string) => number | null | undefined; + /** Bin edges. With `open`, the last edge starts a final open-ended bin. */ + edges: (values: number[]) => number[]; + open?: boolean; +} + +/** Equal-width edges at multiples of `step` that cover every value. */ +const steps = (step: number) => (values: number[]) => { + const lo = Math.floor(Math.min(...values) / step) * step; + const hi = Math.max(lo + step, Math.ceil((Math.max(...values) + 1e-9) / step) * step); + return Array.from({ length: Math.round((hi - lo) / step) + 1 }, (_, i) => + round(lo + i * step, 3)! ); - const [year, month] = selectedPeriod.split('-').map(Number); - const calendarDays = new Date(Date.UTC(year, month, 0)).getUTCDate(); - const dates = Array.from( - { length: calendarDays }, - (_, i) => `${selectedPeriod}-${String(i + 1).padStart(2, '0')}` +}; + +export function samplingConditions(d: Pick) { + const nurseries = new Set(d.sites.filter((s) => s.kind === 'nursery').map((s) => s.id)); + const nights = new Set( + d.sets + .filter((s) => s.gear === 'Gillnet' && s.date && s.siteId && nurseries.has(s.siteId)) + .map((s) => s.date!) ); + const byMonth = new Map(); + for (const date of [...nights].sort()) { + const m = date.slice(0, 7); + byMonth.set(m, [...(byMonth.get(m) ?? []), date]); + } + const months = [...byMonth.keys()]; const weather = new Map(d.weather.map((r) => [r.date, r])); const ocean = new Map(d.ocean.map((r) => [r.date, r])); - const variables = [ + const variables: Variable[] = [ { key: 'rain', - label: 'Daily rainfall', + label: 'Rain that day', unit: 'mm', - get: (date: string) => weather.get(date)?.precip + dp: 1, + get: (x) => weather.get(x)?.precip, + edges: () => [0, 1, 5, 10, 20, 40], + open: true }, { key: 'wind', - label: 'Daily maximum wind', + label: 'Strongest wind that day', unit: 'km/h', - get: (date: string) => weather.get(date)?.windMax + dp: 1, + get: (x) => weather.get(x)?.windMax, + edges: steps(5) }, { key: 'sst', - label: 'Daily offshore SST (OISST)', + label: 'Sea-surface temperature', unit: '°C', - get: (date: string) => ocean.get(date)?.oisst + dp: 2, + // Same source as the per-set SST (OISST where present, else Coral Reef Watch; #154). + get: (x) => ocean.get(x)?.oisst ?? ocean.get(x)?.crwSst, + edges: steps(0.5) } ]; - const metrics = variables.map((v) => { - const all = dates - .map((date) => ({ date, value: v.get(date) })) - .filter( - (r): r is { date: string; value: number } => r.value != null && Number.isFinite(r.value) - ); - const sampled = all.filter((r) => sampleDates.has(r.date)); - const min = all.length ? Math.min(...all.map((r) => r.value)) : 0; - const max = all.length ? Math.max(...all.map((r) => r.value)) : 1; - const lower = min === max ? min - 0.5 : min; - const upper = min === max ? max + 0.5 : max; - const edges = Array.from({ length: 11 }, (_, i) => lower + ((upper - lower) * i) / 10); - const counts = (rs: typeof all) => - edges - .slice(0, -1) - .map( - (lo, i) => - rs.filter( - (r) => r.value >= lo && (i === 9 ? r.value <= edges[i + 1] : r.value < edges[i + 1]) - ).length - ); - const a = counts(all), - s = counts(sampled); - const rows = a.map((n, i) => ({ - from: edges[i], - to: edges[i + 1], - all: n, - sampled: s[i], - allPct: all.length ? (100 * n) / all.length : null, - sampledPct: sampled.length ? (100 * s[i]) / sampled.length : null + + const metrics = variables.map((v, vi) => { + const value = (x: string) => { + const y = v.get(x); + return y != null && Number.isFinite(y) ? y : null; + }; + // One stratum per month with fieldwork: its valid days and its valid fieldwork nights. + const strata = months + .map((m) => ({ + days: daysOf(m) + .map(value) + .filter((y): y is number => y != null), + nights: byMonth + .get(m)! + .map(value) + .filter((y): y is number => y != null) + })) + .filter((s) => s.nights.length && s.days.length); + const sampledN = strata.reduce((n, s) => n + s.nights.length, 0); + const nightsMissing = nights.size - sampledN; + const refDays = strata.reduce((n, s) => n + s.days.length, 0); + const refMissing = + months.reduce((n, m) => n + daysOf(m).length, 0) - + months.reduce((n, m) => n + daysOf(m).filter((x) => value(x) != null).length, 0); + if (!sampledN) return { key: v.key, label: v.label, unit: v.unit, sampled: 0, rows: [] }; + + const all = strata.flatMap((s) => s.days); + const edges = v.edges(all); + // Closed scales have one bin per pair of edges (the last includes its top edge); an open + // scale's last edge starts a final "and above" bin. + const nBins = v.open ? edges.length : edges.length - 1; + const bin = (y: number) => { + let i = nBins - 1; + while (i > 0 && y < edges[i]) i--; + return i; + }; + const sampled = new Array(nBins).fill(0); + const refCount = new Array(nBins).fill(0); + const refShare = new Array(nBins).fill(0); + let sampledSum = 0, + refMean = 0; + for (const s of strata) { + const w = s.nights.length / sampledN / s.days.length; + for (const y of s.nights) { + sampled[bin(y)]++; + sampledSum += y; + } + for (const y of s.days) { + refCount[bin(y)]++; + refShare[bin(y)] += w; + refMean += w * y; + } + } + const sampledMean = sampledSum / sampledN; + const diff = sampledMean - refMean; + + // Within-month randomisation: draw each month's number of nights from its own days. + const rand = rng(124 + vi); + let extreme = 0; + for (let b = 0; b < DRAWS; b++) { + let total = 0; + for (const s of strata) { + const pool = s.days.slice(); + for (let i = 0; i < s.nights.length; i++) { + const j = i + Math.floor(rand() * (pool.length - i)); + [pool[i], pool[j]] = [pool[j], pool[i]]; + total += pool[i]; + } + } + if (Math.abs(total / sampledN - refMean) >= Math.abs(diff) - 1e-9) extreme++; + } + const p = (extreme + 1) / (DRAWS + 1); + + const fmt = (y: number) => num(y, v.dp === 2 ? 1 : 0); + const rows = sampled.map((n, i) => ({ + bin: + v.open && i === nBins - 1 ? `${fmt(edges[i])}+` : `${fmt(edges[i])}–${fmt(edges[i + 1])}`, + sampled: n, + sampledPct: round((100 * n) / sampledN, 1)!, + reference: refCount[i], + referencePct: round(100 * refShare[i], 1)! })); + const shown = round(diff, v.dp)!; + const gap = + shown === 0 + ? `0 ${v.unit}` + : `${shown > 0 ? '+' : '−'}${num(Math.abs(shown), v.dp)} ${v.unit}`; + const stats = `Fieldwork nights averaged ${num(sampledMean, v.dp)} ${v.unit} against ${num(refMean, v.dp)} ${v.unit} on all days of the same months, weighted by fieldwork per month (${gap}; within-month randomisation test, ${pValue(p)}, ${num(DRAWS)} draws).`; return { key: v.key, label: v.label, unit: v.unit, - edges, - rows, - all: all.length, - sampled: sampled.length, - missingAll: calendarDays - all.length, - missingSample: sampleDates.size - sampled.length + sampled: sampledN, + nightsMissing, + refDays, + refMissing, + sampledMean: round(sampledMean, v.dp + 1), + refMean: round(refMean, v.dp + 1), + p, + stats, + rows }; }); + return { - sites, - periods, - site: selectedSite, - period: selectedPeriod, - metrics, - sampleDays: sampleDates.size, - calendarDays + nights: nights.size, + months: months.length, + first: months[0] ?? null, + last: months.at(-1) ?? null, + metrics }; } diff --git a/src/routes/effort/+page.server.ts b/src/routes/effort/+page.server.ts index 69c8947..d1aae0a 100644 --- a/src/routes/effort/+page.server.ts +++ b/src/routes/effort/+page.server.ts @@ -1,4 +1,4 @@ -import { dataset, memo } from '#lib/server/data'; +import { memo } from '#lib/server/data'; import { samplingConditions } from '#lib/server/analytics/samplingConditions'; import { activeNurseries, @@ -12,8 +12,8 @@ import { histogram } from '#lib/stats'; import { rateSlopeText, rateTest, tableText } from '#lib/server/analytics/tests'; import type { PageServerLoad } from './$types'; -export const load: PageServerLoad = ({ url }) => { - const summary = memo('effort', (d) => { +export const load: PageServerLoad = () => + memo('effort', (d) => { const gill = d.sets.filter((s) => s.gear === 'Gillnet'); const eff = effortSets(d); const nurseries = activeNurseries(d); @@ -84,15 +84,7 @@ export const load: PageServerLoad = ({ url }) => { g, n: d.sets.filter((s) => s.gear === g).length })), - other: d.sets.filter((s) => s.siteId && d.siteById.get(s.siteId)?.kind !== 'nursery').length + other: d.sets.filter((s) => s.siteId && d.siteById.get(s.siteId)?.kind !== 'nursery').length, + samplingConditions: samplingConditions(d) }; }); - return { - ...summary, - samplingConditions: samplingConditions( - dataset(), - url.searchParams.get('nursery'), - url.searchParams.get('period') - ) - }; -}; diff --git a/src/routes/effort/+page.svelte b/src/routes/effort/+page.svelte index 1e8743a..34ac2de 100644 --- a/src/routes/effort/+page.svelte +++ b/src/routes/effort/+page.svelte @@ -1,6 +1,6 @@