コード例 #1
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_freq_broadcast_whole_band():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)
    # spoof the metric array
    ins.metric_array[:] = 1
    ins.metric_array[2, 10:20] = 10
    ins.metric_array[4, 40:50] = 10
    ins.metric_ms = ins.mean_subtract()

    shape_dict = {
        'shape1': [ins.freq_array[10], ins.freq_array[20]],
        'shape2': [ins.freq_array[40], ins.freq_array[50]]
    }

    mf = MF(ins.freq_array, 5, shape_dict=shape_dict, broadcast_streak=True)

    mf.apply_match_test(ins, event_record=True, freq_broadcast=True)

    assert np.all(ins.metric_array.mask[2])
    assert np.all(ins.metric_array.mask[4])
コード例 #2
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_match_test():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array
    ins.metric_array[:] = np.ones_like(ins.metric_array)
    ins.weights_array = np.copy(ins.metric_array)
    ins.weights_square_array = np.copy(ins.weights_array)

    # Make a shape dictionary for a shape that will be injected later
    ch_wid = ins.freq_array[1] - ins.freq_array[0]
    shape = [
        ins.freq_array[8] - 0.2 * ch_wid, ins.freq_array[12] + 0.2 * ch_wid
    ]
    shape_dict = {'shape': shape}
    sig_thresh = {'shape': 5, 'narrow': 5, 'streak': 5}
    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict)

    # Inject a shape, narrow, and streak event
    ins.metric_array[3, 5] = 10
    ins.metric_array[5] = 10
    ins.metric_array[7, 7:13] = 10
    ins.metric_ms = ins.mean_subtract()

    t_max, f_max, shape_max, sig_max = mf.match_test(ins)

    assert t_max == slice(5, 6), "Wrong time"
    assert f_max == slice(0, ins.Nfreqs), "Wrong freq"
    assert shape_max == 'streak', "Wrong shape"
コード例 #3
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_time_broadcast_no_new_event():
    """
    Tests that a new event is not added because the agression threshold is not
    exceeded.
    """

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array (and weights...)
    ins.metric_array[:] = 1
    ins.weights_array = np.copy(ins.metric_array)
    ins.weights_square_array = np.copy(ins.weights_array)
    ins.metric_ms = ins.mean_subtract()
    ins.sig_array = np.ma.copy(ins.metric_ms)

    sig_thresh = {'narrow': 5}
    mf = MF(ins.freq_array, sig_thresh, streak=False, tb_aggro=0.5)
    # Put in an outlier so it gets to samp_thresh_test
    ins.metric_array[1, 10] = 100
    ins.metric_ms = ins.mean_subtract()
    mf.apply_match_test(ins, event_record=True, time_broadcast=True)

    event = mf.time_broadcast(ins, ins.match_events[0], event_record=True)
    assert event == ins.match_events[0]
コード例 #4
0
def test_sig_plot():

    matplotlib = pytest.importorskip("matplotlib")

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)
    outdir = os.path.join(DATA_PATH, 'test_plots')

    prefix = '%s/%s_flagged' % (outdir, obs)
    dataplotfile = '%s_SSINS.pdf' % prefix
    outfile = '%s_SSINS_sig.pdf' % prefix

    ins = INS(insfile)
    shape_dict = {
        'TV6': [1.74e8, 1.81e8],
        'TV7': [1.81e8, 1.88e8],
        'TV8': [1.88e8, 1.95e8]
    }
    sig_thresh = {'TV6': 5, 'TV7': 5, 'TV8': 5, 'narrow': 5, 'streak': 5}
    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict)
    mf.apply_match_test(ins)

    xticks = np.arange(0, 384, 96)
    xticklabels = ['%.1f' % (10**-6 * ins.freq_array[tick]) for tick in xticks]
    yticks = np.arange(0, 50, 10)
    yticklabels = ['%i' % (2 * tick) for tick in yticks]

    cp.INS_plot(ins, prefix)

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

    os.remove(outfile)
    os.remove(dataplotfile)
    os.rmdir(outdir)
コード例 #5
0
ファイル: test_MF.py プロジェクト: cjordan/SSINS
def test_match_test():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array
    ins.metric_array = np.ma.ones([10, 20, 1])
    ins.weights_array = np.copy(ins.metric_array)
    ins.freq_array = np.zeros([1, 20])
    ins.freq_array = np.arange(20)

    # Make a shape dictionary for a shape that will be injected later
    shape = [7.9, 12.1]
    shape_dict = {'shape': shape}
    sig_thresh = {'shape': 5, 'narrow': 5, 'streak': 5}
    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict)

    # Inject a shape, narrow, and streak event
    ins.metric_array[3, 5] = 10
    ins.metric_array[5] = 10
    ins.metric_array[7, 7:13] = 10
    ins.metric_ms = ins.mean_subtract()

    t_max, f_max, R_max, shape_max = mf.match_test(ins)
    print(shape_max)

    assert t_max == 5, "Wrong time"
    assert f_max == slice(0, 20), "Wrong freq"
    assert shape_max == 'streak', "Wrong shape"
