NeurodataWithoutBorders / pynwb

A Python API for working with Neurodata stored in the NWB Format
https://pynwb.readthedocs.io
Other
174 stars 85 forks source link

Programmatically find all the time series in any NWB file #560

Open afonsobspinto opened 6 years ago

afonsobspinto commented 6 years ago

Hello everyone. I've been using your API to develop my GSoC project and one of my tasks was to find a way to programmatically find all the time series present in any NWB 2.x file. I've tried to develop this as follows:

def _get_data_interfaces(self, node):
        """Given a NWBHDF5IO returns all the data_interfaces objects presents on it."""
        data_interfaces_list = []
        for child in node.children:
            if isinstance(child, NWBDataInterface):
                data_interfaces_list.append(child)
            data_interfaces_list += self._get_data_interfaces(child)
       return data_interfaces_list

def _get_timeseries(self):
        """Given all the nwb_data_interfaces returns all of those that are timeseries objects."""
        time_series_list = []
        for data_interface in self.nwb_data_interfaces_list:
            if isinstance(data_interface, TimeSeries):
                time_series_list.append(data_interface)
return time_series_list

I believe this returns all the TimeSeries objects, then I simply run over each one of them to get mono dimensional time series easily plottable:

def get_mono_dimensional_timeseries(self, values):
        """Given a timeseries object returns all mono dimensional timeseries presents on it."""
        mono_time_series_list = []
        if isinstance(values, collections.Iterable):
            try:
                data = [float(i) for i in values]
                mono_time_series_list.append(data)
            except:
                for inner_list in values:
                    mono_time_series_list += self.get_mono_dimensional_timeseries(inner_list)
return mono_time_series_list

I would like to get your opinion on this. I wasn't able to find this feature in your API. Do you think it has any value? Would you do this any other way? I understand that this is not a typical issue, so apologies in advance if it should be asked elsewhere. :pray:

bendichter commented 6 years ago

Hi @afonsobspinto, thanks for working on this. I know @ajtritt has been working on query functionality that looks a lot like this, but it's not finished/supported/documented. He may be able to point you in the direction of these tools so you can use them for your own work. In the meantime, keep hacking away!

ajtritt commented 6 years ago

hey @afonsobspinto, look at the getitem implementation on LabelledDict. I apologize for the lack of documentation--as @bendichter said, it's not finished.

https://github.com/NeurodataWithoutBorders/pynwb/blob/6d2cc9f8ed3ab1a67e2da2fb4fec77029aed2215/src/pynwb/core.py#L43

jezekp commented 6 years ago

@afonsobspinto. Maybe The NWB query engine: https://github.com/jezekp/NwbQueryEngine could be useful too

afonsobspinto commented 6 years ago

Thanks guys. I'll take a look. :+1: