def div_ncerr(left, right, **kwargs): """ This function divides two objects (C{SOM}, C{SO} or {tuple(val,val_err2)}) and returns the result of the division in an C{SOM}, C{SO} or C{tuple}. @param left: Object on the left of the division sign @type left: C{SOM.SOM} or C{SOM.SO} or C{tuple} @param right: Object on the right of the division 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 division order is not commutative. The default value is 2. @type length_one_som_pos: C{int}=<1 or 2> @return: Object containing the results of the division @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": 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 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) # 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.div_ncerr(val1, err2_1, val2, err2_2) map_so = hlr_utils.get_map_so(left, right, i) hlr_utils.result_insert(result, res_descr, value, map_so, axis, axis_pos) if is_number: return tuple(result) else: return result
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
def rebin_efficiency(obj1, obj2, **kwargs): """ This function takes two objects and rebins the data for obj1 onto the axis provided by obj2. The units on the x-axes needs to be I{Angstroms}, since this is what the efficiencies will be present as. @param obj1: Object that will be rebinned @type obj1: C{SOM.SOM} or C{SOM.SO} @param obj2: Object that will provide the axis for rebinning @type obj2: C{SOM.SOM} or C{SOM.SO} @param kwargs: A list of keyword arguments that the function accepts: @keyword units: The expected units for this function. The default for this function is I{Angstroms}. @type units: C{string} @return: Object that has been rebinned @rtype: C{SOM.SOM} or C{SOM.SO} @raise TypeError: The C{SOM}-C{SO} operation is attempted @raise TypeError: The C{SO}-C{SOM} operation is attempted @raise TypeError: obj1 not a C{SOM} or C{SO} @raise TypeError: obj2 not a C{SOM} or C{SO} @raise IndexError: The C{SOM}s do not have the same number of C{SO}s @raise RuntimeError: The C{SOM} x-axis units are not I{Angstroms} @raise RuntimeError: The x-axis units of the C{SOM}s do not match """ # import the helper functions import hlr_utils # Kickout if monitor object is None if obj1 is None: return obj1 # 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 checking for types if o1_descr == "SOM" and o2_descr == "SO": raise TypeError, "SOM-SO operation not supported" elif o1_descr == "SO" and o2_descr == "SOM": raise TypeError("SO-SOM operation not supported") # Have the right object combination, go on else: pass # Setup keyword arguments try: units = kwargs["units"] except KeyError: units = "Angstroms" if o1_descr == "SOM" and o2_descr == "SOM": hlr_utils.math_compatible(obj1, o1_descr, obj2, o2_descr) # If both objects are not SOMs, do nothing else: pass result = hlr_utils.copy_som_attr(result, res_descr, obj2, o2_descr) if res_descr == "SOM": result = hlr_utils.force_units(result, units) # Can't force units on anything other than a SOM else: pass # iterate through the values import common_lib for i in xrange(hlr_utils.get_length(obj1, obj2)): val1 = hlr_utils.get_value(obj1, i, o1_descr, "all") val2 = hlr_utils.get_value(obj2, i, o2_descr, "x") value = common_lib.rebin_axis_1D(val1, val2) hlr_utils.result_insert(result, res_descr, value, None, "all") return result
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
def rebin_efficiency(obj1, obj2, **kwargs): """ This function takes two objects and rebins the data for obj1 onto the axis provided by obj2. The units on the x-axes needs to be I{Angstroms}, since this is what the efficiencies will be present as. @param obj1: Object that will be rebinned @type obj1: C{SOM.SOM} or C{SOM.SO} @param obj2: Object that will provide the axis for rebinning @type obj2: C{SOM.SOM} or C{SOM.SO} @param kwargs: A list of keyword arguments that the function accepts: @keyword units: The expected units for this function. The default for this function is I{Angstroms}. @type units: C{string} @return: Object that has been rebinned @rtype: C{SOM.SOM} or C{SOM.SO} @raise TypeError: The C{SOM}-C{SO} operation is attempted @raise TypeError: The C{SO}-C{SOM} operation is attempted @raise TypeError: obj1 not a C{SOM} or C{SO} @raise TypeError: obj2 not a C{SOM} or C{SO} @raise IndexError: The C{SOM}s do not have the same number of C{SO}s @raise RuntimeError: The C{SOM} x-axis units are not I{Angstroms} @raise RuntimeError: The x-axis units of the C{SOM}s do not match """ # import the helper functions import hlr_utils # Kickout if monitor object is None if obj1 is None: return obj1 # 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 checking for types if o1_descr == "SOM" and o2_descr == "SO": raise TypeError, "SOM-SO operation not supported" elif o1_descr == "SO" and o2_descr == "SOM": raise TypeError("SO-SOM operation not supported") # Have the right object combination, go on else: pass # Setup keyword arguments try: units = kwargs["units"] except KeyError: units = "Angstroms" if o1_descr == "SOM" and o2_descr == "SOM": hlr_utils.math_compatible(obj1, o1_descr, obj2, o2_descr) # If both objects are not SOMs, do nothing else: pass result = hlr_utils.copy_som_attr( result, res_descr, obj2, o2_descr) if res_descr == "SOM": result = hlr_utils.force_units(result, units) # Can't force units on anything other than a SOM else: pass # iterate through the values import common_lib for i in xrange(hlr_utils.get_length(obj1, obj2)): val1 = hlr_utils.get_value(obj1, i, o1_descr, "all") val2 = hlr_utils.get_value(obj2, i, o2_descr, "x") value = common_lib.rebin_axis_1D(val1, val2) hlr_utils.result_insert(result, res_descr, value, None, "all") return result
def subtract_bkg_from_data(data_som, bkg_som, **kwargs): """ This function subtracts one data set from another. @param data_som: Object containing the data to subtract from @type data_som: C{SOM.SOM} or C{SOM.SO} @param bkg_som: Object containing the data to be subtracted @type bkg_som: C{SOM.SOM} or C{SOM.SO} @param kwargs: A list of keyword arguments that the function accepts: @keyword dataset1: The type name of the first dataset. Default is I{dataset1}. @type dataset1: C{string} @keyword dataset2: The type name of the second dataset. Default is I{dataset2}. @keyword scale: The constant by which to scale the background spectra @type scale: L{hlr_utils.DrParameter} @keyword verbose: A flag for turning on information from the function. @type verbose: C{boolean} @keyword timer: Timing object so the function can perform timing @type timer: C{sns_timer.DiffTime} @return: The data subtracted by the background @rtype: C{SOM.SOM} or C{SOM.SO} @raise TypeError: Both objects are not C{SOM}s, a C{SO} and a C{SOM} or a C{SOM} and a C{SO} """ # Kickout if data object is NoneType if data_som is None: return None # Kickout if background object is NoneType if bkg_som is None: return data_som # import the helper functions import hlr_utils (data_descr, bkg_descr) = hlr_utils.get_descr(data_som, bkg_som) if data_descr == "SOM" and bkg_descr == "SOM": hlr_utils.math_compatible(data_som, data_descr, bkg_som, bkg_descr) elif data_descr == "SOM" and bkg_descr == "SO" or \ data_descr == "SO" and bkg_descr == "SOM": # You have SO-SOM or SOM-SO, so assume everything is OK pass else: raise TypeError("The object combinations must be SOM-SOM, SO-SOM "\ +"or SOM-SO. You provided a %s and a %s" % \ (data_descr, bkg_descr)) # Check for keywords try: verbose = kwargs["verbose"] except KeyError: verbose = False try: t = kwargs["timer"] except KeyError: t = None try: dataset1 = kwargs["dataset1"] except KeyError: dataset1 = "dataset1" try: dataset2 = kwargs["dataset2"] except KeyError: dataset2 = "dataset2" try: scale = kwargs["scale"] except KeyError: scale = None import common_lib if scale is not None: if verbose: print "Scaling %s for %s" % (dataset2, dataset1) bkg_som2 = common_lib.mult_ncerr(bkg_som, scale.toValErrTuple()) if t is not None: t.getTime(msg="After scaling %s for %s " % (dataset2, dataset1)) else: bkg_som2 = bkg_som del bkg_som if verbose: print "Subtracting %s from %s" % (dataset2, dataset1) data_som2 = common_lib.sub_ncerr(data_som, bkg_som2) if t is not None: t.getTime(msg="After subtracting %s from %s " % (dataset2, dataset1)) return data_som2