Example #1
0
def test_polyfit():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, diff=True)

    ins = INS(ss, order=1)

    # Mock some data for which the polyfit is exact
    x = np.arange(1, ins.Ntimes + 1)
    for ind in range(ins.Nfreqs):
        ins.metric_array[:, ind, 0] = 3 * x + 5
    ins.metric_array.mask = np.zeros(ins.metric_array.shape, dtype=bool)
    ins.weights_array = np.ones(ins.metric_array.shape)
    ins.weights_square_array = np.copy(ins.weights_array)
    ins.metric_ms, coeffs = ins.mean_subtract(return_coeffs=True)
    test_coeffs = np.zeros((ins.order + 1, ) + ins.metric_ms.shape[1:])
    test_coeffs[0, :] = 3
    test_coeffs[1, :] = 5

    assert np.all(np.allclose(ins.metric_ms, np.zeros(
        ins.metric_ms.shape))), "The polyfit was not exact"
    assert np.all(np.allclose(
        coeffs, test_coeffs)), "The polyfit got the wrong coefficients"

    ins.metric_array[:] = np.ma.masked
    ins.metric_ms = ins.mean_subtract()
    assert np.all(ins.metric_ms.mask), "The metric_ms array was not all masked"
Example #2
0
def test_init():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, flag_choice='original', diff=True)
    # Needs to be in time order for averaging comparison to work
    ss.reorder_blts(order='time')

    ins = INS(ss)

    # Mock the averaging method
    new_shape = [ss.Ntimes, ss.Nbls, ss.Nfreqs, ss.Npols]
    test_dat = np.mean(np.abs(ss.data_array).reshape(new_shape), axis=1)

    # Mock the weights array
    test_weights = np.sum(np.logical_not(
        ss.data_array.mask).reshape(new_shape),
                          axis=1)

    # Check that the data array averaged correctly
    # Weights are floating-point, which introdices itty bitty errors compared to masked average.
    assert np.all(np.isclose(test_dat, ins.metric_array, rtol=1e-6,
                             atol=1e-7)), "Averaging did not work as intended."
    # Check that the weights summed correctly
    assert np.all(
        test_weights == ins.weights_array), "Weights did not sum properly"
Example #3
0
def test_rev_ind():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile)

    # Make a band that will pick out only the largest value in the data
    dat_sort = np.sort(np.abs(ss.data_array), axis=None)
    band = [0.5 * (dat_sort[-2] + dat_sort[-1]), dat_sort[-1] + 1]

    # Find the indices of this data point
    ind = np.unravel_index(
        np.absolute(ss.data_array).argmax(), ss.data_array.shape)
    # Convert the blt to a time index
    t = ind[0] // ss.Nbls
    f = ind[2]
    p = ind[3]

    # Make the waterfall histogram
    wf_hist = ss.rev_ind(band)

    # Check that it picked up that point
    assert wf_hist[t, f, p] == 1, "The algorithm did not find the data point"

    # Check no other points were picked up
    assert np.count_nonzero(wf_hist) == 1, "The algorithm found other data"
def test_mask_to_flags():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile)

    ins = INS(ss)
    freq_inds_1 = np.arange(0, len(ins.freq_array), 2)
    freq_inds_2 = np.arange(1, len(ins.freq_array), 2)
    ins.metric_array[1, freq_inds_1] = np.ma.masked
    ins.metric_array[3, freq_inds_1] = np.ma.masked
    ins.metric_array[7, freq_inds_2] = np.ma.masked
    ins.metric_array[-2, freq_inds_2] = np.ma.masked
    flags = ins.mask_to_flags()

    test_flags = np.zeros(flags.shape, dtype=bool)
    test_flags[1:5, freq_inds_1] = True
    test_flags[7, freq_inds_2] = True
    test_flags[8, freq_inds_2] = True
    test_flags[-3:-1, freq_inds_2] = True

    assert np.all(
        flags == test_flags), "Test flags were not equal to calculated flags."