コード例 #6
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_freq_broadcast_no_new_event():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)
    # spoof the metric array
    ins.metric_array[:] = 1
    ins.metric_array[2, 10:20] = 10
    ins.metric_ms = ins.mean_subtract()

    # Slice will go up to 21 since shapes are inclusive at the boundaries
    # when boundary is on channel center
    shape_dict = {'shape1': [ins.freq_array[10], ins.freq_array[20]]}
    broadcast_dict = {'sb2': [ins.freq_array[30], ins.freq_array[59]]}
    test_event = (slice(2, 3), slice(10, 21), 'shape1', 10)

    mf = MF(ins.freq_array,
            5,
            shape_dict=shape_dict,
            broadcast_streak=False,
            broadcast_dict=broadcast_dict)
    mf.apply_match_test(ins, event_record=True, freq_broadcast=True)
    event = mf.freq_broadcast(ins, event=test_event, event_record=True)
    assert event == test_event
    assert ins.match_events[0][:-1] == test_event[:-1]
コード例 #7
0
ファイル: test_MF.py プロジェクト: houyu611/SSINS
def test_init():

    freq_path = os.path.join(DATA_PATH, 'MWA_Highband_Freq_Array.npy')
    freqs = np.load(freq_path)

    # Make a shape that encompasses the first five channels
    ch_width = freqs[1] - freqs[0]
    shape = [freqs[0] - 0.1 * ch_width, freqs[4] + 0.1 * ch_width]
    shape_dict = {'shape': shape}

    mf_1 = MF(freqs, 5, shape_dict=shape_dict)

    assert mf_1.slice_dict['shape'] == slice(
        0, 5), "It did not set the shape correctly"
    assert mf_1.slice_dict['narrow'] is None, "narrow did not get set correctly"
    assert mf_1.slice_dict['streak'] == slice(
        None), "streak did not get set correctly"

    # Test disabling streak/narrow
    mf_2 = MF(freqs, 5, shape_dict=shape_dict, narrow=False, streak=False)

    assert 'narrow' not in mf_2.slice_dict, "narrow is still in the shape_dict"
    assert 'streak' not in mf_2.slice_dict, "streak still in shape_dict"
    assert 'shape' in mf_2.slice_dict, "shape not in shape_dict"

    # Test if error gets raised with bad shape_dict
    try:
        mf_3 = MF(freqs, 5, shape_dict={}, streak=False, narrow=False)
    except ValueError:
        pass
コード例 #8
0
def test_calc_occ():

    obs = "1061313128_99bl_1pol_half_time_SSINS"
    testfile = os.path.join(DATA_PATH, f"{obs}.h5")

    ins = INS(testfile)

    # Mock some flaggable data
    ins.select(freq_chans=np.arange(32), times=ins.time_array[:22])

    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()

    num_int_flag = 2
    num_chan_flag = 2

    num_init_flag = np.sum(ins.metric_array.mask)

    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)

    occ_dict = util.calc_occ(ins, mf, num_init_flag, num_int_flag=2,
                             lump_narrowband=False)
    assert occ_dict["streak"] == 0
    assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[1] * 10**(-6))] == 0.05
    assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[26] * 10**(-6))] == 1
    assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[30] * 10**(-6))] == 0.05
    assert occ_dict["shape"] == 1

    occ_dict = util.calc_occ(ins, mf, num_init_flag, num_int_flag=2,
                             lump_narrowband=True)

    # total narrow over total valid
    assert occ_dict["narrow"] == 24 / 600
    assert occ_dict["streak"] == 0
    assert occ_dict["shape"] == 1
    assert "narrow_%.3fMHz" % (ins.freq_array[1] * 10**(-6)) not in occ_dict.keys()
    assert "narrow_%.3fMHz" % (ins.freq_array[30] * 10**(-6)) not in occ_dict.keys()

    yml_outpath = os.path.join(DATA_PATH, "test_occ_.yml")
    with open(yml_outpath, "w") as occ_file:
        yaml.safe_dump(occ_dict, occ_file)

    os.remove(yml_outpath)
コード例 #9
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_apply_samp_thresh_dep_error_match_test():
    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)
    mf = MF(ins.freq_array, 5, tb_aggro=0.2)
    with pytest.raises(ValueError,
                       match="apply_samp_thresh has been deprecated"):
        mf.apply_match_test(ins, apply_samp_thresh=True)
コード例 #10
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_freq_broadcast_no_dict():
    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)

    mf = MF(ins.freq_array, 5)

    with pytest.raises(ValueError,
                       match="MF object does not have a broadcast_dict"):
        mf.apply_match_test(ins, freq_broadcast=True)
コード例 #11
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_freq_broadcast_subbands():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)
    # spoof the metric array
    ins.metric_array[:] = 1
    ins.metric_array[2, 10:20] = 10
    ins.metric_array[4, 40:50] = 10
    ins.metric_ms = ins.mean_subtract()

    # Slice will go up to 21 since shapes are inclusive at the boundaries
    # when boundary is on channel center
    shape_dict = {
        'shape1': [ins.freq_array[10], ins.freq_array[20]],
        'shape2': [ins.freq_array[40], ins.freq_array[50]]
    }

    # boundaries are INCLUSIVE
    broadcast_dict = {
        'sb1': [ins.freq_array[0], ins.freq_array[29]],
        'sb2': [ins.freq_array[30], ins.freq_array[59]]
    }

    mf = MF(ins.freq_array,
            5,
            shape_dict=shape_dict,
            broadcast_streak=False,
            broadcast_dict=broadcast_dict)
    mf.apply_match_test(ins, event_record=True, freq_broadcast=True)

    assert np.all(ins.metric_array.mask[2, :30])
    assert not np.any(ins.metric_array.mask[2, 30:])
    assert np.all(ins.metric_array.mask[4, 30:60])
    assert not np.any(ins.metric_array.mask[4, :30])
    assert not np.any(ins.metric_array.mask[4, 60:])

    print(ins.match_events)

    test_match_events = [(slice(2, 3), slice(10, 21), 'shape1'),
                         (slice(2, 3), slice(0, 30), 'freq_broadcast_sb1'),
                         (slice(4, 5), slice(40, 51), 'shape2'),
                         (slice(4, 5), slice(30, 60), 'freq_broadcast_sb2')]
    for event, test_event in zip(ins.match_events, test_match_events):
        assert event[:3] == test_event
コード例 #12
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_time_broadcast():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array
    ins.metric_array[:] = 1
    ins.weights_array = np.copy(ins.metric_array)
    ins.weights_square_array = np.copy(ins.weights_array)
    ins.metric_ms = ins.mean_subtract()
    ins.sig_array = np.ma.copy(ins.metric_ms)

    # Arbitrarily flag enough data in channel 10
    sig_thresh = {'narrow': 5}
    mf = MF(ins.freq_array, sig_thresh, streak=False, tb_aggro=0.5)
    ins.metric_array[4:, 9] = np.ma.masked
    ins.metric_array[4:, 10] = np.ma.masked
    # Put in an outlier so it gets to samp_thresh_test
    ins.metric_array[2, 9] = 100
    ins.metric_array[1, 10] = 100
    ins.metric_ms = ins.mean_subtract()
    bool_ind = np.zeros(ins.metric_array.shape, dtype=bool)
    bool_ind[:, 10] = 1
    bool_ind[:, 9] = 1

    mf.apply_match_test(ins, event_record=True, time_broadcast=True)
    print(ins.match_events)
    test_match_events = [
        (slice(1, 2), slice(10, 11),
         'narrow_%.3fMHz' % (ins.freq_array[10] * 10**(-6))),
        (slice(0, ins.Ntimes), slice(10, 11),
         'time_broadcast_narrow_%.3fMHz' % (ins.freq_array[10] * 10**(-6))),
        (slice(2, 3), slice(9, 10),
         'narrow_%.3fMHz' % (ins.freq_array[9] * 10**(-6))),
        (slice(0, ins.Ntimes), slice(9, 10),
         'time_broadcast_narrow_%.3fMHz' % (ins.freq_array[9] * 10**(-6)))
    ]
    # Test stuff
    assert np.all(
        ins.metric_array.mask == bool_ind), "The right flags were not applied"
    for i, event in enumerate(test_match_events):
        assert ins.match_events[
            i][:-1] == event, "The events weren't appended correctly"

    # Test that writing with samp_thresh flags is OK
    ins.write(out_prefix, output_type='match_events')
    test_match_events_read = ins.match_events_read(match_outfile)
    os.remove(match_outfile)
    assert ins.match_events == test_match_events_read

    # Test that exception is raised when tb_aggro is too high
    with pytest.raises(ValueError):
        mf = MF(ins.freq_array, {'narrow': 5, 'streak': 5}, tb_aggro=100)
        mf.apply_samp_thresh_test(ins, (slice(1, 2), slice(10, 11), 'narrow'))
コード例 #13
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_MF_write():
    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    prefix = os.path.join(DATA_PATH, f'{obs}')
    outfile = f"{prefix}_test_SSINS_matchfilter.yml"

    ins = INS(insfile)
    sig_thresh = 5
    broadcast_dict = {"TV7": [174e6, 181e6]}
    shape_dict = {"TV7": [174e6, 181e6]}

    mf = MF(ins.freq_array,
            sig_thresh,
            broadcast_dict=broadcast_dict,
            broadcast_streak=True)

    mf.write(f"{prefix}_test", clobber=True)

    assert os.path.exists(
        outfile), "Outfile was not written or has the wrong name."

    with open(f"{prefix}_control_SSINS_matchfilter.yml", 'r') as control_file:
        control_dict = yaml.safe_load(control_file)
    control_dict.pop("version")
    test_dict = mf._make_yaml_dict()
    test_dict.pop("version")
    assert test_dict == control_dict

    with pytest.raises(ValueError, match="matchfilter file with prefix"):
        mf.write(f"{prefix}_test", clobber=False)

    os.remove(outfile)
