brainbox.io.one

Functions for loading IBL ephys and trial data using the Open Neurophysiology Environment.

Functions

channel_locations_interpolation

oftentimes the channel map for different spike sorters may be different so interpolate the alignment onto if there is no spike sorting in the base folder, the alignment doesn't have the localCoordinates field so we reconstruct from the Neuropixel map.

load_channel_locations

Load the brain locations of each channel for a given session/probe

load_channels_from_insertion

load_ephys_session

From an eid, hits the Alyx database and downloads a standard default set of dataset types From a local session Path (pathlib.Path), loads a standard default set of dataset types to perform analysis: 'clusters.channels', 'clusters.depths', 'clusters.metrics', 'spikes.clusters', 'spikes.times', 'probes.description'

load_lfp

TODO Verify works From an eid, hits the Alyx database and downloads the standard set of datasets needed for LFP

load_passive_rfmap

For a given eid load in the passive receptive field mapping protocol data

load_spike_sorting

From an eid, loads spikes and clusters for all probes The following set of dataset types are loaded: 'clusters.channels', 'clusters.depths', 'clusters.metrics', 'spikes.clusters', 'spikes.times', 'probes.description'

load_spike_sorting_fast

From an eid, loads spikes and clusters for all probes The following set of dataset types are loaded: 'clusters.channels', 'clusters.depths', 'clusters.metrics', 'spikes.clusters', 'spikes.times', 'probes.description'

load_spike_sorting_with_channel

For a given eid, get spikes, clusters and channels information, and merges clusters and channels information before returning all three variables.

load_trials_df

Generate a pandas dataframe of per-trial timing information about a given session.

load_wheel_reaction_times

Return the calculated reaction times for session.

merge_clusters_channels

Takes (default and any extra) values in given keys from channels and assign them to clusters.

Classes

SpikeSortingLoader

Object that will load spike sorting data for a given probe insertion. This class can be instantiated in several manners - With Alyx database probe id: SpikeSortingLoader(pid=pid, one=one) - With Alyx database eic and probe name: SpikeSortingLoader(eid=eid, pname='probe00', one=one) - From a local session and probe name: SpikeSortingLoader(session_path=session_path, pname='probe00') NB: When no ONE instance is passed, any datasets that are loaded will not be recorded.

load_lfp(eid, one=None, dataset_types=None, **kwargs)[source]

TODO Verify works From an eid, hits the Alyx database and downloads the standard set of datasets needed for LFP

Parameters
  • eid

  • dataset_types – additional dataset types to add to the list

  • open – if True, spikeglx readers are opened

Returns

spikeglx.Reader

channel_locations_interpolation(channels_aligned, channels=None, brain_regions=None)[source]

oftentimes the channel map for different spike sorters may be different so interpolate the alignment onto if there is no spike sorting in the base folder, the alignment doesn’t have the localCoordinates field so we reconstruct from the Neuropixel map. This only happens for early pykilosort sorts

Parameters
  • channels_aligned

    Bunch or dictionary of aligned channels containing at least keys ‘localCoordinates’, ‘mlapdv’ and ‘brainLocationIds_ccf_2017’ OR

    ’x’, ‘y’, ‘z’, ‘acronym’, ‘axial_um’ those are the guide for the interpolation

  • channels – Bunch or dictionary of aligned channels containing at least keys ‘localCoordinates’

  • brain_regions

    None (default) or ibllib.atlas.BrainRegions object if None will return a dict with keys ‘localCoordinates’, ‘mlapdv’, ‘brainLocationIds_ccf_2017 if a brain region object is provided, outputts a dict with keys

    ’x’, ‘y’, ‘z’, ‘acronym’, ‘atlas_id’, ‘axial_um’, ‘lateral_um’

Returns

Bunch or dictionary of channels with brain coordinates keys

load_channel_locations(eid, probe=None, one=None, aligned=False, brain_atlas=None)[source]

