<s> import argparse <EOL> import sys <EOL> import os <EOL> import numpy as np <EOL> from PIL import Image <EOL> import cStringIO <EOL> import zlib <EOL> sys . path += [ os . path . abspath ( '<STR_LIT>' ) ] <EOL> import OCP . settings <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> from django . conf import settings <EOL> import OCP . ocppaths <EOL> import ocpcarest <EOL> import zindex <EOL> import gc <EOL> """<STR_LIT>""" <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:path>' , action = "<STR_LIT:store>" , help = '<STR_LIT>' ) <EOL> result = parser . parse_args ( ) <EOL> resolution = <NUM_LIT:0> <EOL> [ db , proj , projdb ] = ocpcarest . loadDBProj ( result . token ) <EOL> ( xcubedim , ycubedim , zcubedim ) = proj . datasetcfg . cubedim [ resolution ] <EOL> ( startslice , endslice ) = proj . datasetcfg . slicerange <EOL> batchsz = zcubedim <EOL> ximagesz = <NUM_LIT> <EOL> yimagesz = <NUM_LIT> <EOL> for sl in range ( startslice , endslice + <NUM_LIT:1> , batchsz ) : <EOL> slab = np . zeros ( [ batchsz , yimagesz , ximagesz ] , dtype = np . uint8 ) <EOL> for b in range ( batchsz ) : <EOL> if ( sl + b <= endslice ) : <EOL> filenm = '<STR_LIT>' . format ( result . path , sl + b ) <EOL> print "<STR_LIT>" + filenm <EOL> img = Image . open ( filenm , '<STR_LIT:r>' ) <EOL> imgdata = np . asarray ( img ) <EOL> slab [ b , : , : ] = imgdata <EOL> endz = b <EOL> for y in range ( <NUM_LIT:0> , yimagesz , ycubedim ) : <EOL> for x in range ( <NUM_LIT:0> , ximagesz , xcubedim ) : <EOL> mortonidx = zindex . XYZMorton ( [ x / xcubedim , y / ycubedim , ( sl - startslice ) / zcubedim ] ) <EOL> cubedata = np . zeros ( [ zcubedim , ycubedim , xcubedim ] , dtype = np . uint8 ) <EOL> xmin = x <EOL> ymin = y <EOL> xmax = min ( ximagesz , x + xcubedim ) <EOL> ymax = min ( yimagesz , y + ycubedim ) <EOL> zmin = <NUM_LIT:0> <EOL> zmax = min ( sl + zcubedim , endslice + <NUM_LIT:1> ) <EOL> cubedata [ <NUM_LIT:0> : zmax - zmin , <NUM_LIT:0> : ymax - ymin , <NUM_LIT:0> : xmax - xmin ] = slab [ zmin : zmax , ymin : ymax , xmin : xmax ] <EOL> if len ( np . unique ( cubedata ) ) == <NUM_LIT:1> : <EOL> continue <EOL> fileobj = cStringIO . StringIO ( ) <EOL> np . save ( fileobj , cubedata ) <EOL> cdz = zlib . compress ( fileobj . getvalue ( ) ) <EOL> cursor = db . conn . cursor ( ) <EOL> sql = "<STR_LIT>" . format ( int ( resolution ) ) <EOL> cursor . execute ( sql , ( mortonidx , cdz ) ) <EOL> cursor . close ( ) <EOL> print "<STR_LIT>" % ( x , y , sl ) <EOL> db . conn . commit ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
