def cacheMissXY ( self, resolution, xtile, ytile, zslice ): """On a miss. Cutout, return the image and load the cache in a background thread""" # figure out the cutout (limit to max image size) xstart = xtile*self.tilesz ystart = ytile*self.tilesz xend = min ((xtile+1)*self.tilesz,self.proj.datasetcfg.imagesz[resolution][0]) yend = min ((ytile+1)*self.tilesz,self.proj.datasetcfg.imagesz[resolution][1]) # call the mcfc interface imageargs = '{}/{},{}/{},{}/{},{}/'.format(resolution,xstart,xend,ystart,yend,zslice,zslice+1) tiledata = None for i in range(len(self.channels)): cutout = ocpcarest.cutout ( imageargs, self.proj, self.db, self.channels[i] ) # initialize the tiledata by type if tiledata == None: tiledata = np.zeros((len(self.channels), cutout.data.shape[0],self.tilesz,self.tilesz), dtype=cutout.data.dtype) tiledata[i,0,0:((yend-1)%self.tilesz+1),0:((xend-1)%self.tilesz+1)] = cutout.data[0,:,:] # reduction factor. How to scale data. 16 bit->8bit, or windowed (startwindow,endwindow) = self.proj.datasetcfg.windowrange if self.proj.getDBType() == ocpcaproj.CHANNELS_16bit and ( startwindow == endwindow == 0): tiledata = np.uint8(tiledata * 1.0/256) elif self.proj.getDBType() == ocpcaproj.CHANNELS_16bit and ( endwindow!=0 ): from windowcutout import windowCutout windowCutout ( tiledata, (startwindow, endwindow) ) # We have an compound array. Now color it. colors = ('C','M','Y','R','G','B') return mcfc.mcfcPNG ( tiledata.reshape((tiledata.shape[0],tiledata.shape[2],tiledata.shape[3])), colors )
def addXZCuboid ( self, cuboid, res, xtile, ztile, ymin, ydim ): """ Add the cutout to the cache """ # will create the dataset if it doesn't exist self.checkDirHier(res) # get the dataset id for this token (dsidstr,ximagesz,yimagesz,zoffset,zmaxslice,zscale) = self.db.getDataset ( self.datasetname ) dsid = int(dsidstr) # counter of how many new tiles we get newtiles = 0 # number of tiles if self.channels == None: numtiles = cuboid.shape[1] else: numtiles = cuboid.shape[2] # get the dataset window range startwindow, endwindow = self.info['dataset']['windowrange'] # windodcutout function if window is non-zero if endwindow !=0: windowCutout ( cuboid, (startwindow,endwindow) ) # need to make channels take shape arguments # add each image slice to memcache for y in range(numtiles): self.checkSliceDir(res,y+ymin) tilefname = '{}/{}/r{}/sl{}/z{}x{}.png'.format(settings.CACHE_DIR,self.datasetname,res,y+ymin,ztile,xtile) if self.channels == None: img = self.tile2WebPNG ( cuboid.shape[2], cuboid.shape[0], cuboid[:,y,:] ) else: # looks good to here img = self.channels2WebPNG ( cuboid.shape[3], cuboid.shape[1], cuboid[:,:,y,:] ) # convert into a catmaid perspective tile. img = img.resize ( [settings.TILESIZE,settings.TILESIZE] ) fobj = open ( tilefname, "w" ) img.save ( fobj, "PNG" ) try: self.db.insert ( tilekey.tileKey(dsid,res,xtile,y+ymin,ztile), tilefname ) newtiles += 1 except MySQLdb.Error, e: # ignore duplicate entries if e.args[0] != 1062: raise
def test_window_default(self): "Test the window functionality" p.args = (3000,3100,4000,4100,200,201) image_data = np.ones([2,1,100,100], dtype=np.uint16) * 2000 response = postNPZ(p, image_data) url = "http://{}/ca/{}/{}/xy/{}/{},{}/{},{}/{}/".format(SITE_HOST, p.token, p.channels[0], p.resolution, p.args[0], p.args[1], p.args[2], p.args[3], p.args[4]) f = getURL (url) from windowcutout import windowCutout windowCutout(image_data, p.window) slice_data = np.asarray ( Image.open(StringIO(f.read())) ) assert ( np.array_equal(slice_data,image_data[0][0]) )
def test_window_default(self): "Test the window functionality" p.args = (3000,3100,4000,4100,200,201,50,52) image_data = np.ones([2,2,1,100,100], dtype=np.uint16) * 2000 response = postNPZ(p, image_data, time=True) url = "http://{}/ca/{}/{}/xy/{}/{},{}/{},{}/{}/{}".format(SITE_HOST, p.token, p.channels[0], p.resolution, p.args[0], p.args[1], p.args[2], p.args[3], p.args[4], p.args[6]) f = getURL (url) from windowcutout import windowCutout windowCutout(image_data, p.window) slice_data = np.asarray ( Image.open(StringIO(f.read())) ) assert ( np.array_equal(slice_data,image_data[0][0][0]) )
def window(data, ch, window_range=None): """Performs a window transformation on the cutout area""" if window_range is None: window_range = ch.getWindowRange() [startwindow, endwindow] = window_range if ch.getChannelType() in IMAGE_CHANNELS and ch.getChannelDataType() in DTYPE_uint16: if (startwindow == endwindow == 0): return np.uint8(data * 1.0/256) elif endwindow!=0: windowCutout (data, window_range) return np.uint8(data) return data
def cacheMissXZ ( self, resolution, xtile, yslice, ztile ): """On a miss. Cutout, return the image and load the cache in a background thread""" # figure out the cutout (limit to max image size) xstart = xtile*self.tilesz xend = min ((xtile+1)*self.tilesz,self.proj.datasetcfg.imagesz[resolution][0]) # z cutouts need to get rescaled # we'll map to the closest pixel range and tolerate one pixel error at the boundary scalefactor = self.proj.datasetcfg.zscale[resolution] zoffset = self.proj.datasetcfg.slicerange[0] ztilestart = int((ztile*self.tilesz)/scalefactor) + zoffset zstart = max ( ztilestart, zoffset ) ztileend = int(math.ceil(((ztile+1)*self.tilesz)/scalefactor)) + zoffset zend = min ( ztileend, self.proj.datasetcfg.slicerange[1] ) # call the mcfc interface imageargs = '{}/{},{}/{},{}/{},{}/'.format(resolution,xstart,xend,yslice,yslice+1,zstart,zend) tiledata = None for i in range(len(self.channels)): cutout = ocpcarest.cutout ( imageargs, self.proj, self.db, self.channels[i] ) # initialize the tiledata by type if tiledata == None: tiledata = np.zeros((len(self.channels), zend-zstart, cutout.data.shape[1],self.tilesz), dtype=cutout.data.dtype) tiledata[i,0:zend-zstart,0,0:((xend-1)%self.tilesz+1)] = cutout.data[:,0,:] # reduction factor. How to scale data. 16 bit->8bit, or windowed (startwindow,endwindow) = self.proj.datasetcfg.windowrange if self.proj.getDBType() == ocpcaproj.CHANNELS_16bit and ( startwindow == endwindow == 0): tiledata = np.uint8(tiledata * 1.0/256) elif self.proj.getDBType() == ocpcaproj.CHANNELS_16bit and ( endwindow!=0 ): from windowcutout import windowCutout windowCutout ( tiledata, (startwindow, endwindow) ) # We have an compound array. Now color it. colors = ('C','M','Y','R','G','B') img = mcfc.mcfcPNG ( tiledata.reshape((tiledata.shape[0],tiledata.shape[1],tiledata.shape[3])), colors ) return img.resize ((self.tilesz,self.tilesz))