@@ -208,10 +208,16 @@ def from_filename(cls, filename, fmt="X5", reference=None, moving=None, x5_chain
208208
209209 retval = []
210210 if fmt and fmt .upper () == "X5" :
211- xfm_list = load_x5 (filename )
211+ # Get list of X5 nodes and generate transforms
212+ xfm_list = [
213+ globals ()[f"{ node .type } _from_x5" ]([node ]) for node in load_x5 (filename )
214+ ]
212215 if not xfm_list :
213216 raise TransformError ("Empty transform group" )
214217
218+ if x5_chain is None :
219+ return xfm_list
220+
215221 with h5py .File (str (filename ), "r" ) as f :
216222 chain_grp = f .get ("TransformChain" )
217223 if chain_grp is None :
@@ -221,12 +227,7 @@ def from_filename(cls, filename, fmt="X5", reference=None, moving=None, x5_chain
221227 if isinstance (chain_path , bytes ):
222228 chain_path = chain_path .decode ()
223229
224- for idx in chain_path .split ("/" ):
225- node = x5io ._read_x5_group (xfm_list [int (idx )])
226- from_x5 = globals ()[f"{ node .type } _from_x5" ]
227- retval .append (from_x5 ([node ]))
228-
229- return TransformChain (retval )
230+ return TransformChain ([xfm_list [int (idx )] for idx in chain_path .split ("/" )])
230231
231232 if str (filename ).endswith (".h5" ):
232233 reference = None
@@ -247,9 +248,14 @@ def to_filename(self, filename, fmt="X5"):
247248 if fmt .upper () != "X5" :
248249 raise NotImplementedError ("Only X5 format is supported for chains" )
249250
250- existing = load_x5 (filename ) if os .path .exists (filename ) else []
251+ existing = (
252+ self .from_filename (filename , x5_chain = None )
253+ if os .path .exists (filename )
254+ else []
255+ )
256+
251257 xfm_chain = []
252- new_nodes = []
258+ new_xfms = []
253259 next_xfm_index = len (existing )
254260 for xfm in self .transforms :
255261 for eidx , existing_xfm in enumerate (existing ):
@@ -258,7 +264,7 @@ def to_filename(self, filename, fmt="X5"):
258264 break
259265 else :
260266 xfm_chain .append (next_xfm_index )
261- new_nodes .append ((next_xfm_index , xfm ))
267+ new_xfms .append ((next_xfm_index , xfm ))
262268 existing .append (xfm )
263269 next_xfm_index += 1
264270
@@ -269,9 +275,9 @@ def to_filename(self, filename, fmt="X5"):
269275 f .attrs ["Version" ] = np .uint16 (1 )
270276
271277 tg = f .require_group ("TransformGroup" )
272- for idx , node in new_nodes :
278+ for idx , node in new_xfms :
273279 g = tg .create_group (str (idx ))
274- x5io ._write_x5_group (g , node )
280+ x5io ._write_x5_group (g , node . to_x5 () )
275281
276282 cg = f .require_group ("TransformChain" )
277283 cg .create_dataset (str (len (cg )), data = "/" .join (str (i ) for i in xfm_chain ))
0 commit comments