def downloadImage(queryService, rawPixelStore, imageId, imageName): """ This method downloads the first 3D data of the OMERO image and saves it as a local image. @param session The OMERO session @param imageId The ID of the image to download @param imageName The name of the image to write. If no path, saved in the current directory. """ # set up pixel-store and get the pixels object query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() sizeC = pixels.getSizeC().getValue() sizeT = pixels.getSizeT().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) em = EMData(sizeX,sizeY,sizeZ) pixelsType = pixels.pixelsType theC, theT = 0, 0 for theZ in range(sizeZ): plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) try: # method seems to depend which version of EMAN2 you have EMNumPy.numpy2em(plane2D, e) except: e = EMNumPy.numpy2em(plane2D) em.insert_clip(e,(0,0,theZ)) em.write_image(imageName)
def downloadImages(services, imageIds, local2dStack): """ This method downloads the first (only?) plane of the OMERO image and saves it as a local image. @param session The OMERO session @param imageId The ID of the image to download @param imageName The name of the image to write. If no path, saved in the current directory. """ # get services queryService = services["queryService"] rawPixelsStore = services["rawPixelsStore"] d = EMData() for imageId in imageIds: # get pixels with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) # get the plane theZ, theC, theT = (0,0,0) pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelsStore.setPixelsId(pixelsId, bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelsStore, pixels, theZ, theC, theT) # convert to EMData and add to stack. See e2proc2d.py approx line 500. try: # method seems to depend which version of EMAN2 you have EMNumPy.numpy2em(plane2D, d) except: d = EMNumPy.numpy2em(plane2D) d.write_image(local2dStack, -1, EMUtil.get_image_ext_type("hdf"), False, None, EMUtil.EMDataType.EM_FLOAT, True)
def downloadImage(session, imageId, imageName): """ This method downloads the first (only?) plane of the OMERO image and saves it as a local image. @param session The OMERO session @param imageId The ID of the image to download @param imageName The name of the image to write. If no path, saved in the current directory. """ queryService = session.getQueryService() rawPixelStore = session.createRawPixelsStore() # get pixels with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) theX = pixels.getSizeX().getValue() theY = pixels.getSizeY().getValue() # get the plane theZ, theC, theT = (0,0,0) pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) p = Image.fromarray(plane2D) #p.show() p.save(imageName) return (theX, theY)
def downloadImage(queryService, rawPixelsStore, imageId, imageName): """ This method downloads the first (only?) plane of the OMERO image and saves it as a local image. @param session The OMERO session @param imageId The ID of the image to download @param imageName The name of the image to write. If no path, saved in the current directory. """ print "Downloading image ID: %s to local file: %s using EMAN2" % (imageId, imageName) # get pixels with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() em = EMData(sizeX,sizeY,sizeZ) # get the planes pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelsStore.setPixelsId(pixelsId, bypassOriginalFile) theC, theT = (0,0) for theZ in range(sizeZ): plane2D = scriptUtil.downloadPlane(rawPixelsStore, pixels, theZ, theC, theT) e = EMNumPy.numpy2em(plane2D) em.insert_clip(e,(0,0,theZ)) em.write_image(imageName)
def analyseEllipses(ellipses, pixels, rawPixelStore, theC, theT, theZ): """ Returns a list of average pixel intensities corresponding to the ellipses, which must all exist on a single plane, defined by pixels, and C, T, Z indexes. @param ellipses: A list of the ellipses to analyse, each defined as (cx, cy, rx, ry, z) @param pixels: The pixels object, with PixelsType object loaded, so that getPixelsType() can be called. @param rawPixelStore: The Omero rawPixelsStore @param theC: C index for plane to analyse @param theT: T index for plane to analyse @param theZ: Z index for plane to analyse """ plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) results = [] for ellipse in ellipses: points = getEllipsePixels(ellipse) totalValue = 0 for p in points: x, y = p value = plane2D[y][x] totalValue += value average = totalValue/len(points) results.append(average) return results
def getImagePlane(imageId): query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0,0,0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) return plane2D
def getImagePlane(imageId): query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0, 0, 0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) return plane2D
def eman(request, imageId, conn=None, **kwargs): if not EMAN2_IMPORTED: return HttpResponse("EMAN2 not found") rawPixelStore = conn.createRawPixelsStore() queryService = conn.getQueryService() query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0, 0, 0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = 1 #em = EMData() em = EMNumPy.numpy2em(plane2D) f = kwargs['filter'] if f == "fft": filterName = "basis.fft" filterParamMap = { "dir": 1, } em.process_inplace(filterName, filterParamMap) elif f == "median": if 'radius' in kwargs: filterParamMap = {"radius": int(kwargs['radius'])} em.process_inplace("eman1.filter.median", filterParamMap) else: em.process_inplace("eman1.filter.median") elif f == "log": em.process_inplace("math.log") tempdir = settings.FILE_UPLOAD_TEMP_DIR tempJpg = os.path.join(tempdir, ('%s.emanFilter.jpg' % (conn._sessionUuid))).replace('\\', '/') em.write_image(tempJpg) originalFile_data = FileWrapper(file(tempJpg)) rsp = HttpResponse(originalFile_data) rsp['Content-Type'] = "image/jpg" return rsp
def eman(request, imageId, **kwargs): if not EMAN2_IMPORTED: return HttpResponse("EMAN2 not found") conn = getConnection(request) rawPixelStore = conn.createRawPixelsStore() queryService = conn.getQueryService() query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0, 0, 0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = 1 # em = EMData() em = EMNumPy.numpy2em(plane2D) f = kwargs["filter"] if f == "fft": filterName = "basis.fft" filterParamMap = {"dir": 1} em.process_inplace(filterName, filterParamMap) elif f == "median": if "radius" in kwargs: filterParamMap = {"radius": int(kwargs["radius"])} em.process_inplace("eman1.filter.median", filterParamMap) else: em.process_inplace("eman1.filter.median") elif f == "log": em.process_inplace("math.log") tempdir = settings.FILE_UPLOAD_TEMP_DIR tempJpg = os.path.join(tempdir, ("%s.emanFilter.jpg" % (conn._sessionUuid))).replace("\\", "/") em.write_image(tempJpg) originalFile_data = FileWrapper(file(tempJpg)) rsp = HttpResponse(originalFile_data) rsp["Content-Type"] = "image/jpg" return rsp
def getPlane( conn, iid, pixels=0, channel=0, zslice=0, timepoint=0 ): ''' Returns a plane with the given image id (iid) as well as pixels, channels, zslice and timepoint index. @param connection (conn) @param image id (iid) @param pixels index @param channel index @param zslice index @param timepoint index @return plane ''' if not conn.isConnected(): return None if not hasImage( conn, iid ): return None #create pixel service (needed to extract pixels from image object) rawPixelsStore = conn.createRawPixelsStore() #get image image = conn.getObject( "Image", long(iid) ) #get plane id from image (we use zero because our images have only #one pixel object per image object pid = image.getPixelsId(); #retrieve pixel object pixels = conn.getPixelsService().retrievePixDescription(pid); #update pixel service to match the current pixel object rawPixelsStore.setPixelsId( pid, True ) try: #extract pixel object plane = utils.downloadPlane( rawPixelsStore, pixels, zslice, channel, timepoint ); except: plane = None #close services rawPixelsStore.close() #return plane return plane
def getPlane(rawPixelStore, pixels, theZ, theC, theT): """ This method downloads the specified plane of the OMERO image and returns it as a numpy array. @param session The OMERO session @param imageId The ID of the image to download @param pixels The pixels object, with pixelsType @param imageName The name of the image to write. If no path, saved in the current directory. """ # get the plane pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) return plane2D
def getPlane(rawPixelStore, pixels, theZ, theC, theT): """ This method downloads the specified plane of the OMERO image and returns it as a numpy array. @param session The OMERO session @param imageId The ID of the image to download @param pixels The pixels object, with pixelsType @param imageName The name of the image to write. If no path, saved in the current directory. """ sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() # get the plane pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) return plane2D
def omeroToEm(commandArgs): # log-in client = omero.client(commandArgs["host"]) session = client.createSession(commandArgs["username"], commandArgs["password"]) # get the services we need queryService = session.getQueryService() updateService = session.getUpdateService() rawFileStore = session.createRawFileStore() rawPixelStore = session.createRawPixelsStore() containerService = session.getContainerService() images = [] if "image" in commandArgs: iId = long(commandArgs["image"]) i = containerService.getImages("Image", [iId], None)[0] images.append(i) elif "dataset" in commandArgs: dIds = [long(commandArgs["dataset"])] images = containerService.getImages("Dataset", dIds, None) else: print "No image or dataset ID given" return path = None if "path" in commandArgs: path = commandArgs["path"] if not os.path.exists(path): print "Given path: %s not found. Saving images in current directory." % path path = None extension = "dat" # default format = None if "extension" in commandArgs: ext = commandArgs["extension"] if ext in filetypes: extension = ext print "Saving all images as .%s files." % extension else: print "Invalid extension: %s (not supported by Spider). Using %s" % (ext, extension) for image in images: iName = image.getName().getValue() imageName = os.path.basename(iName) # make sure no dir separators in name. imageId = image.getId().getValue() if not imageName.endswith(".%s" % extension): imageName = "%s.%s" % (imageName, extension) if path: imageName = os.path.join(path,imageName) i = 1 # don't overwrite. Add number before extension dirName, ext = imageName.rsplit(".", 1) while os.path.exists(imageName): imageName = "%s_%s.%s" % (dirName,i,ext) i +=1 print "Preparing to save image: %s" % imageName figLegend = "" # get pixels, with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) ptype = pixels.pixelsType.getValue().getValue() sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() # prepare rawPixelStore theC, theT = (0, 0) pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) if sizeZ == 1: plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, 0, theC, theT) array2spider(plane2D, imageName) else: numpyType = pixelstypetopython.toNumpy(ptype) array3D = zeros( (sizeZ, sizeY, sizeX), dtype=numpyType ) for z in range(sizeZ): # get each plane and add to 3D array plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, z, theC, theT) array3D[z] = plane2D array2spider(array3D, imageName)
def saveImageAs(session, parameterMap): # get the services we need queryService = session.getQueryService() updateService = session.getUpdateService() rawFileStore = session.createRawFileStore() rawPixelStore = session.createRawPixelsStore() imageIds = [] if "Image_IDs" in parameterMap: for idCount, imageId in enumerate(parameterMap["Image_IDs"]): iId = long(imageId.getValue()) imageIds.append(iId) else: print "No images" return cIndexes = None theT = 0 if "Channel_Index" in parameterMap: cIndexes = [parameterMap["Channel_Index"]] extension = None format = None if "Extension" in parameterMap: extension = parameterMap["Extension"] if extension in filetypes: format = filetypes[extension] print "Saving all images as .%s files. Format: %s" % (extension, filetypes[extension]) else: print "Invalid extension: %s (not supported by EMAN2). Will attempt to get extensions from image names." % extension extension = None else: print "No extension specified. Will attempt get extensions from image names." originalFiles = [] for imageId in imageIds: image = queryService.get("Image", imageId) n = image.getName().getValue() imageName = os.path.basename(n) # make sure we don't have path as name. if imageName == "": imageName = os.path.basename(n[:-1]) if (extension == None) or (extension not in filetypes): # try to get extension from image name lastDotIndex = imageName.rfind(".") # .rpartition(sep) if lastDotIndex >= 0: extension = imageName[lastDotIndex+1:] if extension in filetypes: format = filetypes[extension] if (extension == None) or (extension not in filetypes): print "File extension from image invalid. Could not export image ID: %d Name: %s Extension: %s" % (imageId, imageName, extension) continue if not imageName.endswith(".%s" % extension): imageName = "%s.%s" % (imageName, extension) print "Preparing to save image: %s" % imageName figLegend = "" # get pixels, with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) xSize = pixels.getSizeX().getValue() ySize = pixels.getSizeY().getValue() zSize = pixels.getSizeZ().getValue() cSize = pixels.getSizeC().getValue() if pixels.getPhysicalSizeX() == None: physicalSizeX = 1.0 else: physicalSizeX = pixels.getPhysicalSizeX().getValue() if pixels.getPhysicalSizeY() == None: physicalSizeY = 1.0 else: physicalSizeY = pixels.getPhysicalSizeY().getValue() if pixels.getPhysicalSizeZ() == None: physicalSizeZ = 1.0 else: physicalSizeZ = pixels.getPhysicalSizeZ().getValue() if cIndexes == None: cIndexes = range(cSize) # prepare rawPixelStore pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) # export an EM image for every channel for theC in cIndexes: e = EMData() em = EMData(xSize,ySize,zSize) # if the physical size was in microns (in OMERO) now it's in Angstroms! em.set_attr('apix_x', physicalSizeX) em.set_attr('apix_y', physicalSizeY) em.set_attr('apix_z', physicalSizeZ) if theC > 0: saveName = imageName.replace(extension, "%s.%s" % (theC, extension)) else: saveName = imageName for z in range(zSize): # get each plane and add to EMData #print "Downloading plane: %d" % z plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, z, theC, theT) e = EMNumPy.numpy2em(plane2D) em.insert_clip(e,(0,0,z)) em.write_image(saveName) if format == None: format = "" # upload method will pick generic format. print "Uploading image: %s to server with file type: %s" % (saveName, format) # attach to image #fileId = scriptUtil.uploadAndAttachFile(queryService, updateService, rawFileStore, image, imageName, format, figLegend) # want to return the image to client, without attaching it to anything the server # still need to upload it... fileformat = scriptUtil.getFormat(queryService, format) if fileformat == None: # if we didn't find a matching format in the DB, use a generic format. fileformat = scriptUtil.getFormat(queryService, "text/plain") originalFile = scriptUtil.createFile(updateService, saveName, fileformat, saveName) scriptUtil.uploadFile(rawFileStore, originalFile, saveName) print "File uploaded with ID:", originalFile.getId().getValue() originalFiles.append(originalFile) return originalFiles
def omeroToEm(commandArgs): # log-in client = omero.client(commandArgs["host"]) session = client.createSession(commandArgs["username"], commandArgs["password"]) # get the services we need queryService = session.getQueryService() updateService = session.getUpdateService() rawFileStore = session.createRawFileStore() rawPixelStore = session.createRawPixelsStore() containerService = session.getContainerService() images = [] if "image" in commandArgs: iId = long(commandArgs["image"]) i = containerService.getImages("Image", [iId], None)[0] images.append(i) elif "dataset" in commandArgs: dIds = [long(commandArgs["dataset"])] images = containerService.getImages("Dataset", dIds, None) else: print "No image or dataset ID given" return path = None if "path" in commandArgs: path = commandArgs["path"] if not os.path.exists(path): print "Given path: %s not found. Saving images in current directory." % path path = None extension = None format = None if "extension" in commandArgs: extension = commandArgs["extension"] if extension in filetypes: format = filetypes[extension] print "Saving all images as .%s files. Format: %s" % (extension, filetypes[extension]) else: print "Invalid extension: %s (not supported by EMAN2). Will attempt to get extensions from image names." % extension extension = None else: print "No extension specified. Will attempt get extensions from image names." for image in images: iName = image.getName().getValue() imageName = os.path.basename(iName) # make sure no dir separators in name. imageId = image.getId().getValue() if (extension == None) or (extension not in filetypes): # try to get extension from image name lastDotIndex = imageName.rfind(".") # .rpartition(sep) if lastDotIndex >= 0: extension = imageName[lastDotIndex+1:] if extension in filetypes: format = filetypes[extension] if (extension == None) or (extension not in filetypes): print "File extension from image invalid. Could not export image ID: %d Name: %s Extension: %s" % (imageId, imageName, extension) continue if not imageName.endswith(".%s" % extension): imageName = "%s.%s" % (imageName, extension) if path: imageName = os.path.join(path,imageName) i = 1 # don't overwrite. Add number before extension dirName, ext = imageName.rsplit(".", 1) while os.path.exists(imageName): imageName = "%s_%s.%s" % (dirName,i,ext) i +=1 print "Preparing to save image: %s" % imageName figLegend = "" # get pixels, with pixelsType query_string = "select p from Pixels p join fetch p.image i join fetch p.pixelsType pt where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) xSize = pixels.getSizeX().getValue() ySize = pixels.getSizeY().getValue() zSize = pixels.getSizeZ().getValue() # prepare rawPixelStore theC, theT = (0, 0) pixelsId = pixels.getId().getValue() bypassOriginalFile = True rawPixelStore.setPixelsId(pixelsId, bypassOriginalFile) e = EMData() em = EMData(xSize,ySize,zSize) for z in range(zSize): # get each plane and add to EMData #print "Downloading plane: %d" % z plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, z, theC, theT) EMNumPy.numpy2em(plane2D, e) em.insert_clip(e,(0,0,z)) em.write_image(imageName)
def split_image(client, imageId, dir, unformattedImageName = "tubulin_P037_T%05d_C%s_Z%d_S1.tif", dims = ('T', 'C', 'Z')): """ ** NB: This method is implemented in script_utils. I have simply copied it here temporarily since I have made some edits that are not yet committed and updated on nightshade. ** Splits the image into component planes, which are saved as local tiffs according to unformattedImageName. E.g. myLocalDir/tubulin_P037_T%05d_C%s_Z%d_S1.tif which will be formatted according to dims, E.g. ('T', 'C', 'Z') Channel will be formatted according to channel name, not index. @param rawPixelsStore The rawPixelStore @param queryService @param c The C-Section to retrieve. @param t The T-Section to retrieve. @param imageName the local location to save the image. """ unformattedImageName = os.path.join(dir, unformattedImageName) session = client.getSession() queryService = session.getQueryService() rawPixelsStore = session.createRawPixelsStore() pixelsService = session.getPixelsService() from numpy import zeros, int8, fromfunction try: from PIL import Image except: import Image query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() sizeC = pixels.getSizeC().getValue() sizeT = pixels.getSizeT().getValue() rawPixelsStore.setPixelsId(pixels.getId().getValue(), True) channelMap = {} cIndex = 0 pixels = pixelsService.retrievePixDescription(pixels.id.val) # load channels for c in pixels.iterateChannels(): lc = c.getLogicalChannel() channelMap[cIndex] = lc.getName() and lc.getName().getValue() or str(cIndex) cIndex += 1 def formatName(unformatted, z, c, t): # need to turn dims E.g. ('T', 'C', 'Z') into tuple, E.g. (t, c, z) dimMap = {'T': t, 'C':channelMap[c], 'Z': z} dd = tuple([dimMap[d] for d in dims]) return unformatted % dd # cecog does this, but other formats may want to start at 0 zStart = 1 tStart = 1 # loop through dimensions, saving planes as tiffs. for z in range(sizeZ): for c in range(sizeC): for t in range(sizeT): imageName = formatName(unformattedImageName, z+zStart, c, t+tStart) print "downloading plane z: %s c: %s t: %s to %s" % (z, c, t, imageName) plane = script_utils.downloadPlane(rawPixelsStore, pixels, z, c, t) print "plane dtype: %s min: %s max: %s" % (plane.dtype.name, plane.min(), plane.max()) # need plane dtype to be int8 for conversion to tiff by PIL if plane.dtype.name == 'int16' or plane.dtype.name == 'uint16': minVal = plane.min() maxVal = plane.max() valRange = maxVal - minVal scaled = (plane - minVal) * (float(255) / valRange) convArray = zeros(plane.shape, dtype=int8) convArray += scaled print "using converted int8 plane: dtype: %s min: %s max: %s" % (convArray.dtype.name, convArray.min(), convArray.max()) i = Image.fromarray(convArray) else: i = Image.fromarray(plane) i.save(imageName)
def projection_axis(request, imageId, axis, get_slice=False, conn=None, **kwargs): import time startTime = time.time() x_proj_key = "x_proj_%s" % imageId y_proj_key = "y_proj_%s" % imageId z_proj_key = "z_proj_%s" % imageId x_slice_key = "x_slice_%s" % imageId y_slice_key = "y_slice_%s" % imageId z_slice_key = "z_slice_%s" % imageId # see if we have cached the projection we need key = "%s_proj_%s" % (axis, imageId) if get_slice: key = "%s_slice_%s" % (axis, imageId) #print "\n\nchecking cache for array: %s" % key #print " %s secs" % (time.time() - startTime) proj = cache.get(key) if proj == None: #print "creating cube of data..." #print " %s secs" % (time.time() - startTime) rawPixelStore = conn.createRawPixelsStore() queryService = conn.getQueryService() query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0, 0, 0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() # create a 3D numpy array, and populate it with data cube = zeros((sizeZ, sizeY, sizeX)) theC, theT = (0, 0) for theZ in range(sizeZ): plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) cube[sizeZ - theZ - 1] = plane2D # do the 3 projections and 3 central slices while we have the cube - save projections, not cube #print "doing projection" #print " %s secs" % (time.time() - startTime) proj_z = zeros((sizeY, sizeX)) for z in range(sizeZ): zPlane = cube[z, :, :] proj_z += zPlane slice_z = cube[sizeZ / 2, :, :] proj_x = zeros((sizeZ, sizeY)) for x in range(sizeX): xPlane = cube[:, :, x] proj_x += xPlane slice_x = cube[:, :, sizeX / 2] proj_y = zeros((sizeZ, sizeX)) for y in range(sizeY): yPlane = cube[:, y, :] proj_y += yPlane slice_y = cube[:, sizeY / 2, :] #print "setting cache" #print " %s secs" % (time.time() - startTime) # save arrays to cache cache.set(x_proj_key, proj_x) cache.set(x_slice_key, slice_x) cache.set(y_proj_key, proj_y) cache.set(y_slice_key, slice_y) cache.set(z_proj_key, proj_z) cache.set(z_slice_key, slice_z) if axis == "z": if get_slice: proj = slice_z else: proj = proj_z elif axis == "x": if get_slice: proj = slice_x else: proj = proj_x elif axis == "y": if get_slice: proj = slice_y else: proj = proj_y #print "got 2D plane...", proj.shape #print " %s secs" % (time.time() - startTime) tempdir = settings.FILE_UPLOAD_TEMP_DIR tempJpg = os.path.join(tempdir, ('%s.projection.jpg' % (conn._sessionUuid))).replace('\\', '/') #import matplotlib.pyplot as plt #plt.savefig(tempJpg) #import scipy #scipy.misc.imsave(tempJpg, proj) em = EMNumPy.numpy2em(proj) em.write_image(tempJpg) originalFile_data = FileWrapper(file(tempJpg)) rsp = HttpResponse(originalFile_data) rsp['Content-Type'] = "image/jpg" return rsp
def emanFilter(session, parameterMap): """ This is where the action happens. For each image, we get the data from OMERO as an EMData object, do the filtering and write back to OMERO. """ # create services we need queryService = session.getQueryService() rawFileStore = session.createRawFileStore() rawPixelStore = session.createRawPixelsStore() rawPixelStoreUpload = session.createRawPixelsStore() renderingEngine = session.createRenderingEngine() pixelsService = session.getPixelsService() containerService = session.getContainerService() updateService = session.getUpdateService() imageIds = [] dataType = parameterMap["Data_Type"] if dataType == "Image": for imageId in parameterMap["IDs"]: iId = long(imageId.getValue()) imageIds.append(iId) else: # Dataset for datasetId in parameterMap["IDs"]: datasetIds = [] try: dId = long(datasetId.getValue()) datasetIds.append(dId) except: pass # simply aggregate all images from the datasets images = containerService.getImages("Dataset", datasetIds, None) for i in images: imageIds.append(i.getId().getValue()) if len(imageIds) == 0: return # get the project from the first image project = None dataset = None imageId = imageIds[0] query_string = "select i from Image i join fetch i.datasetLinks idl join fetch idl.parent d join fetch d.projectLinks pl join fetch pl.parent where i.id in (%s)" % imageId image = queryService.findByQuery(query_string, None) if image: for link in image.iterateDatasetLinks(): dataset = link.parent print "Dataset", dataset.name.val for dpLink in dataset.iterateProjectLinks(): project = dpLink.parent print "Project", project.name.val break # only use 1st Project break # only use 1st Dataset if "New_Dataset_Name" in parameterMap: # make a dataset for images dataset = omero.model.DatasetI() dataset.name = rstring(parameterMap["New_Dataset_Name"]) from datetime import datetime dt = datetime.now() dataset.description = rstring("Images generated by EMAN2 Filtering on %s" % dt.strftime("%A, %d. %B %Y %I:%M%p")) dataset = updateService.saveAndReturnObject(dataset) if project: # and put it in the same project link = omero.model.ProjectDatasetLinkI() link.parent = omero.model.ProjectI(project.id.val, False) link.child = omero.model.DatasetI(dataset.id.val, False) updateService.saveAndReturnObject(link) filterName = parameterMap["Filter_Name"] paramStrings = [] filterParamMap = None if "Filter_Params" in parameterMap: filterParamMap = {} fpMap = parameterMap["Filter_Params"] for p, v in fpMap.items(): paramStrings.append("%s: %s" % (p, v.getValue())) # get value from rtype try: filterParamMap[p] = float(v.getValue()) # if the value is a float except: filterParamMap[p] = v.getValue() paramString = ", ".join(paramStrings) # just for adding to new image description e = EMData() bypassOriginalFile = True newImages = [] for imageId in imageIds: # set up pixel-store and get the pixels object query_string = "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%d'" % imageId pixels = queryService.findByQuery(query_string, None) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() sizeC = pixels.getSizeC().getValue() sizeT = pixels.getSizeT().getValue() rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) em = EMData(sizeX,sizeY,sizeZ) pixelsType = pixels.pixelsType # create the new image, ready to take planes description = "Created from image ID: %s by applying EMAN2 filter: '%s' with parameters: %s" % (imageId, filterName, paramString) imageName = pixels.image.name.val # new image has same name print imageName, description iId = None image = None # need to loop through extra dimensions, since EMAN2 only handles 3D. for theC in range(sizeC): minValue = None maxValue = 0 for theT in range(sizeT): for z in range(sizeZ): plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, z, theC, theT) try: # method seems to depend which version of EMAN2 you have EMNumPy.numpy2em(plane2D, e) except: e = EMNumPy.numpy2em(plane2D) em.insert_clip(e,(0,0,z)) # do the filtering if filterParamMap: em.process_inplace(filterName, filterParamMap) else: em.process_inplace(filterName) # convert back to numpy (datatype may be different) and upload to OMERO as new image filteredPlanes = EMNumPy.em2numpy(em) print "em.get_zsize()", em.get_zsize() print "filteredPlanes", filteredPlanes.shape if iId == None: iId = createImage(pixelsService, queryService, filteredPlanes, sizeT, sizeC, imageName, description) image = containerService.getImages("Image", [iId], None)[0] pixelsId = image.getPrimaryPixels().getId().getValue() rawPixelStoreUpload.setPixelsId(pixelsId, bypassOriginalFile) if em.get_zsize() > 1: # 3D array for z, plane in enumerate(filteredPlanes): if minValue == None: minValue = plane.min() minValue = min(minValue, plane.min()) maxValue = max(maxValue, plane.max()) scriptUtil.uploadPlane(rawPixelStoreUpload, plane, z, theC, theT) else: # 2D array if minValue == None: minValue = filteredPlanes.min() minValue = min(minValue, filteredPlanes.min()) maxValue = max(maxValue, filteredPlanes.max()) scriptUtil.uploadPlane(rawPixelStoreUpload, filteredPlanes, z, theC, theT) print "Setting the min, max ", minValue, maxValue pixelsService.setChannelGlobalMinMax(pixelsId, theC, float(minValue), float(maxValue)) scriptUtil.resetRenderingSettings(renderingEngine, pixelsId, theC, minValue, maxValue) image.name = rstring(imageName) image.description = rstring(description) newImages.append(updateService.saveAndReturnObject(image)) # put image in dataset if dataset: dlink = omero.model.DatasetImageLinkI() dlink.parent = omero.model.DatasetI(dataset.id.val, False) dlink.child = omero.model.ImageI(iId, False) updateService.saveAndReturnObject(dlink) return (dataset, newImages)
def projection_axis(request, imageId, axis, get_slice=False): conn = getConnection(request) import time startTime = time.time() x_proj_key = "x_proj_%s" % imageId y_proj_key = "y_proj_%s" % imageId z_proj_key = "z_proj_%s" % imageId x_slice_key = "x_slice_%s" % imageId y_slice_key = "y_slice_%s" % imageId z_slice_key = "z_slice_%s" % imageId # see if we have cached the projection we need key = "%s_proj_%s" % (axis, imageId) if get_slice: key = "%s_slice_%s" % (axis, imageId) # print "\n\nchecking cache for array: %s" % key # print " %s secs" % (time.time() - startTime) proj = cache.get(key) if proj == None: # print "creating cube of data..." # print " %s secs" % (time.time() - startTime) rawPixelStore = conn.createRawPixelsStore() queryService = conn.getQueryService() query_string = ( "select p from Pixels p join fetch p.image as i join fetch p.pixelsType where i.id='%s'" % imageId ) pixels = queryService.findByQuery(query_string, None) theZ, theC, theT = (0, 0, 0) bypassOriginalFile = True rawPixelStore.setPixelsId(pixels.getId().getValue(), bypassOriginalFile) sizeX = pixels.getSizeX().getValue() sizeY = pixels.getSizeY().getValue() sizeZ = pixels.getSizeZ().getValue() # create a 3D numpy array, and populate it with data cube = zeros((sizeZ, sizeY, sizeX)) theC, theT = (0, 0) for theZ in range(sizeZ): plane2D = scriptUtil.downloadPlane(rawPixelStore, pixels, theZ, theC, theT) cube[sizeZ - theZ - 1] = plane2D # do the 3 projections and 3 central slices while we have the cube - save projections, not cube # print "doing projection" # print " %s secs" % (time.time() - startTime) proj_z = zeros((sizeY, sizeX)) for z in range(sizeZ): zPlane = cube[z, :, :] proj_z += zPlane slice_z = cube[sizeZ / 2, :, :] proj_x = zeros((sizeZ, sizeY)) for x in range(sizeX): xPlane = cube[:, :, x] proj_x += xPlane slice_x = cube[:, :, sizeX / 2] proj_y = zeros((sizeZ, sizeX)) for y in range(sizeY): yPlane = cube[:, y, :] proj_y += yPlane slice_y = cube[:, sizeY / 2, :] # print "setting cache" # print " %s secs" % (time.time() - startTime) # save arrays to cache cache.set(x_proj_key, proj_x) cache.set(x_slice_key, slice_x) cache.set(y_proj_key, proj_y) cache.set(y_slice_key, slice_y) cache.set(z_proj_key, proj_z) cache.set(z_slice_key, slice_z) if axis == "z": if get_slice: proj = slice_z else: proj = proj_z elif axis == "x": if get_slice: proj = slice_x else: proj = proj_x elif axis == "y": if get_slice: proj = slice_y else: proj = proj_y # print "got 2D plane...", proj.shape # print " %s secs" % (time.time() - startTime) tempdir = settings.FILE_UPLOAD_TEMP_DIR tempJpg = os.path.join(tempdir, ("%s.projection.jpg" % (conn._sessionUuid))).replace("\\", "/") # import matplotlib.pyplot as plt # plt.savefig(tempJpg) # import scipy # scipy.misc.imsave(tempJpg, proj) em = EMNumPy.numpy2em(proj) em.write_image(tempJpg) originalFile_data = FileWrapper(file(tempJpg)) rsp = HttpResponse(originalFile_data) rsp["Content-Type"] = "image/jpg" return rsp