Repository: mne-tools/mne-bids

Issue:
Write -> Read -> Write -> ... cycles lead do decimation of events
#### Describe the bug
Repeatedly writing and reading the same dataset decimates the events present in the data.

#### Steps to reproduce
```python
import pathlib
from tempfile import TemporaryDirectory
import mne
from mne_bids import make_bids_basename, read_raw_bids, write_raw_bids


mne.set_log_level(verbose=False)


def write_and_read_bids(raw, events, subjcect, bids_root):
    bids_basename = make_bids_basename(subject=subject)
    bids_fname = bids_basename + '_meg.fif'

    print(f'Number of events before writing: {events.shape[0]}')
    write_raw_bids(raw=raw,
                   bids_basename=bids_basename,
                   bids_root=bids_root,
                   events_data=events,
                   overwrite=True,
                   verbose=False)

    raw_read = read_raw_bids(bids_fname=bids_fname,
                             bids_root=bids_root,
                             verbose=False)
    print(f'Number of Annotations after reading: {len(raw_read.annotations)}')



subject = 'sample'
data_dir = pathlib.Path(mne.datasets.sample.data_path())
raw_fname = data_dir / 'MEG' / subject / 'sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(raw_fname)
raw_copy = raw.copy()  # Gonna use this for a small test later.
events = mne.find_events(raw)


with TemporaryDirectory() as bids_root:
    for pass_count in range(1, 5+1):
        print(f'\n==> Pass number {pass_count} …')
        write_and_read_bids(raw=raw, events=events, subjcect=subject,
                            bids_root=bids_root)


# Check that raw hasn't been modified in-place
raw.load_data()
raw_copy.load_data()
print(f'\n==> raw == raw_copy: {raw == raw_copy}')
```

#### Expected results
```
==> Pass number 1 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> Pass number 2 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> Pass number 3 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> Pass number 4 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> Pass number 5 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> raw == raw_copy: True
```

#### Actual results
```
==> Pass number 1 …
Number of events before writing: 320
Number of Annotations after reading: 320

==> Pass number 2 …
Number of events before writing: 320
Number of Annotations after reading: 260

==> Pass number 3 …
Number of events before writing: 320
Number of Annotations after reading: 194

==> Pass number 4 …
Number of events before writing: 320
Number of Annotations after reading: 130

==> Pass number 5 …
Number of events before writing: 320
Number of Annotations after reading: 65

==> raw == raw_copy: True
```

#### Additional information
`mne-bids:master`
`mne-python` 0.20.3


Discussion:

Resolving patch:
diff --git a/mne_bids/read.py b/mne_bids/read.py
--- a/mne_bids/read.py
+++ b/mne_bids/read.py
@@ -186,7 +186,6 @@ def _handle_events_reading(events_fname, raw):
                                         description=descriptions,
                                         orig_time=None)
     raw.set_annotations(annot_from_events)
-
     return raw
 
 
diff --git a/mne_bids/write.py b/mne_bids/write.py
--- a/mne_bids/write.py
+++ b/mne_bids/write.py
@@ -234,6 +234,7 @@ def _events_tsv(events, raw, fname, trial_type, overwrite=False,
     # Start by filling all data that we know into an ordered dictionary
     first_samp = raw.first_samp
     sfreq = raw.info['sfreq']
+    events = events.copy()
     events[:, 0] -= first_samp
 
     # Onset column needs to be specified in seconds


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
@@ -1159,3 +1159,22 @@ def test_write_anat_pathlike():
 
     # write_anat() should return a string.
     assert isinstance(anat_dir, str)
+
+
+def test_write_does_not_alter_events_inplace():
+    """Test that writing does not modify the passed events array."""
+    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')
+
+    raw = mne.io.read_raw_fif(raw_fname)
+    events = mne.read_events(events_fname)
+    events_orig = events.copy()
+
+    bids_root = _TempDir()
+    write_raw_bids(raw=raw, bids_basename=bids_basename, bids_root=bids_root,
+                   events_data=events, overwrite=True)
+
+    assert np.array_equal(events, events_orig)