def test_TriangleKernel(): kernel = network_hotspot.TriangleKernel(1) assert kernel.cutoff == pytest.approx(1) assert kernel(1) == 0 assert kernel(2) == 0 assert kernel(0.5) == pytest.approx(0.5) assert kernel(0) == pytest.approx(1) np.testing.assert_allclose(kernel([1, 2, 0.5, 0]), [0, 0, 0.5, 1]) kernel = network_hotspot.TriangleKernel(5) assert kernel.cutoff == pytest.approx(5) assert kernel(10) == 0 assert kernel(4) == pytest.approx(1 / 25) assert kernel(1) == pytest.approx(4 / 25) assert kernel(0) == pytest.approx(1 / 5) np.testing.assert_allclose(kernel([10, 4, 1, 0]), [0, 1 / 25, 4 / 25, 1 / 5]) assert kernel.integrate(0, 5) == pytest.approx(0.5) assert kernel.integrate(0, 6) == pytest.approx(0.5) assert kernel.integrate(1, 2) == pytest.approx(7 / 50) assert kernel.integrate(5, 10) == pytest.approx(0) np.testing.assert_allclose(kernel.integrate([0], [5]), [0.5]) np.testing.assert_allclose(kernel.integrate([0, 0, 1, 5], [5, 6, 2, 10]), [0.5, 0.5, 7 / 50, 0])
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_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))