def electrical_amplitudes(durations, polarity, ratio=None):
    """Calculate amplitudes of each "phase" in a signle electrical pulse
    in a cochlear implant.

    The resulting pulses are charged ballanced (if possible).  The
    function supports mono-, bi- and tri-phasic pulses.

    Parameters
    ----------
    durations : array_like
        List of phase durations in the pulse.
    polarity : {-1, 1, 'c' 'cathodic', 'a', 'anodic'}
        Polarity of the first phase.  -1, 'c' and 'cathodic' are
        equivalent.  As well as 1, 'a' and 'anodic'.
    ratio : float, optional
        Only valid for triphasic pulses.  It is equal to the charge
        ratio between the first and second phase.

    Returns
    -------
    tuple
        Amplitudes of phases corresponding to `durations`.

    """
    if len(durations) == 3:
        assert 0 <= ratio <= 1
    else:
        assert ratio is None
    if polarity in ('c', 'cathodic', -1):
        polarity = -1
    elif polarity in ('a', 'anodic', 1):
        polarity = 1
    else:
        raise RuntimeError('Unknown polarity')
    if len(durations) == 1:
        amplitude = polarity / durations[0]
        amplitudes = amplitude,
    elif len(durations) == 2:
        amplitudes = +1 * polarity * (0.5 / durations[0]), -1 * polarity * (
            0.5 / durations[1])
    elif len(durations) == 3:
        amplitudes = +1 * polarity * (0.5 * ratio / durations[0]
            ), -1 * polarity * (0.5 / durations[1]), +1 * polarity * (0.5 *
            (1 - ratio) / durations[2])
    else:
        raise RuntimeError('Unknown pulse shape')
    return amplitudes
