pub fn read_matrix<P: AsRef<Path>>(path: P) -> Result<Array2<f64>> {
    let header_match = Regex::new(r"NUMPY\x01\x00(?s:..)\{'descr': ?'<f8', ?'fortran_order': ?False, ?'shape': ?\((\d+), ?(\d+)\)\} *\n").unwrap();
    let mut reader = File::open(path.as_ref())?;
    let mut content = vec![];
    reader.read_to_end(&mut content)?;
    // 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 res: Result<(usize, usize, usize)> = {
        let captures = header_match.captures(&content)
            .ok_or(helpful_complaint(path.as_ref(), &content))?;
        Ok((// where the full match ends
            captures.get(0).unwrap().end(),
            // The shape of the array as described in the metadata
            str::from_utf8(&captures[1]).unwrap().parse().unwrap(),
            str::from_utf8(&captures[2]).unwrap().parse().unwrap()
        ))
    };
    let (skip, h, w) = res?;
    let content_f64 = unsafe {
        // O(N) array copy without memory overhead. 
        let length = content.len() - skip;
        assert_eq!(length, h*w*8,
            "The numpy file's array is the wrong length for a {}x{} array. \
            It should be {} elements, ({} bytes), but it is actually {} bytes.",
            h, w, h*w, h*w*8, length
        );
        ptr::copy((&content[skip..]).as_ptr(),
                  (&mut content[..length]).as_mut_ptr(),
                  length);
        content.set_len(length);
        // Treat the incoming data as floating point
        let mut out : Vec<f64> = ::std::mem::transmute(content);
        out.set_len(length/8);
        out
    };
    Ok(ArrayBase::from_shape_vec([h, w], content_f64)?)
}
/// Read a Numpy matrix into memory. Be careful if it's large. You could run out of memory.
///
/// You need to know the number of dimensions at compile time so for convenience, we assume you
/// need a matrix. Also, this method is fast but only works for native byte order.
Read a Numpy matrix into memory. Be careful if it's large. You could run out of memory.
You need to know the number of dimensions at compile time so for convenience, we assume you
need a matrix. Also, this method is fast but only works for native byte order.
Read a Numpy matrix into memory . Be careful if it ' s large . You could run out of memory . You need to know the number of dimensions at compile time so for convenience we assume you need a matrix . Also this method is fast but only works for native byte order .
pub fn read_matrix<P: AsRef<Path>>(path: P) -> Result<Array2<f64>> {
    let header_match = Regex::new(r"NUMPY\x01\x00(?s:..)\{'descr': ?'<f8', ?'fortran_order': ?False, ?'shape': ?\((\d+), ?(\d+)\)\} *\n").unwrap();
    let mut reader = File::open(path.as_ref())?;
    let mut content = vec![];
    reader.read_to_end(&mut content)?;
    // 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 res: Result<(usize, usize, usize)> = {
        let captures = header_match.captures(&content)
            .ok_or(helpful_complaint(path.as_ref(), &content))?;
        Ok((// where the full match ends
            captures.get(0).unwrap().end(),
            // The shape of the array as described in the metadata
            str::from_utf8(&captures[1]).unwrap().parse().unwrap(),
            str::from_utf8(&captures[2]).unwrap().parse().unwrap()
        ))
    };
    let (skip, h, w) = res?;
    let content_f64 = unsafe {
        // O(N) array copy without memory overhead. 
        let length = content.len() - skip;
        assert_eq!(length, h*w*8,
            "The numpy file's array is the wrong length for a {}x{} array. \
            It should be {} elements, ({} bytes), but it is actually {} bytes.",
            h, w, h*w, h*w*8, length
        );
        ptr::copy((&content[skip..]).as_ptr(),
                  (&mut content[..length]).as_mut_ptr(),
                  length);
        content.set_len(length);
        // Treat the incoming data as floating point
        let mut out : Vec<f64> = ::std::mem::transmute(content);
        out.set_len(length/8);
        out
    };
    Ok(ArrayBase::from_shape_vec([h, w], content_f64)?)
}
pub fn read_matrix < P : AsRef < Path > > ( path : P ) -> Result < Array2 < f64 > > { let header_match = Regex :: new ( r"NUMPY\x01\x00(?s:..)\{'descr': ?'<f8', ?'fortran_order': ?False, ?'shape': ?\((\d+), ?(\d+)\)\} *\n" ) . unwrap ( ) ; let mut reader = File :: open ( path . as_ref ( ) ) ? ; let mut content = vec ! [ ] ; reader . read_to_end ( & mut content ) ? ; // 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 res : Result < ( usize , usize , usize ) > = { let captures = header_match . captures ( & content ) . ok_or ( helpful_complaint ( path . as_ref ( ) , & content ) ) ? ; Ok ( ( // where the full match ends captures . get ( 0 ) . unwrap ( ) . end ( ) , // The shape of the array as described in the metadata str :: from_utf8 ( & captures [ 1 ] ) . unwrap ( ) . parse ( ) . unwrap ( ) , str :: from_utf8 ( & captures [ 2 ] ) . unwrap ( ) . parse ( ) . unwrap ( ) ) ) } ; let ( skip , h , w ) = res ? ; let content_f64 = unsafe { // O(N) array copy without memory overhead.  let length = content . len ( ) - skip ; assert_eq ! ( length h w 8 " \
 " h w h w h w 8 length ) ; ptr :: copy ( ( & content [ skip .. ] ) . as_ptr ( ) , ( & mut content [ .. length ] ) . as_mut_ptr ( ) , length ) ; content . set_len ( length ) ; // Treat the incoming data as floating point let mut out : Vec < f64 > = :: std :: mem :: transmute ( content ) ; out . set_len ( length / 8 ) ; out } ; Ok ( ArrayBase :: from_shape_vec ( [ h , w ] , content_f64 ) ? ) }
Read a Numpy matrix into memory.
Read a Numpy matrix into memory .
path
P
path
P