Load the brain locations of each channel for a given session/probe

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • probe ([str, list of str]) – The probe label(s), e.g. ‘probe01’

  • one (one.api.OneAlyx) – An instance of ONE (shouldn’t be in ‘local’ mode)

  • aligned (bool) – Whether to get the latest user aligned channel when not resolved or use histology track

  • brain_atlas (ibllib.atlas.BrainAtlas) – Brain atlas object (default: Allen atlas)

Returns

  • dict of one.alf.io.AlfBunch – A dict with probe labels as keys, contains channel locations with keys (‘acronym’, ‘atlas_id’, ‘x’, ‘y’, ‘z’). Atlas IDs non-lateralized.

  • optional (string ‘resolved’, ‘aligned’, ‘traced’ or ‘’)

load_spike_sorting_fast(eid, one=None, probe=None, dataset_types=None, spike_sorter=None, revision=None, brain_regions=None, nested=True, collection=None, return_collection=False)[source]

From an eid, loads spikes and clusters for all probes The following set of dataset types are loaded:

‘clusters.channels’, ‘clusters.depths’, ‘clusters.metrics’, ‘spikes.clusters’, ‘spikes.times’, ‘probes.description’

Parameters
  • eid – experiment UUID or pathlib.Path of the local session

  • one – an instance of OneAlyx

  • probe – name of probe to load in, if not given all probes for session will be loaded

  • dataset_types – additional spikes/clusters objects to add to the standard default list

  • spike_sorter – name of the spike sorting you want to load (None for default)

  • collection – name of the spike sorting collection to load - exclusive with spike sorter name ex: “alf/probe00”

  • brain_regions – ibllib.atlas.regions.BrainRegions object - will label acronyms if provided

  • nested – if a single probe is required, do not output a dictionary with the probe name as key

  • return_collection – (False) if True, will return the collection used to load

Returns

spikes, clusters, channels (dict of bunch, 1 bunch per probe)

load_spike_sorting(eid, one=None, probe=None, dataset_types=None, spike_sorter=None, revision=None, brain_regions=None, return_collection=False)[source]

From an eid, loads spikes and clusters for all probes The following set of dataset types are loaded:

‘clusters.channels’, ‘clusters.depths’, ‘clusters.metrics’, ‘spikes.clusters’, ‘spikes.times’, ‘probes.description’

Parameters
  • eid – experiment UUID or pathlib.Path of the local session

  • one – an instance of OneAlyx

  • probe – name of probe to load in, if not given all probes for session will be loaded

  • dataset_types – additional spikes/clusters objects to add to the standard default list

  • spike_sorter – name of the spike sorting you want to load (None for default)

  • brain_regions – ibllib.atlas.regions.BrainRegions object - will label acronyms if provided

:param return_collection:(bool - False) if True, returns the collection for loading the data :return: spikes, clusters (dict of bunch, 1 bunch per probe)

load_spike_sorting_with_channel(eid, one=None, probe=None, aligned=False, dataset_types=None, spike_sorter=None, brain_atlas=None, nested=True, return_collection=False)[source]

For a given eid, get spikes, clusters and channels information, and merges clusters and channels information before returning all three variables.

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • one (one.api.OneAlyx) – An instance of ONE (shouldn’t be in ‘local’ mode)

  • probe ([str, list of str]) – The probe label(s), e.g. ‘probe01’

  • aligned (bool) – Whether to get the latest user aligned channel when not resolved or use histology track

  • dataset_types (list of str) – Optional additional spikes/clusters objects to add to the standard default list

  • spike_sorter (str) – Name of the spike sorting you want to load (None for default which is pykilosort if it’s available otherwise the default MATLAB kilosort)

  • brain_atlas (ibllib.atlas.BrainAtlas) – Brain atlas object (default: Allen atlas)

  • return_collection (bool) – Returns an extra argument with the collection chosen

