Beispiel #1
0
def add_ncerr(left, right, **kwargs):
    """
    This function adds two objects (C{SOM}, C{SO} or C{tuple(val,val_err2)})
    and returns the result of the addition in an C{SOM}, C{SO} or C{tuple}. 

    @param left:  Object on the left of the addition sign
    @type left: C{SOM.SOM} or C{SOM.SO} or C{tuple}
    
    @param right: Object on the right of the addition sign
    @type right: C{SOM.SOM} or C{SOM.SO} or C{tuple}

    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword axis: This is the axis one wishes to manipulate. If no argument
                   is given the default value is y
    @type axis: C{string}=<y or x>
    
    @keyword axis_pos: This is position of the axis in the axis array. If no
                       argument is given, the default value is 0
    @type axis_pos: C{int}
    
    @keyword length_one_som: This is a flag that lets the function know it is
                             dealing with a length 1 C{SOM} so that attributes
                             may be passed along. The length 1 C{SOM} will be
                             turned into a C{SO}. The default value is False.
    @type length_one_som: C{boolean}
    
    @keyword length_one_som_pos: This is the argument position of the
                                 length 1 C{SOM} since the check is done
                                 before the arguments are swapped. The default
                                 value is 2.
    @type length_one_som_pos: C{int}=<1 or 2>
    
    @keyword add_nxpars: This is a flag that will turn on code to add
                         C{SOM.NxParameters} in the two C{SOM}'s attribute
                         lists.
    @type add_nxpars: C{boolean} 


    @return: Object containing the results of the addition
    @rtype: C{SOM.SOM}, C{SOM.SO} or C{tuple}


    @raise IndexError: The two C{SOM}s do not contain the same number of
                       spectra
                       
    @raise RunTimeError: The x-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The y-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The x-axes of the two C{SO}s are not equal
    """
    
    # import the helper functions
    import hlr_utils

    # Check to see if we are working with a length 1 SOM
    try:
        length_one_som = kwargs["length_one_som"]
    except KeyError:
        length_one_som = False

    try:
        length_one_som_pos = kwargs["length_one_som_pos"]
        if length_one_som_pos != 1 or length_one_som_pos != 2:
            raise RuntimeError("length_one_som_pos must be either 1 or 2 and "\
                               +"%d" % length_one_som_pos)
    except KeyError:
        length_one_som_pos = 2

    if length_one_som:
        if length_one_som_pos == 1:
            som_copy = left
            left = left[0]
        else:
            som_copy = right
            right = right[0]
    else:
        # Not working with a length 1 SOM, do nothing
        pass

    # set up for working through data
    (result, res_descr) = hlr_utils.empty_result(left, right)
    (l_descr, r_descr) = hlr_utils.get_descr(left, right)

    is_number = False

    # error check information
    if (r_descr == "SOM" and l_descr != "SOM") \
           or (r_descr == "SO" and l_descr == "number"):
        left, right = hlr_utils.swap_args(left, right)
        (l_descr, r_descr) = hlr_utils.swap_args(l_descr, r_descr)
    elif r_descr == "SOM" and l_descr == "SOM":
        hlr_utils.math_compatible(left, l_descr, right, r_descr)
    elif l_descr == "number" and r_descr == "number":
        is_number = True
    else:
        pass

    # Check for axis keyword argument
    try:
        axis = kwargs["axis"]
    except KeyError:
        axis = "y"
        
    # Check for axis_pos keyword argument
    try:
        axis_pos = kwargs["axis_pos"]
    except KeyError:
        axis_pos = 0

    # Check for add_nxpars keyword argument
    try:
        add_nxpars_val = kwargs["add_nxpars"]
    except KeyError:
        add_nxpars_val = False

    if length_one_som:
        if length_one_som_pos == 1:
            result = hlr_utils.copy_som_attr(result, res_descr,
                                             som_copy, "SOM",
                                             right, r_descr)
        else:
            result = hlr_utils.copy_som_attr(result, res_descr, left, l_descr,
                                             som_copy, "SOM")            
    else:
        result = hlr_utils.copy_som_attr(result, res_descr, left, l_descr,
                                         right, r_descr,
                                         add_nxpars=add_nxpars_val)

    # iterate through the values
    import array_manip
    
    for i in xrange(hlr_utils.get_length(left, right)):
        val1 = hlr_utils.get_value(left, i, l_descr, axis, axis_pos)
        err2_1 = hlr_utils.get_err2(left, i, l_descr, axis, axis_pos)

        val2 = hlr_utils.get_value(right, i, r_descr, axis, axis_pos)
        err2_2 = hlr_utils.get_err2(right, i, r_descr, axis, axis_pos)
        
        (descr_1, descr_2) = hlr_utils.get_descr(val1, val2)

        hlr_utils.math_compatible(val1, descr_1, val2, descr_2)

        value = array_manip.add_ncerr(val1, err2_1, val2, err2_2)

        map_so = hlr_utils.get_map_so(left, None, i)
        hlr_utils.result_insert(result, res_descr, value, map_so, axis,
                                axis_pos)

    if is_number:
        return tuple(result)
    else:
        return result
