-
Notifications
You must be signed in to change notification settings - Fork 48
Add an automatic algorithm to find group of triggers, i.e. "takes" or "runs", and attempt splitting recording automagically. #482
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
smoia
wants to merge
8
commits into
physiopy:master
Choose a base branch
from
smoia:enh/spikegroups
base: master
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8288587
Actually allow SMR files to be processed
smoia a374403
Update workflows
smoia bf0c136
Fix some string issues
smoia a2ba8e9
Update setup to newer physiopy
smoia 412f642
Add a brand new cluster estimation to find groups of triggers, i.e. t…
smoia ee18224
numpy docstrings.
smoia 6324c1c
Fix issue with help
smoia 3e5bea3
Fix more explanation of CI and also add a tiny comment to stop puzzli…
smoia 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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,127 @@ | |||||
| LGR = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
||||||
| def estimate_ntp_and_tr(phys_in, thr=None, ci=1): | ||||||
| """ | ||||||
| Find groups of trigger in a spiky signal like the trigger channel signal. | ||||||
| Parameters | ||||||
| ---------- | ||||||
| phys_in : BlueprintInput object | ||||||
| A BlueprintInput object containing a physiological acquisition | ||||||
| thr : None, optional | ||||||
| The threshold for automatic spike detection. Default is to use the average of | ||||||
| the signal. | ||||||
| ci : int or float, optional | ||||||
| Confidence Interval (CI) to use in the estimation of the trigger clusters. The | ||||||
| cluster algorithm considers triggers with duration (in samples) within this CI | ||||||
| as part of the same group, thus the same. If CI is an integer, it will consider | ||||||
| that amount of samples. If CI is a float and < 1, it will consider that | ||||||
| percentage of the trigger duration. CI cannot be a float > 1. Default is 1. | ||||||
| Change to .25 if there is a CMRR DWI sequence or when recording sub-triggers. | ||||||
| Returns | ||||||
| ------- | ||||||
| ntp | ||||||
| The list of number of timepoints found for each take. | ||||||
| tr | ||||||
| The list of corresponding TR, computed as average samples per group / frequency. | ||||||
| Raises | ||||||
| ------ | ||||||
| Exception | ||||||
| If it doesn't find at least a group. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ValueError | ||||||
| If CI is a float above 1 or if it is not an int or a float. | ||||||
| """ | ||||||
| LGR.info('Running automatic clustering of triggers to find timepoints and tr of each "take"') | ||||||
| trigger = phys_in.timeseries[phys_in.trigger_idx] | ||||||
|
|
||||||
| thr = np.mean(trigger) if thr is None else thr | ||||||
| timepoints = trigger > thr | ||||||
| spikes = np.flatnonzero(np.ediff1d(timepoints.astype(np.int8)) > 0) | ||||||
| interspike_interval = np.diff(spikes) | ||||||
| unique_isi, counts = np.unique(interspike_interval, return_counts=True) | ||||||
|
|
||||||
| # The following line is for python < 3.12. From 3.12, ci.is_integer() is enough. | ||||||
| if isinstance(ci, int) or isinstance(ci, float) and ci.is_integer(): | ||||||
| upper_ci_isi = unique_isi + ci | ||||||
| elif isinstance(ci, float) and ci < 1: | ||||||
| upper_ci_isi = unique_isi * (1 + ci) | ||||||
| elif isinstance(ci, float) and ci > 1: | ||||||
| raise ValueError("Confidence intervals percentages above 1 are not supported.") | ||||||
| else: | ||||||
| raise ValueError("Confidence intervals must be either integers or floats.") | ||||||
|
|
||||||
| # Loop through the uniques ISI and group them within the specified CI. | ||||||
| # Also compute the average TR of the group. | ||||||
| isi_groups = {} | ||||||
| average_tr = {} | ||||||
| k = 0 | ||||||
| current_group = [unique_isi[0]] | ||||||
|
|
||||||
| # np.unique returns sorted elements → unique_isi[0] == min(unique_isi), so THIS WORKS. | ||||||
| for n, i in enumerate(range(1, len(unique_isi))): | ||||||
| if unique_isi[i] <= upper_ci_isi[n]: | ||||||
| current_group.append(unique_isi[i]) | ||||||
| else: | ||||||
| isi_groups[k] = current_group | ||||||
| average_tr[k] = np.mean(current_group) / phys_in.freq[0] | ||||||
| k += 1 | ||||||
| current_group = [unique_isi[i]] | ||||||
|
|
||||||
| isi_groups[k] = current_group | ||||||
| average_tr[k] = np.mean(current_group) / phys_in.freq[0] | ||||||
|
|
||||||
| # Invert the isi_group into value per group | ||||||
| group_by_isi = {isi: group for group, isis in isi_groups.items() for isi in isis} | ||||||
|
|
||||||
| # Use the found groups to find the number of timepoints and assign the right TR | ||||||
| estimated_ntp = [] | ||||||
| estimated_tr = [] | ||||||
|
|
||||||
| i = 0 | ||||||
| while i < interspike_interval.size - 1: | ||||||
| current_group = group_by_isi.get(interspike_interval[i]) | ||||||
| for n in range(i + 1, interspike_interval.size): | ||||||
| if current_group != group_by_isi.get(interspike_interval[n]): | ||||||
| break | ||||||
| # Repeat one last time outside of for loop | ||||||
| estimated_ntp += [n - i] | ||||||
| estimated_tr += [average_tr[current_group]] | ||||||
| i = n | ||||||
|
|
||||||
| if len(estimated_ntp) < 1: | ||||||
| raise Exception("This should not happen. Something went very wrong.") | ||||||
| # The algorithm found n groups, the last of which has two timepoints less due to | ||||||
| # diff computations. Each real group of n>1 triggers counts one trigger less but is | ||||||
| # followed by a "fake" group of 1 trigger that is actually the interval to the next | ||||||
| # group. That does not hold if there is a real group of 1 trigger. | ||||||
| # Loop through the estiamtions to fix all that. | ||||||
| ntp = [] | ||||||
| tr = [] | ||||||
| i = 0 | ||||||
|
|
||||||
| while i < len(estimated_ntp): | ||||||
| if estimated_ntp[i] == 1: | ||||||
| ntp.append(estimated_ntp[i]) | ||||||
| tr.append(estimated_tr[i]) | ||||||
| i += 1 | ||||||
| elif i + 1 < len(estimated_ntp): | ||||||
| ntp.append(estimated_ntp[i] + estimated_ntp[i + 1]) | ||||||
| tr.append(estimated_tr[i]) | ||||||
| i += 2 | ||||||
| else: | ||||||
| ntp.append(estimated_ntp[i] + 2) | ||||||
| tr.append(estimated_tr[i]) | ||||||
| i += 1 | ||||||
|
|
||||||
| LGR.info( | ||||||
| f"The automatic clustering found {len(ntp)} groups of triggers long: {ntp} with respective TR: {tr}" | ||||||
| ) | ||||||
| return ntp, tr | ||||||
|
|
||||||
|
|
||||||
| def find_takes(phys_in, ntp_list, tr_list, thr=None, padding=9): | ||||||
| """ | ||||||
| Find takes slicing index. | ||||||
|
|
||||||
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 |
|---|---|---|
|
|
@@ -2,18 +2,14 @@ | |
| name = phys2bids | ||
| url = https://github.com/physiopy/phys2bids | ||
| download_url = https://github.com/physiopy/phys2bids | ||
| author = phys2bids developers | ||
| maintainer = Stefano Moia | ||
| author = The Physiopy Community | ||
| maintainer = The Physiopy Community | ||
| maintainer_email = [email protected] | ||
| classifiers = | ||
| Development Status :: 5 - Production/Stable | ||
| Intended Audience :: Science/Research | ||
| License :: OSI Approved :: Apache Software License | ||
| Programming Language :: Python :: 3.6 | ||
| Programming Language :: Python :: 3.7 | ||
| Programming Language :: Python :: 3.8 | ||
| Programming Language :: Python :: 3.9 | ||
| Programming Language :: Python :: 3.10 | ||
| Programming Language :: Python :: 3 | ||
| license = Apache-2.0 | ||
| description = Python library to convert physiological data files into BIDS format | ||
| long_description = file:README.md | ||
|
|
@@ -64,7 +60,7 @@ test = | |
| coverage | ||
| %(interfaces)s | ||
| %(style)s | ||
| all = | ||
| dev = | ||
| %(doc)s | ||
| %(duecredit)s | ||
| %(interfaces)s | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"This might work 95%% of the time" is a funny sentence! Can we be clearer on why it may not? Or can we say something like "please check this output/plot carefully to see if it has done X task correctly"