nwb_helper_fn.py
NWB helper functions for finding processing modules and data interfaces.
get_nwb_file(nwb_file_path)
¶
Return an NWBFile object with the given file path in read mode.
If the file is not found locally, this will check if it has been shared with kachery and if so, download it and open it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwb_file_path |
str
|
Path to the NWB file or NWB file name. If it does not start with a "/", get path with Nwbfile.get_abs_path |
required |
Returns:
Name | Type | Description |
---|---|---|
nwbfile |
NWBFile
|
NWB file object for the given path opened in read mode. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_config(nwb_file_path)
¶
Return a dictionary of config settings for the given NWB file. If the file does not exist, return an empty dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwb_file_path |
str
|
Absolute path to the NWB file. |
required |
Returns:
Type | Description |
---|---|
dict
|
Dictionary of configuration settings loaded from the corresponding YAML file |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_data_interface(nwbfile, data_interface_name, data_interface_class=None)
¶
Search for NWBDataInterface or DynamicTable in processing modules of an NWB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwbfile |
NWBFile
|
The NWB file object to search in. |
required |
data_interface_name |
str
|
The name of the NWBDataInterface or DynamicTable to search for. |
required |
data_interface_class |
type
|
The class (or superclass) to search for. This argument helps to prevent accessing an object with the same name but the incorrect type. Default: no restriction. |
None
|
Warns:
Type | Description |
---|---|
UserWarning
|
If multiple NWBDataInterface and DynamicTable objects with the matching name are found. |
Returns:
Name | Type | Description |
---|---|---|
data_interface |
NWBDataInterface
|
The data interface object with the given name, or None if not found. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_position_obj(nwbfile)
¶
Return the Position object from the behavior processing module.
Meant to find position spatial series that are not found by
get_data_interface(nwbfile, 'position', pynwb.behavior.Position)
.
The code returns the first pynwb.behavior.Position
object (technically
there should only be one).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwbfile |
NWBFile
|
The NWB file object. |
required |
Returns:
Type | Description |
---|---|
pynwb.behavior.Position object
|
|
Source code in src/spyglass/utils/nwb_helper_fn.py
get_raw_eseries(nwbfile)
¶
Return all ElectricalSeries in the acquisition group of an NWB file.
ElectricalSeries found within LFP objects in the acquisition will also be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwbfile |
NWBFile
|
The NWB file object to search in. |
required |
Returns:
Name | Type | Description |
---|---|---|
ret |
list
|
A list of all ElectricalSeries in the acquisition group of an NWB file |
Source code in src/spyglass/utils/nwb_helper_fn.py
estimate_sampling_rate(timestamps, multiplier=1.75, verbose=False, filename='file')
¶
Estimate the sampling rate given a list of timestamps.
Assumes that the most common temporal differences between timestamps approximate the sampling rate. Note that this can fail for very high sampling rates and irregular timestamps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps |
ndarray
|
1D numpy array of timestamp values. |
required |
multiplier |
float or int
|
Deft |
1.75
|
verbose |
bool
|
Log sampling rate to stdout. Default, False |
False
|
filename |
str
|
Filename to reference when logging or err. Default, "file" |
'file'
|
Returns:
Name | Type | Description |
---|---|---|
estimated_rate |
float
|
The estimated sampling rate. |
Raises:
Type | Description |
---|---|
ValueError
|
If estimated rate is less than 0. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_valid_intervals(timestamps, sampling_rate, gap_proportion=2.5, min_valid_len=0)
¶
Finds the set of all valid intervals in a list of timestamps. Valid interval: (start time, stop time) during which there are no gaps (i.e. missing samples).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps |
ndarray
|
1D numpy array of timestamp values. |
required |
sampling_rate |
float
|
Sampling rate of the data. |
required |
gap_proportion |
float
|
Threshold for detecting a gap; i.e. if the difference (in samples) between consecutive timestamps exceeds gap_proportion, it is considered a gap. Must be > 1. Default to 2.5 |
2.5
|
min_valid_len |
float
|
Length of smallest valid interval. Default to 0. If greater than interval duration, log warning and use half the total time. |
0
|
Returns:
Name | Type | Description |
---|---|---|
valid_times |
ndarray
|
Array of start and stop times of shape (N, 2) for valid data. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_electrode_indices(nwb_object, electrode_ids)
¶
Return indices of the specified electrode_ids given an NWB file.
Also accepts electrical series object. If an ElectricalSeries is given, then the indices returned are relative to the selected rows in ElectricalSeries.electrodes. For example, if electricalseries.electrodes = [5], and row index 5 of nwbfile.electrodes has ID 10, then calling get_electrode_indices(electricalseries, 10) will return 0, the index of the matching electrode in electricalseries.electrodes.
Indices for electrode_ids that are not in the electrical series are returned as np.nan
If an NWBFile is given, then the row indices with the matching IDs in the file's electrodes table are returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwb_object |
NWBFile or ElectricalSeries
|
The NWB file object or NWB electrical series object. |
required |
electrode_ids |
ndarray or list
|
Array or list of electrode IDs. |
required |
Returns:
Name | Type | Description |
---|---|---|
electrode_indices |
list
|
Array of indices of the specified electrode IDs. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_all_spatial_series(nwbf, verbose=False, incl_times=True)
¶
Given an NWB, get the spatial series and return a dictionary by epoch.
If incl_times is True, then the valid intervals are included in the output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nwbf |
NWBFile
|
The source NWB file object. |
required |
verbose |
bool
|
Flag representing whether to log the sampling rate. |
False
|
incl_times |
bool
|
Include valid times in the output. Default, True. Set to False for only spatial series object IDs. |
True
|
Returns:
Name | Type | Description |
---|---|---|
pos_data_dict |
dict
|
Dict mapping indices to a dict with keys 'valid_times' and 'raw_position_object_id'. Returns None if there is no position data in the file. The 'raw_position_object_id' is the object ID of the SpatialSeries object. |
Source code in src/spyglass/utils/nwb_helper_fn.py
get_nwb_copy_filename(nwb_file_name)
¶
Get file name of copy of nwb file without the electrophys data