コード例 #14
0
ファイル: test_MF.py プロジェクト: cjordan/SSINS
def test_samp_thresh():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)
    out_prefix = os.path.join(DATA_PATH, '%s_test' % obs)
    match_outfile = '%s_SSINS_match_events.yml' % out_prefix

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array
    ins.metric_array = np.ma.ones([10, 20, 1])
    ins.weights_array = np.copy(ins.metric_array)
    ins.metric_ms = ins.mean_subtract()
    ins.sig_array = np.ma.copy(ins.metric_ms)
    ins.freq_array = np.zeros([1, 20])
    ins.freq_array = np.arange(20)

    # Arbitrarily flag enough data in channel 10
    sig_thresh = {'narrow': 5}
    mf = MF(ins.freq_array, sig_thresh, streak=False, N_samp_thresh=5)
    ins.metric_array[3:, 10] = np.ma.masked
    ins.metric_array[3:, 9] = np.ma.masked
    # Put in an outlier so it gets to samp_thresh_test
    ins.metric_array[0, 11] = 10
    ins.metric_ms = ins.mean_subtract()
    bool_ind = np.zeros(ins.metric_array.shape, dtype=bool)
    bool_ind[:, 10] = 1
    bool_ind[:, 9] = 1
    bool_ind[0, 11] = 1

    mf.apply_match_test(ins, event_record=True, apply_samp_thresh=True)
    test_match_events = [(0, slice(11, 12), 'narrow')]
    test_match_events += [(ind, slice(9, 10), 'samp_thresh')
                          for ind in range(3)]
    test_match_events += [(ind, slice(10, 11), 'samp_thresh')
                          for ind in range(3)]
    # Test stuff
    assert np.all(
        ins.metric_array.mask == bool_ind), "The right flags were not applied"
    for i, event in enumerate(test_match_events):
        assert ins.match_events[
            i][:-1] == event, "The events weren't appended correctly"

    # Test that writing with samp_thresh flags is OK
    ins.write(out_prefix, output_type='match_events')
    test_match_events_read = ins.match_events_read(match_outfile)
    os.remove(match_outfile)
    assert ins.match_events == test_match_events_read

    # Test that exception is raised when N_samp_thresh is too high
    with pytest.raises(ValueError):
        mf = MF(ins.freq_array, {'narrow': 5, 'streak': 5}, N_samp_thresh=100)
        mf.apply_samp_thresh_test(ins)
コード例 #15
0
ファイル: test_util.py プロジェクト: mwilensky768/SSINS
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)
コード例 #16
0
ファイル: test_MF.py プロジェクト: mwilensky768/SSINS
def test_N_samp_thresh_dep_error():
    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5')
    out_prefix = os.path.join(DATA_PATH, f'{obs}_test')
    match_outfile = f'{out_prefix}_SSINS_match_events.yml'

    ins = INS(insfile)

    with pytest.raises(ValueError,
                       match="The N_samp_thresh parameter is now deprected."):
        mf = MF(ins.freq_array, 5, N_samp_thresh=10)
コード例 #17
0
ファイル: test_MF.py プロジェクト: cjordan/SSINS
def test_apply_match_test():

    obs = '1061313128_99bl_1pol_half_time'
    insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs)

    ins = INS(insfile)

    # Mock a simple metric_array and freq_array
    ins.metric_array = np.ma.ones([10, 20, 1])
    ins.weights_array = np.copy(ins.metric_array)
    ins.freq_array = np.zeros([1, 20])
    ins.freq_array = np.arange(20)

    # Make a shape dictionary for a shape that will be injected later
    shape = [7.9, 12.1]
    shape_dict = {'shape': shape}
    sig_thresh = {'shape': 5, 'narrow': 5, 'streak': 5}
    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict)

    # Inject a shape, narrow, and streak event
    ins.metric_array[3, 5] = 10
    ins.metric_array[5] = 10
    ins.metric_array[7, 7:13] = 10
    ins.metric_ms = ins.mean_subtract()
    ins.sig_array = np.ma.copy(ins.metric_ms)

    mf.apply_match_test(ins, event_record=True)

    # Check that the right events are flagged
    test_mask = np.zeros(ins.metric_array.shape, dtype=bool)
    test_mask[3, 5] = 1
    test_mask[5] = 1
    test_mask[7, 7:13] = 1

    assert np.all(test_mask == ins.metric_array.mask), "Flags are incorrect"

    test_match_events_slc = [(5, slice(0, 20), 'streak'),
                             (7, slice(7, 13), 'shape'),
                             (3, slice(5, 6), 'narrow')]

    for i, event in enumerate(test_match_events_slc):
        assert ins.match_events[i][:-1] == test_match_events_slc[
            i], "%ith event is wrong" % i

    assert not np.any([ins.match_events[i][-1] < 5 for i in range(3)
                       ]), "Some significances were less than 5"

    # Test a funny if block that is required when the last time in a shape is flagged
    ins.metric_array[1:, 7:13] = np.ma.masked
    ins.metric_ms[0, 7:13] = 10

    mf.apply_match_test(ins, event_record=True)

    assert np.all(ins.metric_ms.mask[:, 7:13]
                  ), "All the times were not flagged for the shape"
