Skip to content

fix(normalizers): treat an empty per-frame sequence as missing; one ImagePositionPatient array per frame - #665

Merged
wayfarer3130 merged 5 commits into
1.0-betafrom
fix/1.0-beta-normalize-empty-per-frame
Nov 26, 2026
Merged

wayfarer3130 merged 5 commits into
1.0-betafrom
fix/1.0-beta-normalize-empty-per-frame

Conversation

@wayfarer3130

@wayfarer3130 wayfarer3130 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR is a follow-up to #662. PR #662 is the 1.x port of #603, which normalizes a multiframe dataset that has no functional groups. The merged 1.x code has defects in src/normalizers.js. The first commit (3a7e159) ports the 0.x fix of #664 (commit 4b0b844). The second commit (07f8324) fixes three more defects that the review of this PR found. The third commit (83fdb5e) fixes two defects that the review of 07f8324 found. The fourth commit (8223bec) applies the decisions of the final review. The merge commit (20809da) merges 1.0-beta with #663 and #666.

Defects

  • S3: an empty per-frame sequence. normalizeMultiframe treats PerFrameFunctionalGroupsSequence: [] as a sequence that is present. For an input with NumberOfFrames 8 and one source dataset, the result has 1 per-frame item, not 8.
  • N6: one shared array. ImageNormalizer.planePositionGroup puts the same ImagePositionPatient array object in each per-frame item. A change to the position of one frame changes the position of all frames.
  • S1: an empty shared sequence. normalizeMultiframe treats SharedFunctionalGroupsSequence: [] as present. The normalizer does not build the plane orientation and the pixel measures. The normalizer then sets PixelValueTransformationSequence as a named property of the array, and the writer drops that property. The same loss occurs for a shared sequence that is an array of one item. In 1.0, naturalizeDataset does not give this array for a real file: it gives the item object with [0] and length accessors (addAccessors). A one-item array comes from a dataset that code builds by hand, or from the 0.x shape, for example JSON.parse of a 0.x dataset.
  • Q2: an incomplete per-frame sequence. When the per-frame sequence has items, the normalizer adds FrameContentSequence only to item 1 for a multiframe source. A per-frame sequence with fewer items than NumberOfFrames stays short.
  • N1: a number value of ImagePositionPatient. The copy of N6 used ?.slice(), and a number value gave a TypeError.
  • F2: a null per-frame item after NumberOfFrames. The window guess reads every per-frame item. A null item after NumberOfFrames gave TypeError: Cannot read properties of null (reading 'FrameVOILUT'). The base branch has the same defect.

Fix

