-
Notifications
You must be signed in to change notification settings - Fork 216
Add documentation to handle (physical) units of recording channels #3844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
h-mayorquin
wants to merge
27
commits into
SpikeInterface:main
Choose a base branch
from
h-mayorquin:metric_system_units_docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0f61068
add documentation to handle units
h-mayorquin ee6542b
fix scaling
h-mayorquin 3c81942
add tests
h-mayorquin 9f05e45
Apply suggestions from code review
h-mayorquin 3bd19e9
Merge branch 'main' into metric_system_units_docs
h-mayorquin 2b10362
Merge branch 'main' into metric_system_units_docs
h-mayorquin 06c4aba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 102507c
Merge branch 'main' into metric_system_units_docs
h-mayorquin 1748800
fix
h-mayorquin 2a86ae3
Merge branch 'main' into metric_system_units_docs
h-mayorquin 0498e44
to class
h-mayorquin 0752f30
fixes and chris suggestion for docs
h-mayorquin 5a37cc3
add more notes
h-mayorquin 75fe09a
Apply suggestions from code review
h-mayorquin 32841a3
zach suggestions
h-mayorquin 87f8b63
another zach suggestion
h-mayorquin b00d190
Merge branch 'main' into metric_system_units_docs
h-mayorquin fe732c9
Merge branch 'main' into metric_system_units_docs
h-mayorquin 703c8ed
Merge branch 'main' into metric_system_units_docs
h-mayorquin 966cce8
Apply suggestions from code review
h-mayorquin aadcddb
chris suggestion
h-mayorquin 5d82382
Update src/spikeinterface/preprocessing/scale.py
h-mayorquin 052b2d7
Update doc/how_to/physical_units.rst
h-mayorquin 124c803
improve
h-mayorquin 458d5ca
Update doc/how_to/physical_units.rst
h-mayorquin cf26c9a
Update doc/how_to/physical_units.rst
h-mayorquin 47b1c35
Merge branch 'main' into metric_system_units_docs
h-mayorquin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
Working with Units in SpikeInterface Recordings | ||
=============================================== | ||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In neurophysiology recordings, data is often stored in raw ADC (Analog-to-Digital Converter) units | ||
but needs to be analyzed in physical units. For extracellular recordings, this is typically microvolts (µV), | ||
but some recording devices may use different physical units. SpikeInterface provides tools to handle both | ||
situations. | ||
|
||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Understanding Physical Units | ||
---------------------------- | ||
|
||
Most recording devices store data in ADC units (integers) to save space and preserve the raw data. | ||
To convert these values to physical units, two parameters are needed: | ||
|
||
* **gain**: A multiplicative factor to scale the raw values | ||
* **offset**: An additive factor to shift the values | ||
|
||
The conversion formula is: | ||
|
||
.. code-block:: text | ||
|
||
physical_value = raw_value * gain + offset | ||
|
||
|
||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Converting to Physical Units | ||
------------------------- | ||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
SpikeInterface provides two preprocessing classes for converting recordings to physical units. Both wrap the | ||
``RecordingExtractor`` class and ensures that the data is returned in physical units when calling get_traces() | ||
h-mayorquin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. ``scale_to_uV``: The primary function for extracellular recordings. SpikeInterface is centered around | ||
extracellular recordings, and this function is designed to convert the data to microvolts (µV). Many plottinf | ||
h-mayorquin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
and analyzing functions in SpikeInterface expect data in microvolts, so this is the recommended approach for most users. | ||
2. ``scale_to_physical_units``: A general function for any physical unit conversion. This will allow you to extract the data in any | ||
physical unit, not just microvolts. This is useful for other types of recordings, such as force measurements in Newtons but should be | ||
handled with care. | ||
|
||
For most users working with extracellular recordings, ``scale_to_uV`` is the recommended choice: | ||
|
||
.. code-block:: python | ||
|
||
from spikeinterface.extractors import read_intan | ||
from spikeinterface.preprocessing import scale_to_uV | ||
|
||
# Load recording (data is in ADC units) | ||
recording = read_intan("path/to/file.rhs") | ||
|
||
# Convert to microvolts | ||
recording_uv = scale_to_uV(recording) | ||
|
||
For recordings with non-standard units (e.g., force measurements in Newtons), use ``scale_to_physical_units``: | ||
|
||
.. code-block:: python | ||
|
||
from spikeinterface.preprocessing import scale_to_physical_units | ||
|
||
# Convert to physical units (whatever they may be) | ||
recording_physical = scale_to_physical_units(recording) | ||
|
||
Both preprocessors automatically: | ||
|
||
1. Detect the appropriate gain and offset from the recording properties | ||
2. Apply the conversion to all channels | ||
3. Update the recording properties to reflect that data is now in physical units | ||
|
||
Setting Custom Physical Units | ||
--------------------------- | ||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
While most extractors automatically set the appropriate ``gain_to_uV`` and ``offset_to_uV`` values, | ||
there might be cases where you want to set custom physical units. In these cases, you can set | ||
the following properties: | ||
|
||
* ``physical_unit``: The target physical unit (e.g., 'uV', 'mV', 'N') | ||
* ``gain_to_unit``: The gain to convert from raw values to the target unit | ||
* ``offset_to_unit``: The offset to convert from raw values to the target unit | ||
|
||
Here's an example of how to set custom units: | ||
chrishalcrow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: python | ||
|
||
# Set custom physical units | ||
num_channels = recording.get_num_channels() | ||
values = ["volts"] * num_channels | ||
recording.set_property(key='physical_unit', value=values) | ||
|
||
values = [0.001] * num_channels # Convert from ADC to volts | ||
zm711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recording.set_property(key='gain_to_unit', values=values) # Convert to volts | ||
|
||
values = [0] * num_channels # No offset | ||
recording.set_property(key='offset_to_unit', values=values) # No offset | ||
|
||
# Apply the conversion using scale_to_physical_units | ||
recording_physical = scale_to_physical_units(recording) | ||
|
||
This approach gives you full control over the unit conversion process while maintaining | ||
compatibility with SpikeInterface's preprocessing pipeline. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.