Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
add get_bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Milk committed Sep 9, 2020
1 parent 7ec82e6 commit 36ebe25
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neighborhood_analysis"
version = "0.1.4"
version = "0.1.5"
authors = ["Mr-Milk <zym.zym1220@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -28,6 +28,7 @@ rand = "0.7.3"
kdbush = "0.2.0"
rayon = "1.4.0"
rstar = "0.8.2"
spade = "1.8.2"

[profile.dev]
opt-level = 3
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ z_score = comb_bootstrap(X, Y, neighbors, ignore_self=True)

```python

def get_bbox(points_collections):
"""A utility function to return minimum bounding box list of polygons
Args:
points_collections: List[List[(float, float)]]; List of 2d points collections
Return:
A dictionary of the index of every points, with the index of its neighbors
"""

def get_point_neighbors(points, r):
"""A utility function to search for neighbors
Expand Down
2 changes: 1 addition & 1 deletion neighborhood_analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .neighborhood_analysis import CellCombs, get_point_neighbors, get_bbox_neighbors, comb_bootstrap
from .neighborhood_analysis import CellCombs, get_bbox, get_point_neighbors, get_bbox_neighbors, comb_bootstrap
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rand::seq::SliceRandom;
use itertools::Itertools;
use kdbush::KDBush;
use rstar::{RTree, RTreeObject, AABB};
use spade::{BoundingRect};
use std::collections::HashMap;
use counter::Counter;
use rayon::prelude::*;
Expand All @@ -18,12 +19,38 @@ use pyo3::wrap_pyfunction;
#[pymodule]
fn neighborhood_analysis(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<CellCombs>()?;
m.add_wrapped(wrap_pyfunction!(get_bbox))?;
m.add_wrapped(wrap_pyfunction!(get_point_neighbors))?;
m.add_wrapped(wrap_pyfunction!(get_bbox_neighbors))?;
m.add_wrapped(wrap_pyfunction!(comb_bootstrap))?;
Ok(())
}


/// A utility function to return minimum bounding box list of polygons
///
/// Args:
/// points_collections: List[List[(float, float)]]; List of 2d points collections
///
/// Return:
/// A dictionary of the index of every points, with the index of its neighbors
///
#[pyfunction]
fn get_bbox(points_collections: Vec<Vec<(f64, f64)>>)
-> Vec<(f64, f64, f64, f64)> {

let bbox: Vec<(f64, f64, f64, f64)> = points_collections.par_iter().map(|p| {
let points: Vec<[f64;2]> = p.iter().map(|ps| [ps.0, ps.1]).collect();
let rect = BoundingRect::from_points(points);
let lower: [f64;2] = rect.lower();
let upper: [f64;2] = rect.upper();
(lower[0], lower[1], upper[0], upper[1])
}).collect();

bbox
}


/// A utility function to search for point neighbors using kd-tree
///
/// Args:
Expand Down
20 changes: 12 additions & 8 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import neighborhood_analysis as na
from neighborhood_analysis import CellCombs, get_point_neighbors, get_bbox_neighbors,comb_bootstrap
from neighborhood_analysis import CellCombs, get_bbox, get_point_neighbors, get_bbox_neighbors,comb_bootstrap

from time import time

Expand All @@ -9,13 +9,17 @@
corr_types = np.random.choice(types, 10000)
points = [(x, y) for (x, y) in points]

bbox = []
for _ in range(100):
ix1, ix2 = np.random.choice(range(len(points)), 2)
if points[ix1][0] < points[ix2][0]:
bbox.append((*points[ix1], *points[ix2]))
else:
bbox.append((*points[ix2], *points[ix1]))
polygons = []
for _ in range(10000):
ixs = np.random.choice(range(len(points)), 5)
polygon = []
for x in ixs:
polygon.append(points[x])

start = time()
bbox = get_bbox(polygons)
end = time()
print(f"Get bbox used {(end-start):.2f}s")

start = time()
neighbors = get_bbox_neighbors(bbox, 2)
Expand Down

0 comments on commit 36ebe25

Please sign in to comment.