CrossSpectralData

class syncopy.CrossSpectralData(data=None, filename=None, channel_i=None, channel_j=None, samplerate=None, freq=None, dimord=None)[source]

Bases: ContinuousData

Multi-channel real or complex spectral connectivity data

This class can be used for representing channel-channel interactions involving frequency and optionally time or lag. The datatype can be complex or float.

Attributes Summary

cfg

Dictionary of previous operations on data

channel

list of recording channel names

channel_i

list of recording channel names

channel_j

list of recording channel names

container

data

array-like object representing data without trials

dimord

ordered list of data dimension labels

filename

freq

frequency axis in Hz

info

Dictionary of auxiliary meta information

log

log of previous operations on data

mode

write mode for data, 'r' for read-only, 'w' for writable

sampleinfo

nTrials x 2 numpy.ndarray of [start, end] sample indices

samplerate

sampling rate of uniformly sampled data in Hz

selection

Data selection specified by Selector

tag

time

trigger-relative time axes of each trial

trialdefinition

]]

trialinfo

nTrials x M numpy.ndarray with numeric information about each trial

trials

list-like array of trials

Methods Summary

clear()

Clear loaded data from memory

copy()

Create a copy of the entire object on disk

definetrial([trialdefinition, pre, post, ...])

(Re-)define trials of a Syncopy data object

multipanelplot()

save([container, tag, filename, overwrite])

Save data object as new spy container to disk (syncopy.save_data())

selectdata([trials, channel, channel_i, ...])

Create a new Syncopy object from a selection

show([squeeze])

Show (partial) contents of Syncopy object

singlepanelplot(**show_kwargs)

Attributes Documentation

cfg

Dictionary of previous operations on data

channel
channel_i

list of recording channel names

Type:

numpy.ndarray

channel_j

list of recording channel names

Type:

numpy.ndarray

container
data

array-like object representing data without trials

Trials are concatenated along the time axis.

dimord

ordered list of data dimension labels

Type:

list(str)

filename
freq

frequency axis in Hz

Type:

numpy.ndarray

info

Dictionary of auxiliary meta information

log

log of previous operations on data

Type:

str

mode

write mode for data, ‘r’ for read-only, ‘w’ for writable

FIXME: append/replace with HDF5?

Type:

str

sampleinfo

nTrials x 2 numpy.ndarray of [start, end] sample indices

samplerate

sampling rate of uniformly sampled data in Hz

Type:

float

selection

Data selection specified by Selector

tag
time

trigger-relative time axes of each trial

Type:

list(float)

trialdefinition

]]

Type:

