def change_data_orientation(data, old_orientation='RPI', orientation="RPI"):
    """
    This function changes the orientation of a data matrix from a give orientation to another.
    This function assumes that the user already knows the orientation of the data
    :param data: data of the image
    :param old_orientation: Current orientation of the data
    :param orientation: Desired orientation for the data
    :return: Data matrix representing the
    """
    opposite_character = {'L': 'R', 'R': 'L', 'A': 'P', 'P': 'A', 'I': 'S', 'S': 'I'}

    # change the orientation of the image
    perm = [0, 1, 2]
    inversion = [1, 1, 1]
    for i, character in enumerate(old_orientation):
        try:
            perm[i] = orientation.index(character)
        except __HOLE__:
            perm[i] = orientation.index(opposite_character[character])
            inversion[i] = -1

    # axes inversion
    data = data[::inversion[0], ::inversion[1], ::inversion[2]]

    # axes manipulations
    from numpy import swapaxes

    if perm == [1, 0, 2]:
        data = swapaxes(data, 0, 1)
    elif perm == [2, 1, 0]:
        data = swapaxes(data, 0, 2)
    elif perm == [0, 2, 1]:
        data = swapaxes(data, 1, 2)
    elif perm == [2, 1, 0]:
        data = swapaxes(data, 0, 2)
    elif perm == [2, 0, 1]:
        data = swapaxes(data, 0, 2)  # transform [2, 0, 1] to [1, 0, 2]
        data = swapaxes(data, 0, 1)  # transform [1, 0, 2] to [0, 1, 2]
    elif perm == [1, 2, 0]:
        data = swapaxes(data, 0, 2)  # transform [1, 2, 0] to [0, 2, 1]
        data = swapaxes(data, 1, 2)  # transform [0, 2, 1] to [0, 1, 2]
    elif perm == [0, 1, 2]:
        # do nothing
        pass
    else:
        print 'Error: wrong orientation'

    return data

# =======================================================================================================================
# Start program
#=======================================================================================================================
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/scripts/msct_image.py/change_data_orientation