def get_image_ids(self): """Get the Ids of imported images. Note that this will not find images if they have not been imported. Also, while image_ids are returned, this method also sets ``self.image_ids``. Returns ------- image_ids : list of ints Ids of images imported from the specified client path, which itself is derived from ``self.file_path`` and ``self.filename``. """ if self.imported is not True: logging.error(f'File {self.file_path} has not been imported') return None else: q = self.conn.getQueryService() params = Parameters() path_query = str(self.file_path).strip('/') params.map = {"cpath": rstring(path_query)} results = q.projection( "SELECT i.id FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " WHERE u.clientPath=:cpath", params, self.conn.SERVICE_OPTS) self.image_ids = [r[0].val for r in results] return self.image_ids
def image_has_imported_filename(conn, im_id, imported_filename): """Ask whether an image is associated with a particular image file. THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED Sometimes we know the filename of an image that has been imported into OMERO but not necessarily the image ID. This is frequently the case when we want to annotate a recently imported image. This funciton will help to filter a list of image IDs to only those associated with a particular filename in ImportedImageFiles. Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. im_id : int ID of OMERO image. imported_filename : str The full filename (with extension) of the file whose OMERO image we are looking for. NOT the path of the image. Returns ------- answer : boolean Answer to the question of whether the given image has an associated ImportedImageFile of the given name. Notes ----- This function should be used as a filter on an image list that has been already narrowed down as much as possible. Note that many different images in OMERO may share the same filename (e.g., image.tif). Examples -------- >>> im_ids = get_image_ids(conn, dataset=303) >>> im_ids = [im_id for im_id in im_ids ... if image_has_imported_filename(conn, im_id, "feb_2020.tif")] """ q = conn.getQueryService() params = Parameters() params.map = {"imid": rlong(im_id)} results = q.projection( "SELECT o.name FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " JOIN u.originalFile o" " WHERE i.id=:imid", params, conn.SERVICE_OPTS) imp_filenames = [r[0].val for r in results] if imported_filename in imp_filenames: return True else: return False
def getAnnotationsForObjects(obj_type, oids): # Get the images that match hql = "select distinct link.child.id from %sAnnotationLink link " \ "where link.parent.id in (:oids)" % obj_type params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in oids]) qs = conn.getQueryService() return [result[0].val for result in qs.projection(hql,params, service_opts)]
def getObjectsWithAllAnnotations(obj_type, annids): # Get the images that match hql = "select link.parent.id from %sAnnotationLink link " \ "where link.child.id in (:oids) " \ "group by link.parent.id " \ "having count (distinct link.child) = %s" % (obj_type, len(annids)) params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in set(annids)]) qs = conn.getQueryService() return [x[0].getValue() for x in qs.projection(hql,params,service_opts)]
def user_creation_time(conn, user_id): """Return creation date of Experimenter.""" q = conn.getQueryService() params = Parameters() params.map = {"userid": rlong(user_id)} results = q.projection( "SELECT e.event.time" " FROM EventLog e" " WHERE e.action='INSERT' and" " e.entityType='ome.model.meta.Experimenter' and" " e.entityId=:userid", params, conn.SERVICE_OPTS) time = datetime.fromtimestamp(results[0][0].val / 1000) return time
def getAnnotationsForObjects(obj_type, oids): # Get the images that match hql = "select distinct link.child.id from %sAnnotationLink link " \ "where link.parent.id in (:oids)" % obj_type params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in oids]) qs = conn.getQueryService() return [ result[0].val for result in qs.projection(hql, params, service_opts) ]
def filter_by_filename(conn, im_ids, imported_filename): """Filter list of image ids by originalFile name Sometimes we know the filename of an image that has been imported into OMERO but not necessarily the image ID. This is frequently the case when we want to annotate a recently imported image. This funciton will help to filter a list of image IDs to only those associated with a particular filename. Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. im_ids : list of int List of OMERO image IDs. imported_filename : str The full filename (with extension) of the file whose OMERO image we are looking for. NOT the path of the image. Returns ------- filtered_im_ids : list of int Filtered list of images with originalFile name matching ``imported_filename``. Notes ----- This function should be used as a filter on an image list that has been already narrowed down as much as possible. Note that many different images in OMERO may share the same filename (e.g., image.tif). Examples -------- >>> im_ids = get_image_ids(conn, dataset=303) >>> im_ids = filter_by_filename(conn, im_ids, "feb_2020.tif")] """ q = conn.getQueryService() params = Parameters() params.map = {"oname": rstring(imported_filename)} results = q.projection( "SELECT i.id FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " JOIN u.originalFile o" " WHERE o.name=:oname", params, conn.SERVICE_OPTS) im_id_matches = [r[0].val for r in results] return list(set(im_ids) & set(im_id_matches))
def getObjectsWithAllAnnotations(obj_type, annids): # Get the images that match hql = "select link.parent.id from %sAnnotationLink link " \ "where link.child.id in (:oids) " \ "group by link.parent.id " \ "having count (distinct link.child) = %s" % (obj_type, len(annids)) params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in set(annids)]) qs = conn.getQueryService() return [ x[0].getValue() for x in qs.projection(hql, params, service_opts) ]
def originalfile_size_date(conn, originalfile_id): """Return size of OriginalFile in bytes, with date stamp. Note: OriginalFile doesn't necessarily have an associated size. Needs to be part of an image fileset """ q = conn.getQueryService() params = Parameters() params.map = {"fid": rlong(originalfile_id)} results = q.projection( "SELECT f.size, ce.time" " FROM OriginalFile f" " JOIN f.details.creationEvent ce" " WHERE f.id=:fid", params, conn.SERVICE_OPTS) return [(x[0].val, datetime.fromtimestamp(x[1].val / 1000)) for x in results]
def get_plate_ids(self): """Get the Ids of imported plates. Note that this will not find plates if they have not been imported. Also, while plate_ids are returned, this method also sets ``self.plate_ids``. Returns ------- plate_ids : list of ints Ids of plates imported from the specified client path, which itself is derived from ``self.file_path`` and ``self.filename``. """ if self.imported is not True: logging.error(f'File {self.file_path} has not been imported') return None else: print("time to get some IDs") q = self.conn.getQueryService() print(q) params = Parameters() path_query = str(self.file_path).strip('/') print(f"path query: f{path_query}") params.map = {"cpath": rstring(path_query)} print(params) results = q.projection( "SELECT DISTINCT p.id FROM Plate p" " JOIN p.plateAcquisitions pa" " JOIN pa.wellSample ws" " JOIN ws.image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " WHERE u.clientPath=:cpath", params, self.conn.SERVICE_OPTS) print(results) self.plate_ids = [r[0].val for r in results] return self.plate_ids
def get_image_ids(conn, dataset=None, well=None): """return a list of image ids based on project and dataset Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. dataset : int, optional ID of Dataset from which to return image IDs. well : int, optional ID of Well from which to return image IDs. Returns ------- im_ids : list of ints List of image IDs contained in the given Dataset, Well, or orphans. Notes ----- User and group information comes from the `conn` object. Be sure to use ``ezomero.set_group`` to specify group prior to passing the `conn` object to this function. If no Dataset or Well is specified, orphaned images are returned. Examples -------- Return orphaned images: >>> orphans = get_image_ids(conn) Return IDs of all images from Dataset with ID 448: >>> ds_ims = get_image_ids(conn, dataset=448) """ if (dataset is not None) & (well is not None): raise Exception('Dataset and Well can not both be specified') q = conn.getQueryService() params = Parameters() if dataset is not None: if not isinstance(dataset, int): raise TypeError('dataset must be integer') params.map = {"dataset": rlong(dataset)} results = q.projection( "SELECT i.id FROM Dataset d" " JOIN d.imageLinks dil" " JOIN dil.child i" " WHERE d.id=:dataset", params, conn.SERVICE_OPTS) elif well is not None: if not isinstance(well, int): raise TypeError('well must be integer') params.map = {"well": rlong(well)} results = q.projection( "SELECT i.id FROM Well w" " JOIN w.wellSamples ws" " JOIN ws.image i" " WHERE w.id=:well", params, conn.SERVICE_OPTS) elif (well is None) & (dataset is None): results = q.projection( "SELECT i.id FROM Image i" " WHERE NOT EXISTS (" " SELECT dil FROM DatasetImageLink dil" " WHERE dil.child=i.id" " )" " AND NOT EXISTS (" " SELECT ws from WellSample ws" " WHERE ws.image=i.id" " )", params, conn.SERVICE_OPTS) else: results = [] return [r[0].val for r in results]