nTrials x >=3 numpy.ndarray of [start, end, offset, trialinfo[

trialinfo

nTrials x M numpy.ndarray with numeric information about each trial

Each trial can have M properties (condition, original trial no., …) coded by numbers. This property are the fourth and onward columns of BaseData._trialdefinition.

trials

list-like array of trials

Methods Documentation

clear()

Clear loaded data from memory

Calls flush method of HDF5 dataset.

copy()

Create a copy of the entire object on disk

Returns:

cpy – Reference to the copied data object on disk

Return type:

Syncopy data object

Notes

For copying only a subset of the data use syncopy.selectdata() directly with the default inplace=False parameter.

See also

syncopy.save()

save to specific file path

syncopy.selectdata()

creates copy of a selection with inplace=False

definetrial(trialdefinition=None, pre=None, post=None, start=None, trigger=None, stop=None, clip_edges=False)

(Re-)define trials of a Syncopy data object

Data can be structured into trials based on timestamps of a start, trigger and end events:

            start    trigger    stop
|---- pre ----|--------|---------|--- post----|

Note: To define a trial encompassing the whole dataset simply invoke this routine with no arguments, i.e., definetrial(obj) or equivalently obj.definetrial()

Parameters:
  • obj (Syncopy data object (BaseData-like)) –

  • trialdefinition (EventData object or Mx3 array) – [start, stop, trigger_offset] sample indices for M trials

  • pre (float) – offset time (s) before start event

  • post (float) – offset time (s) after end event

  • start (int) – event code (id) to be used for start of trial

  • stop (int) – event code (id) to be used for end of trial

  • trigger – event code (id) to be used center (t=0) of trial

  • clip_edges (bool) – trim trials to actual data-boundaries.

Return type:

Syncopy data object (BaseData-like))

Notes

definetrial() supports the following argument combinations:

>>> # define M trials based on [start, end, offset] indices
>>> definetrial(obj, trialdefinition=[M x 3] array)
>>> # define trials based on event codes stored in <:class:`EventData` object>
>>> definetrial(obj, trialdefinition=<EventData object>,
                pre=0, post=0, start=startCode, stop=stopCode,
                trigger=triggerCode)
>>> # apply same trial definition as defined in <:class:`EventData` object>
>>> definetrial(<AnalogData object>,
                trialdefinition=<EventData object w/sampleinfo/t0/trialinfo>)
>>> # define whole recording as single trial
>>> definetrial(obj, trialdefinition=None)
multipanelplot()
save(container=None, tag=None, filename=None, overwrite=False)

Save data object as new spy container to disk (syncopy.save_data())

FIXME: update docu

Parameters:
  • container (str) – Path to Syncopy container folder (*.spy) to be used for saving. If omitted, a .spy extension will be added to the folder name.

  • tag (str) – Tag to be appended to container basename

  • filename (str) – Explicit path to data file. This is only necessary if the data should not be part of a container folder. An extension (*.<dataclass>) will be added if omitted. The tag argument is ignored.

  • overwrite (bool) – If True an existing HDF5 file and its accompanying JSON file is overwritten (without prompt).

Examples

>>> save_spy(obj, filename="session1")
>>> # --> os.getcwd()/session1.<dataclass>
>>> # --> os.getcwd()/session1.<dataclass>.info
>>> save_spy(obj, filename="/tmp/session1")
>>> # --> /tmp/session1.<dataclass>
>>> # --> /tmp/session1.<dataclass>.info
>>> save_spy(obj, container="container.spy")
>>> # --> os.getcwd()/container.spy/container.<dataclass>
>>> # --> os.getcwd()/container.spy/container.<dataclass>.info
>>> save_spy(obj, container="/tmp/container.spy")
>>> # --> /tmp/container.spy/container.<dataclass>
>>> # --> /tmp/container.spy/container.<dataclass>.info
>>> save_spy(obj, container="session1.spy", tag="someTag")
>>> # --> os.getcwd()/container.spy/session1_someTag.<dataclass>
>>> # --> os.getcwd()/container.spy/session1_someTag.<dataclass>.info
selectdata(trials=None, channel=None, channel_i=None, channel_j=None, toi=None, toilim=None, foi=None, foilim=None, taper=None, unit=None, eventid=None, inplace=False, clear=False, parallel=None, **kwargs)

Create a new Syncopy object from a selection

Usage Notice

Syncopy offers two modes for selecting data:

  • in-place selections mark subsets of a Syncopy data object for processing via a select dictionary without creating a new object

  • deep-copy selections copy subsets of a Syncopy data object to keep and preserve in a new object created by selectdata()

All Syncopy metafunctions, such as freqanalysis(), support in-place data selection via a select keyword, effectively avoiding potentially slow copy operations and saving disk space. The keys accepted by the select dictionary are identical to the keyword arguments discussed below. In addition, select = "all" can be used to select entire object contents. Examples

>>> select = {"toilim" : [-0.25, 0]}
>>> spy.freqanalysis(data, select=select)
>>> # or equivalently
>>> cfg = spy.get_defaults(spy.freqanalysis)
>>> cfg.select = select
>>> spy.freqanalysis(cfg, data)

Usage Summary

List of Syncopy data objects and respective valid data selectors:

AnalogDatatrials, channel, toi/toilim

Examples

>>> spy.selectdata(data, trials=[0, 3, 5], channel=["channel01", "channel02"])
>>> cfg = spy.StructDict()
>>> cfg.trials = [5, 3, 0]; cfg.toilim = [0.25, 0.5]
>>> spy.selectdata(cfg, data)
SpectralDatatrials, channel, toi/toilim, foi/foilim, taper

Examples

>>> spy.selectdata(data, trials=[0, 3, 5], channel=["channel01", "channel02"])
>>> cfg = spy.StructDict()
>>> cfg.foi = [30, 40, 50]; cfg.taper = slice(2, 4)
>>> spy.selectdata(cfg, data)
EventDatatrials, toi/toilim, eventid

Examples

>>> spy.selectdata(data, toilim=[-1, 2.5], eventid=[0, 1])
>>> cfg = spy.StructDict()
>>> cfg.trials = [0, 0, 1, 0]; cfg.eventid = slice(2, None)
>>> spy.selectdata(cfg, data)
SpikeDatatrials, toi/toilim, unit, channel

Examples

>>> spy.selectdata(data, toilim=[-1, 2.5], unit=range(0, 10))
>>> cfg = spy.StructDict()
>>> cfg.toi = [1.25, 3.2]; cfg.trials = [0, 1, 2, 3]
>>> spy.selectdata(cfg, data)

Note Any property that is not specifically accessed via one of the provided selectors is taken as is, e.g., spy.selectdata(data, trials=[1, 2]) selects the entire contents of trials no. 2 and 3, while spy.selectdata(data, channel=range(0, 50)) selects the first 50 channels of data across all defined trials. Consequently, if no keywords are specified, the entire contents of data is selected.

Full documentation below

The parameters listed below can be provided as is or a via a cfg configuration ‘structure’, see Notes for details.

Parameters:
  • data (Syncopy data object) – A non-empty Syncopy data object. Note the type of data determines which keywords can be used. Some keywords are only valid for certain types of Syncopy objects, e.g., “freqs” is not a valid selector for an AnalogData object.

  • trials (list (integers) or None or "all") – List of integers representing trial numbers to be selected; can include repetitions and need not be sorted (e.g., trials = [0, 1, 0, 0, 2] is valid) but must be finite and not NaN. If trials is None, or trials = "all" all trials are selected.

  • channel (list (integers or strings), slice, range, str, int, None or "all") – Channel-selection; can be a list of channel names (['channel3', 'channel1']), a list of channel indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, channels are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, channel = [0, 1, 2] or channel = slice(0, 3) selects the first up to (and including) the third channel. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If channel is None, or channel = "all" all channels are selected.

  • toi (list (floats), float, None or "all") – Time-points to be selected (in seconds) in each trial. Timing is expected to be on a by-trial basis (e.g., relative to trigger onsets). Selections can be approximate, unsorted and may include repetitions but must be finite and not NaN. Fuzzy matching is performed for approximate selections (i.e., selected time-points are close but not identical to timing information found in data) using a nearest-neighbor search for elements of toi. If toi is None or toi = "all", the entire time-span in each trial is selected.

  • toilim (list (floats [tmin, tmax]) or None or "all") – Time-window [tmin, tmax] (in seconds) to be extracted from each trial. Window specifications must be sorted (e.g., [2.2, 1.1] is invalid) and not NaN but may be unbounded (e.g., [1.1, np.inf] is valid). Edges tmin and tmax are included in the selection. If toilim is None or toilim = "all", the entire time-span in each trial is selected.

  • foi (list (floats), float, None or "all") – Frequencies to be selected (in Hz). Selections can be approximate, unsorted and may include repetitions but must be finite and not NaN. Fuzzy matching is performed for approximate selections (i.e., selected frequencies are close but not identical to frequencies found in data) using a nearest- neighbor search for elements of foi in data.freq. If foi is None or foi = "all", all frequencies are selected.

  • foilim (list (floats [fmin, fmax]) or None or "all") – Frequency-window [fmin, fmax] (in Hz) to be extracted. Window specifications must be sorted (e.g., [90, 70] is invalid) and not NaN but may be unbounded (e.g., [-np.inf, 60.5] is valid). Edges fmin and fmax are included in the selection. If foilim is None or foilim = "all", all frequencies are selected.

  • taper (list (integers or strings), slice, range, str, int, None or "all") – Taper-selection; can be a list of taper names (['dpss-win-1', 'dpss-win-3']), a list of taper indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, tapers are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, taper = [0, 1, 2] or taper = slice(0, 3) selects the first up to (and including) the third taper. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If taper is None or taper = "all", all tapers are selected.

  • unit (list (integers or strings), slice, range, str, int, None or "all") – Unit-selection; can be a list of unit names (['unit10', 'unit3']), a list of unit indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, units are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, unit = [0, 1, 2] or unit = slice(0, 3) selects the first up to (and including) the third unit. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If unit is None or unit = "all", all units are selected.

  • eventid (list (integers), slice, range, int, None or "all") – Event-ID-selection; can be a list of event-id codes ([2, 0, 1]), slice (slice(0, 2)) or range (range(0, 2)). Note that following Python conventions, range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If eventid is None or eventid = "all", all events are selected.

  • inplace (bool) – If inplace is True no new object is created. Instead the provided selection is stored in the input object’s selection attribute for later use. By default inplace is False and all calls to selectdata create a new Syncopy data object.

  • clear (bool) – If True remove any active in-place selection. Note that in-place selections can also be removed manually by assinging None to the selection property, i.e., mydata.selection = None is equivalent

  • parallel (None or bool) – If None (recommended), processing is automatically performed in parallel (i.e., concurrently across trials/channel-groups), provided a dask parallel processing client is running and available. Parallel processing can be manually disabled by setting parallel to False. If parallel is True but no parallel processing client is running, computing will be performed sequentially. to spy.selectdata(mydata, clear=True) or mydata.selectdata(clear=True)

Returns:

dataselection – Syncopy data object of the same type as data but containing only the subset specified by provided selectors.

Return type:

Syncopy data object

Notes

This function can be either called providing its input arguments directly or via a cfg configuration ‘structure’. For instance, the following function calls are equivalent

>>> spy.selectdata(data, trials=...)
>>> cfg = spy.StructDict()
>>> cfg.trials = ...
>>> spy.selectdata(cfg, data)
>>> cfg.data = data
>>> spy.selectdata(cfg)

Please refer to Syncopy for FieldTrip Users for further details.

This routine represents a convenience function for creating new Syncopy objects based on existing data entities. However, in many situations, the creation of a new object (and thus the allocation of additional disk-space) might not be necessary: all Syncopy metafunctions, such as freqanalysis(), support in-place data selection.

Consider the following example: assume data is an AnalogData object representing 220 trials of LFP recordings containing baseline (between second -0.25 and 0) and stimulus-on data (on the interval [0.25, 0.5]). To compute the baseline spectrum, data-selection does not have to be performed before calling freqanalysis() but instead can be done in-place:

>>> import syncopy as spy
>>> cfg = spy.get_defaults(spy.freqanalysis)
>>> cfg.method = 'mtmfft'
>>> cfg.taper = 'dpss'
>>> cfg.output = 'pow'
>>> cfg.tapsmofrq = 10
>>> # define baseline/stimulus-on ranges
>>> baseSelect = {"toilim": [-0.25, 0]}
>>> stimSelect = {"toilim": [0.25, 0.5]}
>>> # in-place selection of baseline interval performed by `freqanalysis`
>>> cfg.select = baseSelect
>>> baselineSpectrum = spy.freqanalysis(cfg, data)
>>> # in-place selection of stimulus-on time-frame performed by `freqanalysis`
>>> cfg.select = stimSelect
>>> stimonSpectrum = spy.freqanalysis(cfg, data)

Especially for large data-sets, in-place data selection performed by Syncopy’s metafunctions does not only save disk-space but can significantly increase performance.

Examples

Use generate_artificial_data() to create a synthetic syncopy.AnalogData object.

>>> from syncopy.tests.misc import generate_artificial_data
>>> adata = generate_artificial_data(nTrials=10, nChannels=32)

Assume a hypothetical trial onset at second 2.0 with the first second of each trial representing baseline recordings. To extract only the stimulus-on period from adata, one could use

>>> stimon = spy.selectdata(adata, toilim=[2.0, np.inf])

Note that this is equivalent to

>>> stimon = adata.selectdata(toilim=[2.0, np.inf])

See also

syncopy.show()

Show (subsets) of Syncopy objects

show(squeeze=True, **kwargs)

Show (partial) contents of Syncopy object

Usage Notice

Syncopy uses HDF5 files as on-disk backing device for data storage. This allows working with larger-than-memory data-sets by streaming only relevant subsets of data from disk on demand without excessive RAM use. However, using show() this mechanism is bypassed and the requested data subset is loaded into memory at once. Thus, inadvertent usage of show() on a large data object can lead to memory overflow or even out-of-memory errors.

Usage Summary

Data selectors for showing subsets of Syncopy data objects follow the syntax of selectdata(). Please refer to selectdata() for a list of valid data selectors for respective Syncopy data objects.

Parameters:
  • data (Syncopy data object) – As for subset-selection via selectdata(), the type of data determines which keywords can be used. Some keywords are only valid for certain types of Syncopy objects, e.g., “freqs” is not a valid selector for an AnalogData object.

  • squeeze (bool) – If True (default) any singleton dimensions are removed from the output array, i.e., the shape of the returned array does not contain ones (e.g., arr.shape = (2,) not arr.shape = (1,2,1,1)).

  • **kwargs (keywords) – Valid data selectors (e.g., trials, channels, toi etc.). Please refer to selectdata() for a full list of available data selectors.

Returns:

arr – A (selection) of data retrieved from the data input object.

Return type:

NumPy nd-array

Notes

This routine represents a convenience function for quickly inspecting the contents of Syncopy objects. It is always possible to manually access an object’s numerical data by indexing the underlying HDF5 dataset: data.data[idx]. The dimension labels of the dataset are encoded in data.dimord, e.g., if data is a AnalogData with data.dimord being [‘time’, ‘channel’] and data.data.shape is (15000, 16), then data.data[:, 3] returns the contents of the fourth channel across all time points.

Examples

Use generate_artificial_data() to create a synthetic syncopy.AnalogData object.

>>> from syncopy.tests.misc import generate_artificial_data
>>> adata = generate_artificial_data(nTrials=10, nChannels=32)

Show the contents of ‘channel02’ across all trials:

>>> spy.show(adata, channel='channel02')
Syncopy <show> INFO: Showing all times 10 trials
Out[2]: array([1.0871, 0.7267, 0.2816, ..., 1.0273, 0.893 , 0.7226], dtype=float32)

Note that this is equivalent to

>>> adata.show(channel='channel02')

To preserve singleton dimensions use squeeze=False:

>>> adata.show(channel='channel02', squeeze=False)
Out[3]:
array([[1.0871],
       [0.7267],
       [0.2816],
       ...,
       [1.0273],
       [0.893 ],
       [0.7226]], dtype=float32)

See also

syncopy.selectdata()

Create a new Syncopy object from a selection

singlepanelplot(**show_kwargs)[source]
property channel

list of recording channel names

Type:

numpy.ndarray

property channel_i

list of recording channel names

Type:

numpy.ndarray

property cfg

Dictionary of previous operations on data

clear()

Clear loaded data from memory

Calls flush method of HDF5 dataset.

property container
copy()

Create a copy of the entire object on disk

Returns:

cpy – Reference to the copied data object on disk

Return type:

Syncopy data object

Notes

For copying only a subset of the data use syncopy.selectdata() directly with the default inplace=False parameter.

See also

syncopy.save()

save to specific file path

syncopy.selectdata()

creates copy of a selection with inplace=False

property data

array-like object representing data without trials

Trials are concatenated along the time axis.

definetrial(trialdefinition=None, pre=None, post=None, start=None, trigger=None, stop=None, clip_edges=False)

(Re-)define trials of a Syncopy data object

Data can be structured into trials based on timestamps of a start, trigger and end events:

            start    trigger    stop
|---- pre ----|--------|---------|--- post----|

Note: To define a trial encompassing the whole dataset simply invoke this routine with no arguments, i.e., definetrial(obj) or equivalently obj.definetrial()

Parameters:
  • obj (Syncopy data object (BaseData-like)) –

  • trialdefinition (EventData object or Mx3 array) – [start, stop, trigger_offset] sample indices for M trials

  • pre (float) – offset time (s) before start event

  • post (float) – offset time (s) after end event

  • start (int) – event code (id) to be used for start of trial

  • stop (int) – event code (id) to be used for end of trial

  • trigger – event code (id) to be used center (t=0) of trial

  • clip_edges (bool) – trim trials to actual data-boundaries.

Return type:

Syncopy data object (BaseData-like))

