Skip to content

Commit a595527

Browse files
committed
fix(io): skip unknown CBOR keys in image, mesh, and transform IO for forward compatibility
Replace itkExceptionMacro throws with silent skips for unrecognized CBOR map keys in ReadCBOR across all three IO classes: - itkWasmImageIO: imageType inner sub-map (top-level was already fixed) - itkWasmMeshIO: meshType inner sub-map + explicit top-level else clause - itkWasmTransformIO: transformType inner sub-map + explicit top-level else clause This improves interoperability with third-party CBOR writers (e.g. NiiVue) that may include additional or extended fields in the type sub-maps. Also replace empty test stubs in transform-io with real round-trip tests for wasm-zstd and MNC (.xfm) formats, using programmatically created affine transforms to avoid dependency on IPFS test data downloads.
1 parent a6053c5 commit a595527

File tree

7 files changed

+230
-19
lines changed

7 files changed

+230
-19
lines changed
Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
1-
# Generated file. To retain edits, remove this comment.
1+
import numpy as np
22

3-
from itkwasm_transform_io_wasi import mnc_read_transform
3+
from itkwasm import Transform, TransformParameterizations, FloatTypes
4+
from itkwasm_transform_io_wasi import mnc_read_transform, mnc_write_transform
5+
6+
from .common import test_output_path, verify_test_linear_transform
7+
8+
9+
def _make_affine_transform():
10+
"""Create an affine transform matching the LinearTransform test fixture."""
11+
return Transform(
12+
transformType={
13+
"transformParameterization": TransformParameterizations.Affine,
14+
"parametersValueType": FloatTypes.Float64,
15+
"inputDimension": 3,
16+
"outputDimension": 3,
17+
},
18+
numberOfFixedParameters=3,
19+
numberOfParameters=12,
20+
fixedParameters=np.array([0.0, 0.0, 0.0]),
21+
parameters=np.array(
22+
[
23+
0.65631490118447,
24+
0.5806583745824385,
25+
-0.4817536741017158,
26+
-0.7407986817430222,
27+
0.37486398378429736,
28+
-0.5573995934598175,
29+
-0.14306664045479867,
30+
0.7227121458012518,
31+
0.676179776908723,
32+
-65.99999999999997,
33+
69.00000000000004,
34+
32.000000000000036,
35+
]
36+
),
37+
)
438

5-
from .common import test_input_path, test_output_path
639

740
def test_mnc_read_transform():
8-
pass
41+
"""Test reading an MNC (.xfm) transform file."""
42+
transform = _make_affine_transform()
43+
transform_list = [transform]
44+
45+
xfm_path = test_output_path / "mnc-read-test-LinearTransform.xfm"
46+
could_write = mnc_write_transform(transform_list, str(xfm_path))
47+
assert could_write
48+
49+
could_read, transform_list_back = mnc_read_transform(xfm_path)
50+
assert could_read
51+
verify_test_linear_transform(transform_list_back)
Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,61 @@
1-
# Generated file. To retain edits, remove this comment.
1+
import numpy as np
22

3-
from itkwasm_transform_io_wasi import mnc_write_transform
3+
from itkwasm import Transform, TransformParameterizations, FloatTypes
4+
from itkwasm_transform_io_wasi import mnc_read_transform, mnc_write_transform
5+
6+
from .common import test_output_path, verify_test_linear_transform
7+
8+
9+
def _make_affine_transform():
10+
"""Create an affine transform matching the LinearTransform test fixture."""
11+
return Transform(
12+
transformType={
13+
"transformParameterization": TransformParameterizations.Affine,
14+
"parametersValueType": FloatTypes.Float64,
15+
"inputDimension": 3,
16+
"outputDimension": 3,
17+
},
18+
numberOfFixedParameters=3,
19+
numberOfParameters=12,
20+
fixedParameters=np.array([0.0, 0.0, 0.0]),
21+
parameters=np.array(
22+
[
23+
0.65631490118447,
24+
0.5806583745824385,
25+
-0.4817536741017158,
26+
-0.7407986817430222,
27+
0.37486398378429736,
28+
-0.5573995934598175,
29+
-0.14306664045479867,
30+
0.7227121458012518,
31+
0.676179776908723,
32+
-65.99999999999997,
33+
69.00000000000004,
34+
32.000000000000036,
35+
]
36+
),
37+
)
438

5-
from .common import test_input_path, test_output_path
639

740
def test_mnc_write_transform():
8-
pass
41+
"""Test writing an MNC (.xfm) transform and reading it back."""
42+
transform = _make_affine_transform()
43+
transform_list = [transform]
44+
45+
xfm_path = test_output_path / "mnc-write-test-LinearTransform.xfm"
46+
could_write = mnc_write_transform(transform_list, str(xfm_path))
47+
assert could_write
48+
49+
could_read, transform_list_back = mnc_read_transform(xfm_path)
50+
assert could_read
51+
verify_test_linear_transform(transform_list_back)
52+
53+
# Verify exact parameter values survive the round-trip
54+
np.testing.assert_allclose(
55+
transform_list_back[0].parameters,
56+
transform.parameters,
57+
)
58+
np.testing.assert_allclose(
59+
transform_list_back[0].fixedParameters,
60+
transform.fixedParameters,
61+
)
Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
1-
# Generated file. To retain edits, remove this comment.
1+
import numpy as np
22

3-
from itkwasm_transform_io_wasi import wasm_zstd_read_transform
3+
from itkwasm import Transform, TransformParameterizations, FloatTypes
4+
from itkwasm_transform_io_wasi import (
5+
wasm_zstd_read_transform,
6+
wasm_zstd_write_transform,
7+
)
8+
9+
from .common import test_output_path, verify_test_linear_transform
10+
11+
12+
def _make_affine_transform():
13+
"""Create an affine transform matching the LinearTransform test fixture."""
14+
return Transform(
15+
transformType={
16+
"transformParameterization": TransformParameterizations.Affine,
17+
"parametersValueType": FloatTypes.Float64,
18+
"inputDimension": 3,
19+
"outputDimension": 3,
20+
},
21+
numberOfFixedParameters=3,
22+
numberOfParameters=12,
23+
fixedParameters=np.array([0.0, 0.0, 0.0]),
24+
parameters=np.array(
25+
[
26+
0.65631490118447,
27+
0.5806583745824385,
28+
-0.4817536741017158,
29+
-0.7407986817430222,
30+
0.37486398378429736,
31+
-0.5573995934598175,
32+
-0.14306664045479867,
33+
0.7227121458012518,
34+
0.676179776908723,
35+
-65.99999999999997,
36+
69.00000000000004,
37+
32.000000000000036,
38+
]
39+
),
40+
)
441

5-
from .common import test_input_path, test_output_path
642

743
def test_wasm_zstd_read_transform():
8-
pass
44+
"""Test reading a zstd-compressed IWT CBOR transform file."""
45+
transform = _make_affine_transform()
46+
transform_list = [transform]
47+
48+
zstd_path = test_output_path / "zstd-read-test-LinearTransform.iwt.cbor.zst"
49+
could_write = wasm_zstd_write_transform(transform_list, str(zstd_path))
50+
assert could_write
51+
52+
could_read, transform_list_back = wasm_zstd_read_transform(zstd_path)
53+
assert could_read
54+
verify_test_linear_transform(transform_list_back)
Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
1-
# Generated file. To retain edits, remove this comment.
1+
import numpy as np
22

3-
from itkwasm_transform_io_wasi import wasm_zstd_write_transform
3+
from itkwasm import Transform, TransformParameterizations, FloatTypes
4+
from itkwasm_transform_io_wasi import (
5+
wasm_zstd_read_transform,
6+
wasm_zstd_write_transform,
7+
)
8+
9+
from .common import test_output_path, verify_test_linear_transform
10+
11+
12+
def _make_affine_transform():
13+
"""Create an affine transform matching the LinearTransform test fixture."""
14+
return Transform(
15+
transformType={
16+
"transformParameterization": TransformParameterizations.Affine,
17+
"parametersValueType": FloatTypes.Float64,
18+
"inputDimension": 3,
19+
"outputDimension": 3,
20+
},
21+
numberOfFixedParameters=3,
22+
numberOfParameters=12,
23+
fixedParameters=np.array([0.0, 0.0, 0.0]),
24+
parameters=np.array(
25+
[
26+
0.65631490118447,
27+
0.5806583745824385,
28+
-0.4817536741017158,
29+
-0.7407986817430222,
30+
0.37486398378429736,
31+
-0.5573995934598175,
32+
-0.14306664045479867,
33+
0.7227121458012518,
34+
0.676179776908723,
35+
-65.99999999999997,
36+
69.00000000000004,
37+
32.000000000000036,
38+
]
39+
),
40+
)
441

5-
from .common import test_input_path, test_output_path
642

743
def test_wasm_zstd_write_transform():
8-
pass
44+
"""Test writing a zstd-compressed IWT CBOR transform and reading it back."""
45+
transform = _make_affine_transform()
46+
transform_list = [transform]
47+
48+
zstd_path = test_output_path / "zstd-write-test-LinearTransform.iwt.cbor.zst"
49+
could_write = wasm_zstd_write_transform(transform_list, str(zstd_path))
50+
assert could_write
51+
52+
could_read, transform_list_back = wasm_zstd_read_transform(zstd_path)
53+
assert could_read
54+
verify_test_linear_transform(transform_list_back)
55+
56+
# Verify exact parameter values survive the round-trip
57+
np.testing.assert_allclose(
58+
transform_list_back[0].parameters,
59+
transform.parameters,
60+
)
61+
np.testing.assert_allclose(
62+
transform_list_back[0].fixedParameters,
63+
transform.fixedParameters,
64+
)

src/itkWasmImageIO.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ WasmImageIO ::ReadCBOR(void * buffer, unsigned char * cborBuffer, size_t cborBuf
248248
}
249249
else
250250
{
251-
itkExceptionMacro("Unexpected imageType cbor map key: " << imageTypeKey);
251+
// Skip unknown keys for forward compatibility and interoperability
252+
// with third-party CBOR writers that may include additional fields.
252253
}
253254
}
254255
}

src/itkWasmMeshIO.cxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ WasmMeshIO ::ReadCBOR(void * buffer, unsigned char * cborBuffer, size_t cborBuff
234234
}
235235
else
236236
{
237-
itkExceptionMacro("Unexpected meshType cbor map key: " << meshTypeKey);
237+
// Skip unknown keys for forward compatibility and interoperability
238+
// with third-party CBOR writers that may include additional fields.
238239
}
239240
}
240241
}
@@ -279,6 +280,11 @@ WasmMeshIO ::ReadCBOR(void * buffer, unsigned char * cborBuffer, size_t cborBuff
279280
const auto components = cbor_get_uint64(indexHandle[ii].value);
280281
this->SetCellBufferSize(components);
281282
}
283+
else
284+
{
285+
// Skip unknown keys for forward compatibility and interoperability
286+
// with third-party CBOR writers that may include additional fields.
287+
}
282288
}
283289
}
284290

src/itkWasmTransformIO.cxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ WasmTransformIOTemplate<TParametersValueType>::ReadCBOR(void * buffer,
342342
}
343343
else
344344
{
345-
itkExceptionMacro("Unexpected transformType cbor map key: " << transformTypeKey);
345+
// Skip unknown keys for forward compatibility and interoperability
346+
// with third-party CBOR writers that may include additional fields.
346347
}
347348
}
348349
}
@@ -374,6 +375,11 @@ WasmTransformIOTemplate<TParametersValueType>::ReadCBOR(void * buffer,
374375
cbor_string_length(transformHandle[jj].value));
375376
transformJSON.outputSpaceName = outputSpaceName;
376377
}
378+
else
379+
{
380+
// Skip unknown keys for forward compatibility and interoperability
381+
// with third-party CBOR writers that may include additional fields.
382+
}
377383
}
378384
transformListJSON.push_back(transformJSON);
379385
}

0 commit comments

Comments
 (0)