def test_attempted_overwrite():
    # TODO: test the case where this fails while everything else is valid
    data_pars = {
        'input_file': 'not/a/real/file.fits',
        'apply_cart_cuts': True,
    }
    data_pars['output_file'] = data_pars['input_file']
    try:
        datatool.prepare_data(data_pars)
    except UserWarning:
        pass
def test_convert_astro_only():
    data_pars = {
        'input_file': 'sample_data/sample_table_astro_only.fits',
        'convert_astrometry': True,
        'output_file': 'temp_data/astro_only_output.fits',
        'overwrite_datafile': True,
        'return_data_table': True,
    }
    result = datatool.prepare_data(data_pars)

    orig_table = Table.read(data_pars['input_file'])

    assert len(result) == len(orig_table)
    assert 'X' in result.colnames
Exemple #3
0
def test_get_region():
    """
    Test whether get_region applies data cut successfully.

    Synthesise two data sets, one which made up of Component A, and the other
    made up of Component A & B. Then, check if applying a get_region cut on
    the combined data set, with the Component A set as reference, only returns
    the Component A stars.
    """
    data_a_filename = 'temp_data/test_get_region_A.fits'
    synth_dataset_a = synthdata.SynthData(pars=PARS[0],
                                          starcounts=STARCOUNTS[0])
    np.random.seed(0)
    synth_dataset_a.synthesise_everything(filename=data_a_filename,
                                          overwrite=True)
    tabletool.convert_table_astro2cart(synth_dataset_a.table,
                                       write_table=True,
                                       filename=data_a_filename)

    data_both_filename = 'temp_data/test_get_region_both.fits'
    synth_dataset_both = synthdata.SynthData(pars=PARS, starcounts=STARCOUNTS)
    np.random.seed(0)
    synth_dataset_both.synthesise_everything(filename=data_both_filename,
                                             overwrite=True)

    # Prepare .par file
    par_file = 'temp_data/test_get_region.par'

    with open(par_file, 'w') as fp:
        fp.write('par_log_file = temp_data/test_get_region_pars.log\n')
        fp.write('input_file = {}\n'.format(data_both_filename))

        fp.write('convert_astrometry = True\n')

        fp.write('apply_cart_cuts = True\n')
        fp.write('cut_on_region = True\n')
        fp.write('cut_ref_table = {}\n'.format(data_a_filename))

        # fp.write('output_file = {}\n'.format())
        fp.write('return_data_table = True\n')

    # Apply datatool to synthetically generated dataset
    data_table = datatool.prepare_data(par_file)

    assert len(data_table) == len(synth_dataset_a.table)
def test_bg_overlaps():
    ref_table_filename = '../data/gaia_cartesian_full_6d_table.fits'
    ref_table = Table.read(ref_table_filename)

    # Only use subset
    sub_ref_table = ref_table[:100]
    data_pars = {
        'input_file': 'sample_data/sample_table_astro_only.fits',
        'convert_astrometry': True,
        'output_file': 'temp_data/astro_only_output.fits',
        'overwrite_datafile': True,
        'calc_overlaps': True,
        'bg_col_name': 'background_log_overlap',
        # 'bg_ref_table':'../data/gaia_cartesian_full_6d_table.fits',
        'bg_ref_table': sub_ref_table,
        'return_data_table': True,
    }

    result = datatool.prepare_data(data_pars)

    assert not np.any(np.isnan(result[data_pars['bg_col_name']]))
def test_cut_on_region():
    '''
    Sets a single star in the mock reference table to be the
    set around which to construct out 6D box.
    This has the effect of enforcing the dimensions of the box to be
    at most double the margins (totalling 60 pc in psoition and 10 km/s in
    velocity)
    '''

    ref_table = Table.read('sample_data/small_current_sample_table.fits')
    mock_assoc_colname = 'moving_groups'
    mock_assoc_label = 'bpmg'

    ref_table[mock_assoc_colname] = 10 * ' '
    ref_table[mock_assoc_colname][0] = mock_assoc_label

    data_pars = {
        'input_file': 'sample_data/sample_table_astro_only.fits',
        'convert_astrometry': True,
        'output_file': 'temp_data/astro_only_output.fits',
        'overwrite_datafile': True,
        'apply_cart_cuts': True,
        'cut_on_region': True,
        'cut_ref_table': ref_table,
        'cut_colname': mock_assoc_colname,
        'cut_assoc_name': mock_assoc_label,
        'return_data_table': True,
    }

    result = datatool.prepare_data(data_pars)

    pos_dims = 'XYZ'
    for pos_dim in pos_dims:
        assert np.max(result[pos_dim]) - np.min(result[pos_dim]) <= 60.

    vel_dims = 'UVW'
    for vel_dim in vel_dims:
        assert np.max(result[vel_dim]) - np.min(result[vel_dim]) <= 10.