Notes

definetrial() supports the following argument combinations:

>>> # define M trials based on [start, end, offset] indices
>>> definetrial(obj, trialdefinition=[M x 3] array)
>>> # define trials based on event codes stored in <:class:`EventData` object>
>>> definetrial(obj, trialdefinition=<EventData object>,
                pre=0, post=0, start=startCode, stop=stopCode,
                trigger=triggerCode)
>>> # apply same trial definition as defined in <:class:`EventData` object>
>>> definetrial(<AnalogData object>,
                trialdefinition=<EventData object w/sampleinfo/t0/trialinfo>)
>>> # define whole recording as single trial
>>> definetrial(obj, trialdefinition=None)
property filename
property info

Dictionary of auxiliary meta information

property log

log of previous operations on data

Type:

str

property mode

write mode for data, ‘r’ for read-only, ‘w’ for writable

FIXME: append/replace with HDF5?

Type:

str

multipanelplot()
property sampleinfo

nTrials x 2 numpy.ndarray of [start, end] sample indices

property samplerate

sampling rate of uniformly sampled data in Hz

Type:

float

save(container=None, tag=None, filename=None, overwrite=False)

Save data object as new spy container to disk (syncopy.save_data())

FIXME: update docu

Parameters:
  • container (str) – Path to Syncopy container folder (*.spy) to be used for saving. If omitted, a .spy extension will be added to the folder name.

  • tag (str) – Tag to be appended to container basename

  • filename (str) – Explicit path to data file. This is only necessary if the data should not be part of a container folder. An extension (*.<dataclass>) will be added if omitted. The tag argument is ignored.

  • overwrite (bool) – If True an existing HDF5 file and its accompanying JSON file is overwritten (without prompt).

