<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> import struct <EOL> import numpy <EOL> MNIST_IMAGE_MAGIC = <NUM_LIT> <EOL> MNIST_LABEL_MAGIC = <NUM_LIT> <EOL> class open_if_filename ( object ) : <EOL> def __init__ ( self , f , mode = '<STR_LIT:r>' , buffering = - <NUM_LIT:1> ) : <EOL> self . _f = f <EOL> self . _mode = mode <EOL> self . _buffering = buffering <EOL> self . _handle = None <EOL> def __enter__ ( self ) : <EOL> if isinstance ( self . _f , basestring ) : <EOL> self . _handle = open ( self . _f , self . _mode , self . _buffering ) <EOL> else : <EOL> self . _handle = self . _f <EOL> return self . _handle <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> if self . _handle is not self . _f : <EOL> self . _handle . close ( ) <EOL> def read_mnist_images ( fn , dtype = None ) : <EOL> """<STR_LIT>""" <EOL> with open_if_filename ( fn , '<STR_LIT:rb>' ) as f : <EOL> magic , number , rows , cols = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:16> ) ) <EOL> if magic != MNIST_IMAGE_MAGIC : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> fn ) <EOL> array = numpy . fromfile ( f , dtype = '<STR_LIT>' ) . reshape ( ( number , rows , cols ) ) <EOL> if dtype : <EOL> dtype = numpy . dtype ( dtype ) <EOL> if dtype . kind is '<STR_LIT:b>' : <EOL> array = array >= <NUM_LIT> <EOL> else : <EOL> array = array . astype ( dtype ) <EOL> if dtype . kind in ( '<STR_LIT:f>' , '<STR_LIT:c>' ) : <EOL> array /= <NUM_LIT> <EOL> return array <EOL> def read_mnist_labels ( fn ) : <EOL> """<STR_LIT>""" <EOL> with open_if_filename ( fn , '<STR_LIT:rb>' ) as f : <EOL> magic , number = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:8> ) ) <EOL> if magic != MNIST_LABEL_MAGIC : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> fn ) <EOL> array = numpy . fromfile ( f , dtype = '<STR_LIT>' ) <EOL> return array </s>
