def test_data_report():
    """
    Test function that confirms spectrafit.data_report behaves as expected.
    """
    compound = SHOYU_DATA_DICT['WATER']
    data = spectrafit.data_report(compound['x'], compound['y'])
    assert len(data) == 5, 'more values in return than expected'
    assert len(data[0]) == 3, 'more than three peaks detected for WATER'
    assert isinstance(data, tuple), 'output data type is not a tuple'
    try:
        spectrafit.data_report(1.1, compound['y'])
    except TypeError:
        print(
            'A float was passed to the function, and was handled well with a TypeError.'
        )
    try:
        spectrafit.data_report(compound['x'], 1.2)
    except TypeError:
        print(
            'A float was passed to the function, and was handled well with a TypeError.'
        )
Exemplo n.º 2
0
def peak_assignment(unknown_x,
                    unknown_y,
                    known_compound_list,
                    precision=0.03,
                    plot=True):
    """This function is a wrapper function from which all classification of peaks occurs."""

    #Handling errors in inputs.
    if not isinstance(unknown_x, np.ndarray):
        raise TypeError(
            "Passed value of `unknown_x` is not a np.ndarray! Instead, it is: "
            + str(type(unknown_x)))

    if not isinstance(unknown_y, np.ndarray):
        raise TypeError(
            "Passed value of `unknown_y` is not a np.ndarray! Instead, it is: "
            + str(type(unknown_y)))

    if not isinstance(known_compound_list, list):
        raise TypeError(
            "Passed value of `known_compound_list` is not a list! Instead, it is: "
            + str(type(known_compound_list)))

    #Now we need to check the elements within the known_compound_list to make sure they are correct.
    for i, _ in enumerate(known_compound_list):
        if not isinstance(known_compound_list[i], dict):
            raise TypeError(
                """Passed value within `known_compound_list` is not a dictionary!
            Instead, it is: """ + str(type(known_compound_list[i])))

    if not isinstance(precision, (float, int)):
        raise TypeError("""Passed value of `precision` is not a float or int!
        Instead, it is: """ + str(type(precision)))

    if not isinstance(plot, bool):
        raise TypeError("""Passed value of `plot` is not a Boolean!
        Instead, it is: """ + str(type(plot)))

    #Lets identify the peaks in the unknown spectrum.
    unknown_peaks = spectrafit.data_report(unknown_x, unknown_y)[0]

    #OK, next identify all of the peaks present in the known compound set.
    #For efficiency, we'll also compare them against the unknown in the same for loop.
    known_compound_peaks = []
    assignment_matrix = []

    for i, _ in enumerate(known_compound_list):
        known_compound_peaks.append(
            spectrafit.compound_report(known_compound_list[i])[0])
        print("The peaks that we found for " +
              str(known_compound_list[i]['title']) + " are: ")
        print(known_compound_peaks[i])
        assignment_matrix.append(
            compare_unknown_to_known(unknown_peaks, known_compound_peaks[i],
                                     precision))

    #Ok, so that generates a full association matrix that contains everything
    #we need to assign peaks.
    #Now, let's go through and actually assign text to peaks.
    unknown_peak_assignments = peak_position_comparisons(
        unknown_peaks, known_compound_peaks, known_compound_list,
        assignment_matrix)
    print(unknown_peak_assignments)

    if plot:
        plotting_peak_assignments(unknown_x, unknown_y, unknown_peaks,
                                  unknown_peak_assignments)

    percentages = percentage_of_peaks_found(known_compound_peaks,
                                            assignment_matrix,
                                            known_compound_list)
    print(percentages)
Exemplo n.º 3
0
def test_compare_unknown_to_known():
    """This function tests the operation of the compare_unknown_to_known
    function in peakidentify.py"""
    #Build our test dataset.
    shoyu_data_dict = pickle.load(open('raman_spectra/shoyu_data_dict.p',
                                       'rb'))
    compound_1 = shoyu_data_dict['WATER']
    compound_2 = shoyu_data_dict['CARBON MONOXIDE']
    compound_3 = shoyu_data_dict['CARBON DIOXIDE']
    unknown_x, unknown_y = shoyu.combine_spectra(compound_1,
                                                 compound_2,
                                                 plot=False)
    unknown_x = np.asarray(unknown_x)
    unknown_y = np.asarray(unknown_y)
    known_compound_list = [compound_1, compound_2, compound_3]
    precision = 0.03
    known_peaks = []
    for i, _ in enumerate(known_compound_list):
        known_peaks.append(
            spectrafit.compound_report(known_compound_list[i])[0])
    unknown_peaks = spectrafit.data_report(unknown_x, unknown_y)[0]

    try:
        peakidentify.compare_unknown_to_known(1, known_peaks[0], precision)
    except TypeError:
        print("An invalid unknown_peaks value was passed to the function, "
              "and was handled correctly.")

    try:
        peakidentify.compare_unknown_to_known(unknown_peaks, 'known_peaks',
                                              precision)
    except TypeError:
        print("An invalid known_peaks value was passed to the function, "
              "and was handled correctly.")

    try:
        peakidentify.compare_unknown_to_known(unknown_peaks, known_peaks[0],
                                              'precision')
    except TypeError:
        print("An invalid precision value was passed to the function, and "
              "was handled correctly.")

    #After testing for resilience to unexpected inputs, now ensure outputs are performing correctly

    #First, make sure function is returning the list.
    assert isinstance(
        peakidentify.compare_unknown_to_known(unknown_peaks, known_peaks[0],
                                              precision),
        np.ndarray), (""
                      "Function is not returning list")

    #Compare one set of peaks to itself. The full association matrix should have all values = 1.
    self_comp = np.mean(
        peakidentify.compare_unknown_to_known(known_peaks[0], known_peaks[0],
                                              precision))
    assert self_comp == 1, ("Peak Assignment Error. Comparison of compound "
                            "against itself should find all peaks.")

    dif_comp = np.mean(
        peakidentify.compare_unknown_to_known([1, 3, 6], [1000, 2000, 5000],
                                              precision))
    assert dif_comp == 0, ("Peak Assignment Error. Passed values should "
                           "have no matching assignments.")
Exemplo n.º 4
0
def test_plotting_peak_assignments():
    """This function tests the operation of the peak_assignment
    function in peakidentify.py"""
    #First, generate good data.
    shoyu_data_dict = pickle.load(open('raman_spectra/shoyu_data_dict.p',
                                       'rb'))
    compound_1 = shoyu_data_dict['WATER']
    compound_2 = shoyu_data_dict['CARBON MONOXIDE']
    compound_3 = shoyu_data_dict['CARBON DIOXIDE']
    unknown_x, unknown_y = shoyu.combine_spectra(compound_1,
                                                 compound_2,
                                                 plot=False)
    unknown_x = np.asarray(unknown_x)
    unknown_y = np.asarray(unknown_y)
    known_compound_list = [compound_1, compound_2, compound_3]
    precision = 0.03
    unknown_peaks = spectrafit.data_report(unknown_x, unknown_y)[0]
    known_peaks = []
    association_matrix = []
    for i, _ in enumerate(known_compound_list):
        known_peaks.append(
            spectrafit.compound_report(known_compound_list[i])[0])
        association_matrix.append(
            peakidentify.compare_unknown_to_known(unknown_peaks,
                                                  known_peaks[i], precision))
    unknown_peak_assignments = peakidentify.peak_position_comparisons(
        unknown_peaks, known_peaks, known_compound_list, association_matrix)

    #Test for input error handling.
    try:
        peakidentify.plotting_peak_assignments(1, unknown_y, unknown_peaks,
                                               unknown_peak_assignments)
    except TypeError:
        print("""The function correctly handled the error
        when an int was input instead of the unknown_x list""")

    try:
        peakidentify.plotting_peak_assignments(unknown_x, 3, unknown_peaks,
                                               unknown_peak_assignments)
    except TypeError:
        print("""The function correctly handled the error when an int
        was input instead of the unknown_y list""")

    try:
        peakidentify.plotting_peak_assignments(unknown_x, unknown_y,
                                               'unknown_peaks',
                                               unknown_peak_assignments)
    except TypeError:
        print("""The function correctly handled the error when a string
        was input instead of the unknown_peaks list""")

    try:
        peakidentify.plotting_peak_assignments(unknown_x, unknown_y,
                                               unknown_peaks, 3)
    except TypeError:
        print("""The function correctly handled the error when an int
        was input instead of the unknown_peak_assignments""")

    try:
        peakidentify.plotting_peak_assignments(unknown_x, unknown_y,
                                               unknown_peaks,
                                               ['WATER', 23, 'CO'])
    except TypeError:
        print("""The function correctly handled the case when an int
        was passed in the unknown_peak_assignment list""")
Exemplo n.º 5
0
def test_percentage_of_peaks_found():
    """This function tests the operation of the
    percentage_of_peaks_found function in peakidentify.py"""
    #First, generate good data.
    shoyu_data_dict = pickle.load(open('raman_spectra/shoyu_data_dict.p',
                                       'rb'))
    compound_1 = shoyu_data_dict['WATER']
    compound_2 = shoyu_data_dict['CARBON MONOXIDE']
    compound_3 = shoyu_data_dict['CARBON DIOXIDE']
    unknown_x, unknown_y = shoyu.combine_spectra(compound_1,
                                                 compound_2,
                                                 plot=False)
    unknown_x = np.asarray(unknown_x)
    unknown_y = np.asarray(unknown_y)
    known_compound_list = [compound_1, compound_2, compound_3]
    unknown_peaks = spectrafit.data_report(unknown_x, unknown_y)[0]
    known_peaks = []
    association_matrix = []
    for i, _ in enumerate(known_compound_list):
        known_peaks.append(
            spectrafit.compound_report(known_compound_list[i])[0])
        association_matrix.append(
            peakidentify.compare_unknown_to_known(unknown_peaks,
                                                  known_peaks[i], 0.03))

    #Test for input error handling.
    try:
        peakidentify.percentage_of_peaks_found(1, association_matrix,
                                               known_compound_list)
    except TypeError:
        print("""The function correctly handled the error when an int
        was input instead of the known_peaks list""")

    try:
        peakidentify.percentage_of_peaks_found(known_peaks, 1,
                                               known_compound_list)
    except TypeError:
        print("""The function correctly handled the error when an int
        was input instead of the association matrix""")

    try:
        peakidentify.percentage_of_peaks_found(known_peaks, association_matrix,
                                               'known_compound_list')
    except TypeError:
        print("""The function correctly handled the error when a string
        was input instead of the known_compound_list""")

    try:
        peakidentify.percentage_of_peaks_found(
            known_peaks, association_matrix,
            [compound_1, compound_2, 'compound_3'])
    except TypeError:
        print("""The function correctly handled the case where the compound
        list contains something that is not a compound""")

    #Test to make sure function returns a dictionary.
    assert isinstance(
        peakidentify.percentage_of_peaks_found(known_peaks, association_matrix,
                                               known_compound_list),
        dict), """The function is not
        returning a dictionary."""

    #Test for function output.
    water_peaks = spectrafit.compound_report(compound_1)[0]
    water_dict_0 = peakidentify.percentage_of_peaks_found([water_peaks],
                                                          [[0, 0, 0]],
                                                          [compound_1])
    assert water_dict_0['WATER'] == 0, """The function is not correctly
    calculating percentages when no peaks are found"""

    water_dict_1 = peakidentify.percentage_of_peaks_found([water_peaks],
                                                          [[1, 1, 1]],
                                                          [compound_1])
    assert water_dict_1['WATER'] == 100, """The function is not correctly
Exemplo n.º 6
0
def test_peak_position_comparisons():
    """This function tests the operation of the peak_position_comparisons
    function in peakidentify. Said function returns a list of strings that
    contain text assignments of each peak in the unknown spectrum."""

    #First, generate good data.
    shoyu_data_dict = pickle.load(open('raman_spectra/shoyu_data_dict.p',
                                       'rb'))
    compound_1 = shoyu_data_dict['WATER']
    compound_2 = shoyu_data_dict['CARBON MONOXIDE']
    compound_3 = shoyu_data_dict['CARBON DIOXIDE']
    unknown_x, unknown_y = shoyu.combine_spectra(compound_1,
                                                 compound_2,
                                                 plot=False)
    unknown_x = np.asarray(unknown_x)
    unknown_y = np.asarray(unknown_y)
    known_compound_list = [compound_1, compound_2, compound_3]
    unknown_peaks = spectrafit.data_report(unknown_x, unknown_y)[0]
    known_peaks = []
    association_matrix = []
    for i, _ in enumerate(known_compound_list):
        known_peaks.append(
            spectrafit.compound_report(known_compound_list[i])[0])
        association_matrix.append(
            peakidentify.compare_unknown_to_known(unknown_peaks,
                                                  known_peaks[i], 0.03))

    #Then, test error handling of bad inputs for the function.
    try:
        peakidentify.peak_position_comparisons(1, known_peaks,
                                               known_compound_list,
                                               association_matrix)
    except TypeError:
        print("An invalid unknown_peaks value was passed to the function, "
              "and was handled correctly.")

    try:
        peakidentify.peak_position_comparisons(unknown_peaks, 'known_peaks',
                                               known_compound_list,
                                               association_matrix)
    except TypeError:
        print("An invalid known_peaks value was passed to the function, "
              "and was handled correctly.")

    try:
        peakidentify.peak_position_comparisons(unknown_peaks, known_peaks,
                                               'known_compound_list',
                                               association_matrix)
    except TypeError:
        print(
            "An invalid known_compound_list value was passed to the function,"
            "and was handled correctly.")

    try:
        peakidentify.peak_position_comparisons(unknown_peaks, known_peaks,
                                               known_compound_list,
                                               'association_matrix')
    except TypeError:
        print("An invalid association_matrix value was passed to the function,"
              "and was handled correctly.")

    #Check to make sure the function is returning a list.
    assert isinstance(
        peakidentify.peak_position_comparisons(unknown_peaks, known_peaks,
                                               known_compound_list,
                                               association_matrix),
        list), "The function is not returning a list."

    #Test a call that says that no peaks have associations
    association_matrix_0 = []
    association_matrix_0.append(
        peakidentify.compare_unknown_to_known(known_peaks[0], known_peaks[1],
                                              0.03))
    zero_output = peakidentify.peak_position_comparisons(
        known_peaks[0], [known_peaks[1]], [compound_1],
        association_matrix_0)[0]
    assert zero_output[0] == 'Unassigned', """The function is not properly
    handling unassigned peaks."""

    #Test the function to make sure that it has the right functionality
    association_matrix = []
    #Generate a matrix with all associations equal to 1
    association_matrix.append(
        peakidentify.compare_unknown_to_known(known_peaks[0], known_peaks[0],
                                              0.03))
    #change the middle index to 0
    association_matrix[0][1] = 0
    test_peak_labels = peakidentify.peak_position_comparisons(
        known_peaks[0], [known_peaks[0]], [compound_1], association_matrix)
    assert test_peak_labels[0][0] == 'WATER', """The function is
    not correctly assigning peaks when association matrix = 1"""
    assert test_peak_labels[1][0] == 'Unassigned', """The function is
    not correctly handling a lack of peak assignments"""
    assert test_peak_labels[2][0] == 'WATER', """The funciton is