def _find_stimulus_presentation_group(
        nwb_file,
        stimulus_name,
        base_path=_STIMULUS_PRESENTATION_PATH,
        group_patterns=_STIMULUS_PRESENTATION_PATTERNS):
    ''' Searches an NWB file for a stimulus presentation group.

    Parameters
    ----------
    nwb_file : h5py.File
        File to search
    stimulus_name : str
        Identifier for this stimulus. Corresponds to the relative name of its h5 
        group.
    base_path : str, optional
        Begin the search from here. Defaults to 'stimulus/presentation'
    group_patterns : array-like of str, optional
        Patterns for the relative name of the stimulus' h5 group. Defaults to 
        the name, and the name suffixed by '_stimulus'

    Returns
    -------
    h5py.Group, h5py.Dataset : 
        h5 object found

    '''

    group_candidates = [
        pattern.format(stimulus_name) for pattern in group_patterns
    ]
    matcher = functools.partial(h5_utilities.h5_object_matcher_relname_in,
                                group_candidates)
    matches = h5_utilities.locate_h5_objects(matcher, nwb_file, base_path)

    if len(matches) == 0:
        raise MissingStimulusException(
            'Unable to locate stimulus: {}. '
            'Looked for this stimulus under the names: {} '.format(
                stimulus_name, group_candidates))

    if len(matches) > 1:
        raise MissingStimulusException(
            'Unable to locate stimulus: {}. '
            'Found multiple matching stimuli: {}'.format(
                stimulus_name, [match.name for match in matches]))

    return matches[0]
Beispiel #2
0
def _get_abstract_feature_series_stimulus_table(nwb_file, stimulus_name):
    ''' Return the a stimulus table for an abstract feature series.

    Returns
    -------
    stimulus table: pd.DataFrame
    '''

    k = "stimulus/presentation/%s" % stimulus_name

    with h5py.File(nwb_file, 'r') as f:
        if k not in f:
            raise MissingStimulusException("Stimulus not found: %s" %
                                           stimulus_name)
        stim_data = f[k + '/data'].value
        features = [v.decode('UTF-8') for v in f[k + '/features'].value]
        frame_dur = f[k + '/frame_duration'].value

    stimulus_table = pd.DataFrame(stim_data, columns=features)
    stimulus_table.loc[:, 'start'] = frame_dur[:, 0].astype(int)
    stimulus_table.loc[:, 'end'] = frame_dur[:, 1].astype(int)

    return stimulus_table
Beispiel #3
0
def _get_indexed_time_series_stimulus_table(nwb_file, stimulus_name):
    ''' Return the a stimulus table for an indexed time series.

    Returns
    -------
    stimulus table: pd.DataFrame
    '''

    k = "stimulus/presentation/%s" % stimulus_name

    with h5py.File(nwb_file, 'r') as f:
        if k not in f:
            k = "stimulus/presentation/%s" % (stimulus_name + "_stimulus")
            if k not in f:
                raise MissingStimulusException("Stimulus not found: %s" %
                                               stimulus_name)
        inds = f[k + '/data'].value
        frame_dur = f[k + '/frame_duration'].value

    stimulus_table = pd.DataFrame(inds, columns=['frame'])
    stimulus_table.loc[:, 'start'] = frame_dur[:, 0].astype(int)
    stimulus_table.loc[:, 'end'] = frame_dur[:, 1].astype(int)

    return stimulus_table