The change is in src/normalizers.js:

  • normalizeMultiframe treats an empty PerFrameFunctionalGroupsSequence array the same as a missing sequence. The normalizer keeps the declared NumberOfFrames (decision Q1 of fix(normalizers): normalize multiframe datasets missing functional groups (port of #603) #662).
  • normalizeMultiframe uses item 1 of a SharedFunctionalGroupsSequence array as the shared group. An empty array thus counts as missing, and the normalizer builds the shared groups from the top-level attributes. All other code in src/ reads the shared group as one object.
  • normalizeMultiframe calls addAccessors (from @dcmjs-org/core) on the shared group. Both SharedFunctionalGroupsSequence.X and SharedFunctionalGroupsSequence[0].X thus work after the normalizer. This is also true for a one-item array that code builds by hand, and for a shared group that the normalizer builds.
  • normalizeMultiframe fills the per-frame sequence up to NumberOfFrames items. A new item has the top-level position, the same as for a missing sequence. An existing item keeps its content.
  • normalizeMultiframe adds a FrameContentSequence only to an item up to NumberOfFrames that has none. An item that has a FrameContentSequence keeps it, item 1 included. Before this PR, the normalizer replaced the FrameContentSequence of item 1.
  • normalizeMultiframe replaces a null per-frame item up to NumberOfFrames with an empty item, so PerFrameFunctionalGroupsSequence: [null, {}] does not give a TypeError.
  • normalizeMultiframe does not change items after NumberOfFrames. The normalizer does not remove these items, and it does not add a FrameContentSequence to them. This is the behaviour before this PR. The window guess skips a null item after NumberOfFrames, and the item stays null.
  • planePositionGroup gives each item its own copy of an ImagePositionPatient array. A value that is not an array goes through unchanged, the same as before fix(normalizers): normalize multiframe datasets missing functional groups (port of #603) #662.

The doc comment of normalizeMultiframe records these rules. No copy of normalizeMultiframe or convertToMultiframe is in packages/. No spec in docs/specs/ covers the normalizers, so this PR does not change a document.

Note: the shape of a one-item shared array

Before this PR, the normalizer kept a one-item SharedFunctionalGroupsSequence array as an array. After this PR, the normalizer gives the item with the [0] and length accessors, the same as naturalizeDataset. SharedFunctionalGroupsSequence[0] gives the item, as before. SharedFunctionalGroupsSequence.X now also gives the value. Array.isArray(SharedFunctionalGroupsSequence) is now false.

Tests

The tests are in test/segIncompleteMultiframe.test.js:

  • An empty per-frame sequence with NumberOfFrames 8 gives 8 per-frame items, and two items do not share one ImagePositionPatient array.
  • The existing test for a missing sequence also checks that two items do not share one ImagePositionPatient array.
  • An empty shared sequence gives a shared group with PlaneOrientationSequence, PixelMeasuresSequence, and PixelValueTransformationSequence. These groups survive a denaturalizeDataset and naturalizeDataset round trip. The shared group has the [0] accessor.
  • An existing per-frame item keeps its own FrameContentSequence. A new item gets a FrameContentSequence with the correct InStackPositionNumber.
  • A per-frame sequence of 3 items, of 8 items, and [null, {}], with NumberOfFrames 8, gives 8 items. The first items keep their positions, and each item has a FrameContentSequence with the correct InStackPositionNumber.
  • A null per-frame item after NumberOfFrames does not throw, and the item stays null.
  • A one-item shared array gives a shared group where .PixelMeasuresSequence and [0] both work.
  • The test "keeps a complete multiframe dataset unchanged" now uses NumberOfFrames 1, so that the input is complete.

Before the fix of 07f8324, the 3 new tests of that commit fail:

  • The empty shared sequence: Received: [].
  • The per-frame sequence of 3 items: Expected length: 8, Received length: 3.
  • The per-frame sequence of 8 items: TypeError: Cannot read properties of undefined (reading 'InStackPositionNumber').

Before the fix of 83fdb5e, the test for an existing FrameContentSequence fails: the normalizer replaces the sequence of each item. Before 07f8324, the round trip test fails, because the shared group is an empty array.

Before the fix of 8223bec, 3 tests fail:

  • The null item after NumberOfFrames: TypeError from expect(received).not.toThrow().
  • The one-item shared array: expect(shared[0]).toBe(shared) gives Received: undefined.
  • The empty shared sequence: expect(shared[0]).toBe(shared) gives Received: undefined.

When the ||= {} guard is removed, the [null, {}] case fails with TypeError: Cannot read properties of null (reading 'FrameContentSequence').

After the fix:

  • npx jest test/segIncompleteMultiframe.test.js test/normalizers: 16 tests pass.
  • npx jest after the merge: 125 suites pass and 2 are skipped. 1461 tests pass and 11 are skipped.
  • pnpm run build and test/bundle-size.test.js pass after the merge of 1.0-beta with fix: byte order of binary value fields in Explicit VR Big Endian (port of #622) #663. dcmjs.js is 1515056 bytes (ratio ×1.09435). This size is more than the ceiling ×1.094 of 1.0-beta, so this PR sets the ceiling to ×1.095. dcmjs.es.js is 1460580 bytes (ratio ×1.07600, ceiling ×1.076, no change). The comment in test/bundle-size.test.js gives the same ratios.

Known items for later

  • The FrameContentSequence that the normalizer adds has one DimensionIndexValues value (the frame number). The DimensionIndexSequence of an Enhanced input can have more dimensions. This PR does not change that behaviour.
  • Two older defects of 1.0-beta will go to separate issues: the frame time goes to the wrong frame after the sort of convertToMultiframe, and the window guess reads FrameVOILUT and not FrameVOILUTSequence.

Links

🤖 Generated with Claude Code

wayfarer3130 and others added 5 commits November 25, 2026 13:20
…magePositionPatient array per frame

An empty PerFrameFunctionalGroupsSequence no longer reduces the number
of frames: the normalizer builds one item per declared frame, as for a
missing sequence. Each generated item gets its own ImagePositionPatient
copy. Port of the 0.x fix in #664 (follow-up to #662).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the per-frame sequence

- An empty SharedFunctionalGroupsSequence array counts as missing, and a
  one-item array is used as its item, so the shared groups the normalizer
  sets (PixelValueTransformationSequence and the others) are written.
- The per-frame sequence is filled up to NumberOfFrames, and each item
  gets its FrameContentSequence, not only the first item.
- planePositionGroup passes a non-array ImagePositionPatient through.
- Raise the dcmjs.js bundle ceiling to x1.091 (measured x1.09014).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ll per-frame item

- The normalizer adds a FrameContentSequence only to a per-frame item
  that has none; an existing item keeps its own (dimension index values,
  temporal position, stack ID).
- A null or missing per-frame item becomes an empty item again, so
  [null, {}] no longer throws a TypeError.
- Tests: an existing item keeps its FrameContentSequence, and the shared
  groups survive a denaturalize/naturalize round trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… after NumberOfFrames

- Call addAccessors on the SharedFunctionalGroupsSequence item, so that
  both SharedFunctionalGroupsSequence.X and SharedFunctionalGroupsSequence[0].X
  work, also for a hand-built one-item array.
- The window guess skips a null per-frame item after NumberOfFrames and
  does not throw.
- Tests: a [null, {}] case in the completes-a-per-frame-sequence it.each,
  a null item after NumberOfFrames, a one-item shared array. The test
  "keeps a complete multiframe dataset unchanged" now uses NumberOfFrames 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 26, 2026 01:27
@wayfarer3130
wayfarer3130 merged commit 9612db6 into 1.0-beta Nov 26, 2026
4 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/1.0-beta-normalize-empty-per-frame branch November 26, 2026 01:27
wayfarer3130 added a commit that referenced this pull request Nov 26, 2026
…t of #665)

- An empty SharedFunctionalGroupsSequence array counts as missing. A
  one-item array gets the list accessors (addAccessors), so the shared
  groups the normalizer sets are written. A plain object stays as is.
- The per-frame sequence is filled up to NumberOfFrames. Existing items
  keep their content, and only an item without a FrameContentSequence
  gets one.
- A null per-frame item becomes an empty item; a null item after
  NumberOfFrames is kept and the window guess does not throw.
- planePositionGroup copies an array ImagePositionPatient per frame and
  passes a non-array value through.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.22 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant