Naming of BIDS '*events.tsv' files seems to disrupt fMRIprep 1.5.0rc1

Dear all,

I have been experimenting with using fMRIprep 1.5.0rc1. My BIDS-formatted data folder also contains task events files, such as ‘./sub-01/func/sub-01_task-Emotion_run-1_events.tsv’.

Strangely, when these files had a ‘run’ key in their filename, then fMRIprep 1.5.0rc1 (but not 1.4.1) exited with an error right after BIDS validation. But when I deleted the ‘run’ key (changing the file name to ‘sub-01_task-Emotion_x-1_events.tsv’), fMRIprep seemed to continue running fine.

I copied the error at the end of this message. I hope it is useful! Please let me know if you need any other information. My current workaround is simply to delete ‘*events.tsv’ files from a data set that I am running fMRIprep on. fMRIprep 1.5.0rc1 still seems very helpful though, as I seemed to have memory usage issues with 1.4.1 when I tried running it on a cluster.

Many thanks,
Andres

Research Assistant for cognitive fMRI projects at the University of Edinburgh

## Console output 
STARTING 67f6c8b8-9bfb-3a13-bc70-f14fa156a1ed

executing: fmriprep
Making sure the input data is BIDS compliant (warnings can be ignored in most cases).
1: [WARN] The recommended file /README is missing. See Section 03 (Modality agnostic files) of the BIDS specification. (code: 101 - README_FILE_MISSING)

Please visit https://neurostars.org/search?q=README_FILE_MISSING for existing conversations about this issue.

        Summary:                  Available Tasks:        Available Modalities:
        14 Files, 919.43MB        Emotion                 T1w
        1 - Subject                                       T2w
        1 - Session                                       bold
                                                          events
                                                          fieldmap

        If you have any questions, please post on https://neurostars.org/tags/bids.

Process Process-2:
Traceback (most recent call last):
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  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>
                                                              *

Hi @andres.tamm, it looks like a pybids issue - please follow up here https://github.com/bids-standard/pybids/issues/465

1 Like

I’ll reply on the pybids repo as well, but my initial guess is that you have another event file that’s conflicting with this one for some reason. Do you by chance have another *run-1_events.tsv file in a parent directory? Or multiple events.tsv in the current one?

1 Like

Thank you both for your quick reply! I do not have an additional events file, but I do have a .json file (*run-1_events.json) that describes custom columns in the *events.tsv file. If I remove the .json file, I no longer get the error message.

I wonder if that .json file was named correctly according to the BIDS standard? It was called ‘sub-01_task-Emotion_run-1_events.json’. When I submitted my data set to BIDS validator, I did not receive errors referring to my events files.

Let me know if there is anything else I can do to help clarify the error!

My data set folder had just one subject folder (‘sub-01’) and it contained the following files and folders:
./sub-01:
anat
fmap
func

./sub-01/anat:
sub-01_T1w.json
sub-01_T1w.nii
sub-01_T2w.json
sub-01_T2w.nii

./sub-01/fmap:
sub-01_magnitude1.json
sub-01_magnitude2.json
sub-01_phasediff.json
sub-01_magnitude1.nii
sub-01_magnitude2.nii
sub-01_phasediff.nii

./sub-01/func:
sub-01_task-Emotion_run-1_bold.json
sub-01_task-Emotion_run-1_bold.nii
sub-01_task-Emotion_run-1_events.json
sub-01_task-Emotion_run-1_events.tsv

Thank you again
Andres

Thanks Andres—I think this is a bug in PyBIDS. As far as I can see, what you’re doing is fine, and including a .json sidecar for events.tsv file is actually the recommended behavior.

Hi Tal, thanks, my .json file contained a description for a variable ‘run’ (see below). If I remove that block of text from the .json file, I no longer get the error that I had.

{
	"trial_type": {
		"LongName": "Trial type",
		"Description": "Primary categorisation of each trial",
		"Levels": {
			"cue_control": "Indicates cues in control block",
			"cue_emotion": "Indicates cues in emotion block",
			"cue_motivation": "Indicates cues in motivation block",
			"target_control": "Indicates targets in control block",
			"target_emotion": "Indicates targets in emotion block",
			"target_motivation": "Indicates targets in motvation block"
		}
	},
	"response_type": {
		"LongName": "Response type",
		"Description": "Participant's response to each target",
		"Levels": {
			"correct": "Indicates that response to target was correct",
			"incorrect": "Indicates that response to target was icorrect",
			"missing": "Indicates that response to target was missing"
		}
	},
	"response_time": {
		"LongName": "Response time",
		"Description": "Response time to targets measured in seconds",
		"Units": "seconds"
	},
	"run": {
		"LongName": "Run",
		"Description": "Indicates the scanning run"
	},
	"phase": {
		"LongName": "Task phase",
		"Description": "Indicates whether an event was a cue or a target",
		"Levels": {
			"cue": "Indicates cues",
			"target": "Indicates targets"
		}
	},
	"block": {
		"LongName": "Task block",
		"Description": "Indicates which task block the event belonged to",
		"Levels": {
			"control": "Indicates that event was in the control block",
			"emotion": "Indicates that event was in the emotion block",
			"motivation": "Indicates that event was in the motivation block"
		}
	}
}