Skip to content

Commit

Permalink
Merge pull request #377 from 62442katieb/bug/trigger-thresh
Browse files Browse the repository at this point in the history
Update trigger threshold guess estimation to mean of trigger channel
  • Loading branch information
Stefano Moia authored Nov 15, 2021
2 parents edbb77d + 6fda3f6 commit af56df4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Now the output says:
Tip: Time 0 is the time of first trigger
------------------------------------------------
``phys2bids`` has an automatic way of finding the right threshold, in order to find the correct number of timepoints, by using the mean and standard deviation of the trigger channel. If "Found just the right amount of timepoints!" appears everything should be working properly!
``phys2bids`` has an automatic way of finding the right threshold, in order to find the correct number of timepoints, by using the mean of the trigger channel. If "Found just the right amount of timepoints!" appears everything should be working properly!

Alright, so now we have some outputs that make sense! We have 158 timepoints expected (as inputted with ``-ntp``) and found. The output also tells us that the fMRI sampling started around 0.25 seconds later than the start of this physiological sampling.

Expand All @@ -231,7 +231,7 @@ If for some reason ``-ntp`` and the number of timepoints found by ``phys2bids``
3. The file doesn't have all the trigger pulses you expect because the recording started later than the MRI recording (e.g. by mistake).

.. note::
``phys2bids`` was created to deal with little sampling errors - such as distracted researchers that started sampling a bit too late than expected. For this reason, if it finds less trigger pulses than the amount specified, it will assume that the missing ones are at the beginning and anticipate the starting time consequently.
``phys2bids`` was created to deal with little sampling errors - such as distracted researchers that started sampling a bit too late than expected. For this reason, if it finds fewer trigger pulses than the amount specified, it will assume that the missing ones are at the beginning and anticipate the starting time consequently.


Let's go through an example where the number of timepoints automatically found is not correct. For that, will we use tutorial_file_v2.txt (in the same location as tutorial_file.txt):
Expand Down
9 changes: 7 additions & 2 deletions phys2bids/physio_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0):
Outcome:
self.thr: float
Threshold used by the function to detect trigger points.
If no threshold is provided, value is the mean of the trigger channel.
self.num_timepoints_found: int
Property of the `BlueprintInput` class.
Contains the number of timepoints found
Expand All @@ -477,10 +478,14 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0):
'of time to find the starting time.')
time = np.linspace(time[0], time[-1], len(trigger))

# Check if thr was given, if not "guess" it.
flag = 0
if thr is None:
thr = np.mean(trigger) + 2 * np.std(trigger)
# If trigger channels are binary
# (i.e., "on" is a higher value and "off" is a lower value)
# and each "on" and "off" are each always approzimately the same value
# then any value above the mean is "on" and every value below the mean
# is "off".
thr = np.mean(trigger)
flag = 1
timepoints = trigger > thr
num_timepoints_found = len([is_true for is_true, _ in groupby(timepoints,
Expand Down

0 comments on commit af56df4

Please sign in to comment.