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
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ src/lib/server/data.ts loads the whole dataset into memory per import; memo()
src/lib/server/analytics common.ts (CPUE, Fulton's K, recapture pairs…) · env.ts (per-set variables)
findings.ts (live findings) · insitu.ts (logger temperature summaries)
population.ts (Schnabel abundance, von Bertalanffy growth)
glm.ts (NB/Poisson GLM, splines) · catchModel.ts (joint catch model)
src/lib/server/astro.ts sunset/twilight/moon (local formulas) · tides.ts (extrema, interpolation)
src/lib/server/holidays.ts French Polynesia public holidays (Easter-based too) + festive window
src/lib/stats.ts regression, Spearman, Welch, Wilson, binomial, histograms, box stats
src/lib/components/ ChartCard, StatTile, DataTable, FindingCard, MooreaMap (SSR SVG),
InteractiveMap (Leaflet, lazy; MooreaMap is its fallback), charts/*
(ForestPlot: rate ratios with intervals on a log scale)
src/routes/ one folder per page (+page.server.ts computes, +page.svelte renders)
scripts/ fetch-external.mjs · make-fixture.mjs · server.mjs · nightlyRelease.mjs
external/ public environmental snapshots (committed)
Expand Down Expand Up @@ -83,6 +85,12 @@ samples/fixture/ synthetic workbooks for tests/CI (committed)
- 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.
- 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.

### Catch model (#54)

- `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.

### 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
198 changes: 198 additions & 0 deletions src/lib/components/charts/ForestPlot.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<script lang="ts" module>
export interface ForestValue {
est: number;
lo: number;
hi: number;
}
export interface ForestSeries {
name: string;
color: string;
values: (ForestValue | null)[];
}
</script>

<script lang="ts">
/**
* Estimates with 95% intervals on a log scale around a reference line (rate ratios, #54).
* One row per term; several series sit side by side within a row.
*/
import { registerCsv } from './csv';
import { num } from '#lib/format';
import Legend from './Legend.svelte';
import Tip, { type TipState } from './Tip.svelte';
import { linear } from './scale';

let {
rows,
series,
ref = 1,
xLabel = '',
format = (v: number) => num(v, 2)
}: {
rows: string[];
series: ForestSeries[];
ref?: number;
xLabel?: string;
format?: (v: number) => string;
} = $props();

registerCsv(() => ({
columns: [
'term',
...series.flatMap((s) =>
series.length > 1
? [s.name, `${s.name} ci_low`, `${s.name} ci_high`]
: ['estimate', 'ci_low', 'ci_high']
)
],
rows: rows.map((r, i) => [
r,
...series.flatMap((s) => [s.values[i]?.est, s.values[i]?.lo, s.values[i]?.hi])
])
}));

let width = $state(640);
const CHAR_W = 6.2;
const longest = $derived(Math.max(0, ...rows.map((c) => c.length)));
const m = $derived({
top: 6,
right: 16,
bottom: xLabel ? 42 : 26,
left: Math.min(Math.max(110, width * 0.45), 12 + longest * CHAR_W)
});
// Labels that don't fit the margin wrap onto a second line (narrow screens).
const fit = $derived(Math.max(8, Math.floor((m.left - 12) / CHAR_W)));
const wrap = (s: string, n: number): string[] => {
if (s.length <= n) return [s];
const cut = s.lastIndexOf(' ', n);
if (cut <= 0) return [`${s.slice(0, n - 1)}…`];
const rest = s.slice(cut + 1);
return [s.slice(0, cut), rest.length > n ? `${rest.slice(0, n - 1)}…` : rest];
};
const labels = $derived(rows.map((r) => wrap(r, fit)));
const band = $derived(series.length > 1 || labels.some((l) => l.length > 1) ? 30 : 24);
const h = $derived(m.top + m.bottom + rows.length * band);

const all = $derived(series.flatMap((s) => s.values.filter((v): v is ForestValue => v != null)));
const ext = $derived.by<[number, number]>(() => {
const lo = Math.min(ref, ...all.map((v) => v.lo));
const hi = Math.max(ref, ...all.map((v) => v.hi));
const pad = (Math.log(hi) - Math.log(lo)) * 0.06 || 0.1;
return [Math.exp(Math.log(lo) - pad), Math.exp(Math.log(hi) + pad)];
});
const sx = $derived(linear(ext, [m.left, Math.max(m.left + 10, width - m.right)], true));
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];
const xt = $derived.by(() => {
const inside = CANDIDATES.filter((v) => v >= ext[0] && v <= ext[1]);
const room = Math.max(3, Math.floor((width - m.left - m.right) / 48));
if (inside.length <= room) return inside;
const keep = [0.1, 0.2, 0.5, 1, 2, 5, 10].filter((v) => v >= ext[0] && v <= ext[1]);
return keep.length >= 2 ? keep : inside.filter((_, i) => i % 2 === 0);
});
const off = (k: number) => (series.length > 1 ? (k - (series.length - 1) / 2) * 9 : 0);

