Saf

Struct Saf 

Source
pub struct Saf<const N: usize> { /* private fields */ }
Expand description

Joint SAF likelihood matrix for N populations.

Internally, the matrix is represented with each site in continuous memory. That is, the first values are those from the first site of all populations, then comes the next site, and so on. Saf::shape gives the number of values per site per population. This should only be important when operating directly on the underlying storage, e.g. using Saf::as_slice or Saf::as_mut_slice.

Implementations§

Source§

impl<const N: usize> Saf<N>

Source

pub fn as_mut_slice(&mut self) -> &mut [f32]

Returns a mutable reference to the values of the SAF as a flat slice.

See the Saf documentation for details on the storage order.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, f32>

Returns an iterator over mutable references to all values in the SAF.

See the Saf documentation for details on the storage order.

Source

pub fn iter_blocks(&self, block_size: usize) -> BlockIter<'_, N>

Returns an iterator over blocks of sites in the SAF.

If the number of sites in the SAF is not evenly divided by block_size, the last block will be smaller than the others.

§Examples
use winsfs_core::saf1d;
let saf = saf1d![
    [0.0,  0.1,  0.2],
    [0.3,  0.4,  0.5],
    [0.6,  0.7,  0.8],
    [0.9,  0.10, 0.11],
    [0.12, 0.13, 0.14],
];
let mut iter = saf.iter_blocks(2);
assert_eq!(
    iter.next().unwrap(),
    saf1d![[0.0, 0.1, 0.2], [0.3, 0.4, 0.5]].view()
);
assert_eq!(
    iter.next().unwrap(),
    saf1d![[0.6, 0.7, 0.8], [0.9, 0.10, 0.11]].view()
);
assert_eq!(iter.next().unwrap(), saf1d![[0.12, 0.13, 0.14]].view());
assert!(iter.next().is_none());
Source

pub fn iter_sites(&self) -> SiteIter<'_, N>

Returns an iterator over the sites in the SAF.

§Examples
use winsfs_core::saf1d;
let saf = saf1d![
    [0.0,  0.1,  0.2],
    [0.3,  0.4,  0.5],
    [0.6,  0.7,  0.8],
];
let mut iter = saf.iter_sites();
assert_eq!(iter.next().unwrap().as_slice(), [0.0,  0.1,  0.2]);
assert_eq!(iter.next().unwrap().as_slice(), [0.3,  0.4,  0.5]);
assert_eq!(iter.next().unwrap().as_slice(), [0.6,  0.7,  0.8]);
assert!(iter.next().is_none());
Source

pub fn new(values: Vec<f32>, shape: [usize; N]) -> Result<Self, ShapeError<N>>

Returns a new SAF.

The number of provided values must be a multiple of the sum of shapes. See the Saf documentation for details on the storage order.

§Examples
use winsfs_core::{saf::Saf, saf2d};
let vec = vec![0.0, 0.1, 0.2, 1.0, 1.1, 0.3, 0.4, 0.5, 1.2, 1.3];
let shape = [3, 2];
assert_eq!(
    Saf::new(vec, shape).unwrap(),
    saf2d![
        [0.0,  0.1,  0.2 ; 1.0, 1.1],
        [0.3,  0.4,  0.5 ; 1.2, 1.3],
    ],
);

A ShapeError is thrown if the shape does not fit the number of values:

use winsfs_core::saf::Saf;
let vec = vec![0.0, 0.1, 0.2, 1.0, 1.1, 0.3, 0.4, 0.5, 1.2, 1.3];
let wrong_shape = [4, 2];
assert!(Saf::new(vec, wrong_shape).is_err());
Source

pub fn par_iter_blocks(&self, block_size: usize) -> ParBlockIter<'_, N>

Returns a parallel iterator over the blocks in the SAF.

This is the parallel version of Saf::iter_blocks. If the number of sites in the SAF is not evenly divided by block_size, the last block will be smaller than the others.

§Examples
use winsfs_core::{saf::SafView, saf1d};
use rayon::iter::ParallelIterator;
let saf = saf1d![
    [0.0,  0.1,  0.2],
    [0.3,  0.4,  0.5],
    [0.6,  0.7,  0.8],
    [0.9,  0.10, 0.11],
    [0.12, 0.13, 0.14],
];
let blocks: Vec<SafView<1>> = saf.par_iter_blocks(2).collect();
assert_eq!(blocks.len(), 3);
assert_eq!(
    blocks[0],
    saf1d![[0.0, 0.1, 0.2], [0.3, 0.4, 0.5]].view()
);
assert_eq!(
    blocks[1],
    saf1d![[0.6, 0.7, 0.8], [0.9, 0.10, 0.11]].view()
);
assert_eq!(blocks[2], saf1d![[0.12,  0.13,  0.14]].view());
Source

pub fn par_iter_sites(&self) -> ParSiteIter<'_, N>

Returns a parallel iterator over the sites in the SAF.

