예제 #1
0
def plot_1D_so(som, pix_id, **kwargs):
    """
    This function allows one to plot the data in a C{SOM.SO}. This happens by
    passing the C{SOM.SOM} and a pixel ID. The pixel ID is searched in the
    C{SOM.SOM} and the returned C{SOM.SO} is plotted. The C{SOM.SOM} is passed
    since it contains the metadata information. NOTE: This function only
    creates the plot. Viewing the actual plot requires invoking
    I{pylab.show()}.

    @param som: The object containing the data to plot.
    @type som: C{SOM.SOM}

    @param pix_id: The pixel ID for the C{SOM.SO} to plot.
    @type pix_id: various
    """
    # Retrieve the SO
    so = som.getSO(pix_id)
    
    # Get the data type
    dataset_type = som.getDataSetType()

    # Get data arrays
    if dataset_type == "histogram":
        x = so.axis[0].val.toNumPy(True)
    else:
        x = so.axis[0].val.toNumPy()
    y = so.y.toNumPy()
    var_y = so.var_y.toNumPy()
    try:
        var_x = so.axis[0].var.toNumPy()
    except AttributeError:
        var_x = None

    # Set plot attributes
    try:
        xlabel = kwargs["xlabel"]
        del kwargs["xlabel"]
    except KeyError:
        xlabel = som.getAxisLabel(0) + " [" + som.getAxisUnits(0) + "]"

    try:
        ylabel = kwargs["ylabel"]
        del kwargs["ylabel"]
    except KeyError:
        ylabel = som.getYLabel() + " [" + som.getYUnits() + "]"
        
    try:
        title = kwargs["title"]
        del kwargs["title"]
    except KeyError :       
        title = str(pix_id)

    # Make 1D plot
    drplot.plot_1D_arr(x, y, var_y, var_x, xlabel=xlabel, ylabel=ylabel,
                       title=title, **kwargs)
예제 #2
0
def plot_1D_slice(som, axis, xslice, yslice, **kwargs):
    """
    This function plots a 1D slice from a 2D spectrum. The function requires
    the axis to project onto and a set of slice ranges. NOTE: This
    function only creates the plot. Viewing the actual plot requires invoking
    I{pylab.show()}.

    @param som: The object containing the data to plot.
    @type som: C{SOM.SOM}

    @param axis: The particular axis to clean. This is either I{x} or I{y}.
    @type axis: C{string}

    @param xslice: A set of axis values that determine the x-axis values to
                   get the slice over in the format of (min, max). Passing
                   I{None} for either min or max gets the first or last bin
                   respectively.
    @type xslice: C{tuple} of two numbers

    @param yslice: A set of axis values that determine the y-axis values to
                   get the slice over in the format of (min, max). Passing
                   I{None} for either min or max gets the first or last bin
                   respectively.
    @type yslice: C{tuple} of two numbers

    @param kwargs: A list of keyword arguments that the function accepts. The
                   function also takes keywords for L{drplot.plot_1D_arr}.
    
    @keyword index: A flag that tells the function that the slice ranges are
                    indicies and not axis values. The default is I{False}.
    @type index: C{boolean}


    @raise RuntimeError: The axis value is not recognized
    """
    # Lookup all the keywords
    try:
        index = kwargs["index"]
    except KeyError:
        index = False

    try:
        xlabel = kwargs["xlabel"]
        del kwargs["xlabel"]
    except KeyError:
        xlabel = None

    [(x, y, z, var_z)] = som.toXY(withYvar=True)

    # Get dimensions of data
    Nx = x.size
    Ny = y.size

    # z values are filtered since plotting has trouble with NaNs. The
    # I{nan_to_num} function zeros NaNs and sets (-)Inf to the largest
    # (negative) positive value.
    z = numpy.reshape(numpy.nan_to_num(z), (Nx, Ny))
    var_z = numpy.reshape(numpy.nan_to_num(var_z), (Nx, Ny))

    # Find the x and y slice ranges in terms of array indicies
    if index:
        sx = __get_slice(xslice, Nx)
        sy = __get_slice(yslice, Ny)
    else:
        x_min = __find_index(x, xslice[0])
        x_max = __find_index(x, xslice[1])
        y_min = __find_index(y, yslice[0])
        y_max = __find_index(y, yslice[1])

        sx = __get_slice((x_min, x_max), Nx)
        sy = __get_slice((y_min, y_max), Ny)

    # Setup axis specific values
    if axis == "y":
        naxis = 0
        if xlabel is None:
            xlabel = som.getAxisLabel(1) + " [" + som.getAxisUnits(1) + "]"
        xp = y[sy]
    elif axis == "x":
        naxis = 1
        if xlabel is None:
            xlabel = som.getAxisLabel(0) + " [" + som.getAxisUnits(0) + "]"
        xp = x[sx]
    else:
        raise RuntimeError("Only understand x or y for axis and not: %s" \
                           % axis)

    yp = z[sx, sy].sum(axis=naxis)
    var_yp = var_z[sx, sy].sum(axis=naxis)

    drplot.plot_1D_arr(xp, yp, var_yp, xlabel=xlabel, **kwargs)