Example #5
0
def test_combine_ins_use_nsample():
    obs = "1061313128_99bl_1pol_half_time"
    testfile = os.path.join(DATA_PATH, f"{obs}.uvfits")

    ss = SS()
    ss.read(testfile, diff=True)

    whole_ins = INS(ss, use_integration_weights=True)

    all_bls = ss.get_antpairs()
    first_50 = all_bls[:50]
    remaining = all_bls[50:]

    ss_first_50 = ss.select(bls=first_50, inplace=False)
    ss_remaining = ss.select(bls=remaining, inplace=False)

    ins_first_50 = INS(ss_first_50, use_integration_weights=True)
    ins_remaining = INS(ss_remaining, use_integration_weights=True)

    test_ins = util.combine_ins(ins_first_50, ins_remaining)

    # Metric arrays are off by 10^-14 (1 part in 10^16)
    for attr in whole_ins._data_params:
        assert np.all(
            np.isclose(getattr(whole_ins, attr), getattr(
                test_ins, attr))), f"{attr} is not equal between the two INS"
Example #6
0
def test_VDH_plot():

    matplotlib = pytest.importorskip("matplotlib")

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    outdir = os.path.join(DATA_PATH, 'test_plots')

    prefix = '%s/%s' % (outdir, obs)
    outfile = '%s_VDH.pdf' % prefix

    dens_prefix = '%s/%s_dens' % (outdir, obs)
    dens_outfile = '%s_VDH.pdf' % dens_prefix

    ss = SS()
    ss.read(testfile, flag_choice='original', diff=True)

    cp.VDH_plot(ss, prefix, backend='Agg')
    # Test with density prefix and error bars
    cp.VDH_plot(ss,
                dens_prefix,
                density=True,
                error_sig=1,
                ylim=[1e-5, 1e5],
                pre_model_label='model label')

    assert os.path.exists(outfile), "The first plot was not made"
    assert os.path.exists(dens_outfile), "The second plot was not made"

    os.remove(outfile)
    os.remove(dens_outfile)
    os.rmdir(outdir)
Example #7
0
def test_write():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'
    outfile = os.path.join(DATA_PATH, 'test_write.uvfits')

    ss = SS()
    ss.read(testfile)

    custom = np.zeros_like(ss.data_array.mask)
    custom[:ss.Nbls] = 1

    # Flags the first time and no others
    ss.apply_flags(flag_choice='custom', custom=custom)

    # Write this out without combining flags
    ss.write(outfile, 'uvfits', filename_in=testfile, combine=False)

    # Check if the flags propagated correctly
    UV = UVData()
    UV.read(outfile)
    assert np.all(
        UV.flag_array[:2 * UV.Nbls]), "Not all expected flags were propagated"
    assert not np.any(
        UV.flag_array[2 * UV.Nbls:]), "More flags were made than expected"
    os.remove(outfile)
def test_init():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, flag_choice='original')

    ins = INS(ss)

    # Mock the averaging method
    new_shape = [ss.Ntimes, ss.Nbls, ss.Nfreqs, ss.Npols]
    test_dat = np.mean(np.abs(ss.data_array).reshape(new_shape), axis=1)

    # Mock the weights array
    test_weights = np.sum(np.logical_not(
        ss.data_array.mask).reshape(new_shape),
                          axis=1)

    # Check that the data array averaged correctly
    assert np.all(
        test_dat == ins.metric_array), "Averaging did not work as intended."
    # Check that the weights summed correctly
    assert np.all(
        test_weights == ins.weights_array), "Weights did not sum properly"
Example #9
0
def test_no_cross_auto_spectrum():
    obs = "1061312640_autos"
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')

    ss = SS()
    ss.read(testfile, diff=True)

    with pytest.raises(ValueError, match="Requested spectrum type is 'cross', but no cross"):
        ins = INS(ss)
Example #10
0
def test_spectrum_type_bl_init():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')

    ss = SS()
    ss.read(testfile, diff=True)

    ins = INS(ss)
    assert "Initialized spectrum_type:cross from visibility data." in ins.history

    with pytest.raises(ValueError, match="Requested spectrum type is 'auto', but no autos exist."):
        ins = INS(ss, spectrum_type="auto")
Example #11
0
def test_no_diff_start():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    # Don't diff - will fail to mask data array
    ss = SS()
    ss.read(testfile, flag_choice='original', diff=False)

    ins = INS(ss)

    assert ss.flag_choice is None
