Repository navigation
fix(cornerstone): decode ECG waveform bulkdata through the data source #6346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+227
−303
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9049509
fix(cornerstone): decode ECG waveform bulkdata through the data source
wayfarer3130 f3773c4
Merge remote-tracking branch 'origin/master' into fix/ecg-waveform-bu…
wayfarer3130 6ca0462
fix(cornerstone): cache ECG waveform bulkdata and report load failures
wayfarer3130 da59b56
Merge remote-tracking branch 'origin/master' into fix/ecg-waveform-bu…
wayfarer3130 1747e78
Merge branch 'master' into fix/ecg-waveform-bulkdata
jbocce e05d356
feat(cornerstone): pass the ECG trace layout from the hanging protoco…
wayfarer3130 b317d9f
Merge remote-tracking branch 'origin/fix/ecg-waveform-bulkdata' into …
wayfarer3130 e7cd14c
fix(cornerstone): report a failed ECG mount, and defer the ECG trace …
wayfarer3130 c5c84d7
Merge branch 'master' into fix/ecg-waveform-bulkdata
wayfarer3130 6e49ed0
fix(hanging-protocols): rank image, ECG and SM display sets first in …
wayfarer3130 e662b12
fix(cornerstone): skip the orientation markers until the viewport has…
wayfarer3130 a96c66d
revert(hanging-protocols): move the mnGrid rule change to its own PR
wayfarer3130 12980b1
fix(cornerstone): log a failed viewport re-mount instead of an unhand…
wayfarer3130 89b2d41
Merge branch 'master' into fix/ecg-waveform-bulkdata
wayfarer3130 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,217 +1,137 @@ | ||
| import { decodeInt16Multiplex, base64ToArrayBuffer, buildEcgModule } from './ecgMetadata'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // decodeInt16Multiplex | ||
| // --------------------------------------------------------------------------- | ||
| describe('decodeInt16Multiplex', () => { | ||
| it('demultiplexes 2 channels correctly', () => { | ||
| // Interleaved layout: [ch0_s0, ch1_s0, ch0_s1, ch1_s1, ch0_s2, ch1_s2] | ||
| const interleaved = new Int16Array([10, 20, 30, 40, 50, 60]); | ||
| const channels = decodeInt16Multiplex(interleaved.buffer, 2, 3); | ||
|
|
||
| expect(channels).toHaveLength(2); | ||
| expect(Array.from(channels[0])).toEqual([10, 30, 50]); | ||
| expect(Array.from(channels[1])).toEqual([20, 40, 60]); | ||
| }); | ||
|
|
||
| it('handles a single channel', () => { | ||
| const data = new Int16Array([100, 200, 300]); | ||
| const channels = decodeInt16Multiplex(data.buffer, 1, 3); | ||
|
|
||
| expect(channels).toHaveLength(1); | ||
| expect(Array.from(channels[0])).toEqual([100, 200, 300]); | ||
| }); | ||
|
|
||
| it('handles negative values (signed short)', () => { | ||
| const data = new Int16Array([-1000, -2000, -3000]); | ||
| const channels = decodeInt16Multiplex(data.buffer, 1, 3); | ||
|
|
||
| expect(Array.from(channels[0])).toEqual([-1000, -2000, -3000]); | ||
| }); | ||
|
|
||
| it('returns empty arrays when numberOfChannels or numberOfSamples is 0', () => { | ||
| const emptyBuffer = new Int16Array([]).buffer; | ||
|
|
||
| expect(decodeInt16Multiplex(emptyBuffer, 0, 0)).toHaveLength(0); | ||
| expect(decodeInt16Multiplex(emptyBuffer, 0, 10)).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('produces Int16Array instances for each channel', () => { | ||
| const data = new Int16Array([1, 2]); | ||
| const channels = decodeInt16Multiplex(data.buffer, 2, 1); | ||
|
|
||
| expect(channels[0]).toBeInstanceOf(Int16Array); | ||
| expect(channels[1]).toBeInstanceOf(Int16Array); | ||
| }); | ||
| import { utilities as csMetadataUtilities } from '@cornerstonejs/metadata'; | ||
| import { buildEcgModule } from './ecgMetadata'; | ||
|
|
||
| jest.mock('@cornerstonejs/metadata', () => { | ||
| const actual = jest.requireActual('@cornerstonejs/metadata'); | ||
| const { buildEcgModuleFromInstance } = actual.utilities; | ||
| return { | ||
| ...actual, | ||
| utilities: { ...actual.utilities, buildEcgModuleFromInstance: jest.fn(buildEcgModuleFromInstance) }, | ||
| }; | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // base64ToArrayBuffer | ||
| // --------------------------------------------------------------------------- | ||
| describe('base64ToArrayBuffer', () => { | ||
| it('decodes a simple base64 string to the correct bytes', () => { | ||
| // "ABC" in ASCII = [65, 66, 67]; base64 of that is 'QUJD' | ||
| const buffer = base64ToArrayBuffer('QUJD'); | ||
| const bytes = new Uint8Array(buffer); | ||
|
|
||
| expect(bytes).toHaveLength(3); | ||
| expect(Array.from(bytes)).toEqual([65, 66, 67]); | ||
| }); | ||
|
|
||
| it('round-trips an Int16Array through base64', () => { | ||
| const original = new Int16Array([500, -300, 0, 32767]); | ||
| const base64 = btoa(String.fromCharCode(...new Uint8Array(original.buffer))); | ||
| const recovered = new Int16Array(base64ToArrayBuffer(base64)); | ||
|
|
||
| expect(Array.from(recovered)).toEqual(Array.from(original)); | ||
| }); | ||
|
|
||
| it('returns an ArrayBuffer', () => { | ||
| const result = base64ToArrayBuffer('AA=='); // single zero byte | ||
| expect(result).toBeInstanceOf(ArrayBuffer); | ||
| expect(result.byteLength).toBe(1); | ||
| }); | ||
| // Two channels, two samples, interleaved: ch0=[1, -2], ch1=[3, 4]. | ||
| const SAMPLES = new Int16Array([1, 3, -2, 4]); | ||
|
|
||
| const makeInstance = WaveformData => ({ | ||
| WaveformSequence: [ | ||
| { | ||
| NumberOfWaveformChannels: 2, | ||
| NumberOfWaveformSamples: 2, | ||
| SamplingFrequency: 500, | ||
| WaveformBitsAllocated: 16, | ||
| WaveformSampleInterpretation: 'SS', | ||
| MultiplexGroupLabel: '12 Lead ECG', | ||
| ChannelDefinitionSequence: [ | ||
| { ChannelSourceSequence: [{ CodeMeaning: 'Lead I' }] }, | ||
| { ChannelSourceSequence: [{ CodeMeaning: 'Lead II' }] }, | ||
| ], | ||
| WaveformData, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // buildEcgModule | ||
| // --------------------------------------------------------------------------- | ||
| describe('buildEcgModule', () => { | ||
| const makeInstance = (overrides = {}) => ({ | ||
| WaveformSequence: [ | ||
| { | ||
| NumberOfWaveformChannels: 12, | ||
| NumberOfWaveformSamples: 5000, | ||
| SamplingFrequency: 500, | ||
| WaveformBitsAllocated: 16, | ||
| WaveformSampleInterpretation: 'SS', | ||
| MultiplexGroupLabel: '12 Lead ECG', | ||
| ChannelDefinitionSequence: [ | ||
| { ChannelSourceSequence: [{ CodeMeaning: 'Lead I' }] }, | ||
| { ChannelSourceSequence: [{ CodeMeaning: 'Lead II' }] }, | ||
| ], | ||
| WaveformData: { InlineBinary: btoa('\x00\x01') }, | ||
| }, | ||
| ], | ||
| ...overrides, | ||
| }); | ||
| const channelsOf = (channels: Int16Array[]) => channels.map(channel => Array.from(channel)); | ||
|
|
||
| describe('buildEcgModule', () => { | ||
| it('returns null when WaveformSequence is absent', () => { | ||
| expect(buildEcgModule({})).toBeNull(); | ||
| expect(buildEcgModule({ WaveformSequence: [] })).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns a valid ecgModule for a complete instance', () => { | ||
| const module = buildEcgModule(makeInstance()); | ||
| it('returns the waveform attributes and the lead names', () => { | ||
| const module = buildEcgModule(makeInstance({})); | ||
|
|
||
| expect(module).not.toBeNull(); | ||
| expect(module!.numberOfWaveformChannels).toBe(12); | ||
| expect(module!.numberOfWaveformSamples).toBe(5000); | ||
| expect(module!.numberOfWaveformChannels).toBe(2); | ||
| expect(module!.numberOfWaveformSamples).toBe(2); | ||
| expect(module!.samplingFrequency).toBe(500); | ||
| expect(module!.waveformBitsAllocated).toBe(16); | ||
| expect(module!.waveformSampleInterpretation).toBe('SS'); | ||
| expect(module!.multiplexGroupLabel).toBe('12 Lead ECG'); | ||
| expect(module!.channelDefinitionSequence).toHaveLength(2); | ||
| expect(module!.channelDefinitionSequence[0].channelSourceSequence.codeMeaning).toBe('Lead I'); | ||
| expect(typeof module!.waveformData.retrieveBulkData).toBe('function'); | ||
| }); | ||
|
|
||
| it('applies defaults for missing optional fields', () => { | ||
| const instance = { | ||
| WaveformSequence: [ | ||
| { | ||
| // No explicit values — all defaults | ||
| WaveformData: { InlineBinary: btoa('\x00\x00') }, | ||
| }, | ||
| ], | ||
| }; | ||
| const module = buildEcgModule(instance); | ||
| it('decodes InlineBinary data', async () => { | ||
| const base64 = btoa(String.fromCharCode(...new Uint8Array(SAMPLES.buffer))); | ||
| const module = buildEcgModule(makeInstance({ InlineBinary: base64 })); | ||
|
|
||
| expect(module!.numberOfWaveformChannels).toBe(0); | ||
| expect(module!.numberOfWaveformSamples).toBe(0); | ||
| expect(module!.samplingFrequency).toBe(1); | ||
| expect(module!.waveformBitsAllocated).toBe(16); | ||
| expect(module!.waveformSampleInterpretation).toBe('SS'); | ||
| expect(module!.multiplexGroupLabel).toBe(''); | ||
| expect(module!.channelDefinitionSequence).toHaveLength(0); | ||
| expect(channelsOf(await module!.waveformData.retrieveBulkData())).toEqual([ | ||
| [1, -2], | ||
| [3, 4], | ||
| ]); | ||
| }); | ||
|
|
||
| it('retrieveBulkData decodes InlineBinary data correctly', async () => { | ||
| const samples = new Int16Array([10, 20, 30]); | ||
| const base64 = btoa(String.fromCharCode(...new Uint8Array(samples.buffer))); | ||
|
|
||
| const instance = { | ||
| WaveformSequence: [ | ||
| { | ||
| NumberOfWaveformChannels: 1, | ||
| NumberOfWaveformSamples: 3, | ||
| WaveformData: { InlineBinary: base64 }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const module = buildEcgModule(instance); | ||
| const channels = await module!.waveformData.retrieveBulkData(); | ||
| it('decodes the bytes that the data source retrieveBulkData returns', async () => { | ||
| const retrieveBulkData = jest.fn().mockResolvedValue(SAMPLES.buffer.slice(0)); | ||
| const module = buildEcgModule( | ||
| makeInstance({ BulkDataURI: 'http://example.com/waveform', retrieveBulkData }) | ||
| ); | ||
|
|
||
| expect(channels).toHaveLength(1); | ||
| expect(Array.from(channels[0])).toEqual([10, 20, 30]); | ||
| expect(channelsOf(await module!.waveformData.retrieveBulkData())).toEqual([ | ||
| [1, -2], | ||
| [3, 4], | ||
| ]); | ||
| expect(retrieveBulkData).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('retrieveBulkData returns [] when WaveformData is absent', async () => { | ||
| const instance = { | ||
| WaveformSequence: [ | ||
| { NumberOfWaveformChannels: 1, NumberOfWaveformSamples: 1 }, | ||
| ], | ||
| }; | ||
| const module = buildEcgModule(instance); | ||
| const channels = await module!.waveformData.retrieveBulkData(); | ||
| it('decodes a typed array from the data source retrieveBulkData', async () => { | ||
| const bytes = new Uint8Array(SAMPLES.buffer.slice(0)); | ||
| const module = buildEcgModule( | ||
| makeInstance({ BulkDataURI: 'x', retrieveBulkData: jest.fn().mockResolvedValue(bytes) }) | ||
| ); | ||
|
|
||
| expect(channels).toEqual([]); | ||
| expect(channelsOf(await module!.waveformData.retrieveBulkData())).toEqual([ | ||
| [1, -2], | ||
| [3, 4], | ||
| ]); | ||
| }); | ||
|
|
||
| it('retrieveBulkData fetches BulkDataURI with auth header when provided', async () => { | ||
| const samples = new Int16Array([1, 2]); | ||
| global.fetch = jest.fn().mockResolvedValue({ | ||
| ok: true, | ||
| arrayBuffer: () => Promise.resolve(samples.buffer), | ||
| it('uses the cached Value and does not fetch again on a re-mount', async () => { | ||
| const retrieveBulkData = jest.fn(); | ||
| const module = buildEcgModule( | ||
| makeInstance({ BulkDataURI: 'x', Value: SAMPLES.buffer.slice(0), retrieveBulkData }) | ||
| ); | ||
|
|
||
| await module!.waveformData.retrieveBulkData(); | ||
| expect(channelsOf(await module!.waveformData.retrieveBulkData())).toEqual([ | ||
| [1, -2], | ||
| [3, 4], | ||
| ]); | ||
| expect(retrieveBulkData).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('gives the auth headers to the Cornerstone bulkdata fetch', () => { | ||
| const builder = csMetadataUtilities.buildEcgModuleFromInstance as jest.Mock; | ||
| const authHeader = { Authorization: 'Bearer token' }; | ||
| buildEcgModule(makeInstance({ BulkDataURI: 'x' }), { | ||
| getAuthorizationHeader: () => authHeader, | ||
| }); | ||
|
|
||
| const userAuthenticationService = { | ||
| getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }), | ||
| }; | ||
| const instance = { | ||
| WaveformSequence: [ | ||
| { | ||
| NumberOfWaveformChannels: 1, | ||
| NumberOfWaveformSamples: 2, | ||
| WaveformData: { BulkDataURI: 'http://example.com/waveform' }, | ||
| }, | ||
| ], | ||
| }; | ||
| const options = builder.mock.lastCall[2] as { getHeaders: () => unknown }; | ||
| expect(options.getHeaders()).toBe(authHeader); | ||
| }); | ||
|
|
||
| const module = buildEcgModule(instance, userAuthenticationService); | ||
| await module!.waveformData.retrieveBulkData(); | ||
| it('reads lead names from a lowercase codeMeaning', () => { | ||
| const instance = makeInstance({}); | ||
| instance.WaveformSequence[0].ChannelDefinitionSequence[0].ChannelSourceSequence = [ | ||
| { codeMeaning: 'Lead I' } as never, | ||
| ]; | ||
|
|
||
| expect(global.fetch).toHaveBeenCalledWith('http://example.com/waveform', { | ||
| headers: { | ||
| Accept: 'application/octet-stream', | ||
| Authorization: 'Bearer token123', | ||
| }, | ||
| }); | ||
| const module = buildEcgModule(instance); | ||
|
|
||
| expect(module!.channelDefinitionSequence[0].channelSourceSequence.codeMeaning).toBe('Lead I'); | ||
| }); | ||
|
|
||
| it('retrieveBulkData throws when BulkDataURI response is not ok', async () => { | ||
| global.fetch = jest.fn().mockResolvedValue({ ok: false, status: 403 }); | ||
| it('passes the lead code value and coding scheme', () => { | ||
| const instance = makeInstance({}); | ||
| instance.WaveformSequence[0].ChannelDefinitionSequence[1].ChannelSourceSequence = [ | ||
| { CodeValue: '5.6.3-9-2', CodingSchemeDesignator: 'SCPECG', CodeMeaning: 'Lead II' } as never, | ||
| ]; | ||
|
|
||
| const instance = { | ||
| WaveformSequence: [ | ||
| { | ||
| WaveformData: { BulkDataURI: 'http://example.com/waveform' }, | ||
| }, | ||
| ], | ||
| const source = buildEcgModule(instance)!.channelDefinitionSequence[1].channelSourceSequence as { | ||
| codeValue?: string; | ||
| codingSchemeDesignator?: string; | ||
| }; | ||
|
|
||
| const module = buildEcgModule(instance); | ||
| await expect(module!.waveformData.retrieveBulkData()).rejects.toThrow('403'); | ||
| expect(source.codeValue).toBe('5.6.3-9-2'); | ||
| expect(source.codingSchemeDesignator).toBe('SCPECG'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.