def test_cut_on_bounds():
    '''
    Applies a data cut based on provided cartesian boundaries.
    '''
    DMIN = -10.
    DMAX = 50.

    data_pars = {
        'input_file': 'sample_data/sample_table_astro_only.fits',
        'convert_astrometry': True,
        'output_file': 'temp_data/astro_only_output.fits',
        'overwrite_datafile': True,
        'apply_cart_cuts': True,
        'cut_on_bounds': True,
        'cut_bound_min': [DMIN, DMIN, DMIN, DMIN, DMIN, DMIN],
        'cut_bound_max': [DMAX, DMAX, DMAX, DMAX, DMAX, DMAX],
        'return_data_table': True,
    }

    result = datatool.prepare_data(data_pars)

    for col in 'XYZUVW':
        assert np.min(result[col]) >= DMIN
        assert np.max(result[col]) <= DMAX
#! /usr/bin/env python
"""
A helper script that prepares data for input into Chronostar.
Accepts as a command line argument a path to a parameter file
(see README.md for how to structure the parameter file and
various available parameters).
"""

import os.path
import sys
sys.path.insert(0, '..')

from chronostar import datatool

if len(sys.argv) != 2:
    raise UserWarning('Incorrect usage. Path to parameter file is required'
                      ' as a single command line argument. e.g.\n'
                      '   > python prepare_data.py path/to/parsfile.par')

data_par_file = sys.argv[1]

if not os.path.isfile(data_par_file):
    raise UserWarning('Provided file does not exist')

datatool.prepare_data(data_par_file)
Exemple #8
0
def test_convert_astrometry():
    """
    Use a synethetically generated set of astrometry, convert
    to cartesian both manually, and via datatool

    In order to compare results, the datatool cartesian conversion
    will be stored in alternatively named columns
    """
    synth_table_filename = 'temp_data/test_convert_astrometry_data.fits'

    synth_dataset = synthdata.SynthData(pars=PARS, starcounts=STARCOUNTS)

    synth_dataset.synthesise_everything(filename=synth_table_filename,
                                        overwrite=True)

    tabletool.convert_table_astro2cart(synth_table_filename,
                                       write_table=True,
                                       filename=synth_table_filename)

    # Prepare a pars file
    par_file = 'temp_data/test_convert_astrometry.par'

    alt_cart_main_colnames = ['{}_alt'.format(dim) for dim in DIMS]
    alt_cart_error_colnames = ['{}_error_alt'.format(dim) for dim in DIMS]
    alt_cart_corr_colnames = []
    for i, colname1 in enumerate(DIMS):
        for colname2 in DIMS[i + 1:]:
            alt_cart_corr_colnames.append('{}_{}_corr_alt'.format(
                colname1, colname2))

    with open(par_file, 'w') as fp:
        fp.write('par_log_file = temp_data/test_convert_astrometry_pars.log\n')
        fp.write('input_file = {}\n'.format(synth_table_filename))

        fp.write('convert_astrometry = True\n')

        fp.write('{} = {}\n'.format('cart_main_colnames',
                                    alt_cart_main_colnames).replace("'", ''))
        fp.write('{} = {}\n'.format('cart_error_colnames',
                                    alt_cart_error_colnames).replace("'", ''))
        fp.write('{} = {}\n'.format('cart_corr_colnames',
                                    alt_cart_corr_colnames).replace("'", ''))

        fp.write('overwrite_datafile = True\n')
        fp.write('output_file = {}\n'.format(synth_table_filename))
        fp.write('return_data_table = True\n')

    # Apply datatool to synthetically generated dataset
    data_table = datatool.prepare_data(par_file)

    main_colnames, error_colnames, corr_colnames = tabletool.get_colnames(
        cartesian=True)

    for orig, alt in zip([main_colnames, error_colnames, corr_colnames], [
            alt_cart_main_colnames, alt_cart_error_colnames,
            alt_cart_corr_colnames
    ]):

        for orig_colname, alt_colname in zip(orig, alt):
            assert np.allclose(data_table[orig_colname],
                               data_table[alt_colname],
                               rtol=1e-5)
            print(
                np.max(
                    np.abs(data_table[alt_colname] -
                           data_table[orig_colname])))