Repository: bids-standard/pybids

Issue:
Including a .json sidecar for events.tsv files causes a SQLAlchemy Tag conflict
We have an fMRIPrep user experiencing the following issue (more details here https://neurostars.org/t/naming-of-bids-events-tsv-files-seems-to-disrupt-fmriprep-1-5-0rc1/4771):

```
  File "/usr/local/miniconda/lib/python3.7/site-packages/fmriprep/cli/run.py", line 524, in build_workflow
    layout = BIDSLayout(str(bids_dir), validate=False)
  File "/usr/local/miniconda/lib/python3.7/site-packages/bids/layout/layout.py", line 212, in __init__
    indexer.index_metadata()
  File "/usr/local/miniconda/lib/python3.7/site-packages/bids/layout/index.py", line 338, in index_metadata
    self.session.commit()
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1027, in commit
    self.transaction.commit()
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 494, in commit
    self._prepare_impl()
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 473, in _prepare_impl
    self.session.flush()
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2459, in flush
    self._flush(objects)
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2597, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
    raise value
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2557, in _flush
    flush_context.execute()
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
    rec.execute(self)
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
    uow,
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 213, in save_obj
    ) in _organize_states_for_save(base_mapper, states, uowtransaction):
  File "/usr/local/miniconda/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 408, in _organize_states_for_save
    % (state_str(state), instance_key, state_str(existing))
sqlalchemy.orm.exc.FlushError: New instance <Tag at 0x2b66784f7ba8> with identity key (<class 'bids.layout.models.Tag'>, ('/inp/sub-02/func/sub-02_task-Emotion_run-1_events.tsv', 'run'), None) conflicts with persistent instance <Tag at 0x2b66784c6630>
```


Discussion:
It's hard to know if this is a bug without knowing what other files are in the BIDS project. My sense is it could be a genuine conflict between file names induced by some minor departure from the BIDS standard. It would be helpful if the user could provide a list of all of the `events.tsv` files in the project.
If it is not a bug, I'd say a more descriptive error message would improve user experience a lot.
Well, if we had a more descriptive error message, it would be easier for me to tell you if it's a bug or not. :p
Per discussion in the [linked NeuroStars thread](https://neurostars.org/t/naming-of-bids-events-tsv-files-seems-to-disrupt-fmriprep-1-5-0rc1/4771), this seems like a bug induced by the inclusion of a `.json` file with the same filename (except for extension) as an `events.tsv` file.
This wasn't actually fixed in #467; I'm working on a fix now.

Resolving patch:
diff --git a/bids/layout/index.py b/bids/layout/index.py
--- a/bids/layout/index.py
+++ b/bids/layout/index.py
@@ -179,6 +179,15 @@ def index_metadata(self):
         for c in self.config:
             all_entities.update(c.entities)
 
+        # If key/value pairs in JSON files duplicate ones extracted from files,
+        # we can end up with Tag collisions in the DB. To prevent this, we
+        # store all filename/entity pairs and the value, and then check against
+        # that before adding each new Tag.
+        all_tags = {}
+        for t in self.session.query(Tag).all():
+            key = '{}_{}'.format(t.file_path, t.entity_name)
+            all_tags[key] = str(t.value)
+
         # We build up a store of all file data as we iterate files. It looks
         # like: { extension/suffix: dirname: [(entities, payload)]}}.
         # The payload is left empty for non-JSON files.
@@ -326,6 +335,19 @@ def create_association_pair(src, dst, kind, kind2=None):
 
             # Create Tag <-> Entity mappings, and any newly discovered Entities
             for md_key, md_val in file_md.items():
+                tag_string = '{}_{}'.format(bf.path, md_key)
+                # Skip pairs that were already found in the filenames
+                if tag_string in all_tags:
+                    file_val = all_tags[tag_string]
+                    if str(md_val) != file_val:
+                        msg = (
+                            "Conflicting values found for entity '{}' in "
+                            "filename {} (value='{}') versus its JSON sidecar "
+                            "(value='{}'). Please reconcile this discrepancy."
+                            )
+                        raise ValueError(msg.format(md_key, bf.path, file_val,
+                                                    md_val))
+                    continue
                 if md_key not in all_entities:
                     all_entities[md_key] = Entity(md_key, is_metadata=True)
                     self.session.add(all_entities[md_key])


Test patch:
diff --git a/bids/layout/tests/test_layout.py b/bids/layout/tests/test_layout.py
--- a/bids/layout/tests/test_layout.py
+++ b/bids/layout/tests/test_layout.py
@@ -327,17 +327,18 @@ def test_layout_with_multi_derivs(layout_ds005_multi_derivs):
 
 
 def test_query_derivatives(layout_ds005_derivs):
-    result = layout_ds005_derivs.get(suffix='events', return_type='object')
+    result = layout_ds005_derivs.get(suffix='events', return_type='object',
+                                     extension='tsv')
     result = [f.filename for f in result]
     assert len(result) == 49
     assert 'sub-01_task-mixedgamblestask_run-01_desc-extra_events.tsv' in result
     result = layout_ds005_derivs.get(suffix='events', return_type='object',
-                                     scope='raw')
+                                     scope='raw', extension='tsv')
     assert len(result) == 48
     result = [f.filename for f in result]
     assert 'sub-01_task-mixedgamblestask_run-01_desc-extra_events.tsv' not in result
     result = layout_ds005_derivs.get(suffix='events', return_type='object',
-                                     desc='extra')
+                                     desc='extra', extension='tsv')
     assert len(result) == 1
     result = [f.filename for f in result]
     assert 'sub-01_task-mixedgamblestask_run-01_desc-extra_events.tsv' in result
