def roiFigure(conn, commandArgs): """ This processes the script parameters, adding defaults if needed. Then calls a method to make the figure, and finally uploads and attaches this to the primary image. @param: session The OMERO session @param: commandArgs Map of String:Object parameters for the script. Objects are not rtypes, since getValue() was called when the map was processed below. But, list and map objects may contain rtypes (need to call getValue()) @return: the id of the originalFileLink child. (ID object, not value) """ log("ROI figure created by OMERO on %s" % date.today()) log("") message = "" # message to be returned to the client pixelIds = [] imageIds = [] imageLabels = [] # function for getting image labels. def getImageNames(fullName, tagsList, pdList): name = fullName.split("/")[-1] return [name] # default function for getting labels is getName (or use datasets / tags) if "Image_Labels" in commandArgs: if commandArgs["Image_Labels"] == "Datasets": def getDatasets(name, tagsList, pdList): return [dataset for project, dataset in pdList] getLabels = getDatasets elif commandArgs["Image_Labels"] == "Tags": def getTags(name, tagsList, pdList): return tagsList getLabels = getTags else: getLabels = getImageNames else: getLabels = getImageNames # Get the images images, logMessage = scriptUtil.getObjects(conn, commandArgs) message += logMessage if not images: return None, message # Check for rectangular ROIs and filter images list images = [image for image in images if image.getROICount("Rectangle") > 0] if not images: message += "No rectangle ROI found." return None, message # Attach figure to the first image omeroImage = images[0] # process the list of images. If imageIds is not set, script can't run. log("Image details:") for image in images: imageIds.append(image.getId()) pixelIds.append(image.getPrimaryPixels().getId()) # a map of imageId : list of (project, dataset) names. pdMap = figUtil.getDatasetsProjectsFromImages(conn.getQueryService(), imageIds) tagMap = figUtil.getTagsFromImages(conn.getMetadataService(), imageIds) # Build a legend entry for each image for image in images: name = image.getName() imageDate = image.getAcquisitionDate() iId = image.getId() tagsList = tagMap[iId] pdList = pdMap[iId] tags = ", ".join(tagsList) pdString = ", ".join(["%s/%s" % pd for pd in pdList]) log(" Image: %s ID: %d" % (name, iId)) if imageDate: log(" Date: %s" % imageDate) else: log(" Date: not set") log(" Tags: %s" % tags) log(" Project/Datasets: %s" % pdString) imageLabels.append(getLabels(name, tagsList, pdList)) # use the first image to define dimensions, channel colours etc. sizeX = omeroImage.getSizeX() sizeY = omeroImage.getSizeY() sizeZ = omeroImage.getSizeZ() sizeC = omeroImage.getSizeC() width = sizeX if "Width" in commandArgs: w = commandArgs["Width"] try: width = int(w) except: log("Invalid width: %s Using default value: %d" % (str(w), sizeX)) height = sizeY if "Height" in commandArgs: h = commandArgs["Height"] try: height = int(h) except: log("Invalid height: %s Using default value" % (str(h), sizeY)) log("Image dimensions for all panels (pixels): width: %d height: %d" % (width, height)) mergedIndexes = [] # the channels in the combined image, mergedColours = {} if "Merged_Colours" in commandArgs: cColourMap = commandArgs["Merged_Colours"] for c in cColourMap: rgb = cColourMap[c] try: rgb = int(rgb) cIndex = int(c) except ValueError: print "Merged_Colours map should be index:rgbInt. Not %s:%s" \ % (c, rgb) continue rgba = imgUtil.RGBIntToRGBA(rgb) mergedColours[cIndex] = rgba mergedIndexes.append(cIndex) mergedIndexes.sort() # make sure we have some merged channels if len(mergedIndexes) == 0: mergedIndexes = range(sizeC) mergedIndexes.reverse() mergedNames = False if "Merged_Names" in commandArgs: mergedNames = commandArgs["Merged_Names"] # Make channel-names map. If argument wasn't specified, name by index channelNames = {} if "Channel_Names" in commandArgs: cNameMap = commandArgs["Channel_Names"] for c in range(sizeC): if str(c) in cNameMap: channelNames[c] = cNameMap[str(c)] else: channelNames[c] = str(c) else: for c in range(sizeC): channelNames[c] = str(c) # Make split-indexes list. If no "Split_Indexes", show none: # http://www.openmicroscopy.org/community/viewtopic.php?f=4&t=940 splitIndexes = [] if "Split_Indexes" in commandArgs: for index in commandArgs["Split_Indexes"]: splitIndexes.append(index) colourChannels = True if "Split_Panels_Grey" in commandArgs and commandArgs["Split_Panels_Grey"]: colourChannels = False algorithm = ProjectionType.MAXIMUMINTENSITY if "Algorithm" in commandArgs: a = commandArgs["Algorithm"] if (a == "Mean Intensity"): algorithm = ProjectionType.MEANINTENSITY stepping = 1 if "Stepping" in commandArgs: s = commandArgs["Stepping"] if (0 < s < sizeZ): stepping = s scalebar = None if "Scalebar" in commandArgs: sb = commandArgs["Scalebar"] try: scalebar = int(sb) if scalebar <= 0: scalebar = None else: log("Scalebar is %d microns" % scalebar) except: log("Invalid value for scalebar: %s" % str(sb)) scalebar = None overlayColour = (255, 255, 255) if "Overlay_Colour" in commandArgs: r, g, b, a = OVERLAY_COLOURS[commandArgs["Overlay_Colour"]] overlayColour = (r, g, b) roiZoom = None if "ROI_Zoom" in commandArgs: roiZoom = float(commandArgs["ROI_Zoom"]) if roiZoom == 0: roiZoom = None roiLabel = "FigureROI" if "ROI_Label" in commandArgs: roiLabel = commandArgs["ROI_Label"] spacer = (width/50) + 2 fig = getSplitView( conn, imageIds, pixelIds, splitIndexes, channelNames, mergedNames, colourChannels, mergedIndexes, mergedColours, width, height, imageLabels, spacer, algorithm, stepping, scalebar, overlayColour, roiZoom, roiLabel) if fig is None: logMessage = "No figure produced" log("\n"+logMessage) message += logMessage return None, message # fig.show() # bug-fixing only log("") figLegend = "\n".join(logStrings) # print figLegend # bug fixing only format = commandArgs["Format"] figureName = "roiFigure" if "Figure_Name" in commandArgs: figureName = commandArgs["Figure_Name"] figureName = os.path.basename(figureName) output = "localfile" if format == 'PNG': output = output + ".png" figureName = figureName + ".png" fig.save(output, "PNG") mimetype = "image/png" elif format == 'TIFF': output = output + ".tiff" figureName = figureName + ".tiff" fig.save(output, "TIFF") mimetype = "image/tiff" else: output = output + ".jpg" figureName = figureName + ".jpg" fig.save(output) mimetype = "image/jpeg" # Use util method to upload the figure 'output' to the server, attaching # it to the omeroImage, adding the # figLegend as the fileAnnotation description. # Returns the id of the originalFileLink child. (ID object, not value) namespace = NSCREATED + "/omero/figure_scripts/ROI_Split_Figure" fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation( conn, output, omeroImage, output="ROI Split figure", mimetype=mimetype, ns=namespace, desc=figLegend, origFilePathAndName=figureName) message += faMessage return fileAnnotation, message
def splitViewFigure(conn, scriptParams): """ Processes the arguments, populating defaults if necessary. Prints the details to log (fig-legend). Even handles missing arguments that are not optional (from when this ran from commandline with everything optional) then calls makeSplitViewFigure() to make the figure, attaches it to the Image as an 'originalFile' annotation, with fig-legend as the description. @return: the id of the originalFileLink child. (ID object, not value) """ log("Split-View figure created by OMERO on %s" % date.today()) log("") message = "" # message to be returned to the client imageIds = [] pixelIds = [] imageLabels = [] # function for getting image labels. def getLabels(fullName, tagsList, pdList): name = fullName.split("/")[-1] return [name] # default function for getting labels is getName (or use datasets / tags) if scriptParams["Image_Labels"] == "Datasets": def getDatasets(name, tagsList, pdList): return [dataset for project, dataset in pdList] getLabels = getDatasets elif scriptParams["Image_Labels"] == "Tags": def getTags(name, tagsList, pdList): return tagsList getLabels = getTags # Get the images images, logMessage = scriptUtil.getObjects(conn, scriptParams) message += logMessage if not images: return None, message # Attach figure to the first image omeroImage = images[0] # process the list of images log("Image details:") for image in images: imageIds.append(image.getId()) pixelIds.append(image.getPrimaryPixels().getId()) pdMap = figUtil.getDatasetsProjectsFromImages( conn.getQueryService(), imageIds) # a map of imageId : list of (project, dataset) names. tagMap = figUtil.getTagsFromImages(conn.getMetadataService(), imageIds) # Build a legend entry for each image for image in images: name = image.getName() imageDate = image.getAcquisitionDate() iId = image.getId() tagsList = tagMap[iId] pdList = pdMap[iId] tags = ", ".join(tagsList) pdString = ", ".join(["%s/%s" % pd for pd in pdList]) log(" Image: %s ID: %d" % (name, iId)) log(" Date: %s" % date.fromtimestamp(imageDate / 1000)) log(" Tags: %s" % tags) log(" Project/Datasets: %s" % pdString) imageLabels.append(getLabels(name, tagsList, pdList)) # use the first image to define dimensions, channel colours etc. sizeX = omeroImage.getSizeX() sizeY = omeroImage.getSizeY() sizeZ = omeroImage.getSizeZ() sizeC = omeroImage.getSizeC() # set image dimensions zStart = -1 zEnd = -1 if "Z_Start" in scriptParams: zStart = scriptParams["Z_Start"] if "Z_End" in scriptParams: zEnd = scriptParams["Z_End"] width = "Width" in scriptParams and scriptParams["Width"] or sizeX height = "Height" in scriptParams and scriptParams["Height"] or sizeY log("Image dimensions for all panels (pixels): width: %d height: %d" % (width, height)) # Make split-indexes list. If argument wasn't specified, include them all. splitIndexes = [] if "Split_Indexes" in scriptParams: splitIndexes = scriptParams["Split_Indexes"] else: splitIndexes = range(sizeC) # Make channel-names map. If argument wasn't specified, name by index channelNames = {} for c in range(sizeC): channelNames[c] = str(c) if "Channel_Names" in scriptParams: cNameMap = scriptParams["Channel_Names"] for c in cNameMap: index = int(c) channelNames[index] = cNameMap[c] mergedIndexes = [] # the channels in the combined image, mergedColours = {} if "Merged_Colours" in scriptParams: cColourMap = scriptParams["Merged_Colours"] for c in cColourMap: rgb = cColourMap[c] rgba = imgUtil.RGBIntToRGBA(rgb) mergedColours[int(c)] = rgba mergedIndexes.append(int(c)) mergedIndexes.sort() else: mergedIndexes = range(sizeC) colourChannels = not scriptParams["Split_Panels_Grey"] algorithm = omero.constants.projection.ProjectionType.MAXIMUMINTENSITY if "Mean Intensity" == scriptParams["Algorithm"]: algorithm = omero.constants.projection.ProjectionType.MEANINTENSITY stepping = min(scriptParams["Stepping"], sizeZ) scalebar = None if "Scalebar" in scriptParams: scalebar = scriptParams["Scalebar"] log("Scalebar is %d microns" % scalebar) r, g, b, a = OVERLAY_COLOURS[scriptParams["Overlay_Colour"]] overlayColour = (r, g, b) mergedNames = scriptParams["Merged_Names"] print "splitIndexes", splitIndexes print "channelNames", channelNames print "colourChannels", colourChannels print "mergedIndexes", mergedIndexes print "mergedColours", mergedColours print "mergedNames", mergedNames fig = makeSplitViewFigure(conn, pixelIds, zStart, zEnd, splitIndexes, channelNames, colourChannels, mergedIndexes, mergedColours, mergedNames, width, height, imageLabels, algorithm, stepping, scalebar, overlayColour) figLegend = "\n".join(logStrings) format = JPEG if scriptParams["Format"] == "PNG": format = PNG output = scriptParams["Figure_Name"] if format == PNG: output = output + ".png" fig.save(output, "PNG") mimetype = "image/png" else: output = output + ".jpg" fig.save(output) mimetype = "image/jpeg" # Upload the figure 'output' to the server, creating a file annotation and attaching it to the omeroImage, adding the # figLegend as the fileAnnotation description. namespace = omero.constants.namespaces.NSCREATED + "/omero/figure_scripts/Split_View_Figure" fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation( conn, output, omeroImage, output="Split view figure", mimetype=mimetype, ns=namespace, desc=figLegend) message += faMessage return fileAnnotation, message