Returns

  • spikes (dict of one.alf.io.AlfBunch) – A dict with probe labels as keys, contains bunch(es) of spike data for the provided session and spike sorter, with keys (‘clusters’, ‘times’)

  • clusters (dict of one.alf.io.AlfBunch) – A dict with probe labels as keys, contains bunch(es) of cluster data, with keys (‘channels’, ‘depths’, ‘metrics’)

  • channels (dict of one.alf.io.AlfBunch) – A dict with probe labels as keys, contains channel locations with keys (‘acronym’, ‘atlas_id’, ‘x’, ‘y’, ‘z’). Atlas IDs non-lateralized.

load_ephys_session(eid, one=None)[source]

From an eid, hits the Alyx database and downloads a standard default set of dataset types From a local session Path (pathlib.Path), loads a standard default set of dataset types

to perform analysis:

‘clusters.channels’, ‘clusters.depths’, ‘clusters.metrics’, ‘spikes.clusters’, ‘spikes.times’, ‘probes.description’

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • one (oneibl.one.OneAlyx, optional) – ONE object to use for loading. Will generate internal one if not used, by default None

Returns

  • spikes (dict of one.alf.io.AlfBunch) – A dict with probe labels as keys, contains bunch(es) of spike data for the provided session and spike sorter, with keys (‘clusters’, ‘times’)

  • clusters (dict of one.alf.io.AlfBunch) – A dict with probe labels as keys, contains bunch(es) of cluster data, with keys (‘channels’, ‘depths’, ‘metrics’)

  • trials (one.alf.io.AlfBunch of numpy.ndarray) – The session trials data

merge_clusters_channels(dic_clus, channels, keys_to_add_extra=None)[source]

Takes (default and any extra) values in given keys from channels and assign them to clusters. If channels does not contain any data, the new keys are added to clusters but left empty.

Parameters
  • dic_clus (dict of one.alf.io.AlfBunch) – 1 bunch per probe, containing cluster information

  • channels (dict of one.alf.io.AlfBunch) – 1 bunch per probe, containing channels bunch with keys (‘acronym’, ‘atlas_id’, ‘x’, ‘y’, z’, ‘localCoordinates’)

  • keys_to_add_extra (list of str) – Any extra keys to load into channels bunches

Returns

clusters (1 bunch per probe) with new keys values.

Return type

dict of one.alf.io.AlfBunch

load_passive_rfmap(eid, one=None)[source]

For a given eid load in the passive receptive field mapping protocol data

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • one (oneibl.one.OneAlyx, optional) – An instance of ONE (may be in ‘local’ - offline - mode)

Returns

Passive receptive field mapping data

Return type

one.alf.io.AlfBunch

load_wheel_reaction_times(eid, one=None)[source]

Return the calculated reaction times for session. Reaction times are defined as the time between the go cue (onset tone) and the onset of the first substantial wheel movement. A movement is considered sufficiently large if its peak amplitude is at least 1/3rd of the distance to threshold (~0.1 radians).

Negative times mean the onset of the movement occurred before the go cue. Nans may occur if there was no detected movement withing the period, or when the goCue_times or feedback_times are nan.

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • one (one.api.OneAlyx, optional) – one object to use for loading. Will generate internal one if not used, by default None

Returns

reaction times

Return type

array-like

load_trials_df(eid, one=None, maxlen=None, t_before=0.0, t_after=0.2, ret_wheel=False, ret_abswheel=False, wheel_binsize=0.02, addtl_types=[], align_event='stimOn_times', keeptrials=None)[source]

Generate a pandas dataframe of per-trial timing information about a given session. Each row in the frame will correspond to a single trial, with timing values indicating timing session-wide (i.e. time in seconds since session start). Can optionally return a resampled wheel velocity trace of either the signed or absolute wheel velocity.

The resulting dataframe will have a new set of columns, trial_start and trial_end, which define via t_before and t_after the span of time assigned to a given trial. (useful for bb.modeling.glm)