@@ -548,3 +549,12 @@ def test_layout_save(layout_7t_trt):
     newfies = set(layout.get(suffix='events', return_type='file'))
     assert oldfies == newfies
     os.unlink(f)
+
+
+def test_indexing_tag_conflict():
+    data_dir = join(get_test_data_path(), 'ds005_conflict')
+    with pytest.raises(ValueError) as exc:
+        layout = BIDSLayout(data_dir)
+        print(exc.value.message)
+        assert exc.value.message.startswith("Conflicting values found")
+        assert 'run' in exc.value.message
\ No newline at end of file
diff --git a/bids/tests/data/ds005/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json b/bids/tests/data/ds005/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json
@@ -0,0 +1,3 @@
+{
+    "run": 1
+}
\ No newline at end of file
diff --git a/bids/tests/data/ds005_conflict/dataset_description.json b/bids/tests/data/ds005_conflict/dataset_description.json
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005_conflict/dataset_description.json
@@ -0,0 +1,6 @@
+{
+    "BIDSVersion": "1.0.0rc2",
+    "License": "This dataset is made available under the Public Domain Dedication and License \nv1.0, whose full text can be found at \nhttp://www.opendatacommons.org/licenses/pddl/1.0/. \nWe hope that all users will follow the ODC Attribution/Share-Alike \nCommunity Norms (http://www.opendatacommons.org/norms/odc-by-sa/); \nin particular, while not legally required, we hope that all users \nof the data will acknowledge the OpenfMRI project and NSF Grant \nOCI-1131441 (R. Poldrack, PI) in any publications.",
+    "Name": "Mixed-gambles task",
+    "ReferencesAndLinks": "Tom, S.M., Fox, C.R., Trepel, C., Poldrack, R.A. (2007). The neural basis of loss aversion in decision-making under risk. Science, 315(5811):515-8"
+}
\ No newline at end of file
diff --git a/bids/tests/data/ds005_conflict/sub-01/anat/sub-01_T1w.nii.gz b/bids/tests/data/ds005_conflict/sub-01/anat/sub-01_T1w.nii.gz
new file mode 100644
diff --git a/bids/tests/data/ds005_conflict/sub-01/dwi/sub-01_dwi.nii.gz b/bids/tests/data/ds005_conflict/sub-01/dwi/sub-01_dwi.nii.gz
new file mode 100644
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_bold.nii.gz b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_bold.nii.gz
new file mode 100644
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.json
@@ -0,0 +1,3 @@
+{
+    "run": 2
+}
\ No newline at end of file
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.tsv b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.tsv
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-01_events.tsv
@@ -0,0 +1,87 @@
+onset	duration	trial_type	distance from indifference	parametric gain	parametric loss	gain	loss	PTval	respnum	respcat	RT
+0.0	3	parametric gain	n/a	-0.139	n/a	20	15	5.15	0	-1	0.0
+4.0	3	parametric gain	n/a	-0.189	n/a	18	12	6.12	2	1	1.7930000000000001
+8.0	3	parametric gain	n/a	-0.389	n/a	10	15	-4.85	3	0	1.6369999999999998
+18.0	3	parametric gain	n/a	0.211	n/a	34	16	18.16	1	1	1.3159999999999998
+24.0	3	parametric gain	n/a	-0.189	n/a	18	5	13.05	1	1	1.67
+28.0	3	parametric gain	n/a	0.011	n/a	26	13	13.13	2	1	1.232
+38.0	3	parametric gain	n/a	0.161	n/a	32	17	15.17	2	1	1.5019999999999998
+42.0	3	parametric gain	n/a	-0.239	n/a	16	5	11.05	2	1	1.925
+46.0	3	parametric gain	n/a	-0.089	n/a	22	7	15.07	1	1	1.061
+52.0	3	parametric gain	n/a	0.261	n/a	36	8	28.08	1	1	1.079
+56.0	3	parametric gain	n/a	-0.239	n/a	16	10	6.1	2	1	1.4769999999999999
+62.0	3	parametric gain	n/a	0.111	n/a	30	13	17.13	1	1	1.599
+74.0	3	parametric gain	n/a	-0.289	n/a	14	17	-2.83	4	0	1.8219999999999998
+78.0	3	parametric gain	n/a	0.061	n/a	28	19	9.19	2	1	1.405
+82.0	3	parametric gain	n/a	-0.289	n/a	14	20	-5.8	4	0	1.206
+86.0	3	parametric gain	n/a	0.011	n/a	26	16	10.16	2	1	1.272
+90.0	3	parametric gain	n/a	0.361	n/a	40	5	35.05	1	1	0.9
+94.0	3	parametric gain	n/a	0.061	n/a	28	12	16.12	2	1	1.318
+98.0	3	parametric gain	n/a	-0.139	n/a	20	16	4.16	2	1	1.367
+102.0	3	parametric gain	n/a	0.011	n/a	26	5	21.05	1	1	1.5659999999999998
+106.0	3	parametric gain	n/a	0.161	n/a	32	20	12.2	2	1	1.36
+110.0	3	parametric gain	n/a	-0.189	n/a	18	17	1.17	3	0	1.321
+116.0	3	parametric gain	n/a	-0.139	n/a	20	6	14.06	1	1	1.433
+120.0	3	parametric gain	n/a	-0.139	n/a	20	18	2.18	3	0	1.215
+126.0	3	parametric gain	n/a	0.361	n/a	40	7	33.07	1	1	0.8240000000000001
+130.0	3	parametric gain	n/a	0.261	n/a	36	6	30.06	1	1	0.903
+134.0	3	parametric gain	n/a	-0.239	n/a	16	20	-3.8	4	0	1.047
+140.0	3	parametric gain	n/a	0.361	n/a	40	15	25.15	1	1	1.169
+148.0	3	parametric gain	n/a	-0.389	n/a	10	6	4.06	3	0	1.137
+156.0	3	parametric gain	n/a	-0.339	n/a	12	15	-2.85	4	0	1.082
+160.0	3	parametric gain	n/a	0.211	n/a	34	17	17.17	2	1	1.0959999999999999
+164.0	3	parametric gain	n/a	-0.189	n/a	18	14	4.14	2	1	2.2
+170.0	3	parametric gain	n/a	0.361	n/a	40	13	27.13	1	1	0.7759999999999999
+174.0	3	parametric gain	n/a	0.161	n/a	32	19	13.19	2	1	1.3359999999999999
+178.0	3	parametric gain	n/a	-0.289	n/a	14	12	2.12	3	0	1.585
+182.0	3	parametric gain	n/a	0.161	n/a	32	10	22.1	2	1	1.3619999999999999
+192.0	3	parametric gain	n/a	0.261	n/a	36	17	19.17	2	1	1.786
+202.0	3	parametric gain	n/a	0.061	n/a	28	13	15.13	2	1	1.107
+208.0	3	parametric gain	n/a	0.211	n/a	34	13	21.13	1	1	1.2429999999999999
+212.0	3	parametric gain	n/a	0.311	n/a	38	14	24.14	1	1	1.112
+220.0	3	parametric gain	n/a	0.161	n/a	32	11	21.11	1	1	1.235
+224.0	3	parametric gain	n/a	0.161	n/a	32	9	23.09	1	1	0.866
+234.0	3	parametric gain	n/a	0.111	n/a	30	16	14.16	2	1	1.6269999999999998
+238.0	3	parametric gain	n/a	0.211	n/a	34	10	24.1	1	1	1.0
+246.0	3	parametric gain	n/a	-0.189	n/a	18	8	10.08	2	1	1.526
+250.0	3	parametric gain	n/a	0.211	n/a	34	20	14.2	2	1	1.251
+256.0	3	parametric gain	n/a	0.161	n/a	32	16	16.16	2	1	2.28
+260.0	3	parametric gain	n/a	-0.039	n/a	24	12	12.12	2	1	1.242
+266.0	3	parametric gain	n/a	0.311	n/a	38	12	26.12	1	1	1.011
+272.0	3	parametric gain	n/a	0.311	n/a	38	10	28.1	1	1	1.043
+280.0	3	parametric gain	n/a	-0.039	n/a	24	7	17.07	1	1	1.172
+284.0	3	parametric gain	n/a	-0.239	n/a	16	13	3.13	2	1	1.115
+296.0	3	parametric gain	n/a	0.261	n/a	36	19	17.19	2	1	1.75
+310.0	3	parametric gain	n/a	0.161	n/a	32	5	27.05	1	1	0.9179999999999999
+316.0	3	parametric gain	n/a	0.211	n/a	34	9	25.09	1	1	0.7340000000000001
+320.0	3	parametric gain	n/a	0.061	n/a	28	7	21.07	1	1	1.067
+324.0	3	parametric gain	n/a	-0.039	n/a	24	13	11.13	2	1	1.6
+330.0	3	parametric gain	n/a	0.311	n/a	38	5	33.05	1	1	0.805
+336.0	3	parametric gain	n/a	-0.089	n/a	22	9	13.09	1	1	1.39
+342.0	3	parametric gain	n/a	-0.389	n/a	10	11	-0.89	4	0	1.806
+346.0	3	parametric gain	n/a	-0.339	n/a	12	14	-1.86	4	0	1.6
+350.0	3	parametric gain	n/a	0.211	n/a	34	19	15.19	2	1	1.237
+360.0	3	parametric gain	n/a	0.311	n/a	38	7	31.07	1	1	0.9570000000000001
+364.0	3	parametric gain	n/a	0.111	n/a	30	6	24.06	1	1	0.877
+368.0	3	parametric gain	n/a	0.011	n/a	26	18	8.18	2	1	1.454
+374.0	3	parametric gain	n/a	-0.289	n/a	14	6	8.06	2	1	1.9040000000000001
+378.0	3	parametric gain	n/a	-0.239	n/a	16	12	4.12	2	1	1.486
+382.0	3	parametric gain	n/a	-0.089	n/a	22	20	2.2	3	0	1.598
+386.0	3	parametric gain	n/a	-0.339	n/a	12	17	-4.83	4	0	1.368
+398.0	3	parametric gain	n/a	-0.389	n/a	10	7	3.07	2	1	1.32
+402.0	3	parametric gain	n/a	-0.189	n/a	18	20	-1.8	4	0	2.302
+408.0	3	parametric gain	n/a	-0.339	n/a	12	6	6.06	2	1	1.433
+412.0	3	parametric gain	n/a	0.161	n/a	32	6	26.06	1	1	0.9990000000000001
+416.0	3	parametric gain	n/a	-0.189	n/a	18	7	11.07	2	1	1.071
+420.0	3	parametric gain	n/a	0.361	n/a	40	10	30.1	1	1	0.7829999999999999
+424.0	3	parametric gain	n/a	-0.289	n/a	14	14	0.14	3	0	1.6869999999999998
+428.0	3	parametric gain	n/a	0.111	n/a	30	19	11.19	2	1	1.167
+436.0	3	parametric gain	n/a	-0.039	n/a	24	18	6.18	2	1	1.145
+440.0	3	parametric gain	n/a	-0.389	n/a	10	12	-1.88	3	0	1.383
+444.0	3	parametric gain	n/a	0.361	n/a	40	19	21.19	1	1	0.862
+448.0	3	parametric gain	n/a	-0.039	n/a	24	9	15.09	1	1	0.8340000000000001
+458.0	3	parametric gain	n/a	-0.239	n/a	16	18	-1.82	4	0	1.785
+462.0	3	parametric gain	n/a	0.261	n/a	36	12	24.12	2	1	1.28
+466.0	3	parametric gain	n/a	0.011	n/a	26	9	17.09	1	1	1.3940000000000001
+470.0	3	parametric gain	n/a	-0.139	n/a	20	12	8.12	2	1	1.249
+474.0	3	parametric gain	n/a	-0.039	n/a	24	14	10.14	2	1	1.266
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-02_bold.nii.gz b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-02_bold.nii.gz
new file mode 100644
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-02_events.tsv b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-02_events.tsv
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-02_events.tsv
@@ -0,0 +1,86 @@
+onset	duration	trial_type	distance from indifference	parametric gain	parametric loss	gain	loss	PTval	respnum	respcat	RT
+0.0	3	parametric gain	n/a	-0.1194	n/a	20	5	15.05	1	1	1.29
+4.0	3	parametric gain	n/a	-0.0694	n/a	22	17	5.17	2	1	1.163
+8.0	3	parametric gain	n/a	-0.3694	n/a	10	16	-5.84	4	0	1.265
+12.0	3	parametric gain	n/a	0.3306	n/a	38	18	20.18	2	1	1.4880000000000002
+16.0	3	parametric gain	n/a	-0.1194	n/a	20	14	6.14	2	1	1.446
+24.0	3	parametric gain	n/a	-0.2694	n/a	14	13	1.13	3	0	0.998
+28.0	3	parametric gain	n/a	0.3806	n/a	40	8	32.08	1	1	0.7809999999999999
+32.0	3	parametric gain	n/a	0.1806	n/a	32	18	14.18	2	1	1.189
+36.0	3	parametric gain	n/a	-0.0194	n/a	24	8	16.08	1	1	1.189
+40.0	3	parametric gain	n/a	0.2806	n/a	36	14	22.14	2	1	1.102
+44.0	3	parametric gain	n/a	0.0806	n/a	28	6	22.06	1	1	0.863
+48.0	3	parametric gain	n/a	-0.2194	n/a	16	14	2.14	2	1	1.9809999999999999
+56.0	3	parametric gain	n/a	-0.2694	n/a	14	9	5.09	2	1	1.031
+60.0	3	parametric gain	n/a	-0.1194	n/a	20	13	7.13	2	1	1.229
+64.0	3	parametric gain	n/a	0.1306	n/a	30	18	12.18	2	1	2.101
+72.0	3	parametric gain	n/a	-0.2694	n/a	14	15	-0.85	4	0	1.536
+76.0	3	parametric gain	n/a	-0.3194	n/a	12	12	0.12	3	0	1.7269999999999999
+84.0	3	parametric gain	n/a	-0.0194	n/a	24	11	13.11	2	1	1.255
+88.0	3	parametric gain	n/a	-0.3194	n/a	12	10	2.1	2	1	1.702
+94.0	3	parametric gain	n/a	-0.2194	n/a	16	6	10.06	2	1	1.161
+98.0	3	parametric gain	n/a	0.3306	n/a	38	20	18.2	2	1	1.463
+102.0	3	parametric gain	n/a	0.0806	n/a	28	10	18.1	2	1	1.744
+106.0	3	parametric gain	n/a	-0.1194	n/a	20	7	13.07	1	1	1.439
+110.0	3	parametric gain	n/a	0.3306	n/a	38	9	29.09	1	1	0.8059999999999999
+120.0	3	parametric gain	n/a	0.0306	n/a	26	12	14.12	2	1	1.041
+132.0	3	parametric gain	n/a	-0.3694	n/a	10	14	-3.86	4	0	1.121
+136.0	3	parametric gain	n/a	-0.3194	n/a	12	13	-0.87	4	0	1.621
+146.0	3	parametric gain	n/a	-0.0194	n/a	24	15	9.15	2	1	1.138
+150.0	3	parametric gain	n/a	0.1806	n/a	32	15	17.15	2	1	1.974
+154.0	3	parametric gain	n/a	0.2806	n/a	36	15	21.15	1	1	2.408
+162.0	3	parametric gain	n/a	0.2806	n/a	36	18	18.18	2	1	1.8980000000000001
+166.0	3	parametric gain	n/a	-0.3194	n/a	12	18	-5.82	4	0	1.222
+176.0	3	parametric gain	n/a	-0.1194	n/a	20	9	11.09	1	1	1.403
+180.0	3	parametric gain	n/a	-0.2194	n/a	16	8	8.08	2	1	1.328
+184.0	3	parametric gain	n/a	-0.3194	n/a	12	20	-7.8	4	0	1.25
+194.0	3	parametric gain	n/a	-0.0694	n/a	22	19	3.19	2	1	1.915
+200.0	3	parametric gain	n/a	0.2806	n/a	36	9	27.09	1	1	0.8340000000000001
+204.0	3	parametric gain	n/a	0.0306	n/a	26	6	20.06	1	1	1.169
+208.0	3	parametric gain	n/a	0.1306	n/a	30	5	25.05	1	1	0.826
+212.0	3	parametric gain	n/a	0.2806	n/a	36	7	29.07	1	1	0.835
+216.0	3	parametric gain	n/a	-0.1694	n/a	18	10	8.1	2	1	1.369
+220.0	3	parametric gain	n/a	0.1306	n/a	30	9	21.09	1	1	0.745
+226.0	3	parametric gain	n/a	0.1306	n/a	30	12	18.12	2	1	1.0190000000000001
+236.0	3	parametric gain	n/a	-0.2694	n/a	14	8	6.08	2	1	0.932
+240.0	3	parametric gain	n/a	-0.1194	n/a	20	19	1.19	2	1	0.887
+244.0	3	parametric gain	n/a	0.0806	n/a	28	14	14.14	2	1	0.986
+250.0	3	parametric gain	n/a	-0.2194	n/a	16	17	-0.83	4	0	2.204
+258.0	3	parametric gain	n/a	0.0306	n/a	26	8	18.08	2	1	1.084
+262.0	3	parametric gain	n/a	-0.3694	n/a	10	17	-6.83	4	0	1.088
+274.0	3	parametric gain	n/a	-0.3194	n/a	12	5	7.05	2	1	0.917
+282.0	3	parametric gain	n/a	0.3806	n/a	40	6	34.06	1	1	1.2209999999999999
+286.0	3	parametric gain	n/a	0.2806	n/a	36	13	23.13	2	1	1.496
+294.0	3	parametric gain	n/a	-0.3694	n/a	10	9	1.09	2	1	2.91
+298.0	3	parametric gain	n/a	0.2306	n/a	34	18	16.18	2	1	1.196
+304.0	3	parametric gain	n/a	0.1306	n/a	30	15	15.15	2	1	1.1340000000000001
+316.0	3	parametric gain	n/a	-0.1694	n/a	18	9	9.09	2	1	1.229
+322.0	3	parametric gain	n/a	0.0306	n/a	26	20	6.2	2	1	1.39
+326.0	3	parametric gain	n/a	0.2806	n/a	36	20	16.2	2	1	1.101
+330.0	3	parametric gain	n/a	-0.0694	n/a	22	15	7.15	2	1	1.3319999999999999
+334.0	3	parametric gain	n/a	0.3306	n/a	38	8	30.08	1	1	0.878
+342.0	3	parametric gain	n/a	0.0806	n/a	28	16	12.16	2	1	0.9259999999999999
+348.0	3	parametric gain	n/a	0.2306	n/a	34	12	22.12	2	1	1.775
+358.0	3	parametric gain	n/a	0.0806	n/a	28	15	13.15	2	1	1.183
+364.0	3	parametric gain	n/a	-0.0194	n/a	24	17	7.17	2	1	0.782
+368.0	3	parametric gain	n/a	0.0806	n/a	28	11	17.11	2	1	0.7979999999999999
+372.0	3	parametric gain	n/a	-0.2194	n/a	16	9	7.09	2	1	0.982
+378.0	3	parametric gain	n/a	-0.0694	n/a	22	13	9.13	2	1	1.008
+384.0	3	parametric gain	n/a	0.3806	n/a	40	11	29.11	1	1	1.088
+390.0	3	parametric gain	n/a	0.0806	n/a	28	8	20.08	1	1	2.935
+396.0	3	parametric gain	n/a	0.2806	n/a	36	16	20.16	1	1	1.744
+400.0	3	parametric gain	n/a	-0.1194	n/a	20	10	10.1	2	1	1.214
+404.0	3	parametric gain	n/a	-0.3694	n/a	10	18	-7.82	4	0	1.023
+408.0	3	parametric gain	n/a	0.3806	n/a	40	9	31.09	1	1	0.6970000000000001
+412.0	3	parametric gain	n/a	0.1806	n/a	32	7	25.07	1	1	1.3030000000000002
+422.0	3	parametric gain	n/a	0.3806	n/a	40	16	24.16	1	1	0.777
+426.0	3	parametric gain	n/a	0.1306	n/a	30	17	13.17	2	1	1.239
+432.0	3	parametric gain	n/a	-0.0194	n/a	24	5	19.05	1	1	1.073
+436.0	3	parametric gain	n/a	-0.2194	n/a	16	7	9.07	2	1	1.063
+440.0	3	parametric gain	n/a	-0.0694	n/a	22	18	4.18	2	1	1.5919999999999999
+444.0	3	parametric gain	n/a	0.0306	n/a	26	19	7.19	2	1	1.128
+448.0	3	parametric gain	n/a	-0.0194	n/a	24	20	4.2	2	1	1.258
+452.0	3	parametric gain	n/a	0.2306	n/a	34	6	28.06	1	1	1.794
+460.0	3	parametric gain	n/a	-0.0194	n/a	24	16	8.16	2	1	0.914
+468.0	3	parametric gain	n/a	-0.0694	n/a	22	6	16.06	1	1	1.849
+474.0	3	parametric gain	n/a	-0.3194	n/a	12	7	5.07	2	1	1.122
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-03_bold.nii.gz b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-03_bold.nii.gz
new file mode 100644
diff --git a/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-03_events.tsv b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-03_events.tsv
new file mode 100644
--- /dev/null
+++ b/bids/tests/data/ds005_conflict/sub-01/func/sub-01_task-mixedgamblestask_run-03_events.tsv
@@ -0,0 +1,86 @@
+onset	duration	trial_type	distance from indifference	parametric gain	parametric loss	gain	loss	PTval	respnum	respcat	RT
+0.0	3	parametric gain	n/a	0.0835	n/a	28	17	11.17	2	1	1.203
+4.0	3	parametric gain	n/a	-0.2665	n/a	14	7	7.07	2	1	1.123
+8.0	3	parametric gain	n/a	0.0835	n/a	28	18	10.18	2	1	0.9690000000000001
+14.0	3	parametric gain	n/a	-0.2165	n/a	16	11	5.11	2	1	1.102
+18.0	3	parametric gain	n/a	0.3835	n/a	40	20	20.2	1	1	0.9690000000000001
+22.0	3	parametric gain	n/a	-0.1665	n/a	18	16	2.16	2	1	1.261
+28.0	3	parametric gain	n/a	0.3335	n/a	38	11	27.11	2	1	1.35
+32.0	3	parametric gain	n/a	0.2835	n/a	36	10	26.1	1	1	2.157
+40.0	3	parametric gain	n/a	0.3835	n/a	40	18	22.18	1	1	1.6780000000000002
+44.0	3	parametric gain	n/a	-0.3665	n/a	10	10	0.1	2	1	1.581
+50.0	3	parametric gain	n/a	-0.0165	n/a	24	6	18.06	1	1	1.133
+56.0	3	parametric gain	n/a	-0.1165	n/a	20	8	12.08	2	1	2.2230000000000003
+62.0	3	parametric gain	n/a	0.3835	n/a	40	12	28.12	1	1	1.046
+68.0	3	parametric gain	n/a	-0.0665	n/a	22	10	12.1	2	1	1.5519999999999998
+76.0	3	parametric gain	n/a	-0.1665	n/a	18	6	12.06	2	1	1.2790000000000001
+80.0	3	parametric gain	n/a	0.3335	n/a	38	16	22.16	2	1	2.4530000000000003
+90.0	3	parametric gain	n/a	0.1335	n/a	30	20	10.2	2	1	1.328
+98.0	3	parametric gain	n/a	-0.0665	n/a	22	14	8.14	2	1	1.008
+102.0	3	parametric gain	n/a	-0.1665	n/a	18	18	0.18	2	1	2.222
+106.0	3	parametric gain	n/a	-0.2165	n/a	16	19	-2.81	4	0	1.767
+110.0	3	parametric gain	n/a	-0.0165	n/a	24	10	14.1	2	1	1.024
+116.0	3	parametric gain	n/a	-0.3165	n/a	12	19	-6.81	4	0	1.176
+120.0	3	parametric gain	n/a	0.0835	n/a	28	9	19.09	1	1	2.383
+126.0	3	parametric gain	n/a	0.1335	n/a	30	10	20.1	1	1	1.936
+130.0	3	parametric gain	n/a	-0.1665	n/a	18	15	3.15	2	1	1.551
+142.0	3	parametric gain	n/a	0.1835	n/a	32	14	18.14	2	1	1.4169999999999998
+146.0	3	parametric gain	n/a	0.2335	n/a	34	11	23.11	1	1	1.52
+152.0	3	parametric gain	n/a	0.3335	n/a	38	19	19.19	1	1	2.488
+156.0	3	parametric gain	n/a	-0.2665	n/a	14	10	4.1	2	1	1.202
+160.0	3	parametric gain	n/a	0.2335	n/a	34	8	26.08	1	1	0.888
+164.0	3	parametric gain	n/a	0.1335	n/a	30	11	19.11	1	1	1.546
+168.0	3	parametric gain	n/a	0.1835	n/a	32	13	19.13	2	1	2.403
+174.0	3	parametric gain	n/a	-0.1665	n/a	18	11	7.11	2	1	1.176
+178.0	3	parametric gain	n/a	0.1335	n/a	30	7	23.07	1	1	1.3519999999999999
+186.0	3	parametric gain	n/a	-0.1665	n/a	18	13	5.13	2	1	0.8340000000000001
+190.0	3	parametric gain	n/a	-0.2665	n/a	14	19	-4.81	4	0	0.9690000000000001
+198.0	3	parametric gain	n/a	-0.3165	n/a	12	11	1.11	2	1	1.858
+202.0	3	parametric gain	n/a	-0.3665	n/a	10	13	-2.87	4	0	1.403
+206.0	3	parametric gain	n/a	-0.0665	n/a	22	16	6.16	2	1	1.136
+212.0	3	parametric gain	n/a	-0.3165	n/a	12	16	-3.84	4	0	0.971
+218.0	3	parametric gain	n/a	0.3335	n/a	38	17	21.17	2	1	1.811
+226.0	3	parametric gain	n/a	0.3835	n/a	40	14	26.14	1	1	1.651
+230.0	3	parametric gain	n/a	-0.0665	n/a	22	5	17.05	1	1	0.914
+234.0	3	parametric gain	n/a	-0.2165	n/a	16	16	0.16	2	1	2.441
+240.0	3	parametric gain	n/a	0.2335	n/a	34	5	29.05	1	1	0.866
+250.0	3	parametric gain	n/a	-0.3665	n/a	10	19	-8.81	4	0	0.836
+254.0	3	parametric gain	n/a	-0.2665	n/a	14	16	-1.84	4	0	1.9069999999999998
+258.0	3	parametric gain	n/a	0.1335	n/a	30	8	22.08	1	1	0.9690000000000001
+262.0	3	parametric gain	n/a	0.2335	n/a	34	14	20.14	2	1	2.204
+266.0	3	parametric gain	n/a	0.1335	n/a	30	14	16.14	2	1	1.97
+274.0	3	parametric gain	n/a	0.0335	n/a	26	15	11.15	2	1	1.068
+282.0	3	parametric gain	n/a	-0.3665	n/a	10	20	-9.8	4	0	1.077
+286.0	3	parametric gain	n/a	0.2335	n/a	34	7	27.07	1	1	1.051
+292.0	3	parametric gain	n/a	-0.3665	n/a	10	5	5.05	2	1	1.445
+296.0	3	parametric gain	n/a	-0.1165	n/a	20	20	0.2	2	1	1.67
+300.0	3	parametric gain	n/a	0.0835	n/a	28	20	8.2	2	1	0.956
+304.0	3	parametric gain	n/a	0.2835	n/a	36	5	31.05	1	1	1.11
+308.0	3	parametric gain	n/a	0.0335	n/a	26	7	19.07	1	1	2.516
+316.0	3	parametric gain	n/a	0.0335	n/a	26	17	9.17	2	1	1.254
+326.0	3	parametric gain	n/a	-0.2165	n/a	16	15	1.15	2	1	1.51
+330.0	3	parametric gain	n/a	0.0335	n/a	26	10	16.1	2	1	0.757
+338.0	3	parametric gain	n/a	0.3335	n/a	38	15	23.15	1	1	1.963
+346.0	3	parametric gain	n/a	0.0335	n/a	26	11	15.11	2	1	0.9179999999999999
+356.0	3	parametric gain	n/a	0.0835	n/a	28	5	23.05	1	1	0.855
+362.0	3	parametric gain	n/a	-0.2665	n/a	14	18	-3.82	4	0	1.391
+368.0	3	parametric gain	n/a	-0.0665	n/a	22	12	10.12	2	1	0.966
+372.0	3	parametric gain	n/a	0.2335	n/a	34	15	19.15	1	1	2.406
+376.0	3	parametric gain	n/a	0.3335	n/a	38	13	25.13	1	1	0.87
+380.0	3	parametric gain	n/a	-0.3665	n/a	10	8	2.08	2	1	1.271
+394.0	3	parametric gain	n/a	-0.3165	n/a	12	9	3.09	2	1	1.15
+398.0	3	parametric gain	n/a	0.2835	n/a	36	11	25.11	1	1	1.564
+404.0	3	parametric gain	n/a	-0.2665	n/a	14	5	9.05	2	1	1.695
+408.0	3	parametric gain	n/a	-0.0665	n/a	22	11	11.11	2	1	1.11
+412.0	3	parametric gain	n/a	-0.1665	n/a	18	19	-0.81	4	0	1.286
+418.0	3	parametric gain	n/a	-0.0665	n/a	22	8	14.08	2	1	1.7369999999999999
+428.0	3	parametric gain	n/a	0.3335	n/a	38	6	32.06	1	1	1.063
+434.0	3	parametric gain	n/a	-0.3165	n/a	12	8	4.08	2	1	0.887
+438.0	3	parametric gain	n/a	-0.2665	n/a	14	11	3.11	2	1	1.247
+442.0	3	parametric gain	n/a	0.1835	n/a	32	12	20.12	2	1	1.545
+446.0	3	parametric gain	n/a	-0.1165	n/a	20	11	9.11	2	1	0.8490000000000001
+450.0	3	parametric gain	n/a	0.0335	n/a	26	14	12.14	2	1	0.9440000000000001
+454.0	3	parametric gain	n/a	0.1835	n/a	32	8	24.08	1	1	0.825
+466.0	3	parametric gain	n/a	0.3835	n/a	40	17	23.17	1	1	1.963
+472.0	3	parametric gain	n/a	-0.1165	n/a	20	17	3.17	2	1	1.161
+476.0	3	parametric gain	n/a	-0.0165	n/a	24	19	5.19	2	1	0.888