示例#1
0
def fit(args):
    config_fp = args.config_fp
    config = get_config_parser()
    print('reading configuration file path: {}'.format(config_fp))
    with open(config_fp) as config_file:
        config.read_file(config_file)

    level = config.get('log', 'level', fallback='INFO')
    logging.basicConfig(level=logging.getLevelName(level))
    log = logging.getLogger(name=__name__)

    # log the configuration file
    with io.StringIO() as string_buffer:
        config.write(string_buffer)
        log.info('configuration file contents:\n{}'.format(
            string_buffer.getvalue()))

    try:
        fitter = build_fit_task(config)
        plots_pdf_file_dir = os.path.expanduser(
            config.get('output', 'plots_pdf_dir', fallback=None))
        if not os.path.exists(plots_pdf_file_dir):
            log.info('output directory "%s" will be created',
                     plots_pdf_file_dir)
            os.makedirs(plots_pdf_file_dir)
        fitter.fit_all(plots_pdf_dp=plots_pdf_file_dir)
    except ConfigurationFileError as e:
        log.error(e)
        log.exception('configuration file error in "%s"', config_fp)
        quit()

    table_file_path = os.path.expanduser(
        config.get('output', 'table_fp', fallback=None))
    if table_file_path:
        log.info(
            'writing table output to file path {}'.format(table_file_path))
        fitter.write_table(table_file_path)
    else:
        log.warning('No file path specified for table output')

        #plots_pdf_file_path = os.path.expanduser(config.get('output', 'plots_pdf_fp', fallback=None))
        #if plots_pdf_file_path:
        #    log.info('writing plots to PDF {}'.format(plots_pdf_file_path))
        #    fitter.draw_plots_matplotlib(plots_pdf_file_path)
        #    plots_html_file_path = os.path.splitext(plots_pdf_file_path)[0] + '.html'
        #    log.info('writing plots to HTML {}'.format(plots_html_file_path))
        #    fitter.draw_plots_bokeh(plots_html_file_path)
        #else:
        #    log.warning('No file path specified for plot output')

        best_fit_files_dir_path = os.path.expanduser(
            config.get('output', 'best_fit_files_dir', fallback=None))
        if best_fit_files_dir_path:
            if not os.path.expanduser(best_fit_files_dir_path):
                log.info('creating directory %s', best_fit_files_dir_path)
                os.makedirs(best_fit_files_dir_path)
            fitter.write_best_fit_arrays(best_fit_files_dir_path)
        else:
            log.warning('No directory specified for best fit files')
示例#2
0
def test_get_fit_parameters_from_config_file():
    fit_config = fit_task_builder.get_config_parser()
    fit_config.read_string("""\
[fit]    
max_component_count = 3
min_component_count = 1

""")
示例#3
0
def test_build_reference_spectrum_list_from_config_file(fs):
    reference_config = fit_task_builder.get_config_parser()
    reference_config.read_string("""\
[references]
references/*.e    
""")

    fs.CreateDirectory(directory_path='references')
    fs.CreateFile(file_path='references/arsenate_aqueous_avg_als_cal.e', contents=_spectrum_file_content)
    fs.CreateFile(file_path='references/arsenate_sorbed_anth_avg_als_cal.e', contents=_spectrum_file_content)

    reference_list = fit_task_builder.build_reference_spectrum_list_from_config_file(reference_config)

    assert len(reference_list) == 2
示例#4
0
def test_build_unknown_spectrum_list_from_config_file(fs):
    data_config = fit_task_builder.get_config_parser()
    data_config.read_string("""\
[data]
data/*.e
""")

    fs.CreateDirectory(directory_path='data')
    fs.CreateFile(file_path='data/data_0.e', contents=_spectrum_file_content)
    fs.CreateFile(file_path='data/data_1.e', contents=_spectrum_file_content)

    data_list = fit_task_builder.build_unknown_spectrum_list_from_config_file(data_config)

    assert len(data_list) == 2
