Repository: mne-tools/mne-bids

Issue:
command raw_to_bids takes overwrite and line_freq as text  and fails for brainvision format
### Description of the problem

raw_to_bids passes the arguments --overwrite and --line_freq to as text causing type errors. The --overwrite error only occurs  when writing to brainvision format.


### Steps to reproduce

```Python
from mne.datasets import eegbci
from mne.io import read_raw_edf
import os.path as path
import subprocess

def main():
    subject=1
    run=2
    fname = eegbci.load_data(subject=subject,runs=run, path="data/external")[0]

    subprocess.run(["mne_bids", "--version"])
    # Example with type problem for line_freq option
    subprocess.run(["mne_bids", "raw_to_bids", 
                   "--subject_id","TEST001", 
                   "--task","passive", 
			      "--raw", fname,
   	      		  "--overwrite", "True",
			      "--line_freq", "50",  
			      "--bids_root", "data/fail"])

    my_raw = read_raw_edf(fname)
    root, _ = path.splitext(fname)
    fname_fif = root + ".fif"
    my_raw.save(fname_fif)
    # The problem for overwrite appears when starting with a fif formatted file
 
    subprocess.run(["mne_bids", "raw_to_bids", 
                   "--subject_id","TEST001", 
                   "--task","passive", 
	           "--raw", fname_fif,
   	           "--overwrite", "True",
	           "--bids_root", "data/fail"])


if __name__ == "__main__":
    main()O
```


### Expected results

```
Downloading file 'S001/S001R02.edf' from 'https://physionet.org/files/eegmmidb/1.0.0/S001/S001R02.edf' to '/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0'.
MNE-BIDS 0.12
Extracting EDF parameters from /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Extracting EDF parameters from /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Writing 'data/fail/README'...
Writing 'data/fail/participants.tsv'...
Writing 'data/fail/participants.json'...
The provided raw data contains annotations, but you did not pass an "event_id" mapping from annotation descriptions to event codes. We will generate arbitrary event codes. To specify custom event codes, please pass "event_id".
Used Annotations descriptions: ['T0']
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_events.tsv'...
Writing 'data/fail/dataset_description.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_eeg.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_channels.tsv'...
Copying data files to sub-TEST001_task-passive_eeg.edf
Writing 'data/fail/sub-TEST001/sub-TEST001_scans.tsv'...
Wrote data/fail/sub-TEST001/sub-TEST001_scans.tsv entry with eeg/sub-TEST001_task-passive_eeg.edf.
Extracting EDF parameters from /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
/home/apitsill/projects/bids/eeg/my_brain_technologies/failing_example.py:24: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  my_raw.save(fname_fif)
Writing /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif
Closing /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif
[done]
Opening raw data file /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  run()
Isotrak not found
    Range : 0 ... 9759 =      0.000 ...    60.994 secs
Ready.
Opening raw data file /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  run()
Isotrak not found
    Range : 0 ... 9759 =      0.000 ...    60.994 secs
Ready.
Writing 'data/fail/participants.tsv'...
Writing 'data/fail/participants.json'...
The provided raw data contains annotations, but you did not pass an "event_id" mapping from annotation descriptions to event codes. We will generate arbitrary event codes. To specify custom event codes, please pass "event_id".
Used Annotations descriptions: ['T0']
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_events.tsv'...
Writing 'data/fail/dataset_description.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_eeg.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_channels.tsv'...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: Converting data files to BrainVision format
  run()
Writing 'data/fail/sub-TEST001/sub-TEST001_scans.tsv'...
Wrote data/fail/sub-TEST001/sub-TEST001_scans.tsv entry with eeg/sub-TEST001_task-passive_eeg.vhdr.

```

### Actual results

```
Downloading file 'S001/S001R02.edf' from 'https://physionet.org/files/eegmmidb/1.0.0/S001/S001R02.edf' to '/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0'.
MNE-BIDS 0.12
Extracting EDF parameters from /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Traceback (most recent call last):
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py", line 92, in <module>
    run()
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py", line 84, in run
    raw.info['line_freq'] = line_freq
    ~~~~~~~~^^^^^^^^^^^^^
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne/io/meas_info.py", line 885, in __setitem__
    val = self._attributes[key](val)  # attribute checker function
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne/io/meas_info.py", line 351, in _check_line_freq
    _validate_type(line_freq, (None, 'numeric'), 'line_freq')
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne/utils/check.py", line 543, in _validate_type
    raise TypeError(
TypeError: line_freq must be an instance of None or numeric, got <class 'str'> instead.
Extracting EDF parameters from /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
/home/apitsill/projects/bids/eeg/my_brain_technologies/failing_example.py:24: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  my_raw.save(fname_fif)
Writing /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif
Closing /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif
[done]
Opening raw data file /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  run()
Isotrak not found
    Range : 0 ... 9759 =      0.000 ...    60.994 secs
Ready.
Opening raw data file /home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: This filename (/home/apitsill/projects/bids/eeg/my_brain_technologies/data/external/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R02.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz
  run()
Isotrak not found
    Range : 0 ... 9759 =      0.000 ...    60.994 secs
Ready.
Writing 'data/fail/README'...
Writing 'data/fail/participants.tsv'...
Writing 'data/fail/participants.json'...
The provided raw data contains annotations, but you did not pass an "event_id" mapping from annotation descriptions to event codes. We will generate arbitrary event codes. To specify custom event codes, please pass "event_id".
Used Annotations descriptions: ['T0']
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_events.tsv'...
Writing 'data/fail/dataset_description.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_eeg.json'...
Writing 'data/fail/sub-TEST001/eeg/sub-TEST001_task-passive_channels.tsv'...
/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py:92: RuntimeWarning: Converting data files to BrainVision format
  run()
Traceback (most recent call last):
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py", line 92, in <module>
    run()
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/commands/mne_bids_raw_to_bids.py", line 86, in run
    write_raw_bids(raw, bids_path, event_id=opt.event_id,
  File "<decorator-gen-620>", line 10, in write_raw_bids
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/write.py", line 1950, in write_raw_bids
    _write_raw_brainvision(raw, bids_path.fpath, events=events_array,
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/mne_bids/write.py", line 1064, in _write_raw_brainvision
    write_brainvision(data=raw.get_data(),
  File "/home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/lib/python3.11/site-packages/pybv/io.py", line 194, in write_brainvision
    raise ValueError("overwrite must be a boolean (True or False).")
ValueError: overwrite must be a boolean (True or False).

```