コード例 #18
0
ファイル: Read_INS.py プロジェクト: mwilensky768/MJW-MWA
    read_paths = {
        'data':
        arr,
        'Nbls':
        '%s/arrs/%s_%s_INS_Nbls.npym' % (args.inpath, obs, args.flag_choice),
        'freq_array':
        '%s/metadata/%s_freq_array.npy' % (args.inpath, obs),
        'pols':
        '%s/metadata/%s_pols.npy' % (args.inpath, obs),
        'vis_units':
        '%s/metadata/%s_vis_units.npy' % (args.inpath, obs)
    }
    ins = INS(obs=obs,
              outpath=args.outpath,
              flag_choice=args.flag_choice,
              read_paths=read_paths)
    # ins.data[:, :, :82] = np.ma.masked
    # ins.data[:, :, -21:] = np.ma.masked
    ins.data_ms = ins.mean_subtract()
    ins.counts, ins.bins, ins.sig_thresh = ins.hist_make()
    cp.INS_plot(ins, **ms_plot_kwargs)
    mf = MF(ins,
            shape_dict=shape_dict,
            point=args.point,
            streak=args.streak,
            **mf_kwargs)
    for test in args.tests:
        getattr(mf, 'apply_%s_test' % test)(args.order)
    ins.save()
    cp.MF_plot(mf, **ms_plot_kwargs)
コード例 #19
0
    ins = INS(ss)
    prefix = f'{args.outdir}/{args.obsid}'
    Catalog_Plot.INS_plot(ins, prefix, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5,
                          title=args.obsid, xlabel='Frequency (Mhz)',
                          ylabel='Time (UTC)')

    # Try to save memory - hope for garbage collector
    del ss

    # Set up MF flagging for routine shapes
    shape_dict = {'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8],
                  'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8]}
    sig_thresh = {shape: 5 for shape in shape_dict}
    sig_thresh['narrow'] = 5
    sig_thresh['streak'] = 10
    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict,
            tb_aggro=0.5, broadcast_streak=True)

    # Do the flagging
    mf.apply_match_test(ins, event_record=True, time_broadcast=True,
                        freq_broadcast=True)
    Catalog_Plot.INS_plot(ins, f'{prefix}_flagged', data_cmap=cm.plasma,
                          ms_vmin=-5, ms_vmax=5, title=args.obsid,
                          xlabel='Frequency (Mhz)', ylabel='Time (UTC)')

    # Make the flag object
    uvd = UVData()
    uvd.read(args.uvd, read_data=False)
    uvf = UVFlag(uvd, mode='flag', waterfall=True)
    uvf.flag_array = ins.mask_to_flags()

    # Write SSINS outputs
コード例 #20
0
    'TV6', 'TV7', 'TV8', 'broad6', 'broad7', 'broad8', 'streak', 'point',
    'total'
]
occ_dict = {sig: {shape: {} for shape in shapes} for sig in sig_list}

for obs in obslist:
    flist = glob.glob('%s/metadata/%s*' % (basedir, obs))
    if len(flist):
        read_paths = util.read_paths_construct(basedir, 'original', obs, 'INS')
        for sig_thresh in sig_list:
            ins = INS(read_paths=read_paths,
                      obs=obs,
                      outpath=outdir,
                      flag_choice='original')
            mf = MF(ins,
                    sig_thresh=sig_thresh,
                    N_thresh=15,
                    shape_dict=shape_dict)
            mf.apply_match_test(apply_N_thresh=True)
            occ_dict[sig_thresh]['total'][obs] = np.mean(ins.data.mask[:, 0, :,
                                                                       0],
                                                         axis=0)
            if len(ins.match_events):
                event_frac = util.event_fraction(ins.match_events,
                                                 ins.data.shape[0], shapes,
                                                 384)
                for shape in shapes[:-1]:
                    occ_dict[sig_thresh][shape][obs] = event_frac[shape]
            if obs == '1061312152' and sig_thresh is 5:
                pickle.dump(mf.slice_dict,
                            open('%s/long_run_shape_dict.pik' % outdir, 'wb'))
            del ins
コード例 #21
0
                    ax,
                    ins.data[:, 0, :, 0],
                    cbar_label='Amplitude (UNCALIB)',
                    freq_array=ins.freq_array[0],
                    aspect=aspect)

fig.savefig('%s/%s_INS.png' % (outpath, obs))

fig_new, ax_new = plt.subplots(figsize=(16, 9), ncols=2)

plot_lib.image_plot(fig,
                    ax_new[0],
                    ins.data_ms[:, 0, :, 0],
                    cbar_label='Deviation ($\hat{\sigma}$)',
                    freq_array=ins.freq_array[0],
                    cmap=cm.coolwarm,
                    aspect=aspect)

mf = MF(ins, sig_thresh=5)
mf.apply_match_test()

plot_lib.image_plot(fig_new,
                    ax_new[1],
                    ins.data_ms[:, 0, :, 0],
                    cbar_label='Deviation ($\hat{\sigma}$)',
                    freq_array=ins.freq_array[0],
                    cmap=cm.coolwarm,
                    aspect=aspect,
                    mask_color='black')
