Ejemplo n.º 1
0
def test_add_calibration():
    """
    A function that tests the `add_calibration` function from dataprep. It first tests that no
    errors occur when the function is run before testing the output to ensure that the calibration
    compound was sucessfully added and labeled appropriately. It checks that the proper number of
    peaks were saved as well as the wavenumber, counts, and residuals. It tests both the custom and
    automatic labeling functionality before finally ensuring that input errors are handled well.
    """
    # check to ensure that the test file does not already exist and remove if it does
    if os.path.exists('test.hdf5'):
        os.remove('test.hdf5')
    else:
        pass
    dataprep.new_hdf5('test')
    dataprep.add_calibration(
        'test.hdf5',
        'ramandecompy/tests/test_files/Methane_Baseline_Calibration.xlsx',
        label='Methane')
    cal_file = h5py.File('test.hdf5', 'r')
    assert list(
        cal_file.keys())[0] == 'Methane', 'custom label not applied correctly'
    assert len(
        cal_file) == 1, 'more than one first order group assigned to test.hdf5'
    assert len(cal_file['Methane']) == 4, 'more then 1 peak was stored'
    assert 'Methane/wavenumber' in cal_file, 'x data (wavenumber) not stored correctly'
    assert 'Methane/counts' in cal_file, 'y data (counts) not stored correctly'
    assert 'Methane/residuals' in cal_file, 'residuals not stored correctly'
    # test that function assigns filename correctly as compound label
    dataprep.new_hdf5('test1')
    dataprep.add_calibration(
        'test1.hdf5',
        'ramandecompy/tests/test_files/Methane_Baseline_Calibration.xlsx')
    cal_file1 = h5py.File('test1.hdf5', 'r')
    assert list(cal_file1.keys())[0] == 'Methane_Baseline_Calibration', """
    filename label not applied correctly"""
    # test inputs
    try:
        dataprep.add_calibration(
            4.2, """ramandecompy/tests/test_files/
        CarbonMonoxide_Baseline_Calibration.xlsx""")
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_calibration('test.hdp5', 4.2)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_calibration(
            'test.txt', """ramandecompy/tests/
        test_files/CarbonMonoxide_Baseline_Calibration""")
    except TypeError:
        print(
            'A .txt file was passed to the function, and it was handled will with a TypeError.'
        )
    os.remove('test.hdf5')
    os.remove('test1.hdf5')
Ejemplo n.º 2
0
def test_add_experiment():
    """
    A function that tests the `add_experiment` function from dataprep. It first tests that no
    errors occur when the function is run before testing the output to ensure that the
    experimental data was sucessfully added and labeled appropriately. It checks that the
    proper number of peaks were saved as well as the wavenumber, counts, and residuals.
    Lastly it ensures that input errors are handled well.
    """
    # check to ensure that the test file does not already exist and remove if it does
    if os.path.exists('exp_test_1.hdf5'):
        os.remove('exp_test_1.hdf5')
    else:
        pass
    dataprep.new_hdf5('exp_test_1')
    dataprep.add_experiment(
        'exp_test_1.hdf5',
        'ramandecompy/tests/test_files/FA_3.6wt%_300C_25s.csv')
    exp_file = h5py.File('exp_test_1.hdf5', 'r')
    # test generated file
    assert len(exp_file) == 1, 'incorrect number of 1st order groups'
    assert list(exp_file.keys())[0] == '300C', '1st order group name incorrect'
    assert len(exp_file['300C']) == 1, 'incorrect number of 2nd order groups'
    assert list(
        exp_file['300C'].keys())[0] == '25s', '2nd order group name incorrect'
    assert '300C/25s/wavenumber' in exp_file, 'x data (wavenumber) not stored correctly'
    assert '300C/25s/counts' in exp_file, 'y data (counts) not stored correctly'
    assert '300C/25s/residuals' in exp_file, 'residuals not stored correctly'
    assert len(exp_file['300C/25s']
               ) == 19, 'incorrect number of peaks + raw_data stored'
    # test inputs
    try:
        dataprep.add_experiment(
            4.2, """ramandecompy/tests/test_files/
        CarbonMonoxide_Baseline_Calibration.xlsx""")
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_experiment('exp_test_1.hdp5', 4.2)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_experiment(
            'exp_test_1.txt', """ramandecompy/tests/
        test_files/CarbonMonoxide_Baseline_Calibration""")
    except TypeError:
        print(
            'A .txt file was passed to the function, and it was handled will with a TypeError.'
        )
    os.remove('exp_test_1.hdf5')
