Files
faunosteo/packages/anatomy/src/legacy/importer.ts
T

344 lines
10 KiB
TypeScript

import {
AnatomicalSheetDocumentSchema,
SHEET_DOCUMENT_SCHEMA_VERSION,
validateSheetDocument,
type AnatomicalSheetDocumentInput,
type Annotation,
type SheetView,
} from '@faunosteo/domain'
import {
mapAnnotation,
zoneNameKey,
type MappedAnnotation,
} from './annotations'
import {
mapCatalog,
parseLegacyCatalog,
type LegacyCatalog,
type MappedCatalog,
} from './catalog'
import { authoredLabelAnomalies } from './authored-labels'
import { catalogAnomalies } from './catalog-checks'
import { mapCategories, type MappedCategories } from './categories'
import { assetIdFromSha256, sha256Hex, sheetIdFor } from './ids'
import { parseAnnotatedSvg, type ParsedSvg } from './svg'
import type {
Anomaly,
AssetRecord,
EquineLegacyInput,
ImportResult,
ImportStats,
LegacyIdMap,
SourceDigest,
SvgSource,
} from './types'
import {
parseVertebralFormula,
vertebralRegions,
type LegacyVertebralFormula,
} from './vertebrae'
/**
* One-off, deterministic import of the historical equine assets into the
* first canonical anatomical sheet document (Step 4b). The result is a
* DRAFT for business review: every correction is listed in `anomalies`,
* nothing is published.
*/
export const EQUINE_SPECIES_ID = 'equine'
export const EQUINE_SHEET_VERSION = 1
export const MAIN_VIEW_ID = 'main'
export const HEAD_VIEW_ID = 'head'
interface ParsedSources {
assets: AssetRecord[]
main: ParsedSvg
head: ParsedSvg
catalog: LegacyCatalog
formula: LegacyVertebralFormula
}
interface Mapped {
annotations: MappedAnnotation[]
categories: MappedCategories
catalog: MappedCatalog
}
function assetRecord(source: SvgSource): AssetRecord {
const sha256 = sha256Hex(source.bytes)
return {
file: source.fileName,
sha256,
byte_size: source.bytes.byteLength,
svg_asset_id: assetIdFromSha256(sha256),
}
}
function textDigest(file: string, text: string): SourceDigest {
const bytes = new TextEncoder().encode(text)
return { file, sha256: sha256Hex(bytes), byte_size: bytes.byteLength }
}
function svgText(source: SvgSource): string {
return new TextDecoder('utf-8', { fatal: true }).decode(source.bytes)
}
function parseSources(input: EquineLegacyInput): ParsedSources {
return {
assets: [assetRecord(input.mainSvg), assetRecord(input.headSvg)],
main: parseAnnotatedSvg(svgText(input.mainSvg), input.mainSvg.fileName),
head: parseAnnotatedSvg(svgText(input.headSvg), input.headSvg.fileName),
catalog: parseLegacyCatalog(input.catalogJson),
formula: parseVertebralFormula(input.vertebraeSource),
}
}
function mapSources(sources: ParsedSources): Mapped {
const catalog = mapCatalog(sources.catalog)
const zoneByNames = new Map(
catalog.zones.map((z) => [zoneNameKey(z.name.fr, z.name.en), z.id]),
)
const views = [
{ viewId: MAIN_VIEW_ID, svg: sources.main, file: sources.assets[0]! },
{ viewId: HEAD_VIEW_ID, svg: sources.head, file: sources.assets[1]! },
]
const annotations = views.flatMap(({ viewId, svg, file }) =>
svg.annotations.map((a) =>
mapAnnotation(a, { viewId, fileName: file.file, zoneByNames }),
),
)
const legacy = [...sources.main.annotations, ...sources.head.annotations]
return { annotations, categories: mapCategories(legacy), catalog }
}
function buildViews(assets: AssetRecord[]): SheetView[] {
return [
{
id: MAIN_VIEW_ID,
kind: 'main',
name: { fr: 'Vue générale', en: 'Overview' },
svg_asset_id: assets[0]!.svg_asset_id,
view_box: null,
},
{
id: HEAD_VIEW_ID,
kind: 'detail',
parent_view_id: MAIN_VIEW_ID,
name: { fr: 'Tête', en: 'Head' },
svg_asset_id: assets[1]!.svg_asset_id,
view_box: null,
},
]
}
function buildDocument(
sources: ParsedSources,
mapped: Mapped,
): AnatomicalSheetDocumentInput {
const disclaimer = sources.catalog.scope_disclaimer
return {
schema_version: SHEET_DOCUMENT_SCHEMA_VERSION,
species_id: EQUINE_SPECIES_ID,
name: { fr: 'Cheval', en: 'Horse' },
description: {
fr: 'Fiche anatomique officielle Faunosteo',
en: 'Official Faunosteo anatomical sheet',
},
scope_disclaimer: disclaimer === undefined ? {} : { fr: disclaimer },
attribution: {
authors: ['Faunosteo'],
license: null,
notes:
'Import des données historiques: Horse.svg, Horse_head.svg, ' +
`pathologies.json (schema ${sources.catalog.schema_version})`,
},
vertebral_config: { regions: vertebralRegions(sources.formula) },
references: [],
categories: mapped.categories.categories,
views: buildViews(sources.assets),
annotations: mapped.annotations.map((m) => m.annotation),
finding_zones: mapped.catalog.zones,
}
}
function buildIdMap(sources: ParsedSources, mapped: Mapped): LegacyIdMap {
return {
views: { [HEAD_VIEW_ID]: HEAD_VIEW_ID },
annotations: Object.fromEntries(
mapped.annotations.map((m) => [m.row.legacyId, m.row.newId]),
),
categories: mapped.categories.idMap,
finding_nodes: mapped.catalog.nodeIdMap,
assets: Object.fromEntries(
sources.assets.map((a) => [a.file, a.svg_asset_id]),
),
}
}
function countBy(
annotations: readonly Annotation[],
test: (a: Annotation) => boolean,
) {
return annotations.filter(test).length
}
function buildStats(
sources: ParsedSources,
document: AnatomicalSheetDocumentInput,
mapped: Mapped,
): ImportStats {
const annotations = document.annotations
const totals = mapped.catalog.zoneStats
const sum = (key: 'nodes' | 'leaves' | 'vetReferrals') =>
totals.reduce((acc, zone) => acc + zone[key], 0)
return {
legacyAnnotations:
sources.main.annotations.length + sources.head.annotations.length,
annotations: annotations.length,
annotationsByView: {
[MAIN_VIEW_ID]: countBy(annotations, (a) => a.view_id === MAIN_VIEW_ID),
[HEAD_VIEW_ID]: countBy(annotations, (a) => a.view_id === HEAD_VIEW_ID),
},
linkedAnnotations: countBy(
annotations,
(a) => a.kind !== 'detail_trigger' && a.finding_zone_id !== null,
),
triggers: countBy(annotations, (a) => a.kind === 'detail_trigger'),
views: document.views.length,
vertebralAnnotations: countBy(
annotations,
(a) => a.kind !== 'detail_trigger' && a.vertebral_region_code !== null,
),
bilateral: countBy(annotations, (a) => a.laterality === 'bilateral'),
findingZones: document.finding_zones.length,
nodes: sum('nodes'),
leaves: sum('leaves'),
vetReferrals: sum('vetReferrals'),
missingDescriptionEn: mapped.catalog.missingDescriptionEn,
sources: mapped.catalog.sources,
}
}
/** Every data-anno-id read from the SVGs must be written once. */
function assertAnnotationsKept(stats: ImportStats): void {
if (stats.legacyAnnotations === stats.annotations) return
throw new Error(
`${stats.legacyAnnotations} annotations read, ${stats.annotations} written`,
)
}
/**
* The canonical document cites a shared references registry; the legacy
* inline `sources` are not mapped. None exist today: refuse to drop any.
*/
function assertNoInlineSources(stats: ImportStats): void {
if (stats.sources === 0) return
throw new Error(
`${stats.sources} inline legacy sources found: map them to references`,
)
}
function sourceAnomalies(
sources: ParsedSources,
stats: ImportStats,
): Anomaly[] {
const out: Anomaly[] = []
const add = (code: string, subject: string, message: string) =>
out.push({ code, level: 'a_valider', subject, message })
if (stats.missingDescriptionEn > 0) {
add(
'finding.description_en.missing',
'pathologies.json',
`${stats.missingDescriptionEn} descriptions sans traduction anglaise ` +
`(clé « en » absente, rien d'inventé).`,
)
}
if (sources.catalog.scope_disclaimer !== undefined) {
add(
'document.scope_disclaimer.no_en',
'scope_disclaimer',
'Texte de périmètre légal en français seulement : traduction anglaise à rédiger.',
)
}
const range = sources.formula.variesRange ?? 'non précisée'
add(
'vertebral.cd.capped',
'Cd',
`Caudales plafonnées à ${sources.formula.counts.Cd} pour le sélecteur, ` +
`alors que l'espèce varie (${range}) selon le commentaire source.`,
)
if (stats.sources === 0) {
add(
'finding.sources.none',
'pathologies.json',
'Aucune source bibliographique sur les nœuds, alors que la règle ' +
'projet en demande au moins une par entrée.',
)
}
return out
}
function informativeNotes(): Anomaly[] {
return [
{
code: 'annotation.geometry.hitbox',
level: 'informatif',
subject: 'géométrie',
message:
'Géométrie lue sur le premier élément de chaque annotation (zone ' +
"de clic), le marqueur visuel n'est pas importé.",
},
]
}
function collectAnomalies(
sources: ParsedSources,
mapped: Mapped,
stats: ImportStats,
document: AnatomicalSheetDocumentInput,
): Anomaly[] {
return [
...authoredLabelAnomalies(document),
...sources.main.anomalies,
...sources.head.anomalies,
...mapped.annotations.flatMap((m) => m.anomalies),
...mapped.categories.anomalies,
...mapped.catalog.anomalies,
...catalogAnomalies(sources.catalog),
...sourceAnomalies(sources, stats),
...informativeNotes(),
]
}
export function importEquineLegacy(input: EquineLegacyInput): ImportResult {
const sources = parseSources(input)
const mapped = mapSources(sources)
const document = buildDocument(sources, mapped)
const parsed = AnatomicalSheetDocumentSchema.parse(document)
const stats = buildStats(sources, document, mapped)
assertAnnotationsKept(stats)
assertNoInlineSources(stats)
return {
document,
legacyIdMap: buildIdMap(sources, mapped),
assets: sources.assets,
sourceDigests: [
textDigest('pathologies.json', input.catalogJson),
textDigest('vertebrae.ts', input.vertebraeSource),
],
sheetId: sheetIdFor(EQUINE_SPECIES_ID, EQUINE_SHEET_VERSION),
viewBoxes: {
[MAIN_VIEW_ID]: sources.main.viewBox,
[HEAD_VIEW_ID]: sources.head.viewBox,
},
annotationRows: mapped.annotations.map((m) => m.row),
regionInferences: mapped.annotations.flatMap((m) =>
m.region ? [m.region] : [],
),
zoneStats: mapped.catalog.zoneStats,
stats,
anomalies: collectAnomalies(sources, mapped, stats, document),
issues: validateSheetDocument(parsed),
}
}