Example #1
0
def download_image(conn, image, polys):

    im_name = 'Image%s.ome.tif' % (image.getId())
    exporter = OMEExporter(conn, image, input_dir, im_name, ROI=polys)
    exporter.generate()
    im_path = os.path.join(input_dir, im_name)
    return im_name, im_path
Example #2
0
def download_tiles(conn, image, theC, theZ):
    """ 
    Export every plane in the original image, or those
    selected by the user, as OME-TIFF

    :param conn:    BlitzGateway connection
    :param image:   the omero image being downloaded
    :param theC:    the image channel being downloaded
    :parem theZ:    the z-plane being downloaded
    """
    num_tiles = image.getSizeT()
    image_names = []
    for t in range(num_tiles):
        im_name = "tile_T{}.ome.tif".format(t)
        exporter = OMEExporter(conn,
                               image,
                               input_dir,
                               im_name,
                               theZ=theZ,
                               theC=theC,
                               theT=t)
        exporter.generate()
        image_names.append(im_name)

    return image_names
def download_image(conn,image, polys):
    
    im_name = 'Image%s.ome.tif' % (image.getId())
    exporter = OMEExporter(conn,image,input_dir,im_name,ROI=polys)
    exporter.generate()
    im_path = os.path.join(input_dir,im_name)
    return im_name,im_path
def download_tiles(conn,image,theC,theZ):
    # export every plane in the original image, or those
    # selected by the user, as OME-TIFF
    print "theZ passed to exporter",theZ    
    num_tiles = image.getSizeT()
    image_names = []
    for t in range(num_tiles):
        im_name = 'tile_T%s.ome.tif' % t
        exporter = OMEExporter(conn,image,input_dir,im_name,theZ=theZ,theC=theC,theT=t)
        exporter.generate()
        image_names.append(im_name)

    return image_names
Example #5
0
def download_tiles(conn, image, theC, theZ):
    # export every plane in the original image, or those
    # selected by the user, as OME-TIFF
    print "theZ passed to exporter", theZ
    num_tiles = image.getSizeT()
    image_names = []
    for t in range(num_tiles):
        im_name = 'tile_T%s.ome.tif' % t
        exporter = OMEExporter(conn,
                               image,
                               input_dir,
                               im_name,
                               theZ=theZ,
                               theC=theC,
                               theT=t)
        exporter.generate()
        image_names.append(im_name)

    return image_names
def process_image(conn, parent_id, script_params, session):
    """
    Makes a new image from an ROI on a parent image. If the
    ROI is a PolygonI and the user chooses to 'clear outside'
    a script is run via Fiji to run the clearing. Otherwise the
    new image is created from the bounding box of the PolygonI
    
    @param conn: The BlitzGateway connection
    @param parent_id: The OMERO ID of the parent image
    @param script_params: The parameters required to run the script
    @param session: A dictionary containing session ID and hostname
    """

    global input_dir
    global output_dir
    input_dir = tempfile.mkdtemp(prefix='tiling_input')
    output_dir = tempfile.mkdtemp(prefix='tiling_output')

    def empty_dir(dir_path):
        for old_file in os.listdir(dir_path):
            file_path = os.path.join(dir_path, old_file)
            os.unlink(file_path)

    updateService = conn.getUpdateService()
    parent_image = conn.getObject("Image", parent_id)
    parentDataset = parent_image.getParent()
    parentProject = parentDataset.getParent()
    rects, polys = get_rects_from_rois(conn, parent_id)

    # Get the channels
    channels = None
    if script_params["Select_Channels"]:
        channels = script_params["Channels"]

    children = []
    child_ids = []
    # loop over bounding rectangles -
    # could have originated from a polygon ROI
    for i, r in enumerate(rects):

        # so check the shape

        empty_dir(input_dir)
        empty_dir(output_dir)

        xbox, ybox, wbox, hbox, rid, shape = r
        print 'shape:', shape
        box = r[:-1]
        child_name = 'ImageID%s_ROI%s.ome.tif' % (parent_image.getId(), rid)
        child_path = os.path.join(input_dir, child_name)

        if script_params['Clear_Outside_Polygon'] and ('PolygonI' in shape):
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels,ROI=polys)
            exporter.generate()
            child_path = run_clearing(conn, session, parent_image, \
                                      child_path, xbox, ybox)
        elif ('PolygonI' in shape):
            # retain the PolygonI in the metadata
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels,ROI=polys)
            exporter.generate()
        elif ('RectI' in shape):
            # do not write ROI to metadata
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels)
            exporter.generate()

        print 'child_path:', child_path
        newImg = do_import(conn, session, child_path)

        children.append(newImg)
        child_ids.append(newImg.getId())

        if len(child_ids) == 0:
            print "No new images created."
            return

    if script_params['New_Dataset'] and \
       len(script_params['Container_Name'].strip()) > 0:
        # create a new dataset for new images
        datasetName = script_params['Container_Name']
        print "\nMaking Dataset '%s' of Images from ROIs of Image: %s" \
            % (datasetName, parent_id)
        dataset = omero.model.DatasetI()
        dataset.name = rstring(datasetName)
        desc = "Images in this Dataset are from ROIs of parent Image:\n"\
            "  Name: %s\n  Image ID: %d" % (parent_image.getName(), parent_id)
        dataset.description = rstring(desc)
        dataset = updateService.saveAndReturnObject(dataset)
        parentDataset = dataset
    else:
        # put new images in existing dataset
        dataset = None
        if parentDataset is not None and parentDataset.canLink():
            parentDataset = parentDataset._obj
        else:
            parentDataset = None
        parentProject = None  # don't add Dataset to parent.

    if parentDataset is None:
        link = None
        print "No dataset created or found for new images."\
            " Images will be orphans."
    else:
        link = []
        for cid in child_ids:
            dsLink = omero.model.DatasetImageLinkI()
            dsLink.parent = omero.model.DatasetI(parentDataset.id.val, False)
            dsLink.child = omero.model.ImageI(cid, False)
            updateService.saveObject(dsLink)
            link.append(dsLink)
        if parentProject and parentProject.canLink():
            # and put it in the   current project
            projectLink = omero.model.ProjectDatasetLinkI()
            projectLink.parent = omero.model.ProjectI(parentProject.getId(),
                                                      False)
            projectLink.child = omero.model.DatasetI(dataset.id.val, False)
            updateService.saveAndReturnObject(projectLink)

    shutil.rmtree(input_dir)
    shutil.rmtree(output_dir)

    return children, dataset, link, child_ids
