/// Load a Numpy matrix as an mmap. This only consumes address space. (Part 1)
///
/// This is a two-step process because the Mmap needs to outlive the matrix.
Load a Numpy matrix as an mmap. This only consumes address space. (Part 1)
This is a two-step process because the Mmap needs to outlive the matrix.
Load a Numpy matrix as an mmap . This only consumes address space . ( Part 1 ) This is a two - step process because the Mmap needs to outlive the matrix .
pub fn open_matrix_mmap<P: AsRef<Path>>(path: P) -> Result<MatFile> {
    let header_match = Regex::new(r"NUMPY\x01\x00(?s:..)\{'descr': ?'<f8', ?'fortran_order': ?False, ?'shape': ?\((\d+), ?(\d+)\)\} *\n").unwrap();
    let mut reader = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(path.as_ref())?;
    let mut content = [0u8; 128];
    let bytes_read = reader.read(&mut content)?;
    assert!(bytes_read > 0, format!("The numpy file {} seems to be empty.", path.as_ref().display())); 
    // The skip and the nested context here is so that we can regex parse (with a borrow)
    // and then reuse the buffer as the array. This way there are never two copies in memory.
    let captures = header_match.captures(&content)
        .ok_or(helpful_complaint(path.as_ref(), &content))?;
    // where the full match ends
    let skip = captures.get(0).unwrap().end();
    // The shape of the array as described in the metadata
    let h = str::from_utf8(&captures[1]).unwrap().parse().unwrap();
    let w = str::from_utf8(&captures[2]).unwrap().parse().unwrap();
    Ok(MatFile(h, w, Mmap::open_with_offset(&reader, Protection::ReadWrite, skip, h*w*8)?))
}
pub fn open_matrix_mmap < P : AsRef < Path > > ( path : P ) -> Result < MatFile > { let header_match = Regex :: new ( r"NUMPY\x01\x00(?s:..)\{'descr': ?'<f8', ?'fortran_order': ?False, ?'shape': ?\((\d+), ?(\d+)\)\} *\n" ) . unwrap ( ) ; let mut reader = OpenOptions :: new ( ) . read ( true ) . write ( true ) . create ( true ) . open ( path . as_ref ( ) ) ? ; let mut content = [ 0u8 ; 128 ] ; let bytes_read = reader . read ( & mut content ) ? ; assert ! ( bytes_read 0 format ( " " path as_ref ( ) display ( ) ) ) ; // The skip and the nested context here is so that we can regex parse (with a borrow) // and then reuse the buffer as the array. This way there are never two copies in memory. let captures = header_match . captures ( & content ) . ok_or ( helpful_complaint ( path . as_ref ( ) , & content ) ) ? ; // where the full match ends let skip = captures . get ( 0 ) . unwrap ( ) . end ( ) ; // The shape of the array as described in the metadata let h = str :: from_utf8 ( & captures [ 1 ] ) . unwrap ( ) . parse ( ) . unwrap ( ) ; let w = str :: from_utf8 ( & captures [ 2 ] ) . unwrap ( ) . parse ( ) . unwrap ( ) ; Ok ( MatFile ( h , w , Mmap :: open_with_offset ( & reader , Protection :: ReadWrite , skip , h * w * 8 ) ? ) ) }
Load a Numpy matrix as an mmap.
Load a Numpy matrix as an mmap .
path
P