import re


def map_axis(s,axes):
    """
    Return an axis identified mapped via an axes permutation list:
    i,j,k are input axes; I,J,K are output axes.

    This is used to check how a given axis (eg phase direction) has
    been changed in the image grid by flips and tranpositions.  It
    operates on voxel indices, not world co-ordinates.
    """

    ret_axes = ['I','J','K']

    # exact match: "j" or "-j"
    if s in axes:
        return ret_axes[axes.index(s)]

    # "j" = s, "-j" in axes
    elif ("-" + s) in axes:
        return "-" + ret_axes[axes.index("-" + s)]

    # "-j" = s, "j" in axes
    elif re.match(r'-.', s) and s[1:] in axes:
        return "-" + ret_axes[axes.index(s[1:])]

    else:
        return "F"