def process_image(conn, parent_id, script_params, session):
    """
    Makes a new image from an ROI on a parent image. If the
    ROI is a PolygonI and the user chooses to 'clear outside'
    a script is run via Fiji to run the clearing. Otherwise the
    new image is created from the bounding box of the PolygonI
    
    @param conn: The BlitzGateway connection
    @param parent_id: The OMERO ID of the parent image
    @param script_params: The parameters required to run the script
    @param session: A dictionary containing session ID and hostname
    """ 
    
    global input_dir
    global output_dir
    input_dir = tempfile.mkdtemp(prefix='tiling_input')    
    output_dir = tempfile.mkdtemp(prefix='tiling_output')

    def empty_dir(dir_path):
        for old_file in os.listdir(dir_path):
            file_path = os.path.join(dir_path, old_file)
            os.unlink(file_path)
                
    updateService = conn.getUpdateService()
    parent_image = conn.getObject("Image", parent_id)
    parentDataset = parent_image.getParent()
    parentProject = parentDataset.getParent()
    rects,polys = get_rects_from_rois(conn, parent_id)
    
    # Get the channels
    channels = None
    if script_params["Select_Channels"]:
        channels = script_params["Channels"]
        
    children = []
    child_ids = []
    # loop over bounding rectangles - 
    # could have originated from a polygon ROI
    for i,r in enumerate(rects):
        
        # so check the shape
        
        empty_dir(input_dir)
        empty_dir(output_dir)
        
        xbox, ybox, wbox, hbox, rid, shape = r
        print 'shape:',shape
        box = r[:-1]
        child_name ='ImageID%s_ROI%s.ome.tif'%(parent_image.getId(),rid)
        child_path = os.path.join(input_dir,child_name)
        
        if script_params['Clear_Outside_Polygon'] and ('PolygonI' in shape):
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels,ROI=polys)
            exporter.generate()
            child_path = run_clearing(conn, session, parent_image, \
                                      child_path, xbox, ybox)
        elif ('PolygonI' in shape):
            # retain the PolygonI in the metadata
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels,ROI=polys)
            exporter.generate() 
        elif ('RectI' in shape):
            # do not write ROI to metadata
            exporter = OMEExporter(conn,parent_image,input_dir,child_name,\
                                   box,theC=channels)
            exporter.generate()            
        
        print 'child_path:',child_path
        newImg = do_import(conn,session,child_path)
        
        children.append(newImg)
        child_ids.append(newImg.getId())
    
        if len(child_ids) == 0:
            print "No new images created."
            return
        
    if script_params['New_Dataset'] and \
       len(script_params['Container_Name'].strip()) > 0:
        # create a new dataset for new images
        datasetName = script_params['Container_Name']
        print "\nMaking Dataset '%s' of Images from ROIs of Image: %s" \
            % (datasetName, parent_id)
        dataset = omero.model.DatasetI()
        dataset.name = rstring(datasetName)
        desc = "Images in this Dataset are from ROIs of parent Image:\n"\
            "  Name: %s\n  Image ID: %d" % (parent_image.getName(), parent_id)
        dataset.description = rstring(desc)
        dataset = updateService.saveAndReturnObject(dataset)
        parentDataset = dataset
    else:
        # put new images in existing dataset
        dataset = None
        if parentDataset is not None and parentDataset.canLink():
            parentDataset = parentDataset._obj
        else:
            parentDataset = None
        parentProject = None    # don't add Dataset to parent.
     
    if parentDataset is None:
        link = None
        print "No dataset created or found for new images."\
            " Images will be orphans."
    else:
        link = []
        for cid in child_ids:
            dsLink = omero.model.DatasetImageLinkI()
            dsLink.parent = omero.model.DatasetI(
                parentDataset.id.val, False)
            dsLink.child = omero.model.ImageI(cid, False)
            updateService.saveObject(dsLink)
            link.append(dsLink)
        if parentProject and parentProject.canLink():
            # and put it in the   current project
            projectLink = omero.model.ProjectDatasetLinkI()
            projectLink.parent = omero.model.ProjectI(
                parentProject.getId(), False)
            projectLink.child = omero.model.DatasetI(
                dataset.id.val, False)
            updateService.saveAndReturnObject(projectLink)
            
    shutil.rmtree(input_dir)
    shutil.rmtree(output_dir)
    
    return children, dataset, link, child_ids