def test_data_import():
    """
    This function tests the operation of the peak_assignment
    function in peakidentify.py
    """
    dataprep.new_hdf5('exp_test_3')
    dataprep.add_experiment(
        'exp_test_3.hdf5',
        'ramandecompy/tests/test_files/FA_3.6wt%_300C_25s.csv')
    exp_file = h5py.File('exp_test_3.hdf5', 'r')
    # test generated file
    assert len(exp_file) == 1, 'incorrect number of 1st order groups'
    assert list(exp_file.keys())[0] == '300C', '1st order group name incorrect'
    assert len(exp_file['300C']) == 1, 'incorrect number of 2nd order groups'
    assert list(
        exp_file['300C'].keys())[0] == '25s', '2nd order group name incorrect'
    assert '300C/25s/wavenumber' in exp_file, 'x data (wavenumber) not stored correctly'
    assert '300C/25s/counts' in exp_file, 'y data (counts) not stored correctly'
    assert len(exp_file['300C/25s']
               ) == 19, 'incorrect number of peaks + raw_data stored'
    # test inputs
    try:
        dataprep.add_experiment(
            4.2,
            'ramandecompy/tests/test_files/CarbonMonoxide_Baseline_Calibration.xlsx'
        )
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_experiment('exp_test_3.hdp5', 4.2)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.add_experiment(
            'exp_test_3.txt',
            'ramandecompy/tests/test_files/CarbonMonoxide_Baseline_Calibration'
        )
    except TypeError:
        print(
            'A .txt file was passed to the function, and it was handled will with a TypeError.'
        )
    os.remove('exp_test_3.hdf5')
Ejemplo n.º 4
0
def test_new_hdf5():
    """
    A function that tests that there are no errors in the `new_hdf5` function from dataprep.
    """
    # check to ensure that the test file does not already exist and remove if it does
    if os.path.exists('function_test.hdf5'):
        os.remove('function_test.hdf5')
    else:
        pass
    dataprep.new_hdf5('function_test')
    # test inputs
    try:
        dataprep.new_hdf5(4.2)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    os.remove('function_test.hdf5')
def data_import(hdf5_filename, directory):
    """
    This function adds Raman experimental data to an existing hdf5 file. It uses the
    spectrafit.fit_data function to fit the data before saving the fit result and
    the raw data to the hdf5 file. The data_filename must be in a standardized format
    to interact properly with this function. It must take the form anyname_temp_time.xlsx
    (or .csv) since this function will parse the the temp and time from the filename to
    label the data and fit result in the hdf5 file.

    Args:
        hdf5_filename (str): the filename and location of an existing hdf5 file to add the
                             experiment data too. Variable must be in a string format.



        directory (str): the folder location of raw Raman spectroscopy data in a 
                             string format.

    Returns:
        None
    """
    # open hdf5 file as read/write
    dataprep.new_hdf5(hdf5_filename)
    exp_file = h5py.File(hdf5_filename+'.hdf5', 'r+')
    for filename in os.listdir(directory):
        if filename.startswith('FA_') and filename.endswith('.csv'):
            locationandfile = directory + filename
            dataprep.add_experiment(str(hdf5_filename)+'.hdf5', locationandfile)
            print('Data from {} fit with compound pseudo-Voigt model. Results saved to {}.'.format(filename, hdf5_filename))
            # printing out to user the status of the import (because it can take a long time if importing a lot of data,
            # about minute/data set for test files
            exp_file.close()
            continue
        else:
            print('Data from {} fit with compound pseudo-Voigt model. Results saved to {}.'.format(filename, hdf5_filename))
            exp_file.close()
            continue
    return