This is the parallel version of Saf::iter_sites.

§Examples
use winsfs_core::saf1d;
use rayon::iter::ParallelIterator;
let saf = saf1d![
    [1.,  1.,  1.],
    [1.,  1.,  1.],
    [1.,  1.,  1.],
];
saf.par_iter_sites().all(|site| site.as_slice() == &[1., 1., 1.]);
Source

pub fn read<R>(readers: [ReaderV3<R>; N]) -> Result<Self>
where R: BufRead + Seek,

Creates a new SAF by reading intersecting sites among SAF readers.

SAF files contain values in log-space. The returned values will be exponentiated to get out of log-space.

§Panics

Panics if N == 0.

Source

pub fn read_from_banded<R>(readers: [ReaderV4<R>; N]) -> Result<Self>
where R: BufRead + Seek,

Creates a new SAF by reading intersecting sites among banded SAF readers.

SAF files contain values in log-space. The returned values will be exponentiated to get out of log-space.

Note that this simply fills all non-explicitly represented values in the banded SAF with zeros (after getting out of log-space). Hence, this amounts to in some sense “undoing” the banding.

§Panics

Panics if N == 0.

Source

pub fn shuffle<R>(&mut self, rng: &mut R)
where R: Rng,

Shuffles the SAF sitewise according to a random permutation.

Source

pub fn view(&self) -> SafView<'_, N>

Returns a view of the entire SAF.

Source

pub fn as_slice(&self) -> &[f32]

Returns the values of the SAF as a flat slice.

See the Saf documentation for details on the storage order.

Source

pub fn iter(&self) -> Iter<'_, f32>

Returns an iterator over all values in the SAF.

See the Saf documentation for details on the storage order.

Source

pub fn get_site(&self, index: usize) -> SiteView<'_, N>

Returns a single site in the SAF.

Source

pub fn sites(&self) -> usize

Returns the number of sites in the SAF.

Source

pub fn shape(&self) -> [usize; N]

Returns the shape of the SAF.

Trait Implementations§

Source§

impl<'a, const N: usize> AsSafView<N> for &'a Saf<N>

Source§

fn as_saf_view(&self) -> <Self as Lifetime<'_>>::Item

Returns a SAF view of self.
Source§

impl<const N: usize> AsSafView<N> for Saf<N>

Source§

fn as_saf_view(&self) -> <Self as Lifetime<'_>>::Item

Returns a SAF view of self.
Source§

impl<const N: usize> Clone for Saf<N>

Source§

fn clone(&self) -> Saf<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for Saf<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, const N: usize> IntoBlockIterator<N> for &'a Saf<N>

Source§

type Item = SafView<'a, N>

The type of each individual block.
Source§

type Iter = BlockIter<'a, N>

The type of iterator.
Source§

fn into_block_iter(self, block_size: usize) -> Self::Iter

Convert this type into an iterator over SAF blocks containing block_size sites per block.
Source§

impl<'a, const N: usize> IntoParallelBlockIterator<N> for &'a Saf<N>

Source§

type Item = SafView<'a, N>

The type of each individual block.
Source§

type Iter = ParBlockIter<'a, N>

The type of iterator.
Source§

fn into_par_block_iter(self, block_size: usize) -> Self::Iter

Convert this type into a parallel iterator over SAF blocks containing block_size sites per block.
Source§

impl<'a, const N: usize> IntoParallelSiteIterator<N> for &'a Saf<N>

Source§

type Item = SiteView<'a, N>

The type of each individual site.
Source§

type Iter = ParSiteIter<'a, N>

The type of iterator.
Source§

fn into_par_site_iter(self) -> Self::Iter

Convert this type into a parallel SAF site iterator.
Source§

impl<'a, const N: usize> IntoSiteIterator<N> for &'a Saf<N>

Source§

type Item = SiteView<'a, N>

The type of each individual site.
Source§

type Iter = SiteIter<'a, N>

The type of iterator.
Source§

fn into_site_iter(self) -> Self::Iter

Convert this type into a SAF site iterator.
Source§

impl<'a, 'b, const N: usize> Lifetime<'a> for &'b Saf<N>

Source§

type Item = SafView<'a, N>

The inner item, the lifetime of which should be tied to Self.
Source§

impl<'a, const N: usize> Lifetime<'a> for Saf<N>

Source§

type Item = SafView<'a, N>

The inner item, the lifetime of which should be tied to Self.
Source§

impl<const N: usize> PartialEq for Saf<N>

Source§

fn eq(&self, other: &Saf<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> StructuralPartialEq for Saf<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Saf<N>

§

impl<const N: usize> RefUnwindSafe for Saf<N>

§

impl<const N: usize> Send for Saf<N>

§

impl<const N: usize> Sync for Saf<N>

§

impl<const N: usize> Unpin for Saf<N>

§

impl<const N: usize> UnwindSafe for Saf<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V