+ Scar changes in the same shark
+
+ Each pair is two consecutive captures of one shark in the same season with a scar stage recorded
+ both times; captures without a stage are skipped. The days are the time between those captures,
+ not how long the scar took to heal or the shark's age: the change happened somewhere in between.
+ A last unhealed record doesn't mean the scar never healed, and pairs from one shark aren't
+ independent.
+
+ {#each groups as g (g.species)}
+ {@const name = speciesName(g.species).toLowerCase()}
+ {speciesName(g.species)}
+
+ {plural(g.pairs, 'pair')} from {plural(g.sharks, 'shark')}. Left out: {plural(
+ g.excluded.identity,
+ 'pair'
+ )} from sharks whose records disagree (sex, species or a shrinking length), {num(
+ g.excluded.chronology
+ )} with two captures on one day, {num(g.excluded.season)} spanning two seasons and {num(
+ g.excluded.lab
+ )} with a lab stay in between. {plural(g.unknown, 'capture')} had no scar stage recorded{g.missingIdentityOrDate
+ ? `, and ${plural(g.missingIdentityOrDate, 'capture')} no tag or date`
+ : ''}. {g.backward
+ ? `${plural(g.backward, 'pair goes', 'pairs go')} backwards (a healed scar later recorded as open or healing); they stay in the chart for the lab to check.`
+ : 'No pair goes backwards.'}
+
+ {#if g.pairs}
+
+
+
+
+
+ r.stats)
+ .map((r) => ({
+ name: `${r.from} → ${r.to}`,
+ color: SPECIES[g.species].color,
+ stats: r.stats
+ }))}
+ yLabel="Days between captures"
+ />
+
+
+
+ Pairs per shark: {g.contributions
+ .map((r) => `${plural(r.sharks, 'shark')} gave ${plural(r.pairs, 'pair')}`)
+ .join(', ')}.
+
+ {:else}No sharks were recorded at a known scar stage twice in one season.
{/if}
+ {/each}
+
diff --git a/src/lib/server/analytics/scarTransitions.test.ts b/src/lib/server/analytics/scarTransitions.test.ts
new file mode 100644
index 0000000..e886f08
--- /dev/null
+++ b/src/lib/server/analytics/scarTransitions.test.ts
@@ -0,0 +1,67 @@
+import { expect, it } from 'vitest';
+import type { Dataset, Capture } from '../data';
+import { scarTransitions } from './scarTransitions';
+
+const cap = (id: string, date: string, stage: string | null, extra = {}) =>
+ ({
+ individualId: id,
+ date,
+ umbilical: stage,
+ season: '2020-2021',
+ species: 'Cm',
+ kind: 'field',
+ ...extra
+ }) as Capture;
+it('skips unknown stages but retains backward transitions and per-shark contributions', () => {
+ const d = {
+ captures: [
+ cap('a', '2020-10-01', 'open'),
+ cap('a', '2020-10-03', null),
+ cap('a', '2020-10-08', 'closed'),
+ cap('a', '2020-10-10', 'semi')
+ ],
+ individuals: [],
+ labSharks: []
+ };
+ const [r] = scarTransitions(d);
+ expect(r).toMatchObject({
+ pairs: 2,
+ sharks: 1,
+ unknown: 1,
+ backward: 1,
+ contributions: [{ pairs: 2, sharks: 1 }]
+ });
+ // One open → healed pair: counted, but too few for a box.
+ expect(r.rows[2]).toMatchObject({ from: 'Open', to: 'Healed', pairs: 1, stats: null });
+});
+it('draws a days-between box once a transition has five pairs', () => {
+ const d = {
+ captures: ['a', 'b', 'c', 'd', 'e'].flatMap((id, i) => [
+ cap(id, '2020-10-01', 'open'),
+ cap(id, `2020-10-${String(5 + i * 2).padStart(2, '0')}`, 'closed')
+ ]),
+ individuals: [],
+ labSharks: []
+ };
+ // Gaps of 4, 6, 8, 10 and 12 days.
+ expect(scarTransitions(d)[0].rows[2].stats?.median).toBe(8);
+});
+it('excludes conflicts, same-day observations, season boundaries and lab interruptions', () => {
+ const d = {
+ captures: ['a', 'b', 'c', 'd'].flatMap((id) => [
+ cap(id, '2020-10-01', 'open'),
+ cap(
+ id,
+ id === 'b' ? '2020-10-01' : '2020-10-08',
+ 'closed',
+ id === 'c' ? { season: '2021-2022' } : {}
+ )
+ ]),
+ individuals: [{ id: 'a', conflict: 'sex' }],
+ labSharks: [{ individualId: 'd', arrivalDate: '2020-10-03', releaseDate: '2020-10-06' }]
+ } as Pick