From 245e936e199dc44c1a3e41394e512371ff0c28d0 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Mon, 11 Jan 2021 15:07:05 -0500 Subject: [PATCH 1/7] update trigger guesstimate calculation --- phys2bids/physio_obj.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 3466aa626..94fb5ed5b 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -467,11 +467,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) - flag = 1 + thr = np.mean(trigger) + 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, lambda x: x != 0) if is_true]) From 16bcef52a77b4045c026a0d403ac6c53ecd9fbc3 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Mon, 11 Jan 2021 15:11:54 -0500 Subject: [PATCH 2/7] update check_trigger_amount() documentation to reflect changes --- phys2bids/physio_obj.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 94fb5ed5b..0e8fbd803 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -444,7 +444,8 @@ 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. + 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 From 1ceaabcc4fdc5c0c52594322ca21be1e6c06c8c0 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Mon, 11 Jan 2021 22:13:40 -0500 Subject: [PATCH 3/7] Update physio_obj.py accidentally deleted flag initial assignment, fixed now! --- phys2bids/physio_obj.py | 1 + 1 file changed, 1 insertion(+) diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 0e8fbd803..154618a86 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -468,6 +468,7 @@ 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)) + flag = 0 if thr is None: thr = np.mean(trigger) + np.std(trigger) # If trigger channels are binary # (i.e., "on" is a higher value and "off" is a lower value) From ae7deeaa4e0c78e0167a2f78d46f0c1d16f70139 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Tue, 12 Jan 2021 10:47:00 -0500 Subject: [PATCH 4/7] fixed linting errors --- phys2bids/physio_obj.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 0e8fbd803..4113ed0a4 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -444,7 +444,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. + 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. @@ -469,13 +469,14 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0): time = np.linspace(time[0], time[-1], len(trigger)) if thr is None: - thr = np.mean(trigger) + np.std(trigger) # If trigger channels are binary + thr = np.mean(trigger) + 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". + # is "off". thr = np.mean(trigger) - flag = 1 + flag = 1 timepoints = trigger > thr num_timepoints_found = len([is_true for is_true, _ in groupby(timepoints, lambda x: x != 0) if is_true]) From fa6467a7f7c7c435b35d9758e9e2027fcc7f6813 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Tue, 12 Jan 2021 10:50:32 -0500 Subject: [PATCH 5/7] fix duplicate thr typo, updated docs --- docs/howto.rst | 2 +- phys2bids/physio_obj.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/howto.rst b/docs/howto.rst index 81a5a26d5..ddf6a5274 100644 --- a/docs/howto.rst +++ b/docs/howto.rst @@ -202,7 +202,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. diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 7b6397231..7c1ac6959 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -469,8 +469,7 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0): time = np.linspace(time[0], time[-1], len(trigger)) flag = 0 - if thr is None: - thr = np.mean(trigger) + np.std(trigger) + if thr is None: # 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 From 3c54b5223f307395f200717a6868cef0389136c0 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Tue, 12 Jan 2021 10:53:05 -0500 Subject: [PATCH 6/7] fix typo in howto docs --- docs/howto.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/howto.rst b/docs/howto.rst index ddf6a5274..f7c099393 100644 --- a/docs/howto.rst +++ b/docs/howto.rst @@ -225,7 +225,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): From 6fda3f6183da42e872d147d7d1e80232c2086976 Mon Sep 17 00:00:00 2001 From: Katie Bottenhorn Date: Tue, 12 Jan 2021 10:59:40 -0500 Subject: [PATCH 7/7] fix linting issues --- phys2bids/physio_obj.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phys2bids/physio_obj.py b/phys2bids/physio_obj.py index 7c1ac6959..a1845e6eb 100644 --- a/phys2bids/physio_obj.py +++ b/phys2bids/physio_obj.py @@ -469,14 +469,14 @@ def check_trigger_amount(self, thr=None, num_timepoints_expected=0, tr=0): time = np.linspace(time[0], time[-1], len(trigger)) flag = 0 - if thr is None: + if thr is None: # 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 + flag = 1 timepoints = trigger > thr num_timepoints_found = len([is_true for is_true, _ in groupby(timepoints, lambda x: x != 0) if is_true])