def test_ApproxPredictorCaching(graph): pred = network_hotspot.Predictor(None, graph) pred.kernel = mock.Mock() pred.kernel.return_value = 1.0 pred = network_hotspot.ApproxPredictorCaching(pred) risks = np.asarray([0, 0, 0, 0], dtype=np.float) pred.add_edge(risks, 0, None, 1) np.testing.assert_allclose(risks, [1, 1, 1, 1])
def test_Predictor_with_time_kernel(graph, netpoints): pred = network_hotspot.Predictor(netpoints, graph) pred.kernel = network_hotspot.TriangleKernel(0.2) pred.time_kernel = network_hotspot.ExponentialTimeKernel(1) result = pred.predict() assert result.graph is pred.graph assert result.risks[0] == pytest.approx(1 * np.exp(-1 / 24)) assert result.risks[1] == pytest.approx(1)
def test_Predictor(graph, netpoints): pred = network_hotspot.Predictor(netpoints, graph) pred.kernel = network_hotspot.TriangleKernel(0.2) pred.time_kernel = network_hotspot.ConstantTimeKernel() result = pred.predict() assert result.graph is pred.graph assert result.risks[0] == pytest.approx(1) assert result.risks[1] == pytest.approx(1)
def test_ApproxPredictorCaching2(graph2): sq2 = np.sqrt(2) pred = network_hotspot.Predictor(None, graph2) pred.kernel = mock.Mock() pred.kernel.return_value = 1.0 pred = network_hotspot.ApproxPredictorCaching(pred) risks = np.asarray([0] * 9, dtype=np.float) pred.add_edge(risks, 0, None, 1) np.testing.assert_allclose( risks, [1, sq2 / 2, 1 / 4, sq2 / 4, sq2 / 2, 1 / 4, sq2 / 4, 2 / 4, 1 / 8])
def test_Predictor_add_split(graph2): pred = network_hotspot.Predictor(None, graph2) pred.kernel = mock.Mock() pred.kernel.integrate.return_value = 1.0 pred.kernel.cutoff = 3 risks = np.zeros(9) pred.add(risks, 0, 1, 0) np.testing.assert_allclose(risks, [1, 0.5, 0.25, 0, 0.5, 0.25, 0, 0.5, 0]) risks = np.zeros(9) pred.add(risks, 7, -1, 0.5) np.testing.assert_allclose(risks, [0.25, 0.25, 0, 0, 0.5, 0.5, 0.5, 1, 0])
def test_ApproxPredictor(graph): pred = network_hotspot.Predictor(None, graph) pred.kernel = mock.Mock() pred.kernel.return_value = 1.0 pred = network_hotspot.ApproxPredictor(pred) risks = np.asarray([0, 0, 0, 0], dtype=np.float) pred.add_edge(risks, 0, None, 1) np.testing.assert_allclose(risks, [1, 1, 1, 1]) assert pred.kernel.call_args_list == [ mock.call(0), mock.call(1.0), mock.call(2.0), mock.call(1.0) ]
def test_Predictor_add(graph): pred = network_hotspot.Predictor(None, graph) pred.kernel = network_hotspot.TriangleKernel(5) risks = [0, 0, 0, 0] pred.add(risks, 0, 1, 0.2) assert risks[0] == pytest.approx(0.4 * (0.4 - 0.8 / 25)) assert risks[1] == pytest.approx(pred.kernel.integrate(0.8, 1.8)) assert risks[2] == pytest.approx(pred.kernel.integrate(1.8, 2.8)) assert risks[3] == pytest.approx(pred.kernel.integrate(2.8, 3.8)) risks = [0, 0, 0, 0] pred.add(risks, 0, -1, 0.8) assert risks[0] == pytest.approx(pred.kernel.integrate(0, 0.2)) assert risks[3] == pytest.approx(pred.kernel.integrate(0.2, 1.2)) assert risks[2] == pytest.approx(pred.kernel.integrate(1.2, 2.2)) assert risks[1] == pytest.approx(pred.kernel.integrate(2.2, 3.2))