def switchAndTranslate(gm1, gm2, v1, v2, wrapModulo):
    """
    Transforms [v1,v2] and returns it
    such that it is in the same order
    and has the same middle interval as [gm1, gm2]
    """
    assert(v1 < v2)
    # keep the same middle of the interval
    if (wrapModulo):
        gmMiddle = float(gm1 + gm2) / 2.0
        half = float(v2 - v1) / 2.0
        v1 = gmMiddle - half
        v2 = gmMiddle + half
    # if gm margins are increasing and dataset bounds are decreasing
    # or the other way around switch them
    if ((gm1 - gm2) * (v1 - v2) < 0):
        v1, v2 = v2, v1
    return [v1, v2]