Skip to content

Commit 7bdaae9

Browse files
Luenclaude
andcommitted
Merge main (#108 catch model) into diet
Both branches added blocks at the same spots in stats.ts (normalP vs the chi-square helpers), findings.ts (catch-model vs lavage finding) and AGENTS.md; kept both. Restores the normalP assertions in stats.test.ts now that both helpers are on one branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents 9f58423 + 9690c2d commit 7bdaae9

12 files changed

Lines changed: 1024 additions & 7 deletions

File tree

‎AGENTS.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ src/lib/server/analytics common.ts (CPUE, Fulton's K, recapture pairs…) · env
4444
findings.ts (live findings) · insitu.ts (logger temperature summaries)
4545
population.ts (Schnabel abundance, von Bertalanffy growth)
4646
diet.ts (gastric lavage: fullness, prey groups)
47+
glm.ts (NB/Poisson GLM, splines) · catchModel.ts (joint catch model)
4748
src/lib/server/astro.ts sunset/twilight/moon (local formulas) · tides.ts (extrema, interpolation)
4849
src/lib/server/holidays.ts French Polynesia public holidays (Easter-based too) + festive window
4950
src/lib/stats.ts regression, Spearman, Welch, Wilson, binomial, histograms, box stats
5051
src/lib/components/ ChartCard, StatTile, DataTable, FindingCard, MooreaMap (SSR SVG),
5152
InteractiveMap (Leaflet, lazy; MooreaMap is its fallback), charts/*
53+
(ForestPlot: rate ratios with intervals on a log scale)
5254
src/routes/ one folder per page (+page.server.ts computes, +page.svelte renders)
5355
scripts/ fetch-external.mjs · make-fixture.mjs · server.mjs · nightlyRelease.mjs
5456
external/ public environmental snapshots (committed)
@@ -85,6 +87,12 @@ samples/fixture/ synthetic workbooks for tests/CI (committed)
8587
- ERA5 `cloud_evening` covers every set; the crew's 1–4 `cloud_cover` score exists only in 2020–22 and tracks ERA5 (score 1 ≈ 43%, 4 ≈ 94%). `moonlight()` in `analytics/cloud.ts` = illumination × sin(moon altitude) × clear-sky fraction.
8688
- Cloud vs water temperature must be compared within the same month: the cloudy wet season is also the warmest, so raw correlations are ~0 while within-month ones are strongly negative.
8789

90+
### Catch model (#54)
91+
92+
- `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).
93+
- 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.
94+
- `glm.ts` is generic (IRLS + ML θ, Wald intervals via `contrast`); reuse it rather than writing another fitter.
95+
8896
### Adding a new season
8997

9098
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.
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<script lang="ts" module>
2+
export interface ForestValue {
3+
est: number;
4+
lo: number;
5+
hi: number;
6+
}
7+
export interface ForestSeries {
8+
name: string;
9+
color: string;
10+
values: (ForestValue | null)[];
11+
}
12+
</script>
13+
14+
<script lang="ts">
15+
/**
16+
* Estimates with 95% intervals on a log scale around a reference line (rate ratios, #54).
17+
* One row per term; several series sit side by side within a row.
18+
*/
19+
import { registerCsv } from './csv';
20+
import { num } from '#lib/format';
21+
import Legend from './Legend.svelte';
22+
import Tip, { type TipState } from './Tip.svelte';
23+
import { linear } from './scale';
24+
25+
let {
26+
rows,
27+
series,
28+
ref = 1,
29+
xLabel = '',
30+
format = (v: number) => num(v, 2)
31+
}: {
32+
rows: string[];
33+
series: ForestSeries[];
34+
ref?: number;
35+
xLabel?: string;
36+
format?: (v: number) => string;
37+
} = $props();
38+
39+
registerCsv(() => ({
40+
columns: [
41+
'term',
42+
...series.flatMap((s) =>
43+
series.length > 1
44+
? [s.name, `${s.name} ci_low`, `${s.name} ci_high`]
45+
: ['estimate', 'ci_low', 'ci_high']
46+
)
47+
],
48+
rows: rows.map((r, i) => [
49+
r,
50+
...series.flatMap((s) => [s.values[i]?.est, s.values[i]?.lo, s.values[i]?.hi])
51+
])
52+
}));
53+
54+
let width = $state(640);
55+
const CHAR_W = 6.2;
56+
const longest = $derived(Math.max(0, ...rows.map((c) => c.length)));
57+
const m = $derived({
58+
top: 6,
59+
right: 16,
60+
bottom: xLabel ? 42 : 26,
61+
left: Math.min(Math.max(110, width * 0.45), 12 + longest * CHAR_W)
62+
});
63+
// Labels that don't fit the margin wrap onto a second line (narrow screens).
64+
const fit = $derived(Math.max(8, Math.floor((m.left - 12) / CHAR_W)));
65+
const wrap = (s: string, n: number): string[] => {
66+
if (s.length <= n) return [s];
67+
const cut = s.lastIndexOf(' ', n);
68+
if (cut <= 0) return [`${s.slice(0, n - 1)}…`];
69+
const rest = s.slice(cut + 1);
70+
return [s.slice(0, cut), rest.length > n ? `${rest.slice(0, n - 1)}…` : rest];
71+
};
72+
const labels = $derived(rows.map((r) => wrap(r, fit)));
73+
const band = $derived(series.length > 1 || labels.some((l) => l.length > 1) ? 30 : 24);
74+
const h = $derived(m.top + m.bottom + rows.length * band);
75+
76+
const all = $derived(series.flatMap((s) => s.values.filter((v): v is ForestValue => v != null)));
77+
const ext = $derived.by<[number, number]>(() => {
78+
const lo = Math.min(ref, ...all.map((v) => v.lo));
79+
const hi = Math.max(ref, ...all.map((v) => v.hi));
80+
const pad = (Math.log(hi) - Math.log(lo)) * 0.06 || 0.1;
81+
return [Math.exp(Math.log(lo) - pad), Math.exp(Math.log(hi) + pad)];
82+
});
83+
const sx = $derived(linear(ext, [m.left, Math.max(m.left + 10, width - m.right)], true));
84+
const CANDIDATES = [0.05, 0.1, 0.2, 0.25, 0.33, 0.5, 0.67, 0.8, 1, 1.25, 1.5, 2, 3, 4, 5, 10, 20];
85+
const xt = $derived.by(() => {
86+
const inside = CANDIDATES.filter((v) => v >= ext[0] && v <= ext[1]);
87+
const room = Math.max(3, Math.floor((width - m.left - m.right) / 48));
88+
if (inside.length <= room) return inside;
89+
const keep = [0.1, 0.2, 0.5, 1, 2, 5, 10].filter((v) => v >= ext[0] && v <= ext[1]);
90+
return keep.length >= 2 ? keep : inside.filter((_, i) => i % 2 === 0);
91+
});
92+
const off = (k: number) => (series.length > 1 ? (k - (series.length - 1) / 2) * 9 : 0);
93+
94+
let tip = $state<TipState | null>(null);
95+
function show(e: PointerEvent, i: number) {
96+
const wrap = (e.currentTarget as Element).closest('.wrap')!.getBoundingClientRect();
97+
tip = {
98+
x: e.clientX - wrap.left,
99+
y: e.clientY - wrap.top,
100+
title: rows[i],
101+
lines: series.map((s) => {
102+
const v = s.values[i];
103+
return {
104+
label: series.length > 1 ? s.name : undefined,
105+
value: v ? `${format(v.est)} (${format(v.lo)}–${format(v.hi)})` : '–',
106+
color: s.color
107+
};
108+
})
109+
};
110+
}
111+
</script>
112+
113+
<Legend items={series.map((s) => ({ label: s.name, color: s.color, kind: 'dot' as const }))} />
114+
<div class="wrap" bind:clientWidth={width}>
115+
<svg
116+
class="chart-svg"
117+
viewBox={`0 0 ${width} ${h}`}
118+
{width}
119+
height={h}
120+
role="img"
121+
aria-label={`${xLabel || 'Estimates'} with 95% intervals`}
122+
onpointerleave={() => (tip = null)}
123+
>
124+
{#each xt as t (t)}
125+
<line x1={sx(t)} x2={sx(t)} y1={m.top} y2={h - m.bottom} stroke="var(--grid)" />
126+
<text x={sx(t)} y={h - m.bottom + 15} text-anchor="middle" font-size="11" fill="var(--muted)"
127+
>{format(t)}</text
128+
>
129+
{/each}
130+
<line
131+
x1={sx(ref)}
132+
x2={sx(ref)}
133+
y1={m.top}
134+
y2={h - m.bottom}
135+
stroke="var(--axis)"
136+
stroke-dasharray="4 3"
137+
/>
138+
{#each rows as row, i (i)}
139+
{@const cy = m.top + band * i + band / 2}
140+
<g>
141+
<rect
142+
x={m.left}
143+
y={m.top + band * i}
144+
width={Math.max(0, width - m.left - m.right)}
145+
height={band}
146+
fill="transparent"
147+
role="presentation"
148+
onpointermove={(e) => show(e, i)}
149+
/>
150+
{#each series as s, k (s.name)}
151+
{@const v = s.values[i]}
152+
{#if v}
153+
<line
154+
x1={sx(v.lo)}
155+
x2={sx(v.hi)}
156+
y1={cy + off(k)}
157+
y2={cy + off(k)}
158+
stroke={s.color}
159+
stroke-width="2"
160+
stroke-linecap="round"
161+
pointer-events="none"
162+
/>
163+
<circle cx={sx(v.est)} cy={cy + off(k)} r="4" fill={s.color} pointer-events="none" />
164+
{/if}
165+
{/each}
166+
<text x={m.left - 8} y={cy} text-anchor="end" font-size="11.5" fill="var(--ink-2)">
167+
{#each labels[i] as line, j (j)}
168+
<tspan x={m.left - 8} dy={j ? '1.15em' : labels[i].length > 1 ? '-0.25em' : '0.32em'}
169+
>{line}</tspan
170+
>
171+
{/each}
172+
<title>{row}</title>
173+
</text>
174+
</g>
175+
{/each}
176+
{#if xLabel}
177+
<text
178+
x={(m.left + width - m.right) / 2}
179+
y={h - 6}
180+
text-anchor="middle"
181+
font-size="11"
182+
fill="var(--ink-2)">{xLabel}</text
183+
>
184+
{/if}
185+
</svg>
186+
<Tip {tip} {width} />
187+
</div>
188+
189+
<style>
190+
.wrap {
191+
position: relative;
192+
width: 100%;
193+
}
194+
svg {
195+
display: block;
196+
overflow: visible;
197+
}
198+
</style>

‎src/lib/format.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export function monthYear(iso: string): string {
5353
return `${MONTHS[m - 1]} ${y}`;
5454
}
5555

56+
/** Day of the fieldwork season (1 Aug = day 0) → `5 Nov`. */
57+
export const seasonDayLabel = (day: number) => {
58+
const d = new Date(Date.UTC(2001, 7, 1) + day * 86_400_000);
59+
return `${d.getUTCDate()} ${MONTHS[d.getUTCMonth()]}`;
60+
};
61+
5662
/** `2013-2014` → `2013–14`. */
5763
export function season(s: string): string {
5864
return s.length === 9 ? `${s.slice(0, 4)}–${s.slice(7)}` : s;

0 commit comments

Comments
 (0)