def test_iterative_antenna_metrics_and_flagging_and_saving_and_loading( self): am = ant_metrics.AntennaMetrics(self.dataFileList, self.reds, fileformat='miriad') with self.assertRaises(KeyError): filename = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') am.save_antenna_metrics(filename) am.iterative_antenna_metrics_and_flagging() for stat in self.summaryStats: self.assertTrue(hasattr(am, stat)) self.assertIn((81, 'x'), am.xants) self.assertIn((81, 'y'), am.xants) self.assertIn((81, 'x'), am.deadAntsRemoved) self.assertIn((81, 'y'), am.deadAntsRemoved) outfile = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') am.save_antenna_metrics(outfile) loaded = ant_metrics.load_antenna_metrics(outfile) # json names for summary statistics jsonStats = [ 'xants', 'crossed_ants', 'dead_ants', 'removal_iteration', 'final_metrics', 'all_metrics', 'final_mod_z_scores', 'all_mod_z_scores', 'cross_pol_z_cut', 'dead_ant_z_cut', 'datafile_list', 'reds', 'version' ] for stat, jsonStat in zip(self.summaryStats, jsonStats): nt.assert_true(np.array_equal(loaded[jsonStat], getattr(am, stat))) os.remove(outfile)
def test_iterative_antenna_metrics_and_flagging_and_saving_and_loading( antmetrics_data): am = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') with pytest.raises(KeyError): filename = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') am.save_antenna_metrics(filename) am.iterative_antenna_metrics_and_flagging() for stat in antmetrics_data.summaryStats: assert hasattr(am, stat) assert (81, 'x') in am.xants assert (81, 'y') in am.xants assert (81, 'x') in am.deadAntsRemoved assert (81, 'y') in am.deadAntsRemoved outfile = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') am.save_antenna_metrics(outfile) loaded = ant_metrics.load_antenna_metrics(outfile) # json names for summary statistics jsonStats = [ 'xants', 'crossed_ants', 'dead_ants', 'removal_iteration', 'final_metrics', 'all_metrics', 'final_mod_z_scores', 'all_mod_z_scores', 'cross_pol_z_cut', 'dead_ant_z_cut', 'datafile_list', 'reds', 'version' ] for stat, jsonStat in zip(antmetrics_data.summaryStats, jsonStats): assert np.array_equal(loaded[jsonStat], getattr(am, stat)) os.remove(outfile)
def test_init(antmetrics_data): am = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') assert len(am.ants) == 19 assert set(am.pols) == set(['xx', 'yy', 'xy', 'yx']) assert set(am.antpols) == set(['x', 'y']) assert len(am.bls) == 19 * 18 / 2 + 19 assert len(am.reds) == 27
def test_init(self): am = ant_metrics.AntennaMetrics(self.dataFileList, self.reds, fileformat='miriad') self.assertEqual(len(am.ants), 19) self.assertEqual(set(am.pols), set(['xx', 'yy', 'xy', 'yx'])) self.assertEqual(set(am.antpols), set(['x', 'y'])) self.assertEqual(len(am.bls), 19 * 18 / 2 + 19) self.assertEqual(len(am.reds), 27)
def test_cross_detection(antmetrics_data): am2 = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') am2.iterative_antenna_metrics_and_flagging(crossCut=3, deadCut=10) for stat in antmetrics_data.summaryStats: assert hasattr(am2, stat) assert (81, 'x') in am2.xants assert (81, 'y') in am2.xants assert (81, 'x') in am2.crossedAntsRemoved assert (81, 'y') in am2.crossedAntsRemoved
def test_load_errors(antmetrics_data): with pytest.raises(ValueError): uvtest.checkWarnings( ant_metrics.AntennaMetrics, [[DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], []], {"fileformat": 'miriad'}, nwarnings=1, message='antenna_diameters is not set') with pytest.raises(IOError): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='uvfits') with pytest.raises(NotImplementedError): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='fhd') with pytest.raises(NotImplementedError): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='not_a_format')
def test_cross_detection(self): am2 = ant_metrics.AntennaMetrics(self.dataFileList, self.reds, fileformat='miriad') am2.iterative_antenna_metrics_and_flagging(crossCut=3, deadCut=10) for stat in self.summaryStats: self.assertTrue(hasattr(am2, stat)) self.assertIn((81, 'x'), am2.xants) self.assertIn((81, 'y'), am2.xants) self.assertIn((81, 'x'), am2.crossedAntsRemoved) self.assertIn((81, 'y'), am2.crossedAntsRemoved)
def test_load_errors(self): with self.assertRaises(ValueError): uvtest.checkWarnings( ant_metrics.AntennaMetrics, [[DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], []], {"fileformat": 'miriad'}, nwarnings=1, message='antenna_diameters is not set') with self.assertRaises(IOError): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='uvfits') with self.assertRaises(Exception): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='fhd') with self.assertRaises(ValueError): ant_metrics.AntennaMetrics( [DATA_PATH + '/zen.2457698.40355.xx.HH.uvcA'], [], fileformat='not_a_format')
def test_save_json(antmetrics_data): am = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') am.iterative_antenna_metrics_and_flagging() for stat in antmetrics_data.summaryStats: assert hasattr(am, stat) assert (81, 'x') in am.xants assert (81, 'y') in am.xants assert (81, 'x') in am.deadAntsRemoved assert (81, 'y') in am.deadAntsRemoved outfile = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.json') warn_message = [ "JSON-type files can still be written " "but are no longer written by default.\n" "Write to HDF5 format for future compatibility." ] uvtest.checkWarnings(am.save_antenna_metrics, func_args=[outfile], func_kwargs={'overwrite': True}, category=PendingDeprecationWarning, nwarnings=1, message=warn_message) # am.save_antenna_metrics(json_file) warn_message = [ "JSON-type files can still be read but are no longer " "written by default.\n" "Write to HDF5 format for future compatibility." ] loaded = uvtest.checkWarnings(ant_metrics.load_antenna_metrics, func_args=[outfile], category=PendingDeprecationWarning, nwarnings=1, message=warn_message) _ = loaded.pop('history', '') jsonStats = [ 'xants', 'crossed_ants', 'dead_ants', 'removal_iteration', 'final_metrics', 'all_metrics', 'final_mod_z_scores', 'all_mod_z_scores', 'cross_pol_z_cut', 'dead_ant_z_cut', 'datafile_list', 'reds', 'version' ] for stat, jsonStat in zip(antmetrics_data.summaryStats, jsonStats): file_val = loaded[jsonStat] obj_val = getattr(am, stat) if isinstance(file_val, dict): assert qmtest.recursive_compare_dicts(file_val, obj_val) else: assert file_val == obj_val os.remove(outfile)
def test_totally_dead_ants(antmetrics_data): am2 = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') deadant = 9 for ant1, ant2 in am2.bls: if deadant in (ant1, ant2): for pol in am2.pols: am2.data[ant1, ant2, pol][:] = 0.0 am2.reset_summary_stats() am2.find_totally_dead_ants() for antpol in am2.antpols: assert (deadant, antpol) in am2.xants assert (deadant, antpol) in am2.deadAntsRemoved assert am2.removalIter[(deadant, antpol)] == -1
def test_totally_dead_ants(self): am2 = ant_metrics.AntennaMetrics(self.dataFileList, self.reds, fileformat='miriad') deadant = 9 for ant1, ant2 in am2.bls: if deadant in (ant1, ant2): for pol in am2.pols: am2.data[ant1, ant2, pol][:] = 0.0 am2.reset_summary_stats() am2.find_totally_dead_ants() for antpol in am2.antpols: self.assertIn((deadant, antpol), am2.xants) self.assertIn((deadant, antpol), am2.deadAntsRemoved) self.assertEqual(am2.removalIter[(deadant, antpol)], -1)
def test_add_file_appellation(antmetrics_data): am = ant_metrics.AntennaMetrics(antmetrics_data.dataFileList, antmetrics_data.reds, fileformat='miriad') am.iterative_antenna_metrics_and_flagging() for stat in antmetrics_data.summaryStats: assert hasattr(am, stat) assert (81, 'x') in am.xants assert (81, 'y') in am.xants assert (81, 'x') in am.deadAntsRemoved assert (81, 'y') in am.deadAntsRemoved outfile = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output') am.save_antenna_metrics(outfile, overwrite=True) outname = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') assert os.path.isfile(outname) os.remove(outname)
def test_add_file_appellation(self): am = ant_metrics.AntennaMetrics(self.dataFileList, self.reds, fileformat='miriad') am.iterative_antenna_metrics_and_flagging() for stat in self.summaryStats: self.assertTrue(hasattr(am, stat)) self.assertIn((81, 'x'), am.xants) self.assertIn((81, 'y'), am.xants) self.assertIn((81, 'x'), am.deadAntsRemoved) self.assertIn((81, 'y'), am.deadAntsRemoved) outfile = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output') am.save_antenna_metrics(outfile) outname = os.path.join(DATA_PATH, 'test_output', 'ant_metrics_output.hdf5') nt.assert_true(os.path.isfile(outname)) os.remove(outname)