Repository: mne-tools/mne-bids

Issue:
_read_events() changes event values
Currently I use following procedure to write a BIDS dataset (simplified): 
```
raw = mne.io.read_raw_fif(raw_file,preload=False)
events = mne.find_events(raw)
bids_basename = mne_bids.BIDSPath(root = output_path, subject=f'{sub:02d}',session=f'{ses:02d}',task=f'{task}')
mne-bids.write_raw_bids(raw, bids_basename,event_id = event_id[task], events_data = events,overwrite=True)
```
Doing so, the event values that are being written to `events.tsv`are not the ones that are defined in the meg file. Specifically, it seems that the original values are ignored and instead the number of unique codes is simply used incrementally. I started with following events: `1, 2, 4, 8, 16, 512, 2048` and ended up with `1, 2, 3, 4, 5, 6, 7`.
The events in the meg file are unchanged.

From looking at [read.py](https://github.com/mne-tools/mne-bids/blob/master/mne_bids/read.py), the problem seems to be that during conversion of events to annotations the original values get lost (see [here](https://github.com/mne-tools/mne-bids/blob/d65b8ca3edf38a4be5a1db8d24592cf2a1a3d5ef/mne_bids/read.py#L131)). So, there is a good chance that might be an issue with `mne` rather than `mne_bids`




Discussion:
Nice catch! I managed to reproduce and fix this locally. Will file a PR tomorrow.

Resolving patch:
diff --git a/mne_bids/read.py b/mne_bids/read.py
--- a/mne_bids/read.py
+++ b/mne_bids/read.py
@@ -144,6 +144,7 @@ def _read_events(events_data, event_id, raw, verbose=None):
     # Now convert the Annotations to events.
     all_events, all_desc = events_from_annotations(
         raw,
+        event_id=event_id,
         regexp=None,  # Include `BAD_` and `EDGE_` Annotations, too.
         verbose=verbose
     )


Test patch:
diff --git a/mne_bids/tests/test_write.py b/mne_bids/tests/test_write.py
--- a/mne_bids/tests/test_write.py
+++ b/mne_bids/tests/test_write.py
@@ -2154,6 +2154,36 @@ def test_undescribed_events(_bids_validate, drop_undescribed_events):
     _bids_validate(bids_root)
 
 
+@pytest.mark.filterwarnings(warning_str['channel_unit_changed'])
+def test_event_storage():
+    """Test we're retaining the original event IDs when storing events."""
+    bids_root = _TempDir()
+    bids_path = _bids_path.copy().update(root=bids_root, datatype='meg')
+    data_path = testing.data_path()
+    raw_fname = op.join(data_path, 'MEG', 'sample',
+                        'sample_audvis_trunc_raw.fif')
+    events_fname = op.join(data_path, 'MEG', 'sample',
+                           'sample_audvis_trunc_raw-eve.fif')
+    events_tsv_fname = (bids_path.copy()
+                        .update(suffix='events', extension='.tsv'))
+
+    events = mne.read_events(events_fname)
+    events = events[events[:, -1] != 0]  # Drop unused events
+    # Change an event ID
+    idx = np.where(events[:, -1] == 1)[0]
+    events[idx, -1] = 123
+
+    event_id = {'Auditory/Left': 123, 'Auditory/Right': 2, 'Visual/Left': 3,
+                'Visual/Right': 4, 'Smiley': 5, 'Button': 32}
+
+    raw = _read_raw_fif(raw_fname)
+    write_raw_bids(raw=raw, bids_path=bids_path, events_data=events,
+                   event_id=event_id, overwrite=False)
+
+    events_tsv = _from_tsv(events_tsv_fname)
+    assert set(int(e) for e in events_tsv['value']) == set(event_id.values())
+
+
 @pytest.mark.parametrize(
     'dir_name, fname, reader, datatype, coord_frame', [
         ('EDF', 'test_reduced.edf', _read_raw_edf, 'ieeg', 'mni_tal'),