<s> import argparse <EOL> import sys <EOL> import os <EOL> import argparse <EOL> import cStringIO <EOL> import numpy as np <EOL> from PIL import Image <EOL> import zlib <EOL> from contextlib import closing <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 imagecube <EOL> import ocplib <EOL> import ocpcarest <EOL> import ocpcaproj <EOL> import ocpcadb <EOL> END_WINDOWS = [ ] <EOL> class CatmaidIngest : <EOL> def __init__ ( self , token , resolution , path ) : <EOL> """<STR_LIT>""" <EOL> self . token = token <EOL> self . path = path <EOL> self . resolution = resolution <EOL> def ingest ( self ) : <EOL> """<STR_LIT>""" <EOL> with closing ( ocpcaproj . OCPCAProjectsDB ( ) ) as projdb : <EOL> proj = projdb . loadProject ( self . token ) <EOL> with closing ( ocpcadb . OCPCADB ( proj ) ) as db : <EOL> ( startslice , endslice ) = proj . datasetcfg . slicerange <EOL> ( xcubedim , ycubedim , zcubedim ) = cubedims = proj . datasetcfg . cubedim [ self . resolution ] <EOL> ( ximagesz , yimagesz ) = proj . datasetcfg . imagesz [ self . resolution ] <EOL> batchsz = zcubedim <EOL> for sl in range ( startslice , endslice , batchsz ) : <EOL> slab = np . zeros ( [ zcubedim , yimagesz , ximagesz ] , dtype = np . uint8 ) <EOL> for b in range ( batchsz ) : <EOL> if ( sl + b <= endslice ) : <EOL> filename = '<STR_LIT>' . format ( self . path , sl + b , ( sl + b - <NUM_LIT:1> ) * <NUM_LIT> ) <EOL> print filename <EOL> try : <EOL> img = Image . open ( filename , '<STR_LIT:r>' ) <EOL> slab [ b , : , : ] = np . asarray ( img ) <EOL> except IOError , e : <EOL> print "<STR_LIT>" % ( e ) <EOL> img = np . zeros ( ( yimagesz , ximagesz ) , dtype = np . uint8 ) <EOL> slab [ b , : , : ] = img <EOL> for y in range ( <NUM_LIT:0> , yimagesz , ycubedim ) : <EOL> for x in range ( <NUM_LIT:0> , ximagesz , xcubedim ) : <EOL> zidx = ocplib . 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 - <NUM_LIT:1> , x + xcubedim - <NUM_LIT:1> ) ) + <NUM_LIT:1> <EOL> ymax = ( min ( yimagesz - <NUM_LIT:1> , y + ycubedim - <NUM_LIT:1> ) ) + <NUM_LIT:1> <EOL> zmin = <NUM_LIT:0> <EOL> zmax = min ( sl + zcubedim , endslice ) <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> cube = imagecube . ImageCube16 ( cubedims ) <EOL> cube . zeros ( ) <EOL> cube . data = cubedata <EOL> if np . count_nonzero ( cube . data ) != <NUM_LIT:0> : <EOL> print zidx , ocplib . MortonXYZ ( zidx ) <EOL> db . putCube ( zidx , self . resolution , cube ) <EOL> print "<STR_LIT>" % ( x , y , sl ) <EOL> db . conn . commit ( ) <EOL> slab = None <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" , type = int ) <EOL> parser . add_argument ( '<STR_LIT:path>' , action = "<STR_LIT:store>" ) <EOL> result = parser . parse_args ( ) <EOL> ci = CatmaidIngest ( result . token , result . resolution , result . path ) <EOL> ci . ingest ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
