def an_STSResult_nonaligned_centres(): region = open_cp.RectangularRegion(xmin=0, xmax=100, ymin=50, ymax=100) clusters = [ testmod.Cluster([9, 59.5], 10), testmod.Cluster([49, 50.5], 12) ] return testmod.STSResult(region, clusters)
def set_data_region(xcoords,ycoords): # Extracts the bounding regions maxx = max(xcoords) minx = min(xcoords) maxy = max(ycoords) miny = min(ycoords) region = open_cp.RectangularRegion(xmin=minx, xmax=maxx, ymin=miny, ymax=maxy) return region
def a_valid_predictor(): region = open_cp.RectangularRegion(0, 150, 0, 150) predictor = testmod.ProspectiveHotSpot(region) timestamps = [datetime(2017, 3, 1)] xcoords = [50] ycoords = [50] predictor.data = open_cp.TimedPoints.from_coords(timestamps, xcoords, ycoords) return predictor
def a_predictor(): region = open_cp.RectangularRegion(0, 100, 0, 200) mu = np.random.random((10, 5)) predictor = testmod.SEPPPredictor(region=region, grid_size=20, omega=1.2, theta=0.7, mu=mu) return mu, predictor
def test_STSTrainer_grid_coords(): trainer = a_custom_trainer() region = open_cp.RectangularRegion(xmin=5.2, ymin=4.5, xmax=0, ymax=0) new = trainer.grid_coords(region, 2) # Centres of grid will have offset of (6.2, 5.5) and each width/height is 2 assert (new.data.xcoords[0] == pytest.approx(0.2)) assert (new.data.xcoords[1] == pytest.approx(0.2)) assert (new.data.xcoords[2] == pytest.approx(2.2)) assert (new.data.ycoords[0] == pytest.approx(199.5)) assert (new.data.ycoords[1] == pytest.approx(199.5)) assert (new.data.ycoords[2] == pytest.approx(197.5))
def test__make_cells(): region = open_cp.RectangularRegion(0, 100, 0, 100) events = mock.Mock() events.xcoords = np.asarray([0, 25, 26, 90, 90]) events.ycoords = np.asarray([0, 0, 1, 90, 90]) times = [5, 6, 7, 1, 2] cells = testmod._make_cells(region, 20, events, times) assert [cells[0, 0] == [5]] assert [cells[0, 1] == [6, 7]] assert [cells[4, 4] == [1, 2]] assert [cells[3, 3] == []]
def test_SEPP_Predictor_predict_wrong_region_size(): region = open_cp.RectangularRegion(0, 100, 0, 150) mu = np.random.random((10, 5)) predictor = testmod.SEPPPredictor(region=region, grid_size=20, omega=1.2, theta=0.7, mu=mu) predictor.data = open_cp.TimedPoints.from_coords([], [], []) with pytest.raises(ValueError): predictor.predict(np.datetime64("2018-01-01"))
def test_RetroHotSpotGrid(): region = open_cp.RectangularRegion(xmin=0, xmax=500, ymin=100, ymax=500) r = testmod.RetroHotSpotGrid(region, grid_size=20) r.weight = TestWeight() times = [np.datetime64("2017-04-02")] * 3 r.data = open_cp.TimedPoints.from_coords(times, [0, 50, 50], [100, 120, 210]) grid = r.predict() assert (grid.intensity_matrix.shape == (20, 25)) # Count 1 for each event with l^\infty distance <= 50 from centre of grid point # (0,0) -> (10, 110) assert (grid.grid_risk(0, 0) == 2) assert (grid.grid_risk(2, 5) == 1) assert (grid.grid_risk(4, 1) == 1) assert (grid.grid_risk(5, 1) == 0)
def test_uniform_random(poisson_mock): poisson_mock.return_value = 5 region = open_cp.RectangularRegion(xmin=0, xmax=100, ymin=-20, ymax=-10) start = datetime(2017, 3, 10, 0) end = datetime(2017, 3, 20, 0) points = testmod.random_uniform(region, start, end, 100) poisson_mock.assert_called_with(lam=100) expected_times = [ np.datetime64(s) for s in ["2017-03-15", "2017-03-12", "2017-03-18", "2017-03-15", "2017-03-12"] ] expected_times = np.array(expected_times) expected_times.sort() assert (all(expected_times == points.timestamps)) assert (all(points.coords[0] == [80, 50, 20, 80, 50])) assert (all(points.coords[1] == [-18, -12, -15, -18, -12]))
def test_KernelSampler(rejection_sample_mock): rejection_sample_mock.return_value = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]) region = open_cp.RectangularRegion(10, 30, 50, 100) def kernel(pts): return pts[0] + 2 * pts[1] sampler = testmod.KernelSampler(region, kernel, None) points = sampler(size=3) np.testing.assert_allclose(points[0], [12, 14, 16]) np.testing.assert_allclose(points[1], [70, 75, 80]) kernel_in_use = rejection_sample_mock.call_args_list[0][0][0] assert (kernel_in_use([0, 0]) == 110) assert (kernel_in_use([0, 1]) == 10 + 100 * 2) assert (kernel_in_use([1, 0]) == 30 + 50 * 2) assert (kernel_in_use([1, 1]) == 30 + 100 * 2) np.testing.assert_allclose( kernel_in_use(np.array([[0, 0, 1, 1], [0, 1, 0, 1]])), [110, 210, 130, 230])
def an_STSResult(): region = open_cp.RectangularRegion(xmin=0, xmax=100, ymin=50, ymax=100) clusters = [testmod.Cluster([10, 60], 10), testmod.Cluster([50, 50], 20)] return testmod.STSResult(region, clusters)
def an_STSResult_overlapping_clusters(): region = open_cp.RectangularRegion(xmin=0, xmax=100, ymin=50, ymax=50) clusters = [testmod.Cluster([10, 60], 10), testmod.Cluster([15, 60], 15)] return testmod.STSResult(region, clusters)
np.random.seed(1) import open_cp.sources.ukpolice as ukpolice points = ukpolice.default_burglary_data() print("ukpolice, len-points") print(inspect.getfile(ukpolice)) print(len(points.timestamps)) # Use pyproj to make a more properly projected visualization of the data projected_points = open_cp.data.points_from_lon_lat(points, epsg=7405) points = projected_points bbox = points.bounding_box fig, ax = plt.subplots(figsize=(10, 10 * bbox.aspect_ratio)) ax.scatter(points.xcoords, points.ycoords, s=10, alpha=0.2) region = open_cp.RectangularRegion(bbox.xmin, bbox.xmax, bbox.ymin, bbox.ymax) print("bbox, bbox type, region, region type") print(bbox) print(type(bbox)) print(region) print(type(region)) print("") predictor = naive.ScipyKDE() predictor.data = points prediction = predictor.predict() print("predictor, points, prediction") print(type(predictor)) print(type(points))