@@ -10,23 +10,24 @@ import { useDatasetStore } from '@/src/store/datasets';
1010import { useDICOMStore } from '@/src/store/datasets-dicom' ;
1111import { useLayersStore } from '@/src/store/datasets-layers' ;
1212import { useSegmentGroupStore } from '@/src/store/segmentGroups' ;
13- import { wrapInArray , nonNullable } from '@/src/utils' ;
13+ import { wrapInArray , nonNullable , partition } from '@/src/utils' ;
1414import { basename } from '@/src/utils/path' ;
1515import { parseUrl } from '@/src/utils/url' ;
1616import { logError } from '@/src/utils/loggers' ;
17- import { PipelineResultSuccess , partitionResults } from '@/src/core/pipeline' ;
1817import {
19- ImportDataSourcesResult ,
2018 importDataSources ,
2119 toDataSelection ,
2220} from '@/src/io/import/importDataSources' ;
2321import {
22+ ErrorResult ,
2423 ImportResult ,
2524 LoadableResult ,
26- VolumeResult ,
25+ LoadableVolumeResult ,
2726 isLoadableResult ,
2827 isVolumeResult ,
28+ ImportDataSourcesResult ,
2929} from '@/src/io/import/common' ;
30+ import { isDicomImage } from '@/src/utils/dataSelection' ;
3031
3132// higher value priority is preferred for picking a primary selection
3233const BASE_MODALITY_TYPES = {
@@ -38,8 +39,8 @@ const BASE_MODALITY_TYPES = {
3839
3940function findBaseDicom ( loadableDataSources : Array < LoadableResult > ) {
4041 // find dicom dataset for primary selection if available
41- const dicoms = loadableDataSources . filter (
42- ( { dataType } ) => dataType === 'dicom'
42+ const dicoms = loadableDataSources . filter ( ( { dataID } ) =>
43+ isDicomImage ( dataID )
4344 ) ;
4445 // prefer some modalities as base
4546 const dicomStore = useDICOMStore ( ) ;
@@ -97,19 +98,15 @@ function findBaseImage(
9798}
9899
99100// returns image and dicom sources, no config files
100- function filterLoadableDataSources (
101- succeeded : Array < PipelineResultSuccess < ImportResult > >
102- ) {
103- return succeeded . flatMap ( ( result ) => {
104- return result . data . filter ( isLoadableResult ) ;
105- } ) ;
101+ function filterLoadableDataSources ( succeeded : Array < ImportResult > ) {
102+ return succeeded . filter ( isLoadableResult ) ;
106103}
107104
108105// Returns list of dataSources with file names where the name has the extension argument
109106// and the start of the file name matches the primary file name.
110107function filterMatchingNames (
111- primaryDataSource : VolumeResult ,
112- succeeded : Array < PipelineResultSuccess < ImportResult > > ,
108+ primaryDataSource : LoadableVolumeResult ,
109+ succeeded : Array < ImportResult > ,
113110 extension : string
114111) {
115112 const primaryName = getDataSourceName ( primaryDataSource . dataSource ) ;
@@ -137,7 +134,7 @@ function getStudyUID(volumeID: string) {
137134}
138135
139136function findBaseDataSource (
140- succeeded : Array < PipelineResultSuccess < ImportResult > > ,
137+ succeeded : Array < ImportResult > ,
141138 segmentGroupExtension : string
142139) {
143140 const loadableDataSources = filterLoadableDataSources ( succeeded ) ;
@@ -151,24 +148,24 @@ function findBaseDataSource(
151148
152149function filterOtherVolumesInStudy (
153150 volumeID : string ,
154- succeeded : Array < PipelineResultSuccess < ImportResult > >
151+ succeeded : Array < ImportResult >
155152) {
156153 const targetStudyUID = getStudyUID ( volumeID ) ;
157154 const dicomDataSources = filterLoadableDataSources ( succeeded ) . filter (
158- ( { dataType } ) => dataType === 'dicom'
155+ ( { dataID } ) => isDicomImage ( dataID )
159156 ) ;
160157 return dicomDataSources . filter ( ( ds ) => {
161158 const sourceStudyUID = getStudyUID ( ds . dataID ) ;
162159 return sourceStudyUID === targetStudyUID && ds . dataID !== volumeID ;
163- } ) as Array < VolumeResult > ;
160+ } ) as Array < LoadableVolumeResult > ;
164161}
165162
166163// Layers a DICOM PET on a CT if found
167164function loadLayers (
168- primaryDataSource : VolumeResult ,
169- succeeded : Array < PipelineResultSuccess < ImportResult > >
165+ primaryDataSource : LoadableVolumeResult ,
166+ succeeded : Array < ImportResult >
170167) {
171- if ( primaryDataSource . dataType !== 'dicom' ) return ;
168+ if ( ! isDicomImage ( primaryDataSource . dataID ) ) return ;
172169 const otherVolumesInStudy = filterOtherVolumesInStudy (
173170 primaryDataSource . dataID ,
174171 succeeded
@@ -194,8 +191,8 @@ function loadLayers(
194191// - DICOM SEG modalities with matching StudyUIDs.
195192// - DataSources that have a name like foo.segmentation.bar and the primary DataSource is named foo.baz
196193function loadSegmentations (
197- primaryDataSource : VolumeResult ,
198- succeeded : Array < PipelineResultSuccess < ImportResult > > ,
194+ primaryDataSource : LoadableVolumeResult ,
195+ succeeded : Array < ImportResult > ,
199196 segmentGroupExtension : string
200197) {
201198 const matchingNames = filterMatchingNames (
@@ -233,13 +230,19 @@ function loadDataSources(sources: DataSource[]) {
233230
234231 let results : ImportDataSourcesResult [ ] ;
235232 try {
236- results = await importDataSources ( sources ) ;
233+ results = ( await importDataSources ( sources ) ) . filter ( ( result ) =>
234+ // only look at data and error results
235+ [ 'data' , 'error' ] . includes ( result . type )
236+ ) ;
237237 } catch ( error ) {
238238 loadDataStore . setError ( error as Error ) ;
239239 return ;
240240 }
241241
242- const [ succeeded , errored ] = partitionResults ( results ) ;
242+ const [ succeeded , errored ] = partition (
243+ ( result ) => result . type !== 'error' ,
244+ results
245+ ) ;
243246
244247 if ( ! dataStore . primarySelection && succeeded . length ) {
245248 const primaryDataSource = findBaseDataSource (
@@ -260,14 +263,12 @@ function loadDataSources(sources: DataSource[]) {
260263 }
261264
262265 if ( errored . length ) {
263- const errorMessages = errored . map ( ( errResult ) => {
264- // pick first error
265- const [ firstError ] = errResult . errors ;
266- // pick innermost dataset that errored
267- const name = getDataSourceName ( firstError . inputDataStackTrace [ 0 ] ) ;
266+ const errorMessages = ( errored as ErrorResult [ ] ) . map ( ( errResult ) => {
267+ const { dataSource, error } = errResult ;
268+ const name = getDataSourceName ( dataSource ) ;
268269 // log error for debugging
269- logError ( firstError . cause ) ;
270- return `- ${ name } : ${ firstError . message } ` ;
270+ logError ( error ) ;
271+ return `- ${ name } : ${ error . message } ` ;
271272 } ) ;
272273 const failedError = new Error (
273274 `These files failed to load:\n${ errorMessages . join ( '\n' ) } `
0 commit comments