示例#5
0
def test_build_reference_spectrum_list_from_prm_section__bad_component_counts(fs):
    reference_config = fit_task_builder.get_config_parser()
    reference_config.read_string("""\
[prm]
NBCompoMax = 1
NBCompoMin = 4
arsenate_aqueous_avg_als_cal.e
arsenate_sorbed_anth_avg_als_cal.e
""")

    fs.CreateFile(file_path='arsenate_aqueous_avg_als_cal.e', contents=_spectrum_file_content)
    fs.CreateFile(file_path='arsenate_sorbed_anth_avg_als_cal.e', contents=_spectrum_file_content)

    with pytest.raises(fit_task_builder.ConfigurationFileError):
        fit_task_builder.build_reference_spectrum_list_from_config_prm_section(reference_config)
示例#6
0
def test__get_required_config_value():
    config = fit_task_builder.get_config_parser()

    test_section = 'blah'
    test_option = 'bleh'
    test_value = 'blih'

    config.add_section(section=test_section)
    config.set(section=test_section, option=test_option, value=test_value)

    assert test_value == fit_task_builder._get_required_config_value(
        config=config, section=test_section, option=test_option)

    with pytest.raises(fit_task_builder.ConfigurationFileError):
        fit_task_builder._get_required_config_value(config=config, section='missing', option='missing')
示例#7
0
def test_build_reference_spectrum_list_from_prm_section(fs):
    reference_config = fit_task_builder.get_config_parser()
    reference_config.read_string("""\
[prm]
NBCompoMax = 4
NBCompoMin = 1
arsenate_aqueous_avg_als_cal.e
arsenate_sorbed_anth_avg_als_cal.e
""")

    fs.CreateFile(file_path='arsenate_aqueous_avg_als_cal.e', contents=_spectrum_file_content)
    fs.CreateFile(file_path='arsenate_sorbed_anth_avg_als_cal.e', contents=_spectrum_file_content)

    max_count, min_count, reference_list = \
        fit_task_builder.build_reference_spectrum_list_from_config_prm_section(reference_config)

    assert max_count == 4
    assert min_count == 1
    assert len(reference_list) == 2
示例#8
0
def test_main(fs):

    # write the necessary input files:
    #   configuration file
    #   reference files
    #   sample files
    #   prm file
    # then check for the expected output files
    #   plot pdf
    #   table txt
    #   sample fit files

    fs.CreateFile(
        'ref_1.e', '''\
1000.0\t0.01
1000.1\t0.02
1000.2\t0.03
1000.3\t0.04
1000.4\t0.05
''')

    fs.CreateFile(
        'ref_2.e', '''\
1000.0\t0.05
1000.1\t0.04
1000.2\t0.03
1000.3\t0.02
1000.4\t0.01
''')

    fs.CreateFile(
        'ref_3.e', '''\
1000.0\t0.01
1000.1\t0.01
1000.2\t0.01
1000.3\t0.01
1000.4\t0.01
''')

    fs.CreateFile(
        'test_main.prm', '''\
NbCompoMax=3
NbCompoMin=1
ref=ref_1.e
ref=ref_2.e
ref=ref_3.e
''')

    # sample 1 is twice ref_1
    fs.CreateFile('sample_1.e', '''\
1000.1\t0.02
1000.2\t0.04
1000.3\t0.06
''')

    # sample 2 is half ref_2
    fs.CreateFile('sample_2.e', '''\
1000.1\t0.015
1000.2\t0.010
1000.3\t0.005
''')

    # sample 3 is ref_1 plus ref_3
    fs.CreateFile('sample_3.e', '''\
1000.1\t0.03
1000.2\t0.04
1000.3\t0.05
''')

    test_main_config = get_config_parser()
    fs.CreateFile(
        'test_main.cfg', '''\
[reference_spectra]
prm = test_main.prm

[data]
sample*.e

[output]
best_fit_files_dir = .
plots_pdf_fp = test_main_plots.pdf
table_fp = test_main_table.txt
reference_plots_pdf = test_main_reference_plots.pdf
''')

    result = cli(['test_main.cfg'])

    assert result.exit_code == 0
    assert os.path.exists('test_main_plots.pdf')
    assert os.path.exists('test_main_table.txt')
    assert os.path.exists('sample_1_fit.txt')