Example #12
0
def test_write():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'
    prefix = os.path.join(DATA_PATH, '%s_test' % obs)
    data_outfile = '%s_SSINS_data.h5' % prefix
    z_score_outfile = '%s_SSINS_z_score.h5' % prefix
    mask_outfile = '%s_SSINS_mask.h5' % prefix
    match_outfile = '%s_SSINS_match_events.yml' % prefix
    sep_data_outfile = '%s.SSINS.data.h5' % prefix

    ss = SS()
    ss.read(testfile, flag_choice='original', diff=True)

    ins = INS(ss)
    # Mock some events
    ins.match_events.append((slice(0, 1), slice(1, 3), 'shape', 5))
    ins.match_events.append((slice(1, 2), slice(1, 3), 'shape', 5))
    ins.metric_array[:2, 1:3] = np.ma.masked
    ins.metric_ms = ins.mean_subtract()

    ins.write(prefix, output_type='data', clobber=True)
    ins.write(prefix, output_type='z_score', clobber=True)
    ins.write(prefix, output_type='mask', clobber=True)
    ins.write(prefix, output_type='match_events', clobber=True)
    ins.write(prefix, output_type='data', sep='.', clobber=True)
    with pytest.raises(ValueError):
        ins.write(prefix, output_type='bad_label')
    with pytest.raises(ValueError):
        ins.write(prefix, output_type='flags')

    new_ins = INS(data_outfile,
                  mask_file=mask_outfile,
                  match_events_file=match_outfile)
    assert np.all(ins.metric_array == new_ins.metric_array
                  ), "Elements of the metric array were not equal"
    assert np.all(ins.weights_array == new_ins.weights_array
                  ), "Elements of the weights array were not equal"
    assert np.all(ins.metric_array.mask == new_ins.metric_array.mask
                  ), "Elements of the mask were not equal"
    assert np.all(ins.metric_ms == new_ins.metric_ms
                  ), "Elements of the metric_ms were not equal"
    assert np.all(ins.match_events == new_ins.match_events
                  ), "Elements of the match_events were not equal"
    assert os.path.exists(
        sep_data_outfile), "sep_data_outfile was note written"

    for path in [
            data_outfile, z_score_outfile, mask_outfile, match_outfile,
            sep_data_outfile
    ]:
        os.remove(path)
Example #13
0
def test_use_integration_weights():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, flag_choice='original', diff=True)

    ins = INS(ss, use_integration_weights=True)

    # These will not be equal if weights are not binary to begin with
    # The accuracy of return_weights_square is already checked in pyuvdata
    assert not np.all(ins.weights_array == ins.weights_square_array)
Example #14
0
def test_write():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')
    file_type = 'uvfits'
    outfile = os.path.join(DATA_PATH, 'test_write.uvfits')

    ss = SS()
    ss.read(testfile, diff=True)

    blt_inds = np.where(ss.time_array == np.unique(ss.time_array)[10])
    custom = np.zeros_like(ss.data_array.mask)
    custom[blt_inds, :, 64:128, :] = 1

    # Flags the first time and no others
    ss.apply_flags(flag_choice='custom', custom=custom)

    # Write this out without combining flags, will issue a warning
    with pytest.warns(
            UserWarning,
            match=
            "Some nsamples are 0, which will result in failure to propagate flags. Setting nsample to default values where 0."
    ):
        ss.write(outfile, 'uvfits', filename_in=testfile, combine=False)

    # Check if the flags propagated correctly
    UV = UVData()
    UV.read(outfile)
    blt_inds = np.isin(UV.time_array, np.unique(UV.time_array)[10:12])
    assert np.all(
        UV.flag_array[blt_inds, :,
                      64:128, :]), "Not all expected flags were propagated"

    new_blt_inds = np.logical_not(
        np.isin(UV.time_array,
                np.unique(UV.time_array)[10:12]))
    assert not np.any(
        UV.flag_array[new_blt_inds, :,
                      64:128, :]), "More flags were made than expected"
    os.remove(outfile)

    # Test bad read.
    bad_uv_filepath = os.path.join(DATA_PATH, '1061312640_mix.uvfits')
    bad_uv = UVData()
    bad_uv.read(bad_uv_filepath)
    with pytest.raises(
            ValueError,
            match="UVData and SS objects were found to be incompatible."):
        ss.write(outfile, 'uvfits', bad_uv)