Examples

>>> save_spy(obj, filename="session1")
>>> # --> os.getcwd()/session1.<dataclass>
>>> # --> os.getcwd()/session1.<dataclass>.info
>>> save_spy(obj, filename="/tmp/session1")
>>> # --> /tmp/session1.<dataclass>
>>> # --> /tmp/session1.<dataclass>.info
>>> save_spy(obj, container="container.spy")
>>> # --> os.getcwd()/container.spy/container.<dataclass>
>>> # --> os.getcwd()/container.spy/container.<dataclass>.info
>>> save_spy(obj, container="/tmp/container.spy")
>>> # --> /tmp/container.spy/container.<dataclass>
>>> # --> /tmp/container.spy/container.<dataclass>.info
>>> save_spy(obj, container="session1.spy", tag="someTag")
>>> # --> os.getcwd()/container.spy/session1_someTag.<dataclass>
>>> # --> os.getcwd()/container.spy/session1_someTag.<dataclass>.info
selectdata(trials=None, channel=None, channel_i=None, channel_j=None, toi=None, toilim=None, foi=None, foilim=None, taper=None, unit=None, eventid=None, inplace=False, clear=False, parallel=None, **kwargs)

Create a new Syncopy object from a selection

Usage Notice

Syncopy offers two modes for selecting data:

  • in-place selections mark subsets of a Syncopy data object for processing via a select dictionary without creating a new object

  • deep-copy selections copy subsets of a Syncopy data object to keep and preserve in a new object created by selectdata()