### Additional information
```
Platform:         Linux-6.1.0-5-amd64-x86_64-with-glibc2.36
Python:           3.11.2 (main, Feb 12 2023, 00:48:52) [GCC 12.2.0]
Executable:       /home/apitsill/.local/share/virtualenvs/my_brain_technologies-HhFDe8va/bin/python
CPU:              : 8 cores
Memory:           Unavailable (requires "psutil" package)
mne:              1.3.1
numpy:            1.24.2 {unknown linalg bindings (threadpoolctl module not found: No module named 'threadpoolctl')}
scipy:            1.10.1
matplotlib:       3.7.1 {backend=TkAgg}

sklearn:          Not found
numba:            Not found
nibabel:          Not found
nilearn:          Not found
dipy:             Not found
openmeeg:         Not found
cupy:             Not found
pandas:           1.5.3
pyvista:          Not found
pyvistaqt:        Not found
ipyvtklink:       Not found
vtk:              Not found
qtpy:             Not found
ipympl:           Not found
pyqtgraph:        Not found
pooch:            v1.7.0

mne_bids:         0.12
mne_nirs:         Not found
mne_features:     Not found
mne_qt_browser:   Not found
mne_connectivity: Not found
mne_icalabel:     Not found
```


Discussion:
Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️


Resolving patch:
diff --git a/mne_bids/commands/mne_bids_raw_to_bids.py b/mne_bids/commands/mne_bids_raw_to_bids.py
--- a/mne_bids/commands/mne_bids_raw_to_bids.py
+++ b/mne_bids/commands/mne_bids_raw_to_bids.py
@@ -8,6 +8,8 @@
 #          Stefan Appelhoff <stefan.appelhoff@mailbox.org>
 #
 # License: BSD-3-Clause
+from itertools import chain, repeat
+
 import mne_bids
 from mne_bids import write_raw_bids, BIDSPath
 from mne_bids.read import _read_raw
@@ -80,9 +82,16 @@ def run():
                     hsp=opt.hsp, config_path=opt.config,
                     allow_maxshield=allow_maxshield)
     if opt.line_freq is not None:
-        line_freq = None if opt.line_freq == "None" else opt.line_freq
+        line_freq = None if opt.line_freq == "None" else float(opt.line_freq)
         raw.info['line_freq'] = line_freq
 
+    if opt.overwrite is not None:
+        truthy = [1, "True", "true"]
+        falsy = [0, "False", "false"]
+        bool_mapping = dict(
+            chain(zip(truthy, repeat(True)), zip(falsy, repeat(False))))
+        opt.overwrite = bool_mapping[opt.overwrite]
+
     write_raw_bids(raw, bids_path, event_id=opt.event_id,
                    events=opt.events, overwrite=opt.overwrite,
                    events_data=opt.events_data, verbose=True)


Test patch:
diff --git a/mne_bids/commands/tests/test_cli.py b/mne_bids/commands/tests/test_cli.py
--- a/mne_bids/commands/tests/test_cli.py
+++ b/mne_bids/commands/tests/test_cli.py
@@ -67,7 +67,7 @@ def test_raw_to_bids(tmp_path):
     # Should work
     with ArgvSetter(('--subject_id', subject_id, '--task', task, '--raw',
                      raw_fname, '--bids_root', output_path,
-                     '--line_freq', 60)):
+                     '--line_freq', "60")):
         mne_bids_raw_to_bids.run()
 
     # Test line_freq == 'None'
@@ -81,7 +81,7 @@ def test_raw_to_bids(tmp_path):
     edf_fname = op.join(edf_data_path, 'test.edf')
     with ArgvSetter(('--subject_id', subject_id, '--task', task, '--raw',
                      edf_fname, '--bids_root', output_path,
-                     '--line_freq', 60)):
+                     '--overwrite', "false", '--line_freq', 60)):
         mne_bids_raw_to_bids.run()
 
     # Too few input args