dicom-synth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +66 -0
- package/README.md +136 -0
- package/bin/dicom-synth-fetch.mjs +23 -0
- package/bin/dicom-synth-write.mjs +9 -0
- package/data/public-cases.json +25 -0
- package/dist/data/public-cases.json +25 -0
- package/dist/esm/dcmjs.d.js +0 -0
- package/dist/esm/index.js +209 -0
- package/dist/esm/loadDcmjs.js +22 -0
- package/dist/esm/public-fixtures/catalog.js +36 -0
- package/dist/esm/public-fixtures/fetch.js +46 -0
- package/dist/esm/publicCorpus.js +72 -0
- package/dist/esm/synthetic.js +109 -0
- package/dist/esm/syntheticFixtures/generator.js +129 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/loadDcmjs.d.ts +6 -0
- package/dist/types/public-fixtures/catalog.d.ts +22 -0
- package/dist/types/public-fixtures/fetch.d.ts +4 -0
- package/dist/types/publicCorpus.d.ts +25 -0
- package/dist/types/synthetic.d.ts +11 -0
- package/dist/types/syntheticFixtures/generator.d.ts +11 -0
- package/package.json +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Copyright 2025 Telix Pharmaceuticals Limited
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
14
|
+
|
|
15
|
+
Apache License
|
|
16
|
+
Version 2.0, January 2004
|
|
17
|
+
http://www.apache.org/licenses/
|
|
18
|
+
|
|
19
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
20
|
+
|
|
21
|
+
1. Definitions.
|
|
22
|
+
|
|
23
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
24
|
+
|
|
25
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
26
|
+
|
|
27
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
28
|
+
|
|
29
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
30
|
+
|
|
31
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
32
|
+
|
|
33
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
38
|
+
|
|
39
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
40
|
+
|
|
41
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
42
|
+
|
|
43
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
44
|
+
|
|
45
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
46
|
+
|
|
47
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
48
|
+
|
|
49
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
50
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
51
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
52
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
53
|
+
|
|
54
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
55
|
+
|
|
56
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
57
|
+
|
|
58
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
59
|
+
|
|
60
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
61
|
+
|
|
62
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
63
|
+
|
|
64
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
65
|
+
|
|
66
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# dicom-synth
|
|
2
|
+
|
|
3
|
+
Toolkit for **synthetic DICOM fixtures** and **public fixture** fetch/cache. Portable and independent of any consumer (e.g. [dicom-curate](https://github.com/clintools/dicom-curate)).
|
|
4
|
+
|
|
5
|
+
## Disclaimer
|
|
6
|
+
|
|
7
|
+
Pre-1.0.0 — APIs may change without notice.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add dicom-synth
|
|
13
|
+
pnpm add dcmjs # peer dependency (^0.51.1)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Synthetic fixtures (in memory)
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { buildSyntheticCtBuffer, SYNTHETIC_FIXTURES } from 'dicom-synth'
|
|
20
|
+
|
|
21
|
+
const buf = buildSyntheticCtBuffer('valid-uid')
|
|
22
|
+
// use buf in tests or write to disk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Variants (see `SYNTHETIC_FIXTURES` in `src/syntheticFixtures/generator.ts`):
|
|
26
|
+
|
|
27
|
+
| Variant | Purpose |
|
|
28
|
+
|---------|---------|
|
|
29
|
+
| `minimal-invalid-uid` | Invalid UID characters (many dciodvfy errors) |
|
|
30
|
+
| `valid-uid` | Numeric UIDs only (missing-module noise) |
|
|
31
|
+
| `vendor-warnings` | Numeric UIDs plus empty Laterality and zero PatientWeight |
|
|
32
|
+
|
|
33
|
+
## Write synthetic fixtures to disk
|
|
34
|
+
|
|
35
|
+
Published CLI:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pnpm dlx dicom-synth@latest dicom-synth-write ./tmp/fixtures
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Local development:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pnpm write:synthetic -- ./out/fixtures
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Public fixtures
|
|
48
|
+
|
|
49
|
+
Catalog metadata lives in `data/public-cases.json` (also importable as `dicom-synth/public-cases.json`). Fetched binaries are cached under `~/.cache/dicom-synth-testcases/<sha256>/file.dcm`.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import {
|
|
53
|
+
defaultPublicCasesPath,
|
|
54
|
+
fetchPublicCaseToCache,
|
|
55
|
+
loadCaseById,
|
|
56
|
+
} from 'dicom-synth'
|
|
57
|
+
|
|
58
|
+
const record = loadCaseById(defaultPublicCasesPath(), 'pydicom-CT-small')
|
|
59
|
+
const path = await fetchPublicCaseToCache(record)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Published CLI:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pnpm dlx dicom-synth@latest dicom-synth-fetch pydicom-CT-small
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Local development:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pnpm fetch:public-case -- pydicom-CT-small
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Use in tests (ephemeral files)
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
78
|
+
import { tmpdir } from 'node:os'
|
|
79
|
+
import { join } from 'node:path'
|
|
80
|
+
import { buildSyntheticCtBuffer } from 'dicom-synth'
|
|
81
|
+
|
|
82
|
+
const dir = await mkdtemp(join(tmpdir(), 'synth-'))
|
|
83
|
+
try {
|
|
84
|
+
await writeFile(join(dir, 'test.dcm'), buildSyntheticCtBuffer('valid-uid'))
|
|
85
|
+
// run pipeline against dir
|
|
86
|
+
} finally {
|
|
87
|
+
await rm(dir, { recursive: true, force: true })
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The same pattern works in CI when `dicom-synth` and `dcmjs` are installed; you do not need to commit `.dcm` binaries in the consumer repo.
|
|
92
|
+
|
|
93
|
+
## Source layout
|
|
94
|
+
|
|
95
|
+
| Path | Responsibility |
|
|
96
|
+
|------|----------------|
|
|
97
|
+
| `src/syntheticFixtures/generator.ts` | Deterministic synthetic CT generation |
|
|
98
|
+
| `src/public-fixtures/catalog.ts` | Load bundled public case catalogue |
|
|
99
|
+
| `src/public-fixtures/fetch.ts` | Fetch, SHA-256 verify, content-addressed cache |
|
|
100
|
+
| `scripts/write-synthetic-fixtures.ts` | Dev CLI for synthetic output |
|
|
101
|
+
| `scripts/fetch-public-case.ts` | Dev CLI for public fetch |
|
|
102
|
+
| `bin/dicom-synth-write.mjs` | Published write CLI (requires `pnpm build`) |
|
|
103
|
+
| `bin/dicom-synth-fetch.mjs` | Published fetch CLI (requires `pnpm build`) |
|
|
104
|
+
|
|
105
|
+
## Development
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pnpm install
|
|
109
|
+
pnpm build
|
|
110
|
+
pnpm test
|
|
111
|
+
pnpm code:quality
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| Script | Purpose |
|
|
115
|
+
|--------|---------|
|
|
116
|
+
| `pnpm write:synthetic` | Write all synthetic fixtures to a directory |
|
|
117
|
+
| `pnpm fetch:public-case` | Fetch one public case by id |
|
|
118
|
+
| `pnpm hooks:pre-commit` | Reproduce commit hook |
|
|
119
|
+
| `pnpm hooks:pre-push` | Reproduce push hook |
|
|
120
|
+
|
|
121
|
+
Git hooks: see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
122
|
+
|
|
123
|
+
## Future development
|
|
124
|
+
|
|
125
|
+
The current release targets stable, deterministic fixtures for conformance basic conformance testing. Planned extensions include:
|
|
126
|
+
|
|
127
|
+
- **Parameterised synthetic generation** — options for instance count, dimensions, frames, transfer syntax, and modality/profile presets.
|
|
128
|
+
- **Stress and scale** — very large pixel payloads, high file counts, and multi-series study layouts for performance and memory testing.
|
|
129
|
+
- **Seeded variation** — reproducible pseudo-random datasets (fixed seed) for broader coverage without losing determinism in CI.
|
|
130
|
+
- **Conformance modes** — `strict` (standards-valid stress data) vs `edge` (intentional faults for parser and validator hardening).
|
|
131
|
+
- **Private fixture catalogues** — same SHA-256 fetch/cache pattern as public fixtures, for credentials-backed sources (e.g. S3).
|
|
132
|
+
- **Export helpers** — optional upload or packaging of generated datasets for remote CI or shared test stores.
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
Apache-2.0
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
defaultPublicCasesPath,
|
|
4
|
+
fetchPublicCaseToCache,
|
|
5
|
+
loadCasesFromJson,
|
|
6
|
+
} from '../dist/esm/index.js'
|
|
7
|
+
|
|
8
|
+
const id = process.argv[2]
|
|
9
|
+
if (!id) {
|
|
10
|
+
console.error('Usage: dicom-synth-fetch <case-id>')
|
|
11
|
+
process.exit(1)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const record = loadCasesFromJson(defaultPublicCasesPath()).find(
|
|
15
|
+
(c) => c.id === id,
|
|
16
|
+
)
|
|
17
|
+
if (!record) {
|
|
18
|
+
console.error(`Unknown case id: ${id}`)
|
|
19
|
+
process.exit(1)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const outPath = await fetchPublicCaseToCache(record)
|
|
23
|
+
console.log(outPath)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
import { writeSyntheticFixturesToDir } from '../dist/esm/index.js'
|
|
4
|
+
|
|
5
|
+
const outDir = resolve(process.argv[2] ?? './fixtures/synthetic')
|
|
6
|
+
const paths = writeSyntheticFixturesToDir(outDir)
|
|
7
|
+
for (const p of paths) {
|
|
8
|
+
console.log(`wrote ${p}`)
|
|
9
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cases": [
|
|
3
|
+
{
|
|
4
|
+
"id": "pydicom-badVR",
|
|
5
|
+
"violation_class": "invalid-vr",
|
|
6
|
+
"dciodvfy_skip": true,
|
|
7
|
+
"notes": "MIT-licensed pydicom test file. dciodvfy aborts (assertion) on this encoding; use for fetch/cache only.",
|
|
8
|
+
"source": {
|
|
9
|
+
"kind": "url",
|
|
10
|
+
"url": "https://raw.githubusercontent.com/pydicom/pydicom/main/src/pydicom/data/test_files/badVR.dcm"
|
|
11
|
+
},
|
|
12
|
+
"sha256": "d12583df0cf66146256238b4cada266956a317c0546c7516cb36f56982ce85b2"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"id": "pydicom-CT-small",
|
|
16
|
+
"violation_class": "real-world-warnings",
|
|
17
|
+
"notes": "MIT-licensed pydicom CT_small.dcm; dciodvfy reports warnings on a mostly-valid file.",
|
|
18
|
+
"source": {
|
|
19
|
+
"kind": "url",
|
|
20
|
+
"url": "https://raw.githubusercontent.com/pydicom/pydicom/main/src/pydicom/data/test_files/CT_small.dcm"
|
|
21
|
+
},
|
|
22
|
+
"sha256": "3dd31e5cc835b3f2cdd46c9da1982f59251e78518fefa8163d914631c66437d6"
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cases": [
|
|
3
|
+
{
|
|
4
|
+
"id": "pydicom-badVR",
|
|
5
|
+
"violation_class": "invalid-vr",
|
|
6
|
+
"dciodvfy_skip": true,
|
|
7
|
+
"notes": "MIT-licensed pydicom test file. dciodvfy aborts (assertion) on this encoding; use for fetch/cache only.",
|
|
8
|
+
"source": {
|
|
9
|
+
"kind": "url",
|
|
10
|
+
"url": "https://raw.githubusercontent.com/pydicom/pydicom/main/src/pydicom/data/test_files/badVR.dcm"
|
|
11
|
+
},
|
|
12
|
+
"sha256": "d12583df0cf66146256238b4cada266956a317c0546c7516cb36f56982ce85b2"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"id": "pydicom-CT-small",
|
|
16
|
+
"violation_class": "real-world-warnings",
|
|
17
|
+
"notes": "MIT-licensed pydicom CT_small.dcm; dciodvfy reports warnings on a mostly-valid file.",
|
|
18
|
+
"source": {
|
|
19
|
+
"kind": "url",
|
|
20
|
+
"url": "https://raw.githubusercontent.com/pydicom/pydicom/main/src/pydicom/data/test_files/CT_small.dcm"
|
|
21
|
+
},
|
|
22
|
+
"sha256": "3dd31e5cc835b3f2cdd46c9da1982f59251e78518fefa8163d914631c66437d6"
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/public-fixtures/catalog.ts
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
var bundledCatalogPath = join(
|
|
6
|
+
dirname(fileURLToPath(import.meta.url)),
|
|
7
|
+
"../../data/public-cases.json"
|
|
8
|
+
);
|
|
9
|
+
function defaultPublicCasesPath() {
|
|
10
|
+
return bundledCatalogPath;
|
|
11
|
+
}
|
|
12
|
+
function loadDefaultCases() {
|
|
13
|
+
return loadCasesFromJson(defaultPublicCasesPath());
|
|
14
|
+
}
|
|
15
|
+
function loadCasesFromJson(casesJsonPath) {
|
|
16
|
+
const j = JSON.parse(readFileSync(casesJsonPath, "utf8"));
|
|
17
|
+
if (!Array.isArray(j.cases)) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Invalid catalog in ${casesJsonPath}: expected "cases" array`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return j.cases;
|
|
23
|
+
}
|
|
24
|
+
function loadCaseById(casesJsonPath, id) {
|
|
25
|
+
const found = loadCasesFromJson(casesJsonPath).find((r) => r.id === id);
|
|
26
|
+
if (!found) {
|
|
27
|
+
throw new Error(`Unknown case id "${id}" in ${casesJsonPath}`);
|
|
28
|
+
}
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/public-fixtures/fetch.ts
|
|
33
|
+
import { createHash } from "node:crypto";
|
|
34
|
+
import { existsSync, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "node:fs";
|
|
35
|
+
import { homedir } from "node:os";
|
|
36
|
+
import { join as join2 } from "node:path";
|
|
37
|
+
var DEFAULT_CACHE_ROOT = join2(homedir(), ".cache", "dicom-synth-testcases");
|
|
38
|
+
function caseCachePath(sha256, root = DEFAULT_CACHE_ROOT) {
|
|
39
|
+
return join2(root, sha256, "file.dcm");
|
|
40
|
+
}
|
|
41
|
+
function verifySha256(buffer, expected) {
|
|
42
|
+
const h = createHash("sha256").update(buffer).digest("hex");
|
|
43
|
+
if (h !== expected) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`SHA256 mismatch: expected ${expected}, got ${h}. Upstream may have changed; bump metadata after review.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function fetchPublicCaseToCache(record, cacheRoot = DEFAULT_CACHE_ROOT) {
|
|
50
|
+
if (record.source.kind !== "url") {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Case ${record.id}: only url sources are supported (s3 requires credentials).`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const dest = caseCachePath(record.sha256, cacheRoot);
|
|
56
|
+
if (existsSync(dest)) {
|
|
57
|
+
const buf2 = readFileSync2(dest);
|
|
58
|
+
verifySha256(buf2, record.sha256);
|
|
59
|
+
return dest;
|
|
60
|
+
}
|
|
61
|
+
mkdirSync(join2(cacheRoot, record.sha256), { recursive: true });
|
|
62
|
+
const res = await fetch(record.source.url);
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Fetch failed ${res.status} ${res.statusText} for ${record.source.url}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
69
|
+
verifySha256(buf, record.sha256);
|
|
70
|
+
writeFileSync(dest, buf);
|
|
71
|
+
return dest;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/syntheticFixtures/generator.ts
|
|
75
|
+
import { mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
76
|
+
import { resolve } from "node:path";
|
|
77
|
+
|
|
78
|
+
// src/loadDcmjs.ts
|
|
79
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
80
|
+
function loadDcmjs() {
|
|
81
|
+
if (dcmjsNamespace.data?.DicomDict) {
|
|
82
|
+
return dcmjsNamespace;
|
|
83
|
+
}
|
|
84
|
+
const d = dcmjsDefaultImport;
|
|
85
|
+
if (d?.data?.DicomDict) {
|
|
86
|
+
return d;
|
|
87
|
+
}
|
|
88
|
+
if (d?.default?.data?.DicomDict) {
|
|
89
|
+
return d.default;
|
|
90
|
+
}
|
|
91
|
+
throw new Error(
|
|
92
|
+
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
var dcmjs = loadDcmjs();
|
|
96
|
+
|
|
97
|
+
// src/syntheticFixtures/generator.ts
|
|
98
|
+
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
99
|
+
var VARIANT_INDEX = {
|
|
100
|
+
"minimal-invalid-uid": 0,
|
|
101
|
+
"valid-uid": 1,
|
|
102
|
+
"vendor-warnings": 2
|
|
103
|
+
};
|
|
104
|
+
function uidFor(variant, role) {
|
|
105
|
+
const i = VARIANT_INDEX[variant];
|
|
106
|
+
if (variant === "minimal-invalid-uid") {
|
|
107
|
+
return `2.25.100000000000000000000000000000.${role}.${i}`;
|
|
108
|
+
}
|
|
109
|
+
const uidRoots = [
|
|
110
|
+
"1000000000000000000000000000000000001",
|
|
111
|
+
"1000000000000000000000000000000000002",
|
|
112
|
+
"1000000000000000000000000000000000003"
|
|
113
|
+
];
|
|
114
|
+
const roleSuffix = role === "study" ? 1 : role === "series" ? 2 : 3;
|
|
115
|
+
return `2.25.${uidRoots[i]}.${roleSuffix}`;
|
|
116
|
+
}
|
|
117
|
+
function naturalDataset(variant) {
|
|
118
|
+
const studyUID = uidFor(variant, "study");
|
|
119
|
+
const seriesUID = uidFor(variant, "series");
|
|
120
|
+
const sopUID = uidFor(variant, "sop");
|
|
121
|
+
const dataset = {
|
|
122
|
+
PatientName: "SYNTH^SUBJECT",
|
|
123
|
+
PatientID: "SYNTH_PID",
|
|
124
|
+
Modality: "CT",
|
|
125
|
+
SOPClassUID: CT_SOP_CLASS,
|
|
126
|
+
SOPInstanceUID: sopUID,
|
|
127
|
+
SeriesInstanceUID: seriesUID,
|
|
128
|
+
StudyInstanceUID: studyUID,
|
|
129
|
+
StudyDescription: "Synthetic study",
|
|
130
|
+
SeriesDescription: "Synthetic series",
|
|
131
|
+
Rows: 1,
|
|
132
|
+
Columns: 1,
|
|
133
|
+
BitsAllocated: 16,
|
|
134
|
+
BitsStored: 16,
|
|
135
|
+
HighBit: 15,
|
|
136
|
+
SamplesPerPixel: 1,
|
|
137
|
+
PhotometricInterpretation: "MONOCHROME2",
|
|
138
|
+
PixelRepresentation: 0,
|
|
139
|
+
PixelData: new Uint8Array([0, 0]).buffer
|
|
140
|
+
};
|
|
141
|
+
if (variant === "vendor-warnings") {
|
|
142
|
+
dataset.Laterality = "";
|
|
143
|
+
dataset.PatientWeight = "0";
|
|
144
|
+
}
|
|
145
|
+
return dataset;
|
|
146
|
+
}
|
|
147
|
+
function buildDicomDict(variant) {
|
|
148
|
+
const meta = {
|
|
149
|
+
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
150
|
+
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
151
|
+
MediaStorageSOPInstanceUID: uidFor(variant, "sop"),
|
|
152
|
+
TransferSyntaxUID: "1.2.840.10008.1.2.1",
|
|
153
|
+
ImplementationClassUID: "1.2.3.4",
|
|
154
|
+
ImplementationVersionName: "SYNTH"
|
|
155
|
+
};
|
|
156
|
+
const dicomDict = new dcmjs.data.DicomDict(
|
|
157
|
+
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(meta)
|
|
158
|
+
);
|
|
159
|
+
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
160
|
+
naturalDataset(variant)
|
|
161
|
+
);
|
|
162
|
+
return dicomDict;
|
|
163
|
+
}
|
|
164
|
+
function buildSyntheticCtBuffer(variant) {
|
|
165
|
+
return Buffer.from(
|
|
166
|
+
buildDicomDict(variant).write({ allowInvalidVRLength: true })
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
var SYNTHETIC_FIXTURES = [
|
|
170
|
+
{
|
|
171
|
+
filename: "minimal-ct-0.dcm",
|
|
172
|
+
variant: "minimal-invalid-uid",
|
|
173
|
+
description: "Minimal CT with text in UIDs (many dciodvfy errors)"
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
filename: "valid-uid-ct-0.dcm",
|
|
177
|
+
variant: "valid-uid",
|
|
178
|
+
description: "Minimal CT with numeric UIDs only (missing-module noise)"
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
filename: "vendor-warnings-ct-0.dcm",
|
|
182
|
+
variant: "vendor-warnings",
|
|
183
|
+
description: "Valid UIDs plus empty Laterality and zero PatientWeight"
|
|
184
|
+
}
|
|
185
|
+
];
|
|
186
|
+
function writeSyntheticFixturesToDir(outDir) {
|
|
187
|
+
const root = resolve(outDir);
|
|
188
|
+
mkdirSync2(root, { recursive: true });
|
|
189
|
+
const written = [];
|
|
190
|
+
for (const { filename, variant } of SYNTHETIC_FIXTURES) {
|
|
191
|
+
const path = resolve(root, filename);
|
|
192
|
+
writeFileSync2(path, buildSyntheticCtBuffer(variant));
|
|
193
|
+
written.push(path);
|
|
194
|
+
}
|
|
195
|
+
return written;
|
|
196
|
+
}
|
|
197
|
+
export {
|
|
198
|
+
SYNTHETIC_FIXTURES,
|
|
199
|
+
buildDicomDict,
|
|
200
|
+
buildSyntheticCtBuffer,
|
|
201
|
+
caseCachePath,
|
|
202
|
+
defaultPublicCasesPath,
|
|
203
|
+
fetchPublicCaseToCache,
|
|
204
|
+
loadCaseById,
|
|
205
|
+
loadCasesFromJson,
|
|
206
|
+
loadDefaultCases,
|
|
207
|
+
verifySha256,
|
|
208
|
+
writeSyntheticFixturesToDir
|
|
209
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/loadDcmjs.ts
|
|
2
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
3
|
+
function loadDcmjs() {
|
|
4
|
+
if (dcmjsNamespace.data?.DicomDict) {
|
|
5
|
+
return dcmjsNamespace;
|
|
6
|
+
}
|
|
7
|
+
const d = dcmjsDefaultImport;
|
|
8
|
+
if (d?.data?.DicomDict) {
|
|
9
|
+
return d;
|
|
10
|
+
}
|
|
11
|
+
if (d?.default?.data?.DicomDict) {
|
|
12
|
+
return d.default;
|
|
13
|
+
}
|
|
14
|
+
throw new Error(
|
|
15
|
+
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
var dcmjs = loadDcmjs();
|
|
19
|
+
export {
|
|
20
|
+
dcmjs,
|
|
21
|
+
loadDcmjs
|
|
22
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/public-fixtures/catalog.ts
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
var bundledCatalogPath = join(
|
|
6
|
+
dirname(fileURLToPath(import.meta.url)),
|
|
7
|
+
"../../data/public-cases.json"
|
|
8
|
+
);
|
|
9
|
+
function defaultPublicCasesPath() {
|
|
10
|
+
return bundledCatalogPath;
|
|
11
|
+
}
|
|
12
|
+
function loadDefaultCases() {
|
|
13
|
+
return loadCasesFromJson(defaultPublicCasesPath());
|
|
14
|
+
}
|
|
15
|
+
function loadCasesFromJson(casesJsonPath) {
|
|
16
|
+
const j = JSON.parse(readFileSync(casesJsonPath, "utf8"));
|
|
17
|
+
if (!Array.isArray(j.cases)) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Invalid catalog in ${casesJsonPath}: expected "cases" array`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return j.cases;
|
|
23
|
+
}
|
|
24
|
+
function loadCaseById(casesJsonPath, id) {
|
|
25
|
+
const found = loadCasesFromJson(casesJsonPath).find((r) => r.id === id);
|
|
26
|
+
if (!found) {
|
|
27
|
+
throw new Error(`Unknown case id "${id}" in ${casesJsonPath}`);
|
|
28
|
+
}
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
defaultPublicCasesPath,
|
|
33
|
+
loadCaseById,
|
|
34
|
+
loadCasesFromJson,
|
|
35
|
+
loadDefaultCases
|
|
36
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/public-fixtures/fetch.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
var DEFAULT_CACHE_ROOT = join(homedir(), ".cache", "dicom-synth-testcases");
|
|
7
|
+
function caseCachePath(sha256, root = DEFAULT_CACHE_ROOT) {
|
|
8
|
+
return join(root, sha256, "file.dcm");
|
|
9
|
+
}
|
|
10
|
+
function verifySha256(buffer, expected) {
|
|
11
|
+
const h = createHash("sha256").update(buffer).digest("hex");
|
|
12
|
+
if (h !== expected) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`SHA256 mismatch: expected ${expected}, got ${h}. Upstream may have changed; bump metadata after review.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function fetchPublicCaseToCache(record, cacheRoot = DEFAULT_CACHE_ROOT) {
|
|
19
|
+
if (record.source.kind !== "url") {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Case ${record.id}: only url sources are supported (s3 requires credentials).`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const dest = caseCachePath(record.sha256, cacheRoot);
|
|
25
|
+
if (existsSync(dest)) {
|
|
26
|
+
const buf2 = readFileSync(dest);
|
|
27
|
+
verifySha256(buf2, record.sha256);
|
|
28
|
+
return dest;
|
|
29
|
+
}
|
|
30
|
+
mkdirSync(join(cacheRoot, record.sha256), { recursive: true });
|
|
31
|
+
const res = await fetch(record.source.url);
|
|
32
|
+
if (!res.ok) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`Fetch failed ${res.status} ${res.statusText} for ${record.source.url}`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
38
|
+
verifySha256(buf, record.sha256);
|
|
39
|
+
writeFileSync(dest, buf);
|
|
40
|
+
return dest;
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
caseCachePath,
|
|
44
|
+
fetchPublicCaseToCache,
|
|
45
|
+
verifySha256
|
|
46
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/publicCorpus.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
var DEFAULT_CACHE_ROOT = join(homedir(), ".cache", "dicom-synth-testcases");
|
|
8
|
+
var bundledCatalogPath = join(
|
|
9
|
+
dirname(fileURLToPath(import.meta.url)),
|
|
10
|
+
"../data/public-cases.json"
|
|
11
|
+
);
|
|
12
|
+
function defaultPublicCasesPath() {
|
|
13
|
+
return bundledCatalogPath;
|
|
14
|
+
}
|
|
15
|
+
function loadDefaultCases() {
|
|
16
|
+
return loadCasesFromJson(defaultPublicCasesPath());
|
|
17
|
+
}
|
|
18
|
+
function loadCasesFromJson(casesJsonPath) {
|
|
19
|
+
const j = JSON.parse(readFileSync(casesJsonPath, "utf8"));
|
|
20
|
+
return j.cases;
|
|
21
|
+
}
|
|
22
|
+
function loadCaseById(casesJsonPath, id) {
|
|
23
|
+
const found = loadCasesFromJson(casesJsonPath).find((r) => r.id === id);
|
|
24
|
+
if (!found) {
|
|
25
|
+
throw new Error(`Unknown case id "${id}" in ${casesJsonPath}`);
|
|
26
|
+
}
|
|
27
|
+
return found;
|
|
28
|
+
}
|
|
29
|
+
function caseCachePath(sha256, root = DEFAULT_CACHE_ROOT) {
|
|
30
|
+
return join(root, sha256, "file.dcm");
|
|
31
|
+
}
|
|
32
|
+
function verifySha256(buffer, expected) {
|
|
33
|
+
const h = createHash("sha256").update(buffer).digest("hex");
|
|
34
|
+
if (h !== expected) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`SHA256 mismatch: expected ${expected}, got ${h}. Upstream may have changed; bump metadata after review.`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function fetchPublicCaseToCache(record, cacheRoot = DEFAULT_CACHE_ROOT) {
|
|
41
|
+
if (record.source.kind !== "url") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Case ${record.id}: only url sources are supported (s3 requires credentials).`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const dest = caseCachePath(record.sha256, cacheRoot);
|
|
47
|
+
if (existsSync(dest)) {
|
|
48
|
+
const buf2 = readFileSync(dest);
|
|
49
|
+
verifySha256(buf2, record.sha256);
|
|
50
|
+
return dest;
|
|
51
|
+
}
|
|
52
|
+
mkdirSync(join(cacheRoot, record.sha256), { recursive: true });
|
|
53
|
+
const res = await fetch(record.source.url);
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Fetch failed ${res.status} ${res.statusText} for ${record.source.url}`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
60
|
+
verifySha256(buf, record.sha256);
|
|
61
|
+
writeFileSync(dest, buf);
|
|
62
|
+
return dest;
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
caseCachePath,
|
|
66
|
+
defaultPublicCasesPath,
|
|
67
|
+
fetchPublicCaseToCache,
|
|
68
|
+
loadCaseById,
|
|
69
|
+
loadCasesFromJson,
|
|
70
|
+
loadDefaultCases,
|
|
71
|
+
verifySha256
|
|
72
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/synthetic.ts
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import * as dcmjs from "dcmjs";
|
|
5
|
+
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
6
|
+
var VARIANT_INDEX = {
|
|
7
|
+
"minimal-invalid-uid": 0,
|
|
8
|
+
"valid-uid": 1,
|
|
9
|
+
"vendor-warnings": 2
|
|
10
|
+
};
|
|
11
|
+
function uidFor(variant, role) {
|
|
12
|
+
const i = VARIANT_INDEX[variant];
|
|
13
|
+
if (variant === "minimal-invalid-uid") {
|
|
14
|
+
return `2.25.100000000000000000000000000000.${role}.${i}`;
|
|
15
|
+
}
|
|
16
|
+
const uidRoots = [
|
|
17
|
+
"1000000000000000000000000000000000001",
|
|
18
|
+
"1000000000000000000000000000000000002",
|
|
19
|
+
"1000000000000000000000000000000000003"
|
|
20
|
+
];
|
|
21
|
+
const roleSuffix = role === "study" ? 1 : role === "series" ? 2 : 3;
|
|
22
|
+
return `2.25.${uidRoots[i]}.${roleSuffix}`;
|
|
23
|
+
}
|
|
24
|
+
function naturalDataset(variant) {
|
|
25
|
+
const studyUID = uidFor(variant, "study");
|
|
26
|
+
const seriesUID = uidFor(variant, "series");
|
|
27
|
+
const sopUID = uidFor(variant, "sop");
|
|
28
|
+
const dataset = {
|
|
29
|
+
PatientName: "SYNTH^SUBJECT",
|
|
30
|
+
PatientID: "SYNTH_PID",
|
|
31
|
+
Modality: "CT",
|
|
32
|
+
SOPClassUID: CT_SOP_CLASS,
|
|
33
|
+
SOPInstanceUID: sopUID,
|
|
34
|
+
SeriesInstanceUID: seriesUID,
|
|
35
|
+
StudyInstanceUID: studyUID,
|
|
36
|
+
StudyDescription: "Synthetic study",
|
|
37
|
+
SeriesDescription: "Synthetic series",
|
|
38
|
+
Rows: 1,
|
|
39
|
+
Columns: 1,
|
|
40
|
+
BitsAllocated: 16,
|
|
41
|
+
BitsStored: 16,
|
|
42
|
+
HighBit: 15,
|
|
43
|
+
SamplesPerPixel: 1,
|
|
44
|
+
PhotometricInterpretation: "MONOCHROME2",
|
|
45
|
+
PixelRepresentation: 0,
|
|
46
|
+
PixelData: new Uint8Array([0, 0]).buffer
|
|
47
|
+
};
|
|
48
|
+
if (variant === "vendor-warnings") {
|
|
49
|
+
dataset.Laterality = "";
|
|
50
|
+
dataset.PatientWeight = "0";
|
|
51
|
+
}
|
|
52
|
+
return dataset;
|
|
53
|
+
}
|
|
54
|
+
function buildDicomDict(variant) {
|
|
55
|
+
const meta = {
|
|
56
|
+
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
57
|
+
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
58
|
+
MediaStorageSOPInstanceUID: uidFor(variant, "sop"),
|
|
59
|
+
TransferSyntaxUID: "1.2.840.10008.1.2.1",
|
|
60
|
+
ImplementationClassUID: "1.2.3.4",
|
|
61
|
+
ImplementationVersionName: "SYNTH"
|
|
62
|
+
};
|
|
63
|
+
const dicomDict = new dcmjs.data.DicomDict(
|
|
64
|
+
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(meta)
|
|
65
|
+
);
|
|
66
|
+
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
67
|
+
naturalDataset(variant)
|
|
68
|
+
);
|
|
69
|
+
return dicomDict;
|
|
70
|
+
}
|
|
71
|
+
function buildSyntheticCtBuffer(variant) {
|
|
72
|
+
return Buffer.from(
|
|
73
|
+
buildDicomDict(variant).write({ allowInvalidVRLength: true })
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
var SYNTHETIC_FIXTURES = [
|
|
77
|
+
{
|
|
78
|
+
filename: "minimal-ct-0.dcm",
|
|
79
|
+
variant: "minimal-invalid-uid",
|
|
80
|
+
description: "Minimal CT with text in UIDs (many dciodvfy errors)"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
filename: "valid-uid-ct-0.dcm",
|
|
84
|
+
variant: "valid-uid",
|
|
85
|
+
description: "Minimal CT with numeric UIDs only (missing-module noise)"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
filename: "vendor-warnings-ct-0.dcm",
|
|
89
|
+
variant: "vendor-warnings",
|
|
90
|
+
description: "Valid UIDs plus empty Laterality and zero PatientWeight"
|
|
91
|
+
}
|
|
92
|
+
];
|
|
93
|
+
function writeSyntheticFixturesToDir(outDir) {
|
|
94
|
+
const root = resolve(outDir);
|
|
95
|
+
mkdirSync(root, { recursive: true });
|
|
96
|
+
const written = [];
|
|
97
|
+
for (const { filename, variant } of SYNTHETIC_FIXTURES) {
|
|
98
|
+
const path = resolve(root, filename);
|
|
99
|
+
writeFileSync(path, buildSyntheticCtBuffer(variant));
|
|
100
|
+
written.push(path);
|
|
101
|
+
}
|
|
102
|
+
return written;
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
SYNTHETIC_FIXTURES,
|
|
106
|
+
buildDicomDict,
|
|
107
|
+
buildSyntheticCtBuffer,
|
|
108
|
+
writeSyntheticFixturesToDir
|
|
109
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/syntheticFixtures/generator.ts
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/loadDcmjs.ts
|
|
6
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
7
|
+
function loadDcmjs() {
|
|
8
|
+
if (dcmjsNamespace.data?.DicomDict) {
|
|
9
|
+
return dcmjsNamespace;
|
|
10
|
+
}
|
|
11
|
+
const d = dcmjsDefaultImport;
|
|
12
|
+
if (d?.data?.DicomDict) {
|
|
13
|
+
return d;
|
|
14
|
+
}
|
|
15
|
+
if (d?.default?.data?.DicomDict) {
|
|
16
|
+
return d.default;
|
|
17
|
+
}
|
|
18
|
+
throw new Error(
|
|
19
|
+
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
var dcmjs = loadDcmjs();
|
|
23
|
+
|
|
24
|
+
// src/syntheticFixtures/generator.ts
|
|
25
|
+
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
26
|
+
var VARIANT_INDEX = {
|
|
27
|
+
"minimal-invalid-uid": 0,
|
|
28
|
+
"valid-uid": 1,
|
|
29
|
+
"vendor-warnings": 2
|
|
30
|
+
};
|
|
31
|
+
function uidFor(variant, role) {
|
|
32
|
+
const i = VARIANT_INDEX[variant];
|
|
33
|
+
if (variant === "minimal-invalid-uid") {
|
|
34
|
+
return `2.25.100000000000000000000000000000.${role}.${i}`;
|
|
35
|
+
}
|
|
36
|
+
const uidRoots = [
|
|
37
|
+
"1000000000000000000000000000000000001",
|
|
38
|
+
"1000000000000000000000000000000000002",
|
|
39
|
+
"1000000000000000000000000000000000003"
|
|
40
|
+
];
|
|
41
|
+
const roleSuffix = role === "study" ? 1 : role === "series" ? 2 : 3;
|
|
42
|
+
return `2.25.${uidRoots[i]}.${roleSuffix}`;
|
|
43
|
+
}
|
|
44
|
+
function naturalDataset(variant) {
|
|
45
|
+
const studyUID = uidFor(variant, "study");
|
|
46
|
+
const seriesUID = uidFor(variant, "series");
|
|
47
|
+
const sopUID = uidFor(variant, "sop");
|
|
48
|
+
const dataset = {
|
|
49
|
+
PatientName: "SYNTH^SUBJECT",
|
|
50
|
+
PatientID: "SYNTH_PID",
|
|
51
|
+
Modality: "CT",
|
|
52
|
+
SOPClassUID: CT_SOP_CLASS,
|
|
53
|
+
SOPInstanceUID: sopUID,
|
|
54
|
+
SeriesInstanceUID: seriesUID,
|
|
55
|
+
StudyInstanceUID: studyUID,
|
|
56
|
+
StudyDescription: "Synthetic study",
|
|
57
|
+
SeriesDescription: "Synthetic series",
|
|
58
|
+
Rows: 1,
|
|
59
|
+
Columns: 1,
|
|
60
|
+
BitsAllocated: 16,
|
|
61
|
+
BitsStored: 16,
|
|
62
|
+
HighBit: 15,
|
|
63
|
+
SamplesPerPixel: 1,
|
|
64
|
+
PhotometricInterpretation: "MONOCHROME2",
|
|
65
|
+
PixelRepresentation: 0,
|
|
66
|
+
PixelData: new Uint8Array([0, 0]).buffer
|
|
67
|
+
};
|
|
68
|
+
if (variant === "vendor-warnings") {
|
|
69
|
+
dataset.Laterality = "";
|
|
70
|
+
dataset.PatientWeight = "0";
|
|
71
|
+
}
|
|
72
|
+
return dataset;
|
|
73
|
+
}
|
|
74
|
+
function buildDicomDict(variant) {
|
|
75
|
+
const meta = {
|
|
76
|
+
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
77
|
+
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
78
|
+
MediaStorageSOPInstanceUID: uidFor(variant, "sop"),
|
|
79
|
+
TransferSyntaxUID: "1.2.840.10008.1.2.1",
|
|
80
|
+
ImplementationClassUID: "1.2.3.4",
|
|
81
|
+
ImplementationVersionName: "SYNTH"
|
|
82
|
+
};
|
|
83
|
+
const dicomDict = new dcmjs.data.DicomDict(
|
|
84
|
+
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(meta)
|
|
85
|
+
);
|
|
86
|
+
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
87
|
+
naturalDataset(variant)
|
|
88
|
+
);
|
|
89
|
+
return dicomDict;
|
|
90
|
+
}
|
|
91
|
+
function buildSyntheticCtBuffer(variant) {
|
|
92
|
+
return Buffer.from(
|
|
93
|
+
buildDicomDict(variant).write({ allowInvalidVRLength: true })
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
var SYNTHETIC_FIXTURES = [
|
|
97
|
+
{
|
|
98
|
+
filename: "minimal-ct-0.dcm",
|
|
99
|
+
variant: "minimal-invalid-uid",
|
|
100
|
+
description: "Minimal CT with text in UIDs (many dciodvfy errors)"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
filename: "valid-uid-ct-0.dcm",
|
|
104
|
+
variant: "valid-uid",
|
|
105
|
+
description: "Minimal CT with numeric UIDs only (missing-module noise)"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
filename: "vendor-warnings-ct-0.dcm",
|
|
109
|
+
variant: "vendor-warnings",
|
|
110
|
+
description: "Valid UIDs plus empty Laterality and zero PatientWeight"
|
|
111
|
+
}
|
|
112
|
+
];
|
|
113
|
+
function writeSyntheticFixturesToDir(outDir) {
|
|
114
|
+
const root = resolve(outDir);
|
|
115
|
+
mkdirSync(root, { recursive: true });
|
|
116
|
+
const written = [];
|
|
117
|
+
for (const { filename, variant } of SYNTHETIC_FIXTURES) {
|
|
118
|
+
const path = resolve(root, filename);
|
|
119
|
+
writeFileSync(path, buildSyntheticCtBuffer(variant));
|
|
120
|
+
written.push(path);
|
|
121
|
+
}
|
|
122
|
+
return written;
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
SYNTHETIC_FIXTURES,
|
|
126
|
+
buildDicomDict,
|
|
127
|
+
buildSyntheticCtBuffer,
|
|
128
|
+
writeSyntheticFixturesToDir
|
|
129
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { defaultPublicCasesPath, loadCaseById, loadCasesFromJson, loadDefaultCases, type PublicCaseRecord, type PublicCaseSource, } from './public-fixtures/catalog';
|
|
2
|
+
export { caseCachePath, fetchPublicCaseToCache, verifySha256, } from './public-fixtures/fetch';
|
|
3
|
+
export { buildDicomDict, buildSyntheticCtBuffer, SYNTHETIC_FIXTURES, type SyntheticCtVariant, writeSyntheticFixturesToDir, } from './syntheticFixtures/generator';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from 'dcmjs';
|
|
2
|
+
type DcmjsModule = typeof dcmjsNamespace;
|
|
3
|
+
/** Resolve dcmjs across ESM namespace vs default export interop. */
|
|
4
|
+
export declare function loadDcmjs(): DcmjsModule;
|
|
5
|
+
export declare const dcmjs: typeof dcmjsDefaultImport;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type PublicCaseSource = {
|
|
2
|
+
kind: 'url';
|
|
3
|
+
url: string;
|
|
4
|
+
} | {
|
|
5
|
+
kind: 's3';
|
|
6
|
+
bucket: string;
|
|
7
|
+
key: string;
|
|
8
|
+
version_id?: string;
|
|
9
|
+
};
|
|
10
|
+
export type PublicCaseRecord = {
|
|
11
|
+
id: string;
|
|
12
|
+
violation_class: string;
|
|
13
|
+
source: PublicCaseSource;
|
|
14
|
+
sha256: string;
|
|
15
|
+
notes?: string;
|
|
16
|
+
dciodvfy_skip?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** Path to bundled `public-cases.json` (works from `src/` and `dist/esm/`). */
|
|
19
|
+
export declare function defaultPublicCasesPath(): string;
|
|
20
|
+
export declare function loadDefaultCases(): PublicCaseRecord[];
|
|
21
|
+
export declare function loadCasesFromJson(casesJsonPath: string): PublicCaseRecord[];
|
|
22
|
+
export declare function loadCaseById(casesJsonPath: string, id: string): PublicCaseRecord;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PublicCaseRecord } from './catalog';
|
|
2
|
+
export declare function caseCachePath(sha256: string, root?: string): string;
|
|
3
|
+
export declare function verifySha256(buffer: Buffer, expected: string): void;
|
|
4
|
+
export declare function fetchPublicCaseToCache(record: PublicCaseRecord, cacheRoot?: string): Promise<string>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type PublicCaseSource = {
|
|
2
|
+
kind: 'url';
|
|
3
|
+
url: string;
|
|
4
|
+
} | {
|
|
5
|
+
kind: 's3';
|
|
6
|
+
bucket: string;
|
|
7
|
+
key: string;
|
|
8
|
+
version_id?: string;
|
|
9
|
+
};
|
|
10
|
+
export type PublicCaseRecord = {
|
|
11
|
+
id: string;
|
|
12
|
+
violation_class: string;
|
|
13
|
+
source: PublicCaseSource;
|
|
14
|
+
sha256: string;
|
|
15
|
+
notes?: string;
|
|
16
|
+
dciodvfy_skip?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** Path to bundled `public-cases.json` (works from `src/` and `dist/esm/`). */
|
|
19
|
+
export declare function defaultPublicCasesPath(): string;
|
|
20
|
+
export declare function loadDefaultCases(): PublicCaseRecord[];
|
|
21
|
+
export declare function loadCasesFromJson(casesJsonPath: string): PublicCaseRecord[];
|
|
22
|
+
export declare function loadCaseById(casesJsonPath: string, id: string): PublicCaseRecord;
|
|
23
|
+
export declare function caseCachePath(sha256: string, root?: string): string;
|
|
24
|
+
export declare function verifySha256(buffer: Buffer, expected: string): void;
|
|
25
|
+
export declare function fetchPublicCaseToCache(record: PublicCaseRecord, cacheRoot?: string): Promise<string>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as dcmjs from 'dcmjs';
|
|
2
|
+
export type SyntheticCtVariant = 'minimal-invalid-uid' | 'valid-uid' | 'vendor-warnings';
|
|
3
|
+
export declare function buildDicomDict(variant: SyntheticCtVariant): dcmjs.data.DicomDict;
|
|
4
|
+
export declare function buildSyntheticCtBuffer(variant: SyntheticCtVariant): Buffer;
|
|
5
|
+
export declare const SYNTHETIC_FIXTURES: {
|
|
6
|
+
filename: string;
|
|
7
|
+
variant: SyntheticCtVariant;
|
|
8
|
+
description: string;
|
|
9
|
+
}[];
|
|
10
|
+
/** Write all catalogue fixtures to a directory (created if missing). */
|
|
11
|
+
export declare function writeSyntheticFixturesToDir(outDir: string): string[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type * as Dcmjs from 'dcmjs';
|
|
2
|
+
export type SyntheticCtVariant = 'minimal-invalid-uid' | 'valid-uid' | 'vendor-warnings';
|
|
3
|
+
export declare function buildDicomDict(variant: SyntheticCtVariant): Dcmjs.data.DicomDict;
|
|
4
|
+
export declare function buildSyntheticCtBuffer(variant: SyntheticCtVariant): Buffer;
|
|
5
|
+
export declare const SYNTHETIC_FIXTURES: {
|
|
6
|
+
filename: string;
|
|
7
|
+
variant: SyntheticCtVariant;
|
|
8
|
+
description: string;
|
|
9
|
+
}[];
|
|
10
|
+
/** Write all catalogue fixtures to a directory (created if missing). */
|
|
11
|
+
export declare function writeSyntheticFixturesToDir(outDir: string): string[];
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dicom-synth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Toolkit for synthetic DICOM fixtures and public fixture fetch/cache.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@11.3.0",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/clintools/dicom-synth.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"dicom",
|
|
14
|
+
"synthetic",
|
|
15
|
+
"test-data",
|
|
16
|
+
"medical-imaging"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=20"
|
|
20
|
+
},
|
|
21
|
+
"main": "dist/esm/index.js",
|
|
22
|
+
"module": "dist/esm/index.js",
|
|
23
|
+
"types": "dist/types/index.d.ts",
|
|
24
|
+
"bin": {
|
|
25
|
+
"dicom-synth-write": "./bin/dicom-synth-write.mjs",
|
|
26
|
+
"dicom-synth-fetch": "./bin/dicom-synth-fetch.mjs"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": "./dist/esm/index.js",
|
|
31
|
+
"types": "./dist/types/index.d.ts",
|
|
32
|
+
"default": "./dist/esm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./public-cases.json": "./data/public-cases.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"data/public-cases.json",
|
|
39
|
+
"bin"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"prepare": "husky",
|
|
43
|
+
"prepublishOnly": "pnpm build",
|
|
44
|
+
"clean": "rm -rf dist",
|
|
45
|
+
"build:esm": "node esbuild.config.js",
|
|
46
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist/types -p tsconfig.build.json",
|
|
47
|
+
"build": "pnpm build:esm && pnpm build:types && pnpm build:data",
|
|
48
|
+
"build:data": "mkdir -p dist/data && cp data/public-cases.json dist/data/",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"lint": "eslint src test scripts --ext .ts",
|
|
53
|
+
"lint:fix": "eslint src test scripts --ext .ts --fix",
|
|
54
|
+
"check": "biome check .",
|
|
55
|
+
"check:errors": "biome check . --diagnostic-level=error",
|
|
56
|
+
"check:fix": "biome check . --write",
|
|
57
|
+
"code:quality": "pnpm check:errors && pnpm typecheck && pnpm test",
|
|
58
|
+
"hooks:pre-commit": "bash scripts/hooks/pre-commit.sh",
|
|
59
|
+
"hooks:pre-push": "bash scripts/hooks/pre-push.sh",
|
|
60
|
+
"write:synthetic": "tsx scripts/write-synthetic-fixtures.ts",
|
|
61
|
+
"fetch:public-case": "tsx scripts/fetch-public-case.ts"
|
|
62
|
+
},
|
|
63
|
+
"lint-staged": {
|
|
64
|
+
"*.{js,jsx,ts,tsx,mjs,mts,json,css,md}": [
|
|
65
|
+
"biome check --write --no-errors-on-unmatched"
|
|
66
|
+
],
|
|
67
|
+
"*.{ts,tsx}": [
|
|
68
|
+
"eslint --fix"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"dcmjs": "^0.51.1"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@biomejs/biome": "2.4.7",
|
|
76
|
+
"@eslint/js": "^9.34.0",
|
|
77
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
78
|
+
"@semantic-release/git": "^10.0.1",
|
|
79
|
+
"@semantic-release/github": "^12.0.3",
|
|
80
|
+
"@semantic-release/npm": "^13.1.3",
|
|
81
|
+
"@types/node": "^22.15.3",
|
|
82
|
+
"@typescript-eslint/eslint-plugin": "^8.46.1",
|
|
83
|
+
"@typescript-eslint/parser": "^8.46.1",
|
|
84
|
+
"dcmjs": "^0.51.1",
|
|
85
|
+
"esbuild": "^0.25.9",
|
|
86
|
+
"eslint": "^9.38.0",
|
|
87
|
+
"globals": "^16.4.0",
|
|
88
|
+
"husky": "^9.1.7",
|
|
89
|
+
"lint-staged": "^16.4.0",
|
|
90
|
+
"semantic-release": "^25.0.3",
|
|
91
|
+
"tsx": "^4.19.3",
|
|
92
|
+
"typescript": "^5.8.3",
|
|
93
|
+
"vitest": "^3.2.4"
|
|
94
|
+
}
|
|
95
|
+
}
|