예제 #3
0
def plot_1D_slice(som, axis, xslice, yslice, **kwargs):
    """
    This function plots a 1D slice from a 2D spectrum. The function requires
    the axis to project onto and a set of slice ranges. NOTE: This
    function only creates the plot. Viewing the actual plot requires invoking
    I{pylab.show()}.

    @param som: The object containing the data to plot.
    @type som: C{SOM.SOM}

    @param axis: The particular axis to clean. This is either I{x} or I{y}.
    @type axis: C{string}

    @param xslice: A set of axis values that determine the x-axis values to
                   get the slice over in the format of (min, max). Passing
                   I{None} for either min or max gets the first or last bin
                   respectively.
    @type xslice: C{tuple} of two numbers

    @param yslice: A set of axis values that determine the y-axis values to
                   get the slice over in the format of (min, max). Passing
                   I{None} for either min or max gets the first or last bin
                   respectively.
    @type yslice: C{tuple} of two numbers

    @param kwargs: A list of keyword arguments that the function accepts. The
                   function also takes keywords for L{drplot.plot_1D_arr}.
    
    @keyword index: A flag that tells the function that the slice ranges are
                    indicies and not axis values. The default is I{False}.
    @type index: C{boolean}


    @raise RuntimeError: The axis value is not recognized
    """
    # Lookup all the keywords
    try:
        index = kwargs["index"]
    except KeyError:
        index = False

    try:
        xlabel = kwargs["xlabel"]
        del kwargs["xlabel"]
    except KeyError:
        xlabel = None

    [(x, y, z, var_z)] = som.toXY(withYvar=True)

    # Get dimensions of data
    Nx = x.size
    Ny = y.size

    # z values are filtered since plotting has trouble with NaNs. The
    # I{nan_to_num} function zeros NaNs and sets (-)Inf to the largest
    # (negative) positive value.
    z = numpy.reshape(numpy.nan_to_num(z), (Nx, Ny))
    var_z = numpy.reshape(numpy.nan_to_num(var_z), (Nx, Ny))

    # Find the x and y slice ranges in terms of array indicies
    if index:
        sx = __get_slice(xslice, Nx)
        sy = __get_slice(yslice, Ny)
    else:
        x_min = __find_index(x, xslice[0])
        x_max = __find_index(x, xslice[1])
        y_min = __find_index(y, yslice[0])
        y_max = __find_index(y, yslice[1])        
            
        sx = __get_slice((x_min, x_max), Nx)
        sy = __get_slice((y_min, y_max), Ny)

    # Setup axis specific values
    if axis == "y":
        naxis = 0
        if xlabel is None:
            xlabel = som.getAxisLabel(1) + " [" + som.getAxisUnits(1) + "]"
        xp = y[sy]
    elif axis == "x":
        naxis = 1
        if xlabel is None:
            xlabel = som.getAxisLabel(0) + " [" + som.getAxisUnits(0) + "]"
        xp = x[sx]
    else:
        raise RuntimeError("Only understand x or y for axis and not: %s" \
                           % axis)

    yp = z[sx, sy].sum(axis=naxis)
    var_yp = var_z[sx, sy].sum(axis=naxis)

    drplot.plot_1D_arr(xp, yp, var_yp, xlabel=xlabel, **kwargs)
예제 #4
0
def plot_1D_so(som, pix_id, **kwargs):
    """
    This function allows one to plot the data in a C{SOM.SO}. This happens by
    passing the C{SOM.SOM} and a pixel ID. The pixel ID is searched in the
    C{SOM.SOM} and the returned C{SOM.SO} is plotted. The C{SOM.SOM} is passed
    since it contains the metadata information. NOTE: This function only
    creates the plot. Viewing the actual plot requires invoking
    I{pylab.show()}.

    @param som: The object containing the data to plot.
    @type som: C{SOM.SOM}

    @param pix_id: The pixel ID for the C{SOM.SO} to plot.
    @type pix_id: various
    """
    # Retrieve the SO
    so = som.getSO(pix_id)

    # Get the data type
    dataset_type = som.getDataSetType()

    # Get data arrays
    if dataset_type == "histogram":
        x = so.axis[0].val.toNumPy(True)
    else:
        x = so.axis[0].val.toNumPy()
    y = so.y.toNumPy()
    var_y = so.var_y.toNumPy()
    try:
        var_x = so.axis[0].var.toNumPy()
    except AttributeError:
        var_x = None

    # Set plot attributes
    try:
        xlabel = kwargs["xlabel"]
        del kwargs["xlabel"]
    except KeyError:
        xlabel = som.getAxisLabel(0) + " [" + som.getAxisUnits(0) + "]"

    try:
        ylabel = kwargs["ylabel"]
        del kwargs["ylabel"]
    except KeyError:
        ylabel = som.getYLabel() + " [" + som.getYUnits() + "]"

    try:
        title = kwargs["title"]
        del kwargs["title"]
    except KeyError:
        title = str(pix_id)

    # Make 1D plot
    drplot.plot_1D_arr(x,
                       y,
                       var_y,
                       var_x,
                       xlabel=xlabel,
                       ylabel=ylabel,
                       title=title,
                       **kwargs)