All Syncopy metafunctions, such as freqanalysis(), support in-place data selection via a select keyword, effectively avoiding potentially slow copy operations and saving disk space. The keys accepted by the select dictionary are identical to the keyword arguments discussed below. In addition, select = "all" can be used to select entire object contents. Examples

>>> select = {"toilim" : [-0.25, 0]}
>>> spy.freqanalysis(data, select=select)
>>> # or equivalently
>>> cfg = spy.get_defaults(spy.freqanalysis)
>>> cfg.select = select
>>> spy.freqanalysis(cfg, data)

Usage Summary

List of Syncopy data objects and respective valid data selectors:

AnalogDatatrials, channel, toi/toilim

Examples

>>> spy.selectdata(data, trials=[0, 3, 5], channel=["channel01", "channel02"])
>>> cfg = spy.StructDict()
>>> cfg.trials = [5, 3, 0]; cfg.toilim = [0.25, 0.5]
>>> spy.selectdata(cfg, data)
SpectralDatatrials, channel, toi/toilim, foi/foilim, taper

Examples

>>> spy.selectdata(data, trials=[0, 3, 5], channel=["channel01", "channel02"])
>>> cfg = spy.StructDict()
>>> cfg.foi = [30, 40, 50]; cfg.taper = slice(2, 4)
>>> spy.selectdata(cfg, data)
EventDatatrials, toi/toilim, eventid