fig_new.savefig('%s/%s_INS_MS.png' % (outpath, obs))
コード例 #22
0
ファイル: Catalog_Gen.py プロジェクト: houyu611/SSINS
                    action='store',
                    help='The base directory for saving all outputs')
args = parser.parse_args()

ss = SS()
ss.read(args.inpath, ant_str='cross')

ins = INS(ss)
ins.metric_array[:, :82] = np.ma.masked
ins.metric_array[:, :-21] = np.ma.masked
ins.order = 1
ins.metric_ms = ins.mean_subtract()

shape_dict = {
    'TV4': [1.74e8, 1.82e8],
    'TV5': [1.82e8, 1.9e8],
    'TV6': [1.9e8, 1.98e8],
    'dig1': [1.125e8, 1.15625e8],
    'dig2': [1.375e8, 1.40625e8],
    'dig3': [1.625e8, 1.65625e8],
    'dig4': [1.875e8, 1.90625e8]
}

mf = MF(ins.freq_array, 5, shape_dict=shape_dict, N_samp_thresh=15)
mf.apply_match_test(ins, apply_samp_thresh=True)

prefix = '%s/%s' % (args.outpath, args.obs)
ins.write(prefix)
ins.write(prefix, output_type='mask')
ins.write(prefix, output_type='match_events')
コード例 #23
0
ファイル: 1061318984_MF.py プロジェクト: mwilensky768/MJW-MWA
from SSINS import INS, MF, util, plot_lib
from SSINS import Catalog_Plot as cp
from matplotlib import cm
import matplotlib.pyplot as plt

indir = '/Users/mike_e_dubs/General/1061318984'
outdir = '%s_Filtered' % indir
obs = '1061318984'
read_paths = util.read_paths_construct(indir, None, obs, 'INS')

sig_thresh = 5

ins = INS(read_paths=read_paths, obs=obs, outpath=outdir)
mf = MF(ins, sig_thresh=sig_thresh, N_thresh=15)
mf.apply_match_test()
fig, ax = plt.subplots(figsize=(14, 8), nrows=3)
fig.suptitle('Narrowband SSINS')
plot_lib.image_plot(fig,
                    ax[0],
                    ins.data_ms[:, 0, :, 0],
                    cmap=cm.coolwarm,
                    vmin=-5,
                    vmax=5,
                    title='Before N Sample Test',
                    cbar_label='Deviation ($\hat{\sigma}$)',
                    mask_color='black',
                    freq_array=ins.freq_array[0])
