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

Commit

Permalink
add neighbor components
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Milk committed Apr 8, 2021
1 parent e94c15e commit 0b4efe4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neighborhood_analysis"
version = "0.2.2"
version = "0.2.3"
authors = ["Mr-Milk <zym.zym1220@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion neighborhood_analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .neighborhood_analysis import (get_bbox,
from .neighborhood_analysis import (neighbor_components,
get_bbox,
get_bbox_neighbors,
get_point_neighbors,
comb_bootstrap,
Expand Down
1 change: 1 addition & 0 deletions neighborhood_analysis/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Tuple, Dict, Optional

def neighbor_components(neighbors: Dict[int, List[int]], types: Dict[int, str]) -> (List[int], List[str], List[List[int]]): ...

def get_bbox(points_collections: List[List[Tuple[float, float]]]) -> List[Tuple[float, float, float, float]]: ...

Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rand::thread_rng;
use std::collections::HashMap;

use kdbush::KDBush;
use counter::Counter;
use rayon::prelude::*;
use rstar::{RTree, RTreeObject, AABB};
use spade::BoundingRect;
Expand All @@ -23,9 +24,42 @@ fn neighborhood_analysis(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(get_bbox_neighbors))?;
m.add_class::<CellCombs>()?;
m.add_wrapped(wrap_pyfunction!(comb_bootstrap))?;
m.add_wrapped(wrap_pyfunction!(neighbor_components))?;
Ok(())
}



#[pyfunction]
pub fn neighbor_components(neighbors: HashMap<usize, Vec<usize>>, types: HashMap<usize, &str>)
-> (Vec<usize>, Vec<&str>, Vec<Vec<usize>>) {

let mut uni_types: HashMap<&str, i64> = HashMap::new();
for (_, t) in &types {
uni_types.entry(*t).or_insert(0);
}
let uni_types: Vec<&str> = uni_types.keys().map(|k| *k).collect_vec();
let mut cent_order: Vec<usize> = vec![];
let result: Vec<Vec<usize>> = neighbors.iter().map(|(cent, neigh)| {
let count: HashMap<&&str, usize> = neigh.iter().map(|i| &types[i]).collect::<Counter<_>>().into_map();
let mut result_v: Vec<usize> = vec![];
for t in &uni_types {
let v = match count.get(t) {
Some(v) => *v,
None => {0}
};
result_v.push(v);
}
cent_order.push(*cent);
result_v
}).collect();

(cent_order, uni_types, result)

}



/// get_bbox(points_collections)
/// --
///
Expand Down
25 changes: 18 additions & 7 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import numpy as np
from neighborhood_analysis import CellCombs, get_bbox, get_point_neighbors, get_bbox_neighbors, comb_bootstrap
from neighborhood_analysis import (CellCombs,
get_bbox,
get_point_neighbors,
get_bbox_neighbors,
comb_bootstrap,
neighbor_components)
from time import time

types = [str(i) for i in range(30)]
Expand All @@ -17,30 +22,36 @@
start = time()
bbox = get_bbox(polygons)
end = time()
print(f"Get bbox used {(end-start):.5f}s")
print(f"Get bbox used {(end - start):.5f}s")

start = time()
neighbors = get_bbox_neighbors(bbox, 2, labels=[i for i in range(0, len(bbox))])
end = time()
print(f"search bbox neighbors used {(end-start):.5f}s")

print(f"search bbox neighbors used {(end - start):.5f}s")

start = time()
neighbors = get_point_neighbors(points, 10.0)
end = time()
print(f"search point neighbors used {(end-start):.5f}s")
print(f"search point neighbors used {(end - start):.5f}s")

start = time()

cc = CellCombs(types, False)
results = cc.bootstrap(corr_types, neighbors, times=1000, ignore_self=True)

end = time()
print(f"neighborhood used {(end-start):.5f}s")
print(f"neighborhood used {(end - start):.5f}s")

s1 = time()
X = [bool(i) for i in np.random.choice([True, False], 10000)]
Y = [bool(i) for i in np.random.choice([True, False], 10000)]
v = comb_bootstrap(X, Y, neighbors, times=1000, ignore_self=True)
s2 = time()
print(f"marker co-exp used {(s2-s1):.5f}s")
print(f"marker co-exp used {(s2 - s1):.5f}s")

cents = [i for i in range(len(points))]
s1 = time()
ix, col, data = neighbor_components(dict(zip(cents, neighbors)), dict(zip(cents, corr_types)))
s2 = time()
print(ix, col, data)
print(f"neighbor components used {(s2 - s1):.5f}s")

0 comments on commit 0b4efe4

Please sign in to comment.