Examples

>>> spy.selectdata(data, toilim=[-1, 2.5], eventid=[0, 1])
>>> cfg = spy.StructDict()
>>> cfg.trials = [0, 0, 1, 0]; cfg.eventid = slice(2, None)
>>> spy.selectdata(cfg, data)
SpikeDatatrials, toi/toilim, unit, channel

Examples

>>> spy.selectdata(data, toilim=[-1, 2.5], unit=range(0, 10))
>>> cfg = spy.StructDict()
>>> cfg.toi = [1.25, 3.2]; cfg.trials = [0, 1, 2, 3]
>>> spy.selectdata(cfg, data)

Note Any property that is not specifically accessed via one of the provided selectors is taken as is, e.g., spy.selectdata(data, trials=[1, 2]) selects the entire contents of trials no. 2 and 3, while spy.selectdata(data, channel=range(0, 50)) selects the first 50 channels of data across all defined trials. Consequently, if no keywords are specified, the entire contents of data is selected.

Full documentation below

The parameters listed below can be provided as is or a via a cfg configuration ‘structure’, see Notes for details.

Parameters:
  • data (Syncopy data object) – A non-empty Syncopy data object. Note the type of data determines which keywords can be used. Some keywords are only valid for certain types of Syncopy objects, e.g., “freqs” is not a valid selector for an AnalogData object.

  • trials (list (integers) or None or "all") – List of integers representing trial numbers to be selected; can include repetitions and need not be sorted (e.g., trials = [0, 1, 0, 0, 2] is valid) but must be finite and not NaN. If trials is None, or trials = "all" all trials are selected.

  • channel (list (integers or strings), slice, range, str, int, None or "all") – Channel-selection; can be a list of channel names (['channel3', 'channel1']), a list of channel indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, channels are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, channel = [0, 1, 2] or channel = slice(0, 3) selects the first up to (and including) the third channel. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If channel is None, or channel = "all" all channels are selected.

  • toi (list (floats), float, None or "all") – Time-points to be selected (in seconds) in each trial. Timing is expected to be on a by-trial basis (e.g., relative to trigger onsets). Selections can be approximate, unsorted and may include repetitions but must be finite and not NaN. Fuzzy matching is performed for approximate selections (i.e., selected time-points are close but not identical to timing information found in data) using a nearest-neighbor search for elements of toi. If toi is None or toi = "all", the entire time-span in each trial is selected.

  • toilim (list (floats [tmin, tmax]) or None or "all") – Time-window [tmin, tmax] (in seconds) to be extracted from each trial. Window specifications must be sorted (e.g., [2.2, 1.1] is invalid) and not NaN but may be unbounded (e.g., [1.1, np.inf] is valid). Edges tmin and tmax are included in the selection. If toilim is None or toilim = "all", the entire time-span in each trial is selected.

  • foi (list (floats), float, None or "all") – Frequencies to be selected (in Hz). Selections can be approximate, unsorted and may include repetitions but must be finite and not NaN. Fuzzy matching is performed for approximate selections (i.e., selected frequencies are close but not identical to frequencies found in data) using a nearest- neighbor search for elements of foi in data.freq. If foi is None or foi = "all", all frequencies are selected.

  • foilim (list (floats [fmin, fmax]) or None or "all") – Frequency-window [fmin, fmax] (in Hz) to be extracted. Window specifications must be sorted (e.g., [90, 70] is invalid) and not NaN but may be unbounded (e.g., [-np.inf, 60.5] is valid). Edges fmin and fmax are included in the selection. If foilim is None or foilim = "all", all frequencies are selected.

  • taper (list (integers or strings), slice, range, str, int, None or "all") – Taper-selection; can be a list of taper names (['dpss-win-1', 'dpss-win-3']), a list of taper indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, tapers are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, taper = [0, 1, 2] or taper = slice(0, 3) selects the first up to (and including) the third taper. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If taper is None or taper = "all", all tapers are selected.

  • unit (list (integers or strings), slice, range, str, int, None or "all") – Unit-selection; can be a list of unit names (['unit10', 'unit3']), a list of unit indices ([3, 5]), a slice (slice(3, 10)) or range (range(3, 10)). Note that following Python conventions, units are counted starting at zero, and range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Thus, unit = [0, 1, 2] or unit = slice(0, 3) selects the first up to (and including) the third unit. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If unit is None or unit = "all", all units are selected.

  • eventid (list (integers), slice, range, int, None or "all") – Event-ID-selection; can be a list of event-id codes ([2, 0, 1]), slice (slice(0, 2)) or range (range(0, 2)). Note that following Python conventions, range and slice selections are half-open intervals of the form [low, high), i.e., low is included , high is excluded. Selections can be unsorted and may include repetitions but must match exactly, be finite and not NaN. If eventid is None or eventid = "all", all events are selected.

  • inplace (bool) – If inplace is True no new object is created. Instead the provided selection is stored in the input object’s selection attribute for later use. By default inplace is False and all calls to selectdata create a new Syncopy data object.

  • clear (bool) – If True remove any active in-place selection. Note that in-place selections can also be removed manually by assinging None to the selection property, i.e., mydata.selection = None is equivalent

  • parallel (None or bool) – If None (recommended), processing is automatically performed in parallel (i.e., concurrently across trials/channel-groups), provided a dask parallel processing client is running and available. Parallel processing can be manually disabled by setting parallel to False. If parallel is True but no parallel processing client is running, computing will be performed sequentially. to spy.selectdata(mydata, clear=True) or mydata.selectdata(clear=True)

