brain2qwerty 0.0.1

Brain2Qwerty V1/V2 MEG neural decoding inference in Rust (parity-tested vs Python)
Documentation
//! Character decoding for Brain2Qwerty V1 (29-class SpanishBCBL alphabet).

use crate::tensor::Tensor;

/// Number of character classes (matches `brain2qwerty_v1.utils.NUM_CLASSES`).
pub const NUM_CLASSES: usize = 29;

/// Map a class index to the display character used in parity tests.
pub fn char_from_index(idx: usize) -> char {
    match idx {
        0 => 's',
        1 => 'o',
        2 => 't',
        3 => 'e',
        4 => 'n',
        5 => 'c',
        6 => 'i',
        7 => 'a',
        8 => ' ',
        9 => 'd',
        10 => 'l',
        11 => 'r',
        12 => 'b',
        13 => '@',
        14 => 'z',
        15 => 'v',
        16 => 'f',
        17 => 'm',
        18 => 'u',
        19 => 'h',
        20 => 'p',
        21 => 'g',
        22 => 'q',
        23 => 'w',
        24 => 'x',
        25 => 'y',
        26 => 'j',
        27 => 'k',
        28 => '9',
        _ => '?',
    }
}

/// Argmax decode per keystroke row → concatenated string.
pub fn greedy_decode(logits: &Tensor) -> String {
    assert_eq!(logits.ndim(), 2);
    let n = logits.shape[0];
    let c = logits.shape[1];
    let mut out = String::new();
    for i in 0..n {
        let base = i * c;
        let mut best = 0usize;
        let mut best_v = logits.data[base];
        for j in 1..c {
            if logits.data[base + j] > best_v {
                best_v = logits.data[base + j];
                best = j;
            }
        }
        out.push(char_from_index(best));
    }
    out
}