Ejemplo n.º 6
0
def test_adjust_peaks():
    """
    A function that tests the `adjust_peaks` function from dataprep. The function first looks to
    see that no errors occur when running the function before then checking to ensure that input
    errors are handled well.
    """
    # check to ensure that the test file does not already exist and remove if it does
    if os.path.exists('exp_test_2.hdf5'):
        os.remove('exp_test_2.hdf5')
    else:
        pass
    # generate test hdf5 file
    dataprep.new_hdf5('exp_test_2')
    dataprep.add_experiment(
        'exp_test_2.hdf5',
        'ramandecompy/tests/test_files/FA_3.6wt%_300C_25s.csv')
    # peaks to add and drop form auto-fitting
    add_list = [1270, 1350, 1385]
    drop_list = ['Peak_01']
    dataprep.adjust_peaks('exp_test_2.hdf5', '300C/25s', add_list, drop_list)
    try:
        dataprep.adjust_peaks(4.2,
                              '300C/25s',
                              add_list,
                              drop_list,
                              plot_fits=True)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.adjust_peaks('exp_test_2.txt',
                              '300C/25s',
                              add_list,
                              drop_list,
                              plot_fits=True)
    except TypeError:
        print(
            'A .txt was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.adjust_peaks('exp_test_2.hdf5', ['300C/25s', '400C/25s'],
                              add_list,
                              drop_list,
                              plot_fits=True)
    except TypeError:
        print(
            'A list was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.adjust_peaks('exp_test_2.hdf5',
                              '300C/25s',
                              'add_list',
                              drop_list,
                              plot_fits=True)
    except TypeError:
        print(
            'A str was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.adjust_peaks('exp_test_2.hdf5',
                              '300C/25s',
                              add_list,
                              'drop_list',
                              plot_fits=True)
    except TypeError:
        print(
            'A str was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        dataprep.adjust_peaks('exp_test_2.hdf5',
                              '300C/25s',
                              add_list,
                              drop_list,
                              plot_fits=3)
    except TypeError:
        print(
            'An int was passed to the function, and it was handled well with a TypeError.'
        )
    os.remove('exp_test_2.hdf5')
def test_interpolatedfit():
    """
    This function tests the operation of the interpolatedfit
    function in interpolatespectra.py
    """
    dataprep.new_hdf5(
        'ramandecompy/tests/test_files/interpolated_spectra_calibration_file')
    spectra_count = 2
    # get list of compounds from hdf5 file
    y_data_list = []
    x_data_list = []
    hdf5 = h5py.File(HDF5_CALFILENAME, 'r')
    compound_list = list(HDF5_2.keys())
    hdf5.close()
    key = 'water'
    for _, target_compound in enumerate(compound_list):
        x_data, y_data, labels = interpolatespectra.generate_spectra_dataset(
            HDF5_CALFILENAME, target_compound, spectra_count)
        y_data_list.append(y_data)
        x_data_list.append(x_data)
    # example i and label value
    i = 1
    label = 1
    # Run function
    for j, label_test in enumerate(labels):
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, x_data,
                                           y_data, j)
    # test inputs
    try:
        interpolatespectra.interpolatedfit(4.2, key, x_data, y_data, i)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit('test.txt', key, x_data, y_data, i)
    except TypeError:
        print(
            'A .txt was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, 4.2, x_data,
                                           y_data, i)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, 'test.txt',
                                           x_data, y_data, i)
    except TypeError:
        print(
            'A .txt was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, 4.2,
                                           y_data, i)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, 'x_data',
                                           y_data, i)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, x_data,
                                           4.2, i)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, x_data,
                                           'y_data', i)
    except TypeError:
        print(
            'A .txt was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, x_data,
                                           y_data, 4.2)
    except TypeError:
        print(
            'A float was passed to the function, and it was handled well with a TypeError.'
        )
    try:
        interpolatespectra.interpolatedfit(HDF5_INTERPFILENAME, key, x_data,
                                           y_data, 'num')
    except TypeError:
        print(
            'A .txt was passed to the function, and it was handled well with a TypeError.'
        )
    os.remove(
        'ramandecompy/tests/test_files/interpolated_spectra_calibration_file.hdf5'
    )