@@ -590,44 +590,38 @@ def _events_json(
590590 _write_json (fname , new_data , overwrite = overwrite )
591591
592592
593- def _readme (datatype , fname , overwrite = False ):
593+ def _readme (datatype , fname ):
594594 """Create a README file and save it.
595595
596- This will write a README file containing an MNE-BIDS citation.
597- If a README already exists, the behavior depends on the
598- ``overwrite`` parameter, as described below .
596+ If a README already exists, append an MNE-BIDS citation to it
597+ unless one is already present. Otherwise, create a new README
598+ containing an MNE-BIDS citation .
599599
600600 Parameters
601601 ----------
602602 datatype : string
603603 The type of data contained in the raw file ('meg', 'eeg', 'ieeg')
604604 fname : str | mne_bids.BIDSPath
605605 Filename to save the README to.
606- overwrite : bool
607- Whether to overwrite the existing file (defaults to False).
608- If overwrite is True, create a new README containing an
609- MNE-BIDS citation. If overwrite is False, append an
610- MNE-BIDS citation to the existing README, unless it
611- already contains that citation.
612606 """
613- if fname .is_file () and not overwrite :
614- with _open_lock (fname , encoding = "utf-8-sig" ) as fid :
615- orig_data = fid .read ()
616- mne_bids_ref = REFERENCES ["mne-bids" ] in orig_data
617- datatype_ref = REFERENCES [datatype ] in orig_data
607+ # Hold the lock across read and write so concurrent writers cannot
608+ # observe a partially written file.
609+ with _open_lock (fname ):
610+ if fname .is_file ():
611+ text = fname .read_text ("utf-8-sig" )
612+ else :
613+ text = ""
614+ mne_bids_ref = REFERENCES ["mne-bids" ] in text
615+ datatype_ref = REFERENCES [datatype ] in text
618616 if mne_bids_ref and datatype_ref :
619617 return
620- text = "{}References\n ----------\n {}{}" .format (
621- orig_data + "\n \n " ,
622- "" if mne_bids_ref else REFERENCES ["mne-bids" ] + "\n \n " ,
623- "" if datatype_ref else REFERENCES [datatype ] + "\n " ,
624- )
625- else :
626- text = "References\n ----------\n {}{}" .format (
627- REFERENCES ["mne-bids" ] + "\n \n " , REFERENCES [datatype ] + "\n "
628- )
629-
630- _write_text (fname , text , overwrite = True )
618+ text += "\n \n " if text else ""
619+ text += "References\n ----------\n "
620+ if not mne_bids_ref :
621+ text += REFERENCES ["mne-bids" ] + "\n \n "
622+ if not datatype_ref :
623+ text += REFERENCES [datatype ] + "\n "
624+ _write_text (fname , text , overwrite = True , lock = False )
631625
632626
633627def _participants_tsv (raw , subject_id , fname , overwrite = False ):
@@ -1707,13 +1701,14 @@ def make_dataset_description(
17071701
17081702 # Handle potentially existing file contents
17091703 with _open_lock (fname ):
1704+ orig_cols = {}
17101705 if op .isfile (fname ):
17111706 try :
17121707 with open (fname , encoding = "utf-8-sig" ) as fin :
17131708 orig_cols = json .load (fin )
17141709 except (json .JSONDecodeError , OSError ):
17151710 # File is empty, corrupted, or being written to by another process
1716- orig_cols = {}
1711+ pass
17171712 if "BIDSVersion" in orig_cols and orig_cols ["BIDSVersion" ] != BIDS_VERSION :
17181713 warnings .warn (
17191714 "Conflicting BIDSVersion found in dataset_description.json! "
@@ -1738,6 +1733,11 @@ def make_dataset_description(
17381733 pop_keys = [key for key , val in description .items () if val is None ]
17391734 for key in pop_keys :
17401735 description .pop (key )
1736+
1737+ # Preserve BIDS-spec keys we do not model (e.g. Description, DatasetLinks).
1738+ for key , val in orig_cols .items ():
1739+ description .setdefault (key , val )
1740+
17411741 _write_json (fname , description , overwrite = True , lock = False )
17421742
17431743
@@ -1761,6 +1761,7 @@ def write_raw_bids(
17611761 electrodes_tsv_task = False ,
17621762 emg_placement = None ,
17631763 overwrite = False ,
1764+ readme = True ,
17641765 verbose = None ,
17651766):
17661767 """Save raw data to a BIDS-compliant folder structure.
@@ -1961,6 +1962,11 @@ def write_raw_bids(
19611962 and ``participants.tsv`` by a user will be retained.
19621963 If ``False``, no existing data will be overwritten or
19631964 replaced.
1965+ readme : bool
1966+ If ``False``, leave any existing ``README`` untouched and do
1967+ not create one. Defaults to ``True``.
1968+
1969+ .. versionadded:: 0.19
19641970 %(verbose)s
19651971
19661972 Returns
@@ -2345,7 +2351,8 @@ def write_raw_bids(
23452351 # save readme file unless it already exists
23462352 # XXX: can include README overwrite in future if using a template API
23472353 # XXX: see https://github.com/mne-tools/mne-bids/issues/551
2348- _readme (bids_path .datatype , readme_fname , False )
2354+ if readme :
2355+ _readme (bids_path .datatype , readme_fname )
23492356
23502357 # save all participants meta data
23512358 _participants_tsv (
0 commit comments