<s> """<STR_LIT>""" <EOL> try : <EOL> import mmapfile <EOL> except : <EOL> pass <EOL> import pstat <EOL> import glob , re , string , types , os , numpy , struct , copy , time , tempfile , sys <EOL> from types import * <EOL> N = numpy <EOL> __version__ = <NUM_LIT:0.5> <EOL> def wrap ( f ) : <EOL> """<STR_LIT>""" <EOL> class W : <EOL> def __init__ ( self , f ) : <EOL> self . f = f <EOL> def __repr__ ( self ) : <EOL> x = apply ( self . f ) <EOL> if x : <EOL> return repr ( x ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> return W ( f ) <EOL> def cd ( directory ) : <EOL> """<STR_LIT>""" <EOL> os . chdir ( directory ) <EOL> return <EOL> def pwd ( ) : <EOL> """<STR_LIT>""" <EOL> return os . getcwd ( ) <EOL> pwd = wrap ( pwd ) <EOL> def ls ( d = '<STR_LIT:.>' ) : <EOL> """<STR_LIT>""" <EOL> os . system ( '<STR_LIT>' + d ) <EOL> return None <EOL> def rename ( source , dest ) : <EOL> """<STR_LIT>""" <EOL> infiles = glob . glob ( source ) <EOL> outfiles = [ ] <EOL> incutindex = string . index ( source , '<STR_LIT:*>' ) <EOL> outcutindex = string . index ( source , '<STR_LIT:*>' ) <EOL> findpattern1 = source [ <NUM_LIT:0> : incutindex ] <EOL> findpattern2 = source [ incutindex + <NUM_LIT:1> : ] <EOL> replpattern1 = dest [ <NUM_LIT:0> : incutindex ] <EOL> replpattern2 = dest [ incutindex + <NUM_LIT:1> : ] <EOL> for fname in infiles : <EOL> if incutindex > <NUM_LIT:0> : <EOL> newname = re . sub ( findpattern1 , replpattern1 , fname , <NUM_LIT:1> ) <EOL> if outcutindex < len ( dest ) - <NUM_LIT:1> : <EOL> if incutindex > <NUM_LIT:0> : <EOL> lastone = string . rfind ( newname , replpattern2 ) <EOL> newname = newname [ <NUM_LIT:0> : lastone ] + re . sub ( findpattern2 , replpattern2 , fname [ lastone : ] , <NUM_LIT:1> ) <EOL> else : <EOL> lastone = string . rfind ( fname , findpattern2 ) <EOL> if lastone < > - <NUM_LIT:1> : <EOL> newname = fname [ <NUM_LIT:0> : lastone ] <EOL> newname = newname + re . sub ( findpattern2 , replpattern2 , fname [ lastone : ] , <NUM_LIT:1> ) <EOL> os . rename ( fname , newname ) <EOL> return <EOL> def get ( namepatterns , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> fnames = [ ] <EOL> if type ( namepatterns ) in [ ListType , TupleType ] : <EOL> for item in namepatterns : <EOL> fnames = fnames + glob . glob ( item ) <EOL> else : <EOL> fnames = glob . glob ( namepatterns ) <EOL> if len ( fnames ) == <NUM_LIT:0> : <EOL> if verbose : <EOL> print '<STR_LIT>' + namepatterns + '<STR_LIT>' <EOL> return None <EOL> if verbose : <EOL> print fnames <EOL> elements = [ ] <EOL> for i in range ( len ( fnames ) ) : <EOL> file = open ( fnames [ i ] ) <EOL> newelements = map ( string . split , file . readlines ( ) ) <EOL> for i in range ( len ( newelements ) ) : <EOL> for j in range ( len ( newelements [ i ] ) ) : <EOL> try : <EOL> newelements [ i ] [ j ] = string . atoi ( newelements [ i ] [ j ] ) <EOL> except ValueError : <EOL> try : <EOL> newelements [ i ] [ j ] = string . atof ( newelements [ i ] [ j ] ) <EOL> except : <EOL> pass <EOL> elements = elements + newelements <EOL> if len ( elements ) == <NUM_LIT:1> : elements = elements [ <NUM_LIT:0> ] <EOL> return elements <EOL> def getstrings ( namepattern , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> fnames = glob . glob ( namepattern ) <EOL> if len ( fnames ) == <NUM_LIT:0> : <EOL> if verbose : <EOL> print '<STR_LIT>' + namepattern + '<STR_LIT>' <EOL> return None <EOL> if verbose : <EOL> print fnames <EOL> elements = [ ] <EOL> for filename in fnames : <EOL> file = open ( filename ) <EOL> newelements = map ( string . split , file . readlines ( ) ) <EOL> elements = elements + newelements <EOL> return elements <EOL> def put ( outlist , fname , writetype = '<STR_LIT:w>' , oneperline = <NUM_LIT:0> , delimit = '<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> if type ( outlist ) in [ N . ArrayType ] : <EOL> aput ( outlist , fname , writetype ) <EOL> return <EOL> if type ( outlist [ <NUM_LIT:0> ] ) not in [ ListType , TupleType ] : <EOL> outfile = open ( fname , writetype ) <EOL> if not oneperline : <EOL> outlist = pstat . list2string ( outlist , delimit ) <EOL> outfile . write ( outlist ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> else : <EOL> for item in outlist : <EOL> outfile . write ( str ( item ) + '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> else : <EOL> outfile = open ( fname , writetype ) <EOL> for row in outlist : <EOL> outfile . write ( pstat . list2string ( row , delimit ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> return None <EOL> def isstring ( x ) : <EOL> if type ( x ) == StringType : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def aget ( namepattern , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> fnames = glob . glob ( namepattern ) <EOL> if len ( fnames ) == <NUM_LIT:0> : <EOL> if verbose : <EOL> print '<STR_LIT>' + namepattern + '<STR_LIT>' <EOL> return None <EOL> if verbose : <EOL> print fnames <EOL> elements = [ ] <EOL> for filename in fnames : <EOL> file = open ( filename ) <EOL> newelements = file . readlines ( ) <EOL> del_list = [ ] <EOL> for row in range ( len ( newelements ) ) : <EOL> if ( newelements [ row ] [ <NUM_LIT:0> ] == '<STR_LIT:%>' or newelements [ row ] [ <NUM_LIT:0> ] == '<STR_LIT:#>' <EOL> or len ( newelements [ row ] ) == <NUM_LIT:1> ) : <EOL> del_list . append ( row ) <EOL> del_list . reverse ( ) <EOL> for i in del_list : <EOL> newelements . pop ( i ) <EOL> newelements = map ( string . split , newelements ) <EOL> for i in range ( len ( newelements ) ) : <EOL> for j in range ( len ( newelements [ i ] ) ) : <EOL> try : <EOL> newelements [ i ] [ j ] = string . atof ( newelements [ i ] [ j ] ) <EOL> except : <EOL> pass <EOL> elements = elements + newelements <EOL> for row in range ( len ( elements ) ) : <EOL> if N . add . reduce ( N . array ( map ( isstring , elements [ row ] ) ) ) == len ( elements [ row ] ) : <EOL> print "<STR_LIT>" <EOL> return elements <EOL> try : <EOL> elements = N . array ( elements ) <EOL> except TypeError : <EOL> elements = N . array ( elements , '<STR_LIT:O>' ) <EOL> return elements <EOL> def aput ( outarray , fname , writetype = '<STR_LIT:w>' , delimit = '<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> outfile = open ( fname , writetype ) <EOL> if len ( outarray . shape ) == <NUM_LIT:1> : <EOL> outarray = outarray [ N . NewAxis , : ] <EOL> if len ( outarray . shape ) > <NUM_LIT:2> : <EOL> raise TypeError , "<STR_LIT>" <EOL> else : <EOL> for row in outarray : <EOL> outfile . write ( string . join ( map ( str , row ) , delimit ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> return None <EOL> def bget ( imfile , shp = None , unpackstr = N . int16 , bytesperpixel = <NUM_LIT> , sliceinit = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if imfile [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> return CORget ( imfile ) <EOL> if imfile [ - <NUM_LIT:2> : ] == '<STR_LIT>' : <EOL> return mrget ( imfile , unpackstr ) <EOL> if imfile [ - <NUM_LIT:4> : ] == '<STR_LIT>' : <EOL> return brikget ( imfile , unpackstr , shp ) <EOL> if imfile [ - <NUM_LIT:3> : ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return mincget ( imfile , unpackstr , shp ) <EOL> if imfile [ - <NUM_LIT:3> : ] == '<STR_LIT>' : <EOL> return mghbget ( imfile , unpackstr , shp ) <EOL> if imfile [ - <NUM_LIT:6> : ] == '<STR_LIT>' or imfile [ - <NUM_LIT:6> : ] == '<STR_LIT>' : <EOL> if shp == None : <EOL> return mghbget ( imfile , unpackstr = unpackstr , bytesperpixel = bytesperpixel , sliceinit = sliceinit ) <EOL> else : <EOL> return mghbget ( imfile , shp [ <NUM_LIT:0> ] , shp [ <NUM_LIT:1> ] , shp [ <NUM_LIT:2> ] , unpackstr , bytesperpixel , sliceinit ) <EOL> def CORget ( infile ) : <EOL> """<STR_LIT>""" <EOL> d = braw ( infile , N . Int8 ) <EOL> d . shape = ( <NUM_LIT> , <NUM_LIT> ) <EOL> d = N . where ( N . greater_equal ( d , <NUM_LIT:0> ) , d , <NUM_LIT> + d ) <EOL> return d <EOL> def mincget ( imfile , unpackstr = N . int16 , shp = None ) : <EOL> """<STR_LIT>""" <EOL> if shp == None : <EOL> shp = ( - <NUM_LIT:1> , <NUM_LIT:20> , <NUM_LIT:64> , <NUM_LIT:64> ) <EOL> os . system ( '<STR_LIT>' + <EOL> imfile + '<STR_LIT>' ) <EOL> try : <EOL> d = braw ( '<STR_LIT>' , unpackstr ) <EOL> except : <EOL> print "<STR_LIT>" + imfile <EOL> raise IOError , "<STR_LIT>" <EOL> print shp , d . shape <EOL> d . shape = shp <EOL> os . system ( '<STR_LIT>' ) <EOL> return d <EOL> def brikget ( imfile , unpackstr = N . int16 , shp = None ) : <EOL> """<STR_LIT>""" <EOL> if shp == None : <EOL> shp = ( - <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> try : <EOL> file = open ( imfile , "<STR_LIT:rb>" ) <EOL> except : <EOL> print "<STR_LIT>" + imfile <EOL> raise IOError , "<STR_LIT>" <EOL> try : <EOL> header = imfile [ <NUM_LIT:0> : - <NUM_LIT:4> ] + '<STR_LIT>' <EOL> lines = open ( header ) . readlines ( ) <EOL> for i in range ( len ( lines ) ) : <EOL> if string . find ( lines [ i ] , '<STR_LIT>' ) < > - <NUM_LIT:1> : <EOL> dims = string . split ( lines [ i + <NUM_LIT:2> ] [ <NUM_LIT:0> : string . find ( lines [ i + <NUM_LIT:2> ] , '<STR_LIT>' ) ] ) <EOL> dims = map ( string . atoi , dims ) <EOL> if string . find ( lines [ i ] , '<STR_LIT>' ) < > - <NUM_LIT:1> : <EOL> count = string . atoi ( string . split ( lines [ i + <NUM_LIT:1> ] ) [ <NUM_LIT:2> ] ) <EOL> mults = [ ] <EOL> for j in range ( int ( N . ceil ( count / <NUM_LIT> ) ) ) : <EOL> mults += map ( string . atof , string . split ( lines [ i + <NUM_LIT:2> + j ] ) ) <EOL> mults = N . array ( mults ) <EOL> dims . reverse ( ) <EOL> shp = [ - <NUM_LIT:1> ] + dims <EOL> except IOError : <EOL> print "<STR_LIT>" <EOL> lines = None <EOL> print shp <EOL> print '<STR_LIT>' , unpackstr <EOL> file = open ( imfile , "<STR_LIT:rb>" ) <EOL> bdata = file . read ( ) <EOL> bdata = N . fromstring ( bdata , unpackstr ) <EOL> littleEndian = ( struct . pack ( '<STR_LIT:i>' , <NUM_LIT:1> ) == struct . pack ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if ( littleEndian and os . uname ( ) [ <NUM_LIT:0> ] < > '<STR_LIT>' ) or ( max ( bdata ) > <NUM_LIT> ) : <EOL> bdata = bdata . byteswapped ( ) <EOL> try : <EOL> bdata . shape = shp <EOL> except : <EOL> print '<STR_LIT>' , shp , len ( bdata ) <EOL> raise ValueError , '<STR_LIT>' <EOL> if len ( bdata ) == <NUM_LIT:1> : <EOL> bdata = bdata [ <NUM_LIT:0> ] <EOL> if N . sum ( mults ) == <NUM_LIT:0> : <EOL> return bdata <EOL> try : <EOL> multshape = [ <NUM_LIT:1> ] * len ( bdata . shape ) <EOL> for i in range ( len ( bdata . shape ) ) : <EOL> if len ( mults ) == bdata . shape [ i ] : <EOL> multshape [ i ] = len ( mults ) <EOL> break <EOL> mults . shape = multshape <EOL> return bdata * mults <EOL> except : <EOL> return bdata <EOL> def mghbget ( imfile , numslices = - <NUM_LIT:1> , xsize = <NUM_LIT:64> , ysize = <NUM_LIT:64> , <EOL> unpackstr = N . int16 , bytesperpixel = <NUM_LIT> , sliceinit = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> file = open ( imfile , "<STR_LIT:rb>" ) <EOL> except : <EOL> print "<STR_LIT>" + imfile <EOL> raise IOError , "<STR_LIT>" <EOL> try : <EOL> header = imfile [ <NUM_LIT:0> : - <NUM_LIT:6> ] + '<STR_LIT>' <EOL> vals = get ( header , <NUM_LIT:0> ) <EOL> if type ( vals [ <NUM_LIT:0> ] ) == ListType : <EOL> xsize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> ysize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> numslices = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> else : <EOL> xsize = int ( vals [ <NUM_LIT:0> ] ) <EOL> ysize = int ( vals [ <NUM_LIT:1> ] ) <EOL> numslices = int ( vals [ <NUM_LIT:2> ] ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> suffix = imfile [ - <NUM_LIT:6> : ] <EOL> if suffix == '<STR_LIT>' : <EOL> pass <EOL> elif suffix [ - <NUM_LIT:3> : ] == '<STR_LIT>' : <EOL> pass <EOL> elif suffix == '<STR_LIT>' : <EOL> unpackstr = N . Float32 <EOL> bytesperpixel = <NUM_LIT> <EOL> sliceinit = <NUM_LIT:0.0> <EOL> else : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , unpackstr , '<STR_LIT>' , bytesperpixel <EOL> imsize = xsize * ysize <EOL> file = open ( imfile , "<STR_LIT:rb>" ) <EOL> bdata = file . read ( ) <EOL> numpixels = len ( bdata ) / bytesperpixel <EOL> if numpixels % <NUM_LIT:1> != <NUM_LIT:0> : <EOL> raise ValueError , "<STR_LIT>" <EOL> else : <EOL> bdata = N . fromstring ( bdata , unpackstr ) <EOL> littleEndian = ( struct . pack ( '<STR_LIT:i>' , <NUM_LIT:1> ) == struct . pack ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if ( littleEndian and os . uname ( ) [ <NUM_LIT:0> ] < > '<STR_LIT>' ) or ( max ( bdata ) > <NUM_LIT> ) : <EOL> bdata = bdata . byteswapped ( ) <EOL> if suffix [ - <NUM_LIT:3> : ] == '<STR_LIT>' : <EOL> if numslices == - <NUM_LIT:1> : <EOL> numslices = len ( bdata ) / <NUM_LIT> <EOL> xsize = <NUM_LIT:64> <EOL> ysize = <NUM_LIT> <EOL> slices = N . zeros ( ( numslices , xsize , ysize ) , N . Int ) <EOL> for i in range ( numslices ) : <EOL> istart = i * <NUM_LIT:8> + i * xsize * ysize <EOL> iend = i * <NUM_LIT:8> + ( i + <NUM_LIT:1> ) * xsize * ysize <EOL> print i , istart , iend <EOL> slices [ i ] = N . reshape ( N . array ( bdata [ istart : iend ] ) , ( xsize , ysize ) ) <EOL> else : <EOL> if numslices == <NUM_LIT:1> : <EOL> slices = N . reshape ( N . array ( bdata ) , [ xsize , ysize ] ) <EOL> else : <EOL> slices = N . reshape ( N . array ( bdata ) , [ numslices , xsize , ysize ] ) <EOL> if len ( slices ) == <NUM_LIT:1> : <EOL> slices = slices [ <NUM_LIT:0> ] <EOL> return slices <EOL> def braw ( fname , btype , shp = None ) : <EOL> """<STR_LIT>""" <EOL> file = open ( fname , '<STR_LIT:rb>' ) <EOL> bdata = file . read ( ) <EOL> bdata = N . fromstring ( bdata , btype ) <EOL> littleEndian = ( struct . pack ( '<STR_LIT:i>' , <NUM_LIT:1> ) == struct . pack ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if ( littleEndian and os . uname ( ) [ <NUM_LIT:0> ] < > '<STR_LIT>' ) or ( max ( bdata ) > <NUM_LIT> ) : <EOL> bdata = bdata . byteswapped ( ) <EOL> if shp : <EOL> try : <EOL> bdata . shape = shp <EOL> return bdata <EOL> except : <EOL> pass <EOL> return N . array ( bdata ) <EOL> def glget ( fname , btype ) : <EOL> """<STR_LIT>""" <EOL> d = braw ( fname , btype ) <EOL> d = d [ <NUM_LIT:8> : ] <EOL> f = open ( fname , '<STR_LIT:rb>' ) <EOL> shp = f . read ( <NUM_LIT:8> ) <EOL> f . close ( ) <EOL> shp = N . fromstring ( shp , N . Int ) <EOL> shp [ <NUM_LIT:0> ] , shp [ <NUM_LIT:1> ] = shp [ <NUM_LIT:1> ] , shp [ <NUM_LIT:0> ] <EOL> try : <EOL> carray = N . reshape ( d , shp ) <EOL> return <EOL> except : <EOL> pass <EOL> try : <EOL> r = d [ <NUM_LIT:0> : : <NUM_LIT:3> ] + <NUM_LIT:0> <EOL> g = d [ <NUM_LIT:1> : : <NUM_LIT:3> ] + <NUM_LIT:0> <EOL> b = d [ <NUM_LIT:2> : : <NUM_LIT:3> ] + <NUM_LIT:0> <EOL> r . shape = shp <EOL> g . shape = shp <EOL> b . shape = shp <EOL> carray = N . array ( [ r , g , b ] ) <EOL> except : <EOL> outstr = "<STR_LIT>" + str ( len ( d ) ) <EOL> raise ValueError , outstr <EOL> return carray <EOL> def mget ( fname , btype ) : <EOL> """<STR_LIT>""" <EOL> d = braw ( fname , btype ) <EOL> try : <EOL> header = fname [ <NUM_LIT:0> : - <NUM_LIT:6> ] + '<STR_LIT>' <EOL> vals = get ( header , <NUM_LIT:0> ) <EOL> if type ( vals [ <NUM_LIT:0> ] ) == ListType : <EOL> xsize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> ysize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> numslices = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> else : <EOL> xsize = int ( vals [ <NUM_LIT:0> ] ) <EOL> ysize = int ( vals [ <NUM_LIT:1> ] ) <EOL> numslices = int ( vals [ <NUM_LIT:2> ] ) <EOL> print xsize , ysize , numslices , d . shape <EOL> except : <EOL> print "<STR_LIT>" <EOL> if numslices == <NUM_LIT:1> : <EOL> d . shape = [ ysize , xsize ] <EOL> return N . transpose ( d ) * <NUM_LIT:1> <EOL> else : <EOL> d . shape = [ numslices , ysize , xsize ] <EOL> return N . transpose ( d ) * <NUM_LIT:1> <EOL> def mput ( outarray , fname , writeheader = <NUM_LIT:0> , btype = N . int16 ) : <EOL> """<STR_LIT>""" <EOL> outarray = N . transpose ( outarray ) <EOL> outdata = N . ravel ( outarray ) . astype ( btype ) <EOL> outdata = outdata . tostring ( ) <EOL> outfile = open ( fname , '<STR_LIT:wb>' ) <EOL> outfile . write ( outdata ) <EOL> outfile . close ( ) <EOL> if writeheader == <NUM_LIT:1> : <EOL> try : <EOL> suffixindex = string . rfind ( fname , '<STR_LIT:.>' ) <EOL> hdrname = fname [ <NUM_LIT:0> : suffixindex ] <EOL> except ValueError : <EOL> hdrname = fname <EOL> if len ( outarray . shape ) == <NUM_LIT:2> : <EOL> hdr = [ outarray . shape [ <NUM_LIT:1> ] , outarray . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> else : <EOL> hdr = [ outarray . shape [ <NUM_LIT:2> ] , outarray . shape [ <NUM_LIT:1> ] , outarray . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> , '<STR_LIT:\n>' ] <EOL> print hdrname + '<STR_LIT>' <EOL> outfile = open ( hdrname + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> outfile . write ( pstat . list2string ( hdr ) ) <EOL> outfile . close ( ) <EOL> return None <EOL> def bput ( outarray , fname , writeheader = <NUM_LIT:0> , packtype = N . int16 , writetype = '<STR_LIT:wb>' ) : <EOL> """<STR_LIT>""" <EOL> suffix = fname [ - <NUM_LIT:6> : ] <EOL> if suffix == '<STR_LIT>' : <EOL> packtype = N . int16 <EOL> elif suffix == '<STR_LIT>' : <EOL> packtype = N . Float32 <EOL> else : <EOL> print '<STR_LIT>' , packtype <EOL> outdata = N . ravel ( outarray ) . astype ( packtype ) <EOL> littleEndian = ( struct . pack ( '<STR_LIT:i>' , <NUM_LIT:1> ) == struct . pack ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if littleEndian and os . uname ( ) [ <NUM_LIT:0> ] < > '<STR_LIT>' : <EOL> outdata = outdata . byteswapped ( ) <EOL> outdata = outdata . tostring ( ) <EOL> outfile = open ( fname , writetype ) <EOL> outfile . write ( outdata ) <EOL> outfile . close ( ) <EOL> if writeheader == <NUM_LIT:1> : <EOL> try : <EOL> suffixindex = string . rfind ( fname , '<STR_LIT:.>' ) <EOL> hdrname = fname [ <NUM_LIT:0> : suffixindex ] <EOL> except ValueError : <EOL> hdrname = fname <EOL> if len ( outarray . shape ) == <NUM_LIT:2> : <EOL> hdr = [ outarray . shape [ <NUM_LIT:0> ] , outarray . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> else : <EOL> hdr = [ outarray . shape [ <NUM_LIT:1> ] , outarray . shape [ <NUM_LIT:2> ] , outarray . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> , '<STR_LIT:\n>' ] <EOL> print hdrname + '<STR_LIT>' <EOL> outfile = open ( hdrname + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> outfile . write ( pstat . list2string ( hdr ) ) <EOL> outfile . close ( ) <EOL> return None <EOL> def mrget ( fname , datatype = N . int16 ) : <EOL> """<STR_LIT>""" <EOL> d = braw ( fname , datatype ) <EOL> if len ( d ) > <NUM_LIT> * <NUM_LIT> : <EOL> return N . reshape ( d [ - <NUM_LIT> * <NUM_LIT> : ] , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> elif len ( d ) > <NUM_LIT> * <NUM_LIT> : <EOL> return N . reshape ( d [ - <NUM_LIT> * <NUM_LIT> : ] , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> elif len ( d ) > <NUM_LIT> * <NUM_LIT> : <EOL> return N . reshape ( d [ - <NUM_LIT> * <NUM_LIT> : ] , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> elif len ( d ) > <NUM_LIT:64> * <NUM_LIT:64> : <EOL> return N . reshape ( d [ - <NUM_LIT:64> * <NUM_LIT:64> : ] , ( <NUM_LIT:64> , <NUM_LIT:64> ) ) <EOL> else : <EOL> return N . reshape ( d [ - <NUM_LIT:32> * <NUM_LIT:32> : ] , ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> def quickload ( fname , linestocut = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> f = open ( fname , '<STR_LIT:r>' ) <EOL> d = f . readlines ( ) <EOL> f . close ( ) <EOL> print fname , '<STR_LIT>' <EOL> d = d [ linestocut : ] <EOL> d = map ( string . split , d ) <EOL> print '<STR_LIT>' <EOL> for i in range ( len ( d ) ) : <EOL> d [ i ] = map ( string . atoi , d [ i ] ) <EOL> print '<STR_LIT>' <EOL> return N . array ( d ) <EOL> def writedelimited ( listoflists , delimiter , file , writetype = '<STR_LIT:w>' ) : <EOL> """<STR_LIT>""" <EOL> if type ( listoflists [ <NUM_LIT:0> ] ) not in [ ListType , TupleType ] : <EOL> listoflists = [ listoflists ] <EOL> outfile = open ( file , writetype ) <EOL> rowstokill = [ ] <EOL> list2print = copy . deepcopy ( listoflists ) <EOL> for i in range ( len ( listoflists ) ) : <EOL> if listoflists [ i ] == [ '<STR_LIT:\n>' ] or listoflists [ i ] == '<STR_LIT:\n>' or listoflists [ i ] == '<STR_LIT>' : <EOL> rowstokill = rowstokill + [ i ] <EOL> rowstokill . reverse ( ) <EOL> for row in rowstokill : <EOL> del list2print [ row ] <EOL> maxsize = [ <NUM_LIT:0> ] * len ( list2print [ <NUM_LIT:0> ] ) <EOL> for row in listoflists : <EOL> if row == [ '<STR_LIT:\n>' ] or row == '<STR_LIT:\n>' : <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> elif row == [ '<STR_LIT>' ] or row == '<STR_LIT>' : <EOL> dashes = [ <NUM_LIT:0> ] * len ( maxsize ) <EOL> for j in range ( len ( maxsize ) ) : <EOL> dashes [ j ] = '<STR_LIT>' <EOL> outfile . write ( pstat . linedelimited ( dashes , delimiter ) ) <EOL> else : <EOL> outfile . write ( pstat . linedelimited ( row , delimiter ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> return None <EOL> def writecc ( listoflists , file , writetype = '<STR_LIT:w>' , extra = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> if type ( listoflists [ <NUM_LIT:0> ] ) not in [ ListType , TupleType ] : <EOL> listoflists = [ listoflists ] <EOL> outfile = open ( file , writetype ) <EOL> rowstokill = [ ] <EOL> list2print = copy . deepcopy ( listoflists ) <EOL> for i in range ( len ( listoflists ) ) : <EOL> if listoflists [ i ] == [ '<STR_LIT:\n>' ] or listoflists [ i ] == '<STR_LIT:\n>' or listoflists [ i ] == '<STR_LIT>' : <EOL> rowstokill = rowstokill + [ i ] <EOL> rowstokill . reverse ( ) <EOL> for row in rowstokill : <EOL> del list2print [ row ] <EOL> maxsize = [ <NUM_LIT:0> ] * len ( list2print [ <NUM_LIT:0> ] ) <EOL> for col in range ( len ( list2print [ <NUM_LIT:0> ] ) ) : <EOL> items = pstat . colex ( list2print , col ) <EOL> items = map ( pstat . makestr , items ) <EOL> maxsize [ col ] = max ( map ( len , items ) ) + extra <EOL> for row in listoflists : <EOL> if row == [ '<STR_LIT:\n>' ] or row == '<STR_LIT:\n>' : <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> elif row == [ '<STR_LIT>' ] or row == '<STR_LIT>' : <EOL> dashes = [ <NUM_LIT:0> ] * len ( maxsize ) <EOL> for j in range ( len ( maxsize ) ) : <EOL> dashes [ j ] = '<STR_LIT:->' * ( maxsize [ j ] - <NUM_LIT:2> ) <EOL> outfile . write ( pstat . lineincustcols ( dashes , maxsize ) ) <EOL> else : <EOL> outfile . write ( pstat . lineincustcols ( row , maxsize ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> return None <EOL> def writefc ( listoflists , colsize , file , writetype = '<STR_LIT:w>' ) : <EOL> """<STR_LIT>""" <EOL> if type ( listoflists ) == N . ArrayType : <EOL> listoflists = listoflists . tolist ( ) <EOL> if type ( listoflists [ <NUM_LIT:0> ] ) not in [ ListType , TupleType ] : <EOL> listoflists = [ listoflists ] <EOL> outfile = open ( file , writetype ) <EOL> rowstokill = [ ] <EOL> list2print = copy . deepcopy ( listoflists ) <EOL> for i in range ( len ( listoflists ) ) : <EOL> if listoflists [ i ] == [ '<STR_LIT:\n>' ] or listoflists [ i ] == '<STR_LIT:\n>' or listoflists [ i ] == '<STR_LIT>' : <EOL> rowstokill = rowstokill + [ i ] <EOL> rowstokill . reverse ( ) <EOL> for row in rowstokill : <EOL> del list2print [ row ] <EOL> n = [ <NUM_LIT:0> ] * len ( list2print [ <NUM_LIT:0> ] ) <EOL> for row in listoflists : <EOL> if row == [ '<STR_LIT:\n>' ] or row == '<STR_LIT:\n>' : <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> elif row == [ '<STR_LIT>' ] or row == '<STR_LIT>' : <EOL> dashes = [ <NUM_LIT:0> ] * colsize <EOL> for j in range ( len ( n ) ) : <EOL> dashes [ j ] = '<STR_LIT:->' * ( colsize ) <EOL> outfile . write ( pstat . lineincols ( dashes , colsize ) ) <EOL> else : <EOL> outfile . write ( pstat . lineincols ( row , colsize ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> outfile . close ( ) <EOL> return None <EOL> def load ( fname , lines_to_ignore = <NUM_LIT:4> , type = '<STR_LIT:i>' ) : <EOL> """<STR_LIT>""" <EOL> start = time . time ( ) <EOL> if type == '<STR_LIT:i>' : <EOL> intype = int <EOL> elif type in [ '<STR_LIT:f>' , '<STR_LIT:d>' ] : <EOL> intype = float <EOL> else : <EOL> raise ValueError , "<STR_LIT>" <EOL> tmpname = tempfile . mktemp ( ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> cmd = "<STR_LIT>" + fname + "<STR_LIT>" + tmpname <EOL> print cmd <EOL> os . system ( cmd ) <EOL> else : <EOL> cmd = "<STR_LIT>" + fname + "<STR_LIT>" + tmpname <EOL> print cmd <EOL> os . system ( cmd ) <EOL> wc = string . split ( os . popen ( "<STR_LIT>" + tmpname ) . read ( ) ) <EOL> numlines = int ( wc [ <NUM_LIT:0> ] ) - lines_to_ignore <EOL> tfp = open ( tmpname ) <EOL> if lines_to_ignore < > <NUM_LIT:0> : <EOL> for i in range ( lines_to_ignore ) : <EOL> junk = tfp . readline ( ) <EOL> numcols = len ( string . split ( tfp . readline ( ) ) ) <EOL> tfp . close ( ) <EOL> a = N . zeros ( ( numlines * numcols ) , type ) <EOL> block = <NUM_LIT> <EOL> data = mmapfile . mmapfile ( tmpname , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if lines_to_ignore < > <NUM_LIT:0> and sys . platform == '<STR_LIT:win32>' : <EOL> for i in range ( lines_to_ignore ) : <EOL> junk = data . readline ( ) <EOL> i = <NUM_LIT:0> <EOL> d = '<STR_LIT:U+0020>' <EOL> carryover = '<STR_LIT>' <EOL> while len ( d ) < > <NUM_LIT:0> : <EOL> d = carryover + data . read ( block ) <EOL> cutindex = string . rfind ( d , '<STR_LIT:\n>' ) <EOL> carryover = d [ cutindex + <NUM_LIT:1> : ] <EOL> d = d [ : cutindex + <NUM_LIT:1> ] <EOL> d = map ( intype , string . split ( d ) ) <EOL> a [ i : i + len ( d ) ] = d <EOL> i = i + len ( d ) <EOL> end = time . time ( ) <EOL> print "<STR_LIT>" % round ( end - start , <NUM_LIT:2> ) <EOL> data . close ( ) <EOL> os . remove ( tmpname ) <EOL> return N . reshape ( a , [ numlines , numcols ] ) <EOL> def find_dirs ( sourcedir ) : <EOL> """<STR_LIT>""" <EOL> files = os . listdir ( sourcedir ) <EOL> dirs = [ ] <EOL> for fname in files : <EOL> if os . path . isdir ( os . path . join ( sourcedir , fname ) ) : <EOL> dirs . append ( fname ) <EOL> return dirs <EOL> save = aput <EOL> def binget ( fname , btype = None ) : <EOL> """<STR_LIT>""" <EOL> file = open ( fname , '<STR_LIT:rb>' ) <EOL> bdata = file . read ( ) <EOL> file . close ( ) <EOL> if not btype : <EOL> btype = fname [ - <NUM_LIT:4> ] <EOL> try : <EOL> bdata = N . fromstring ( bdata , btype ) <EOL> except : <EOL> raise ValueError , "<STR_LIT>" <EOL> if not N . LittleEndian : <EOL> bdata = bdata . byteswapped ( ) <EOL> try : <EOL> header = fname [ : - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> vals = get ( header , <NUM_LIT:0> ) <EOL> print vals <EOL> if type ( vals [ <NUM_LIT:0> ] ) == ListType : <EOL> xsize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> ysize = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> numslices = int ( vals [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> else : <EOL> bdata . shape = vals <EOL> except : <EOL> print "<STR_LIT>" <EOL> return N . array ( bdata ) <EOL> def binput ( outarray , fname , packtype = None , writetype = '<STR_LIT:wb>' ) : <EOL> """<STR_LIT>""" <EOL> if not packtype : <EOL> packtype = fname [ - <NUM_LIT:4> ] <EOL> if packtype == N . int16 and outarray . typecode ( ) == '<STR_LIT:f>' : <EOL> if max ( N . ravel ( outarray ) ) > <NUM_LIT> or min ( N . ravel ( outarray ) ) < - <NUM_LIT> : <EOL> print "<STR_LIT>" <EOL> outdata = N . ravel ( outarray ) . astype ( packtype ) <EOL> if not N . LittleEndian : <EOL> outdata = outdata . byteswapped ( ) <EOL> outdata = outdata . tostring ( ) <EOL> outfile = open ( fname , writetype ) <EOL> outfile . write ( outdata ) <EOL> outfile . close ( ) <EOL> try : <EOL> suffixindex = string . rfind ( fname , '<STR_LIT:.>' ) <EOL> hdrname = fname [ <NUM_LIT:0> : suffixindex + <NUM_LIT:2> ] + '<STR_LIT>' <EOL> except ValueError : <EOL> hdrname = fname <EOL> hdr = outarray . shape <EOL> print hdrname <EOL> outfile = open ( hdrname , '<STR_LIT:w>' ) <EOL> outfile . write ( pstat . list2string ( hdr ) ) <EOL> outfile . close ( ) <EOL> return None <EOL> def array2afni ( d , brikprefix , voltype = None , TR = <NUM_LIT> , sliceorder = '<STR_LIT>' , geomparent = None , view = None ) : <EOL> """<STR_LIT>""" <EOL> typecodemapping = { '<STR_LIT:c>' : '<STR_LIT:b>' , <EOL> '<STR_LIT:b>' : '<STR_LIT:b>' , <EOL> '<STR_LIT:f>' : '<STR_LIT:f>' , <EOL> '<STR_LIT:d>' : '<STR_LIT:f>' , <EOL> '<STR_LIT:1>' : '<STR_LIT:b>' , <EOL> '<STR_LIT:s>' : '<STR_LIT>' , <EOL> '<STR_LIT:i>' : '<STR_LIT:i>' , <EOL> '<STR_LIT:l>' : '<STR_LIT:i>' } <EOL> if len ( d . shape ) not in [ <NUM_LIT:3> , <NUM_LIT:4> ] : <EOL> raise ValueError , "<STR_LIT>" % d . shape <EOL> if d . typecode ( ) == N . Float64 : <EOL> outcode = '<STR_LIT:f>' <EOL> else : <EOL> outcode = d . typecode ( ) <EOL> tmpoutname = '<STR_LIT>' % outcode <EOL> binput ( d . astype ( outcode ) , tmpoutname ) <EOL> if not voltype : <EOL> if len ( d . shape ) == <NUM_LIT:3> : <EOL> if d . typecode ( ) in [ '<STR_LIT:s>' , '<STR_LIT:i>' , '<STR_LIT:l>' ] : <EOL> voltype = '<STR_LIT>' <EOL> else : <EOL> voltype = '<STR_LIT>' <EOL> else : <EOL> voltype = '<STR_LIT>' <EOL> if len ( d . shape ) == <NUM_LIT:3> : <EOL> timepts = <NUM_LIT:1> <EOL> slices = d . shape [ <NUM_LIT:0> ] <EOL> timestr = '<STR_LIT>' <EOL> elif len ( d . shape ) == <NUM_LIT:4> : <EOL> timepts = d . shape [ <NUM_LIT:0> ] <EOL> slices = d . shape [ <NUM_LIT:1> ] <EOL> timestr = '<STR_LIT>' % ( slices , timepts , TR , sliceorder ) <EOL> cmd = '<STR_LIT>' % ( voltype , brikprefix ) <EOL> if view : <EOL> cmd += '<STR_LIT>' % view <EOL> if geomparent : <EOL> cmd += '<STR_LIT>' % geomparent <EOL> cmd += timestr <EOL> cmd += '<STR_LIT>' % ( typecodemapping [ d . typecode ( ) ] , d . shape [ - <NUM_LIT:1> ] , d . shape [ - <NUM_LIT:2> ] , slices * timepts , tmpoutname ) <EOL> print cmd <EOL> os . system ( cmd ) <EOL> os . remove ( tmpoutname ) <EOL> os . remove ( tmpoutname [ : - <NUM_LIT:3> ] + '<STR_LIT>' ) </s>
