def test_remove_lytaf_events_2(): """ Test _remove_lytaf_events() with no user artifacts found. """ # Run _remove_lytaf_events with pytest.warns(UserWarning, match='None of user supplied artifacts were found.'): time_test, channels_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts="Offpoint", return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generated expected result time_expected = TIME channels_expected = CHANNELS artifacts_status_expected = { "lytaf": LYTAF_TEST, "removed": EMPTY_LYTAF, "not_removed": LYTAF_TEST, "not_found": ["Offpoint"] } # Assert test values are same as expected assert np.all(time_test == time_expected) assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"] # Test correct values are returned when return_artifacts kwarg not # supplied. # Case 1: channels kwarg is True # Run _remove_lytaf_events with pytest.warns(UserWarning, match='None of user supplied artifacts were found.'): time_test, channels_test = lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts=["Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) assert np.all(time_test == time_expected) assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() # Case 2: channels kwarg is False # Run _remove_lytaf_events with pytest.warns(UserWarning, match='None of user supplied artifacts were found.'): time_test = lyra._remove_lytaf_events(TIME, artifacts=["Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) assert np.all(time_test == time_expected)
def test_remove_lytaf_events_1(): """ Test _remove_lytaf_events() with some artifacts found and others not. """ # Run _remove_lytaf_events time_test, channels_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generated expected result bad_indices = np.logical_and(TIME >= LYTAF_TEST["begin_time"][0], TIME <= LYTAF_TEST["end_time"][0]) bad_indices = np.arange(len(TIME))[bad_indices] time_expected = np.delete(TIME, bad_indices) channels_expected = [ np.delete(CHANNELS[0], bad_indices), np.delete(CHANNELS[1], bad_indices) ] artifacts_status_expected = { "lytaf": LYTAF_TEST, "removed": LYTAF_TEST[0], "not_removed": LYTAF_TEST[1], "not_found": ["Offpoint"] } # Assert test values are same as expected assert time_test.all() == time_expected.all() assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"] # Test that correct values are returned when channels kwarg not # supplied. # Run _remove_lytaf_events time_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert test values are same as expected assert time_test.all() == time_expected.all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"]
def test_remove_lytaf_events_1(): """Test _remove_lytaf_events() with some artifacts found and others not.""" # Run _remove_lytaf_events time_test, channels_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generated expected result bad_indices = np.logical_and(TIME >= LYTAF_TEST["begin_time"][0], TIME <= LYTAF_TEST["end_time"][0]) bad_indices = np.arange(len(TIME))[bad_indices] time_expected = np.delete(TIME, bad_indices) channels_expected = [np.delete(CHANNELS[0], bad_indices), np.delete(CHANNELS[1], bad_indices)] artifacts_status_expected = {"lytaf": LYTAF_TEST, "removed": LYTAF_TEST[0], "not_removed": LYTAF_TEST[1], "not_found": ["Offpoint"]} # Assert test values are same as expected assert time_test.all() == time_expected.all() assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"] # Test that correct values are returned when channels kwarg not # supplied. # Run _remove_lytaf_events time_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert test values are same as expected assert time_test.all() == time_expected.all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"]
def test_remove_lytaf_events_from_timeseries(lyra_ts): """ Test if artifact are correctly removed from a TimeSeries. """ # Check correct errors are raised due to bad input with pytest.raises(AttributeError): ts_test = lyra.remove_lytaf_events_from_timeseries( [], force_use_local_lytaf=True) # Run remove_artifacts_from_timeseries, returning artifact # status ts_test, artifact_status_test = \ lyra.remove_lytaf_events_from_timeseries( lyra_ts, artifacts=["LAR", "Offpoint"], return_artifacts=True, force_use_local_lytaf=True) # Generate expected data by calling _remove_lytaf_events and # constructing expected dataframe manually. time, channels, artifact_status_expected = lyra._remove_lytaf_events( lyra_ts.to_dataframe().index, channels=[ np.asanyarray(lyra_ts.to_dataframe()["CHANNEL1"]), np.asanyarray(lyra_ts.to_dataframe()["CHANNEL2"]), np.asanyarray(lyra_ts.to_dataframe()["CHANNEL3"]), np.asanyarray(lyra_ts.to_dataframe()["CHANNEL4"]) ], artifacts=["LAR", "Offpoint"], return_artifacts=True, force_use_local_lytaf=True) dataframe_expected = pandas.DataFrame(index=time, data={ "CHANNEL1": channels[0], "CHANNEL2": channels[1], "CHANNEL3": channels[2], "CHANNEL4": channels[3] }) # Assert expected result is returned pandas.testing.assert_frame_equal(ts_test.to_dataframe(), dataframe_expected) assert artifact_status_test.keys() == artifact_status_expected.keys() np.testing.assert_array_equal(artifact_status_test["lytaf"], artifact_status_expected["lytaf"]) np.testing.assert_array_equal(artifact_status_test["removed"], artifact_status_expected["removed"]) np.testing.assert_array_equal(artifact_status_test["not_removed"], artifact_status_expected["not_removed"]) assert artifact_status_test["not_found"] == \ artifact_status_expected["not_found"] # Run remove_artifacts_from_timeseries, without returning # artifact status ts_test = \ lyra.remove_lytaf_events_from_timeseries( lyra_ts, artifacts=["LAR", "Offpoint"], force_use_local_lytaf=True) # Assert expected result is returned pandas.testing.assert_frame_equal(ts_test.to_dataframe(), dataframe_expected)
def test_remove_lytaf_events_2(): """Test _remove_lytaf_events() with no user artifacts found.""" # Run _remove_lytaf_events time_test, channels_test, artifacts_status_test = \ lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts="Offpoint", return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generated expected result time_expected = TIME channels_expected = CHANNELS artifacts_status_expected = {"lytaf": LYTAF_TEST, "removed": EMPTY_LYTAF, "not_removed": LYTAF_TEST, "not_found": ["Offpoint"]} # Assert test values are same as expected assert time_test.all() == time_expected.all() assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() assert artifacts_status_test.keys() == artifacts_status_expected.keys() np.testing.assert_array_equal(artifacts_status_test["lytaf"], artifacts_status_expected["lytaf"]) np.testing.assert_array_equal(artifacts_status_test["removed"], artifacts_status_expected["removed"]) np.testing.assert_array_equal(artifacts_status_test["not_removed"], artifacts_status_expected["not_removed"]) assert artifacts_status_test["not_found"] == \ artifacts_status_expected["not_found"] # Test correct values are returned when return_artifacts kwarg not # supplied. # Case 1: channels kwarg is True # Run _remove_lytaf_events time_test, channels_test = lyra._remove_lytaf_events( TIME, channels=CHANNELS, artifacts=["Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) assert time_test.all() == time_expected.all() assert (channels_test[0]).all() == (channels_expected[0]).all() assert (channels_test[1]).all() == (channels_expected[1]).all() # Case 2: channels kwarg is False # Run _remove_lytaf_events time_test = lyra._remove_lytaf_events( TIME, artifacts=["Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) assert time_test.all() == time_expected.all()
def test_remove_lytaf_events_from_timeseries(dtype): """Test if artefacts are correctly removed from a TimeSeries. Also test LYRALightCurve for backwards compatibility.""" # Check correct errors are raised due to bad input with pytest.raises(AttributeError): ts_test = lyra.remove_lytaf_events_from_timeseries( [], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Run remove_artifacts_from_timeseries, returning artifact # status ts = get_lyradata(dtype) ts_test, artifact_status_test = \ lyra.remove_lytaf_events_from_timeseries( ts, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generate expected data by calling _remove_lytaf_events and # constructing expected dataframe manually. time, channels, artifact_status_expected = lyra._remove_lytaf_events( ts.data.index, channels=[ np.asanyarray(ts.data["CHANNEL1"]), np.asanyarray(ts.data["CHANNEL2"]), np.asanyarray(ts.data["CHANNEL3"]), np.asanyarray(ts.data["CHANNEL4"]) ], artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) dataframe_expected = pandas.DataFrame(index=time, data={ "CHANNEL1": channels[0], "CHANNEL2": channels[1], "CHANNEL3": channels[2], "CHANNEL4": channels[3] }) # Assert expected result is returned pandas.util.testing.assert_frame_equal(ts_test.data, dataframe_expected) assert artifact_status_test.keys() == artifact_status_expected.keys() np.testing.assert_array_equal(artifact_status_test["lytaf"], artifact_status_expected["lytaf"]) np.testing.assert_array_equal(artifact_status_test["removed"], artifact_status_expected["removed"]) np.testing.assert_array_equal(artifact_status_test["not_removed"], artifact_status_expected["not_removed"]) assert artifact_status_test["not_found"] == \ artifact_status_expected["not_found"] # Run remove_artifacts_from_timeseries, without returning # artifact status ts_test = \ lyra.remove_lytaf_events_from_timeseries( ts, artifacts=["LAR", "Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert expected result is returned pandas.util.testing.assert_frame_equal(ts_test.data, dataframe_expected)
def test_remove_lytaf_events_from_lightcurve(): """Test if artefacts are correctly removed from a LYRAlightCurve.""" # Create sample LYRALightCurve lyralc = lightcurve.LYRALightCurve.create("2014-01-01") lyralc.data = pandas.DataFrame(index=TIME, data={"CHANNEL1": CHANNELS[0], "CHANNEL2": CHANNELS[1], "CHANNEL3": CHANNELS[0], "CHANNEL4": CHANNELS[1]}) # Check correct errors are raised due to bad input with pytest.raises(TypeError): lyralc_test = lyra.remove_lytaf_events_from_lightcurve( [], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Run remove_artifacts_from_lyralightcurve, returning artifact # status lyralc_test, artifact_status_test = \ lyra.remove_lytaf_events_from_lightcurve( lyralc, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generate expected data by calling _remove_lytaf_events and # constructing expected dataframe manually. time, channels, artifact_status_expected = lyra._remove_lytaf_events( lyralc.data.index, channels=[np.asanyarray(lyralc.data["CHANNEL1"]), np.asanyarray(lyralc.data["CHANNEL2"]), np.asanyarray(lyralc.data["CHANNEL3"]), np.asanyarray(lyralc.data["CHANNEL4"])], artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) dataframe_expected = pandas.DataFrame(index=time, data={"CHANNEL1": channels[0], "CHANNEL2": channels[1], "CHANNEL3": channels[2], "CHANNEL4": channels[3]}) # Assert expected result is returned pandas.util.testing.assert_frame_equal(lyralc_test.data, dataframe_expected) assert artifact_status_test.keys() == artifact_status_expected.keys() np.testing.assert_array_equal(artifact_status_test["lytaf"], artifact_status_expected["lytaf"]) np.testing.assert_array_equal(artifact_status_test["removed"], artifact_status_expected["removed"]) np.testing.assert_array_equal(artifact_status_test["not_removed"], artifact_status_expected["not_removed"]) assert artifact_status_test["not_found"] == \ artifact_status_expected["not_found"] # Run remove_artifacts_from_lyralightcurve, without returning # artifact status lyralc_test = \ lyra.remove_lytaf_events_from_lightcurve( lyralc, artifacts=["LAR", "Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert expected result is returned pandas.util.testing.assert_frame_equal(lyralc_test.data, dataframe_expected)
def test_remove_lytaf_events_from_timeseries(dtype): """Test if artefacts are correctly removed from a TimeSeries. Also test LYRALightCurve for backwards compatibility.""" # Check correct errors are raised due to bad input with pytest.raises(AttributeError): ts_test = lyra.remove_lytaf_events_from_timeseries( [], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Run remove_artifacts_from_timeseries, returning artifact # status ts = get_lyradata(dtype) ts_test, artifact_status_test = \ lyra.remove_lytaf_events_from_timeseries( ts, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generate expected data by calling _remove_lytaf_events and # constructing expected dataframe manually. time, channels, artifact_status_expected = lyra._remove_lytaf_events( ts.data.index, channels=[np.asanyarray(ts.data["CHANNEL1"]), np.asanyarray(ts.data["CHANNEL2"]), np.asanyarray(ts.data["CHANNEL3"]), np.asanyarray(ts.data["CHANNEL4"])], artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) dataframe_expected = pandas.DataFrame(index=time, data={"CHANNEL1": channels[0], "CHANNEL2": channels[1], "CHANNEL3": channels[2], "CHANNEL4": channels[3]}) # Assert expected result is returned pandas.util.testing.assert_frame_equal(ts_test.data, dataframe_expected) assert artifact_status_test.keys() == artifact_status_expected.keys() np.testing.assert_array_equal(artifact_status_test["lytaf"], artifact_status_expected["lytaf"]) np.testing.assert_array_equal(artifact_status_test["removed"], artifact_status_expected["removed"]) np.testing.assert_array_equal(artifact_status_test["not_removed"], artifact_status_expected["not_removed"]) assert artifact_status_test["not_found"] == \ artifact_status_expected["not_found"] # Run remove_artifacts_from_timeseries, without returning # artifact status ts_test = \ lyra.remove_lytaf_events_from_timeseries( ts, artifacts=["LAR", "Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert expected result is returned pandas.util.testing.assert_frame_equal(ts_test.data, dataframe_expected)
def test_remove_lytaf_events_3(local_cache): """ Test if correct errors are raised by _remove_lytaf_events(). """ with pytest.raises(TypeError): lyra._remove_lytaf_events(TIME, channels=6, artifacts=["LAR"], force_use_local_lytaf=True) with pytest.raises(ValueError): lyra._remove_lytaf_events(TIME, force_use_local_lytaf=True) with pytest.raises(TypeError): lyra._remove_lytaf_events(TIME, artifacts=[6], force_use_local_lytaf=True) with pytest.raises(ValueError): lyra._remove_lytaf_events(TIME, artifacts=["LAR", "incorrect artifact type"], force_use_local_lytaf=True)
def test_remove_lytaf_events_3(): """Test if correct errors are raised by _remove_lytaf_events().""" with pytest.raises(TypeError): lyra._remove_lytaf_events(TIME, channels=6, artifacts=["LAR"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) with pytest.raises(ValueError): lyra._remove_lytaf_events(TIME, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) with pytest.raises(TypeError): lyra._remove_lytaf_events(TIME, artifacts=[6], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) with pytest.raises(ValueError): lyra._remove_lytaf_events(TIME, artifacts=["LAR","incorrect artifact type"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True)
def test_remove_lytaf_events_from_lightcurve(): """Test if artefacts are correctly removed from a LYRAlightCurve.""" # Create sample LYRALightCurve lyralc = lightcurve.LYRALightCurve.create("2014-01-01") lyralc.data = pandas.DataFrame(index=TIME, data={ "CHANNEL1": CHANNELS[0], "CHANNEL2": CHANNELS[1], "CHANNEL3": CHANNELS[0], "CHANNEL4": CHANNELS[1] }) # Check correct errors are raised due to bad input with pytest.raises(TypeError): lyralc_test = lyra.remove_lytaf_events_from_lightcurve( [], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Run remove_artifacts_from_lyralightcurve, returning artifact # status lyralc_test, artifact_status_test = \ lyra.remove_lytaf_events_from_lightcurve( lyralc, artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Generate expected data by calling _remove_lytaf_events and # constructing expected dataframe manually. time, channels, artifact_status_expected = lyra._remove_lytaf_events( lyralc.data.index, channels=[ np.asanyarray(lyralc.data["CHANNEL1"]), np.asanyarray(lyralc.data["CHANNEL2"]), np.asanyarray(lyralc.data["CHANNEL3"]), np.asanyarray(lyralc.data["CHANNEL4"]) ], artifacts=["LAR", "Offpoint"], return_artifacts=True, lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) dataframe_expected = pandas.DataFrame(index=time, data={ "CHANNEL1": channels[0], "CHANNEL2": channels[1], "CHANNEL3": channels[2], "CHANNEL4": channels[3] }) # Assert expected result is returned pandas.util.testing.assert_frame_equal(lyralc_test.data, dataframe_expected) assert artifact_status_test.keys() == artifact_status_expected.keys() np.testing.assert_array_equal(artifact_status_test["lytaf"], artifact_status_expected["lytaf"]) np.testing.assert_array_equal(artifact_status_test["removed"], artifact_status_expected["removed"]) np.testing.assert_array_equal(artifact_status_test["not_removed"], artifact_status_expected["not_removed"]) assert artifact_status_test["not_found"] == \ artifact_status_expected["not_found"] # Run remove_artifacts_from_lyralightcurve, without returning # artifact status lyralc_test = \ lyra.remove_lytaf_events_from_lightcurve( lyralc, artifacts=["LAR", "Offpoint"], lytaf_path=TEST_DATA_PATH, force_use_local_lytaf=True) # Assert expected result is returned pandas.util.testing.assert_frame_equal(lyralc_test.data, dataframe_expected)