def get_planes(session, pixels):
    pixels_service = session.createRawPixelsStore()

    planes = list()
    dtype = toNumpy(pixels.pixelsType.value.val)
    try:
        ctx = {'omero.group': '-1'}
        pixels_service.setPixelsId(pixels.id.val, True, ctx)
        for c in xrange(pixels.sizeC.val):
            plane = pixels_service.getPlane(0, c, 0)
            plane = np.fromstring(plane, dtype=dtype)
            plane = np.reshape(plane, (pixels.sizeY.val, -1))
            if sys.byteorder == 'little':
                # Data coming from OMERO is always big endian
                plane = plane.byteswap()
            planes.append(plane)
    finally:
        pixels_service.close()
    return planes
Ejemplo n.º 2
0
def readFlimImageFile(rawPixelsStore, pixels):
    """
    Read the RawImageFlimFile with fileId and return it as an array [c, x, y]
    @param rawPixelsStore The rawPixelStore service to get the image.
    @param pixels The pixels of the image.
    @return The Contents of the image for z = 0, t = 0, all channels;
    """
    from numpy import zeros
    sizeC = pixels.getSizeC().getValue();
    sizeX = pixels.getSizeX().getValue();
    sizeY = pixels.getSizeY().getValue();
    id = pixels.getId().getValue();
    pixelsType = pixels.getPixelsType().getValue().getValue();
    rawPixelsStore.setPixelsId(id , False);
    cRange = range(0, sizeC);
    stack = zeros((sizeC, sizeX, sizeY),dtype=pixelstypetopython.toNumpy(pixelsType));
    for c in cRange:
        plane = downloadPlane(rawPixelsStore, pixels, 0, c, 0);
        stack[c,:,:]=plane;
    return stack;
Ejemplo n.º 3
0
def readFlimImageFile(rawPixelsStore, pixels):
    """
    Read the RawImageFlimFile with fileId and return it as an array [c, x, y]
    @param rawPixelsStore The rawPixelStore service to get the image.
    @param pixels The pixels of the image.
    @return The Contents of the image for z = 0, t = 0, all channels;
    """
    from numpy import zeros
    sizeC = pixels.getSizeC().getValue();
    sizeX = pixels.getSizeX().getValue();
    sizeY = pixels.getSizeY().getValue();
    id = pixels.getId().getValue();
    pixelsType = pixels.getPixelsType().getValue().getValue();
    rawPixelsStore.setPixelsId(id , False);
    cRange = range(0, sizeC);
    stack = zeros((sizeC, sizeX, sizeY),dtype=pixelstypetopython.toNumpy(pixelsType));
    for c in cRange:
        plane = downloadPlane(rawPixelsStore, pixels, 0, c, 0);
        stack[c,:,:]=plane;
    return stack;
Ejemplo n.º 4
0
def downloadPlane(rawPixelsStore, pixels, z, c, t):
    """
    Download the plane [z,c,t] for image pixels. Pixels must have pixelsType loaded.
    N.B. The rawPixelsStore must have already been initialised by setPixelsId()
    @param rawPixelsStore The rawPixelStore service to get the image.
    @param pixels The pixels of the image.
    @param z The Z-Section to retrieve.
    @param c The C-Section to retrieve.
    @param t The T-Section to retrieve.
    @return The Plane of the image for z, c, t
    """
    from numpy import array
    rawPlane = rawPixelsStore.getPlane(z, c, t);
    sizeX = pixels.getSizeX().getValue();
    sizeY = pixels.getSizeY().getValue();
    pixelType = pixels.getPixelsType().getValue().getValue();
    convertType ='>'+str(sizeX*sizeY)+pixelstypetopython.toPython(pixelType);
    convertedPlane = unpack(convertType, rawPlane);
    numpyType = pixelstypetopython.toNumpy(pixelType)
    remappedPlane = array(convertedPlane, numpyType);
    remappedPlane.resize(sizeY, sizeX);
    return remappedPlane;
Ejemplo n.º 5
0
def downloadPlane(rawPixelsStore, pixels, z, c, t):
    """
    Download the plane [z,c,t] for image pixels. Pixels must have pixelsType loaded.
    N.B. The rawPixelsStore must have already been initialised by setPixelsId()
    @param rawPixelsStore The rawPixelStore service to get the image.
    @param pixels The pixels of the image.
    @param z The Z-Section to retrieve.
    @param c The C-Section to retrieve.
    @param t The T-Section to retrieve.
    @return The Plane of the image for z, c, t
    """
    from numpy import array
    rawPlane = rawPixelsStore.getPlane(z, c, t)
    sizeX = pixels.getSizeX().getValue()
    sizeY = pixels.getSizeY().getValue()
    pixelType = pixels.getPixelsType().getValue().getValue()
    convertType = '>' + str(
        sizeX * sizeY) + pixelstypetopython.toPython(pixelType)
    convertedPlane = unpack(convertType, rawPlane)
    numpyType = pixelstypetopython.toNumpy(pixelType)
    remappedPlane = array(convertedPlane, numpyType)
    remappedPlane.resize(sizeY, sizeX)
    return remappedPlane
Ejemplo n.º 6
0
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)