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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ samples/fixture/ synthetic workbooks for tests/CI (committed)
- `analytics/catchModel.ts`: negative-binomial counts per timed gillnet set, offset log(net-hours), natural-spline day of season (knots at the 0/25/50/75/100% quantiles), nursery and season as effect-coded fixed effects (each level vs the average; no random effects), and nine environment terms per +1 SD. Complete cases only; nurseries need ≥ 30 complete sets, and a species gets a nursery effect only where ≥ 10 of it were caught (otherwise the estimate runs off to zero or infinity).
- The headline: sea temperature's raw link with catch rate (≈ −25% per °C) vanishes once day of the season is in the model. Don't describe SST as a driver of catches on other pages.
- `glm.ts` is generic (IRLS + ML θ, Wald intervals via `contrast`); reuse it rather than writing another fitter.
- `selectCatchModel()` holds the sample filters (`MIN_SITE_SETS`, `MIN_SITE_CATCH`, `MIN_MODEL_SETS`); the fits and the "Which sets does the model use?" chart (#123, `modelSelectionFlow`) both call it, so change filters there. The first steps are sequential; each species model restarts from the all-shark sample.
- Measurement coverage on /data-quality (#123, `analytics/measurementCoverage.ts`) counts records with a value over eligible records, per season: availability, not validity. Zero is recorded; a cell with no eligible records stays empty, not 0%. Lavage results only count captures marked lavaged or with a result.

### Sex ratio mixed model (#117)

Expand Down
31 changes: 31 additions & 0 deletions e2e/measurement-coverage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';

test('coverage heatmaps fit a phone and pass axe in both themes', async ({ page }) => {
await page.setViewportSize({ width: 375, height: 800 });
await page.goto('/data-quality');
await expect(
page.getByRole('heading', { name: 'What was recorded, season by season', 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();
// The notes table is wider than a phone: it scrolls and takes keyboard focus (#150).
const notes = page.getByRole('region', { name: 'Data notes' });
await notes.focus();
await expect(notes).toBeFocused();
});

test('the catch model shows which sets it uses', async ({ page }) => {
await page.goto('/drivers');
await page.getByText('Which sets does the model use?', { exact: true }).click();
await expect(page.getByRole('heading', { name: 'Sets left after each filter' })).toBeVisible();
});
4 changes: 4 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
--seq-5: #256abf;
--seq-6: #184f95;
--seq-7: #0d366b;
/* Text on the strong end of the scale (seq-5 to seq-7): dark cells in light mode, pale in dark. */
--seq-strong-ink: #ffffff;
--div-neg: #2a78d6;
--div-mid: #f0efec;
--div-pos: #e34948;
Expand Down Expand Up @@ -83,6 +85,7 @@
--seq-5: #5598e7;
--seq-6: #86b6ef;
--seq-7: #cde2fb;
--seq-strong-ink: #08111f;
--div-mid: #2a3446;
--shadow: 0 1px 2px rgb(0 0 0 / 0.3);
}
Expand Down Expand Up @@ -120,6 +123,7 @@
--seq-5: #5598e7;
--seq-6: #86b6ef;
--seq-7: #cde2fb;
--seq-strong-ink: #08111f;
--div-mid: #2a3446;
--shadow: 0 1px 2px rgb(0 0 0 / 0.3);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/ChartCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{#if table && table.rows.length}
<details class="tbl">
<summary>Show data ({table.rows.length} rows)</summary>
<DataTable columns={table.columns} rows={table.rows} max={500} />
<DataTable columns={table.columns} rows={table.rows} max={500} label={`Data: ${title}`} />
</details>
{/if}
{#if canDownload}
Expand Down
36 changes: 33 additions & 3 deletions src/lib/components/DataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,31 @@
let {
columns,
rows,
max = 200
}: { columns: Column[]; rows: Record<string, unknown>[]; max?: number } = $props();
max = 200,
label = 'Data table'
}: {
columns: Column[];
rows: Record<string, unknown>[];
max?: number;
/** Names the scroll region for screen readers. */
label?: string;
} = $props();

// A table wider than its box scrolls sideways, so keyboard users need to focus it (#150). The
// server can't measure, so it renders every table focusable; after mount only tables that
// overflow keep the tab stop.
let box = $state<HTMLDivElement>();
let overflows = $state(true);
$effect(() => {
const el = box;
if (!el) return;
const check = () => (overflows = el.scrollWidth > el.clientWidth + 1);
check();
const ro = new ResizeObserver(check);
ro.observe(el);
if (el.firstElementChild) ro.observe(el.firstElementChild);
return () => ro.disconnect();
});
const shown = $derived(rows.slice(0, max));
const fmt = (v: unknown, c: Column) =>
v == null || v === ''
Expand All @@ -24,7 +47,14 @@
: String(v);
</script>

<div class="table-scroll">
<!-- svelte-ignore a11y_no_noninteractive_tabindex (Keyboard users need to scroll wide tables.) -->
<div
bind:this={box}
class="table-scroll"
role={overflows ? 'region' : undefined}
aria-label={overflows ? label : undefined}
tabindex={overflows ? 0 : undefined}
>
<table class="data">
<thead>
<tr>
Expand Down
68 changes: 68 additions & 0 deletions src/lib/components/MeasurementCoverage.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<script lang="ts">
import ChartCard from './ChartCard.svelte';
import Heatmap from './charts/Heatmap.svelte';
import { plural } from '#lib/format';
import type { measurementCoverage } from '#lib/server/analytics/measurementCoverage';
let { data }: { data: ReturnType<typeof measurementCoverage> } = $props();

// Don't round 99.6% up to a complete 100%, or 0.4% down to nothing.
const share = (v: number) =>
v >= 100 ? '100%' : v > 99 ? '>99%' : v > 0 && v < 1 ? '<1%' : `${Math.round(v)}%`;
const panels = $derived([
{
title: 'Field captures: share with each measurement',
subtitle: 'Every field capture, recaptures included',
panel: data.captures
},
{
title: 'Gillnet sets: share with each measurement',
subtitle: 'Every gillnet set; SST, rain and tide are joined from outside sources',
panel: data.sets
}
]);
</script>

<section class="section" id="coverage">
<h2>What was recorded, season by season</h2>
<p>
The share of records with a value for each measurement, which shows what each comparison on this
site can draw on. It is not a check that the values are right, and it says nothing about the
lab's work. A zero counts as recorded. A blank can't tell a skipped measurement from one the
protocol didn't call for that season. A dashed cell has no records to count. Each table gives
the counts.
</p>
<p class="small muted">
Lavage results count only captures marked as lavaged or with a result. Of the rest, {plural(
data.unknownLavage,
'capture doesn’t',
'captures don’t'
)} say whether the shark was lavaged and {plural(data.notLavaged, 'was', 'were')} marked as not lavaged.
</p>
{#each panels as p (p.title)}
<ChartCard
title={p.title}
subtitle={p.subtitle}
table={{
columns: [
{ key: 'season', label: 'Season' },
{ key: 'field', label: 'Measurement' },
{ key: 'available', label: 'Recorded', num: true },
{ key: 'eligible', label: 'Eligible', num: true },
{ key: 'percent', label: 'Recorded %', num: true, dp: 1 }
],
rows: p.panel.rows
}}
>
<Heatmap
xLabels={p.panel.fields}
yLabels={p.panel.seasons}
values={p.panel.values}
domain={[0, 100]}
format={share}
cellLabels
xTitle="Measurement"
yTitle="Season"
/>
</ChartCard>
{/each}
</section>
74 changes: 74 additions & 0 deletions src/lib/components/ModelSelection.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<script lang="ts">
import ChartCard from './ChartCard.svelte';
import BarChart from './charts/BarChart.svelte';
import { num, plural } from '#lib/format';
import type { modelSelectionFlow } from '#lib/server/analytics/measurementCoverage';
let { flow }: { flow: ReturnType<typeof modelSelectionFlow> } = $props();

const incomplete = $derived(flow.rows[1]?.excluded ?? 0);
const missingText = $derived(
flow.missing.map((m, i) =>
i ? `${num(m.n)} the ${m.label}` : `${num(m.n)} lack the ${m.label}`
)
);
</script>

<details class="selection">
<summary>Which sets does the model use?</summary>
<p>
Each bar is the number of sets left after a filter, counted by the same code the model uses. The
first three filters run in order and leave the sample for all sharks. Each species model then
starts again from that sample and drops the nurseries where that species was rarely caught.
</p>
{#if incomplete}
<p>
Of the {plural(incomplete, 'set')} missing a model variable, {missingText.length > 1
? `${missingText.slice(0, -1).join(', ')} and ${missingText.at(-1)}`
: missingText[0]}{flow.missing.length > 1 ? `; a set can lack more than one` : ''}.
</p>
{/if}
<p class="small muted">
Being in the sample doesn't guarantee a fit: the model also needs at least {num(
flow.minModelSets
)} sets, two nurseries and two seasons. The sample for all sharks has {plural(
flow.modelSets,
'set'
)} from {plural(flow.nurseries, 'nursery', 'nurseries')} over {plural(flow.seasons, 'season')}.
</p>
<ChartCard
title="Sets left after each filter"
subtitle="Timed gillnet sets, from all of them down to each model's sample"
table={{
columns: [
{ key: 'stage', label: 'Filter' },
{ key: 'rule', label: 'Keeps' },
{ key: 'remaining', label: 'Sets left', num: true },
{ key: 'nurseries', label: 'Nurseries', num: true },
{ key: 'excluded', label: 'Left out at this filter', num: true }
],
rows: flow.rows
}}
>
<BarChart
horizontal
categories={flow.rows.map((r) => r.stage)}
series={[
{ name: 'Sets', color: 'var(--series-1)', values: flow.rows.map((r) => r.remaining) }
]}
valueFormat={(v) => num(v)}
/>
</ChartCard>
</details>

<style>
.selection {
margin-top: 24px;
}
summary {
cursor: pointer;
font-weight: 650;
}
.selection p {
margin: 10px 0;
}
</style>
17 changes: 11 additions & 6 deletions src/lib/components/charts/Heatmap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,27 @@
const hi = $derived(domain?.[1] ?? (flat.length ? Math.max(...flat) : 1));
const absMax = $derived(Math.max(Math.abs(lo), Math.abs(hi)) || 1);

function fill(v: number | null): { color: string; opacity: number; dark: boolean } {
function fill(v: number | null): { color: string; opacity: number; ink: string } {
// No data: an empty dashed cell, never a colour from the scale (the lightest steps are
// close to --surface-2, in dark mode especially).
if (v == null || !Number.isFinite(v)) return { color: 'none', opacity: 1, dark: false };
if (v == null || !Number.isFinite(v)) return { color: 'none', opacity: 1, ink: 'var(--ink)' };
if (mode === 'div') {
const t = Math.max(-1, Math.min(1, v / absMax));
if (Math.abs(t) < 0.06) return { color: 'var(--div-mid)', opacity: 1, dark: false };
if (Math.abs(t) < 0.06) return { color: 'var(--div-mid)', opacity: 1, ink: 'var(--ink)' };
return {
color: t < 0 ? 'var(--div-neg)' : 'var(--div-pos)',
opacity: 0.18 + 0.82 * Math.abs(t),
dark: Math.abs(t) > 0.6
ink: Math.abs(t) > 0.6 ? '#ffffff' : 'var(--ink)'
};
}
const t = hi === lo ? 1 : (v - lo) / (hi - lo);
const k = Math.min(7, 1 + Math.floor(t * 6.999));
return { color: `var(--seq-${k})`, opacity: 1, dark: k >= 5 };
// The strong end is dark in light mode and pale in dark mode, so its text flips with it.
return {
color: `var(--seq-${k})`,
opacity: 1,
ink: k >= 5 ? 'var(--seq-strong-ink)' : 'var(--ink)'
};
}

let tip = $state<TipState | null>(null);
Expand Down Expand Up @@ -133,7 +138,7 @@
dy="0.32em"
text-anchor="middle"
font-size="10"
fill={f.dark ? '#ffffff' : 'var(--ink)'}
fill={f.ink}
pointer-events="none">{format(v)}</text
>
{/if}
Expand Down
27 changes: 19 additions & 8 deletions src/lib/server/analytics/catchModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const TERMS = [
'crew'
] as const;
/** Nurseries need this many complete sets to get their own effect. */
const MIN_SITE_SETS = 30;
export const MIN_SITE_SETS = 30;
/** The model needs this many sets from supported nurseries (and two nurseries, two seasons). */
export const MIN_MODEL_SETS = 200;
const SPLINE_KNOTS = [0, 0.25, 0.5, 0.75, 1];

type Response = 'all' | 'Cm' | 'Na';
Expand All @@ -46,9 +48,10 @@ function count(s: SetRow, r: Response): number {
}

/** A species gets its own nursery effect only where it was caught at least this often. */
const MIN_SITE_CATCH = 10;
export const MIN_SITE_CATCH = 10;

export function catchModel(d: Dataset) {
/** The sample selection behind every fit; /drivers shows it step by step (#123). */
export function selectCatchModel(d: Dataset) {
const vars = TERMS.map((k) => SET_VARIABLES.find((v) => v.key === k)!);
const complete = effortSets(d).filter(
(s) => s.cpue != null && s.date && s.siteId && vars.every((v) => v.get(s) != null)
Expand All @@ -61,7 +64,18 @@ export function catchModel(d: Dataset) {
.sort();
const base = complete.filter((s) => sites.includes(s.siteId!));
const seasons = [...new Set(base.map((s) => s.season))].sort();
if (base.length < 200 || sites.length < 2 || seasons.length < 2) return null;
const selectSpecies = (r: Response) => {
const caught = new Map<string, number>();
for (const s of base) caught.set(s.siteId!, (caught.get(s.siteId!) ?? 0) + count(s, r));
const own = sites.filter((id) => r === 'all' || (caught.get(id) ?? 0) >= MIN_SITE_CATCH);
return { own, rows: base.filter((s) => own.includes(s.siteId!)) };
};
return { vars, complete, perSite, sites, base, seasons, selectSpecies };
}

export function catchModel(d: Dataset) {
const { vars, perSite, sites, base, seasons, selectSpecies } = selectCatchModel(d);
if (base.length < MIN_MODEL_SETS || sites.length < 2 || seasons.length < 2) return null;

// Scales and spline knots come from the full model so every species shares the same units.
const scale = vars.map((v) => {
Expand All @@ -79,10 +93,7 @@ export function catchModel(d: Dataset) {
const iEnv = 1;
const iDay = iEnv + vars.length;
const fitFor = (r: Response) => {
const caught = new Map<string, number>();
for (const s of base) caught.set(s.siteId!, (caught.get(s.siteId!) ?? 0) + count(s, r));
const own = sites.filter((id) => r === 'all' || (caught.get(id) ?? 0) >= MIN_SITE_CATCH);
const rows = base.filter((s) => own.includes(s.siteId!));
const { own, rows } = selectSpecies(r);
const ownSeasons = seasons.filter((x) => rows.some((s) => s.season === x));
const days = rows.map((s) => seasonDay(s.date!));
const meanBasis = new Array<number>(nSpline).fill(0);
Expand Down
Loading
Loading