Ejemplo n.º 1
0
def test_arsenic_1(caplog, request):
    """
    Test fits for known arsenic data and reference_spectra.
    Expect to find PRM, data, and reference files in a directory called 'test_arsenic_fit'.
    See also: http://stackoverflow.com/questions/29627341/pytest-where-to-store-expected-data.

    :param request: pytest fixture with information about the path to this test file
    :return:
    """

    caplog.set_level(logging.INFO)

    test_arsenic_fit_fp = request.module.__file__
    log.info('test_arsenic_fit_fp: {}'.format(test_arsenic_fit_fp))
    test_arsenic_fit_dir_path, _ = os.path.splitext(test_arsenic_fit_fp)

    #reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path, 'reference', 'arsenate_*.e')
    reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path,
                                               'reference', '*.e')
    data_file_path = os.path.join(test_arsenic_fit_dir_path, 'data',
                                  'OTT3_55_spot0.e')

    reference_spectrum_list = [
        ReferenceSpectrum.read_file(file_path)
        for file_path in glob(reference_file_path_pattern)
    ]
    log.info(reference_spectrum_list)

    unknown_spectrum = Spectrum.read_file(data_file_path)
    log.info(unknown_spectrum)

    task = AllCombinationFitTask(
        ls=LinearRegression,
        energy_range_builder=AdaptiveEnergyRangeBuilder(),
        reference_spectrum_list=reference_spectrum_list,
        unknown_spectrum_list=[
            unknown_spectrum,
        ],
        best_fits_plot_limit=1,
        component_count_range=range(1, 3 + 1))

    with tempfile.TemporaryDirectory() as plots_pdf_dp:
        task.fit_all(plots_pdf_dp=plots_pdf_dp)

        unknown_spectrum_fit = task.fit_table[unknown_spectrum]

        assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.fit_spectrum_b.shape
        assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.unknown_spectrum_b.shape
        assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.residuals.shape

        assert 3 == len(unknown_spectrum_fit.best_fit.reference_spectra_seq)
Ejemplo n.º 2
0
def test_arsenic_2(caplog, request):
    """
    Test fits for a single reference against all reference_spectra..
    Expect to find PRM, data, and reference files in a directory called 'test_arsenic_fit'.
    See also: http://stackoverflow.com/questions/29627341/pytest-where-to-store-expected-data.

    This test is not reliable. Fix it.

    :param request: pytest fixture with information about the path to this test file
    :return:
    """

    caplog.set_level(logging.INFO)

    test_arsenic_fit_fp = request.module.__file__
    log.info('test_arsenic_fit_fp: {}'.format(test_arsenic_fit_fp))
    test_arsenic_fit_dir_path, _ = os.path.splitext(test_arsenic_fit_fp)

    #reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path, 'reference', 'arsenate_*.e')
    reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path,
                                               'reference', '*.e')
    #data_file_path = os.path.join(test_arsenic_fit_dir_path, 'reference', 'arsenate_aqueous_avg_als_cal.e')

    reference_spectrum_list = [
        ReferenceSpectrum.read_file(file_path)
        for file_path in glob(reference_file_path_pattern)
    ]
    log.info(reference_spectrum_list)

    unknown_spectrum = reference_spectrum_list[0]
    log.info(unknown_spectrum)

    task = AllCombinationFitTask(
        ls=LinearRegression,
        energy_range_builder=FixedEnergyRangeBuilder(energy_start=11850.0,
                                                     energy_stop=12090.0),
        reference_spectrum_list=reference_spectrum_list,
        unknown_spectrum_list=[unknown_spectrum],
        best_fits_plot_limit=1,
        component_count_range=range(1, 3 + 1))

    with tempfile.TemporaryDirectory() as plots_pdf_dp:
        task.fit_all(plots_pdf_dp=plots_pdf_dp)

        unknown_spectrum_fit = task.fit_table[unknown_spectrum]

        best_fit_ref_count = len(
            unknown_spectrum_fit.best_fit.reference_spectra_seq)
        assert 2 <= best_fit_ref_count <= 3
Ejemplo n.º 3
0
def test_arsenic_1(caplog, request):
    """
    Test fits for known arsenic data and reference_spectra.
    Expect to find PRM, data, and reference files in a directory called 'test_arsenic_fit'.
    See also: http://stackoverflow.com/questions/29627341/pytest-where-to-store-expected-data.

    :param request: pytest fixture with information about the path to this test file
    :return:
    """

    caplog.setLevel(logging.INFO)

    test_arsenic_fit_fp = request.module.__file__
    log.info('test_arsenic_fit_fp: {}'.format(test_arsenic_fit_fp))
    test_arsenic_fit_dir_path, _ = os.path.splitext(test_arsenic_fit_fp)

    #reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path, 'reference', 'arsenate_*.e')
    reference_file_path_pattern = os.path.join(test_arsenic_fit_dir_path, 'reference', '*.e')
    data_file_path = os.path.join(test_arsenic_fit_dir_path, 'data', 'OTT3_55_spot0.e')

    reference_spectrum_list = [
        ReferenceSpectrum.read_file(file_path)
        for file_path
        in glob(reference_file_path_pattern)
    ]
    log.info(reference_spectrum_list)

    unknown_spectrum = Spectrum.read_file(data_file_path)
    log.info(unknown_spectrum)

    task = AllCombinationFitTask(
        energy_range_builder=AdaptiveEnergyRangeBuilder(),
        reference_spectrum_list=reference_spectrum_list,
        unknown_spectrum_list=[unknown_spectrum, ],
        component_count_range=range(1, 3+1)
    )

    task.fit_all()

    unknown_spectrum_fit = task.fit_table[unknown_spectrum]

    assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.fit_spectrum_b.shape
    assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.unknown_spectrum_b.shape
    assert unknown_spectrum_fit.best_fit.interpolant_incident_energy.shape == unknown_spectrum_fit.best_fit.residuals.shape

    assert 3 == len(unknown_spectrum_fit.best_fit.reference_spectra_seq)
