def upload_image_from_file(file, dataset_name, project_name='', attachments=(), wait=-1): attachments = list(attachments) dataset_id = get_or_create_dataset_id(dataset_name, project_name) with cli_login(*LOGIN_ARGS) as cli: conn = BlitzGateway(client_obj=cli._client) r = file_import(cli._client, file, wait) if r: links = [] # TODO - doing this as iterable fileset for single file is weird p = r.pixels[0] image_id = p.image.id.val logger.debug('Imported Image ID: %d' % image_id) link = omero.model.DatasetImageLinkI() link.parent = omero.model.DatasetI(dataset_id, False) link.child = omero.model.ImageI(image_id, False) links.append(link) conn.getUpdateService().saveArray(links, conn.SERVICE_OPTS) if len(attachments) > 0: # have to have loadedness -> True to link an annotation image = conn.getObject("Image", image_id) for attachment in attachments: # TODO - add guess_mimetype / namespace here upload_file_annotation(conn, image, attachment, namespace='pyme.localizations') return image_id
def run(password, project_name, dataset_name, host, port): for user_number in range(1, 41): username = "******" % user_number print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() project = ProjectI() project.setName(rstring(project_name)) update_service = conn.getUpdateService() project = update_service.saveAndReturnObject(project) ds = conn.getObject("Dataset", attributes={'name': dataset_name}, opts={'owner': conn.getUserId()}) if ds is None: print("No dataset with name %s found" % dataset_name) continue dataset_id = ds.getId() print(username, dataset_id) link = ProjectDatasetLinkI() link.setParent(ProjectI(project.getId().getValue(), False)) link.setChild(DatasetI(dataset_id, False)) conn.getUpdateService().saveObject(link) except Exception as exc: print("Error while creating project: %s" % str(exc)) finally: conn.close()
def create_containers(cli, dataset, project=None): """ Creates containers with names provided if they don't exist already. Returns Dataset ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway(host='localhost') conn.connect(sUuid=sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) d = None prId = None if project is not None: p = conn.getObject("Project", attributes={'name': project}, params=params) if p is None: print "Creating Project:", project p = omero.model.ProjectI() p.name = wrap(project) prId = conn.getUpdateService().saveAndReturnObject(p).id.val else: print "Using Project:", project, p prId = p.getId() # Since Project already exists, check children for Dataset for c in p.listChildren(): if c.getName() == dataset: d = c if d is None: d = conn.getObject("Dataset", attributes={'name': dataset}, params=params) if d is None: print "Creating Dataset:", dataset d = omero.model.DatasetI() d.name = wrap(dataset) dsId = conn.getUpdateService().saveAndReturnObject(d).id.val if prId is not None: print "Linking Project-Dataset..." link = omero.model.ProjectDatasetLinkI() link.child = omero.model.DatasetI(dsId, False) link.parent = omero.model.ProjectI(prId, False) conn.getUpdateService().saveObject(link) else: print "Using Dataset:", dataset, d dsId = d.getId() return dsId
def run(password, project_name, dataset_name, host, port): for user_number in range(1, 51): username = "******" % user_number print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) # make sure only one result is returned by query params.page(0, 1) query = "from Project where name='%s' \ AND details.owner.omeName=:username \ ORDER BY id DESC" % project_name service = conn.getQueryService() pr_list = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if pr_list is None: print("No project with name %s found" % project_name) continue project_id = pr_list[0].getId().getValue() print(username, project_id) params = omero.sys.ParametersI() params.addString('username', username) # make sure only one result is returned by query params.page(0, 1) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username \ ORDER BY id DESC" % dataset_name service = conn.getQueryService() ds_list = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if ds_list is None: print("No dataset with name %s found" % dataset_name) continue dataset_id = ds_list[0].getId().getValue() print(username, dataset_id) link = ProjectDatasetLinkI() link.setParent(ProjectI(project_id, False)) link.setChild(DatasetI(dataset_id, False)) conn.getUpdateService().saveObject(link) except Exception as exc: print("Error while linking to project: %s" % str(exc)) finally: conn.close()
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target service = conn.getQueryService() dataset = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(dataset) == 0: print("No dataset with name %s found" % target) continue dataset_obj = dataset[0] datasetId = dataset[0].getId().getValue() print('dataset', datasetId) params2 = omero.sys.ParametersI() params2.addId(dataset_obj.getId()) query = "select l.child.id from DatasetImageLink \ l where l.parent.id = :id" images = service.projection(query, params2, conn.SERVICE_OPTS) values = [] for k in range(0, len(images)): image_id = images[k][0].getValue() image = conn.getObject("Image", image_id) u = omero.model.LengthI(0.33, UnitsLength.MICROMETER) p = image.getPrimaryPixels()._obj p.setPhysicalSizeX(u) p.setPhysicalSizeY(u) values.append(p) if len(images) > 0: conn.getUpdateService().saveArray(values) except Exception as exc: print("Error during calibration: %s" % str(exc)) finally: conn.close()
def main(): try: conn = BlitzGateway(username, password, host=server, port=4064) conn.connect() #Returns True when connected updateService = conn.getUpdateService() from omero.rtypes import rdouble, rint, rstring images = conn.getObjects("Image", opts={ 'owner': owner_id, 'dataset': datasetID }) for image in images: imageId = image.getId() roi_service = conn.getRoiService() result = roi_service.findByImage(imageId, None) for roi in result.rois: print("ROI: ID:", roi.getId().getValue()) for s in roi.copyShapes(): if type(s) == omero.model.EllipseI: ctr_x = s.getX().getValue() ctr_y = s.getY().getValue() x, y, w, h = rect_roi_frm_ctr(image, ctr_x, ctr_y, width, height) MAX_GFP = max_proj(image, max_ch, x, y, w, h) BFZ9 = load_stack_plane(image, slc_ch, slc, x, y, w, h) hstk = np.stack((MAX_GFP, BFZ9), axis=0) tmp_str = 'output_' + str(imageId) + '_' + str( roi.getId().getValue()) + '.tif' output_hyperstack(hstk, tmp_str) finally: if conn: conn.close()
def create_containers(cli, dataset, project=None): """ Creates containers with names provided if they don't exist already. Returns Dataset ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway() conn.connect(sUuid = sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) d = None prId = None if project is not None: p = conn.getObject("Project", attributes={'name': project}, params=params) if p is None: print "Creating Project:", project p = omero.model.ProjectI() p.name = wrap(project) prId = conn.getUpdateService().saveAndReturnObject(p).id.val else: print "Using Project:", project, p prId = p.getId() # Since Project already exists, check children for Dataset for c in p.listChildren(): if c.getName() == dataset: d = c if d is None: d = conn.getObject("Dataset", attributes={'name': dataset}, params=params) if d is None: print "Creating Dataset:", dataset d = omero.model.DatasetI() d.name = wrap(dataset) dsId = conn.getUpdateService().saveAndReturnObject(d).id.val if prId is not None: print "Linking Project-Dataset..." link = omero.model.ProjectDatasetLinkI() link.child = omero.model.DatasetI(dsId, False) link.parent = omero.model.ProjectI(prId, False) conn.getUpdateService().saveObject(link) else: print "Using Dataset:", dataset, d dsId = d.getId() return dsId
def run(password, admin_name, target, tag, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() updateService = conn.getUpdateService() ds = conn.getObject("Dataset", attributes={'name': target}, opts={'owner': conn.getUserId()}) if ds is None: print("No dataset with name %s found" % target) continue params = omero.sys.ParametersI() params.addString('username', admin_name) query = "from TagAnnotation where textvalue='%s' \ AND details.owner.omeName=:username" % tag query_service = conn.getQueryService() tags = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(tags) == 0: print("No tag with name %s found" % tag) continue tag_id = tags[0].id.getValue() print(tag_id) links = [] for image in ds.listChildren(): name = image.getName() if name in images_to_tag: # Check first that the image is not tagged params = omero.sys.ParametersI() params.addLong('parent', image.id) params.addLong('child', tag_id) query = "select link from ImageAnnotationLink as link \ where link.parent.id=:parent \ AND link.child.id=:child" values = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) if len(values) == 0: link = ImageAnnotationLinkI() link.parent = ImageI(image.id, False) link.child = TagAnnotationI(tag_id, False) links.append(link) else: print("Tag %s already linked to %s" % (tag, name)) if len(links) > 0: updateService.saveArray(links) except Exception as exc: print("Error when tagging the images: %s" % str(exc)) finally: conn.close()
def main(argv): parser = argparse.ArgumentParser() # parser.addArgument('--session', type=int, help=( # 'Connect with this session ID, default is to use or create a ' # 'session from the OMERO CLI')) parser.add_argument( '--dataset', type=int, help=( 'Add imported files to this Dataset ID (not valid when wait=-1)')) parser.add_argument( '--wait', type=int, default=-1, help=('Wait for this number of seconds for each import to complete. ' '0: return immediately, -1: wait indefinitely (default)')) parser.add_argument('path', nargs='+', help='Files or directories') args = parser.parse_args(argv) with cli_login() as cli: conn = BlitzGateway(client_obj=cli._client) if args.dataset and not conn.getObject('Dataset', args.dataset): print('Dataset id not found: %s' % args.dataset) sys.exit(1) for fs_path in args.path: print('Importing: %s' % fs_path) rsp = full_import(cli._client, fs_path, args.wait) if rsp: links = [] for p in rsp.pixels: print('Imported Image ID: %d' % p.image.id.val) if args.dataset: link = omero.model.DatasetImageLinkI() link.parent = omero.model.DatasetI(args.dataset, False) link.child = omero.model.ImageI(p.image.id.val, False) links.append(link) conn.getUpdateService().saveArray(links, conn.SERVICE_OPTS)
def run(password, dataset_name, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Image where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() images = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(images) == 0: print("No images with name %s found" % target) continue image_id = images[0].getId().getValue() print('id', image_id) dataset = omero.model.DatasetI() dataset.setName(omero.rtypes.rstring(dataset_name)) dataset = conn.getUpdateService().saveAndReturnObject(dataset) dataset_id = dataset.getId().getValue() print(username, dataset_id) link = omero.model.DatasetImageLinkI() link.setParent(dataset) link.setChild(omero.model.ImageI(image_id, False)) conn.getUpdateService().saveObject(link) except Exception as exc: print("Error while linking images: %s" % str(exc)) finally: conn.close()
def get_or_create_dataset_id(dataset_name, project_name=''): with cli_login(*LOGIN_ARGS) as cli: conn = BlitzGateway(client_obj=cli._client) # handle linking with project if given if project_name != '': # check if the dataset already exists within the project project_id = get_or_create_project_id(conn, project_name) # projects = conn.getContainerService().loadContainerHierarchy('Project', # None, None) project = conn.getObject("Project", project_id) dataset_wrapper = project.findChildByName(dataset_name) if dataset_wrapper != None: return dataset_wrapper.getId() # make a new dataset dataset_id = create_dataset(conn, dataset_name) # link it to the project links = [] link = omero.model.ProjectDatasetLinkI() link.parent = omero.model.ProjectI(project_id, False) link.child = omero.model.DatasetI(dataset_id, False) links.append(link) conn.getUpdateService().saveArray(links, conn.SERVICE_OPTS) return dataset_id # no project specified, check if the dataset already exists datasets = conn.getContainerService().loadContainerHierarchy( 'Dataset', None, None) for d in datasets: if d.getName().getValue() == dataset_name: return d.getId().getValue() # otherwise create a new dataset return create_dataset(conn, dataset_name)
def create_screen(self, cli, screen): """ Creates screen with name provided if it doesn't exist already. Returns Screen ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway(host='localhost') conn.connect(sUuid = sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) slist = list(conn.getObjects("Screen", attributes={'name': screen}, params=params)) if len(slist) == 0: print "Creating Screen:", screen s = ScreenI() s.name = wrap(screen.encode('ascii','ignore')) scrId = conn.getUpdateService().saveAndReturnObject(s).id.val else: scrId = slist[0].getId() print "Using Screen:", screen return scrId
def run(username, password, plate_id, host, port): """Run the script.""" # Create connection to training server conn = BlitzGateway(username, password, host=host, port=port) conn.connect() # Create connection to IDR server # NB: conn.connect() not working on IDR. Do it like this idr_client = omero.client(host="idr.openmicroscopy.org", port=4064) idr_client.createSession('public', 'public') idr_conn = BlitzGateway(client_obj=idr_client) # The plate we want to copy from IDR idr_plate = idr_conn.getObject("Plate", plate_id) plate_name = idr_plate.getName() update_service = conn.getUpdateService() plate = PlateI() plate.name = rstring(plate_name) plate = update_service.saveAndReturnObject(plate) for idr_well in idr_plate.listChildren(): print("Well", idr_well.id, 'row', idr_well.row, 'col', idr_well.column) # For each Well, get image and clone locally... new_imgs = [] for idr_wellsample in idr_well.listChildren(): idr_image = idr_wellsample.getImage() print("Image", idr_image.id) image = copy_image(conn, idr_image) new_imgs.append(image) # link to Plate... add_images_to_plate(update_service, plate, new_imgs, idr_well.row, idr_well.column) conn.close() idr_conn.close()
def runScript(): """ The main entry point of the script, as called by the client via the scripting service, passing the required parameters. """ orders = [rstring('T-Z-C'), rstring('T-C-Z'), rstring('Z-T-C'), rstring('Z-C-T'), rstring('C-Z-T'), rstring('C-T-Z')] dataTypes = [rstring('Dataset'), rstring('Image')] client = scripts.client('ExplodeImages.py', 'Explode multiplane/multitemporal images into multiple images, ' + 'split based on channels, number of planes, and timestamps.', scripts.String("Data_Type", optional=False, grouping="1", description="The data you want to work with.", values=dataTypes, default="Image"), scripts.List("IDs", optional=False, grouping="1.1", description="List of Dataset IDs or Image IDs").ofType(rlong(0)), scripts.String("Storing_Dataset", optional=False, grouping="2", description="Name of data set to store images", default='Explode'), scripts.String("Base_Image_Path", grouping="2.1", description="Base name of image."), scripts.String("Image_Order", grouping="2.2", description="The order in which explosion information is displayed in" + " the image name. (T=time, Z=depth, C=color)", values=orders, default="T-Z-C"), version="1.0", authors=["WebvalleyTeam 2013"], institutions=["FBK"], contact="*****@*****.**", ) try: # process the list of args above. scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) # establish connection to omero conn = BlitzGateway(client_obj=client) #get objects objects, logMessage = script_utils.getObjects(conn, scriptParams) # gather all images images = [] if (scriptParams["Data_Type"] == "Dataset"): for dataSet in objects: images.extend(list(dataSet.listChildren())) if not images: print "No images found in selected dataset." else: images = objects datasetObj = omero.model.DatasetI() datasetObj.setName(rstring(scriptParams["Storing_Dataset"])) datasetObj = conn.getUpdateService().saveAndReturnObject(datasetObj) savePlanes(images, conn, datasetObj, scriptParams) finally: client.closeSession()
""" FOR TRAINING PURPOSES ONLY! """ import omero from omero.rtypes import rdouble, rint, rstring from omero.gateway import BlitzGateway from Connect_To_OMERO import USERNAME, PASSWORD, HOST, PORT # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() updateService = conn.getUpdateService() # Configuration # ================================================================= imageId = 27544 # Create ROI. # ================================================================= # We are using the core Python API and omero.model objects here, since ROIs are # not yet supported in the Python Blitz Gateway. # # In this example, we create an ROI with a rectangular shape and attach it to an # image. x = 50
def run(): """ """ dataTypes = [rstring("Plate")] client = scripts.client( "Manage_Plate_Acquisitions.py", "Add or remove PlateAcquisition(s) in a given Plate", scripts.String("Data_Type", optional=False, grouping="1", description="The data type you want to work with.", values=dataTypes, default="Plate"), scripts.List("IDs", optional=False, grouping="2", description="List of Plate IDs").ofType(rlong(0)), scripts.String("Mode", optional=False, grouping="3", description="Select if you want to add or " "remove PlateAcquisitions", values=[rstring("Add"), rstring("Remove")], default="Add"), version="0.2", authors=["Niko Klaric"], institutions=["Glencoe Software Inc."], contact="*****@*****.**", ) try: scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) connection = BlitzGateway(client_obj=client) updateService = connection.getUpdateService() queryService = connection.getQueryService() processedMessages = [] for plateId in scriptParams["IDs"]: plateObj = connection.getObject("Plate", plateId) if plateObj is None: client.setOutput( "Message", rstring("ERROR: No Plate with ID %s" % plateId)) return if scriptParams["Mode"] == "Add": plateAcquisitionObj = PlateAcquisitionI() plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False)) wellGrid = plateObj.getWellGrid() for axis in wellGrid: for wellObj in axis: wellSampleList = wellObj.copyWellSamples() plateAcquisitionObj.addAllWellSampleSet(wellSampleList) plateAcquisitionObj = updateService.saveAndReturnObject( plateAcquisitionObj) plateAcquisitionId = plateAcquisitionObj.getId()._val processedMessages.append( "Linked new PlateAcquisition with ID %d" " to Plate with ID %d." % (plateAcquisitionId, plateId)) else: params = ParametersI() params.addId(plateId) queryString = """ FROM PlateAcquisition AS pa LEFT JOIN FETCH pa.wellSample LEFT OUTER JOIN FETCH pa.annotationLinks WHERE pa.plate.id = :id """ plateAcquisitionList = queryService.findAllByQuery( queryString, params, connection.SERVICE_OPTS) if plateAcquisitionList: updateList = [] for plate_acquisition in plateAcquisitionList: for well_sample in plate_acquisition.copyWellSample(): well_sample.setPlateAcquisition(None) updateList.append(well_sample) updateService.saveArray(updateList) plate_acquisition.clearWellSample() plate_acquisition.clearAnnotationLinks() plate_acquisition = updateService.saveAndReturnObject( plate_acquisition) updateService.deleteObject(plate_acquisition) processedMessages.append( "%d PlateAcquisition(s) removed from Plate with ID %d." % (len(plateAcquisitionList), plateId)) client.setOutput("Message", rstring("No errors. %s" % " ".join(processedMessages))) finally: client.closeSession()
# Configuration # ================================================================= dataset_name = "MyDataset" tag_name = "MyTag" # Create Datasets # ================================================================= object_array = list() for i in xrange(3): dataset = omero.model.DatasetI() dataset.setName(rstring(dataset_name)) object_array.append(dataset) conn.getUpdateService().saveArray(object_array) # Create Tags # ================================================================= object_array = list() for i in xrange(3): tag = omero.model.TagAnnotationI() tag.setTextValue(rstring(tag_name)) tag.setDescription(rstring("%s %i" % (tag_name, i))) object_array.append(tag) conn.getUpdateService().saveArray(object_array) # Find the datasets by name. # =================================================================
else: shapeTexts.append("") if len(lengthsForImage) == 0: print "No lines found on Image:", image.getName() continue imgAverage = sum(lengthsForImage) / len(lengthsForImage) print "Average length of line for Image: %s is %s" % (image.getName(), imgAverage) # Add the average as an annotation on each image. lengthAnn = omero.model.DoubleAnnotationI() lengthAnn.setDoubleValue(rdouble(imgAverage)) lengthAnn.setNs(rstring("imperial.training.demo.lineLengthAverage")) link = omero.model.ImageAnnotationLinkI() link.setParent(omero.model.ImageI(image.getId(), False)) link.setChild(lengthAnn) conn.getUpdateService().saveAndReturnObject(link) lengthsForImage = [] # reset for next image. # Prepare data for adding to OMERO table. data = [ omero.grid.LongColumn('imageId', '', imageIds), omero.grid.RoiColumn('roidId', '', roiIds), omero.grid.LongColumn('shapeId', '', shapeIds), omero.grid.LongColumn('theZ', '', theZs), omero.grid.LongColumn('theT', '', theTs), omero.grid.DoubleColumn('lineLength', '', lineLengths), omero.grid.StringColumn('shapeText', '', 64, shapeTexts), ] table.addData(data)
class Omg(object): """ OMERO gateway that wraps Blitz gateway and CLI, intended for scripting and interactive work. Attributes ---------- conn : Blitz gateway connection """ def __init__(self, conn=None, user=None, passwd=None, server=SERVER, port=PORT, skey=None): """ Requires active Blitz connection OR username plus password or sesskey """ if conn is None and (user is None or (passwd is None and skey is None)): raise ValueError("Bad parameters," + self.__init__.__doc__) if conn is not None: if conn.isConnected(): self.conn = conn else: raise ValueError("Cannot initialize with closed connection!") else: if passwd is not None: self.conn = BlitzGateway(user, passwd, host=server, port=port) self.conn.connect() else: self.conn = BlitzGateway(user, host=server, port=port) self.conn.connect(skey) if self.conn.isConnected(): self._server = self.conn.host self._port = self.conn.port self._user = self.conn.getUser().getName() self._key = self.conn.getSession().getUuid().getValue() print("Connected to {0} (port {1}) as {2}, session key={3}".format( self._server, self._port, self._user, self._key)) else: print("Failed to open connection :-(") def ls(self): """ Print groups, then projects/datasets/images for current group. """ print("Groups for {0}:-".format(self.conn.getUser().getName())) for gid, gname in self._ls_groups(): print(" {0} ({1})".format(gname, str(gid))) curr_grp = self.conn.getGroupFromContext() gid, gname = curr_grp.getId(), curr_grp.getName() print("\nData for current group, {0} ({1}):-".format(gname, gid)) for pid, pname in self._ls_projects(): print(" Project: {0} ({1})".format(pname, str(pid))) for did, dname in self._ls_datasets(pid): print(" Dataset: {0} ({1})".format(dname, str(did))) for iid, iname in self._ls_images(did): print(" Image: {0} ({1})".format(iname, str(iid))) # TODO, list orphaned Datasets and Images def _ls_groups(self): """list groups (id, name) this session is a member of""" groups = self.conn.getGroupsMemberOf() return [(group.getId(), group.getName()) for group in groups] def _ls_projects(self): """list projects (id, name) in the current session group""" projs = self.conn.listProjects(self.conn.getUserId()) return [(proj.getId(), proj.getName()) for proj in projs] def _ls_datasets(self, proj_id): """list datasets (id, name) within the project id given""" dsets = self.conn.getObject("Project", proj_id).listChildren() return [(dset.getId(), dset.getName()) for dset in dsets] def _ls_images(self, dset_id): """list images (id, name) within the dataset id given""" imgs = self.conn.getObject("Dataset", dset_id).listChildren() return [(img.getId(), img.getName()) for img in imgs] def chgrp(self, group_id): """ Change group for this session to the group_id given. """ self.conn.setGroupForSession(group_id) def get(self, im_id, get_att=True): """ Download the specified image as an OME-TIFF to current directory, with attachments also downloaded to folder: img_path + '_attachments' Return : path to downloaded image """ img = self.conn.getObject("Image", oid=im_id) img_name = self._unique_name(img.getName(), im_id) img_path = os.path.join(os.getcwd(), img_name) img_file = open(str(img_path + ".ome.tiff"), "wb") fsize, blockgen = img.exportOmeTiff(bufsize=65536) for block in blockgen: img_file.write(block) img_file.close() fa_type = omero.model.FileAnnotationI attachments = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == fa_type] if get_att and len(attachments) > 0: att_dir = img_path + "_attachments" os.mkdir(att_dir) def download_attachment(att, att_dir): """download OMERO file annotation to att_dir""" att_file = open(os.path.join(att_dir, att.getFileName()), "wb") for att_chunk in att.getFileInChunks(): att_file.write(att_chunk) att_file.close() for att in attachments: download_attachment(att, att_dir) return img_path def _unique_name(self, img_name, im_id): """Make unique name combining a file basename & OMERO Image id""" path_and_base, ext = os.path.splitext(img_name) base = os.path.basename(path_and_base) # name in OMERO can has path return "{0}_{1}".format(base, str(im_id)) def dget(self, dataset_id): """ Download an entire OMERO Dataset to the current directory. """ downloads = [] wdir = os.getcwd() dset_name = self.conn.getObject("Dataset", dataset_id).getName() dset_path = os.path.join(wdir, dset_name + "_D" + str(dataset_id)) os.mkdir(dset_path) os.chdir(dset_path) for img_id, img_name in self._ls_images(dataset_id): downloads.append(self.get(img_id)) os.chdir(wdir) return downloads def pget(self, project_id): """ Download an entire OMERO Project to the current directory. """ downloads = [] wdir = os.getcwd() proj_name = self.conn.getObject("Project", project_id).getName() proj_path = os.path.join(wdir, proj_name + "_P" + str(project_id)) os.mkdir(proj_path) os.chdir(proj_path) for dset_id, dset_name in self._ls_datasets(project_id): downloads.extend(self.dget(dset_id)) os.chdir(wdir) return downloads def put(self, filename, name=None, dataset=None): """ Import filename using OMERO CLI, optionally with a specified name to a specified dataset (dataset_id). Return : OMERO image Id """ cli = omero.cli.CLI() cli.loadplugins() import_args = ["import"] import_args.extend(["-s", str(self._server)]) import_args.extend(["-k", str(self._key)]) if dataset is not None: import_args.extend(["-d", str(dataset)]) if name is not None: import_args.extend(["-n", str(name)]) clio = "cli.out" clie = "cli.err" import_args.extend(["---errs=" + clie, "---file=" + clio, "--"]) import_args.append(filename) cli.invoke(import_args, strict=True) pix_id = int(open(clio, 'r').read().rstrip()) im_id = self.conn.getQueryService().get("Pixels", pix_id).image.id.val os.remove(clio) os.remove(clie) return im_id def describe(self, im_id, description): """ Append to image description. """ img = self.conn.getObject("Image", oid=im_id) old_description = img.getDescription() or "" img.setDescription(old_description + "\n" + description) img.save() def attach(self, im_id, attachments): """ Attach a list of files to an image. """ img = self.conn.getObject("Image", oid=im_id) for attachment in attachments.split(): fann = self.conn.createFileAnnfromLocalFile(attachment) img.linkAnnotation(fann) img.save() # TODO: ls_tags() and tag() methods? def mkp(self, project_name, description=None): """ Make new OMERO project in current group, returning the new project Id. """ # see: omero/lib/python/omeroweb/webclient/controller/container.py proj = omero.model.ProjectI() proj.name = omero.rtypes.rstring(str(project_name)) if description is not None and description != "": proj.description = omero.rtypes.rstring(str(description)) return self._save_and_return_id(proj) def mkd(self, dataset_name, project_id=None, description=None): """ Make new OMERO dataset, returning the new dataset Id. """ dset = omero.model.DatasetI() dset.name = omero.rtypes.rstring(str(dataset_name)) if description is not None and description != "": dset.description = omero.rtypes.rstring(str(description)) if project_id is not None: l_proj_dset = omero.model.ProjectDatasetLinkI() proj = self.conn.getObject("Project", project_id) l_proj_dset.setParent(proj._obj) l_proj_dset.setChild(dset) dset.addProjectDatasetLink(l_proj_dset) return self._save_and_return_id(dset) def _save_and_return_id(self, obj): """Save new omero object and return id assgined to it""" # see: OmeroWebGateway.saveAndReturnId # in: lib/python/omeroweb/webclient/webclient_gateway.py u_s = self.conn.getUpdateService() res = u_s.saveAndReturnObject(obj, self.conn.SERVICE_OPTS) res.unload() return res.id.val def im(self, im_id): """ Return an Im object for the image id specified. """ img = self.conn.getObject("Image", im_id) # build pixel np.ndarray nx, ny = img.getSizeX(), img.getSizeY() nz, nt, nc = img.getSizeZ(), img.getSizeT(), img.getSizeC() planes = [(z, c, t) for c in range(nc) for t in range(nt) for z in range(nz)] pix_gen = img.getPrimaryPixels().getPlanes(planes) pix = np.array([i for i in pix_gen]).reshape((nc, nt, nz, ny, nx)) # initialize Im using pix and extracted metadata meta = self._extract_meta(img, im_id) return Im(pix=pix, meta=meta) def _extract_meta(self, img, im_id): """Extract metadata attributes from OMERO Blitz gateway Image""" meta = {} meta['name'] = self._unique_name(img.getName(), im_id) meta['description'] = img.getDescription() def _extract_ch_info(ch): """extract core metadata for for channel, return as dict""" ch_info = {'label': ch.getLabel()} ch_info['ex_wave'] = ch.getExcitationWave() ch_info['em_wave'] = ch.getEmissionWave() ch_info['color'] = ch.getColor().getRGB() return ch_info meta['channels'] = [_extract_ch_info(ch) for ch in img.getChannels()] meta['pixel_size'] = {'x': img.getPixelSizeX(), 'y': img.getPixelSizeY(), 'z': img.getPixelSizeZ(), 'units': "um"} tag_type = omero.model.TagAnnotationI tags = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == tag_type] meta['tags'] = {tag.getValue() + " (" + str(tag.getId()) + ")": tag.getDescription() for tag in tags} fa_type = omero.model.FileAnnotationI attachments = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == fa_type] meta['attachments'] = [att.getFileName() + " (" + str(att.getId()) + ")" for att in attachments] user_id = self.conn.getUser().getName() + " (" + \ str(self.conn.getUser().getId()) + ") @" + self.conn.host meta_ext = {} meta_ext['user_id'] = user_id meta['meta_ext'] = meta_ext # TODO: ROIs, display settings? # objective: Image.loadOriginalMetadata()[1][find 'Lens ID Number'][1], return meta def imput(self, im, dataset_id=None): """ Create a new OMERO Image using an Im object, returning new image id. """ # see: omero/lib/python/omero/util/script_utils.py # see: omero/lib/python/omeroweb/webclient/webclient_gateway.py # see: https://gist.github.com/will-moore/4141708 if not isinstance(im, Im): raise TypeError("first imput argument must be of type Im") nc, nt, nz, ny, nx = im.shape ch_nums = range(nc) q_s = self.conn.getQueryService() p_s = self.conn.getPixelsService() c_s = self.conn.getContainerService() u_s = self.conn.getUpdateService() pu_s = self.conn.c.sf.createRawPixelsStore() q_ptype = "from PixelsType as p where p.value='{0}'".format( str(im.dtype)) pixelsType = q_s.findByQuery(q_ptype, None) im_id = p_s.createImage(nx, ny, nz, nt, ch_nums, pixelsType, im.name, im.description) img_i = c_s.getImages("Image", [im_id.getValue()], None)[0] img = self.conn.getObject("Image", im_id.getValue()) pix_id = img_i.getPrimaryPixels().getId().getValue() pu_s.setPixelsId(pix_id, True) for c in range(nc): for t in range(nt): for z in range(nz): plane = im.pix[c, t, z, :, :] script_utils.uploadPlaneByRow(pu_s, plane, z, c, t) l_dset_im = omero.model.DatasetImageLinkI() dset = self.conn.getObject("Dataset", dataset_id) l_dset_im.setParent(dset._obj) l_dset_im.setChild(img._obj) self._update_meta(im, im_id) u_s.saveObject(l_dset_im, self.conn.SERVICE_OPTS) return im_id.getValue() def _update_meta(self, im, im_id): """Set OMERO Image metadata using Im metadata"""
print 'Created new Image:%s Name:"%s"' % (i.getId(), i.getName()) # Set the pixel size using units (new in 5.1.0) # ================================================================= # Lengths are specified by value and a unit enumeration # Here we set the pixel size X and Y to be 9.8 Angstroms from omero.model.enums import UnitsLength # Re-load the image to avoid update conflicts i = conn.getObject("Image", i.getId()) u = omero.model.LengthI(9.8, UnitsLength.ANGSTROM) p = i.getPrimaryPixels()._obj p.setPhysicalSizeX(u) p.setPhysicalSizeY(u) conn.getUpdateService().saveObject(p) # Create an Image from an existing image # ================================================================= # We are going to create a new image by passing the method a 'generator' of 2D # planes This will come from an existing image, by taking the average of 2 # channels. zctList = [] image = conn.getObject('Image', imageId) sizeZ, sizeC, sizeT = image.getSizeZ(), image.getSizeC(), image.getSizeT() dataset = image.getParent() pixels = image.getPrimaryPixels() newSizeC = 1
def run(): """ """ dataTypes = [rstring("Plate")] client = scripts.client( "Manage_Plate_Acquisitions.py", "Add or remove PlateAcquisition(s) in a given Plate", scripts.String("Data_Type", optional=False, grouping="1", description="The data type you want to work with.", values=dataTypes, default="Plate"), scripts.List("IDs", optional=False, grouping="2", description="List of Plate IDs").ofType(rlong(0)), scripts.String("Mode", optional=False, grouping="3", description="Select if you want to add or " "remove PlateAcquisitions", values=[rstring("Add"), rstring("Remove")], default="Add"), version="0.2", authors=["Niko Klaric"], institutions=["Glencoe Software Inc."], contact="*****@*****.**", ) try: scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) connection = BlitzGateway(client_obj=client) updateService = connection.getUpdateService() queryService = connection.getQueryService() processedMessages = [] for plateId in scriptParams["IDs"]: plateObj = connection.getObject("Plate", plateId) if plateObj is None: client.setOutput( "Message", rstring("ERROR: No Plate with ID %s" % plateId)) return if scriptParams["Mode"] == "Add": plateAcquisitionObj = PlateAcquisitionI() plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False)) wellGrid = plateObj.getWellGrid() for axis in wellGrid: for wellObj in axis: wellSampleList = wellObj.copyWellSamples() plateAcquisitionObj.addAllWellSampleSet(wellSampleList) plateAcquisitionObj = updateService.saveAndReturnObject( plateAcquisitionObj) plateAcquisitionId = plateAcquisitionObj.getId()._val processedMessages.append( "Linked new PlateAcquisition with ID %d" " to Plate with ID %d." % (plateAcquisitionId, plateId)) else: params = ParametersI() params.addId(plateId) queryString = """ FROM PlateAcquisition AS pa LEFT JOIN FETCH pa.wellSample LEFT OUTER JOIN FETCH pa.annotationLinks WHERE pa.plate.id = :id """ plateAcquisitionList = queryService.findAllByQuery( queryString, params, connection.SERVICE_OPTS) if plateAcquisitionList: updateList = [] for plate_acquisition in plateAcquisitionList: for well_sample in plate_acquisition.copyWellSample(): well_sample.setPlateAcquisition(None) updateList.append(well_sample) updateService.saveArray(updateList) plate_acquisition.clearWellSample() plate_acquisition.clearAnnotationLinks() plate_acquisition = updateService.saveAndReturnObject( plate_acquisition) updateService.deleteObject(plate_acquisition) processedMessages.append( "%d PlateAcquisition(s) removed from Plate with ID %d." % (len(plateAcquisitionList), plateId)) client.setOutput( "Message", rstring("No errors. %s" % " ".join(processedMessages))) finally: client.closeSession()
def CreatePlate(plateName, user, passwd): import sys sys.path.append('/home/cag3fr/omero_server/lib/python') import Ice import omero from omero.gateway import BlitzGateway import omero.scripts as scripts import omero.util.script_utils as script_utils from omero.rtypes import rint, rlong, rstring, robject, unwrap import pandas as pd import csv, operator import fnmatch import os import math # Dictionary of Well Number and Row/Column Number Well96_to_RowCol = { 'A01': [0, 0], 'A02': [0, 1], 'A03': [0, 2], 'A04': [0, 3], 'A05': [0, 4], 'A06': [0, 5], 'A07': [0, 6], 'A08': [0, 7], 'A09': [0, 8], 'A10': [0, 9], 'A11': [0, 10], 'A12': [0, 11], 'B01': [1, 0], 'B02': [1, 1], 'B03': [1, 2], 'B04': [1, 3], 'B05': [1, 4], 'B06': [1, 5], 'B07': [1, 6], 'B08': [1, 7], 'B09': [1, 8], 'B10': [1, 9], 'B11': [1, 10], 'B12': [1, 11], 'C01': [2, 0], 'C02': [2, 1], 'C03': [2, 2], 'C04': [2, 3], 'C05': [2, 4], 'C06': [2, 5], 'C07': [2, 6], 'C08': [2, 7], 'C09': [2, 8], 'C10': [2, 9], 'C11': [2, 10], 'C12': [2, 11], 'D01': [3, 0], 'D02': [3, 1], 'D03': [3, 2], 'D04': [3, 3], 'D05': [3, 4], 'D06': [3, 5], 'D07': [3, 6], 'D08': [3, 7], 'D09': [3, 8], 'D10': [3, 9], 'D11': [3, 10], 'D12': [3, 11], 'E01': [4, 0], 'E02': [4, 1], 'E03': [4, 2], 'E04': [4, 3], 'E05': [4, 4], 'E06': [4, 5], 'E07': [4, 6], 'E08': [4, 7], 'E09': [4, 8], 'E10': [4, 9], 'E11': [4, 10], 'E12': [4, 11], 'F01': [5, 0], 'F02': [5, 1], 'F03': [5, 2], 'F04': [5, 3], 'F05': [5, 4], 'F06': [5, 5], 'F07': [5, 6], 'F08': [5, 7], 'F09': [5, 8], 'F10': [5, 9], 'F11': [5, 10], 'F12': [5, 11], 'G01': [6, 0], 'G02': [6, 1], 'G03': [6, 2], 'G04': [6, 3], 'G05': [6, 4], 'G06': [6, 5], 'G07': [6, 6], 'G08': [6, 7], 'G09': [6, 8], 'G10': [6, 9], 'G11': [6, 10], 'G12': [6, 11], 'H01': [7, 0], 'H02': [7, 1], 'H03': [7, 2], 'H04': [7, 3], 'H05': [7, 4], 'H06': [7, 5], 'H07': [7, 6], 'H08': [7, 7], 'H09': [7, 8], 'H10': [7, 9], 'H11': [7, 10], 'H12': [7, 11] } #Create plate in omero conn = BlitzGateway(user, passwd, host="localhost", port=4064) conn.connect() updateService = conn.getUpdateService() plate = omero.model.PlateI() plate.name = omero.rtypes.RStringI(plateName) plate.columnNamingConvention = rstring("number") plate.rowNamingConvention = rstring("letter") plate = updateService.saveAndReturnObject(plate) plateId = plate.id.val plate = conn.getObject("Plate", plateId) conn.close() filename = plateName + ".txt" f = open(filename, "w") f.write(str(plateId)) f.close()
def CreateWell(imageId, plateId, wellNum, user, passwd): import sys sys.path.append('/home/cag3fr/omero_server/lib/python') import Ice import omero from omero.gateway import BlitzGateway import omero.scripts as scripts import omero.util.script_utils as script_utils from omero.rtypes import rint, rlong, rstring, robject, unwrap import pandas as pd import csv, operator import fnmatch import os import math # Dictionary of Well Number and Row/Column Number Well96_to_RowCol = { 'A01': [0, 0], 'A02': [0, 1], 'A03': [0, 2], 'A04': [0, 3], 'A05': [0, 4], 'A06': [0, 5], 'A07': [0, 6], 'A08': [0, 7], 'A09': [0, 8], 'A10': [0, 9], 'A11': [0, 10], 'A12': [0, 11], 'B01': [1, 0], 'B02': [1, 1], 'B03': [1, 2], 'B04': [1, 3], 'B05': [1, 4], 'B06': [1, 5], 'B07': [1, 6], 'B08': [1, 7], 'B09': [1, 8], 'B10': [1, 9], 'B11': [1, 10], 'B12': [1, 11], 'C01': [2, 0], 'C02': [2, 1], 'C03': [2, 2], 'C04': [2, 3], 'C05': [2, 4], 'C06': [2, 5], 'C07': [2, 6], 'C08': [2, 7], 'C09': [2, 8], 'C10': [2, 9], 'C11': [2, 10], 'C12': [2, 11], 'D01': [3, 0], 'D02': [3, 1], 'D03': [3, 2], 'D04': [3, 3], 'D05': [3, 4], 'D06': [3, 5], 'D07': [3, 6], 'D08': [3, 7], 'D09': [3, 8], 'D10': [3, 9], 'D11': [3, 10], 'D12': [3, 11], 'E01': [4, 0], 'E02': [4, 1], 'E03': [4, 2], 'E04': [4, 3], 'E05': [4, 4], 'E06': [4, 5], 'E07': [4, 6], 'E08': [4, 7], 'E09': [4, 8], 'E10': [4, 9], 'E11': [4, 10], 'E12': [4, 11], 'F01': [5, 0], 'F02': [5, 1], 'F03': [5, 2], 'F04': [5, 3], 'F05': [5, 4], 'F06': [5, 5], 'F07': [5, 6], 'F08': [5, 7], 'F09': [5, 8], 'F10': [5, 9], 'F11': [5, 10], 'F12': [5, 11], 'G01': [6, 0], 'G02': [6, 1], 'G03': [6, 2], 'G04': [6, 3], 'G05': [6, 4], 'G06': [6, 5], 'G07': [6, 6], 'G08': [6, 7], 'G09': [6, 8], 'G10': [6, 9], 'G11': [6, 10], 'G12': [6, 11], 'H01': [7, 0], 'H02': [7, 1], 'H03': [7, 2], 'H04': [7, 3], 'H05': [7, 4], 'H06': [7, 5], 'H07': [7, 6], 'H08': [7, 7], 'H09': [7, 8], 'H10': [7, 9], 'H11': [7, 10], 'H12': [7, 11] } conn = BlitzGateway(user, passwd, host="localhost", port=4064) conn.connect() image = conn.getObject("Image", int(imageId[6:])) updateService = conn.getUpdateService() well = omero.model.WellI() well.plate = omero.model.PlateI(int(plateId), False) wellRowCol = Well96_to_RowCol.get(wellNum) well.column = rint(wellRowCol[1]) well.row = rint(wellRowCol[0]) ws = omero.model.WellSampleI() ws.image = omero.model.ImageI(image.id, False) ws.well = well well.addWellSample(ws) updateService.saveAndReturnObject(ws) conn.close()
data1 = omero.grid.LongColumn('Uid', 'test Long', ids) data2 = omero.grid.StringColumn('MyStringColumn', '', 64, strings) data = [data1, data2] table.addData(data) orig_file = table.getOriginalFile() table.close() # when we are done, close. # Load the table as an original file # ================================== orig_file_id = orig_file.id.val # ...so you can attach this data to an object e.g. Dataset file_ann = omero.model.FileAnnotationI() # use unloaded OriginalFileI file_ann.setFile(omero.model.OriginalFileI(orig_file_id, False)) file_ann = conn.getUpdateService().saveAndReturnObject(file_ann) link = omero.model.DatasetAnnotationLinkI() link.setParent(omero.model.DatasetI(datasetId, False)) link.setChild(omero.model.FileAnnotationI(file_ann.getId().getValue(), False)) conn.getUpdateService().saveAndReturnObject(link) # Table API # ========= # .. seealso:: :javadoc:` OMERO Tables <slice2html/omero/grid/Table.html>` open_table = resources.openTable(orig_file) print("Table Columns:") for col in open_table.getHeaders(): print(" ", col.name) rowCount = open_table.getNumberOfRows()
class TestSavedMasks(ITest): def setup_method(self): self.conn = BlitzGateway(client_obj=self.client) def save_and_return_masks(self, im, masks): roi = omero.model.RoiI() for mask in masks: roi.addShape(mask) us = self.conn.getUpdateService() roi.setImage(im._obj) roi = us.saveAndReturnObject(roi) assert roi rois = self.conn.getRoiService().findByImage(im.id, None) assert len(rois.rois) == 1 shapes = rois.rois[0].copyShapes() assert len(shapes) == len(masks) assert all((type(mask) == omero.model.MaskI) for mask in shapes) return shapes @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_mask_from_binary_image(self, binary_image, args): im = self.conn.createImageFromNumpySeq(iter([binary_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px, **args) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 2 assert unwrap(mask.getHeight()) == 2 assert unwrap(mask.getX()) == 1 assert unwrap(mask.getY()) == 1 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), np.array([224], dtype=np.uint8)) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None def test_mask_from_binary_full_image(self): binim = np.ones((4, 4), dtype=np.uint8) im = self.conn.createImageFromNumpySeq(iter([binim]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 4 assert unwrap(mask.getHeight()) == 4 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), np.array([255, 255], dtype=np.uint8)) @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_masks_from_label_image(self, label_image, args): im = self.conn.createImageFromNumpySeq(iter([label_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() masks = masks_from_label_image(px, **args) masks = self.save_and_return_masks(im, masks) # The rest of this is more or less the same as the unit test expected = ( # w, h, x, y, bytes (2, 2, 1, 1, np.array([224], dtype=np.uint8)), (2, 3, 2, 0, np.array([72], dtype=np.uint8)), ) assert len(masks) == 2 for i, mask in enumerate(masks): assert unwrap(mask.getWidth()) == expected[i][0] assert unwrap(mask.getHeight()) == expected[i][1] assert unwrap(mask.getX()) == expected[i][2] assert unwrap(mask.getY()) == expected[i][3] assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), expected[i][4]) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None @pytest.mark.parametrize('args', [{}, { 'rgba': (255, 128, 64, 128), 'z': 1, 'c': 2, 't': 3, 'text': 'test' }]) def test_empty_mask_from_binary_image(self, args): empty_binary_image = np.array([[0]], dtype=np.uint8) raise_on_no_mask = False im = self.conn.createImageFromNumpySeq(iter([empty_binary_image]), 'omero-rois-integration') # Reload im = self.conn.getObject('Image', im.id) px = im.getPrimaryPixels().getPlane() mask = mask_from_binary_image(px, raise_on_no_mask=raise_on_no_mask, **args) mask = self.save_and_return_masks(im, [mask])[0] # The rest of this is more or less the same as the unit test assert unwrap(mask.getWidth()) == 0 assert unwrap(mask.getHeight()) == 0 assert unwrap(mask.getX()) == 0 assert unwrap(mask.getY()) == 0 assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), []) if args: assert unwrap(mask.getTheZ()) == 1 assert unwrap(mask.getTheC()) == 2 assert unwrap(mask.getTheT()) == 3 assert unwrap(mask.getTextValue()) == 'test' else: assert unwrap(mask.getTheZ()) is None assert unwrap(mask.getTheC()) is None assert unwrap(mask.getTheT()) is None assert unwrap(mask.getTextValue()) is None
datasetByImageName = {} df = pandas.read_csv(annoFile) for index, row in df.iterrows(): if row["Image Name"] in datasetByImageName: raise Exception(" !!! line %i : %s has already been added" % (index, row["Image Name"])) datasetByImageName[row["Image Name"]] = row["Dataset Name"] datasetByName = {} for imgName, dsName in datasetByImageName.iteritems(): if dsName not in existingDatasetsByName: if dsName not in datasetByName: dataset = omero.model.DatasetI() dataset.setName(rstring(dsName)) dataset = conn.getUpdateService().saveAndReturnObject(dataset) datasetByName[dsName] = dataset datasetByImageName[imgName] = dataset else: datasetByImageName[imgName] = datasetByName[dsName] else: datasetByImageName[imgName] = existingDatasetsByName[dsName] links = [] for ds in datasetByName.values(): link = omero.model.ProjectDatasetLinkI() link.setParent(project._obj) link.setChild(ds) links.append(link) if len(links) > 0: conn.getUpdateService().saveAndReturnArray(links)
import omero from omero.rtypes import rstring from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Create a new Dataset # ================================================================= dataset = omero.model.DatasetI() dataset.setName(rstring("New Dataset")) dataset = conn.getUpdateService().saveAndReturnObject(dataset) print("New dataset, Id:", dataset.getId().getValue()) dataset2 = omero.model.DatasetI() dataset2.setName(rstring("New Dataset2")) dataset2 = conn.getUpdateService().saveAndReturnObject(dataset2) print("New dataset 2, Id:", dataset2.getId().getValue()) # How to annotate multiple Datasets # ================================================================= # 1. you define objects you want to tag dataset_list = [dataset, dataset2] # 2. create a tag tag = omero.model.TagAnnotationI() tag.setTextValue(rstring("new tag"))
from omero.model.enums import UnitsLength from omero.rtypes import rdouble, rint, rstring from omero.gateway import BlitzGateway from omero.gateway import ColorHolder from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT from Parse_OMERO_Properties import imageId imageId = int(imageId) """ start-code """ # Create a connection # =================== conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() updateService = conn.getUpdateService() # Create ROI # ========== # We are using the core Python API and omero.model objects here, since ROIs # are not yet supported in the Python Blitz Gateway. # # First we load our image and pick some parameters for shapes x = 50 y = 200 width = 100 height = 50 image = conn.getObject("Image", imageId) z = old_div(image.getSizeZ(), 2) t = 0
userConn.connect() for g in userConn.getGroupsMemberOf(): if g.getName() == "user": continue print " ", g.getName() userConn.SERVICE_OPTS.setOmeroGroup(g.getId()) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = rlong(exp.getId()) tags = list( userConn.getObjects("TagAnnotation", params=params) ) for i in range( TAG_COUNT-len(tags) ): t = TagAnnotationI() newTagVal = "%s_%s_TEST" % (username, g.getName()) print "creating TAG:", newTagVal t.textValue = wrap(str(newTagVal)) userConn.getUpdateService().saveObject(t, userConn.SERVICE_OPTS) # for t in tags: # print " TAG", t.getId(), t.getTextValue() userConn.c.closeSession() print "\n---- DOING ANNOTATING... ----\n" # We want to Tag loads of stuff with OUR tags and Others' tags for exp in allUsers: username = exp.getOmeName() if username not in USER_NAMES: continue print "\n\n------------ USER:"******"------------" userConn = BlitzGateway(username, "ome") userConn.connect()
""" start-code """ # Create a connection # =================== conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Create a new Dataset # ==================== datasetObj = omero.model.DatasetI() datasetObj.setName(rstring("New Dataset")) datasetObj = conn.getUpdateService().saveAndReturnObject(datasetObj) datasetId = datasetObj.getId().getValue() print "New dataset, Id:", datasetId # Link to Project # =============== project = conn.getObject("Project", projectId) if project is None: import sys sys.stderr.write("Error: Object does not exist.\n") sys.exit(1) link = omero.model.ProjectDatasetLinkI() link.setParent(omero.model.ProjectI(project.getId(), False)) link.setChild(datasetObj) conn.getUpdateService().saveObject(link)
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Configuration # ================================================================= dataset_name = "MyDataset" tag_name = "MyTag" # Create Datasets # ================================================================= object_array = list() for i in xrange(3): dataset = omero.model.DatasetI() dataset.setName(rstring(dataset_name)) object_array.append(dataset) conn.getUpdateService().saveArray(object_array) # Create Tags # ================================================================= object_array = list() for i in xrange(3): tag = omero.model.TagAnnotationI() tag.setTextValue(rstring(tag_name)) tag.setDescription(rstring("%s %i" % (tag_name, i))) object_array.append(tag) conn.getUpdateService().saveArray(object_array) # Find the datasets by name. # ================================================================= datasets = conn.getObjects("Dataset", attributes={'name': dataset_name}) print "\nList Datasets:"
# Save channel names and colors # ================================================================= print("Applying channel Names:", c_names, " Colors:", colors) for i, c in enumerate(new_img.getChannels()): lc = c.getLogicalChannel() lc.setName(c_names[i]) lc.save() r, g, b = colors[i] # need to reload channels to avoid optimistic lock on update c_obj = conn.getQueryService().get("Channel", c.getId()) c_obj.red = rint(r) c_obj.green = rint(g) c_obj.blue = rint(b) c_obj.alpha = rint(255) conn.getUpdateService().saveObject(c_obj) new_img.resetRDefs() # reset based on colors above # Apply pixel sizes from original image # ================================================================= new_pix = conn.getQueryService().get("Pixels", new_img.getPixelsId()) new_pix.setPhysicalSizeX(pixels.getPhysicalSizeX()) new_pix.setPhysicalSizeY(pixels.getPhysicalSizeY()) new_pix.setPhysicalSizeZ(pixels.getPhysicalSizeZ()) conn.getUpdateService().saveObject(new_pix) # Close connection: # ================================================================= # When you are done, close the session to free up server resources. conn.close()
import omero.callbacks from omero.rtypes import rstring from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Create a new Project # ================================================================= project = omero.model.ProjectI() project.setName(rstring("New Project")) project = conn.getUpdateService().saveAndReturnObject(project) projectId = project.id.val # Load the Project # ================================================================= project = conn.getObject("Project", projectId) if project is None: import sys sys.stderr.write("Error: Object does not exist.\n") sys.exit(1) print "\nProject:", project.getName() # Delete Project # =================================================================
import omero import omero.callbacks from omero.rtypes import rstring from omero.gateway import BlitzGateway from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT # Create a connection # ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Create a new Project # ================================================================= project = omero.model.ProjectI() project.setName(rstring("New Project")) project = conn.getUpdateService().saveAndReturnObject(project) projectId = project.id.val # Load the Project # ================================================================= project = conn.getObject("Project", projectId) if project is None: import sys sys.stderr.write("Error: Object does not exist.\n") sys.exit(1) print "\nProject:", project.getName() # Delete Project # ================================================================= # You can delete a number of objects of the same type at the same
def run(username, password, plate_id, host, port): conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() query_service = conn.getQueryService() # Create a name for the Original File tablename = "Channels_Min_Max_Intensity" # Go through all wells in Plate, adding row for each plate = conn.getObject("Plate", plate_id) wellIds = [] rowData = [] chCount = 0 for well in plate._listChildren(): well = omero.gateway.WellWrapper(conn, well) image = well.getImage() if image is None: continue wellIds.append(well.id) chCount = image.getSizeC() row = [] print("well, image", well.id, image.id) params = omero.sys.ParametersI() params.addId(image.getPixelsId()) query = """select pixels from Pixels as pixels left outer join fetch pixels.channels as channels join fetch channels.statsInfo where pixels.id=:id""" result = query_service.findAllByQuery(query, params) row = [] for pix in result: for ch in pix.iterateChannels(): si = ch.statsInfo row.extend([si.globalMin.val, si.globalMax.val]) rowData.append(row) print('wellIds', wellIds) print('rowData', rowData) # Now we know how many channels, we can make the table col1 = omero.grid.WellColumn('Well', '', []) columns = [col1] colNames = [] for chIdx in range(chCount): for name in ['Ch%sMin' % chIdx, 'Ch%sMax' % chIdx]: colNames.append(name) columns.append(omero.grid.LongColumn(name, '', [])) table = conn.c.sf.sharedResources().newTable(1, tablename) table.initialize(columns) # Add Data from above data1 = omero.grid.WellColumn('Well', '', wellIds) data = [data1] for colIdx in range(len(rowData[0])): colData = [r[colIdx] for r in rowData] print("colData", len(colData)) name = colNames[colIdx] data.append(omero.grid.LongColumn(name, '', colData)) print("Adding data: ", len(data)) table.addData(data) table.close() print("table closed...") orig_file = table.getOriginalFile() fileAnn = omero.model.FileAnnotationI() fileAnn.ns = rstring(NAMESPACE) fileAnn.setFile(omero.model.OriginalFileI(orig_file.id.val, False)) fileAnn = conn.getUpdateService().saveAndReturnObject(fileAnn) link = omero.model.PlateAnnotationLinkI() link.setParent(omero.model.PlateI(plate_id, False)) link.setChild(omero.model.FileAnnotationI(fileAnn.id.val, False)) print("save link...") conn.getUpdateService().saveAndReturnObject(link) except Exception as exc: print("Error while changing names: %s" % str(exc)) finally: conn.close()
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT from Parse_OMERO_Properties import projectId """ start-code """ # Create a connection # =================== conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Create a new Dataset # ==================== datasetObj = omero.model.DatasetI() datasetObj.setName(rstring("New Dataset")) datasetObj = conn.getUpdateService().saveAndReturnObject(datasetObj) datasetId = datasetObj.getId().getValue() print "New dataset, Id:", datasetId # Link to Project # =============== project = conn.getObject("Project", projectId) if project is None: import sys sys.stderr.write("Error: Object does not exist.\n") sys.exit(1) link = omero.model.ProjectDatasetLinkI() link.setParent(omero.model.ProjectI(project.getId(), False)) link.setChild(datasetObj) conn.getUpdateService().saveObject(link)
parser.add_argument("--to-pass", default="omero") parser.add_argument("source_image", type=int, help="input image") parser.add_argument("target_image", type=int, help="output image") ns = parser.parse_args() image_id_src = ns.source_image image_id_dst = ns.target_image idr = BlitzGateway(ns.from_user, ns.from_pass, host=ns.from_host, secure=True) local = BlitzGateway(ns.to_user, ns.to_pass, host=ns.to_host, secure=True) idr.connect() local.connect() query_service = idr.getQueryService() update_service = local.getUpdateService() query = "FROM Mask WHERE roi.image.id = :id" params = ParametersI() params.addId(image_id_src) count = 0 for mask_src in query_service.findAllByQuery(query, params): mask_dst = MaskI() mask_dst.x = mask_src.x mask_dst.y = mask_src.y mask_dst.width = mask_src.width mask_dst.height = mask_src.height mask_dst.theZ = mask_src.theZ
# ================================================================= conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) conn.connect() # Configuration # ================================================================= datasetId = 101 projectId = 51 # Create a new Dataset # ================================================================= dataset = omero.model.DatasetI() dataset.setName(rstring("New Dataset")) dataset = conn.getUpdateService().saveAndReturnObject(dataset) print "New dataset, Id:", dataset.getId().getValue() dataset2 = omero.model.DatasetI() dataset2.setName(rstring("New Dataset2")) dataset2 = conn.getUpdateService().saveAndReturnObject(dataset2) print "New dataset 2, Id:", dataset2.getId().getValue() # How to annotate multiple Datasets # ================================================================= # 1. you define objects you want to tag dataset_list = [dataset, dataset2] # 2. create a tag tag = omero.model.TagAnnotationI()
else: shapeTexts.append("") if len(lengthsForImage) == 0: print "No lines found on Image:", image.getName() continue imgAverage = sum(lengthsForImage) / len(lengthsForImage) print "Average length of line for Image: %s is %s" % (image.getName(), imgAverage) # Add the average as an annotation on each image. lengthAnn = omero.model.DoubleAnnotationI() lengthAnn.setDoubleValue(rdouble(imgAverage)) lengthAnn.setNs(rstring("imperial.training.demo.lineLengthAverage")) link = omero.model.ImageAnnotationLinkI() link.setParent(omero.model.ImageI(image.getId(), False)) link.setChild(lengthAnn) conn.getUpdateService().saveAndReturnObject(link) lengthsForImage = [] # reset for next image. # Prepare data for adding to OMERO table. data = [ omero.grid.LongColumn("imageId", "", imageIds), omero.grid.RoiColumn("roidId", "", roiIds), omero.grid.LongColumn("shapeId", "", shapeIds), omero.grid.LongColumn("theZ", "", theZs), omero.grid.LongColumn("theT", "", theTs), omero.grid.DoubleColumn("lineLength", "", lineLengths), omero.grid.StringColumn("shapeText", "", 64, shapeTexts), ] table.addData(data)
"""Adds Image to Dataset (if not alreay in the Dataset).""", # first parameter scripts.Long("ImageId", optional=False), # second parameter scripts.Long("DatasetId", optional=False), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) # get the parameters imageId = unwrap(client.getInput("ImageId")) datasetId = unwrap(client.getInput("DatasetId")) try: link = omero.model.DatasetImageLinkI() link.setParent(omero.model.DatasetI(datasetId, False)) link.setChild(omero.model.ImageI(imageId, False)) conn.getUpdateService().saveObject(link) message = "Added Image to Dataset" except ValidationException: message = "Could not add Image to Dataset - Already added" # Return some value(s). # Here, we return anything useful the script has produced. # NB: The Insight and web clients will display the "Message" output. client.setOutput("Message", rstring(message)) client.closeSession()
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() datasets = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(datasets) == 0: print("No datasets with name %s found" % target) continue dataset_id = datasets[0].getId().getValue() print('dataset', dataset_id) params2 = omero.sys.ParametersI() params2.addId(dataset_id) query = "select l.child.id from DatasetImageLink l \ where l.parent.id = :id" images = query_service.projection(query, params2, conn.SERVICE_OPTS) for k in range(0, len(images)): image_id = images[k][0].getValue() delta_t = 300 image = conn.getObject("Image", image_id) params = omero.sys.ParametersI() params.addLong('pid', image.getPixelsId()) query = "from PlaneInfo as Info where \ Info.theZ=0 and Info.theC=0 and pixels.id=:pid" info_list = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) print('info_list', len(info_list)) if len(info_list) == 0: print("Creating info...", image.getSizeT()) info_list = [] for t_index in range(image.getSizeT()): print(' t', t_index) info = PlaneInfoI() info.theT = rint(t_index) info.theZ = rint(0) info.theC = rint(0) info.pixels = PixelsI(image.getPixelsId(), False) dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) info_list.append(info) else: for info in info_list: unwrap_t = unwrap(info.theT) unwrap_z = unwrap(info.theZ) print('theT %s, theZ %s' % (unwrap_t, unwrap_z)) t_index = info.theT.getValue() dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) print("Saving info_list", len(info_list)) conn.getUpdateService().saveArray(info_list) except Exception as exc: print("Error when setting the timestamps: %s" % str(exc)) finally: conn.close()
# Save channel names and colors # ================================================================= print "Applying channel Names:", cNames, " Colors:", colors for i, c in enumerate(newImg.getChannels()): lc = c.getLogicalChannel() lc.setName(cNames[i]) lc.save() r, g, b = colors[i] # need to reload channels to avoid optimistic lock on update cObj = conn.getQueryService().get("Channel", c.id) cObj.red = rint(r) cObj.green = rint(g) cObj.blue = rint(b) cObj.alpha = rint(255) conn.getUpdateService().saveObject(cObj) newImg.resetRDefs() # reset based on colors above # Apply pixel sizes from original image # ================================================================= newPix = conn.getQueryService().get("Pixels", newImg.getPixelsId()) physicalSizeX = pixels.getPhysicalSizeX() if physicalSizeX is not None: newPix.setPhysicalSizeX(rdouble(physicalSizeX)) physicalSizeY = pixels.getPhysicalSizeY() if physicalSizeY is not None: newPix.setPhysicalSizeY(rdouble(physicalSizeY))