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 link_tags(conn, datasetname, image_tag_links, image_ratings): for i in range(1, 51): username = "******" % i print(username) exp = conn.getAdminService().lookupExperimenter(username) exp_id = exp.id.val dataset = conn.getObject("Dataset", attributes={'name': datasetname}, opts={'owner': exp_id}) if dataset is None: print("Dataset not found") continue links = [] for image in dataset.listChildren(): name = image.name if name in image_tag_links: for tag_id in image_tag_links[name]: link = ImageAnnotationLinkI() link.parent = ImageI(image.id, False) link.child = TagAnnotationI(tag_id, False) link.details.owner = ExperimenterI(exp_id, False) links.append(link) if name in image_ratings: link = ImageAnnotationLinkI() link.parent = ImageI(image.id, False) r = LongAnnotationI() r.ns = rstring(RATING_NS) r.longValue = rlong(image_ratings[name]) link.child = r link.details.owner = ExperimenterI(exp_id, False) links.append(link) print('links', len(links)) group_id = dataset.getDetails().getGroup().id conn.SERVICE_OPTS.setOmeroGroup(group_id) try: conn.getUpdateService().saveArray(links, conn.SERVICE_OPTS) except ValidationException: print("Failed to link for %s" % username)
def process_images(conn, script_params): file_anns = [] message = "" # Get the images images, log_message = script_utils.get_objects(conn, script_params) message += log_message if not images: return None, message # Check for line and polyline ROIs and filter images list images = [ image for image in images if image.getROICount(["Polyline", "Line"]) > 0 ] if not images: message += "No ROI containing line or polyline was found." return None, message csv_data = [] for image in images: if image.getSizeT() > 1: message += "%s ID: %s appears to be a time-lapse Image," \ " not a kymograph." % (image.getName(), image.getId()) continue roi_service = conn.getRoiService() result = roi_service.findByImage(image.getId(), None) secs_per_pixel_y = image.getPixelSizeY() microns_per_pixel_x = image.getPixelSizeX() if secs_per_pixel_y and microns_per_pixel_x: microns_per_sec = microns_per_pixel_x / secs_per_pixel_y else: microns_per_sec = None # for each line or polyline, create a row in csv table: y(t), x, # dy(dt), dx, x/t (line), x/t (average) col_names = "\nt_start (pixels), x_start (pixels), t_end (pixels)," \ " x_end (pixels), dt (pixels), dx (pixels), x/t, speed(um/sec)," \ "avg x/t, avg speed(um/sec)" table_data = "" for roi in result.rois: for s in roi.copyShapes(): if s is None: continue # seems possible in some situations if type(s) == omero.model.LineI: table_data += "\nLine ID: %s" % s.getId().getValue() x1 = s.getX1().getValue() x2 = s.getX2().getValue() y1 = s.getY1().getValue() y2 = s.getY2().getValue() dx = abs(x1 - x2) dy = abs(y1 - y2) dx_per_y = float(dx) / dy speed = "" if microns_per_sec: speed = dx_per_y * microns_per_sec table_data += "\n" table_data += ",".join([ str(x) for x in (y1, x1, y2, x2, dy, dx, dx_per_y, speed) ]) elif type(s) == omero.model.PolylineI: table_data += "\nPolyline ID: %s" % s.getId().getValue() v = s.getPoints().getValue() points = roi_utils.points_string_to_xy_list(v) x_start, y_start = points[0] for i in range(1, len(points)): x1, y1 = points[i - 1] x2, y2 = points[i] dx = abs(x1 - x2) dy = abs(y1 - y2) dx_per_y = float(dx) / dy av_x_per_y = abs(float(x2 - x_start) / (y2 - y_start)) speed = "" avg_speed = "" if microns_per_sec: speed = dx_per_y * microns_per_sec avg_speed = av_x_per_y * microns_per_sec table_data += "\n" table_data += ",".join([ str(x) for x in (y1, x1, y2, x2, dy, dx, dx_per_y, speed, av_x_per_y, avg_speed) ]) # write table data to csv... if len(table_data) > 0: table_string = "Image ID:, %s," % image.getId() table_string += "Name:, %s" % image.getName() table_string += "\nsecsPerPixelY: %s" % secs_per_pixel_y table_string += '\nmicronsPerPixelX: %s' % microns_per_pixel_x table_string += "\n" table_string += col_names table_string += table_data csv_data.append(table_string) iids = [str(i.getId()) for i in images] to_link_csv = [i.getId() for i in images if i.canAnnotate()] csv_file_name = 'kymograph_velocities_%s.csv' % "-".join(iids) with open(csv_file_name, 'w') as csv_file: csv_file.write("\n \n".join(csv_data)) file_ann = conn.createFileAnnfromLocalFile(csv_file_name, mimetype="text/csv") fa_message = "Created Line Plot csv (Excel) file" links = [] if len(to_link_csv) == 0: fa_message += " but could not attach to images." for iid in to_link_csv: link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = file_ann._obj links.append(link) if len(links) > 0: links = conn.getUpdateService().saveAndReturnArray(links) if file_ann: file_anns.append(file_ann) if not file_anns: fa_message = "No Analysis files created. See 'Info' or 'Error'" \ " for more details" elif len(file_anns) > 1: fa_message = "Created %s csv (Excel) files" % len(file_anns) message += fa_message return file_anns, message
def buildFigure(self): """ The main building of the figure happens here, independently of format. We set up directories as needed, call createFigure() to create the PDF or TIFF then iterate through figure pages, adding panels for each page. Then we add an info page and create a zip of everything if needed. Finally the created file or zip is uploaded to OMERO and attached as a file annotation to all the images in the figure. """ # test to see if we've got multiple pages page_count = 'page_count' in self.figure_json and self.figure_json[ 'page_count'] or 1 self.page_count = int(page_count) paper_spacing = 'paper_spacing' in self.figure_json and self.figure_json[ 'paper_spacing'] or 50 page_col_count = 'page_col_count' in self.figure_json and self.figure_json[ 'page_col_count'] or 1 # Create a zip if we have multiple TIFF pages or we're exporting Images export_option = self.scriptParams['Export_Option'] createZip = False if self.exportImages: createZip = True if (self.page_count > 1) and (export_option.startswith("TIFF")): createZip = True # somewhere to put PDF and images self.zip_folder_name = None if createZip: self.zip_folder_name = "figure" curr_dir = os.getcwd() zipDir = os.path.join(curr_dir, self.zip_folder_name) os.mkdir(zipDir) if self.exportImages: for d in (ORIGINAL_DIR, RESAMPLED_DIR, FINAL_DIR): imgDir = os.path.join(zipDir, d) os.mkdir(imgDir) self.addReadMeFile() # Create the figure file(s) self.createFigure() panels_json = self.figure_json['panels'] imageIds = set() groupId = None # We get our group from the first image id1 = panels_json[0]['imageId'] groupId = self.conn.getObject("Image", id1).getDetails().group.id.val # For each page, add panels... col = 0 row = 0 for p in range(self.page_count): print "\n------------------------- PAGE ", p + 1, "--------------------------" px = col * (self.pageWidth + paper_spacing) py = row * (self.pageHeight + paper_spacing) page = {'x': px, 'y': py} # if export_option == "TIFF": # add_panels_to_tiff(conn, tiffFigure, panels_json, imageIds, page) # elif export_option == "PDF": self.add_panels_to_page(panels_json, imageIds, page) # complete page and save self.savePage() col = col + 1 if col >= page_col_count: col = 0 row = row + 1 # Add thumbnails and links page self.addInfoPage(panels_json) # Saves the completed figure file self.saveFigure() # PDF will get created in this group if groupId is None: groupId = self.conn.getEventContext().groupId self.conn.SERVICE_OPTS.setOmeroGroup(groupId) outputFile = self.figureFileName ns = self.ns mimetype = self.mimetype if self.zip_folder_name is not None: zipName = self.getZipName() # Recursively zip everything up compress(zipName, self.zip_folder_name) outputFile = zipName ns = "omero.web.figure.zip" mimetype = "application/zip" fileAnn = self.conn.createFileAnnfromLocalFile(outputFile, mimetype=mimetype, ns=ns) links = [] for iid in list(imageIds): print "linking to", iid link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = fileAnn._obj links.append(link) if len(links) > 0: # Don't want to fail at this point due to strange permissions combo try: links = self.conn.getUpdateService().saveAndReturnArray( links, self.conn.SERVICE_OPTS) except: print "Failed to attach figure: %s to images %s" % (fileAnn, imageIds) return fileAnn
def processImages(conn, scriptParams): fileAnns = [] message = "" # Get the images images, logMessage = scriptUtil.getObjects(conn, scriptParams) message += logMessage if not images: return None, message # Check for line and polyline ROIs and filter images list images = [image for image in images if image.getROICount(["Polyline", "Line"]) > 0] if not images: message += "No ROI containing line or polyline was found." return None, message csvData = [] for image in images: print "\nAnalysing Image: %s ID: %s" \ % (image.getName(), image.getId()) if image.getSizeT() > 1: message += "%s ID: %s appears to be a time-lapse Image," \ " not a kymograph." % (image.getName(), image.getId()) continue roiService = conn.getRoiService() result = roiService.findByImage(image.getId(), None) secsPerPixelY = image.getPixelSizeY() micronsPerPixelX = image.getPixelSizeX() if secsPerPixelY and micronsPerPixelX: micronsPerSec = micronsPerPixelX / secsPerPixelY else: micronsPerSec = None # for each line or polyline, create a row in csv table: y(t), x, # dy(dt), dx, x/t (line), x/t (average) colNames = "\nt_start (pixels), x_start (pixels), t_end (pixels)," \ " x_end (pixels), dt (pixels), dx (pixels), x/t, speed(um/sec)," \ "avg x/t, avg speed(um/sec)" tableData = "" for roi in result.rois: for s in roi.copyShapes(): if s is None: continue # seems possible in some situations if type(s) == omero.model.LineI: tableData += "\nLine ID: %s" % s.getId().getValue() x1 = s.getX1().getValue() x2 = s.getX2().getValue() y1 = s.getY1().getValue() y2 = s.getY2().getValue() dx = abs(x1-x2) dy = abs(y1-y2) dxPerY = float(dx)/dy speed = "" if micronsPerSec: speed = dxPerY * micronsPerSec tableData += "\n" tableData += ",".join( [str(x) for x in (y1, x1, y2, x2, dy, dx, dxPerY, speed)]) elif type(s) == omero.model.PolylineI: tableData += "\nPolyline ID: %s" % s.getId().getValue() points = pointsStringToXYlist(s.getPoints().getValue()) xStart, yStart = points[0] for i in range(1, len(points)): x1, y1 = points[i-1] x2, y2 = points[i] dx = abs(x1-x2) dy = abs(y1-y2) dxPerY = float(dx)/dy avXperY = abs(float(x2-xStart)/(y2-yStart)) speed = "" avgSpeed = "" if micronsPerSec: speed = dxPerY * micronsPerSec avgSpeed = avXperY * micronsPerSec tableData += "\n" tableData += ",".join( [str(x) for x in (y1, x1, y2, x2, dy, dx, dxPerY, speed, avXperY, avgSpeed)]) # write table data to csv... if len(tableData) > 0: tableString = "Image ID:, %s," % image.getId() tableString += "Name:, %s" % image.getName() tableString += "\nsecsPerPixelY: %s" % secsPerPixelY tableString += '\nmicronsPerPixelX: %s' % micronsPerPixelX tableString += "\n" tableString += colNames tableString += tableData print tableString csvData.append(tableString) else: print "Found NO lines or polylines to analyze for Image" iids = [str(i.getId()) for i in images] toLinkCsv = [i.getId() for i in images if i.canAnnotate()] csvFileName = 'kymograph_velocities_%s.csv' % "-".join(iids) csvFile = open(csvFileName, 'w') try: csvFile.write("\n \n".join(csvData)) finally: csvFile.close() fileAnn = conn.createFileAnnfromLocalFile(csvFileName, mimetype="text/csv") faMessage = "Created Line Plot csv (Excel) file" links = [] if len(toLinkCsv) == 0: faMessage += " but could not attach to images." for iid in toLinkCsv: print "linking csv to Image: ", iid link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = fileAnn._obj links.append(link) if len(links) > 0: links = conn.getUpdateService().saveAndReturnArray(links) if fileAnn: fileAnns.append(fileAnn) if not fileAnns: faMessage = "No Analysis files created. See 'Info' or 'Error'" \ " for more details" elif len(fileAnns) > 1: faMessage = "Created %s csv (Excel) files" % len(fileAnns) message += faMessage return fileAnns, message
def testQueryTaggedUnique(self): # get group we're working on... ctx = self.client.sf.getAdminService().getEventContext() groupId = ctx.groupId print 'groupId', groupId # Admin sets permissions to read-ann admin = self.root.sf.getAdminService() rootUpdate = self.root.sf.getUpdateService() gr = admin.getGroup(groupId) p = PermissionsI() p.setUserRead(True) p.setUserWrite(True) p.setGroupRead(True) p.setGroupAnnotate(True) p.setGroupWrite(False) p.setWorldRead(False) p.setWorldAnnotate(False) p.setWorldWrite(False) gr.details.permissions = p admin.updateGroup(gr) # Update context for user ctx = self.client.sf.getAdminService().getEventContext() update = self.client.sf.getUpdateService() queryService = self.client.sf.getQueryService() tagCount = 5 # User creates tag linked to images tag = TagAnnotationI() tag.textValue = wrap("test_iQuerySpeed") links = [] for i in range(tagCount): iid = createImageWithPixels(self.client, self.uuid()) link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = tag links.append(link) links = update.saveAndReturnArray(links) tag = links[0].child # check permissions p = tag.getDetails().getPermissions() assert p.isGroupRead() assert p.isGroupAnnotate() # Root also links user's tag to images rootLinks = [] for l in links: link = ImageAnnotationLinkI() link.parent = ImageI(l.parent.id, False) link.child = TagAnnotationI(l.child.id, False) rootLinks.append(link) rootUpdate.saveAndReturnArray(rootLinks, {'omero.group': str(groupId)}) q = """select distinct new map(obj.id as id, obj.name as name, obj.details.owner.id as ownerId, obj as image_details_permissions, obj.fileset.id as filesetId, lower(obj.name) as n , pix.sizeX as sizeX, pix.sizeY as sizeY, pix.sizeZ as sizeZ ) from Image obj left outer join obj.pixels pix join obj.annotationLinks alink where %s order by lower(obj.name), obj.id """ params = ParametersI() params.add('tid', tag.id) # We can get all the tagged images like this. # We use an additional select statement to give 'unique' results uniqueClause = """alink.id = (select max(alink.id) from ImageAnnotationLink alink where alink.child.id=:tid and alink.parent.id=obj.id)""" query = q % uniqueClause result1 = queryService.projection(query, params, {'omero.group': str(groupId)}) assert len(result1) == tagCount # Without the select statement, we get the same image returned # multiple times if there is no 'distinct' clause = "alink.child.id=:tid" query = q % clause result2 = queryService.projection(query, params, {'omero.group': str(groupId)}) assert len(result2) == tagCount for idx in range(len(result1)-1): # Omit final since == isn't defined for Ice objects. assert result1[idx] == result2[idx]
def create_pdf(conn, scriptParams): # make sure we can find all images conn.SERVICE_OPTS.setOmeroGroup(-1) figure_json_string = scriptParams['Figure_JSON'] figure_json = json.loads(figure_json_string) n = datetime.now() # time-stamp name by default: Figure_2013-10-29_22-43-53.pdf figureName = "Figure_%s-%s-%s_%s-%s-%s." \ "pdf" % (n.year, n.month, n.day, n.hour, n.minute, n.second) if 'figureName' in figure_json: figureName = str(figure_json['figureName']) # get Figure width & height... pageWidth = figure_json['paper_width'] pageHeight = figure_json['paper_height'] # add to scriptParams for convenience scriptParams['Page_Width'] = pageWidth scriptParams['Page_Height'] = pageHeight pdfName = figureName if not pdfName.endswith('.pdf'): pdfName = "%s.pdf" % pdfName c = canvas.Canvas(pdfName, pagesize=(pageWidth, pageHeight)) panels_json = figure_json['panels'] imageIds = set() groupId = None for i, panel in enumerate(panels_json): print "\n---------------- " imageId = panel['imageId'] print "IMAGE", i, imageId image = drawPanel(conn, c, panel, pageHeight, i) if image.canAnnotate(): imageIds.add(imageId) drawLabels(conn, c, panel, pageHeight) # We get our group from the first image if groupId is None: groupId = image.getDetails().group.id.val # complete page and save c.showPage() print panels_json # Add thumbnails and links page addInfoPage(conn, scriptParams, c, panels_json, figureName) c.save() # PDF will get created in this group if groupId is None: groupId = conn.getEventContext().groupId conn.SERVICE_OPTS.setOmeroGroup(groupId) ns = "omero.web.figure.pdf" fileAnn = conn.createFileAnnfromLocalFile( pdfName, mimetype="application/pdf", ns=ns) links = [] for iid in list(imageIds): print "linking to", iid link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = fileAnn._obj links.append(link) if len(links) > 0: # Don't want to fail at this point due to strange permissions combo try: links = conn.getUpdateService().saveAndReturnArray( links, conn.SERVICE_OPTS) except: print "Failed to attach figure: %s to images %s" % (fileAnn, imageIds) return fileAnn
def buildFigure(self): """ The main building of the figure happens here, independently of format. We set up directories as needed, call createFigure() to create the PDF or TIFF then iterate through figure pages, adding panels for each page. Then we add an info page and create a zip of everything if needed. Finally the created file or zip is uploaded to OMERO and attached as a file annotation to all the images in the figure. """ # test to see if we've got multiple pages page_count = 'page_count' in self.figure_json and self.figure_json['page_count'] or 1 self.page_count = int(page_count) paper_spacing = 'paper_spacing' in self.figure_json and self.figure_json['paper_spacing'] or 50 page_col_count = 'page_col_count' in self.figure_json and self.figure_json['page_col_count'] or 1 # Create a zip if we have multiple TIFF pages or we're exporting Images export_option = self.scriptParams['Export_Option'] createZip = False if self.exportImages: createZip = True if (self.page_count > 1) and (export_option.startswith("TIFF")): createZip = True # somewhere to put PDF and images self.zip_folder_name = None if createZip: self.zip_folder_name = "figure" curr_dir = os.getcwd() zipDir = os.path.join(curr_dir, self.zip_folder_name) os.mkdir(zipDir) if self.exportImages: for d in (ORIGINAL_DIR, RESAMPLED_DIR, FINAL_DIR): imgDir = os.path.join(zipDir, d) os.mkdir(imgDir) self.addReadMeFile() # Create the figure file(s) self.createFigure() panels_json = self.figure_json['panels'] imageIds = set() groupId = None # We get our group from the first image id1 = panels_json[0]['imageId'] groupId = self.conn.getObject("Image", id1).getDetails().group.id.val # For each page, add panels... col = 0 row = 0 for p in range(self.page_count): print "\n------------------------- PAGE ", p + 1, "--------------------------" px = col * (self.pageWidth + paper_spacing) py = row * (self.pageHeight + paper_spacing) page = {'x': px, 'y': py} # if export_option == "TIFF": # add_panels_to_tiff(conn, tiffFigure, panels_json, imageIds, page) # elif export_option == "PDF": self.add_panels_to_page(panels_json, imageIds, page) # complete page and save self.savePage() col = col + 1 if col >= page_col_count: col = 0 row = row + 1 # Add thumbnails and links page self.addInfoPage(panels_json) # Saves the completed figure file self.saveFigure() # PDF will get created in this group if groupId is None: groupId = self.conn.getEventContext().groupId self.conn.SERVICE_OPTS.setOmeroGroup(groupId) outputFile = self.figureFileName ns = self.ns mimetype = self.mimetype if self.zip_folder_name is not None: zipName = self.getZipName() # Recursively zip everything up compress(zipName, self.zip_folder_name) outputFile = zipName ns = "omero.web.figure.zip" mimetype = "application/zip" fileAnn = self.conn.createFileAnnfromLocalFile( outputFile, mimetype=mimetype, ns=ns) links = [] for iid in list(imageIds): print "linking to", iid link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = fileAnn._obj links.append(link) if len(links) > 0: # Don't want to fail at this point due to strange permissions combo try: links = self.conn.getUpdateService().saveAndReturnArray( links, self.conn.SERVICE_OPTS) except: print "Failed to attach figure: %s to images %s" % (fileAnn, imageIds) return fileAnn
def create_pdf(conn, scriptParams): figure_json_string = scriptParams['Figure_JSON'] figure_json = json.loads(figure_json_string) n = datetime.now() # time-stamp name by default: Figure_2013-10-29_22-43-53.pdf (tried : but they get replaced) figureName = "Figure_%s-%s-%s_%s-%s-%s.pdf" % (n.year, n.month, n.day, n.hour, n.minute, n.second) # get Figure width & height... pageWidth = figure_json['paper_width'] pageHeight = figure_json['paper_height'] # add to scriptParams for convenience scriptParams['Page_Width'] = pageWidth scriptParams['Page_Height'] = pageHeight if 'Figure_Name' in scriptParams: figureName = scriptParams['Figure_Name'] if not figureName.endswith('.pdf'): figureName = "%s.pdf" % figureName c = canvas.Canvas(figureName, pagesize=(pageWidth, pageHeight)) panels_json = figure_json['panels'] imageIds = set() for i, panel in enumerate(panels_json): print "\n---------------- " imageId = panel['imageId'] print "IMAGE", i, imageId imageIds.add(imageId) drawPanel(conn, c, panel, pageHeight, i) drawLabels(conn, c, panel, pageHeight) # complete page and save c.showPage() if True: addInfoPage(conn, scriptParams, c, panels_json) c.save() ns = "omero.web.figure.pdf" fileAnn = conn.createFileAnnfromLocalFile( figureName, mimetype="application/pdf", ns=ns, desc=figure_json_string) links = [] for iid in list(imageIds): print "linking to", iid link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = fileAnn._obj links.append(link) print len(links) if len(links) > 0: links = conn.getUpdateService().saveAndReturnArray(links, conn.SERVICE_OPTS) return fileAnn