Ejemplo n.º 4
0
def build_reference_spectrum_list_from_config_file(config):
    """
    Read reference spectrum file glob(s) from configuration file to create
    and return a list of ReferenceSpectrum instances.

    :param config: configparser instance
    :return: list of ReferenceSpectrum instances
    """
    log = logging.getLogger(name=__name__)
    references = config.items('references')
    log.debug(references)
    reference_spectrum_list, _ = ReferenceSpectrum.read_all(
        [os.path.expanduser(reference_file_glob) for reference_file_glob, _ in references])

    if len(reference_spectrum_list) == 0:
        raise ConfigurationFileError('no reference spectrum files were found using globs "{}"'.format(references))
    else:
        return reference_spectrum_list
Ejemplo n.º 5
0
    def build_reference_spectrum_list_from_config_file(cls, config):
        """
        Read reference spectrum file glob(s) from configuration file to create
        a list of ReferenceSpectrum instances, maximum component count, and
        minimum component count.

        :param config: configparser instance
        :return: list of ReferenceSpectrum instances
        """

        reference_spectrum_list, reference_spectrum_file_glob_list = ReferenceSpectrum.read_all(
            [reference_file_glob for reference_file_glob, _ in config.items('references')]
        )

        if len(reference_spectrum_file_glob_list) == 0:
            logging.exception(
                'no reference spectrum file paths or patterns were found in section [references] of configuration file {}'.format(
                    config
                )
            )
        elif len(reference_spectrum_list) == 0:
            logging.exception('no reference spectrum files were found')
        else:
            # everything is ok
            pass

        if not config.has_option('fit', 'maximum_component_count'):
            logging.exception(
                'required parameter maximum_component_count is missing from section [fit] in configuration file {}'.format(
                    config
                )
            )
        elif not config.has_option('fit', 'minimum_component_count'):
            logging.exception(
                'required parameter minimum_component_count is missing from section [fit] in configuration file {}'.format(
                    config
                )
            )
        else:
            maximum_component_count = config.getint('fit', 'maximum_component_count')
            minimum_component_count = config.getint('fit', 'minimum_component_count')

        return reference_spectrum_list, maximum_component_count, minimum_component_count
Ejemplo n.º 6
0
    def build_reference_spectrum_list_from_prm_file(cls, prm_file_path):
        """
        Read a PRM file to create a list of ReferenceSpectrum
        instances, maximum component count, and minimum component
        count from a PRM file.

        :param prm_file_path:
        :return:
            list of ReferenceSpectrum instances
            maximum component count
            minimum component count
        """
        reference_spectrum_list = []
        log.info('reading PRM file {}'.format(prm_file_path))
        prm = PRM.read_prm(prm_file_path)
        # read reference files
        for i, fp in enumerate(prm.reference_file_path_list):
            log.info('reading reference file {}: {}'.format(i, fp))
            reference_spectrum = ReferenceSpectrum.read_file(fp)
            reference_spectrum_list.append(reference_spectrum)

        return reference_spectrum_list, prm.nb_component_max, prm.nb_component_min
Ejemplo n.º 7
0
def build_reference_spectrum_list_from_prm_file(prm_file_path):
    """
    Read a PRM file to create a list of ReferenceSpectrum
    instances, maximum component count, and minimum component
    count from a PRM file.

    :param prm_file_path:
    :return:
        list of ReferenceSpectrum instances
        maximum component count
        minimum component count
    """
    log = logging.getLogger(name=__file__)
    reference_spectrum_list = []
    log.info('reading PRM file {}'.format(prm_file_path))
    prm = PRM.read_prm(prm_file_path)
    # read reference files
    for i, fp in enumerate(prm.reference_file_path_list):
        log.info('reading reference file {}: {}'.format(i, fp))
        reference_spectrum = ReferenceSpectrum.read_file(fp)
        reference_spectrum_list.append(reference_spectrum)

    return reference_spectrum_list, prm.nb_component_max, prm.nb_component_min
Ejemplo n.º 8
0
def build_reference_spectrum_list_from_config_prm_section(config):
    log = logging.getLogger(name=__name__)

    max_component_count = int(_get_required_config_value(config, 'prm', 'NBCompoMax'))
    min_component_count = int(_get_required_config_value(config, 'prm', 'NBCompoMin'))
    reference_spectrum_list = [
        ReferenceSpectrum.read_file(file_path_or_buffer=option_name)
        for option_name, option_value
        in _get_required_config_value_list(config, 'prm')
        if len(option_value) == 0]

    log.debug('NBCompoMax: %d', max_component_count)
    log.debug('NBCompoMin: %d', min_component_count)
    log.debug('Reference list length:\n  %d', len(reference_spectrum_list))

    if min_component_count <= 0:
        raise ConfigurationFileError('NBCompoMin must be greater than zero, not "{}"'.format(min_component_count))
    elif max_component_count <= 0:
        raise ConfigurationFileError('NBCompoMax must be greater than zero, not "{}"'.format(max_component_count))
    elif min_component_count > max_component_count:
        raise ConfigurationFileError(
            'NBCompoMin "{}" is greater than NBCompoMax "{}"'.format(min_component_count, max_component_count))
    else:
        return max_component_count, min_component_count, reference_spectrum_list