Beispiel #2
0
def sumw_ncerr(obj1, obj2, **kwargs):
    """
    This function sums by weighting errors of two objects (C{SOM} or C{SO}) and
    returns the result of that action in an C{SOM}. The function does not
    handle the cases of C{SOM}+C{tuple}, C{SO}+C{tuple} or C{tuple}+C{tuple}.

    @param obj1:  First object in the weighted sum
    @type obj1: C{SOM.SOM} or C{SOM.SO} or C{tuple}
    
    @param obj2: Second object in the the weighted sum
    @type obj2: C{SOM.SOM} or C{SOM.SO} or C{tuple}

    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword axis: This is the axis one wishes to manipulate. If no argument
                   is given the default value is y
    @type axis: C{string}=<y or x>
    
    @keyword axis_pos: This is position of the axis in the axis array. If no
                       argument is given, the default value is 0
    @type axis_pos: C{int}
    
    @keyword length_one_som: This is a flag that lets the function know it is
                             dealing with a length 1 C{SOM} so that attributes
                             may be passed along. The length 1 C{SOM} will be
                             turned into a C{SO}. The default value is False.
    @type length_one_som: C{boolean}
    
    @keyword length_one_som_pos: This is the argument position of the
                                 length 1 C{SOM} since the check is done
                                 before the arguments are swapped. The default
                                 value is 2.
    @type length_one_som_pos: C{int}=<1 or 2>
    

    @return: Object containing the results of the addition
    @rtype: C{SOM.SOM} or C{SOM.SO}


    @raise TypeError: The C{SOM}+C{tuple}, C{SO}+C{tuple} or C{tuple}+C{tuple}
                      cases are presented to the function
    
    @raise IndexError: The two C{SOM}s do not contain the same number of
                       spectra
                       
    @raise RunTimeError: The x-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The y-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The x-axes of the two C{SO}s are not equal
    """
    
    # import the helper functions
    import hlr_utils

    # Check to see if we are working with a length 1 SOM
    try:
        length_one_som = kwargs["length_one_som"]
    except KeyError:
        length_one_som = False

    try:
        length_one_som_pos = kwargs["length_one_som_pos"]
        if length_one_som_pos != 1 or length_one_som_pos != 2:
            raise RuntimeError("length_one_som_pos must be either 1 or 2 and "\
                               +"%d" % length_one_som_pos)
    except KeyError:
        length_one_som_pos = 2

    if length_one_som:
        if length_one_som_pos == 1:
            som_copy = obj1
            obj1 = obj1[0]
        else:
            som_copy = obj2
            obj2 = obj2[0]
    else:
        # Not working with a length 1 SOM, do nothing
        pass

    # set up for working through data
    (result, res_descr) = hlr_utils.empty_result(obj1, obj2)
    (o1_descr, o2_descr) = hlr_utils.get_descr(obj1, obj2)

    # error check information
    if o1_descr == "number" or o2_descr == "number":
        raise RuntimeError("Operations with tuples are not supported!")
    elif o2_descr == "SOM" and o1_descr == "SO":
        (obj1, obj2) = hlr_utils.swap_args(obj1, obj2)
        (o1_descr, o2_descr) = hlr_utils.swap_args(o1_descr, o2_descr)
    elif o2_descr == "SOM" and o1_descr == "SOM":
        hlr_utils.math_compatible(obj1, o1_descr, obj2, o2_descr)
    else:
        pass

    # Check for axis keyword argument
    try:
        axis = kwargs["axis"]
    except KeyError:
        axis = "y"
        
    # Check for axis_pos keyword argument
    try:
        axis_pos = kwargs["axis_pos"]
    except KeyError:
        axis_pos = 0
        
    if length_one_som:
        if length_one_som_pos == 1:
            result = hlr_utils.copy_som_attr(result, res_descr,
                                             som_copy, "SOM",
                                             obj2, o2_descr)
        else:
            result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr,
                                             som_copy, "SOM")            
    else:
        result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr,
                                         obj2, o2_descr)
    
    # iterate through the values
    import array_manip
    
    for i in xrange(hlr_utils.get_length(obj1, obj2)):
        val1 = hlr_utils.get_value(obj1, i, o1_descr, axis, axis_pos)
        err2_1 = hlr_utils.get_err2(obj1, i, o1_descr, axis, axis_pos)

        val2 = hlr_utils.get_value(obj2, i, o2_descr, axis, axis_pos)
        err2_2 = hlr_utils.get_err2(obj2, i, o2_descr, axis, axis_pos)
        
        (descr_1, descr_2) = hlr_utils.get_descr(val1, val2)

        hlr_utils.math_compatible(val1, descr_1, val2, descr_2)

        value = array_manip.sumw_ncerr(val1, err2_1, val2, err2_2)
        
        map_so = hlr_utils.get_map_so(obj1, None, i)
        hlr_utils.result_insert(result, res_descr, value, map_so, axis,
                                axis_pos)

    return result
