Repository: mne-tools/mne-python

Issue:
ValueError: Channel XXX has no associated original unit when channel names trucated
### Describe the bug
When channels names in an EDF are long, they are truncated. 
When there are two channels that are truncated to the same name, then they get appended with 0, 1, 2 etc
Because of this appended number, the channel is no longer in the orig_units_trunc, and the Value error is thrown.

#### Steps and/or code to reproduce
```Python
mne.io.read_raw_edf('any edf file with long channel names which will be trucated to create non unique channel names.edf')
```

..\..\..\Anaconda3\lib\site-packages\mne\io\base.py:407: ValueError
```Python
            # Each channel in the data must have a corresponding channel in
            # the original units.
            ch_correspond = [ch in orig_units_trunc for ch in ch_names] #issue is here
            if not all(ch_correspond):
                ch_without_orig_unit = ch_names[ch_correspond.index(False)]
                raise ValueError('Channel {} has no associated original '
                                'unit.'.format(ch_without_orig_unit)) 
throws: ValueError: Channel EMG Ant Tibia-0 has no associated original unit.
```



Discussion:
hum... can you share a file so we can replicate?

>

Yup, here is a shortened version:

[MASS_SS1_subjectid1_001.zip](https://github.com/mne-tools/mne-python/files/3406993/MASS_SS1_subjectid1_001.zip)


Resolving patch:
diff --git a/mne/io/base.py b/mne/io/base.py
--- a/mne/io/base.py
+++ b/mne/io/base.py
@@ -357,6 +357,7 @@ def __init__(self, info, preload=False,
                 load_from_disk = True
         self._last_samps = np.array(last_samps)
         self._first_samps = np.array(first_samps)
+        orig_ch_names = info['ch_names']
         info._check_consistency()  # make sure subclass did a good job
         self.info = info
         self.buffer_size_sec = float(buffer_size_sec)
@@ -380,14 +381,21 @@ def __init__(self, info, preload=False,
         self._filenames = list(filenames)
         self.orig_format = orig_format
         # Sanity check and set original units, if provided by the reader:
+
         if orig_units:
             if not isinstance(orig_units, dict):
                 raise ValueError('orig_units must be of type dict, but got '
                                  ' {}'.format(type(orig_units)))
 
-            # original units need to be truncated to 15 chars, which is what
-            # the MNE IO procedure also does with the other channels
-            orig_units_trunc = [ch[:15] for ch in orig_units]
+            # original units need to be truncated to 15 chars or renamed
+            # to match MNE conventions (channel name unique and less than
+            # 15 characters).
+            orig_units = deepcopy(orig_units)
+            for old_ch, new_ch in zip(orig_ch_names, info['ch_names']):
+                if old_ch in orig_units:
+                    this_unit = orig_units[old_ch]
+                    del orig_units[old_ch]
+                    orig_units[new_ch] = this_unit
 
             # STI 014 channel is native only to fif ... for all other formats
             # this was artificially added by the IO procedure, so remove it
@@ -398,7 +406,7 @@ def __init__(self, info, preload=False,
 
             # Each channel in the data must have a corresponding channel in
             # the original units.
-            ch_correspond = [ch in orig_units_trunc for ch in ch_names]
+            ch_correspond = [ch in orig_units for ch in ch_names]
             if not all(ch_correspond):
                 ch_without_orig_unit = ch_names[ch_correspond.index(False)]
                 raise ValueError('Channel {} has no associated original '


Test patch: