Beispiel #1
0
def trim_escape_peak(data, param_dict, y_size):
    """
    Calculate escape peak within required range.

    Parameters
    ----------
    data : array
        raw spectrum
    param_dict : dict
        parameters for fitting
    y_size : int
        the size of trimmed spectrum

    Returns
    -------
    array :
        trimmed escape peak spectrum
    """
    ratio = param_dict['non_fitting_values']['escape_ratio']
    xe, ye = compute_escape_peak(data, ratio, param_dict)
    lowv = param_dict['non_fitting_values']['energy_bound_low']['value']
    highv = param_dict['non_fitting_values']['energy_bound_high']['value']
    xe, es_peak = trim(xe, ye, lowv, highv)
    logger.info('Escape peak is considered with ratio {}'.format(ratio))

    # align to the same length
    if y_size > es_peak.size:
        temp = es_peak
        es_peak = np.zeros(y_size)
        es_peak[:temp.size] = temp
    else:
        es_peak = es_peak[:y_size]
    return es_peak
Beispiel #2
0
def define_range(data, low, high, a0, a1):
    """
    Cut x range according to values define in param_dict.

    Parameters
    ----------
    data : array
        raw spectrum
    low : float
        low bound in KeV
    high : float
        high bound in KeV
    a0 : float
        offset term of energy calibration
    a1 : float
        linear term of energy calibration

    Returns
    -------
    x : array
        trimmed channel number
    y : array
        trimmed spectrum according to x
    """
    x = np.arange(data.size)

    # ratio to transfer energy value back to channel value
    #approx_ratio = 100

    low_new = int(np.around((low - a0)/a1))
    high_new = int(np.around((high - a0)/a1))
    x0, y0 = trim(x, data, low_new, high_new)
    return x0, y0
Beispiel #3
0
def trim_escape_peak(data, param_dict, y_size):
    """
    Calculate escape peak within required range.

    Parameters
    ----------
    data : array
        raw spectrum
    param_dict : dict
        parameters for fitting
    y_size : int
        the size of trimmed spectrum

    Returns
    -------
    array :
        trimmed escape peak spectrum
    """
    ratio = param_dict['non_fitting_values']['escape_ratio']
    xe, ye = compute_escape_peak(data, ratio, param_dict)
    lowv = param_dict['non_fitting_values']['energy_bound_low']['value']
    highv = param_dict['non_fitting_values']['energy_bound_high']['value']
    xe, es_peak = trim(xe, ye, lowv, highv)
    logger.info('Escape peak is considered with ratio {}'.format(ratio))

    # align to the same length
    if y_size > es_peak.size:
        temp = es_peak
        es_peak = np.zeros(y_size)
        es_peak[:temp.size] = temp
    else:
        es_peak = es_peak[:y_size]
    return es_peak
Beispiel #4
0
def define_range(data, low, high, a0, a1):
    """
    Cut x range according to values define in param_dict.

    Parameters
    ----------
    data : array
        raw spectrum
    low : float
        low bound in KeV
    high : float
        high bound in KeV
    a0 : float
        offset term of energy calibration
    a1 : float
        linear term of energy calibration

    Returns
    -------
    x : array
        trimmed channel number
    y : array
        trimmed spectrum according to x
    """
    x = np.arange(data.size)

    # ratio to transfer energy value back to channel value
    #approx_ratio = 100

    low_new = int(np.around((low - a0)/a1))
    high_new = int(np.around((high - a0)/a1))
    x0, y0 = trim(x, data, low_new, high_new)
    return x0, y0
Beispiel #5
0
def test_fit():
    param = get_para()
    pileup_peak = ['Si_Ka1-Si_Ka1', 'Si_Ka1-Ce_La1']
    user_peak = ['user_peak1']
    elemental_lines = (['Ar_K', 'Fe_K', 'Ce_L', 'Pt_M'] + pileup_peak +
                       user_peak)
    x0 = np.arange(2000)
    y0 = synthetic_spectrum()
    default_area = 1e5
    x, y = trim(x0, y0, 100, 1300)
    MS = ModelSpectrum(param, elemental_lines)
    MS.assemble_models()

    result = MS.model_fit(x, y, weights=1 / np.sqrt(y + 1), maxfev=200)

    # check area of each element
    for k, v in six.iteritems(result.values):
        if 'area' in k:
            # error smaller than 1e-6
            assert abs(v - default_area) / default_area < 1e-6

    # multiple peak sumed, so value should be larger than one peak area 1e5
    sum_Fe = sum_area('Fe_K', result)
    assert sum_Fe > default_area

    sum_Ce = sum_area('Ce_L', result)
    assert sum_Ce > default_area

    sum_Pt = sum_area('Pt_M', result)
    assert sum_Pt > default_area

    # create full list of parameters
    PC = ParamController(param, elemental_lines)
    new_params = PC.params
    # update values
    update_parameter_dict(new_params, result)
    for k, v in six.iteritems(new_params):
        if 'area' in k:
            assert_equal(v['value'], result.values[k])
def test_fit():
    param = get_para()
    pileup_peak = ['Si_Ka1-Si_Ka1', 'Si_Ka1-Ce_La1']
    user_peak = ['user_peak1']
    elemental_lines = (['Ar_K', 'Fe_K', 'Ce_L', 'Pt_M'] + pileup_peak +
                       user_peak)
    x0 = np.arange(2000)
    y0 = synthetic_spectrum()
    default_area = 1e5
    x, y = trim(x0, y0, 100, 1300)
    MS = ModelSpectrum(param, elemental_lines)
    MS.assemble_models()

    result = MS.model_fit(x, y, weights=1/np.sqrt(y+1), maxfev=200)

    # check area of each element
    for k, v in six.iteritems(result.values):
        if 'area' in k:
            # error smaller than 1e-6
            assert_true(abs(v - default_area)/default_area < 1e-6)

    # multiple peak sumed, so value should be larger than one peak area 1e5
    sum_Fe = sum_area('Fe_K', result)
    assert_true(sum_Fe > default_area)

    sum_Ce = sum_area('Ce_L', result)
    assert_true(sum_Ce > default_area)

    sum_Pt = sum_area('Pt_M', result)
    assert_true(sum_Pt > default_area)

    # create full list of parameters
    PC = ParamController(param, elemental_lines)
    new_params = PC.params
    # update values
    update_parameter_dict(new_params, result)
    for k, v in six.iteritems(new_params):
        if 'area' in k:
            assert_equal(v['value'], result.values[k])