Beispiel #3
0
def sumw_ncerr(obj1, obj2, **kwargs):
    """
    This function sums by weighting errors of two objects (C{SOM} or C{SO}) and
    returns the result of that action in an C{SOM}. The function does not
    handle the cases of C{SOM}+C{tuple}, C{SO}+C{tuple} or C{tuple}+C{tuple}.

    @param obj1:  First object in the weighted sum
    @type obj1: C{SOM.SOM} or C{SOM.SO} or C{tuple}
    
    @param obj2: Second object in the the weighted sum
    @type obj2: C{SOM.SOM} or C{SOM.SO} or C{tuple}

    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword axis: This is the axis one wishes to manipulate. If no argument
                   is given the default value is y
    @type axis: C{string}=<y or x>
    
    @keyword axis_pos: This is position of the axis in the axis array. If no
                       argument is given, the default value is 0
    @type axis_pos: C{int}
    
    @keyword length_one_som: This is a flag that lets the function know it is
                             dealing with a length 1 C{SOM} so that attributes
                             may be passed along. The length 1 C{SOM} will be
                             turned into a C{SO}. The default value is False.
    @type length_one_som: C{boolean}
    
    @keyword length_one_som_pos: This is the argument position of the
                                 length 1 C{SOM} since the check is done
                                 before the arguments are swapped. The default
                                 value is 2.
    @type length_one_som_pos: C{int}=<1 or 2>
    

    @return: Object containing the results of the addition
    @rtype: C{SOM.SOM} or C{SOM.SO}


    @raise TypeError: The C{SOM}+C{tuple}, C{SO}+C{tuple} or C{tuple}+C{tuple}
                      cases are presented to the function
    
    @raise IndexError: The two C{SOM}s do not contain the same number of
                       spectra
                       
    @raise RunTimeError: The x-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The y-axis units of the C{SOM}s do not match
    
    @raise RunTimeError: The x-axes of the two C{SO}s are not equal
    """

    # import the helper functions
    import hlr_utils

    # Check to see if we are working with a length 1 SOM
    try:
        length_one_som = kwargs["length_one_som"]
    except KeyError:
        length_one_som = False

    try:
        length_one_som_pos = kwargs["length_one_som_pos"]
        if length_one_som_pos != 1 or length_one_som_pos != 2:
            raise RuntimeError("length_one_som_pos must be either 1 or 2 and "\
                               +"%d" % length_one_som_pos)
    except KeyError:
        length_one_som_pos = 2

    if length_one_som:
        if length_one_som_pos == 1:
            som_copy = obj1
            obj1 = obj1[0]
        else:
            som_copy = obj2
            obj2 = obj2[0]
    else:
        # Not working with a length 1 SOM, do nothing
        pass

    # set up for working through data
    (result, res_descr) = hlr_utils.empty_result(obj1, obj2)
    (o1_descr, o2_descr) = hlr_utils.get_descr(obj1, obj2)

    # error check information
    if o1_descr == "number" or o2_descr == "number":
        raise RuntimeError("Operations with tuples are not supported!")
    elif o2_descr == "SOM" and o1_descr == "SO":
        (obj1, obj2) = hlr_utils.swap_args(obj1, obj2)
        (o1_descr, o2_descr) = hlr_utils.swap_args(o1_descr, o2_descr)
    elif o2_descr == "SOM" and o1_descr == "SOM":
        hlr_utils.math_compatible(obj1, o1_descr, obj2, o2_descr)
    else:
        pass

    # Check for axis keyword argument
    try:
        axis = kwargs["axis"]
    except KeyError:
        axis = "y"

    # Check for axis_pos keyword argument
    try:
        axis_pos = kwargs["axis_pos"]
    except KeyError:
        axis_pos = 0

    if length_one_som:
        if length_one_som_pos == 1:
            result = hlr_utils.copy_som_attr(result, res_descr, som_copy,
                                             "SOM", obj2, o2_descr)
        else:
            result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr,
                                             som_copy, "SOM")
    else:
        result = hlr_utils.copy_som_attr(result, res_descr, obj1, o1_descr,
                                         obj2, o2_descr)

    # iterate through the values
    import array_manip

    for i in xrange(hlr_utils.get_length(obj1, obj2)):
        val1 = hlr_utils.get_value(obj1, i, o1_descr, axis, axis_pos)
        err2_1 = hlr_utils.get_err2(obj1, i, o1_descr, axis, axis_pos)

        val2 = hlr_utils.get_value(obj2, i, o2_descr, axis, axis_pos)
        err2_2 = hlr_utils.get_err2(obj2, i, o2_descr, axis, axis_pos)

        (descr_1, descr_2) = hlr_utils.get_descr(val1, val2)

        hlr_utils.math_compatible(val1, descr_1, val2, descr_2)

        value = array_manip.sumw_ncerr(val1, err2_1, val2, err2_2)

        map_so = hlr_utils.get_map_so(obj1, None, i)
        hlr_utils.result_insert(result, res_descr, value, map_so, axis,
                                axis_pos)

    return result