Repository: mne-tools/mne-python

Issue:
Anonymized raw can't be saved: mne.io.anonymize_info() doesn't seem to work on the annotations
#### Describe the bug
When I instantiate a raw object, attach annotations, and I perform `mne.io.anonymize_info()` after I get an error. I believe this is due to the fact that anonymization doesn't "alter" the annotations. It works fine if I call anonymization before setting annotations.

Possible solutions:

1. better warning/error message
2. anonymization to alter the annotations - origtime as well

#### Steps to reproduce
When I run:

```
# set a measurement date to allow anonymization to run
        raw.set_meas_date(meas_date=datetime.datetime.now(tz=datetime.timezone.utc))
        mne.io.anonymize_info(raw.info)
        raw.save(tmpfpath)
```


#### Actual results
I get the following:

```
  File "/Users/adam2392/Documents/SPORF/mtsmorf/io/run_bids_conversion.py", line 56, in convert_mat_to_bids
    raw.save(tmpfpath)
  File "<decorator-gen-155>", line 21, in save
  File "/Users/adam2392/miniconda3/envs/rf/lib/python3.8/site-packages/mne/io/base.py", line 1345, in save
    _write_raw(fname, self, info, picks, fmt, data_type, reset_range,
  File "/Users/adam2392/miniconda3/envs/rf/lib/python3.8/site-packages/mne/io/base.py", line 1834, in _write_raw
    sk_onsets, sk_ends = _annotations_starts_stops(raw, 'bad_acq_skip')
  File "/Users/adam2392/miniconda3/envs/rf/lib/python3.8/site-packages/mne/annotations.py", line 521, in _annotations_starts_stops
    onsets = _sync_onset(raw, onsets)
  File "/Users/adam2392/miniconda3/envs/rf/lib/python3.8/site-packages/mne/annotations.py", line 495, in _sync_onset
    assert raw.info['meas_date'] == raw.annotations.orig_time
```


#### Additional information
Using v0.20



Discussion:
can you share a short code snippet to replicate the pb?

>

`anonymize_info` only anonymizes the `meas_info` object. If you want to
anonymize the `raw` (both the `meas_info` and `annotations`) you need to
call `raw.anonymize()`

On Tue, Apr 7, 2020 at 11:36 AM Alexandre Gramfort <notifications@github.com>
wrote:

> can you share a short code snippet to replicate the pb?
>
> >
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/mne-tools/mne-python/issues/7575#issuecomment-610459237>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABKTXHLORAJSJCUG43HNJLDRLNCBRANCNFSM4MDGAPGQ>
> .
>


Resolving patch:
diff --git a/mne/io/meas_info.py b/mne/io/meas_info.py
--- a/mne/io/meas_info.py
+++ b/mne/io/meas_info.py
@@ -2053,6 +2053,13 @@ def _add_timedelta_to_stamp(meas_date_stamp, delta_t):
 def anonymize_info(info, daysback=None, keep_his=False, verbose=None):
     """Anonymize measurement information in place.
 
+    .. warning:: If ``info`` is part of an object like
+                 :class:`raw.info <mne.io.Raw>`, you should directly use
+                 the method :meth:`raw.anonymize() <mne.io.Raw.anonymize>`
+                 to ensure that all parts of the data are anonymized and
+                 stay synchronized (e.g.,
+                 :class:`raw.annotations <mne.Annotations>`).
+
     Parameters
     ----------
     info : dict, instance of Info


Test patch: