Exemplo n.º 1
0
def create_X_vs_pixpos(som, *args, **kwargs):
    """
    This function takes a group of single spectrum with any given axes
    (wavelength, energy etc.) and rebins those axes to the given axis and
    converts the spectra into a single I{I(X, pixel)} spectrum.

    @param som: The input object with arbitrary (but same) axis spectra
    @type som: C{SOM.SOM}
    
    @param args: A mandatory list of axes for rebinning.  There is a particular
                 order to them. They should be present in the following order:
    
                 Without errors
                   1. Axis
                 With errors
                   1. Axis
                   2. Axis error^2
    @type args: C{nessi_list.NessiList}s
    
    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword withXVar: A flag for whether the function should be expecting the
                       associated axes to have errors. The default value will
                       be I{False}.
    @type withXVar: C{boolean}

    @keyword data_type: The name of the data type which can be either
                        I{histogram}, I{density} or I{coordinate}. The default
                        value will be I{histogram}.
    @type data_type: C{string}
    
    @keyword so_id: The identifier represents a number, string, tuple or other
                    object that describes the resulting C{SO}.
    @type so_id: C{int}, C{string}, C{tuple}, C{pixel ID}
    
    @keyword y_label: The dependent axis label
    @type y_label: C{string}
    
    @keyword y_units: The dependent axis units
    @type y_units: C{string}
    
    @keyword x_label: The second primary axis label
    @type x_label: C{string}
    
    @keyword x_units: The second primary axis units
    @type x_units: C{string}

    @keyword rebin: A flag for turning rebin on or off. The default is I{True}.
    @type rebin: C{boolean}


    @return: Object with a single 2D C{SO} with the given axis and global pixel
             position
    @rtype: C{SOM.SOM}


    @raise RuntimeError: The parameter given to the keyword argument withXVar
                         is not I{True} or I{False}
                         
    @raise RuntimeError: The parameter given to the keyword argument data_type
                         is not I{histogram} or I{density} or I{coordinate}.
                         
    @raise RuntimeError: The number of given arguments (x-axes) is not either 2
                         (no errors) or 4 (with errors)
    """

    import common_lib
    import nessi_list

    # Setup some variables 
    dim = 2
    N_y = []
    N_tot = 1
    N_args = len(args)

    # Check withXVar keyword argument and also check number of given args.
    # Set xvar to the appropriate value
    try:
        value = kwargs["withXVar"]
        if value.lower() == "true":
            if N_args != 2:
                raise RuntimeError("Since you have requested x errors, 2 x "\
                                   +"axes must be provided.")
            else:
                xvar = True
        elif value.lower() == "false":
            if N_args != 2:
                raise RuntimeError("Since you did not requested x errors, 2 "\
                                   +"x axes must be provided.")
            else:
                xvar = False
        else:
            raise RuntimeError("Do not understand given parameter %s" % \
                               value)
    except KeyError:
        if N_args != 1:
            raise RuntimeError("Since you did not requested x errors, 1 "\
                               +"x axes must be provided.")
        else:
            xvar = False

    # Check dataType keyword argument. An offset will be set to 1 for the
    # histogram type and 0 for either density or coordinate
    try:
        data_type = kwargs["data_type"]
        if data_type.lower() == "histogram":
            offset = 1
        elif data_type.lower() == "density" or \
                 data_type.lower() == "coordinate":
            offset = 0
        else:
            raise RuntimeError("Do not understand data type given: %s" % \
                               data_type)
    # Default is offset for histogram
    except KeyError:
        offset = 1

    so_dim = SOM.SO(dim)

    arb_axis = 1
    pixel_axis = 0

    # Set the x-axis arguments from the *args list into the new SO
    if not xvar:
        so_dim.axis[arb_axis].val = args[0]
    else:
        so_dim.axis[arb_axis].val = args[0]
        so_dim.axis[arb_axis].var = args[1]

    # Set individual value axis sizes (not x-axis size)
    N_y.append(len(args[0]) - offset)

    # Calculate total 2D array size
    N_som = len(som)
    N_tot = N_som * N_y[-1]

    # Make second axis on total number of pixels
    so_dim.axis[pixel_axis].val = hlr_utils.make_axis(0, N_som, 1)
    if xvar:
        so_dim.axis[pixel_axis].var = nessi_list.NessiList(N_som+1)

    # Create y and var_y lists from total 2D size
    so_dim.y = nessi_list.NessiList(N_tot)
    so_dim.var_y = nessi_list.NessiList(N_tot)

    # Check for rebinning
    try:
        rebin = kwargs["rebin"]
    except KeyError:
        rebin = True

    # Rebin data to X axis
    if rebin:
        som_1 = common_lib.rebin_axis_1D(som, args[0])
    else:
        som_1 = som

    del som

    import array_manip
    
    for i in xrange(hlr_utils.get_length(som_1)):

        val = hlr_utils.get_value(som_1, i, "SOM", "y")
        err2 = hlr_utils.get_err2(som_1, i, "SOM", "y")

        start = i * N_y[0]

        (so_dim.y, so_dim.var_y) = array_manip.add_ncerr(so_dim.y,
                                                         so_dim.var_y,
                                                         val,
                                                         err2,
                                                         a_start=start)

        
    # Check for so_id keyword argument
    try:
        so_dim.id = kwargs["so_id"]
    except KeyError:
        so_dim.id = 0

    comb_som = SOM.SOM()
    comb_som.copyAttributes(som_1)

    del som_1

    # Check for y_label keyword argument
    try:
        comb_som.setYLabel(kwargs["y_label"])
    except KeyError:        
        comb_som.setYLabel("Counts")

    # Check for y_units keyword argument
    try:
        comb_som.setYUnits(kwargs["y_units"])
    except KeyError:
        comb_som.setYUnits("Counts / Arb")

    # Check for x_label keyword argument
    try:
        comb_som.setAllAxisLabels(["Pixel Number", kwargs["x_label"]])
    except KeyError:
        comb_som.setAllAxisLabels(["Pixel Number", "Arbitrary"])

    # Check for x_units keyword argument
    try:
        comb_som.setAllAxisUnits(["Pixel#", kwargs["x_units"]])
    except KeyError:
        comb_som.setAllAxisUnits(["Pixel#", "Arb"])

    comb_som.append(so_dim)

    del so_dim

    return comb_som
Exemplo n.º 2
0
        result.attr_list["summed_ids"] = so_id_list

    return result


if __name__ == "__main__":
    import hlr_test
    import SOM

    som1 = SOM.SOM()
    so1 = hlr_test.generate_so("histogram", 0, 5, 1, 1)
    so1.id = 1
    som1.append(so1)
    so2 = hlr_test.generate_so("histogram", 0, 5, 1, 3)
    so2.id = 2
    som1.append(so2)
    so3 = hlr_test.generate_so("histogram", 0, 5, 1, 2)
    so3.id = 3
    som1.append(so3)

    axis_new = hlr_utils.make_axis(0, 5, 2.5)

    print "********** SOM1"
    print "* ", som1[0]
    print "* ", som1[1]
    print "* ", som1[2]

    print "********** sum_all_spectra"
    print "* som:", sum_all_spectra(som1)
    print "* som (rebin):", sum_all_spectra(som1, rebin_axis=axis_new)
Exemplo n.º 3
0
    # Check for x_units keyword argument
    try:
        comb_som.setAllAxisUnits(["Pixel#", kwargs["x_units"]])
    except KeyError:
        comb_som.setAllAxisUnits(["Pixel#", "Arb"])

    comb_som.append(so_dim)

    del so_dim

    return comb_som


if __name__ == "__main__":
    import hlr_test

    som1 = hlr_test.generate_som("histogram", 1, 3)
    som1.attr_list.instrument = SOM.ASG_Instrument()

    x_axis_err = hlr_utils.make_axis(0, 1, 0.25)

    print "********** SOM1"
    print "* ", som1[0]
    print "* ", som1[1]
    print "* ", som1[2]

    print "********** create_X_vs_pixpos"
    print " som :", create_X_vs_pixpos(som1, som1[0].axis[0].val, x_axis_err,
                                       withXVar="True")
Exemplo n.º 4
0
        result.attr_list["summed_ids"] = so_id_list            

    return result

if __name__ == "__main__":
    import hlr_test
    import SOM
    
    som1 = SOM.SOM()
    so1 = hlr_test.generate_so("histogram", 0, 5, 1, 1)
    so1.id = 1
    som1.append(so1)
    so2 = hlr_test.generate_so("histogram", 0, 5, 1, 3)
    so2.id = 2
    som1.append(so2)
    so3 = hlr_test.generate_so("histogram", 0, 5, 1, 2)
    so3.id = 3
    som1.append(so3)

    axis_new = hlr_utils.make_axis(0, 5, 2.5)

    print "********** SOM1"
    print "* ", som1[0]
    print "* ", som1[1]
    print "* ", som1[2]

    print "********** sum_all_spectra"
    print "* som:", sum_all_spectra(som1)
    print "* som (rebin):", sum_all_spectra(som1, rebin_axis=axis_new)

