Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def reserve ( webargs ):
"""Reserve annotation ids"""
[token, channel, reservestr, cnt, other] = webargs.split ('/', 4)
with closing (NDProjectsDB()) as projdb:
proj = projdb.loadToken ( token )
with closing (RamonDB(proj)) as rdb:
ch = NDChannel.fromName(proj,channel)
if ch.channel_type not in ANNOTATION_CHANNELS:
logger.error("Illegal project type for reserve.")
raise NDWSError("Illegal project type for reserve.")
try:
count = int(cnt)
# perform the reservation
firstid = rdb.reserve (ch, count)
return json.dumps ( (firstid, int(cnt)) )
except:
logger.error("Illegal arguments to reserve: {}".format(webargs))
raise NDWSError("Illegal arguments to reserve: {}".format(webargs))
# reading the raw data
file_name = "{}{}".format(self.path, self.generateFileName(slice_number+b))
# print "Open filename {}".format(file_name)
logger.info("Open filename {}".format(file_name))
if ch.channel_datatype in [UINT8, UINT16] and ch.channel_type in IMAGE_CHANNELS:
try:
image_data = np.asarray(Image.open(file_name, 'r'))
slab[b,:,:] = image_data
except Exception as e:
slab[b,:,:] = np.zeros((yimagesz, ximagesz), dtype=ND_dtypetonp.get(ch.channel_datatype))
logger.warning("File corrupted. Cannot open file. {}".format(e))
elif ch.channel_datatype in [UINT32] and ch.channel_type in IMAGE_CHANNELS:
image_data = np.asarray(Image.open(file_name, 'r').convert('RGBA'))
slab[b,:,:] = np.left_shift(image_data[:,:,3], 24, dtype=np.uint32) | np.left_shift(image_data[:,:,2], 16, dtype=np.uint32) | np.left_shift(image_data[:,:,1], 8, dtype=np.uint32) | np.uint32(image_data[:,:,0])
elif ch.channel_type in ANNOTATION_CHANNELS:
image_data = np.asarray(Image.open(file_name, 'r'))
slab[b,:,:] = image_data
else:
logger.error("Cannot ingest this data yet")
raise NDWSError("Cannot ingest this data yet")
except IOError, e:
logger.warning("IOError {}.".format(e))
slab[b,:,:] = np.zeros((yimagesz, ximagesz), dtype=ND_dtypetonp.get(ch.channel_datatype))
for y in range ( 0, yimagesz+1, ysupercubedim ):
for x in range ( 0, ximagesz+1, xsupercubedim ):
# Getting a Cube id and ingesting the data one cube at a time
zidx = XYZMorton ( [x/xsupercubedim, y/ysupercubedim, (slice_number-zoffset)/zsupercubedim] )
cube = Cube.CubeFactory(supercubedim, ch.channel_type, ch.channel_datatype)
cube.zeros()
# get the data region for each channel
for chan in channels:
# data type on a per channel basis
ch = proj.getChannelObj(chan)
try:
cb = db.cutout ( ch, corner, dim, resolution )
# apply window for 16 bit projects
if ch.getDataType() in DTYPE_uint16:
[startwindow, endwindow] = window_range = ch.window_range
if (endwindow != 0):
cb.data = np.uint8(windowCutout(cb.data, window_range))
outputdict[chan] = []
for zslice in cb.data:
if ch.getChannelType() in ANNOTATION_CHANNELS:
# parse annotation project
imagemap = np.zeros( [ dim[1], dim[0] ], dtype=np.uint32 )
imagemap = recolor_ctype( zslice, imagemap )
img = Image.frombuffer( 'RGBA', (dim[0],dim[1]), imagemap, 'raw', 'RGBA', 0, 1 )
else:
# parse image project
img = Image.frombuffer( 'L', (dim[0], dim[1]), zslice.flatten(), 'raw', 'L', 0, 1 )
# convert to base64
fileobj = cStringIO.StringIO()
img.save(fileobj, "PNG")
fileobj.seek(0)
encodedimg = base64.b64encode(fileobj.read())
outputdict[chan].append(encodedimg)