ax[0].set_xlabel('')
plot_lib.image_plot(fig,
                    ax[1],
                    ins.data[:, 0, :, 0],
コード例 #24
0
outpath = '%s/test_combines' % basedir
insarray = []
for x in obs:
    read_paths = util.read_paths_construct(basedir, 'None', x, 'INS')
    insarray.append(
        INS(obs=x,
            outpath=outpath,
            read_paths=read_paths,
            flag_choice='orginal'))
inscombined = INS_helpers.INS_concat(insarray, axis=0)
inscombined.obs = 'LWA_50to60'
inscombined.outpath = outpath
inscombined.vis_units = insarray[0].vis_units
inscombined.pols = insarray[0].pols
inscombined.save()

cp.INS_plot(inscombined, vmax=.05, ms_vmax=5, ms_vmin=-5)

shape_dict = {'44MHZ': [44e6, 45e6], '40.6MHZ': [40.4e6, 40.7e6]}
sig_thresh = 4.35

mf = MF(inscombined,
        shape_dict=shape_dict,
        sig_thresh=sig_thresh,
        point=False,
        streak=False)

mf.apply_match_test()

cp.MF_plot(mf, ms_vmin=-mf.sig_thresh, ms_vmax=mf.sig_thresh)
コード例 #25
0
ファイル: MWA_EoR_High_Flag.py プロジェクト: cjordan/SSINS
def execbody(ins_filepath):
    slash_ind = ins_filepath.rfind('/')
    obsid = ins_filepath[slash_ind + 1:slash_ind + 11]

    ins = INS(ins_filepath)
    ins.select(times=ins.time_array[3:-3])
    ins.metric_ms = ins.mean_subtract()
    shape_dict = {
        'TV6': [1.74e8, 1.81e8],
        'TV7': [1.81e8, 1.88e8],
        'TV8': [1.88e8, 1.95e8],
        'TV9': [1.95e8, 2.02e8]
    }
    sig_thresh = {shape: 5 for shape in shape_dict}
    sig_thresh['narrow'] = 5
    sig_thresh['streak'] = 8
    mf = MF(ins.freq_array,
            sig_thresh,
            shape_dict=shape_dict,
            N_samp_thresh=len(ins.time_array) // 2)

    ins.metric_array[ins.metric_array == 0] = np.ma.masked
    ins.metric_ms = ins.mean_subtract()
    ins.sig_array = np.ma.copy(ins.metric_ms)

    #write plots if command flagged to do so
    if args.plots:
        prefix = '%s/%s_trimmed_zeromask' % (args.outdir, obsid)
        freqs = np.arange(1.7e8, 2e8, 5e6)
        xticks, xticklabels = util.make_ticks_labels(freqs,
                                                     ins.freq_array,
                                                     sig_fig=0)
        yticks = [0, 20, 40]
        yticklabels = []
        for tick in yticks:
            yticklabels.append(
                Time(ins.time_array[tick], format='jd').iso[:-4])

        Catalog_Plot.INS_plot(ins,
                              prefix,
                              xticks=xticks,
                              yticks=yticks,
                              xticklabels=xticklabels,
                              yticklabels=yticklabels,
                              data_cmap=cm.plasma,
                              ms_vmin=-5,
                              ms_vmax=5,
                              title=obsid,
                              xlabel='Frequency (Mhz)',
                              ylabel='Time (UTC)')
        if args.verbose:
            print("wrote trimmed zeromask plot for " + obsid)

    mf.apply_match_test(ins, apply_samp_thresh=False)
    mf.apply_samp_thresh_test(ins, event_record=True)

    flagged_prefix = '%s/%s_trimmed_zeromask_MF_s8' % (args.outdir, obsid)

    #write data/mask/match/ if command flagged to do so
    if args.write:
        ins.write(flagged_prefix, output_type='data', clobber=True)
        ins.write(flagged_prefix, output_type='mask', clobber=True)
        ins.write(flagged_prefix, output_type='match_events')
        if args.verbose:
            print("wrote data/mask/match files for " + obsid)

    #write plots if command flagged to do so
    if args.plots:
        Catalog_Plot.INS_plot(ins,
                              flagged_prefix,
                              xticks=xticks,
                              yticks=yticks,
                              xticklabels=xticklabels,
                              yticklabels=yticklabels,
                              data_cmap=cm.plasma,
                              ms_vmin=-5,
                              ms_vmax=5,
                              title=obsid,
                              xlabel='Frequency (Mhz)',
                              ylabel='Time (UTC)')

        if args.verbose:
            print("wrote trimmed zeromask (w/ match filter) for " + obsid)

    #a hardcoded csv generator for occ_csv
    if args.gencsv is not None:
        csv = "" + obsid + "," + flagged_prefix + "_SSINS_data.h5," + flagged_prefix + "_SSINS_mask.h5," + flagged_prefix + "_SSINS_match_events.yml\n"
        with open(args.gencsv, "a") as csvfile:
            csvfile.write(csv)
        print("wrote entry for " + obsid)
コード例 #26
0
    uvf = UVFlag(uvd, waterfall=True, mode='flag')

    num_init_flag = np.sum(ins.metric_array.mask)
    int_time = uvd.integration_time[0]
    print(f"Using int_time {int_time}")
    num_int_flag = (args.start_flag + args.end_flag) / int_time

    with open(f"{DATA_PATH}/MWA_EoR_Highband_shape_dict.yml",
              "r") as shape_file:
        shape_dict = yaml.safe_load(shape_file)
    sig_thresh = {shape: 5 for shape in shape_dict}
    sig_thresh["narrow"] = 5
    sig_thresh["streak"] = 10
    print(f"Flagging these shapes: {shape_dict}")

    mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, tb_aggro=0.4)
    mf.apply_match_test(ins, time_broadcast=True)

    occ_dict = util.calc_occ(ins,
                             mf,
                             num_init_flag,
                             num_int_flag=num_int_flag,
                             lump_narrowband=True)
    with open(f"{prefix}_occ.yml", "w") as occ_file:
        yaml.safe_dump(occ_dict, occ_file)

    ins.write(prefix, output_type='mask', clobber=True)
    print(ins.Nfreqs)
    print(uvf.Nfreqs)
    ins.write(prefix, output_type='flags', uvf=uvf, clobber=True)
    ins.write(prefix, output_type='match_events', clobber=True)
コード例 #27
0
                    freq_array=ins.freq_array[0],
                    cmap=cm.coolwarm,
                    aspect=aspect,
                    cbar_label='Deviation ($\hat{\sigma}$)',
                    vmin=-5,
                    vmax=5,
                    ylabel='Time (10 s)')

fig_mf, ax_mf = plt.subplots(figsize=(16, 9), ncols=2)
ins.data.mask[:, 0, :82, 0] = True
ins.data.mask[:, 0, -21:, 0] = True
ins.data_ms = ins.mean_subtract(order=1)
mf = MF(ins,
        sig_thresh=5,
        N_thresh=0,
        shape_dict={
            'TV4': [1.74e8, 1.82e8],
            'TV5': [1.82e8, 1.9e8],
            'TV6': [1.9e8, 1.98e8]
        })
mf.apply_match_test(apply_N_thresh=False, order=1)