Example #15
0
def test_apply_flags():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)
    ss = SS()

    ss.read(testfile)

    # Make sure no flags are applied to start with
    assert not np.any(
        ss.data_array.mask), "There are some flags to start with."

    # Apply flags, test equality, test attribute change
    ss.apply_flags(flag_choice='original')
    assert np.all(
        ss.flag_array == ss.data_array.mask), "Flag arrays are not equal"
    assert ss.flag_choice is 'original', "Flag choice attribute was not changed"

    # Revert flags back, test equality, test attribute change
    ss.apply_flags(flag_choice=None)
    assert not np.any(ss.data_array.mask), "Flags did not revert back back"
    assert ss.flag_choice is None, "Flag choice attribute did not revert back"

    # Make a custom flag array where everything is flagged, check application
    custom = np.ones_like(ss.flag_array)
    ss.apply_flags(flag_choice='custom', custom=custom)
    assert np.all(ss.data_array.mask), "The custom flag array was not applied"
    assert ss.flag_choice is 'custom', "The flag choice attribute was not changed"

    # Read an INS in (no flags by default) and flag a channel, test if it applies correctly
    ins = INS(insfile)
    ins.metric_array.mask[:, 0] = True
    ss.apply_flags(flag_choice='INS', INS=ins)
    assert np.all(
        ss.data_array.mask[:, 0, 0]), "Not all of the 0th channel was flagged."
    assert not np.any(ss.data_array.mask[:, 0, 1:]
                      ), "Some of the channels other than the 0th were flagged"
    assert ss.flag_choice is 'INS'

    # Make flag_choice custom but do not provide array - should keep old flags
    ss.apply_flags(flag_choice='custom', custom=None)
    assert not np.any(ss.data_array.mask
                      ), "Some of the channels other than the 0th were flagged"
    assert ss.flag_choice is None

    with pytest.raises(ValueError):
        ss.apply_flags(flag_choice='bad_choice')
Example #16
0
def test_newmask():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, diff=False)

    assert not isinstance(ss.data_array, np.ma.MaskedArray)

    ss.apply_flags()

    assert ss.flag_choice is None
    assert isinstance(ss.data_array, np.ma.MaskedArray)
Example #17
0
def test_no_diff_start():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    # Don't diff - will fail to mask data array
    ss = SS()
    with pytest.warns(UserWarning, match="flag_choice will be ignored"):
        ss.read(testfile, flag_choice='original', diff=False)

    with pytest.warns(UserWarning, match="diff on read defaults to False"):
        ss.read(testfile, flag_choice='original', diff=False)

    ins = INS(ss)

    assert ss.flag_choice is None
Example #18
0
def test_VDH_no_model():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    outdir = os.path.join(DATA_PATH, 'test_plots')
    prefix = '%s/%s' % (outdir, obs)
    outfile = '%s_VDH.pdf' % prefix

    ss = SS()
    ss.read(testfile, flag_choice=None)

    cp.VDH_plot(ss, prefix, pre_model=False, post_model=False)

    assert os.path.exists(outfile), "The plot was not made"

    os.remove(outfile)
    os.rmdir(outdir)
Example #19
0
def test_mixture_prob():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile)
    ss.apply_flags('original')

    # Generate the mixture probabilities
    mixture_prob = ss.mixture_prob(bins='auto')

    # Check that they sum to close to 1
    assert np.isclose(np.sum(mixture_prob),
                      1), "Probabilities did not add up to close to 1"
Example #20
0
def test_mix_spectrum():
    obs = "1061312640_mix"
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')

    ss = SS()
    ss.read(testfile, diff=True)

    with pytest.warns(UserWarning, match="Requested spectrum type is 'cross'. Removing autos before averaging."):
        ins = INS(ss)

    with pytest.warns(UserWarning, match="Requested spectrum type is 'auto'. Removing"):
        ins = INS(ss, spectrum_type="auto")

    # Hack polarization array to check error
    ss.polarization_array[0] = 1
    with pytest.raises(ValueError, match="SS input has pseudo-Stokes data. SSINS does not"):
        ins = INS(ss, spectrum_type="auto")
Example #21
0
def test_SS_read():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)

    ss = SS()

    # Test reading in only metadata skips if block
    ss.read(testfile, read_data=False)
    assert ss.data_array is None, "Data array is not None"

    # Test select on read and diff
    ss.read(testfile, times=np.unique(ss.time_array)[1:10], diff=True)
    assert ss.Ntimes == 8, "Number of times after diff disagrees!"
    assert ss.Nbls == 99, "Number of baselines is incorrect"

    # See that it still passes UVData check
    assert ss.check()
Example #22
0
def test_SS_read():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()

    # Test reading in only metadata skips if block
    ss.read(testfile, read_data=False)
    assert ss.data_array is None, "Data array is not None"

    # Test select on read and diff
    ss.read(testfile, times=np.unique(ss.time_array)[1:10])
    assert ss.Ntimes == 8, "Diff seems like it wasn't executed correctly"

    # See that it still passes UVData check
    assert ss.check()
