hdmf icon indicating copy to clipboard operation
hdmf copied to clipboard

[Feature]: Add dictionary methods to MultiContainerInterface

Open rly opened this issue 1 year ago • 1 comments

What would you like to see added to HDMF?

Classes that extend MultiContainerInterface gain a __getitem__ which provides a convenient interface for accessing an item within it by name. This works only for MultiContainerInterface classes that have one single multi-container attribute (i.e., a single element in __clsconf__) (code). In PyNWB, that is almost all of them (except for NWBFile and possible extensions).

This allows you to do:

position["spatial_series1"]

Instead of:

position.spatial_series["spatial_series1"]

However, if you want to iterate through the contained objects, you have to use the attribute, which is a labelled dictionary:

for spatial_series_name in position.spatial_series:
    print(spatial_series_name)
for spatial_series in position.spatial_series.values():
    print(spatial_series)

It would be nice to be able to do just:

for spatial_series_name in position:
    print(spatial_series_name)
for spatial_series in position.values():
    print(spatial_series)

What solution would you like?

See above.

Do you have any interest in helping implement the feature?

Yes.

rly avatar Aug 02 '24 20:08 rly

This comes from ndx-pose where a TrainingFrame contains a SkeletonInstances object, which is a MultiContainerInterface of SkeletonInstance objects. Iterating through this requires the syntax:

for instance in training_frame.skeleton_instances.skeleton_instances.values()
	print(instance)

The naming makes this even more awkward.

rly avatar Aug 02 '24 20:08 rly