plot_lib.image_plot(fig_mf,
                    ax_mf[0],
                    ins.data[:, 0, :, 0],
                    freq_array=ins.freq_array[0],
                    cbar_label='Amplitude (UNCALIB)',
                    aspect=aspect,
                    vmin=0,
                    vmax=0.03,
                    cmap=cm.viridis,
                    mask_color='white',
コード例 #28
0
if args.labels is not None:
    shape_dict = {label: [min, max] for (label, min, max) in zip(args.labels, args.mins, args.maxs)}
else:
    shape_dict = {}

obslist = util.make_obslist(args.obsfile)
edges = [0 + 16 * i for i in range(24)] + [15 + 16 * i for i in range(24)]
freqs = np.load('/Users/mike_e_dubs/python_stuff/MJW-MWA/Useful_Information/MWA_Highband_Freq_Array.npy')

for i, obs in enumerate(obslist):
    read_paths = util.read_paths_INS(args.basedir, args.flag_choice, obs)
    ins = INS(read_paths=read_paths, flag_choice=args.flag_choice, obs=obs,
              outpath=args.outdir, order=args.order)
    cp.INS_plot(ins)
    mf = MF(ins, sig_thresh=5, shape_dict=shape_dict)
    mf.apply_match_test(order=args.order)
    cp.MF_plot(mf)
    if not i:
        occ_num = np.ma.masked_array(mf.INS.data.mask)
        occ_den = np.ma.masked_array(np.ones(occ_num.shape))
        occ_num[:, 0, edges] = np.ma.masked
        occ_den[:, 0, edges] = np.ma.masked
    else:
        occ_num = occ_num + mf.INS.data.mask
        occ_den = occ_den + np.ones(occ_num.shape)

occ_freq = occ_num.sum(axis=(0, 1, 3)) / occ_den.sum(axis=(0, 1, 3)) * 100
occ_total = occ_num.sum() / occ_den.sum() * 100
fig, ax = plt.subplots(figsize=(14, 8))
plot_lib.error_plot(fig, ax, freqs * 10**(-6), occ_freq,
コード例 #29
0
        for tick in yticks:
            yticklabels.append(Time(ins.time_array[tick], format='jd').iso[:-4])
        Catalog_Plot.INS_plot(ins, prefix,
                              xticks=xticks, yticks=yticks, xticklabels=xticklabels,
                              yticklabels=yticklabels, data_cmap=cm.plasma,
                              ms_vmin=-5, ms_vmax=5, title=args.obsid,
                              xlabel='Frequency (Mhz)', ylabel='Time (UTC)')
        # Try to save memory - hope for garbage collector
        del ss
        # Set up MF flagging for routine shapes
        shape_dict = {'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8],
                      'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8]}
        sig_thresh = {shape: 5 for shape in shape_dict}
        sig_thresh['narrow'] = 5
        sig_thresh['streak'] = 8
        mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict,
                N_samp_thresh=len(ins.time_array) // 2)
        mf.apply_match_test(ins, apply_samp_thresh=False)
        mf.apply_samp_thresh_test(ins, event_record=True)
        Catalog_Plot.INS_plot(ins, '%s_flagged' % prefix,
                              xticks=xticks, yticks=yticks, xticklabels=xticklabels,
                              yticklabels=yticklabels, data_cmap=cm.plasma,
                              ms_vmin=-5, ms_vmax=5, title=args.obsid,
                              xlabel='Frequency (Mhz)', ylabel='Time (UTC)')
        ins.write(prefix, output_type='mask')

    uvd = UVData()
    uvd.read(args.uvd, phase_to_pointing_center=True, correct_cable_len=True)
    uvf = UVFlag(uvd, mode='flag', waterfall=True)
    uvf.flag_array = ins.mask_to_flags()
    utils.apply_uvflag(uvd, uvf, inplace=True)
コード例 #30
0
import pickle

obslist = util.make_obslist(
    '/Users/mike_e_dubs/MWA/Obs_Lists/sidelobe_survey_obsIDs.txt')
indir = '/Users/mike_e_dubs/MWA/INS/Diffuse'
outpath = '%s_Filtered' % indir
sig_thresh = 5
shape_dict = {
    'TV6': [1.74e8, 1.81e8],
    'TV7': [1.81e8, 1.88e8],
    'TV8': [1.88e8, 1.95e8]
}
mf_kwargs = {'sig_thresh': sig_thresh, 'shape_dict': shape_dict}
occ_dict = {}
for obs in obslist:
    if len(glob.glob('%s/figs/%s*' % (indir, obs))):
        read_paths = util.read_paths_construct(indir, None, obs, 'INS')
        ins = INS(obs=obs, outpath=outpath, read_paths=read_paths)
        ins.data[-5:] = np.ma.masked
        ins.data[0] = np.ma.masked
        ins.data_ms = ins.mean_subtract()
        mf = MF(ins, **mf_kwargs)
        mf.apply_match_test()
        cp.MF_plot(mf)
        cp.INS_plot(ins, ms_vmin=-5, ms_vmax=5)
        occ_dict['obs'] = (np.count_nonzero(ins.data_ms.mask[:-3]) /
                           ins.data[:-3].size)
        del ins
        del mf
pickle.dump(occ_dict, open('%s/occ_dict_only_subflags' % outpath, 'wb'))