Example #23
0
def test_write_meta():
    obs = "1061313128_99bl_1pol_half_time"
    testfile = os.path.join(DATA_PATH, f"{obs}.uvfits")
    prefix = os.path.join(DATA_PATH, f"{obs}_test")

    uvd = UVData()
    uvd.read(testfile, freq_chans=np.arange(32))
    ss = SS()
    ss.read(testfile, freq_chans=np.arange(32), diff=True)
    uvf = UVFlag(uvd, mode="flag", waterfall=True)
    ins = INS(ss)

    ins.metric_array[:] = 1
    ins.weights_array[:] = 10
    ins.weights_square_array[:] = 10
    # Make some outliers
    # Narrowband in 1th, 26th, and 31th frequency
    ins.metric_array[1, 1, :] = 100
    ins.metric_array[1, 30, :] = 100
    ins.metric_array[3:14, 26, :] = 100
    # Arbitrary shape in 2, 3, 4
    ins.metric_array[3:14, 2:25, :] = 100
    ins.metric_array[[0, -1], :, :] = np.ma.masked
    ins.metric_array[:, [0, -1], :] = np.ma.masked
    ins.metric_ms = ins.mean_subtract()

    ch_wid = ins.freq_array[1] - ins.freq_array[0]
    shape_dict = {
        "shape":
        [ins.freq_array[2] + 0.1 * ch_wid, ins.freq_array[24] - 0.1 * ch_wid]
    }
    mf = MF(ins.freq_array, 5, tb_aggro=0.5, shape_dict=shape_dict)
    mf.apply_match_test(ins, time_broadcast=True)

    util.write_meta(prefix, ins, uvf=uvf, mf=mf, clobber=True)
    for data_type in ["data", "mask", "flags"]:
        path = f"{prefix}_SSINS_{data_type}.h5"
        assert os.path.exists(path)
        os.remove(path)
    for data_type in ["match_events", "matchfilter"]:
        path = f"{prefix}_SSINS_{data_type}.yml"
        assert os.path.exists(path)
        os.remove(path)
Example #24
0
def test_read_multifiles():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')
    new_fp1 = os.path.join(DATA_PATH, f'{obs}_new1.uvfits')
    new_fp2 = os.path.join(DATA_PATH, f'{obs}_new2.uvfits')
    flist = [new_fp1, new_fp2]

    file_type = 'uvfits'

    # Read in a file's metadata and split it into two objects
    uvd_full = UVData()
    uvd_full.read(testfile, read_data=False)
    times1 = np.unique(uvd_full.time_array)[:14]
    times2 = np.unique(uvd_full.time_array)[14:]

    # Write two separate files to be read in later
    uvd_split1 = UVData()
    uvd_split2 = UVData()
    uvd_split1.read(testfile, times=times1)
    uvd_split2.read(testfile, times=times2)
    uvd_split1.write_uvfits(new_fp1)
    uvd_split2.write_uvfits(new_fp2)

    # Check wanings and diff's
    ss_orig = SS()
    ss_multi = SS()
    # test warning raise
    with pytest.warns(
            UserWarning,
            match=("diff on read defaults to False now. Please double"
                   " check SS.read call and ensure the appropriate"
                   " keyword arguments for your intended use case.")):
        ss_orig.read(testfile, diff=False)
        ss_orig.diff()
        ss_multi.read(flist, diff=True)

    assert np.all(np.isclose(ss_orig.data_array,
                             ss_multi.data_array)), "Diffs were different!"
    assert ss_multi.Ntimes == (uvd_full.Ntimes - 1), "Too many diffs were done"

    for path in flist:
        os.remove(path)
def test_polyfit():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile)

    ins = INS(ss, order=1)

    # Mock some data for which the polyfit is exact
    x = np.arange(1, 11)
    ins.metric_array = np.ma.masked_array([[3 * x + 5 for i in range(3)]])
    ins.metric_array.mask = np.zeros(ins.metric_array.shape, dtype=bool)
    ins.metric_array = np.swapaxes(ins.metric_array, 0, 2)
    ins.weights_array = np.ones(ins.metric_array.shape)
    ins.metric_ms = ins.mean_subtract()

    assert np.all(ins.metric_ms == np.zeros(
        ins.metric_ms.shape)), "The polyfit was not exact"
