feat(anatomy): before/after review drawings per view, questions of every level in the report, tests on the real changeset
This commit is contained in:
1 parent
6870d87910
commit
1a96a1555c
7 files changed
+348
-10
No files matched your search
@@ -13,6 +13,12 @@ export {
|
||||
type DraftInput,
|
||||
type DraftResult,
|
||||
} from './pipeline'
|
||||
export {
|
||||
REVIEW_OVERLAY_DIR,
|
||||
overlayMarkers,
|
||||
renderReviewOverlay,
|
||||
type OverlayInput,
|
||||
} from './overlay'
|
||||
export { renderReviewReport, type ReviewReportInput } from './review-report'
|
||||
export {
|
||||
ASSET_CHANGE_ID,
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
import {
|
||||
DOMParser,
|
||||
XMLSerializer,
|
||||
type Document,
|
||||
type Element,
|
||||
} from '@xmldom/xmldom'
|
||||
import type { AnatomicalSheetDocumentInput, Geometry } from '@faunosteo/domain'
|
||||
import { isDeepStrictEqual } from 'node:util'
|
||||
|
||||
/**
|
||||
* Before/after drawing of one view for the professional review: the
|
||||
* cleaned anatomy, the curated annotations as an SVG layer, and the
|
||||
* baseline position of every moved or removed annotation beside it
|
||||
* (dashed). Pure data, deterministic, opens in any browser.
|
||||
*/
|
||||
|
||||
export const REVIEW_OVERLAY_DIR = 'review'
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg'
|
||||
const LAYER_ID = 'review-annotations'
|
||||
|
||||
type Doc = AnatomicalSheetDocumentInput
|
||||
type Annotation = Doc['annotations'][number]
|
||||
type Status = 'unchanged' | 'moved' | 'added' | 'removed'
|
||||
|
||||
const STYLE: Record<Status, Record<string, string>> = {
|
||||
unchanged: { stroke: '#1d4ed8', fill: '#1d4ed8', 'fill-opacity': '0.12' },
|
||||
moved: { stroke: '#15803d', fill: '#15803d', 'fill-opacity': '0.2' },
|
||||
added: { stroke: '#15803d', fill: '#15803d', 'fill-opacity': '0.2' },
|
||||
// fill-opacity rather than fill="none": some rasterizers drop unfilled shapes.
|
||||
removed: {
|
||||
stroke: '#b91c1c',
|
||||
fill: '#b91c1c',
|
||||
'fill-opacity': '0.04',
|
||||
'stroke-dasharray': '8 6',
|
||||
},
|
||||
}
|
||||
|
||||
interface Marker {
|
||||
id: string
|
||||
geometry: Geometry
|
||||
status: Status
|
||||
}
|
||||
|
||||
function withGeometry(annotations: readonly Annotation[], viewId: string) {
|
||||
return annotations.filter((a) => a.view_id === viewId && a.geometry !== null)
|
||||
}
|
||||
|
||||
/** Curated annotations with their status, then the baseline shapes they left. */
|
||||
export function overlayMarkers(
|
||||
before: Doc,
|
||||
after: Doc,
|
||||
viewId: string,
|
||||
): Marker[] {
|
||||
const previous = new Map(
|
||||
withGeometry(before.annotations, viewId).map((a) => [a.id, a.geometry!]),
|
||||
)
|
||||
const markers: Marker[] = []
|
||||
for (const a of withGeometry(after.annotations, viewId)) {
|
||||
const old = previous.get(a.id)
|
||||
const status: Status =
|
||||
old === undefined
|
||||
? 'added'
|
||||
: isDeepStrictEqual(old, a.geometry)
|
||||
? 'unchanged'
|
||||
: 'moved'
|
||||
markers.push({ id: a.id, geometry: a.geometry!, status })
|
||||
if (status === 'moved') {
|
||||
markers.push({ id: a.id, geometry: old!, status: 'removed' })
|
||||
}
|
||||
}
|
||||
const kept = new Set(after.annotations.map((a) => a.id))
|
||||
for (const [id, geometry] of previous) {
|
||||
if (!kept.has(id)) markers.push({ id, geometry, status: 'removed' })
|
||||
}
|
||||
return markers
|
||||
}
|
||||
|
||||
function shapeElement(doc: Document, geometry: Geometry): Element {
|
||||
switch (geometry.type) {
|
||||
case 'circle': {
|
||||
const el = doc.createElementNS(SVG_NS, 'circle')
|
||||
el.setAttribute('cx', String(geometry.cx))
|
||||
el.setAttribute('cy', String(geometry.cy))
|
||||
el.setAttribute('r', String(geometry.r))
|
||||
return el
|
||||
}
|
||||
case 'polygon': {
|
||||
const el = doc.createElementNS(SVG_NS, 'polygon')
|
||||
el.setAttribute(
|
||||
'points',
|
||||
geometry.points.map(([x, y]) => `${x},${y}`).join(' '),
|
||||
)
|
||||
return el
|
||||
}
|
||||
case 'path': {
|
||||
const el = doc.createElementNS(SVG_NS, 'path')
|
||||
el.setAttribute('d', geometry.d)
|
||||
return el
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function labelAnchor(geometry: Geometry): [number, number] | null {
|
||||
if (geometry.type === 'circle') {
|
||||
return [geometry.cx, geometry.cy - geometry.r - 4]
|
||||
}
|
||||
if (geometry.type === 'polygon') {
|
||||
const n = geometry.points.length
|
||||
const [sx, sy] = geometry.points.reduce(
|
||||
([ax, ay], [x, y]) => [ax + x, ay + y],
|
||||
[0, 0],
|
||||
)
|
||||
return [sx / n, sy / n]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function markerGroup(
|
||||
doc: Document,
|
||||
layer: Element,
|
||||
marker: Marker,
|
||||
fontSize: number,
|
||||
) {
|
||||
const group = doc.createElementNS(SVG_NS, 'g')
|
||||
group.setAttribute('data-review-id', marker.id)
|
||||
group.setAttribute('data-review-status', marker.status)
|
||||
const shape = shapeElement(doc, marker.geometry)
|
||||
for (const [k, v] of Object.entries(STYLE[marker.status])) {
|
||||
shape.setAttribute(k, v)
|
||||
}
|
||||
shape.setAttribute('stroke-width', String(fontSize / 8))
|
||||
const title = doc.createElementNS(SVG_NS, 'title')
|
||||
title.appendChild(doc.createTextNode(`${marker.id} (${marker.status})`))
|
||||
group.appendChild(title)
|
||||
group.appendChild(shape)
|
||||
const anchor = labelAnchor(marker.geometry)
|
||||
if (anchor && marker.status !== 'removed') {
|
||||
const text = doc.createElementNS(SVG_NS, 'text')
|
||||
text.setAttribute('x', String(anchor[0]))
|
||||
text.setAttribute('y', String(anchor[1]))
|
||||
text.setAttribute('text-anchor', 'middle')
|
||||
text.setAttribute('font-family', 'sans-serif')
|
||||
text.setAttribute('font-size', String(fontSize))
|
||||
text.setAttribute('fill', STYLE[marker.status].stroke!)
|
||||
text.appendChild(doc.createTextNode(marker.id))
|
||||
group.appendChild(text)
|
||||
}
|
||||
layer.appendChild(group)
|
||||
}
|
||||
|
||||
function legend(
|
||||
doc: Document,
|
||||
layer: Element,
|
||||
[x, y]: [number, number],
|
||||
fontSize: number,
|
||||
) {
|
||||
const lines = [
|
||||
['unchanged', 'bleu : inchangé'],
|
||||
[
|
||||
'moved',
|
||||
'vert : déplacé ou ajouté (pointillé rouge = position précédente)',
|
||||
],
|
||||
['removed', 'rouge pointillé : retiré'],
|
||||
] as const
|
||||
lines.forEach(([status, label], i) => {
|
||||
const text = doc.createElementNS(SVG_NS, 'text')
|
||||
text.setAttribute('x', String(x))
|
||||
text.setAttribute('y', String(y + (i + 1) * fontSize * 1.3))
|
||||
text.setAttribute('font-family', 'sans-serif')
|
||||
text.setAttribute('font-size', String(fontSize))
|
||||
text.setAttribute('fill', STYLE[status].stroke!)
|
||||
text.appendChild(doc.createTextNode(label))
|
||||
layer.appendChild(text)
|
||||
})
|
||||
}
|
||||
|
||||
export interface OverlayInput {
|
||||
cleanedSvg: string
|
||||
before: Doc
|
||||
after: Doc
|
||||
viewId: string
|
||||
}
|
||||
|
||||
/** The review drawing of one view, framed on that view's box. */
|
||||
export function renderReviewOverlay(input: OverlayInput): string {
|
||||
const doc = new DOMParser().parseFromString(input.cleanedSvg, 'image/svg+xml')
|
||||
const root = doc.documentElement
|
||||
if (!root || root.tagName !== 'svg') throw new Error('not an SVG document')
|
||||
const view = input.after.views.find((v) => v.id === input.viewId)
|
||||
if (!view) throw new Error(`unknown view "${input.viewId}"`)
|
||||
if (view.view_box) {
|
||||
const { min_x, min_y, width, height } = view.view_box
|
||||
root.setAttribute('viewBox', `${min_x} ${min_y} ${width} ${height}`)
|
||||
}
|
||||
root.removeAttribute('width')
|
||||
root.removeAttribute('height')
|
||||
const box = (root.getAttribute('viewBox') ?? '0 0 1000 1000')
|
||||
.split(/[\s,]+/)
|
||||
.map(Number)
|
||||
// ~1% of the frame width, never below 10 units (detail views are small).
|
||||
const fontSize = Math.max(Math.round(box[2]! / 90), 10)
|
||||
const layer = doc.createElementNS(SVG_NS, 'g')
|
||||
layer.setAttribute('id', LAYER_ID)
|
||||
for (const marker of overlayMarkers(
|
||||
input.before,
|
||||
input.after,
|
||||
input.viewId,
|
||||
)) {
|
||||
markerGroup(doc, layer, marker, fontSize)
|
||||
}
|
||||
legend(doc, layer, [box[0]! + fontSize, box[1]!], fontSize)
|
||||
root.appendChild(layer)
|
||||
return new XMLSerializer().serializeToString(doc)
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import type { Anomaly, AssetRecord, ImportResult } from '../legacy/types'
|
||||
import { applyChangeset, curatedIssues } from './apply'
|
||||
import { cleanedAsset, stripLegacyAnnotations } from './assets'
|
||||
import { renderChangelog } from './changelog'
|
||||
import { REVIEW_OVERLAY_DIR, renderReviewOverlay } from './overlay'
|
||||
import { renderReviewReport } from './review-report'
|
||||
import {
|
||||
ASSET_CHANGE_ID,
|
||||
@@ -41,6 +42,8 @@ export interface DraftInput {
|
||||
|
||||
export interface DraftResult {
|
||||
document: AnatomicalSheetDocumentInput
|
||||
/** Legacy import the changeset was applied to. */
|
||||
baseline: AnatomicalSheetDocumentInput
|
||||
records: ChangeRecord[]
|
||||
changes: ChangeSummary[]
|
||||
assets: AssetRecord[]
|
||||
@@ -112,6 +115,7 @@ export function buildEquineDraft(input: DraftInput): DraftResult {
|
||||
const result = applyChangeset(baseline, input.references, { changes })
|
||||
return {
|
||||
document: result.document,
|
||||
baseline,
|
||||
records: result.records,
|
||||
changes: changes.map(changeSummary),
|
||||
assets: [asset],
|
||||
@@ -123,6 +127,21 @@ export function buildEquineDraft(input: DraftInput): DraftResult {
|
||||
}
|
||||
}
|
||||
|
||||
/** One review drawing per view, under review/annotations-<view>.svg. */
|
||||
function reviewOverlays(draft: DraftResult): Record<string, string> {
|
||||
const files: Record<string, string> = {}
|
||||
for (const view of draft.document.views) {
|
||||
files[`${REVIEW_OVERLAY_DIR}/annotations-${view.id}.svg`] =
|
||||
renderReviewOverlay({
|
||||
cleanedSvg: draft.cleanedSvg,
|
||||
before: draft.baseline,
|
||||
after: draft.document,
|
||||
viewId: view.id,
|
||||
})
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
/** Text artifacts of the draft folder, keyed by file name. */
|
||||
export async function renderDraftArtifacts(
|
||||
draft: DraftResult,
|
||||
@@ -134,6 +153,7 @@ export async function renderDraftArtifacts(
|
||||
return {
|
||||
'document.json': documentJson,
|
||||
'assets.json': await formatJson(draft.assets),
|
||||
...reviewOverlays(draft),
|
||||
'CHANGELOG.md': await formatMarkdown(changelog),
|
||||
'REVIEW-REPORT.md': await formatMarkdown(review),
|
||||
[DRAFT_SEED_SQL_FILE]: renderDraftSeedSql({
|
||||
|
||||
@@ -160,7 +160,7 @@ function byPriorityThenId(a: ChangeSummary, b: ChangeSummary): number {
|
||||
|
||||
const professionalDecisions: Section = (input) => {
|
||||
const questions = input.changes
|
||||
.filter((c) => c.level === 'C')
|
||||
.filter((c) => c.question !== undefined)
|
||||
.sort(byPriorityThenId)
|
||||
if (questions.length === 0) return 'Aucune question ouverte.'
|
||||
return questions
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { DOMParser } from '@xmldom/xmldom'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { overlayMarkers, renderReviewOverlay } from '../src'
|
||||
import { miniSheet } from './fixtures/mini-sheet'
|
||||
|
||||
const DRAWING =
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="10">' +
|
||||
'<path d="M0 0h10"/></svg>'
|
||||
|
||||
function moved() {
|
||||
const after = miniSheet()
|
||||
const hip = after.annotations.find((a) => a.id === 'hip')!
|
||||
hip.geometry = { type: 'circle', cx: 50, cy: 60, r: 3 }
|
||||
after.annotations.push({
|
||||
...hip,
|
||||
id: 'knee',
|
||||
geometry: {
|
||||
type: 'polygon',
|
||||
points: [
|
||||
[0, 0],
|
||||
[10, 0],
|
||||
[5, 5],
|
||||
],
|
||||
},
|
||||
})
|
||||
return after
|
||||
}
|
||||
|
||||
describe('review overlay', () => {
|
||||
it('marks moved annotations with their previous position', () => {
|
||||
const markers = overlayMarkers(miniSheet(), moved(), 'main')
|
||||
const byId = (id: string) => markers.filter((m) => m.id === id)
|
||||
expect(byId('hip').map((m) => m.status)).toEqual(['moved', 'removed'])
|
||||
expect(byId('knee').map((m) => m.status)).toEqual(['added'])
|
||||
expect(markers.every((m) => m.geometry !== null)).toBe(true)
|
||||
})
|
||||
|
||||
it('reports an annotation dropped by the changeset', () => {
|
||||
const after = miniSheet()
|
||||
after.annotations = after.annotations.filter((a) => a.id !== 'hip')
|
||||
const markers = overlayMarkers(miniSheet(), after, 'main')
|
||||
expect(markers.find((m) => m.id === 'hip')?.status).toBe('removed')
|
||||
})
|
||||
|
||||
it('renders a framed SVG with one group per marker and a legend', () => {
|
||||
const svg = renderReviewOverlay({
|
||||
cleanedSvg: DRAWING,
|
||||
before: miniSheet(),
|
||||
after: moved(),
|
||||
viewId: 'main',
|
||||
})
|
||||
const root = new DOMParser().parseFromString(
|
||||
svg,
|
||||
'image/svg+xml',
|
||||
).documentElement!
|
||||
expect(root.getAttribute('width')).toBeNull()
|
||||
const groups = Array.from(root.getElementsByTagName('g')).filter((g) =>
|
||||
g.hasAttribute('data-review-id'),
|
||||
)
|
||||
expect(groups.map((g) => g.getAttribute('data-review-status'))).toEqual([
|
||||
'moved',
|
||||
'removed',
|
||||
'added',
|
||||
])
|
||||
expect(svg).toContain('stroke-dasharray')
|
||||
expect(svg).toContain('inchangé')
|
||||
expect(svg).toContain('<path d="M0 0h10"/>')
|
||||
})
|
||||
|
||||
it('frames a detail view on its view box', () => {
|
||||
const after = moved()
|
||||
const svg = renderReviewOverlay({
|
||||
cleanedSvg: DRAWING,
|
||||
before: miniSheet(),
|
||||
after,
|
||||
viewId: after.views[1]!.id,
|
||||
})
|
||||
const box = after.views[1]!.view_box!
|
||||
expect(svg).toContain(
|
||||
`viewBox="${box.min_x} ${box.min_y} ${box.width} ${box.height}"`,
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -46,9 +46,7 @@ describe('equine working draft (real sources + committed curation)', () => {
|
||||
expect(draft.sheetId).toBe(result.sheetId)
|
||||
})
|
||||
|
||||
it('keeps the catalog volumes and carries one drawing', () => {
|
||||
expect(draft.statsAfter.nodes).toBe(draft.statsBefore.nodes)
|
||||
expect(draft.statsAfter.leaves).toBe(draft.statsBefore.leaves)
|
||||
it('carries one drawing and the curated content', () => {
|
||||
expect(draft.statsBefore.assets).toBe(2)
|
||||
expect(draft.statsAfter.assets).toBe(1)
|
||||
expect(draft.assets).toHaveLength(1)
|
||||
@@ -62,13 +60,22 @@ describe('equine working draft (real sources + committed curation)', () => {
|
||||
(r) => r.code === 'Cd',
|
||||
)
|
||||
expect(cd!.count_range).toEqual({ min: 15, max: 21 })
|
||||
// Carpus zone added, linked from its annotation (CHG carpus_zone).
|
||||
const carpus = draft.document.annotations.find((a) => a.id === 'carpus')
|
||||
expect(carpus).toMatchObject({ finding_zone_id: 'carpus', kind: 'point' })
|
||||
expect(draft.document.finding_zones.some((z) => z.id === 'carpus')).toBe(
|
||||
true,
|
||||
)
|
||||
// Node volume: 792 legacy − SSB duplicates − absent sutures + carpus & flags.
|
||||
expect(draft.statsAfter.nodes).toBe(811)
|
||||
expect(draft.statsAfter.descriptionsFrOnly).toBe(0)
|
||||
})
|
||||
|
||||
it('lists CHG-000 and CHG-001 in the CHANGELOG', () => {
|
||||
it('lists CHG-000 and the Cd range change in the CHANGELOG', () => {
|
||||
const changelog = artifacts['CHANGELOG.md']!
|
||||
expect(changelog).toContain('## Comparaison quantitative')
|
||||
expect(changelog).toMatch(/\| `CHG-000` .*`view:head\.view_box`/)
|
||||
expect(changelog).toMatch(/\| `CHG-001` .*`region:Cd\.count_range`/)
|
||||
expect(changelog).toMatch(/\| `CHG-\d{3}` .*`region:Cd\.count_range`/)
|
||||
})
|
||||
|
||||
it('writes the ten review sections in order', () => {
|
||||
|
||||
@@ -27,11 +27,19 @@ const CHANGE = {
|
||||
describe('curation schemas', () => {
|
||||
it('parse the committed curation files', async () => {
|
||||
const changeset = ChangesetSchema.parse(await committed('changes.json'))
|
||||
expect(changeset.changes.map((c) => [c.id, c.priority])).toEqual([
|
||||
['CHG-001', 'medium'],
|
||||
])
|
||||
const ids = changeset.changes.map((c) => c.id)
|
||||
expect(ids[0]).toBe('CHG-001')
|
||||
expect(ids).toEqual([...ids].sort())
|
||||
expect(new Set(ids).size).toBe(ids.length)
|
||||
expect(changeset.changes.every((c) => c.level !== 'C' || c.question)).toBe(
|
||||
true,
|
||||
)
|
||||
const refs = ReferencesFileSchema.parse(await committed('references.json'))
|
||||
expect(refs.references.map((r) => r.id)).toEqual(['legacy-catalog'])
|
||||
const refIds = refs.references.map((r) => r.id)
|
||||
expect(refIds).toContain('legacy-catalog')
|
||||
expect(new Set(refIds).size).toBe(refIds.length)
|
||||
const cited = new Set(changeset.changes.flatMap((c) => c.reference_ids))
|
||||
for (const id of cited) expect(refIds).toContain(id)
|
||||
})
|
||||
|
||||
it.each([
|
||||
|
||||
Reference in new issue
Block a user