let tip = $state<TipState | null>(null);
function show(e: PointerEvent, i: number) {
const wrap = (e.currentTarget as Element).closest('.wrap')!.getBoundingClientRect();
tip = {
x: e.clientX - wrap.left,
y: e.clientY - wrap.top,
title: rows[i],
lines: series.map((s) => {
const v = s.values[i];
return {
label: series.length > 1 ? s.name : undefined,
value: v ? `${format(v.est)} (${format(v.lo)}–${format(v.hi)})` : '–',
color: s.color
};
})
};
}
</script>

<Legend items={series.map((s) => ({ label: s.name, color: s.color, kind: 'dot' as const }))} />
<div class="wrap" bind:clientWidth={width}>
<svg
class="chart-svg"
viewBox={`0 0 ${width} ${h}`}
{width}
height={h}
role="img"
aria-label={`${xLabel || 'Estimates'} with 95% intervals`}
onpointerleave={() => (tip = null)}
>
{#each xt as t (t)}
<line x1={sx(t)} x2={sx(t)} y1={m.top} y2={h - m.bottom} stroke="var(--grid)" />
<text x={sx(t)} y={h - m.bottom + 15} text-anchor="middle" font-size="11" fill="var(--muted)"
>{format(t)}</text
>
{/each}
<line
x1={sx(ref)}
x2={sx(ref)}
y1={m.top}
y2={h - m.bottom}
stroke="var(--axis)"
stroke-dasharray="4 3"
/>
{#each rows as row, i (i)}
{@const cy = m.top + band * i + band / 2}
<g>
<rect
x={m.left}
y={m.top + band * i}
width={Math.max(0, width - m.left - m.right)}
height={band}
fill="transparent"
role="presentation"
onpointermove={(e) => show(e, i)}
/>
{#each series as s, k (s.name)}
{@const v = s.values[i]}
{#if v}
<line
x1={sx(v.lo)}
x2={sx(v.hi)}
y1={cy + off(k)}
y2={cy + off(k)}
stroke={s.color}
stroke-width="2"
stroke-linecap="round"
pointer-events="none"
/>
<circle cx={sx(v.est)} cy={cy + off(k)} r="4" fill={s.color} pointer-events="none" />
{/if}
{/each}
<text x={m.left - 8} y={cy} text-anchor="end" font-size="11.5" fill="var(--ink-2)">
{#each labels[i] as line, j (j)}
<tspan x={m.left - 8} dy={j ? '1.15em' : labels[i].length > 1 ? '-0.25em' : '0.32em'}
>{line}</tspan
>
{/each}
<title>{row}</title>
</text>
</g>
{/each}
{#if xLabel}
<text
x={(m.left + width - m.right) / 2}
y={h - 6}
text-anchor="middle"
font-size="11"
fill="var(--ink-2)">{xLabel}</text
>
{/if}
</svg>
<Tip {tip} {width} />
</div>

<style>
.wrap {
position: relative;
width: 100%;
}
svg {
display: block;
overflow: visible;
}
</style>
6 changes: 6 additions & 0 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export function monthYear(iso: string): string {
return `${MONTHS[m - 1]} ${y}`;
}

/** Day of the fieldwork season (1 Aug = day 0) → `5 Nov`. */
export const seasonDayLabel = (day: number) => {
const d = new Date(Date.UTC(2001, 7, 1) + day * 86_400_000);
return `${d.getUTCDate()} ${MONTHS[d.getUTCMonth()]}`;
};

/** `2013-2014` → `2013–14`. */
export function season(s: string): string {
return s.length === 9 ? `${s.slice(0, 4)}–${s.slice(7)}` : s;
Expand Down
Loading
Loading