Example #26
0
def test_mean_subtract():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, diff=True)

    ins = INS(ss, order=0)

    old_dat = np.copy(ins.metric_ms)

    # Mask the first five frequencies and last two at the first and second times
    ins.metric_array[0, :5] = np.ma.masked

    # Calculate the new mean-subtracted spectrum only over the first few masked frequencies
    ins.metric_ms[:, :5] = ins.mean_subtract(freq_slice=slice(0, 5))

    # See if a new mean was calculated over the first five frequencies
    assert not np.all(old_dat[1:, :5] == ins.metric_ms[1:, :5]), "All elements of the ms array are still equal"
Example #27
0
def test_VDH_no_model():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    outdir = os.path.join(DATA_PATH, 'test_plots')
    prefix = '%s/%s' % (outdir, obs)
    outfile = '%s_VDH.pdf' % prefix

    ss = SS()
    ss.read(testfile, flag_choice=None, diff=True)

    with pytest.warns(
            UserWarning,
            match=
            "Asking to plot post-flagging data, but SS.flag_choice is None. This is identical to plotting pre-flagging data"
    ):
        cp.VDH_plot(ss, prefix, pre_model=False, post_model=False)

    assert os.path.exists(outfile), "The plot was not made"

    os.remove(outfile)
    os.rmdir(outdir)
Example #28
0
def test_diff():
    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)

    ss = SS()
    uv = UVData()

    # Read in two times and two baselines of data, so that the diff is obvious.
    uv.read(testfile, read_data=False)
    times = np.unique(uv.time_array)[:2]
    bls = [(0, 1), (0, 2)]
    uv.read(testfile, times=times, bls=bls)
    uv.reorder_blts(order='baseline')

    diff_dat = uv.data_array[1::2] - uv.data_array[::2]
    diff_flags = np.logical_or(uv.flag_array[::2], uv.flag_array[1::2])
    diff_times = 0.5 * (uv.time_array[::2] + uv.time_array[1::2])
    diff_nsamples = 0.5 * (uv.nsample_array[::2] + uv.nsample_array[1::2])
    diff_ints = uv.integration_time[::2] + uv.integration_time[1::2]
    diff_uvw = 0.5 * (uv.uvw_array[::2] + uv.uvw_array[1::2])

    with pytest.warns(
            UserWarning,
            match=
            "Reordering data array to baseline order to perform differencing."
    ):
        ss.read(testfile, diff=True, times=times, bls=bls)
    ss.reorder_blts(order='baseline')

    assert np.all(ss.data_array == diff_dat), "Data values are different!"
    assert np.all(ss.flag_array == diff_flags), "Flags are different!"
    assert np.all(ss.time_array == diff_times), "Times are different!"
    assert np.all(
        ss.nsample_array == diff_nsamples), "nsample_array is different!"
    assert np.all(
        ss.integration_time == diff_ints), "Integration times are different"
    assert np.all(ss.uvw_array == diff_uvw), "uvw_arrays disagree!"
    assert np.all(ss.ant_1_array == np.array([0, 0])), "ant_1_array disagrees!"
    assert np.all(ss.ant_2_array == np.array([1, 2])), "ant_2_array disagrees!"
Example #29
0
def test_VDH_no_model():

    matplotlib = pytest.importorskip("matplotlib")

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, f'{obs}.uvfits')
    outdir = os.path.join(DATA_PATH, 'test_plots')
    prefix = f'{outdir}/{obs}'
    outfile = f'{prefix}_VDH.pdf'

    ss = SS()
    ss.read(testfile, flag_choice=None, diff=True)

    with pytest.warns(
            UserWarning,
            match=
            "Asking to plot post-flagging data, but SS.flag_choice is None. This is identical to plotting pre-flagging data"
    ):
        cp.VDH_plot(ss, prefix, pre_model=False, post_model=False)

    assert os.path.exists(outfile), "The plot was not made"

    os.remove(outfile)
    os.rmdir(outdir)
Example #30
0
def test_mixture_prob():

    obs = '1061313128_99bl_1pol_half_time'
    testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs)
    file_type = 'uvfits'

    ss = SS()
    ss.read(testfile, diff=True)
    ss.apply_flags('original')

    # Generate the mixture probabilities
    mixture_prob = ss.mixture_prob(bins='auto')

    # Check that they sum to close to 1
    assert np.isclose(np.sum(mixture_prob),
                      1), "Probabilities did not add up to close to 1"

    # Do a new read, but don't diff. Run and check mask.
    ss = SS()
    ss.read(testfile, diff=False)

    mixture_prob = ss.mixture_prob(bins='auto')

    assert ss.flag_choice is None