Parameters
  • eid ([str, UUID, Path, dict]) – Experiment session identifier; may be a UUID, URL, experiment reference string details dict or Path

  • one (one.api.OneAlyx, optional) – one object to use for loading. Will generate internal one if not used, by default None

  • maxlen (float, optional) – Maximum trial length for inclusion in df. Trials where feedback - response is longer than this value will not be included in the dataframe, by default None

  • t_before (float, optional) – Time before stimulus onset to include for a given trial, as defined by the trial_start column of the dataframe. If zero, trial_start will be identical to stimOn, by default 0.

  • t_after (float, optional) – Time after feedback to include in the trail, as defined by the trial_end column of the dataframe. If zero, trial_end will be identical to feedback, by default 0.

  • ret_wheel (bool, optional) – Whether to return the time-resampled wheel velocity trace, by default False

  • ret_abswheel (bool, optional) – Whether to return the time-resampled absolute wheel velocity trace, by default False

  • wheel_binsize (float, optional) – Time bins to resample wheel velocity to, by default 0.02

  • addtl_types (list, optional) – List of additional types from an ONE trials object to include in the dataframe. Must be valid keys to the dict produced by one.load_object(eid, ‘trials’), by default empty.

Returns

Dataframe with trial-wise information. Indices are the actual trial order in the original data, preserved even if some trials do not meet the maxlen criterion. As a result will not have a monotonic index. Has special columns trial_start and trial_end which define start and end times via t_before and t_after

Return type

pandas.DataFrame

load_channels_from_insertion(ins, depths=None, one=None, ba=None)[source]
class SpikeSortingLoader(one: Optional[one.api.One] = None, atlas: None = None, pid: Optional[str] = None, eid: str = '', pname: str = '', session_path: pathlib.Path = '', collections: Optional[list] = None, datasets: Optional[list] = None, files: Optional[dict] = None, collection: str = '', histology: str = '', spike_sorting_path: Optional[pathlib.Path] = None)[source]

Bases: object

Object that will load spike sorting data for a given probe insertion. This class can be instantiated in several manners - With Alyx database probe id:

SpikeSortingLoader(pid=pid, one=one)

  • With Alyx database eic and probe name:

    SpikeSortingLoader(eid=eid, pname=’probe00’, one=one)

  • From a local session and probe name:

    SpikeSortingLoader(session_path=session_path, pname=’probe00’)

NB: When no ONE instance is passed, any datasets that are loaded will not be recorded.

one: one.api.One = None
atlas: None = None
pid: str = None
eid: str = ''
pname: str = ''
session_path: pathlib.Path = ''
collections: list = None
datasets: list = None
files: dict = None
collection: str = ''
histology: str = ''
spike_sorting_path: pathlib.Path = None
download_spike_sorting_object(obj, spike_sorter='pykilosort', dataset_types=None, collection=None)[source]

Downloads an ALF object

Parameters
  • obj – object name, str between ‘spikes’, ‘clusters’ or ‘channels’

  • spike_sorter – (defaults to ‘pykilosort’)

  • dataset_types – list of extra dataset types, for example [‘spikes.samples’]

  • collection – string specifiying the collection, for example ‘alf/probe01/pykilosort’

Returns

download_spike_sorting(**kwargs)[source]

Downloads spikes, clusters and channels

Parameters
  • spike_sorter – (defaults to ‘pykilosort’)

  • dataset_types – list of extra dataset types

Returns

load_spike_sorting(**kwargs)[source]

Loads spikes, clusters and channels

There could be several spike sorting collections, by default the loader will get the pykilosort collection

The channel locations can come from several sources, it will load the most advanced version of the histology available, regardless of the spike sorting version loaded. The steps are (from most advanced to fresh out of the imaging): - alf: the final version of channel locations, same as resolved with the difference that data is on file - resolved: channel locations alignments have been agreed upon - aligned: channel locations have been aligned, but review or other alignments are pending, potentially not accurate - traced: the histology track has been recovered from microscopy, however the depths may not match, inaccurate data

Parameters
  • spike_sorter – (defaults to ‘pykilosort’)

  • dataset_types – list of extra dataset types

Returns

static merge_clusters(spikes, clusters, channels, cache_dir=None)[source]

merge metrics and channels info - optionally saves a clusters.pqt dataframe

property url

Gets flatiron URL for the session