@@ -227,6 +227,7 @@ def read_block(self, lazy=False):
227227 bl_struct = d ['block' ]
228228 bl = self .create_ob_from_struct (
229229 bl_struct , 'Block' )
230+ self ._resolve_references (bl )
230231 bl .check_relationships ()
231232 return bl
232233
@@ -242,38 +243,23 @@ def write_block(self, bl, **kargs):
242243 seg_struct = self .create_struct_from_obj (seg )
243244 bl_struct ['segments' ].append (seg_struct )
244245
245- for anasig in seg .analogsignals :
246- anasig_struct = self .create_struct_from_obj (anasig )
247- seg_struct ['analogsignals' ].append (anasig_struct )
248-
249- for irrsig in seg .irregularlysampledsignals :
250- irrsig_struct = self .create_struct_from_obj (irrsig )
251- seg_struct ['irregularlysampledsignals' ].append (irrsig_struct )
252-
253- for ea in seg .events :
254- ea_struct = self .create_struct_from_obj (ea )
255- seg_struct ['events' ].append (ea_struct )
256-
257- for ea in seg .epochs :
258- ea_struct = self .create_struct_from_obj (ea )
259- seg_struct ['epochs' ].append (ea_struct )
260-
261- for sptr in seg .spiketrains :
262- sptr_struct = self .create_struct_from_obj (sptr )
263- seg_struct ['spiketrains' ].append (sptr_struct )
264-
265- for image_sq in seg .imagesequences :
266- image_sq_structure = self .create_struct_from_obj (image_sq )
267- seg_struct ['imagesequences' ].append (image_sq_structure )
246+ for container_name in seg ._child_containers :
247+ for child_obj in getattr (seg , container_name ):
248+ child_struct = self .create_struct_from_obj (child_obj )
249+ seg_struct [container_name ].append (child_struct )
268250
269251 for group in bl .groups :
270252 group_structure = self .create_struct_from_obj (group )
271253 bl_struct ['groups' ].append (group_structure )
272254
255+ for container_name in group ._child_containers :
256+ for child_obj in getattr (group , container_name ):
257+ group_structure [container_name ].append (id (child_obj ))
258+
273259 scipy .io .savemat (self .filename , {'block' : bl_struct }, oned_as = 'row' )
274260
275261 def create_struct_from_obj (self , ob ):
276- struct = {}
262+ struct = {"neo_id" : id ( ob ) }
277263
278264 # relationship
279265 for childname in getattr (ob , '_child_containers' , []):
@@ -290,11 +276,6 @@ def create_struct_from_obj(self, ob):
290276 for i , attr in enumerate (all_attrs ):
291277 attrname , attrtype = attr [0 ], attr [1 ]
292278
293- # ~ if attrname =='':
294- # ~ struct['array'] = ob.magnitude
295- # ~ struct['units'] = ob.dimensionality.string
296- # ~ continue
297-
298279 if (hasattr (ob , '_quantity_attr' ) and
299280 ob ._quantity_attr == attrname ):
300281 struct [attrname ] = ob .magnitude
@@ -320,13 +301,6 @@ def create_struct_from_obj(self, ob):
320301
321302 def create_ob_from_struct (self , struct , classname ):
322303 cl = class_by_name [classname ]
323- # check if inherits Quantity
324- # ~ is_quantity = False
325- # ~ for attr in cl._necessary_attrs:
326- # ~ if attr[0] == '' and attr[1] == pq.Quantity:
327- # ~ is_quantity = True
328- # ~ break
329- # ~ is_quantiy = hasattr(cl, '_quantity_attr')
330304
331305 # ~ if is_quantity:
332306 if hasattr (cl , '_quantity_attr' ):
@@ -374,20 +348,27 @@ def create_ob_from_struct(self, struct, classname):
374348 # check children
375349 if attrname in getattr (ob , '_child_containers' , []):
376350 child_struct = getattr (struct , attrname )
351+ child_class_name = classname_lower_to_upper [attrname [:- 1 ]]
377352 try :
378353 # try must only surround len() or other errors are captured
379354 child_len = len (child_struct )
380355 except TypeError :
381356 # strange scipy.io behavior: if len is 1 there is no len()
382- child = self .create_ob_from_struct (
383- child_struct ,
384- classname_lower_to_upper [attrname [:- 1 ]])
357+ if classname == "Group" :
358+ child = _Ref (child_struct , child_class_name )
359+ else :
360+ child = self .create_ob_from_struct (
361+ child_struct ,
362+ child_class_name )
385363 getattr (ob , attrname .lower ()).append (child )
386364 else :
387365 for c in range (child_len ):
388- child = self .create_ob_from_struct (
389- child_struct [c ],
390- classname_lower_to_upper [attrname [:- 1 ]])
366+ if classname == "Group" :
367+ child = _Ref (child_struct [c ], child_class_name )
368+ else :
369+ child = self .create_ob_from_struct (
370+ child_struct [c ],
371+ child_class_name )
391372 getattr (ob , attrname .lower ()).append (child )
392373 continue
393374
@@ -432,4 +413,31 @@ def create_ob_from_struct(self, struct, classname):
432413
433414 setattr (ob , attrname , item )
434415
416+ neo_id = getattr (struct , "neo_id" , None )
417+ if neo_id :
418+ setattr (ob , "_id" , neo_id )
435419 return ob
420+
421+ def _resolve_references (self , bl ):
422+ if bl .groups :
423+ obj_lookup = {}
424+ for ob in bl .children_recur :
425+ if hasattr (ob , "_id" ):
426+ obj_lookup [ob ._id ] = ob
427+ for grp in bl .groups :
428+ for container_name in grp ._child_containers :
429+ container = getattr (grp , container_name )
430+ for i , ref in enumerate (container ):
431+ assert isinstance (ref , _Ref )
432+ container [i ] = obj_lookup [ref .identifier ]
433+
434+
435+ class _Ref :
436+
437+ def __init__ (self , identifier , target_class_name ):
438+ self .identifier = identifier
439+ self .target_cls = class_by_name [target_class_name ]
440+
441+ @property
442+ def proxy_for (self ):
443+ return self .target_cls
0 commit comments