Skip to content

Commit

Permalink
improve names and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
clbarnes committed Jan 23, 2024
1 parent 04b7129 commit c197511
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 96 deletions.
4 changes: 2 additions & 2 deletions nblast-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use numpy::{IntoPyArray, PyArray1, PyArray2, PyReadonlyArray1, PyReadonlyArray2}

use nblast::nalgebra::base::{Unit, Vector3};
use nblast::{
neurons::kiddo::ExactKiddoTangentsAlphas as Neuron, BinLookup, NblastArena, NeuronIdx,
Precision, RangeTable, ScoreCalc, ScoreMatrixBuilder, Symmetry, TangentAlpha,
neurons::kiddo::ExactKiddoNeuron as Neuron, BinLookup, NblastArena, NeuronIdx, Precision,
RangeTable, ScoreCalc, ScoreMatrixBuilder, Symmetry, TangentAlpha,
};

use nblast::rayon;
Expand Down
90 changes: 38 additions & 52 deletions nblast-rs/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use csv::ReaderBuilder;
use fastrand::Rng;

#[cfg(feature = "bosque")]
use nblast::neurons::bosque::BosqueTangentsAlphas;
use nblast::neurons::bosque::BosqueNeuron;
#[cfg(feature = "kiddo")]
use nblast::neurons::kiddo::{ExactKiddoTangentsAlphas, KiddoTangentsAlphas};
use nblast::neurons::kiddo::{ExactKiddoNeuron, KiddoNeuron};
#[cfg(feature = "nabo")]
use nblast::neurons::nabo::NaboTangentsAlphas;
use nblast::neurons::rstar::RStarTangentsAlphas;
use nblast::neurons::nabo::NaboNeuron;
use nblast::neurons::rstar::RstarNeuron;
use nblast::{
BinLookup, NblastArena, NblastNeuron, Point3, Precision, QueryNeuron, RangeTable, ScoreCalc,
ScoreMatrixBuilder, TangentAlpha,
Expand Down Expand Up @@ -250,74 +250,72 @@ fn get_score_fn() -> ScoreCalc {

fn bench_query_rstar(b: &mut Bencher) {
let score_fn = get_score_fn();
let query =
RStarTangentsAlphas::new(&read_points(NAMES[0]), N_NEIGHBORS).expect("couldn't parse");
let target =
RStarTangentsAlphas::new(&read_points(NAMES[1]), N_NEIGHBORS).expect("couldn't parse");
let query = RstarNeuron::new(&read_points(NAMES[0]), N_NEIGHBORS).expect("couldn't parse");
let target = RstarNeuron::new(&read_points(NAMES[1]), N_NEIGHBORS).expect("couldn't parse");

b.iter(|| query.query(&target, false, &score_fn))
}

fn bench_construction_rstar(b: &mut Bencher) {
let points = read_points(NAMES[0]);
b.iter(|| RStarTangentsAlphas::new(&points, N_NEIGHBORS).expect("couldn't parse"));
b.iter(|| RstarNeuron::new(&points, N_NEIGHBORS).expect("couldn't parse"));
}

fn bench_construction_nabo(b: &mut Bencher) {
let points = read_points(NAMES[0]);
b.iter(|| NaboTangentsAlphas::new(points.clone(), N_NEIGHBORS))
b.iter(|| NaboNeuron::new(points.clone(), N_NEIGHBORS))
}

fn bench_construction_kiddo(b: &mut Bencher) {
let points = read_points(NAMES[0]);
b.iter(|| KiddoTangentsAlphas::new(points.clone(), N_NEIGHBORS))
b.iter(|| KiddoNeuron::new(points.clone(), N_NEIGHBORS))
}

fn bench_construction_exact_kiddo(b: &mut Bencher) {
let points = read_points(NAMES[0]);
b.iter(|| ExactKiddoTangentsAlphas::new(points.clone(), N_NEIGHBORS))
b.iter(|| ExactKiddoNeuron::new(points.clone(), N_NEIGHBORS))
}

fn bench_construction_bosque(b: &mut Bencher) {
let points = read_points(NAMES[0]);
b.iter(|| BosqueTangentsAlphas::new(points.clone(), N_NEIGHBORS))
b.iter(|| BosqueNeuron::new(points.clone(), N_NEIGHBORS))
}

fn bench_query_nabo(b: &mut Bencher) {
let score_fn = get_score_fn();
let query = NaboTangentsAlphas::new(read_points(NAMES[0]), N_NEIGHBORS);
let target = NaboTangentsAlphas::new(read_points(NAMES[1]), N_NEIGHBORS);
let query = NaboNeuron::new(read_points(NAMES[0]), N_NEIGHBORS);
let target = NaboNeuron::new(read_points(NAMES[1]), N_NEIGHBORS);

b.iter(|| query.query(&target, false, &score_fn))
}

fn bench_query_kiddo(b: &mut Bencher) {
let score_fn = get_score_fn();
let query = KiddoTangentsAlphas::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = KiddoTangentsAlphas::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();
let query = KiddoNeuron::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = KiddoNeuron::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();

b.iter(|| query.query(&target, false, &score_fn))
}

fn bench_query_exact_kiddo(b: &mut Bencher) {
let score_fn = get_score_fn();
let query = ExactKiddoTangentsAlphas::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = ExactKiddoTangentsAlphas::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();
let query = ExactKiddoNeuron::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = ExactKiddoNeuron::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();

b.iter(|| query.query(&target, false, &score_fn))
}

fn bench_query_bosque(b: &mut Bencher) {
let score_fn = get_score_fn();
let query = BosqueTangentsAlphas::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = BosqueTangentsAlphas::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();
let query = BosqueNeuron::new(read_points(NAMES[0]), N_NEIGHBORS).unwrap();
let target = BosqueNeuron::new(read_points(NAMES[1]), N_NEIGHBORS).unwrap();

b.iter(|| query.query(&target, false, &score_fn))
}

fn bench_construction_with_tangents_rstar(b: &mut Bencher) {
let points = read_points(NAMES[0]);
let neuron = RStarTangentsAlphas::new(&points, N_NEIGHBORS).expect("couldn't parse");
let neuron = RstarNeuron::new(&points, N_NEIGHBORS).expect("couldn't parse");
let tangents_alphas: Vec<_> = neuron
.tangents()
.zip(neuron.alphas())
Expand All @@ -326,14 +324,14 @@ fn bench_construction_with_tangents_rstar(b: &mut Bencher) {
alpha: a,
})
.collect();
b.iter(|| RStarTangentsAlphas::new_with_tangents_alphas(&points, tangents_alphas.clone()));
b.iter(|| RstarNeuron::new_with_tangents_alphas(&points, tangents_alphas.clone()));
}

fn bench_arena_construction(b: &mut Bencher) {
let score_fn = get_score_fn();
let pointtangents: Vec<_> = NAMES
.iter()
.map(|n| RStarTangentsAlphas::new(&read_points(n), N_NEIGHBORS).expect("couldn't parse"))
.map(|n| RstarNeuron::new(&read_points(n), N_NEIGHBORS).expect("couldn't parse"))
.collect();
b.iter(|| {
let mut arena = NblastArena::new(score_fn.clone(), false);
Expand All @@ -346,47 +344,39 @@ fn bench_arena_construction(b: &mut Bencher) {
fn bench_arena_query(b: &mut Bencher) {
let mut arena = NblastArena::new(get_score_fn(), false);
let p0 = read_points(NAMES[0]);
let idx0 =
arena.add_neuron(RStarTangentsAlphas::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let idx0 = arena.add_neuron(RstarNeuron::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let p1 = read_points(NAMES[1]);
let idx1 =
arena.add_neuron(RStarTangentsAlphas::new(&p1, N_NEIGHBORS).expect("couldn't parse"));
let idx1 = arena.add_neuron(RstarNeuron::new(&p1, N_NEIGHBORS).expect("couldn't parse"));

b.iter(|| arena.query_target(idx0, idx1, false, &None));
}

fn bench_arena_query_norm(b: &mut Bencher) {
let mut arena = NblastArena::new(get_score_fn(), false);
let p0 = read_points(NAMES[0]);
let idx0 =
arena.add_neuron(RStarTangentsAlphas::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let idx0 = arena.add_neuron(RstarNeuron::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let p1 = read_points(NAMES[1]);
let idx1 =
arena.add_neuron(RStarTangentsAlphas::new(&p1, N_NEIGHBORS).expect("couldn't parse"));
let idx1 = arena.add_neuron(RstarNeuron::new(&p1, N_NEIGHBORS).expect("couldn't parse"));

b.iter(|| arena.query_target(idx0, idx1, true, &None));
}

fn bench_arena_query_geom(b: &mut Bencher) {
let mut arena = NblastArena::new(get_score_fn(), false);
let p0 = read_points(NAMES[0]);
let idx0 =
arena.add_neuron(RStarTangentsAlphas::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let idx0 = arena.add_neuron(RstarNeuron::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let p1 = read_points(NAMES[1]);
let idx1 =
arena.add_neuron(RStarTangentsAlphas::new(&p1, N_NEIGHBORS).expect("couldn't parse"));
let idx1 = arena.add_neuron(RstarNeuron::new(&p1, N_NEIGHBORS).expect("couldn't parse"));

b.iter(|| arena.query_target(idx0, idx1, false, &Some(nblast::Symmetry::GeometricMean)));
}

fn bench_arena_query_norm_geom(b: &mut Bencher) {
let mut arena = NblastArena::new(get_score_fn(), false);
let p0 = read_points(NAMES[0]);
let idx0 =
arena.add_neuron(RStarTangentsAlphas::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let idx0 = arena.add_neuron(RstarNeuron::new(&p0, N_NEIGHBORS).expect("couldn't parse"));
let p1 = read_points(NAMES[1]);
let idx1 =
arena.add_neuron(RStarTangentsAlphas::new(&p1, N_NEIGHBORS).expect("couldn't parse"));
let idx1 = arena.add_neuron(RstarNeuron::new(&p1, N_NEIGHBORS).expect("couldn't parse"));

b.iter(|| arena.query_target(idx0, idx1, true, &Some(nblast::Symmetry::GeometricMean)));
}
Expand All @@ -397,9 +387,7 @@ fn bench_all_to_all_serial_rstar(b: &mut Bencher) {
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(
arena.add_neuron(
RStarTangentsAlphas::new(&points, N_NEIGHBORS).expect("couldn't parse"),
),
arena.add_neuron(RstarNeuron::new(&points, N_NEIGHBORS).expect("couldn't parse")),
);
}

Expand All @@ -411,7 +399,7 @@ fn bench_all_to_all_serial_nabo(b: &mut Bencher) {
let mut idxs = Vec::new();
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(arena.add_neuron(NaboTangentsAlphas::new(points, N_NEIGHBORS)));
idxs.push(arena.add_neuron(NaboNeuron::new(points, N_NEIGHBORS)));
}

b.iter(|| arena.queries_targets(&idxs, &idxs, false, &None, None));
Expand All @@ -422,7 +410,7 @@ fn bench_all_to_all_serial_kiddo(b: &mut Bencher) {
let mut idxs = Vec::new();
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(arena.add_neuron(KiddoTangentsAlphas::new(points, N_NEIGHBORS).unwrap()));
idxs.push(arena.add_neuron(KiddoNeuron::new(points, N_NEIGHBORS).unwrap()));
}

b.iter(|| arena.queries_targets(&idxs, &idxs, false, &None, None));
Expand All @@ -433,7 +421,7 @@ fn bench_all_to_all_serial_exact_kiddo(b: &mut Bencher) {
let mut idxs = Vec::new();
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(arena.add_neuron(ExactKiddoTangentsAlphas::new(points, N_NEIGHBORS).unwrap()));
idxs.push(arena.add_neuron(ExactKiddoNeuron::new(points, N_NEIGHBORS).unwrap()));
}

b.iter(|| arena.queries_targets(&idxs, &idxs, false, &None, None));
Expand All @@ -444,7 +432,7 @@ fn bench_all_to_all_serial_bosque(b: &mut Bencher) {
let mut idxs = Vec::new();
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(arena.add_neuron(BosqueTangentsAlphas::new(points, N_NEIGHBORS).unwrap()));
idxs.push(arena.add_neuron(BosqueNeuron::new(points, N_NEIGHBORS).unwrap()));
}

b.iter(|| arena.queries_targets(&idxs, &idxs, false, &None, None));
Expand All @@ -457,20 +445,18 @@ fn bench_all_to_all_parallel(b: &mut Bencher) {
for name in NAMES.iter() {
let points = read_points(name);
idxs.push(
arena.add_neuron(
RStarTangentsAlphas::new(&points, N_NEIGHBORS).expect("couldn't parse"),
),
arena.add_neuron(RstarNeuron::new(&points, N_NEIGHBORS).expect("couldn't parse")),
);
}

b.iter(|| arena.queries_targets(&idxs, &idxs, false, &None, None));
}

fn make_smatb_kiddo() -> ScoreMatrixBuilder<ExactKiddoTangentsAlphas> {
fn make_smatb_kiddo() -> ScoreMatrixBuilder<ExactKiddoNeuron> {
let (all_points, matches, nonmatches) = match_nonmatch(10);
let neurons: Vec<_> = all_points
.into_iter()
.map(|ps| ExactKiddoTangentsAlphas::new(ps, N_NEIGHBORS).unwrap())
.map(|ps| ExactKiddoNeuron::new(ps, N_NEIGHBORS).unwrap())
.collect();

let mut smatb = ScoreMatrixBuilder::new(neurons.clone(), 1991);
Expand Down
17 changes: 13 additions & 4 deletions nblast-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! Both are [Neuron](trait.Neuron.html)s.
//!
//! [PointsTangentsAlphas](struct.PointsTangentsAlphas.html) and
//! [RStarTangentsAlphas](struct.RStarTangentsAlphas.html) implement these, respectively.
//! [RstarNeuron](struct.RstarNeuron.html) implement these, respectively.
//! Both can be created with pre-calculated tangents and alphas, or calculate them on instantiation.
//!
//! The [NblastArena](struct.NblastArena.html) contains a collection of `TargetNeuron`s
Expand Down Expand Up @@ -523,6 +523,14 @@ pub enum ScoreCalc {
}

impl ScoreCalc {
/// Construct a table from `N` dist bins, `M` dot bins,
/// and the values in `(N-1)*(M-1)` cells.
///
/// The bins vecs must be monotonically increasing.
/// The first and last values of each
/// are effectively ignored and replaced by -inf and +inf respectively.
/// Values are given in dot-major order, i.e.
/// cells in the same dist bin are next to each other.
pub fn table_from_bins(
dists: Vec<Precision>,
dots: Vec<Precision>,
Expand All @@ -534,6 +542,7 @@ impl ScoreCalc {
)?))
}

/// Apply the score function.
pub fn calc(&self, dist_dot: &DistDot) -> Precision {
match self {
// Self::Func(func) => func(dist_dot),
Expand Down Expand Up @@ -1068,8 +1077,8 @@ mod test {
// let q_points = make_points(&[0., 0., 0.], &[1.0, 0.0, 0.0], 10);
// let query = PointsTangentsAlphas::new(q_points.clone(), N_NEIGHBORS)
// .expect("Query construction failed");
// let query2 = RStarTangentsAlphas::new(&q_points, N_NEIGHBORS).expect("Construction failed");
// let target = RStarTangentsAlphas::new(
// let query2 = RstarNeuron::new(&q_points, N_NEIGHBORS).expect("Construction failed");
// let target = RstarNeuron::new(
// &make_points(&[0.5, 0., 0.], &[1.1, 0., 0.], 10),
// N_NEIGHBORS,
// )
Expand Down Expand Up @@ -1173,7 +1182,7 @@ mod test {
// #[test]
// fn alpha_changes_results() {
// let (points, _, _) = tangent_data();
// let neuron = RStarTangentsAlphas::new(points, N_NEIGHBORS).unwrap();
// let neuron = RstarNeuron::new(points, N_NEIGHBORS).unwrap();
// let score_calc = ScoreCalc::Func(Box::new(|dd: &DistDot| dd.dot));

// let sh = neuron.self_hit(&score_calc, false);
Expand Down
18 changes: 9 additions & 9 deletions nblast-rs/src/neurons/bosque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
use bosque::tree::{build_tree, build_tree_with_indices, nearest_k, nearest_one};

#[derive(Debug, Clone, PartialEq)]
pub struct BosqueTangentsAlphas {
pub struct BosqueNeuron {
points: Vec<Point3>,
tangents_alphas: Vec<TangentAlpha>,
}

impl BosqueTangentsAlphas {
impl BosqueNeuron {
pub fn new(mut points: Vec<Point3>, k: usize) -> Result<Self, &'static str> {
if points.len() < k {
return Err("Not enough points to calculate neighborhood");
Expand Down Expand Up @@ -54,7 +54,7 @@ impl BosqueTangentsAlphas {
}
}

impl NblastNeuron for BosqueTangentsAlphas {
impl NblastNeuron for BosqueNeuron {
fn len(&self) -> usize {
self.points.len()
}
Expand All @@ -72,7 +72,7 @@ impl NblastNeuron for BosqueTangentsAlphas {
}
}

impl QueryNeuron for BosqueTangentsAlphas {
impl QueryNeuron for BosqueNeuron {
fn query_dist_dots<'a>(
&'a self,
target: &'a impl crate::TargetNeuron,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl QueryNeuron for BosqueTangentsAlphas {
}
}

impl TargetNeuron for BosqueTangentsAlphas {
impl TargetNeuron for BosqueNeuron {
fn nearest_match_dist_dot(
&self,
point: &Point3,
Expand Down Expand Up @@ -153,15 +153,15 @@ mod tests {
#[cfg(feature = "kiddo")]
#[test]
fn expected_idxs() {
use crate::neurons::kiddo::ExactKiddoTangentsAlphas;
use crate::neurons::kiddo::ExactKiddoNeuron;

let mut rng = Rng::with_seed(1991);
let pts = random_points(100, &mut rng);
let t_kid = ExactKiddoTangentsAlphas::new(pts.clone(), 5).unwrap();
let b_kid = BosqueTangentsAlphas::new(pts, 5).unwrap();
let t_kid = ExactKiddoNeuron::new(pts.clone(), 5).unwrap();
let b_kid = BosqueNeuron::new(pts, 5).unwrap();

let pts2 = random_points(5, &mut rng);
let query = ExactKiddoTangentsAlphas::new(pts2, 5).unwrap();
let query = ExactKiddoNeuron::new(pts2, 5).unwrap();

let t_res: Vec<_> = query.query_dist_dots(&t_kid, false).collect();
let b_res: Vec<_> = query.query_dist_dots(&b_kid, false).collect();
Expand Down
Loading

0 comments on commit c197511

Please sign in to comment.