Skip to content
Snippets Groups Projects
Commit 1b666ede authored by Martin Larralde's avatar Martin Larralde
Browse files

Add method to find the best scoring position from a `StripedScores`

parent 81916263
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,11 @@ pub trait Symbol: Default + Sized + Copy + TryFrom<char> {
pub trait Alphabet: Debug + Copy + Default + 'static {
type Symbol: Symbol;
const K: usize;
/// Get the default symbol for this alphabet.
fn default_symbol() -> Self::Symbol {
Default::default()
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
......
......@@ -257,6 +257,7 @@ impl<V: Vector, const C: usize> StripedScores<V, C> {
}
impl<const C: usize> StripedScores<f32, C> {
/// Convert the striped scores to a vector of scores.
pub fn to_vec(&self) -> Vec<f32> {
let mut vec = Vec::with_capacity(self.length);
for i in 0..self.length {
......@@ -266,18 +267,37 @@ impl<const C: usize> StripedScores<f32, C> {
}
vec
}
/// Get the index of the highest scoring position.
///
/// ## Panic
/// Panics if the data buffer is empty.
pub fn argmax(&self) -> usize {
let mut best_pos = 0;
let mut best_score = self.data[0][0];
for i in 0..self.length {
let col = i / self.data.rows();
let row = i % self.data.rows();
if self.data[row][col] > best_score {
best_pos = i;
best_score = self.data[row][col];
}
}
best_pos
}
}
#[cfg(target_feature = "avx2")]
impl<const C: usize> StripedScores<__m256, C> {
/// Convert the striped scores to a vector of scores.
pub fn to_vec(&self) -> Vec<f32> {
// NOTE(@althonos): Because in AVX2 the __m256 vector is actually
// two independent __m128, the shuffling creates
// intrication in the results.
#[rustfmt::skip]
const COLS: &[usize] = &[
0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27,
4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31,
0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27,
4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31,
];
let mut col = 0;
......@@ -293,4 +313,27 @@ impl<const C: usize> StripedScores<__m256, C> {
}
vec
}
/// Get the index of the highest scoring position.
///
/// ## Panic
/// Panics if the data buffer is empty.
pub fn argmax(&self) -> usize {
let mut best_pos = 0;
let mut best_score = self.data[0][0];
let mut col = 0;
let mut row = 0;
for i in 0..self.length {
if self.data[row][col] > best_score {
best_pos = i;
best_score = self.data[row][col];
}
row += 1;
if row == self.data.rows() {
row = 0;
col += 1;
}
}
best_pos
}
}
......@@ -162,11 +162,10 @@ pub struct Background<A: Alphabet, const K: usize> {
impl<A: Alphabet, const K: usize> Background<A, K> {
pub fn uniform() -> Self {
let mut frequencies = [0.0; K];
for i in 0..K {
if i != A::default_symbol().as_index() {
frequencies[i] = 1.0 / ((K-1) as f32);
frequencies[i] = 1.0 / ((K - 1) as f32);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment