@@ -78,6 +78,7 @@ def from_paths(
7878 imread_kwargs : Mapping [str , Any ],
7979 skip_rounds : list [int ] | None = None ,
8080 ) -> MultiChannelImage :
81+ valid_files : list [Path ] = []
8182 channel_metadata : list [ChannelMetadata ] = []
8283 for p in path_files :
8384 try :
@@ -90,6 +91,7 @@ def from_paths(
9091 )
9192 continue
9293
94+ valid_files .append (p )
9395 channel_metadata .append (
9496 ChannelMetadata (
9597 name = metadata ["name" ],
@@ -103,34 +105,33 @@ def from_paths(
103105 )
104106 )
105107
106- if len (path_files ) != len (channel_metadata ):
107- raise ValueError ("Length of path_files and metadata must be the same." )
108- # if any of round_channels is in skip_rounds, remove that round from the list and from path_files
108+ if not valid_files :
109+ raise ValueError ("No valid files were found." )
110+ if len (valid_files ) != len (channel_metadata ):
111+ raise ValueError ("Length of valid files and metadata must be the same." )
112+ # if any of round_channels is in skip_rounds, remove that round from the list and from valid_files
109113 if skip_rounds :
110114 logger .info (f"Skipping cycles: { skip_rounds } " )
111- path_files , channel_metadata = map (
115+ valid_files , channel_metadata = map (
112116 list ,
113117 zip (
114118 * [
115119 (p , ch_meta )
116- for p , ch_meta in zip (path_files , channel_metadata , strict = True )
120+ for p , ch_meta in zip (valid_files , channel_metadata , strict = True )
117121 if ch_meta .cycle not in skip_rounds
118122 ],
119123 strict = True ,
120124 ),
121125 )
122- imgs = [imread (img , ** imread_kwargs ) for img in path_files ]
123- for img , path in zip (imgs , path_files , strict = True ):
126+ imgs = [imread (img , ** imread_kwargs ) for img in valid_files ]
127+ for img , path in zip (imgs , valid_files , strict = True ):
124128 if img .shape [1 :] != imgs [0 ].shape [1 :]:
125129 raise ValueError (
126130 f"Images are not all the same size. Image { path } has shape { img .shape [1 :]} while the first image "
127- f"{ path_files [0 ]} has shape { imgs [0 ].shape [1 :]} "
131+ f"{ valid_files [0 ]} has shape { imgs [0 ].shape [1 :]} "
128132 )
129133 # create MultiChannelImage object with imgs and metadata
130- output = cls (
131- data = imgs ,
132- metadata = channel_metadata ,
133- )
134+ output = cls (data = imgs , metadata = channel_metadata )
134135 return output
135136
136137 @classmethod
@@ -695,7 +696,16 @@ def create_sdata(
695696
696697 with warnings .catch_warnings ():
697698 warnings .simplefilter ("ignore" )
698- pixels_to_microns = parse_physical_size (path_files [0 ])
699+ # Iterate over path files, as it may still contain invalid files
700+ pixels_to_microns = None
701+ for p in path_files :
702+ try :
703+ pixels_to_microns = parse_physical_size (p )
704+ except Exception :
705+ logger .debug (f"Could not parse physical size from { p } . Trying next file." )
706+ continue
707+ if pixels_to_microns is None :
708+ raise ValueError ("Could not parse physical size from any file" )
699709
700710 image_element = create_image_element (
701711 mci ,
0 commit comments