'Equalize the number of trials in each condition.
It tries to make the remaining epochs occurring as close as possible in
time. This method works based on the idea that if there happened to be
some time-varying (like on the scale of minutes) noise characteristics
during a recording, they could be compensated for (to some extent) in
the equalization process. This method thus seeks to reduce any of
those effects by minimizing the differences in the times of the events
in the two sets of epochs. For example, if one had event times
[1, 2, 3, 4, 120, 121] and the other one had [3.5, 4.5, 120.5, 121.5],
it would remove events at times [1, 2] in the first epochs and not
[20, 21].
Parameters
event_ids : list
The event types to equalize. Each entry in the list can either be
a str (single event) or a list of str. In the case where one of
the entries is a list of str, event_ids in that list will be
grouped together before equalizing trial counts across conditions.
In the case where partial matching is used (using \'/\' in
`event_ids`), `event_ids` will be matched according to the
provided tags, that is, processing works as if the event_ids
matched by the provided tags had been supplied instead.
The event_ids must identify nonoverlapping subsets of the epochs.
method : str
If \'truncate\', events will be truncated from the end of each event
list. If \'mintime\', timing differences between each event list
will be minimized.
Returns
epochs : instance of Epochs
The modified Epochs instance.
indices : array of int
Indices from the original events list that were dropped.
Notes
For example (if epochs.event_id was {\'Left\': 1, \'Right\': 2,
\'Nonspatial\':3}:
epochs.equalize_event_counts([[\'Left\', \'Right\'], \'Nonspatial\'])
would equalize the number of trials in the \'Nonspatial\' condition with
the total number of trials in the \'Left\' and \'Right\' conditions.
If multiple indices are provided (e.g. \'Left\' and \'Right\' in the
example above), it is not guaranteed that after equalization, the
conditions will contribute evenly. E.g., it is possible to end up
with 70 \'Nonspatial\' trials, 69 \'Left\' and 1 \'Right\'.'

def equalize_event_counts(self, event_ids, method='mintime'):

    if (len(event_ids) == 0):
        raise ValueError('event_ids    must    have    at    least    one    element')
    if (not self._bad_dropped):
        self.drop_bad()
    eq_inds = list()
    ids = self.event_id
    orig_ids = list(event_ids)
    tagging = False
    if ('/' in ''.join(ids)):
        event_ids = [([x] if isinstance(x, string_types) else x) for x in event_ids]
        for ids_ in event_ids:
            if any([(id_ not in ids) for id_ in ids_]):
                tagging = True
        event_ids = [([k for k in ids if all(((tag in k.split('/')) for tag in id_))] if all(((id__ not in ids) for id__ in id_)) else id_) for id_ in event_ids]
        for (ii, id_) in enumerate(event_ids):
            if (len(id_) == 0):
                raise KeyError((orig_ids[ii] + "not    found    in    the    epoch    object's    event_id."))
            elif (len(set([(sub_id in ids) for sub_id in id_])) != 1):
                err = ("Don't    mix    hierarchical    and    regular    event_ids    like    in    '%s'." % ',    '.join(id_))
                raise ValueError(err)
        if (tagging is True):
            events_ = [set(self[x].events[:, 0]) for x in event_ids]
            doubles = events_[0].intersection(events_[1])
            if len(doubles):
                raise ValueError('The    two    sets    of    epochs    are    overlapping.    Provide    an    orthogonal    selection.')
    for eq in event_ids:
        eq_inds.append(np.where(self._keys_to_idx(eq))[0])
    event_times = [self.events[(e, 0)] for e in eq_inds]
    indices = _get_drop_indices(event_times, method)
    indices = np.concatenate([e[idx] for (e, idx) in zip(eq_inds, indices)])
    self.drop(indices, reason='EQUALIZED_COUNT')
    return (self, indices)