Returns:

dataselection – Syncopy data object of the same type as data but containing only the subset specified by provided selectors.

Return type:

Syncopy data object

Notes

This function can be either called providing its input arguments directly or via a cfg configuration ‘structure’. For instance, the following function calls are equivalent

>>> spy.selectdata(data, trials=...)
>>> cfg = spy.StructDict()
>>> cfg.trials = ...
>>> spy.selectdata(cfg, data)
>>> cfg.data = data
>>> spy.selectdata(cfg)

Please refer to Syncopy for FieldTrip Users for further details.

This routine represents a convenience function for creating new Syncopy objects based on existing data entities. However, in many situations, the creation of a new object (and thus the allocation of additional disk-space) might not be necessary: all Syncopy metafunctions, such as freqanalysis(), support in-place data selection.

Consider the following example: assume data is an AnalogData object representing 220 trials of LFP recordings containing baseline (between second -0.25 and 0) and stimulus-on data (on the interval [0.25, 0.5]). To compute the baseline spectrum, data-selection does not have to be performed before calling freqanalysis() but instead can be done in-place:

>>> import syncopy as spy
>>> cfg = spy.get_defaults(spy.freqanalysis)
>>> cfg.method = 'mtmfft'
>>> cfg.taper = 'dpss'
>>> cfg.output = 'pow'
>>> cfg.tapsmofrq = 10
>>> # define baseline/stimulus-on ranges
>>> baseSelect = {"toilim": [-0.25, 0]}
>>> stimSelect = {"toilim": [0.25, 0.5]}
>>> # in-place selection of baseline interval performed by `freqanalysis`
>>> cfg.select = baseSelect
>>> baselineSpectrum = spy.freqanalysis(cfg, data)
>>> # in-place selection of stimulus-on time-frame performed by `freqanalysis`
>>> cfg.select = stimSelect
>>> stimonSpectrum = spy.freqanalysis(cfg, data)

Especially for large data-sets, in-place data selection performed by Syncopy’s metafunctions does not only save disk-space but can significantly increase performance.

Examples

Use generate_artificial_data() to create a synthetic syncopy.AnalogData object.

>>> from syncopy.tests.misc import generate_artificial_data
>>> adata = generate_artificial_data(nTrials=10, nChannels=32)

Assume a hypothetical trial onset at second 2.0 with the first second of each trial representing baseline recordings. To extract only the stimulus-on period from adata, one could use

>>> stimon = spy.selectdata(adata, toilim=[2.0, np.inf])

Note that this is equivalent to

>>> stimon = adata.selectdata(toilim=[2.0, np.inf])

See also

syncopy.show()

Show (subsets) of Syncopy objects

property selection

Data selection specified by Selector

show(squeeze=True, **kwargs)

Show (partial) contents of Syncopy object

Usage Notice

Syncopy uses HDF5 files as on-disk backing device for data storage. This allows working with larger-than-memory data-sets by streaming only relevant subsets of data from disk on demand without excessive RAM use. However, using show() this mechanism is bypassed and the requested data subset is loaded into memory at once. Thus, inadvertent usage of show() on a large data object can lead to memory overflow or even out-of-memory errors.

Usage Summary

Data selectors for showing subsets of Syncopy data objects follow the syntax of selectdata(). Please refer to selectdata() for a list of valid data selectors for respective Syncopy data objects.

Parameters:
  • data (Syncopy data object) – As for subset-selection via selectdata(), the type of data determines which keywords can be used. Some keywords are only valid for certain types of Syncopy objects, e.g., “freqs” is not a valid selector for an AnalogData object.

  • squeeze (bool) – If True (default) any singleton dimensions are removed from the output array, i.e., the shape of the returned array does not contain ones (e.g., arr.shape = (2,) not arr.shape = (1,2,1,1)).

  • **kwargs (keywords) – Valid data selectors (e.g., trials, channels, toi etc.). Please refer to selectdata() for a full list of available data selectors.

Returns:

arr – A (selection) of data retrieved from the data input object.

Return type:

NumPy nd-array

Notes

This routine represents a convenience function for quickly inspecting the contents of Syncopy objects. It is always possible to manually access an object’s numerical data by indexing the underlying HDF5 dataset: data.data[idx]. The dimension labels of the dataset are encoded in data.dimord, e.g., if data is a AnalogData with data.dimord being [‘time’, ‘channel’] and data.data.shape is (15000, 16), then data.data[:, 3] returns the contents of the fourth channel across all time points.

Examples

Use generate_artificial_data() to create a synthetic syncopy.AnalogData object.

>>> from syncopy.tests.misc import generate_artificial_data
>>> adata = generate_artificial_data(nTrials=10, nChannels=32)

Show the contents of ‘channel02’ across all trials:

>>> spy.show(adata, channel='channel02')
Syncopy <show> INFO: Showing all times 10 trials
Out[2]: array([1.0871, 0.7267, 0.2816, ..., 1.0273, 0.893 , 0.7226], dtype=float32)

Note that this is equivalent to

>>> adata.show(channel='channel02')

To preserve singleton dimensions use squeeze=False:

>>> adata.show(channel='channel02', squeeze=False)
Out[3]:
array([[1.0871],
       [0.7267],
       [0.2816],
       ...,
       [1.0273],
       [0.893 ],
       [0.7226]], dtype=float32)

See also

syncopy.selectdata()

Create a new Syncopy object from a selection

property tag
property time

trigger-relative time axes of each trial

Type:

list(float)

property trialdefinition

]]

Type:

nTrials x >=3 numpy.ndarray of [start, end, offset, trialinfo[

property trialinfo

nTrials x M numpy.ndarray with numeric information about each trial

Each trial can have M properties (condition, original trial no., …) coded by numbers. This property are the fourth and onward columns of BaseData._trialdefinition.

property trials

list-like array of trials

property channel_j

list of recording channel names

Type:

numpy.ndarray

__init__(data=None, filename=None, channel_i=None, channel_j=None, samplerate=None, freq=None, dimord=None)[source]

Docstring

  1. filename + data = create HDF5 file at filename with data in it

  2. data only

property dimord

ordered list of data dimension labels

Type:

list(str)

property freq

frequency axis in Hz

Type:

numpy.ndarray

singlepanelplot(**show_kwargs)[source]