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
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ samples/fixture/ synthetic workbooks for tests/CI (committed)
- The national data has slips (the 2023 November break "starts" on 28 September): fix them in `SCHOOL_ERRATA`. The job throws if breaks overlap, so a new typo fails the weekly refresh instead of reaching the site.
- `dayType` precedence: public holiday > festive window > school holidays > weekend. The pressure page also compares school-holiday weekdays with term-time weekdays, which keeps weekends out of the contrast.

### Sampling conditions (#124)

- `analytics/samplingConditions.ts` compares daily rain, daily maximum wind and SST on fieldwork nights (distinct dates with a nursery gillnet set: island-wide series, so a night counts once) with every day of the same calendar months. Each month's days are weighted by its fieldwork nights ÷ its days with a value, so the reference has the fieldwork's seasonal mix. One nursery-month holds a median of two nights, too few to plot alone.
- The reference contains the fieldwork nights, so no two-sample test: the statistic is checked against seeded random draws of the same number of days from each month (`DRAWS`). Fieldwork nights are a little drier and calmer than their months (about 0.7 mm less rain); describe it as coverage, not as weather stopping trips.

### Adding a new season

Drop the workbook in `data/` named like `Physioshark_Shark Log 2026-2027.xlsx` (the `YYYY-YYYY` in the file name sets the season). Check the Data quality page for unknown sites, unmatched headers ("Sheet not imported") and unit warnings, and extend aliases if needed. Never rename columns in the workbook to make the importer happy.
Expand Down
20 changes: 20 additions & 0 deletions e2e/sampling-conditions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
test('sampling conditions fit a phone and pass axe in both themes', 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();
});
81 changes: 81 additions & 0 deletions src/lib/components/SamplingConditions.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<script lang="ts">
import ChartCard from './ChartCard.svelte';
import BarChart from './charts/BarChart.svelte';
import { num, plural } from '#lib/format';
import type { samplingConditions } from '#lib/server/analytics/samplingConditions';
let { data }: { data: ReturnType<typeof samplingConditions> } = $props();

const month = (m: string | null) =>
m
? new Date(`${m}-15T00:00:00Z`).toLocaleDateString('en-GB', {
month: 'short',
year: 'numeric',
timeZone: 'UTC'
})
: '–';
</script>

<section class="section" id="sampling-conditions">
<h2>Which conditions did fieldwork sample?</h2>
<p>
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.
</p>
<p class="small muted">
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.
</p>
{#if data.nights}
<div class="grid-2">
{#each data.metrics as m (m.key)}
<ChartCard
title={m.label}
subtitle={`Share of fieldwork nights and of all days in the same months (${m.unit})`}
stats={m.stats}
note={m.sampled
? `${plural(m.sampled, 'night')} and ${plural(m.refDays ?? 0, 'day')} with a value${m.nightsMissing ? `; ${plural(m.nightsMissing, 'night')} without one` : ''}${m.refMissing ? `; ${plural(m.refMissing, 'day')} in those months without one` : ''}.`
: undefined}
table={m.rows.length
? {
columns: [
{ key: 'bin', label: m.unit },
{ key: 'sampled', label: 'Fieldwork nights', num: true },
{ key: 'sampledPct', label: 'Fieldwork nights (%)', num: true, dp: 1 },
{ key: 'reference', label: 'Days in the same months', num: true },
{ key: 'referencePct', label: 'Days, weighted (%)', num: true, dp: 1 }
],
rows: m.rows
}
: undefined}
>
{#if m.rows.length}
<BarChart
categories={m.rows.map((r) => r.bin)}
series={[
{
name: 'Fieldwork nights',
color: 'var(--series-1)',
values: m.rows.map((r) => r.sampledPct)
},
{
name: 'All days, same months',
color: 'var(--muted)',
values: m.rows.map((r) => r.referencePct)
}
]}
yLabel="Share (%)"
valueFormat={(v) => `${num(v, v < 10 ? 1 : 0)}%`}
/>
{:else}
<p class="muted small">No values for the fieldwork nights.</p>
{/if}
</ChartCard>
{/each}
</div>
{:else}
<p class="muted">No nursery gillnet sets to compare.</p>
{/if}
</section>
115 changes: 115 additions & 0 deletions src/lib/server/analytics/samplingConditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { expect, it } from 'vitest';
import type { Dataset } from '../data';
import { samplingConditions } from './samplingConditions';

type D = Pick<Dataset, 'sets' | 'sites' | 'weather' | 'ocean'>;
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 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('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({
nights: 0,
months: 0,
first: null
});
});
Loading
Loading