diff --git a/AGENTS.md b/AGENTS.md index 3707d20..b0670a6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -111,6 +111,7 @@ samples/fixture/ synthetic workbooks for tests/CI (committed) - 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. - Fit diagnostics (#125, `analytics/modelDiagnostics.ts`) reuse each fit's own rows, design matrix, counts and offsets, so they can't drift from the model. NB2 variance is μ + μ²/θ; P(0) falls back to Poisson when θ is infinite. Calibration rates are summed sharks over summed net-hours per group of sets. They are in-sample checks: not forecasting skill, and no leave-one-season-out (season is a fixed effect). Only rounded columns reach the page; keep it that way (the first draft sent 500 kB of JSON). - `glm.ts` is generic (IRLS + ML θ, Wald intervals via `contrast`); reuse it rather than writing another fitter. +- The synthetic fixture's second season (2022-23: 200 sets, lemons at Vaiane and Haapiti) is sized so all three fits converge in unit tests and e2e (#153); `catchModel.test.ts` fails if a threshold change or a fixture edit stops that. - `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. diff --git a/e2e/model-diagnostics.spec.ts b/e2e/model-diagnostics.spec.ts index 5182702..c0becec 100644 --- a/e2e/model-diagnostics.spec.ts +++ b/e2e/model-diagnostics.spec.ts @@ -1,14 +1,9 @@ import { expect, test } from '@playwright/test'; import AxeBuilder from '@axe-core/playwright'; -test('model fit checks follow the catch model', async ({ page }) => { +test('model fit checks render and pass axe for every model', async ({ page }) => { await page.goto('/drivers'); const summary = page.getByText('How well does the model describe the catches?', { exact: true }); - // The synthetic fixture has too few complete sets to fit the model, so there is nothing to check. - if (!(await page.getByRole('heading', { name: 'Sea temperature, step by step' }).count())) { - await expect(summary).toHaveCount(0); - return; - } await summary.click(); const model = page.getByRole('combobox', { name: 'Model', exact: true }); await expect(model).toBeVisible(); diff --git a/samples/fixture/Physioshark_Shark Log 2022-2023.xlsx b/samples/fixture/Physioshark_Shark Log 2022-2023.xlsx new file mode 100644 index 0000000..2c7d3bd Binary files /dev/null and b/samples/fixture/Physioshark_Shark Log 2022-2023.xlsx differ diff --git a/scripts/make-fixture.mjs b/scripts/make-fixture.mjs index 7bb837b..fbb6d13 100644 --- a/scripts/make-fixture.mjs +++ b/scripts/make-fixture.mjs @@ -271,3 +271,104 @@ writeFileSync( join(LOG_DIR, '2021-12-20', '0F1E 2D3C 4B5A 69-20211220 101500.csv'), env.join('\n') + '\n' ); + +// --- a second, larger synthetic season (#153) ------------------------------------------------- +// The catch model needs at least 200 complete sets over two seasons, from nurseries with at least +// 30 sets each, and its species models need two nurseries with at least 10 of that species. One +// set a night for 200 nights covers all five sites 40 times; lemons turn up at Vaiane and Haapiti. +// Its own random stream, so the 2021-22 workbook and the loggers above stay exactly as they were. +{ + let seed2 = 153; + const rand2 = () => (seed2 = (seed2 * 1103515245 + 12345) % 2147483648) / 2147483648; + const pick2 = (xs) => xs[Math.floor(rand2() * xs.length)]; + const sets2 = [setRows[0].slice()]; + const cm2 = [cm[0].slice()]; + const na2 = [cm[0].slice()]; + const tags2 = []; + const count = { Cm: 0, Na: 0 }; + const visits2 = {}; + const LEMONS = { Vaiane: 0.6, Haapiti: 0.4 }; + for (let i = 0; i < 200; i++) { + const site = SITES[i % SITES.length]; + visits2[site] = (visits2[site] ?? 0) + 1; + const day = new Date(Date.UTC(2022, 8, 1) + i * 86_400_000).toISOString().slice(0, 10); + const start = 17 * 60 + Math.floor(rand2() * 40); + const end = start + 140 + Math.floor(rand2() * 60); + // Clumped like real catches (mostly quiet nights, a few busy ones, so θ stays finite), and + // busy nights get rarer as the season goes on, so the day-of-season curve has a trend. + const busy = rand2() < (i < 100 ? 0.3 : 0.15); + const catches = busy ? 3 + Math.floor(rand2() * 7) : Math.floor(rand2() * 3); + let nCm = 0; + let nNa = 0; + for (let k = 0; k < catches; k++) { + const sp = rand2() < (LEMONS[site] ?? 0) ? 'Na' : 'Cm'; + // Recaptures keep their species and sex and grow about 0.3 mm a day, so this season adds + // no identity conflicts to the Data quality page (2021-22 keeps its handful). + const recap = tags2.length > 10 && rand2() < 0.15; + const prev = recap ? pick2(tags2) : null; + const species = prev ? prev.sp : sp; + const tl = prev + ? prev.tl + Math.round(0.3 * (i - prev.i)) + : Math.round(species === 'Cm' ? 540 + rand2() * 90 : 620 + rand2() * 90); + const sex = prev ? prev.sex : rand2() < 0.5 ? 'F' : 'M'; + const tag = prev ? prev.tag : String(955000009100000 + tags2.length); + if (prev) Object.assign(prev, { tl, i }); + else tags2.push({ tag, sp, sex, tl, i }); + const fl = Math.round(tl * 0.82); + const mass = Math.round(3.2e-9 * tl ** 3.1 * (0.9 + rand2() * 0.2) * 1000) / 1000; + const open = i < 120 && rand2() < 0.5; + (species === 'Cm' ? cm2 : na2).push([ + site, + i + 1, + visits2[site], + D(day), + ++count[species], + recap ? 2 : 1, + tag, + species, + open ? 'Neonate' : 'Juvenile', + sex, + 'Yes', + Math.round(fl * 0.88), + fl, + tl, + Math.round(tl * 0.36), + Math.round(tl * 0.33), + Math.round(tl * 0.12), + mass, + T(start + 20 + k * 25), + T(start + 30 + k * 25), + open ? 'Open' : 'Closed', + 'Yes', + null, + 'no', + 'no sample', + 'na' + ]); + if (species === 'Cm') nCm++; + else nNa++; + } + sets2.push([ + i + 1, + site, + visits2[site], + D(day), + `${pick2(CREW)}, ${pick2(CREW)}, ${pick2(CREW)}`, + T(start), + T(end), + T(end - start), + nCm, + nNa, + nCm + nNa, + null, + null, + pick2(['Flooding', 'Ebbing']), + null + ]); + } + workbook('Physioshark_Shark Log 2022-2023.xlsx', { + 'Set Log': sets2, + 'Cm Log Juv': cm2, + 'Na Log Juv': na2 + }); +} diff --git a/src/lib/server/analytics/catchModel.test.ts b/src/lib/server/analytics/catchModel.test.ts new file mode 100644 index 0000000..6fb7b9a --- /dev/null +++ b/src/lib/server/analytics/catchModel.test.ts @@ -0,0 +1,16 @@ +import { expect, it } from 'vitest'; +import { dataset } from '../data'; +import { catchModel } from './catchModel'; + +// The synthetic fixture is sized for the model (#153), so /drivers renders its model section, +// fit diagnostics and selection chart in CI. If this fails, the e2e checks of those charts are +// silently skipped: grow the fixture (scripts/make-fixture.mjs) rather than loosening this. +it('fits all three catch models on the synthetic fixture', () => { + const m = catchModel(dataset()); + expect(m).not.toBeNull(); + expect(m!.seasons.length).toBeGreaterThanOrEqual(2); + expect(m!.cm?.n).toBeGreaterThan(0); + expect(m!.na?.n).toBeGreaterThan(0); + expect(Number.isFinite(m!.all.theta)).toBe(true); + for (const g of m!.diagnostics) expect(g.data?.n).toBeGreaterThan(0); +}); diff --git a/src/lib/server/import/import.test.ts b/src/lib/server/import/import.test.ts index f204118..cedece3 100644 --- a/src/lib/server/import/import.test.ts +++ b/src/lib/server/import/import.test.ts @@ -11,9 +11,10 @@ describe('import of the synthetic fixture', () => { const d = dataset(); it('imports sets, captures and sites', () => { - expect(d.sets.length).toBe(40); - expect(d.captures.length).toBeGreaterThan(40); - expect(d.seasons).toEqual(['2021-2022']); + // 2021-22: 40 sets; 2022-23: 200, sized for the catch model (#153). + expect(d.sets.length).toBe(240); + expect(d.captures.length).toBeGreaterThan(240); + expect(d.seasons).toEqual(['2021-2022', '2022-2023']); const haapiti = d.siteById.get('haapiti')!; expect(haapiti.lat).toBeCloseTo(-17.5542, 3); expect(haapiti.coordSource).toBe('Logger GPS sheet');