Exemplo n.º 5
0
    if options.d_bins is None and options.Q_bins is None:
        parser.error("Must specify at least one type of binning")
    else:
        pass

    # set the verbosity
    configure.verbose = options.verbose

    # set the data paths
    configure.data_paths = hlr_utils.create_data_paths(options.data_paths)

    # set the d bins
    if options.d_bins is not None:
        dfacts = options.d_bins.split(',')
        configure.d_bins = hlr_utils.make_axis(float(dfacts[0]),
                                               float(dfacts[1]),
                                               float(dfacts[2]))
    else:
        configure.d_bins = options.d_bins

    # set the Q bins
    if options.Q_bins is not None:
        qfacts = options.Q_bins.split(',')
        configure.Q_bins = hlr_utils.make_axis(float(qfacts[0]),
                                               float(qfacts[1]),
                                               float(qfacts[2]))
    else:
        configure.Q_bins = options.Q_bins

    # set the tof bins
    if options.tof_bins is not None:
Exemplo n.º 6
0
    if options.d_bins is None and options.Q_bins is None:
        parser.error("Must specify at least one type of binning")
    else:
        pass

    # set the verbosity
    configure.verbose = options.verbose

    # set the data paths
    configure.data_paths = hlr_utils.create_data_paths(options.data_paths)

    # set the d bins
    if options.d_bins is not None:
        dfacts = options.d_bins.split(',')
        configure.d_bins = hlr_utils.make_axis(float(dfacts[0]),
                                               float(dfacts[1]),
                                               float(dfacts[2]))
    else:
        configure.d_bins = options.d_bins

    # set the Q bins
    if options.Q_bins is not None:
        qfacts = options.Q_bins.split(',')
        configure.Q_bins = hlr_utils.make_axis(float(qfacts[0]),
                                               float(qfacts[1]),
                                               float(qfacts[2]))
    else:
        configure.Q_bins = options.Q_bins

    # set the tof bins
    if options.tof_bins is not None:
Exemplo n.º 7
0
def create_X_vs_pixpos(som, *args, **kwargs):
    """
    This function takes a group of single spectrum with any given axes
    (wavelength, energy etc.) and rebins those axes to the given axis and
    converts the spectra into a single I{I(X, pixel)} spectrum.

    @param som: The input object with arbitrary (but same) axis spectra
    @type som: C{SOM.SOM}
    
    @param args: A mandatory list of axes for rebinning.  There is a particular
                 order to them. They should be present in the following order:
    
                 Without errors
                   1. Axis
                 With errors
                   1. Axis
                   2. Axis error^2
    @type args: C{nessi_list.NessiList}s
    
    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword withXVar: A flag for whether the function should be expecting the
                       associated axes to have errors. The default value will
                       be I{False}.
    @type withXVar: C{boolean}

    @keyword data_type: The name of the data type which can be either
                        I{histogram}, I{density} or I{coordinate}. The default
                        value will be I{histogram}.
    @type data_type: C{string}
    
    @keyword so_id: The identifier represents a number, string, tuple or other
                    object that describes the resulting C{SO}.
    @type so_id: C{int}, C{string}, C{tuple}, C{pixel ID}
    
    @keyword y_label: The dependent axis label
    @type y_label: C{string}
    
    @keyword y_units: The dependent axis units
    @type y_units: C{string}
    
    @keyword x_label: The second primary axis label
    @type x_label: C{string}
    
    @keyword x_units: The second primary axis units
    @type x_units: C{string}

    @keyword rebin: A flag for turning rebin on or off. The default is I{True}.
    @type rebin: C{boolean}


    @return: Object with a single 2D C{SO} with the given axis and global pixel
             position
    @rtype: C{SOM.SOM}


    @raise RuntimeError: The parameter given to the keyword argument withXVar
                         is not I{True} or I{False}
                         
    @raise RuntimeError: The parameter given to the keyword argument data_type
                         is not I{histogram} or I{density} or I{coordinate}.
                         
    @raise RuntimeError: The number of given arguments (x-axes) is not either 2
                         (no errors) or 4 (with errors)
    """

    import common_lib
    import nessi_list

    # Setup some variables
    dim = 2
    N_y = []
    N_tot = 1
    N_args = len(args)

    # Check withXVar keyword argument and also check number of given args.
    # Set xvar to the appropriate value
    try:
        value = kwargs["withXVar"]
        if value.lower() == "true":
            if N_args != 2:
                raise RuntimeError("Since you have requested x errors, 2 x "\
                                   +"axes must be provided.")
            else:
                xvar = True
        elif value.lower() == "false":
            if N_args != 2:
                raise RuntimeError("Since you did not requested x errors, 2 "\
                                   +"x axes must be provided.")
            else:
                xvar = False
        else:
            raise RuntimeError("Do not understand given parameter %s" % \
                               value)
    except KeyError:
        if N_args != 1:
            raise RuntimeError("Since you did not requested x errors, 1 "\
                               +"x axes must be provided.")
        else:
            xvar = False

    # Check dataType keyword argument. An offset will be set to 1 for the
    # histogram type and 0 for either density or coordinate
    try:
        data_type = kwargs["data_type"]
        if data_type.lower() == "histogram":
            offset = 1
        elif data_type.lower() == "density" or \
                 data_type.lower() == "coordinate":
            offset = 0
        else:
            raise RuntimeError("Do not understand data type given: %s" % \
                               data_type)
    # Default is offset for histogram
    except KeyError:
        offset = 1

    so_dim = SOM.SO(dim)

    arb_axis = 1
    pixel_axis = 0

    # Set the x-axis arguments from the *args list into the new SO
    if not xvar:
        so_dim.axis[arb_axis].val = args[0]
    else:
        so_dim.axis[arb_axis].val = args[0]
        so_dim.axis[arb_axis].var = args[1]

    # Set individual value axis sizes (not x-axis size)
    N_y.append(len(args[0]) - offset)

    # Calculate total 2D array size
    N_som = len(som)
    N_tot = N_som * N_y[-1]

    # Make second axis on total number of pixels
    so_dim.axis[pixel_axis].val = hlr_utils.make_axis(0, N_som, 1)
    if xvar:
        so_dim.axis[pixel_axis].var = nessi_list.NessiList(N_som + 1)

    # Create y and var_y lists from total 2D size
    so_dim.y = nessi_list.NessiList(N_tot)
    so_dim.var_y = nessi_list.NessiList(N_tot)

    # Check for rebinning
    try:
        rebin = kwargs["rebin"]
    except KeyError:
        rebin = True

    # Rebin data to X axis
    if rebin:
        som_1 = common_lib.rebin_axis_1D(som, args[0])
    else:
        som_1 = som

    del som

    import array_manip

    for i in xrange(hlr_utils.get_length(som_1)):

        val = hlr_utils.get_value(som_1, i, "SOM", "y")
        err2 = hlr_utils.get_err2(som_1, i, "SOM", "y")

        start = i * N_y[0]

        (so_dim.y, so_dim.var_y) = array_manip.add_ncerr(so_dim.y,
                                                         so_dim.var_y,
                                                         val,
                                                         err2,
                                                         a_start=start)

    # Check for so_id keyword argument
    try:
        so_dim.id = kwargs["so_id"]
    except KeyError:
        so_dim.id = 0

    comb_som = SOM.SOM()
    comb_som.copyAttributes(som_1)

    del som_1

    # Check for y_label keyword argument
    try:
        comb_som.setYLabel(kwargs["y_label"])
    except KeyError:
        comb_som.setYLabel("Counts")

    # Check for y_units keyword argument
    try:
        comb_som.setYUnits(kwargs["y_units"])
    except KeyError:
        comb_som.setYUnits("Counts / Arb")

    # Check for x_label keyword argument
    try:
        comb_som.setAllAxisLabels(["Pixel Number", kwargs["x_label"]])
    except KeyError:
        comb_som.setAllAxisLabels(["Pixel Number", "Arbitrary"])

    # Check for x_units keyword argument
    try:
        comb_som.setAllAxisUnits(["Pixel#", kwargs["x_units"]])
    except KeyError:
        comb_som.setAllAxisUnits(["Pixel#", "Arb"])

    comb_som.append(so_dim)

    del so_dim

    return comb_som
Exemplo n.º 8
0
    try:
        comb_som.setAllAxisUnits(["Pixel#", kwargs["x_units"]])
    except KeyError:
        comb_som.setAllAxisUnits(["Pixel#", "Arb"])

    comb_som.append(so_dim)

    del so_dim

    return comb_som


if __name__ == "__main__":
    import hlr_test

    som1 = hlr_test.generate_som("histogram", 1, 3)
    som1.attr_list.instrument = SOM.ASG_Instrument()

    x_axis_err = hlr_utils.make_axis(0, 1, 0.25)

    print "********** SOM1"
    print "* ", som1[0]
    print "* ", som1[1]
    print "* ", som1[2]

    print "********** create_X_vs_pixpos"
    print " som :", create_X_vs_pixpos(som1,
                                       som1[0].axis[0].val,
                                       x_axis_err,
                                       withXVar="True")