Beispiel #1
0
def set_rendersettings(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the object IDs
    objs, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    # Return if all input screens are not found or excluded
    if not objs:
        return message

    # Filter object IDs by permissions (future)


#    IDs = [ob.getId() for ob in objs if ob.canLink()]
#    if len(IDs) != len(objs):
#        permIDs = [str(ob.getId()) for ob in objs if not ob.canLink()]
#        message += "You do not have permission to change rendering settings on images in"\
#            " screen(s): %s." % ",".join(permIDs)
#    if not IDs:
#        return None, message

    for ob in objs:
        set_rendersettings_screen(conn, scriptParams, ob)

    return message
def processImages(client, scriptParams):
    message = ''

    # for params with default values, we can get the value directly
    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']

    # Get the datasets
    conn = BlitzGateway(client_obj = client)
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    if not objects:
        return message

    datasets = conn.getObjects(dataType, ids)

    good, chNames, msg = checkChannels(datasets)
    message += msg
    if not good:
        raise omero.ServerError(
            'Channel check failed, ' +
            'all images must have the same channels: %s' % message)

    return message
def processImages(client, scriptParams):
    message = ''

    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']
    ftset = scriptParams['Feature_set']

    try:
        conn = omero.gateway.BlitzGateway(client_obj=client)

        # Get the objects
        objects, logMessage = script_utils.getObjects(conn, scriptParams)
        message += logMessage

        if not objects:
            return message

        if dataType not in supportedDataTypes:
            m = 'Invalid datatype: %s\n' % datatype
            sys.stderr.write(m)
            return message + m

        images = imageGenerator(objects)
        for image in images:
            message += 'Processing image id:%d\n' % image.getId()
            m = deleteImageFeatures(conn, client, image, ftset, field=True)
            message += m

    except:
        print message
        raise

    return message
def processImages(client, scriptParams):
    message = ''

    # for params with default values, we can get the value directly
    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']
    contextName = scriptParams['Context_Name']

    tableName = '/Wndcharm/' + contextName + '/SmallFeatureSet.h5'
    message += 'tableName:' + tableName + '\n'
    ftb = WndcharmStorage.FeatureTable(client, tableName)

    try:
        # Get the datasets
        objects, logMessage = script_utils.getObjects(ftb.conn, scriptParams)
        message += logMessage

        if not objects:
            return message

        datasets = WndcharmStorage.datasetGenerator(ftb.conn, dataType, ids)
        for ds in datasets:
            message += 'Processing dataset id:%d\n' % ds.getId()
            msg = countCompleted(ftb, ds)
            message += msg

    except:
        print message
        raise
    finally:
        ftb.close()

    return message
def processImages(client, conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, None, message

    # Concatenate images from datasets
    if scriptParams["Data_Type"] == "Image":
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    pixelsService = conn.getPixelsService()
    renderingService = conn.getRenderingSettingsService()

    # Get the channel offsets
    for image in images:
         metadata = dict(image.loadOriginalMetadata()[1])
         pi = image.getPrimaryPixels()
         pixelsService.setChannelGlobalMinMax(pi.getId(), 0, metadata['Min'], metadata['Max'], conn.SERVICE_OPTS)
         renderingService.setOriginalSettingsInImage(image.getId())      

    return message
def saveImages(conn, scriptParams):
    dataType = scriptParams['Data_Type']
    formatType = scriptParams['Format']    
    dirpath='tmp' #Use Root tmp dir so gets cleaned up
    unique_folder = 'Exportfiles' + str(time.time()) #random
    imagepath = path.join(dirpath, unique_folder)
    imagepath = path.sep + imagepath + path.sep
    print "Imagepath=", imagepath
    # Get the images or datasets
    message = ""
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    parent = objects[0] #??
    if not objects:
        return None, message
    
    if dataType == 'Dataset':
        images = []
        for ds in objects:
            images.extend(list(ds.listChildren()))
        if not images:
            message += "No image found in dataset(s)"
            return None, message
    else:
        images = objects

    imageIds = [i.getId() for i in images]
    print "Selected %d images for processing" % len(imageIds)
    #Create download folder
    if len(imageIds) > 0:
        mkdir(imagepath)
        #chdir(imagepath)
    imagefilenames =[]
    for iId in imageIds:
        img = conn.getObject("Image", iId)
        
        if img is not None:
            z = img.getSizeZ() / 2
            t = 0 
            saveimage = img.renderImage(z,t) # returns PIL Image jpeg
            imageFilename = getImageName(img, formatType.lower(), imagepath)
            imagefilenames.append(imageFilename)            
            message += saveAs(saveimage,imageFilename,formatType)
        
    #zip output files
    zipfilename = path.join(dirpath, unique_folder)
    zipfilename = path.sep + zipfilename + ".zip"
    message += createZipFile(zipfilename, imagefilenames)

    mimetype = 'application/zip'
    outputDisplayName = "Image export zip"
    namespace = NSCREATED + "/QBI/Utils/ExportImage"
    
    fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(
        conn, zipfilename, parent, output=outputDisplayName, ns=namespace,
        mimetype=mimetype)
    message += annMessage
    return fileAnnotation, message         
def features(conn, scriptParams, uuid):
    pass

    cli = omero.cli.CLI()
    cli.loadplugins()

    feature_type = scriptParams["feature_type"]
    CP_pipe_params = scriptParams["CP_params"]

    images, logMessage = script_utils.getObjects(conn, scriptParams)
    if not images:
        return None, None, logMessage

    imageIds = [i.getId() for i in images]

    for iId in imageIds:
        image = conn.getObject("Image", iId)
        dataset = image.getParent().getId()
        image_dir = os.path.join(OMERO_FS, image.getFileset().listFiles()[0].getPath()) # Given a single file per fileset
        image_name = image.getFileset().listFiles()[0].getName()
        image_path = os.path.join(image_dir, image_name)
        
        # CellProfiler context manager

        # CP_PATH='/project/projectdirs/ngbi/resources/CP2'
        # my_image_directory='/project/projectdirs/ngbi/jobs/CP_pipes/test/test_in'
        # my_output_directory='/project/projectdirs/ngbi/jobs/CP_pipes/test/test_out'
        # # my_pipe='/project/projectdirs/ngbi/jobs/CP_pipes/pipeline.h5'
        # my_pipe='/project/projectdirs/ngbi/jobs/CP_pipes/pipeline.cppipe'
        CP_args = (CP_PATH, image_path, obj_mask_path, CP_pipe)
        CP_process_str = "python %s/CellProfiler.py -c -r -i %s -o obj_mask_path -p %s" % CP_args # using sgnworker
        
        new_dId, new_dObj = dataset_gen(conn, "datasetname_name")

        # read obj_mask_path and split into objects
        for image_object in object_list:
#           crop image
            cmd = "import -s sgn02 -d %s -n %s -k %s %s" % (new_dId, image_object.getName(), key, image)
            u=cmd.split()
            cli.invoke(u,strict=True)
            new_iId, new_iObj = image_gen(conn, image_object.getName())
            link2project(conn, new_dObj, new_iObj)
    
    #           save image and assotiate with iId
    #           save object as image using omeroFS
    #           create annotation link etc
    #           calculate feature of object
    #           picke feature and store
                

        new_pId, new_pObj = project_gen(conn, image_object.getName())
        
        link2project(conn, new_pObj, new_dObj)
        addTag(conn, new_dId, tags, chans)
def processImages(client, scriptParams):
    message = ''

    # for params with default values, we can get the value directly
    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']
    contextName = scriptParams['Context_Name']
    newOnly = scriptParams['New_Images_Only']

    tableName = '/Wndcharm/' + contextName + '/SmallFeatureSet.h5'
    message += 'tableName:' + tableName + '\n'
    ftb = WndcharmStorage.FeatureTable(client, tableName)

    try:
        nimages = 0

        # Get the datasets
        objects, logMessage = script_utils.getObjects(ftb.conn, scriptParams)
        message += logMessage

        if not objects:
            return message

        datasets = list(WndcharmStorage.datasetGenerator(
                ftb.conn, dataType, ids))

        good, chNames, msg = checkChannels(datasets)
        message += msg
        if not good:
            raise omero.ServerError(
                'Channel check failed, ' +
                'all images must have the same channels: %s' % message)

        for d in datasets:
            message += 'Processing dataset id:%d\n' % d.getId()
            for image in d.listChildren():
                message += 'Processing image id:%d\n' % image.getId()
                msg = extractFeatures(ftb, d, newOnly, chNames, im=image)
                message += msg + '\n'

    except:
        print message
        raise
    finally:
        ftb.close()

    return message
def processImages(client, conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, None, message

    # Concatenate images from datasets
    if scriptParams["Data_Type"] == "Image":
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    pixelsService = conn.getPixelsService()
    renderingService = conn.getRenderingSettingsService()

    # Get the channel offsets
    for image in images:
        min, max = 0, 65535
        for ann in image.listAnnotations():
            if isinstance(ann, omero.gateway.MapAnnotationWrapper):
                kvs = ann.getValue()
                for k, v in kvs:
                    if k == "MinSampleValue":
                        min = v
                    elif k == "MaxSampleValue":
                        max = v

        pi = image.getPrimaryPixels()
        pixelsService.setChannelGlobalMinMax(pi.getId(), 0, int(min), int(max),
                                             conn.SERVICE_OPTS)
        renderingService.setOriginalSettingsInImage(image.getId())

    return message
def processImages(client, scriptParams):
    message = ''

    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']
    ftset = scriptParams['Feature_set']

    try:
        conn = omero.gateway.BlitzGateway(client_obj=client)

        # Get the objects
        objects, logMessage = script_utils.getObjects(conn, scriptParams)
        message += logMessage

        if not objects:
            return message

        if dataType == 'Image':
            for image in objects:
                message += 'Processing image id:%d\n' % image.getId()
                m = deleteImageFeatures(conn, client, image, ftset, field=True)
                message += m

        else:
            if dataType == 'Project':
                datasets = [proj.listChildren() for proj in objects]
                datasets = itertools.chain.from_iterable(datasets)
            else:
                datasets = objects

            for d in datasets:
                message += 'Processing dataset id:%d\n' % d.getId()
                for image in d.listChildren():
                    message += 'Processing image id:%d\n' % image.getId()
                    m = deleteImageFeatures(
                        conn, client, image, ftset, field=True)
                    message += m

    except:
        print message
        raise

    return message
Beispiel #11
0
def set_rendersettings(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the object IDs
    objs, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    # Return if all input containers are not found or excluded
    if not objs:
        return message

    # Filter object IDs by permissions (future)
#    IDs = [ob.getId() for ob in objs if ob.canLink()]
#    if len(IDs) != len(objs):
#        permIDs = [str(ob.getId()) for ob in objs if not ob.canLink()]
#        message += "You do not have permission to change rendering settings on images in"\
#            " screen(s): %s." % ",".join(permIDs)
#    if not IDs:
#        return None, message

    for ob in objs:
        dataType = scriptParams["Data_Type"]
        if (dataType == "Screen"):
            print "Setting rendering settings for Screen: %d %s" \
                % (ob.getId(), ob.getName())

            # sort plates by name (and possible filter by name in future)
            plates = list(ob.listChildren())

            for plate in plates:
                set_rendersettings_plate(conn, scriptParams, plate)

        elif (dataType == "Plate"):
            set_rendersettings_plate(conn, scriptParams, ob)
        else:
    	    message += "Invalid Data_Type"

    return message
Beispiel #12
0
def processImages(client, conn, scriptParams):
    """
    Process the script params t, then iterate
    through the images
    """

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, None, message

    # Concatenate images from datasets
    if scriptParams["Data_Type"] == "Image":
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    updateService = conn.getUpdateService()

    pixSize = omero.model.LengthI(3.81,
                                  omero.model.enums.UnitsLength.NANOMETER)

    # Get the channel offsets
    for image in images:
        metadata = dict(image.loadOriginalMetadata()[1])
        pi = image.getPrimaryPixels()
        pixels = pi._obj

        pixels.setPhysicalSizeX(pixSize)
        pixels.setPhysicalSizeY(pixSize)

        updateService.saveObject(pixels)

    return message
def processObjects(client, scriptParams):
    message = ''

    # for params with default values, we can get the value directly
    dataType = scriptParams['Data_Type']
    ids = scriptParams['IDs']
    rmTables = scriptParams['Remove_tables']
    rmComments = scriptParams['Remove_comments']
    unlinkTags = scriptParams['Untag_images']
    rmTagsets = scriptParams['Remove_tagsets']

    # Get the images or datasets
    conn = BlitzGateway(client_obj=client)
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    for o in objects:
        message += removeAnnotations(
            conn, o, rmTables, rmComments, unlinkTags, rmTagsets)

    return message
Beispiel #14
0
def movieFigure(conn, commandArgs):
    """
    Makes the figure using the parameters in @commandArgs, attaches the figure
    to the parent Project/Dataset, and returns the file-annotation ID

    @param session      The OMERO session
    @param commandArgs  Map of parameters for the script
    @ returns           Returns the id of the originalFileLink child. (ID
                        object, not value)
    """

    log("Movie figure created by OMERO on %s" % date.today())
    log("")

    timeLabels = {
        "SECS_MILLIS": "seconds",
        "SECS": "seconds",
        "MINS": "minutes",
        "HOURS": "hours",
        "MINS_SECS": "mins:secs",
        "HOURS_MINS": "hours:mins"
    }
    timeUnits = "SECS"
    if "Time_Units" in commandArgs:
        timeUnits = commandArgs["Time_Units"]
        # convert from UI name to timeLabels key
        timeUnits = timeUnits.replace(" ", "_")
    if timeUnits not in timeLabels.keys():
        timeUnits = "SECS"
    log("Time units are in %s" % timeLabels[timeUnits])

    pixelIds = []
    imageIds = []
    imageLabels = []
    message = ""  # message to be returned to the client

    # 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

    # 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())

    # 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()
        iId = image.getId()
        imageDate = image.getAcquisitionDate()
        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()
    sizeT = omeroImage.getSizeT()

    tIndexes = []
    if "T_Indexes" in commandArgs:
        for t in commandArgs["T_Indexes"]:
            tIndexes.append(t)
        print "T_Indexes", tIndexes
    if len(tIndexes) == 0:  # if no t-indexes given, use all t-indices
        tIndexes = range(sizeT)

    zStart = -1
    zEnd = -1
    if "Z_Start" in commandArgs:
        zStart = commandArgs["Z_Start"]
    if "Z_End" in commandArgs:
        zEnd = commandArgs["Z_End"]

    width = sizeX
    if "Width" in commandArgs:
        width = commandArgs["Width"]

    height = sizeY
    if "Height" in commandArgs:
        height = commandArgs["Height"]

    spacer = (width / 25) + 2

    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_Size" in commandArgs:
        sb = commandArgs["Scalebar_Size"]
        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 "Scalebar_Colour" in commandArgs:
        r, g, b, a = OVERLAY_COLOURS[commandArgs["Scalebar_Colour"]]
        overlayColour = (r, g, b)

    maxColCount = 10
    if "Max_Columns" in commandArgs:
        maxColCount = commandArgs["Max_Columns"]

    figure = createMovieFigure(conn, pixelIds, tIndexes, zStart, zEnd, width,
                               height, spacer, algorithm, stepping, scalebar,
                               overlayColour, timeUnits, imageLabels,
                               maxColCount)

    log("")
    figLegend = "\n".join(logLines)

    #print figLegend    # bug fixing only
    format = commandArgs["Format"]

    figureName = "movieFigure"
    if "Figure_Name" in commandArgs:
        figureName = str(commandArgs["Figure_Name"])
        figureName = os.path.basename(figureName)
    output = "localfile"
    if format == 'PNG':
        output = output + ".png"
        figureName = figureName + ".png"
        figure.save(output, "PNG")
        mimetype = "image/png"
    elif format == 'TIFF':
        output = output + ".tiff"
        figureName = figureName + ".tiff"
        figure.save(output, "TIFF")
        mimetype = "image/tiff"
    else:
        output = output + ".jpg"
        figureName = figureName + ".jpg"
        figure.save(output)
        mimetype = "image/jpeg"
    namespace = NSCREATED + "/omero/figure_scripts/Movie_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn,
        output,
        omeroImage,
        output="Movie figure",
        mimetype=mimetype,
        ns=namespace,
        desc=figLegend,
        origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
Beispiel #15
0
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

    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, average x/t," \
            " speed(um/sec)"
        tableData = ""
        for roi in result.rois:
            for s in roi.copyShapes():
                if type(s) == omero.model.LineI:
                    tableData += "\nLine"
                    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
                    tableData += "\n"
                    tableData += ",".join([
                        str(x)
                        for x in (y1, x1, y2, x2, dy, dx, dxPerY, dxPerY, "")
                    ])
                    if micronsPerSec:
                        speed = dxPerY * micronsPerSec
                        tableData += "%s" % speed

                elif type(s) == omero.model.PolylineI:
                    tableData += "\nPolyline"
                    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))
                        tableData += "\n"
                        tableData += ",".join([
                            str(x) for x in (y1, x1, y2, x2, dy, dx, dxPerY,
                                             avXperY, "")
                        ])
                        if micronsPerSec:
                            speed = dxPerY * micronsPerSec
                            tableData += "%s" % speed

        # write table data to csv...
        if len(tableData) > 0:
            tableString = "secsPerPixelY: %s" % secsPerPixelY
            tableString += '\nmicronsPerPixelX: %s' % micronsPerPixelX
            tableString += "\nmicronsPerSec: %s" % micronsPerSec
            tableString += "\n"
            tableString += colNames
            tableString += tableData
            print tableString
            csvFileName = 'kymograph_velocities_%s.csv' % image.getId()
            csvFile = open(csvFileName, 'w')
            try:
                csvFile.write(tableString)
            finally:
                csvFile.close()

            fileAnn, faMessage = scriptUtil.createLinkFileAnnotation(
                conn,
                csvFileName,
                image,
                output="Line Plot csv (Excel) file",
                mimetype="text/csv",
                desc=None)
            print fileAnn, faMessage
            if fileAnn:
                fileAnns.append(fileAnn)
        else:
            print "Found NO lines or polylines to analyse for Image"

    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
Beispiel #16
0
def makeThumbnailFigure(conn, scriptParams):
    """
    Makes the figure using the parameters in @scriptParams, attaches the
    figure to the parent Project/Dataset, and returns the file-annotation ID

    @ returns       Returns the id of the originalFileLink child. (ID object,
                    not value)
    """

    log("Thumbnail figure created by OMERO")
    log("")

    message = ""

    # Get the objects (images or datasets)
    objects, logMessage = scriptUtil.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    # Get parent
    parent = None
    if "Parent_ID" in scriptParams and len(scriptParams["IDs"]) > 1:
        if scriptParams["Data_Type"] == "Image":
            parent = conn.getObject("Dataset", scriptParams["Parent_ID"])
        else:
            parent = conn.getObject("Project", scriptParams["Parent_ID"])

    if parent is None:
        parent = objects[0]  # Attach figure to the first object

    parentClass = parent.OMERO_CLASS
    log("Figure will be linked to %s%s: %s"
        % (parentClass[0].lower(), parentClass[1:], parent.getName()))

    tagIds = []
    if "Tag_IDs" in scriptParams:
        tagIds = scriptParams['Tag_IDs']
    if len(tagIds) == 0:
        tagIds = None

    showUntagged = False
    if (tagIds):
        showUntagged = scriptParams["Show_Untagged_Images"]

    thumbSize = scriptParams["Thumbnail_Size"]
    maxColumns = scriptParams["Max_Columns"]

    figHeight = 0
    figWidth = 0
    dsCanvases = []

    if scriptParams["Data_Type"] == "Dataset":
        for dataset in objects:
            log("Dataset: %s     ID: %d"
                % (dataset.getName(), dataset.getId()))
            images = list(dataset.listChildren())
            dsCanvas = paintDatasetCanvas(
                conn, images, dataset.getName(), tagIds, showUntagged,
                length=thumbSize, colCount=maxColumns)
            if dsCanvas is None:
                continue
            dsCanvases.append(dsCanvas)
            figHeight += dsCanvas.size[1]
            figWidth = max(figWidth, dsCanvas.size[0])
    else:
        imageCanvas = paintDatasetCanvas(
            conn, objects, "", tagIds,
            showUntagged, length=thumbSize, colCount=maxColumns)
        dsCanvases.append(imageCanvas)
        figHeight += imageCanvas.size[1]
        figWidth = max(figWidth, imageCanvas.size[0])

    if len(dsCanvases) == 0:
        message += "No figure created"
        return None, message

    figure = Image.new("RGB", (figWidth, figHeight), WHITE)
    y = 0
    for ds in dsCanvases:
        imgUtil.pasteImage(ds, figure, 0, y)
        y += ds.size[1]

    log("")
    figLegend = "\n".join(logLines)

    format = scriptParams["Format"]
    figureName = scriptParams["Figure_Name"]
    figureName = os.path.basename(figureName)
    output = "localfile"

    if format == 'PNG':
        output = output + ".png"
        figureName = figureName + ".png"
        figure.save(output, "PNG")
        mimetype = "image/png"
    elif format == 'TIFF':
        output = output + ".tiff"
        figureName = figureName + ".tiff"
        figure.save(output, "TIFF")
        mimetype = "image/tiff"
    else:
        output = output + ".jpg"
        figureName = figureName + ".jpg"
        figure.save(output)
        mimetype = "image/jpeg"

    namespace = NSCREATED + "/omero/figure_scripts/Thumbnail_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn, output, parent, output="Thumbnail figure", mimetype=mimetype,
        ns=namespace, desc=figLegend, origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
Beispiel #17
0
def processImages(conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""

    # Get the images
    images, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not images:
        return None, None, message
    imageIds = [i.getId() for i in images]

    # Get the channel offsets
    channel_offsets = []
    for i in range(1, 5):
        pName = "Channel_%s" % i
        if scriptParams[pName]:
            index = i - 1  # UI channel index is 1-based - we want 0-based
            x = "Channel%s_X_shift" % i in scriptParams and scriptParams["Channel%s_X_shift" % i] or 0
            y = "Channel%s_Y_shift" % i in scriptParams and scriptParams["Channel%s_Y_shift" % i] or 0
            z = "Channel%s_Z_shift" % i in scriptParams and scriptParams["Channel%s_Z_shift" % i] or 0
            channel_offsets.append({"index": index, "x": x, "y": y, "z": z})

    print channel_offsets

    dataset = None
    if "New_Dataset_Name" in scriptParams:
        # create new Dataset...
        newDatasetName = scriptParams["New_Dataset_Name"]
        dataset = omero.gateway.DatasetWrapper(conn, obj=omero.model.DatasetI())
        dataset.setName(rstring(newDatasetName))
        dataset.save()
        # add to parent Project
        parentDs = images[0].getParent()
        project = parentDs is not None and parentDs.getParent() or None
        if project is not None and project.canLink():
            link = omero.model.ProjectDatasetLinkI()
            link.parent = omero.model.ProjectI(project.getId(), False)
            link.child = omero.model.DatasetI(dataset.getId(), False)
            conn.getUpdateService().saveAndReturnObject(link)

    # need to handle Datasets eventually - Just do images for now
    newImages = []
    links = []
    for iId in imageIds:
        newImg, link = newImageWithChannelOffsets(conn, iId, channel_offsets, dataset)
        if newImg is not None:
            newImages.append(newImg)
            if link is not None:
                links.append(link)

    if not newImages:
        message += "No image created."
    else:
        if len(newImages) == 1:
            if not link:
                linkMessage = " but could not be attached"
            else:
                linkMessage = ""
            message += "New image created%s: %s." % (linkMessage, newImages[0].getName())
        elif len(newImages) > 1:
            message += "%s new images created" % len(newImages)
            if not len(links) == len(newImages):
                message += " but some of them could not be attached."
            else:
                message += "."

    return newImages, dataset, message
Beispiel #18
0
def makeImagesFromRois(conn, parameterMap):
    """
    Processes the list of Image_IDs, either making a new image-stack or a new dataset from each image,
    with new image planes coming from the regions in Rectangular ROIs on the parent images. 
    """
    
    dataType = parameterMap["Data_Type"]
    ids = parameterMap["IDs"]
    imageStack = parameterMap['Make_Image_Stack']
    
    message =""
    
    # Get the images
    objects, logMessage = script_utils.getObjects(conn, parameterMap)
    message += logMessage
    if not objects:
        return None, message
    
    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()
            
    # Check for rectangular ROIs and filter images list
    images = [image for image in images if image.getROICount("Rect")>0]
    if not images:
        message += "No rectangle ROI found."
        return None, message
        
    imageIds = [i.getId() for i in images]    
    newImages = []
    newDatasets = []
    links = []
    for iId in imageIds:
        newImage, newDataset, link = processImage(conn, iId, parameterMap)
        if newImage is not None:
            if isinstance(newImage,list):
                newImages.extend(newImage)
            else:
                newImages.append(newImage)
        if newDataset is not None:
            newDatasets.append(newDataset)
        if link is not None:
            if isinstance(link,list):
                links.extend(link)
            else:
                links.append(link)
      
    if newImages:
        if len(newImages)>1:
            message += "Created %s new images" % len(newImages)
        else:
            message += "Created a new image"
    else:
        message += "No image created"
    
    if newDatasets:
        if len(newDatasets)>1:
            message += " and %s new datasets" % len(newDatasets)
        else:
            message += " and a new dataset"
    
    if not links or not len(links) == len(newImages):
        message += " but some images could not be attached"
    message += "."

    robj = (len(newImages) > 0) and newImages[0]._obj or None
    return robj, message
Beispiel #19
0
def batchImageExport(conn, scriptParams):

    # for params with default values, we can get the value directly
    splitCs = scriptParams["Export_Individual_Channels"]
    mergedCs = scriptParams["Export_Merged_Image"]
    greyscale = scriptParams["Individual_Channels_Grey"]
    dataType = scriptParams["Data_Type"]
    ids = scriptParams["IDs"]
    folder_name = scriptParams["Folder_Name"]
    format = scriptParams["Format"]
    projectZ = "Choose_Z_Section" in scriptParams and scriptParams[
        "Choose_Z_Section"] == 'Max projection'

    if (not splitCs) and (not mergedCs):
        log("Not chosen to save Individual Channels OR Merged Image")
        return

    # check if we have these params
    channelNames = []
    if "Channel_Names" in scriptParams:
        channelNames = scriptParams["Channel_Names"]
    imgWidth = None
    if "Image_Width" in scriptParams:
        imgWidth = scriptParams["Image_Width"]

    # functions used below for each imaage.
    def getZrange(sizeZ, scriptParams):
        zRange = None
        if "Choose_Z_Section" in scriptParams:
            zChoice = scriptParams["Choose_Z_Section"]
            # NB: all Z indices in this script are 1-based
            if zChoice == 'ALL Z planes':
                zRange = (1, sizeZ + 1)
            elif "OR_specify_Z_index" in scriptParams:
                zIndex = scriptParams["OR_specify_Z_index"]
                zIndex = min(zIndex, sizeZ)
                zRange = (zIndex, )
            elif "OR_specify_Z_start_AND..." in scriptParams and "...specify_Z_end" in scriptParams:
                start = scriptParams["OR_specify_Z_start_AND..."]
                start = min(start, sizeZ)
                end = scriptParams["...specify_Z_end"]
                end = min(end, sizeZ)
                zStart = min(start,
                             end)  # in case user got zStart and zEnd mixed up
                zEnd = max(start, end)
                if zStart == zEnd:
                    zRange = (zStart, )
                else:
                    zRange = (zStart, zEnd + 1)
        return zRange

    def getTrange(sizeT, scriptParams):
        tRange = None
        if "Choose_T_Section" in scriptParams:
            tChoice = scriptParams["Choose_T_Section"]
            # NB: all T indices in this script are 1-based
            if tChoice == 'ALL T planes':
                tRange = (1, sizeT + 1)
            elif "OR_specify_T_index" in scriptParams:
                tIndex = scriptParams["OR_specify_T_index"]
                tIndex = min(tIndex, sizeT)
                tRange = (tIndex, )
            elif "OR_specify_T_start_AND..." in scriptParams and "...specify_T_end" in scriptParams:
                start = scriptParams["OR_specify_T_start_AND..."]
                start = min(start, sizeT)
                end = scriptParams["...specify_T_end"]
                end = min(end, sizeT)
                tStart = min(start,
                             end)  # in case user got zStart and zEnd mixed up
                tEnd = max(start, end)
                if tStart == tEnd:
                    tRange = (tStart, )
                else:
                    tRange = (tStart, tEnd + 1)
        return tRange

    # Get the images or datasets
    message = ""
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    # Attach figure to the first image
    parent = objects[0]

    if dataType == 'Dataset':
        images = []
        for ds in objects:
            images.extend(list(ds.listChildren()))
        if not images:
            message += "No image found in dataset(s)"
            return None, message
    else:
        images = objects

    log("Processing %s images" % len(images))

    # somewhere to put images
    curr_dir = os.getcwd()
    exp_dir = os.path.join(curr_dir, folder_name)
    try:
        os.mkdir(exp_dir)
    except:
        pass

    # do the saving to disk
    if format == 'OME-TIFF':
        for img in images:
            log("Exporting image as OME-TIFF: %s" % img.getName())
            saveAsOmeTiff(conn, img, folder_name)

    else:
        for img in images:
            log("\n----------- Saving planes from image: '%s' ------------" %
                img.getName())
            sizeC = img.getSizeC()
            sizeZ = img.getSizeZ()
            sizeT = img.getSizeT()
            zRange = getZrange(sizeZ, scriptParams)
            tRange = getTrange(sizeT, scriptParams)
            log("Using:")
            if zRange is None: log("  Z-index: Last-viewed")
            elif len(zRange) == 1: log("  Z-index: %d" % zRange[0])
            else: log("  Z-range: %s-%s" % (zRange[0], zRange[1] - 1))
            if projectZ: log("  Z-projection: ON")
            if tRange is None: log("  T-index: Last-viewed")
            elif len(tRange) == 1: log("  T-index: %d" % tRange[0])
            else: log("  T-range: %s-%s" % (tRange[0], tRange[1] - 1))
            log("  Format: %s" % format)
            if imgWidth is None: log("  Image Width: no resize")
            else: log("  Image Width: %s" % imgWidth)
            log("  Greyscale: %s" % greyscale)
            log("Channel Rendering Settings:")
            for ch in img.getChannels():
                log("  %s: %d-%d" %
                    (ch.getLabel(), ch.getWindowStart(), ch.getWindowEnd()))

            savePlanesForImage(conn,
                               img,
                               sizeC,
                               splitCs,
                               mergedCs,
                               channelNames,
                               zRange,
                               tRange,
                               greyscale,
                               imgWidth,
                               projectZ=projectZ,
                               format=format,
                               folder_name=folder_name)

        # write log for exported images (not needed for ome-tiff)
        logFile = open(os.path.join(exp_dir, 'Batch_Image_Export.txt'), 'w')
        try:
            for s in logStrings:
                logFile.write(s)
                logFile.write("\n")
        finally:
            logFile.close()

    # zip everything up (unless we've only got a single ome-tiff)
    if format == 'OME-TIFF' and len(os.listdir(exp_dir)) == 1:
        export_file = os.path.join(folder_name, os.listdir(exp_dir)[0])
        mimetype = 'image/tiff'
    else:
        export_file = "%s.zip" % folder_name
        compress(export_file, folder_name)
        mimetype = 'application/zip'

    namespace = omero.constants.namespaces.NSCREATED + "/omero/export_scripts/Batch_Image_Export"
    fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(
        conn,
        export_file,
        parent,
        output="Batch export zip",
        ns=namespace,
        mimetype=mimetype)
    message += annMessage
    return fileAnnotation, message
def weka_segmentation(conn, scriptParams, uuid):


    model = scriptParams["Segmentation_model"]
    model_path=model

    user = conn.getUser()
    user = user.getName()
    print("user: %s" % (user))

    print(model_path)

    images, logMessage = script_utils.getObjects(conn, scriptParams)
    if not images:
        return None, None, logMessage
    imageIds = [i.getId() for i in images]

    for iId in imageIds:

        tmpdir_stack = tempfile.mkdtemp(dir=cache_dir)
        tmpdir_out = tempfile.mkdtemp(dir=cache_dir)

        image = conn.getObject("Image", iId)
        dataset = image.getParent().getId()

        sizeZ = image.getSizeZ()
        print(sizeZ)

        # job_liner=[]

        # all_jobs = open("%s.job" % (os.path.join(cache_dir, tmpdir_out.split('/')[-1])), 'w+')

        image_dir = os.path.join(OMERO_FS, image.getFileset().listFiles()[0].getPath())
        image_name = image.getFileset().listFiles()[0].getName()
        image_path = os.path.join(image_dir, image_name)

        ijmacro_args = "%s:%s/:%s" % (image_path, tmpdir_out, model_path)
        
        memory_ij=int(math.ceil((2.00*sizeZ)))

        all_jobs = open("%s.job" % (os.path.join(cache_dir, tmpdir_out.split('/')[-1])), 'w+')
        job_liner="%s -a %s -Xmx%sg -- -macro %s %s -batch" % (XVFBRUN_PATH, IMAGEJ_PATH, memory_ij, MACRO_PATH2, ijmacro_args)
        all_jobs.writelines(job_liner)

        print(job_liner)

        system = scriptParams["System"]
        wtime = scriptParams["Wall_time"]
        pmem = scriptParams["Private_memory"]
        bigmem = scriptParams["Big_memory_nodes"]
        nodes = scriptParams["Nodes"]
        ppn = scriptParams["PPN"]

        if bigmem:
            print("big memory nodes flag")
        else:

        # ijmacro_args = "%s/:%s/:%s" % (tmpdir_stack, tmpdir_out, model_path)
        pbs_file = "%s.pbs" % (os.path.join(cache_dir, tmpdir_out.split('/')[-1]))

        nodes = int(math.ceil(((2.00*sizeZ)+0.15*(2.00*sizeZ))/48))

        stack_args = "%s/" % (tmpdir_out)
        qsub_cmd = ". %s %s %s %s %s %s %s %s %s %s %s %s > %s" % (PBS_GEN, user, dataset, image_name, uuid, MACRO_PATH, stack_args, tmpdir_out, wtime, pmem, all_jobs.name, nodes, pbs_file)
        # $ ./jobgen.sh <user> <dataset> <name> <uuid> <ijmacro> <ijmacro args> <outpath> <wtime> <pmem> <all_jobs> <no_nodes> > job.pbs

        print(qsub_cmd)
        os.system(qsub_cmd)

        enableKeepAlive_time = (36*60*60)
        conn.c.enableKeepAlive(enableKeepAlive_time)
        os.system("ssh %s '/usr/syscom/opt/torque/4.2.6/bin/qsub %s'" % (system, pbs_file))

def runAsScript():

    dataTypes = [rstring('Image')]

    models_path="/project/projectdirs/ngbi/jobs/ij_macros/classifiers"
    systems=['carver', 'sgnworker']

    # modelPath = []
    segmentationModel = []
    for file in os.listdir(models_path):
        if file.endswith(".model"):
            segmentationModel.append(str(os.path.join(models_path,file)))
            # segmentationModel.append(file)

    # Include files metadata information; link to a web application

    # segmentationModel = ['Desulfovibrio RCH1', 'Geobacter', 'Myxococcus Xanthus1', 'Myxococcus Xanthus2']

    client = scripts.client('weka_serial.py', """Segment a dataset using Random Forest and a known classifier""",

    scripts.String("Data_Type", optional=False, grouping="1",
        description="Pick Images by 'Image' ID or by the ID of their 'Dataset'", values=dataTypes, default="Image"),

    scripts.List("IDs", optional=False, grouping="1.1",
        description="List of Dataset IDs or Image IDs to process.").ofType(rlong(0)),

    scripts.String("Segmentation_model", optional=False, grouping="2",
        description="Select model", values=segmentationModel, default=segmentationModel[0]),

    scripts.String("System", optional=False, grouping="3",
        description="Select the system", values=systems, default=systems[0]),

    scripts.String("Wall_time", grouping="3.1",
        description="Wall time", default='0:30:00'),

    scripts.String("Private_memory", grouping="3.2",
        description="Private memory", default='4GB'),

    scripts.Bool("Big_memory_nodes", grouping="3.2.1",
        description="Big memory nodes", default='False'),

    scripts.String("Nodes", grouping="3.3",
        description="Nodes", default='1'),

    scripts.String("PPN", grouping="3.4",
        description="PPN", default='5'),

    version = "0",
    authors = ["Joaquin Correa", "OSP Team"],
    institutions = ["NERSC"],
    contact = "*****@*****.**",
    )

    try:
        session = client.getSession();





        # print("session key: %s" % (session)

        # process the list of args above.
        scriptParams = {}
        for key in client.getInputKeys():
            if client.getInput(key):
                scriptParams[key] = client.getInput(key, unwrap=True)

        print scriptParams

        print("key: %s" % (key))
        # print("session key: %s" % (session))

        # wrap client to use the Blitz Gateway
        conn = BlitzGateway(client_obj=client)

        admin = conn.getAdminService()
        uuid = admin.getEventContext().sessionUuid
        # uuid = uuid.split(":")[1]


        print(uuid)

        # print("key: %s" % (key))

        # sizeZ, sizeC, sizeT, sizeX, sizeY, dataset = newFilteredImage(conn, scriptParams)
        # newFilteredImage(conn, scriptParams)

        # sloopy("/global/project/projectdirs/ngbi/omero-0.7/OMERO.server/lib/scripts/omero/tmp/WT_7B_duct2.mrc")
        weka_segmentation(conn, scriptParams, uuid)

        # print sizeZ, sizeC, sizeT, sizeX, sizeY, dataset

        # Return message, new image and new dataset (if applicable) to the client
        # client.setOutput("Message", rstring(message))
        # if len(images) == 1:
        #     client.setOutput("Image", robject(images[0]._obj))
        # if dataset is not None:
        #     client.setOutput("New Dataset", robject(dataset._obj))

    finally:
        client.closeSession()

if __name__ == "__main__":
    runAsScript()
Beispiel #21
0
def datasets_to_plates(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the datasets ID
    datasets, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not datasets:
        return None, message

    # Filter dataset IDs by permissions
    IDs = [ds.getId() for ds in datasets if ds.canLink()]
    if len(IDs) != len(datasets):
        permIDs = [str(ds.getId()) for ds in datasets if not ds.canLink()]
        message += "You do not have the permissions to add the images from"\
            " the dataset(s): %s." % ",".join(permIDs)
    if not IDs:
        return None, message

    # find or create Screen if specified
    screen = None
    newscreen = None
    if "Screen" in scriptParams and len(scriptParams["Screen"]) > 0:
        s = scriptParams["Screen"]
        # see if this is ID of existing screen
        try:
            screenId = long(s)
            screen = conn.getObject("Screen", screenId)
        except ValueError:
            pass
        # if not, create one
        if screen is None:
            newscreen = omero.model.ScreenI()
            newscreen.name = rstring(s)
            newscreen = updateService.saveAndReturnObject(newscreen)
            screen = conn.getObject("Screen", newscreen.id.val)

    plates = []
    links = []
    deletes = []
    for datasetId in IDs:
        plate, link, deleteHandle = dataset_to_plate(conn, scriptParams,
                                                     datasetId, screen)
        if plate is not None:
            plates.append(plate)
        if link is not None:
            links.append(link)
        if deleteHandle is not None:
            deletes.append(deleteHandle)

    # wait for any deletes to finish
    for handle in deletes:
        cb = omero.callbacks.DeleteCallbackI(conn.c, handle)
        while True:  # ms
            if cb.block(100) is None:
                print "Waiting for delete"
            else:
                break
        err = handle.errors()
        if err > 0:
            print "Delete error", err
        else:
            print "Delete OK"

    if newscreen:
        message += "New screen created: %s." % newscreen.getName().val
        robj = newscreen
    elif plates:
        robj = plates[0]
    else:
        robj = None

    if plates:
        if len(plates) == 1:
            message += " New plate created: %s" % plates[0].name.val
        else:
            message += " %s plates created" % len(plates)
        if len(plates) == len(links):
            message += "."
        else:
            message += " but could not be attached."
    else:
        message += "No plate created."
    return robj, message
def run_processing(conn, session, script_params):
    """
    Processes the list of Image_IDs, either making a new image-stack or a new
    dataset from each image, with new image planes coming from the regions in
    Rectangular ROIs on the parent images.
    
    @param conn: The BlitzGateway connection
    @param session: A dictionart containing session ID and hostname
    @param script_params: The script input parameters
    """

    dataType = script_params["Data_Type"]

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, script_params)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    # Check for rectangular ROIs and filter images list
    images = [
        image for image in images if (image.getROICount("Rect") > 0) or (
            image.getROICount("Polygon") > 0)
    ]
    if not images:
        message += "No Rectangle or Polygon ROI found."
        return None, message

    total_rects = sum([i.getROICount("Rect") for i in images])
    total_polys = sum([i.getROICount("Polygon") for i in images])
    if total_rects + total_polys > 10:
        message += "Cannot start batch processing - too many rois (maximum is 10)."
        return None, message

    imageIds = [i.getId() for i in images]
    newImages = []
    newDatasets = []
    links = []
    for iId in imageIds:
        newImage, newDataset, link, new_ids = process_image(
            conn, iId, script_params, session)
        if newImage is not None:
            if isinstance(newImage, list):
                newImages.extend(newImage)
            else:
                newImages.append(newImage)
        if newDataset is not None:
            newDatasets.append(newDataset)
        if link is not None:
            if isinstance(link, list):
                links.extend(link)
            else:
                links.append(link)

    if newImages:
        if len(newImages) > 1:
            message += "Created %s new images" % len(newImages)
        else:
            message += "Created a new image"
    else:
        message += "No image created"

    if newDatasets:
        if len(newDatasets) > 1:
            message += " and %s new datasets" % len(newDatasets)
        else:
            message += " and a new dataset"

    print script_params['Email_Results']
    if script_params['Email_Results'] and (newImages or newDatasets):
        email_results(conn, script_params, new_ids)

    if not links or not len(links) == len(newImages):
        message += " but some images could not be attached"
    message += "."

    robj = (len(newImages) > 0) and newImages[0]._obj or None
    return robj, message
def run_processing(conn, session, script_params):
    """
    Collects params and starts the processing
    
    @param conn:          the BlitzGateWay connection
    @param script_params: the parameters collected from the script input
    """ 
        
    global input_dir
    global output_dir

    input_dir = tempfile.mkdtemp(prefix='stitching_input')
    output_dir = tempfile.mkdtemp(prefix='stitching_output')
    
    def empty_dir(dir_path):
        for old_file in os.listdir(dir_path):
            file_path = os.path.join(dir_path, old_file)
            os.unlink(file_path)
        
    message = ""
      
    dataType = script_params["Data_Type"]
    
    # Get the images
    objects, logMessage = script_utils.getObjects(conn, script_params)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()
    
    image_ids = [i.getId() for i in images]
    if len(image_ids) > 10:
        message = 'Max number of datasets for batch exceeded (10)'
        return message
    
    new_images = []
    new_ids = []
    for image in conn.getObjects("Image",image_ids):
        # remove input and processed images
        empty_dir(input_dir)
        empty_dir(output_dir)
        theC = None    
        if script_params['Single_Channel']:
            theC = script_params['Channel']

        theZ = None
        sizeZ = image.getSizeZ()
        if script_params['Single_Z']:
            theZ = script_params['Z_slice']
            sizeZ = 1
        if script_params['Range_Z']:
            zstart = script_params['Z_start']
            zstop = script_params['Z_stop']
            theZ = range(zstart,zstop+1)
            sizeZ = len(theZ)
        
        # download the image
        image_names = download_tiles(conn,image,theC,theZ)
                
        results_file = str(image.getId()) + "_stitching.txt"        
        
        stitching_args = (script_params['grid_x'], script_params['grid_y'], \
                          script_params['tile_overlap'], results_file,\
                          script_params['fusion_method'], script_params['regression_threshold'], \
                          script_params['ave_displacement_threshold'], script_params['abs_displacement_threshold'],\
                          sizeZ,"T{i}")
        
#         new_image = run_stitching(conn,image,channels,zslices,stitching_args,results_file)
        new_image = run_stitching(conn,session,stitching_args)
                    
        if new_image:
            create_containers(conn,image, new_image)
            new_images.append(new_image)
            new_ids.append(new_image.getId())
     
        if len(new_ids) == 0:
            print "No new images created."
            return       
  
    if new_images:
        if len(new_images) > 1:
            message += "Created %s new images" % len(new_images)
        else:
            message += "Created a new image"
    else:
        message += "No image created"
        
    
    print script_params['Email_Results']
    if script_params['Email_Results'] and new_images:
        email_results(conn,script_params,new_ids)
        
    shutil.rmtree(input_dir)
    shutil.rmtree(output_dir)
# 
    robj = (len(new_images) > 0) and new_images[0]._obj or None
    return robj, message
Beispiel #24
0
def makeImagesFromRois(conn, parameterMap):
    """
    Processes the list of Image_IDs, either making a new image-stack or a new
    dataset from each image, with new image planes coming from the regions in
    Rectangular ROIs on the parent images.
    """

    dataType = parameterMap["Data_Type"]

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, parameterMap)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    # Check for rectangular ROIs and filter images list
    images = [image for image in images if image.getROICount("Rect") > 0]
    if not images:
        message += "No rectangle ROI found."
        return None, message

    imageIds = [i.getId() for i in images]
    newImages = []
    newDatasets = []
    links = []
    for iId in imageIds:
        newImage, newDataset, link = processImage(conn, iId, parameterMap)
        if newImage is not None:
            if isinstance(newImage, list):
                newImages.extend(newImage)
            else:
                newImages.append(newImage)
        if newDataset is not None:
            newDatasets.append(newDataset)
        if link is not None:
            if isinstance(link, list):
                links.extend(link)
            else:
                links.append(link)

    if newImages:
        if len(newImages) > 1:
            message += "Created %s new images" % len(newImages)
        else:
            message += "Created a new image"
    else:
        message += "No image created"

    if newDatasets:
        if len(newDatasets) > 1:
            message += " and %s new datasets" % len(newDatasets)
        else:
            message += " and a new dataset"

    if not links or not len(links) == len(newImages):
        message += " but some images could not be attached"
    message += "."

    robj = (len(newImages) > 0) and newImages[0]._obj or None
    return robj, message
Beispiel #25
0
def combineImages(conn, parameterMap):

    # get the services we need
    services = {}
    services["containerService"] = conn.getContainerService()
    services["renderingEngine"] = conn.createRenderingEngine()
    services["queryService"] = conn.getQueryService()
    services["pixelsService"] = conn.getPixelsService()
    services["rawPixelStore"] = conn.c.sf.createRawPixelsStore()
    services["rawPixelStoreUpload"] = conn.c.sf.createRawPixelsStore()
    services["updateService"] = conn.getUpdateService()
    services["rawFileStore"] = conn.createRawFileStore()

    queryService = services["queryService"]
    containerService = services["containerService"]

    colourMap = {}
    if "Channel_Colours" in parameterMap:
        for c, col in enumerate(parameterMap["Channel_Colours"]):
            colour = col.getValue()
            if colour in COLOURS:
                colourMap[c] = COLOURS[colour]

    # Get images or datasets
    message = ""
    objects, logMessage = scriptUtil.getObjects(conn, parameterMap)
    message += logMessage
    if not objects:
        return None, message

    # get the images IDs from list (in order) or dataset (sorted by name)
    outputImages = []
    links = []

    dataType = parameterMap["Data_Type"]
    if dataType == "Image":
        dataset = None
        imageIds = [image.id for image in objects]
        # get dataset from first image
        query_string = "select i from Image i join fetch i.datasetLinks idl join fetch idl.parent where i.id in (%s)" % imageIds[
            0]
        image = queryService.findByQuery(query_string, None)
        if image:
            for link in image.iterateDatasetLinks():
                ds = link.parent
                dataset = conn.getObject("Dataset", ds.id.val)
                print "Dataset", dataset.getName()
                break  # only use 1st dataset
        else:
            print "No Dataset found for Image ID: %s  Combined Image will not be put into dataset." % imageIds[
                0]
        newImg, link = makeSingleImage(services, parameterMap, imageIds,
                                       dataset, colourMap)
        if newImg:
            outputImages.append(newImg)
        if link:
            links.append(link)
    else:
        for dataset in objects:
            images = list(dataset.listChildren())
            if not images:
                print "No images found for Dataset ID: %s" % dataset.getId()
                continue
            images.sort(key=lambda x: (x.getName()))
            imageIds = [i.getId() for i in images]
            newImg, link = makeSingleImage(services, parameterMap, imageIds,
                                           dataset, colourMap)
            if newImg:
                outputImages.append(newImg)
            if link:
                links.append(link)

    # try and close any stateful services
    for s in services:
        try:
            s.close()
        except:
            pass

    if outputImages:
        if len(outputImages) > 1:
            message += "%s new images created" % len(outputImages)
        else:
            message += "New image created"
        if not links or not len(links) == len(outputImages):
            message += " but could not be attached"
    else:
        message += "No image created"
    message += "."

    return outputImages, message
Beispiel #26
0
def batchImageExport(conn, scriptParams):
    
    # for params with default values, we can get the value directly
    splitCs = scriptParams["Export_Individual_Channels"]
    mergedCs = scriptParams["Export_Merged_Image"] 
    greyscale = scriptParams["Individual_Channels_Grey"]
    dataType = scriptParams["Data_Type"]
    ids = scriptParams["IDs"]
    folder_name = scriptParams["Folder_Name"]
    format = scriptParams["Format"]
    projectZ = "Choose_Z_Section" in scriptParams and scriptParams["Choose_Z_Section"] == 'Max projection'
    
    if (not splitCs) and (not mergedCs):
        log("Not chosen to save Individual Channels OR Merged Image")
        return
        
    # check if we have these params
    channelNames = []
    if "Channel_Names" in scriptParams:
        channelNames = scriptParams["Channel_Names"]
    imgWidth = None
    if "Image_Width" in scriptParams:
        imgWidth = scriptParams["Image_Width"]
    
    
    # functions used below for each imaage.
    def getZrange(sizeZ, scriptParams):
        zRange = None
        if "Choose_Z_Section" in scriptParams:
            zChoice = scriptParams["Choose_Z_Section"]
            # NB: all Z indices in this script are 1-based
            if zChoice == 'ALL Z planes':
                zRange = (1, sizeZ+1)
            elif "OR_specify_Z_index" in scriptParams:
                zIndex = scriptParams["OR_specify_Z_index"]
                zIndex = min(zIndex, sizeZ)
                zRange = (zIndex,)
            elif "OR_specify_Z_start_AND..." in scriptParams and "...specify_Z_end" in scriptParams:
                start = scriptParams["OR_specify_Z_start_AND..."]
                start = min(start, sizeZ)
                end = scriptParams["...specify_Z_end"]
                end = min(end, sizeZ)
                zStart = min(start, end)  # in case user got zStart and zEnd mixed up
                zEnd = max(start, end)
                if zStart == zEnd:
                    zRange = (zStart,)
                else:
                    zRange = (zStart, zEnd+1)
        return zRange
    
    def getTrange(sizeT, scriptParams):
        tRange = None
        if "Choose_T_Section" in scriptParams:
            tChoice = scriptParams["Choose_T_Section"]
            # NB: all T indices in this script are 1-based
            if tChoice == 'ALL T planes':
                tRange = (1, sizeT+1)
            elif "OR_specify_T_index" in scriptParams:
                tIndex = scriptParams["OR_specify_T_index"]
                tIndex = min(tIndex, sizeT)
                tRange = (tIndex,)
            elif "OR_specify_T_start_AND..." in scriptParams and "...specify_T_end" in scriptParams:
                start = scriptParams["OR_specify_T_start_AND..."]
                start = min(start, sizeT)
                end = scriptParams["...specify_T_end"]
                end = min(end, sizeT)
                tStart = min(start, end)  # in case user got zStart and zEnd mixed up
                tEnd = max(start, end)
                if tStart == tEnd:
                    tRange = (tStart,)
                else:
                    tRange = (tStart, tEnd+1)
        return tRange

    # Get the images or datasets
    message = ""
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message
    
    # Attach figure to the first image
    parent = objects[0]
    
    if dataType == 'Dataset':
        images = []
        for ds in objects:
            images.extend( list(ds.listChildren()) )
        if not images:
            message += "No image found in dataset(s)"
            return None, message
    else:
        images = objects
        
    log("Processing %s images" % len(images))
    
    # somewhere to put images
    curr_dir = os.getcwd()
    exp_dir = os.path.join(curr_dir, folder_name)
    try:
        os.mkdir(exp_dir)
    except:
        pass
    
    # do the saving to disk
    if format == 'OME-TIFF':
        for img in images:
            log("Exporting image as OME-TIFF: %s" % img.getName())
            saveAsOmeTiff(conn, img, folder_name)

    else:
        for img in images:
            log("\n----------- Saving planes from image: '%s' ------------" % img.getName())
            sizeC = img.getSizeC()
            sizeZ = img.getSizeZ()
            sizeT = img.getSizeT()
            zRange = getZrange(sizeZ, scriptParams)
            tRange = getTrange(sizeT, scriptParams)
            log("Using:")
            if zRange is None:      log("  Z-index: Last-viewed")
            elif len(zRange) == 1:  log("  Z-index: %d" % zRange[0])
            else:                   log("  Z-range: %s-%s" % ( zRange[0],zRange[1]-1) )
            if projectZ:            log("  Z-projection: ON")
            if tRange is None:      log("  T-index: Last-viewed")
            elif len(tRange) == 1:  log("  T-index: %d" % tRange[0])
            else:                   log("  T-range: %s-%s" % ( tRange[0],tRange[1]-1) )
            log("  Format: %s" % format)
            if imgWidth is None:    log("  Image Width: no resize")
            else:                   log("  Image Width: %s" % imgWidth)
            log("  Greyscale: %s" % greyscale)
            log("Channel Rendering Settings:")
            for ch in img.getChannels():
                log("  %s: %d-%d" % (ch.getLabel(), ch.getWindowStart(), ch.getWindowEnd()) )
        
            savePlanesForImage(conn, img, sizeC, splitCs, mergedCs, channelNames,
                zRange, tRange, greyscale, imgWidth, projectZ=projectZ, format=format, folder_name=folder_name)

        # write log for exported images (not needed for ome-tiff)
        logFile = open(os.path.join(exp_dir, 'Batch_Image_Export.txt'), 'w')
        try:
            for s in logStrings:
                logFile.write(s)
                logFile.write("\n")
        finally:
            logFile.close()

    # zip everything up (unless we've only got a single ome-tiff)
    if format == 'OME-TIFF' and len(os.listdir(exp_dir)) == 1:
        export_file = os.path.join(folder_name, os.listdir(exp_dir)[0])
        mimetype = 'image/tiff'
    else:
        export_file = "%s.zip" % folder_name
        compress(export_file, folder_name)
        mimetype='application/zip'

    namespace = omero.constants.namespaces.NSCREATED+"/omero/export_scripts/Batch_Image_Export"
    fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(conn, export_file, parent, 
        output="Batch export zip", ns=namespace, mimetype=mimetype)
    message += annMessage
    return fileAnnotation, message
Beispiel #27
0
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
Beispiel #28
0
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
    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()
        iId = image.getId()
        imageDate = image.getAcquisitionDate()
        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))

    # the channels in the combined image,
    if "Merged_Channels" in commandArgs:
        # convert to 0-based
        mergedIndexes = [c - 1 for c in commandArgs["Merged_Channels"]]
    else:
        mergedIndexes = range(sizeC)  # show all
    mergedIndexes.reverse()

    #  if no colours added, use existing rendering settings.
    mergedColours = {}
    # Actually, nicer to always use existing rendering settings.
    # if "Merged_Colours" in commandArgs:
    #     for i, c in enumerate(commandArgs["Merged_Colours"]):
    #         if c in COLOURS:
    #             mergedColours[i] = COLOURS[c]

    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 "Scalebar_Colour" in commandArgs:
        if commandArgs["Scalebar_Colour"] in OVERLAY_COLOURS:
            r, g, b, a = OVERLAY_COLOURS[commandArgs["Scalebar_Colour"]]
            overlayColour = (r, g, b)

    roiZoom = None
    if "Roi_Zoom" in commandArgs:
        roiZoom = float(commandArgs["Roi_Zoom"])
        if roiZoom == 0:
            roiZoom = None

    maxColumns = None
    if "Max_Columns" in commandArgs:
        maxColumns = commandArgs["Max_Columns"]

    showRoiDuration = False
    if "Show_ROI_Duration" in commandArgs:
        showRoiDuration = commandArgs["Show_ROI_Duration"]

    roiLabel = "FigureROI"
    if "Roi_Selection_Label" in commandArgs:
        roiLabel = commandArgs["Roi_Selection_Label"]

    spacer = (width / 50) + 2

    print "showRoiDuration", showRoiDuration
    fig = getSplitView(conn, imageIds, pixelIds, mergedIndexes, mergedColours,
                       width, height, imageLabels, spacer, algorithm, stepping,
                       scalebar, overlayColour, roiZoom, maxColumns,
                       showRoiDuration, roiLabel)

    # fig.show()        # bug-fixing only

    if fig is None:
        logMessage = "No figure produced"
        log("\n" + logMessage)
        message += logMessage
        return None, message
    figLegend = "\n".join(logStrings)

    # print figLegend    # bug fixing only
    format = commandArgs["Format"]

    figureName = "movieROIFigure"
    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/Movie_ROI_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn,
        output,
        omeroImage,
        output="Movie ROI figure",
        mimetype=mimetype,
        ns=namespace,
        desc=figLegend,
        origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
Beispiel #29
0
def combineImages(conn, parameterMap):

    # get the services we need
    services = {}
    services["containerService"] = conn.getContainerService()
    services["renderingEngine"] = conn.createRenderingEngine()
    services["queryService"] = conn.getQueryService()
    services["pixelsService"] = conn.getPixelsService()
    services["rawPixelStore"] = conn.c.sf.createRawPixelsStore()
    services["rawPixelStoreUpload"] = conn.c.sf.createRawPixelsStore()
    services["updateService"] = conn.getUpdateService()
    services["rawFileStore"] = conn.createRawFileStore()

    queryService = services["queryService"]

    colourMap = {}
    if "Channel_Colours" in parameterMap:
        for c, colour in enumerate(parameterMap["Channel_Colours"]):
            if colour in COLOURS:
                colourMap[c] = COLOURS[colour]
    print "colourMap", colourMap

    # Get images or datasets
    message = ""
    objects, logMessage = scriptUtil.getObjects(conn, parameterMap)
    message += logMessage
    if not objects:
        print message
        return None, message

    # get the images IDs from list (in order) or dataset (sorted by name)
    outputImages = []
    links = []

    dataType = parameterMap["Data_Type"]
    if dataType == "Image":
        dataset = None
        objects.sort(key=lambda x: (x.getName()))    # Sort images by name
        imageIds = [image.id for image in objects]
        # get dataset from first image
        query_string = "select i from Image i join fetch i.datasetLinks idl"\
            " join fetch idl.parent where i.id in (%s)" % imageIds[0]
        image = queryService.findByQuery(query_string, None)
        if image:
            for link in image.iterateDatasetLinks():
                ds = link.parent
                dataset = conn.getObject("Dataset", ds.id.val)
                print "Dataset", dataset.getName()
                break    # only use 1st dataset
        else:
            print "No Dataset found for Image ID: %s  Combined Image will "\
                "not be put into dataset." % imageIds[0]
        newImg, link = makeSingleImage(services, parameterMap, imageIds,
                                       dataset, colourMap)
        if newImg:
            outputImages.append(newImg)
        if link:
            links.append(link)
    else:
        for dataset in objects:
            images = list(dataset.listChildren())
            if not images:
                print "No images found for Dataset ID: %s" % dataset.getId()
                continue
            images.sort(key=lambda x: (x.getName()))
            imageIds = [i.getId() for i in images]
            newImg, link = makeSingleImage(services, parameterMap, imageIds,
                                           dataset, colourMap)
            if newImg:
                outputImages.append(newImg)
            if link:
                links.append(link)

    # try and close any stateful services
    for s in services:
        try:
            s.close()
        except:
            pass

    if outputImages:
        if len(outputImages) > 1:
            message += "%s new images created" % len(outputImages)
        else:
            message += "New image created"
        if not links or not len(links) == len(outputImages):
            message += " but could not be attached"
    else:
        message += "No image created"
    message += "."

    return outputImages, message
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
Beispiel #31
0
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
Beispiel #32
0
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
Beispiel #33
0
def mwell_fields_to_plates(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the well IDs
    wells, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    # Filter dataset IDs by permissions
    IDs = [ws.getId() for ws in wells if ws.canLink()]
    if len(IDs) != len(wells):
        permIDs = [str(ws.getId()) for ws in wells if not ws.canLink()]
        message += "You do not have the permissions to add the images from"\
            " the well(s): %s." % ",".join(permIDs)
    if not IDs:
        return None, message

    # find or create Screen if specified
    screen = None
    newscreen = None
    if "Screen" in scriptParams and len(scriptParams["Screen"]) > 0:
        s = scriptParams["Screen"]
        # see if this is ID of existing screen
        try:
            screenId = long(s)
            screen = conn.getObject("Screen", screenId)
        except ValueError:
            pass
        # if not, create one
        if screen is None:
            newscreen = omero.model.ScreenI()
            newscreen.name = rstring(s)
            newscreen = updateService.saveAndReturnObject(newscreen)
            screen = conn.getObject("Screen", newscreen.id.val)

    plates = []
    links = []
    for wellId in IDs:
        plate, link = well_fields_to_plate(conn, scriptParams,
                                                     wellId, screen)
        if plate is not None:
            plates.append(plate)
        if link is not None:
            links.append(link)

    if newscreen:
        message += "New screen created: %s." % newscreen.getName().val
        robj = newscreen
    elif plates:
        robj = plates[0]
    else:
        robj = None

    if plates:
        if len(plates) == 1:
            message += " New plate created: %s" % plates[0].name.val
        else:
            message += " %s plates created" % len(plates)
        if len(plates) == len(links):
            message += "."
        else:
            message += " but was not attached."
    else:
        message += "No plate created."
    return robj, message
Beispiel #34
0
def writeMovie(commandArgs, conn):
    """
    Makes the movie.
    
    @ returns        Returns the file annotation
    """
    log("Movie created by OMERO")
    log("")

    message = ""

    conn.SERVICE_OPTS.setOmeroGroup('-1')
    session = conn.c.sf
    gateway = conn
    scriptService = session.getScriptService()
    queryService = session.getQueryService()
    updateService = session.getUpdateService()
    rawFileStore = session.createRawFileStore()

    # Get the images
    images, logMessage = scriptUtil.getObjects(conn, commandArgs)
    message += logMessage
    if not images:
        return None, message
    omeroImage = images[
        0]  # Get the first valid image (should be expanded to process the list)

    if commandArgs["RenderingDef_ID"] >= 0:
        omeroImage._prepareRenderingEngine(rdid=commandArgs["RenderingDef_ID"])
    pixels = omeroImage.getPrimaryPixels()
    pixelsId = pixels.getId()

    sizeX = pixels.getSizeX()
    sizeY = pixels.getSizeY()
    sizeZ = pixels.getSizeZ()
    sizeC = pixels.getSizeC()
    sizeT = pixels.getSizeT()

    if (sizeX == None or sizeY == None or sizeZ == None or sizeT == None
            or sizeC == None):
        return

    if (pixels.getPhysicalSizeX() == None):
        commandArgs["Scalebar"] = 0

    cRange = range(0, sizeC)
    if "Channels" in commandArgs and validChannels(commandArgs["Channels"],
                                                   sizeC):
        cRange = commandArgs["Channels"]

    tzList = calculateRanges(sizeZ, sizeT, commandArgs)

    timeMap = calculateAquisitionTime(conn, pixelsId, cRange, tzList)
    if (timeMap == None):
        commandArgs["Show_Time"] = False
    if (timeMap != None):
        if (len(timeMap) == 0):
            commandArgs["Show_Time"] = False

    pixelTypeString = pixels.getPixelsType().getValue()
    frameNo = 1
    omeroImage.setActiveChannels(map(lambda x: x + 1, cRange))
    renderingEngine = omeroImage._re

    overlayColour = (255, 255, 255)
    if "Overlay_Colour" in commandArgs:
        r, g, b, a = COLOURS[commandArgs["Overlay_Colour"]]
        overlayColour = (r, g, b)

    canvasColour = tuple(COLOURS[commandArgs["Canvas_Colour"]][:3])
    mw = commandArgs["Min_Width"]
    if mw < sizeX:
        mw = sizeX
    mh = commandArgs["Min_Height"]
    if mh < sizeY:
        mh = sizeY
    ovlpos = None
    canvas = None
    if sizeX < mw or sizeY < mh:
        ovlpos = ((mw - sizeX) / 2, (mh - sizeY) / 2)
        canvas = Image.new("RGBA", (mw, mh), canvasColour)

    format = commandArgs["Format"]
    fileNames = []

    # add intro...
    if "Intro_Slide" in commandArgs and commandArgs["Intro_Slide"].id:
        intro_duration = commandArgs["Intro_Duration"]
        intro_fileId = commandArgs["Intro_Slide"].id.val
        intro_filenames = write_intro_end_slides(conn, commandArgs,
                                                 intro_fileId, intro_duration,
                                                 mw, mh)
        fileNames.extend(intro_filenames)

    # prepare watermark
    if "Watermark" in commandArgs and commandArgs["Watermark"].id:
        watermark = prepareWatermark(conn, commandArgs, mw, mh)

    # add movie frames...
    for tz in tzList:
        t = tz[0]
        z = tz[1]
        plane = getPlane(renderingEngine, z, t)
        planeImage = numpy.array(plane, dtype='uint32')
        planeImage = planeImage.byteswap()
        planeImage = planeImage.reshape(sizeX, sizeY)
        image = Image.frombuffer('RGBA', (sizeX, sizeY), planeImage.data,
                                 'raw', 'ARGB', 0, 1)
        if ovlpos is not None:
            image2 = canvas.copy()
            image2.paste(image, ovlpos, image)
            image = image2

        if "Scalebar" in commandArgs and commandArgs["Scalebar"]:
            image = addScalebar(commandArgs["Scalebar"], image, pixels,
                                commandArgs)
        planeInfo = "z:" + str(z) + "t:" + str(t)
        if "Show_Time" in commandArgs and commandArgs["Show_Time"]:
            time = timeMap[planeInfo]
            image = addTimePoints(time, pixels, image, overlayColour)
        if "Show_Plane_Info" in commandArgs and commandArgs["Show_Plane_Info"]:
            image = addPlaneInfo(z, t, pixels, image, overlayColour)
        if "Watermark" in commandArgs and commandArgs["Watermark"].id:
            image = pasteWatermark(image, watermark)
        if format == QT:
            filename = str(frameNo) + '.png'
            image.save(filename, "PNG")
        else:
            filename = str(frameNo) + '.jpg'
            image.save(filename, "JPEG")
        fileNames.append(filename)
        frameNo += 1

    # add exit frames... "outro"
    # add intro...
    if "Ending_Slide" in commandArgs and commandArgs["Ending_Slide"].id:
        end_duration = commandArgs["Ending_Duration"]
        end_fileId = commandArgs["Ending_Slide"].id.val
        end_filenames = write_intro_end_slides(conn, commandArgs, end_fileId,
                                               end_duration, mw, mh)
        fileNames.extend(end_filenames)

    filelist = ",".join(fileNames)

    ext = formatMap[format]
    movieName = "Movie"
    if "Movie_Name" in commandArgs:
        movieName = commandArgs["Movie_Name"]
    if not movieName.endswith(".%s" % ext):
        movieName = "%s.%s" % (movieName, ext)

    movieName = re.sub("[$&\;|\(\)<>' ]", "",
                       movieName)  # spaces etc in file name cause problems
    framesPerSec = 2
    if "FPS" in commandArgs:
        framesPerSec = commandArgs["FPS"]
    buildAVI(mw, mh, filelist, framesPerSec, movieName, format)
    figLegend = "\n".join(logLines)
    mimetype = formatMimetypes[format]

    if not os.path.exists(movieName):
        print "mencoder Failed to create movie file: %s" % movieName
        return None, "Failed to create movie file: %s" % movieName
    if not commandArgs["Do_Link"]:
        originalFile = scriptUtil.createFile(updateService, movieName,
                                             mimetype, movieName)
        scriptUtil.uploadFile(rawFileStore, originalFile, movieName)
        return originalFile, message

    namespace = omero.constants.namespaces.NSCREATED + "/omero/export_scripts/Make_Movie"
    fileAnnotation, annMessage = scriptUtil.createLinkFileAnnotation(
        conn,
        movieName,
        omeroImage,
        output="Movie",
        ns=namespace,
        mimetype=mimetype)
    message += annMessage
    return fileAnnotation._obj, message
Beispiel #35
0
def batchImageExport(conn, scriptParams):

    # for params with default values, we can get the value directly
    splitCs = scriptParams["Export_Individual_Channels"]
    mergedCs = scriptParams["Export_Merged_Image"]
    greyscale = scriptParams["Individual_Channels_Grey"]
    dataType = scriptParams["Data_Type"]
    folder_name = scriptParams["Folder_Name"]
    folder_name = os.path.basename(folder_name)
    format = scriptParams["Format"]
    projectZ = "Choose_Z_Section" in scriptParams and scriptParams["Choose_Z_Section"] == "Max projection"

    if (not splitCs) and (not mergedCs):
        log("Not chosen to save Individual Channels OR Merged Image")
        return

    # check if we have these params
    channelNames = []
    if "Channel_Names" in scriptParams:
        channelNames = scriptParams["Channel_Names"]
    zoomPercent = None
    if "Zoom" in scriptParams and scriptParams["Zoom"] != "100%":
        zoomPercent = int(scriptParams["Zoom"][:-1])

    # functions used below for each imaage.
    def getZrange(sizeZ, scriptParams):
        zRange = None
        if "Choose_Z_Section" in scriptParams:
            zChoice = scriptParams["Choose_Z_Section"]
            # NB: all Z indices in this script are 1-based
            if zChoice == "ALL Z planes":
                zRange = (1, sizeZ + 1)
            elif "OR_specify_Z_index" in scriptParams:
                zIndex = scriptParams["OR_specify_Z_index"]
                zIndex = min(zIndex, sizeZ)
                zRange = (zIndex,)
            elif "OR_specify_Z_start_AND..." in scriptParams and "...specify_Z_end" in scriptParams:
                start = scriptParams["OR_specify_Z_start_AND..."]
                start = min(start, sizeZ)
                end = scriptParams["...specify_Z_end"]
                end = min(end, sizeZ)
                # in case user got zStart and zEnd mixed up
                zStart = min(start, end)
                zEnd = max(start, end)
                if zStart == zEnd:
                    zRange = (zStart,)
                else:
                    zRange = (zStart, zEnd + 1)
        return zRange

    def getTrange(sizeT, scriptParams):
        tRange = None
        if "Choose_T_Section" in scriptParams:
            tChoice = scriptParams["Choose_T_Section"]
            # NB: all T indices in this script are 1-based
            if tChoice == "ALL T planes":
                tRange = (1, sizeT + 1)
            elif "OR_specify_T_index" in scriptParams:
                tIndex = scriptParams["OR_specify_T_index"]
                tIndex = min(tIndex, sizeT)
                tRange = (tIndex,)
            elif "OR_specify_T_start_AND..." in scriptParams and "...specify_T_end" in scriptParams:
                start = scriptParams["OR_specify_T_start_AND..."]
                start = min(start, sizeT)
                end = scriptParams["...specify_T_end"]
                end = min(end, sizeT)
                # in case user got zStart and zEnd mixed up
                tStart = min(start, end)
                tEnd = max(start, end)
                if tStart == tEnd:
                    tRange = (tStart,)
                else:
                    tRange = (tStart, tEnd + 1)
        return tRange

    # Get the images or datasets
    message = ""
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    # Attach figure to the first image
    parent = objects[0]

    if dataType == "Dataset":
        images = []
        for ds in objects:
            images.extend(list(ds.listChildren()))
        if not images:
            message += "No image found in dataset(s)"
            return None, message
    else:
        images = objects

    log("Processing %s images" % len(images))

    # somewhere to put images
    curr_dir = os.getcwd()
    exp_dir = os.path.join(curr_dir, folder_name)
    try:
        os.mkdir(exp_dir)
    except:
        pass
    # max size (default 12kx12k)
    size = conn.getDownloadAsMaxSizeSetting()
    size = int(size)

    ids = []
    # do the saving to disk

    for img in images:
        pixels = img.getPrimaryPixels()
        if pixels.getId() in ids:
            continue
        ids.append(pixels.getId())
        sizeX = pixels.getSizeX()
        sizeY = pixels.getSizeY()
        if sizeX * sizeY > size:
            log("  ** Can't export a 'Big' image to %s. **" % format)
            if len(images) == 1:
                return None, "Can't export a 'Big' image to %s." % format
            continue
        else:
            log("Exporting image as %s: %s" % (format, img.getName()))

        if format == "OME-TIFF":
            saveAsOmeTiff(conn, img, folder_name)
        else:
            if img._prepareRE().requiresPixelsPyramid():
                log("  ** Can't export a 'Big' image to OME-TIFF. **")
            log("\n----------- Saving planes from image: '%s' ------------" % img.getName())
            sizeC = img.getSizeC()
            sizeZ = img.getSizeZ()
            sizeT = img.getSizeT()
            zRange = getZrange(sizeZ, scriptParams)
            tRange = getTrange(sizeT, scriptParams)
            log("Using:")
            if zRange is None:
                log("  Z-index: Last-viewed")
            elif len(zRange) == 1:
                log("  Z-index: %d" % zRange[0])
            else:
                log("  Z-range: %s-%s" % (zRange[0], zRange[1] - 1))
            if projectZ:
                log("  Z-projection: ON")
            if tRange is None:
                log("  T-index: Last-viewed")
            elif len(tRange) == 1:
                log("  T-index: %d" % tRange[0])
            else:
                log("  T-range: %s-%s" % (tRange[0], tRange[1] - 1))
            log("  Format: %s" % format)
            if zoomPercent is None:
                log("  Image Zoom: 100%")
            else:
                log("  Image Zoom: %s" % zoomPercent)
            log("  Greyscale: %s" % greyscale)
            log("Channel Rendering Settings:")
            for ch in img.getChannels():
                log("  %s: %d-%d" % (ch.getLabel(), ch.getWindowStart(), ch.getWindowEnd()))

            try:
                savePlanesForImage(
                    conn,
                    img,
                    sizeC,
                    splitCs,
                    mergedCs,
                    channelNames,
                    zRange,
                    tRange,
                    greyscale,
                    zoomPercent,
                    projectZ=projectZ,
                    format=format,
                    folder_name=folder_name,
                )
            finally:
                # Make sure we close Rendering Engine
                img._re.close()

        # write log for exported images (not needed for ome-tiff)
        logFile = open(os.path.join(exp_dir, "Batch_Image_Export.txt"), "w")
        try:
            for s in logStrings:
                logFile.write(s)
                logFile.write("\n")
        finally:
            logFile.close()

    if len(os.listdir(exp_dir)) == 0:
        return None, "No files exported. See 'info' for more details"
    # zip everything up (unless we've only got a single ome-tiff)
    if format == "OME-TIFF" and len(os.listdir(exp_dir)) == 1:
        ometiffIds = [t.id for t in parent.listAnnotations(ns=NSOMETIFF)]
        print "Deleting OLD ome-tiffs: %s" % ometiffIds
        conn.deleteObjects("Annotation", ometiffIds)
        export_file = os.path.join(folder_name, os.listdir(exp_dir)[0])
        namespace = NSOMETIFF
        outputDisplayName = "OME-TIFF"
        mimetype = "image/tiff"
    else:
        export_file = "%s.zip" % folder_name
        compress(export_file, folder_name)
        mimetype = "application/zip"
        outputDisplayName = "Batch export zip"
        namespace = NSCREATED + "/omero/export_scripts/Batch_Image_Export"

    fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(
        conn, export_file, parent, output=outputDisplayName, ns=namespace, mimetype=mimetype
    )
    message += annMessage
    return fileAnnotation, message
def batchImageExport(conn, scriptParams):

    # for params with default values, we can get the value directly
    splitCs = scriptParams["Export_Individual_Channels"]
    mergedCs = scriptParams["Export_Merged_Image"]
    greyscale = scriptParams["Individual_Channels_Grey"]
    dataType = scriptParams["Data_Type"]
    folder_name = scriptParams["Folder_Name"]
    folder_name = os.path.basename(folder_name)
    format = scriptParams["Format"]
    projectZ = "Choose_Z_Section" in scriptParams and \
        scriptParams["Choose_Z_Section"] == 'Max projection'

    if (not splitCs) and (not mergedCs):
        log("Not chosen to save Individual Channels OR Merged Image")
        return

    # check if we have these params
    channelNames = []
    if "Channel_Names" in scriptParams:
        channelNames = scriptParams["Channel_Names"]
    zoomPercent = None
    if "Zoom" in scriptParams and scriptParams["Zoom"] != "100%":
        zoomPercent = int(scriptParams["Zoom"][:-1])

    # functions used below for each imaage.
    def getZrange(sizeZ, scriptParams):
        zRange = None
        if "Choose_Z_Section" in scriptParams:
            zChoice = scriptParams["Choose_Z_Section"]
            # NB: all Z indices in this script are 1-based
            if zChoice == 'ALL Z planes':
                zRange = (1, sizeZ+1)
            elif "OR_specify_Z_index" in scriptParams:
                zIndex = scriptParams["OR_specify_Z_index"]
                zIndex = min(zIndex, sizeZ)
                zRange = (zIndex,)
            elif "OR_specify_Z_start_AND..." in scriptParams and \
                    "...specify_Z_end" in scriptParams:
                start = scriptParams["OR_specify_Z_start_AND..."]
                start = min(start, sizeZ)
                end = scriptParams["...specify_Z_end"]
                end = min(end, sizeZ)
                # in case user got zStart and zEnd mixed up
                zStart = min(start, end)
                zEnd = max(start, end)
                if zStart == zEnd:
                    zRange = (zStart,)
                else:
                    zRange = (zStart, zEnd+1)
        return zRange

    def getTrange(sizeT, scriptParams):
        tRange = None
        if "Choose_T_Section" in scriptParams:
            tChoice = scriptParams["Choose_T_Section"]
            # NB: all T indices in this script are 1-based
            if tChoice == 'ALL T planes':
                tRange = (1, sizeT+1)
            elif "OR_specify_T_index" in scriptParams:
                tIndex = scriptParams["OR_specify_T_index"]
                tIndex = min(tIndex, sizeT)
                tRange = (tIndex,)
            elif "OR_specify_T_start_AND..." in scriptParams and \
                    "...specify_T_end" in scriptParams:
                start = scriptParams["OR_specify_T_start_AND..."]
                start = min(start, sizeT)
                end = scriptParams["...specify_T_end"]
                end = min(end, sizeT)
                # in case user got zStart and zEnd mixed up
                tStart = min(start, end)
                tEnd = max(start, end)
                if tStart == tEnd:
                    tRange = (tStart,)
                else:
                    tRange = (tStart, tEnd+1)
        return tRange

    # Get the images or datasets
    message = ""
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    # Attach figure to the first image
    parent = objects[0]

    if dataType == 'Dataset':
        images = []
        for ds in objects:
            images.extend(list(ds.listChildren()))
        if not images:
            message += "No image found in dataset(s)"
            return None, message
    else:
        images = objects

    log("Processing %s images" % len(images))

    # somewhere to put images
    curr_dir = os.getcwd()
    exp_dir = os.path.join(curr_dir, folder_name)
    try:
        os.mkdir(exp_dir)
    except:
        pass
    # max size (default 12kx12k)
    size = conn.getDownloadAsMaxSizeSetting()
    size = int(size)

    ids = []
    # do the saving to disk

    for img in images:
        pixels = img.getPrimaryPixels()
        if (pixels.getId() in ids):
            continue
        ids.append(pixels.getId())
        sizeX = pixels.getSizeX()
        sizeY = pixels.getSizeY()
        if sizeX*sizeY > size:
            log("  ** Can't export a 'Big' image to %s. **" % format)
            if len(images) == 1:
                return None, "Can't export a 'Big' image to %s." % format
            continue
        else:
            log("Exporting image as %s: %s" % (format, img.getName()))

        if format == 'OME-TIFF':
            saveAsOmeTiff(conn, img, folder_name)
        else:
            if img._prepareRE().requiresPixelsPyramid():
                log("  ** Can't export a 'Big' image to OME-TIFF. **")
            log("\n----------- Saving planes from image: '%s' ------------"
                % img.getName())
            sizeC = img.getSizeC()
            sizeZ = img.getSizeZ()
            sizeT = img.getSizeT()
            zRange = getZrange(sizeZ, scriptParams)
            tRange = getTrange(sizeT, scriptParams)
            log("Using:")
            if zRange is None:
                log("  Z-index: Last-viewed")
            elif len(zRange) == 1:
                log("  Z-index: %d" % zRange[0])
            else:
                log("  Z-range: %s-%s" % (zRange[0], zRange[1]-1))
            if projectZ:
                log("  Z-projection: ON")
            if tRange is None:
                log("  T-index: Last-viewed")
            elif len(tRange) == 1:
                log("  T-index: %d" % tRange[0])
            else:
                log("  T-range: %s-%s" % (tRange[0], tRange[1]-1))
            log("  Format: %s" % format)
            if zoomPercent is None:
                log("  Image Zoom: 100%")
            else:
                log("  Image Zoom: %s" % zoomPercent)
            log("  Greyscale: %s" % greyscale)
            log("Channel Rendering Settings:")
            for ch in img.getChannels():
                log("  %s: %d-%d"
                    % (ch.getLabel(), ch.getWindowStart(), ch.getWindowEnd()))

            try:
                savePlanesForImage(
                    conn, img, sizeC, splitCs, mergedCs, channelNames, zRange,
                    tRange, greyscale, zoomPercent, projectZ=projectZ,
                    format=format, folder_name=folder_name)
            finally:
                # Make sure we close Rendering Engine
                img._re.close()

        # write log for exported images (not needed for ome-tiff)
        logFile = open(os.path.join(exp_dir, 'Batch_Image_Export.txt'), 'w')
        try:
            for s in logStrings:
                logFile.write(s)
                logFile.write("\n")
        finally:
            logFile.close()

    if len(os.listdir(exp_dir)) == 0:
        return None, "No files exported. See 'info' for more details"
    # zip everything up (unless we've only got a single ome-tiff)
    if format == 'OME-TIFF' and len(os.listdir(exp_dir)) == 1:
        ometiffIds = [t.id for t in parent.listAnnotations(ns=NSOMETIFF)]
        print "Deleting OLD ome-tiffs: %s" % ometiffIds
        conn.deleteObjects("Annotation", ometiffIds)
        export_file = os.path.join(folder_name, os.listdir(exp_dir)[0])
        namespace = NSOMETIFF
        outputDisplayName = "OME-TIFF"
        mimetype = 'image/tiff'
    else:
        export_file = "%s.zip" % folder_name
        compress(export_file, folder_name)
        mimetype = 'application/zip'
        outputDisplayName = "Batch export zip"
        namespace = NSCREATED + "/omero/export_scripts/Batch_Image_Export"

    fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(
        conn, export_file, parent, output=outputDisplayName, ns=namespace,
        mimetype=mimetype)
    message += annMessage
    return fileAnnotation, message
Beispiel #37
0
def weka_segmentation(conn, scriptParams, uuid):


    model = scriptParams["Segmentation_model"]
    big_mem = scriptParams["Big_memory_nodes"] #TODO: implement big memory nodes

    model_path=model

    user = conn.getUser()
    user = user.getName()
    # print("user: %s" % (user))

    # print(model_path)

    images, logMessage = script_utils.getObjects(conn, scriptParams)
    if not images:
        return None, None, logMessage
    imageIds = [i.getId() for i in images]

    for iId in imageIds:

        tmpdir_stack = tempfile.mkdtemp(dir=cache_dir)
        tmpdir_out = tempfile.mkdtemp(dir=cache_dir)

        image = conn.getObject("Image", iId)
        dataset = image.getParent().getId()

        sizeZ = image.getSizeZ()
        print(sizeZ)

        # job_liner=[]

        all_jobs = open("%s.job" % (os.path.join(cache_dir, tmpdir_out.split('/')[-1])), 'w+')

        for z in range(sizeZ):
            plane = image.renderImage(z, 0)
            img_path = os.path.join(tmpdir_stack, "plane_%02d.tiff" % z)
            plane.save(img_path)

            ijmacro_args = "%s*%s/*%s" % (img_path, tmpdir_out, model_path)

            img_path2 = os.path.join(tmpdir_out, "plane_%02d.tiff" % z)
            job_liner=("%s -a %s -Xmx2g -- -macro %s %s -batch:%s:0 \n" % (XVFBRUN_PATH, IMAGEJ_PATH, MACRO_PATH2, ijmacro_args, img_path2))
            all_jobs.writelines(job_liner)

        all_jobs.close()

        system = scriptParams["System"]
        wtime = scriptParams["Wall_time"]
        pmem = scriptParams["Private_memory"]

        pbs_file = "%s.pbs" % (os.path.join(cache_dir, tmpdir_out.split('/')[-1]))

        nodes = int(math.ceil(((2.00*sizeZ)+0.15*(2.00*sizeZ))/48))

        stack_args = "%s/" % (tmpdir_out)
        image_name = image.getName()
        qsub_cmd = ". %s %s %s %s %s %s %s %s %s %s %s %s > %s" % (PBS_GEN, user, dataset, image_name, uuid, MACRO_PATH, stack_args, tmpdir_out, wtime, pmem, all_jobs.name, nodes, pbs_file)

        print(qsub_cmd)
        os.system(qsub_cmd)

        enableKeepAlive_time = (72*60*60)
        conn.c.enableKeepAlive(enableKeepAlive_time)
        os.system("ssh %s '%s %s'" % (system, qsub_path, pbs_file))
Beispiel #38
0
def processImages(conn, scriptParams):

    lineWidth = scriptParams['Line_Width']
    newKymographs = []
    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

    for image in images:
        if image.getSizeT() == 1:
            print "Image: %s is not a movie (sizeT = 1)"\
                " - Can't create Kymograph" % image.getId()
            continue
        newImages = []      # kymographs derived from the current image.
        cNames = []
        colors = []
        for ch in image.getChannels():
            cNames.append(ch.getLabel())
            colors.append(ch.getColor().getRGB())

        sizeT = image.getSizeT()
        pixels = image.getPrimaryPixels()

        dataset = image.getDataset()
        if dataset is not None and not dataset.canLink():
            dataset = None

        roiService = conn.getRoiService()
        result = roiService.findByImage(image.getId(), None)

        # kymograph strategy - Using Line and Polyline ROIs:
        # NB: Use ALL time points unless >1 shape AND 'use_all_timepoints' =
        # False
        # If > 1 shape per time-point (per ROI), pick one!
        # 1 - Single line. Use this shape for all time points
        # 2 - Many lines. Use the first one to fix length. Subsequent lines to
        # update start and direction
        # 3 - Single polyline. Use this shape for all time points
        # 4 - Many polylines. Use the first one to fix length.
        for roi in result.rois:
            lines = {}          # map of theT: line
            polylines = {}      # map of theT: polyline
            for s in roi.copyShapes():
                theZ = s.getTheZ() and s.getTheZ().getValue() or 0
                theT = s.getTheT() and s.getTheT().getValue() or 0
                # TODO: Add some filter of shapes. E.g. text? / 'lines' only
                # etc.
                if type(s) == omero.model.LineI:
                    x1 = s.getX1().getValue()
                    x2 = s.getX2().getValue()
                    y1 = s.getY1().getValue()
                    y2 = s.getY2().getValue()
                    lines[theT] = {'theZ': theZ, 'x1': x1, 'y1': y1, 'x2': x2,
                                   'y2': y2}

                elif type(s) == omero.model.PolylineI:
                    points = pointsStringToXYlist(s.getPoints().getValue())
                    polylines[theT] = {'theZ': theZ, 'points': points}

            if len(lines) > 0:
                newImg = linesKymograph(
                    conn, scriptParams, image, lines, lineWidth, dataset)
                newImages.append(newImg)
                lines = []
            elif len(polylines) > 0:
                newImg = polyLineKymograph(
                    conn, scriptParams, image, polylines, lineWidth, dataset)
                newImages.append(newImg)
            else:
                print "ROI: %s had no lines or polylines" \
                    % roi.getId().getValue()

        # look-up the interval for each time-point
        tInterval = None
        infos = list(pixels.copyPlaneInfo(theC=0, theT=sizeT-1, theZ=0))
        if len(infos) > 0:
            duration = infos[0].deltaT
            print "duration", duration
            if sizeT == 1:
                tInterval = duration
            else:
                tInterval = duration/(sizeT-1)
        elif pixels.timeIncrement is not None:
            print "pixels.timeIncrement", pixels.timeIncrement
            tInterval = pixels.timeIncrement
        elif "Time_Increment" in scriptParams:
            tInterval = scriptParams["Time_Increment"]

        pixel_size = None
        if pixels.physicalSizeX is not None:
            pixel_size = pixels.physicalSizeX
        elif "Pixel_Size" in scriptParams:
            pixel_size = scriptParams['Pixel_Size']

        # Save channel names and colors for each new image
        for img in newImages:
            print "Applying channel Names:", cNames, " Colors:", colors
            for i, c in enumerate(img.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 = omero.rtypes.rint(r)
                cObj.green = omero.rtypes.rint(g)
                cObj.blue = omero.rtypes.rint(b)
                cObj.alpha = omero.rtypes.rint(255)
                conn.getUpdateService().saveObject(cObj)
            img.resetRDefs()  # reset based on colors above

            # If we know pixel sizes, set them on the new image
            if pixel_size is not None or tInterval is not None:
                px = conn.getQueryService().get("Pixels", img.getPixelsId())
                if pixel_size is not None:
                    px.setPhysicalSizeX(rdouble(pixel_size))
                if tInterval is not None:
                    t_per_pixel = tInterval / lineWidth
                    px.setPhysicalSizeY(rdouble(t_per_pixel))
                conn.getUpdateService().saveObject(px)
        newKymographs.extend(newImages)

    if not newKymographs:
        message += "No kymograph created. See 'Error' or 'Info' for details."
    else:
        if not dataset:
            linkMessage = " but could not be attached"
        else:
            linkMessage = ""

        if len(newImages) == 1:
            message += "New kymograph created%s: %s." \
                % (linkMessage, newImages[0].getName())
        elif len(newImages) > 1:
            message += "%s new kymographs created%s." \
                % (len(newImages), linkMessage)

    return newKymographs, message
Beispiel #39
0
def processImages(conn, scriptParams):
    """ 
    Process the script params to make a list of channel_offsets, then iterate through
    the images creating a new image from each with the specified channel offsets 
    """
    
    message =""
    
    # Get the images
    images, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not images:
        return None, None, message
    imageIds = [i.getId() for i in images]
    
    # Get the channel offsets
    channel_offsets = []
    for i in range(1, 5):
        pName = "Channel_%s" % i
        if scriptParams[pName]:
            index = i-1     # UI channel index is 1-based - we want 0-based
            x = "Channel%s_X_shift"%i in scriptParams and scriptParams["Channel%s_X_shift"%i] or 0
            y = "Channel%s_Y_shift"%i in scriptParams and scriptParams["Channel%s_Y_shift"%i] or 0
            z = "Channel%s_Z_shift"%i in scriptParams and scriptParams["Channel%s_Z_shift"%i] or 0
            channel_offsets.append({'index':index, 'x':x, 'y':y, 'z':z})
    
    print channel_offsets


    dataset = None
    if "New_Dataset_Name" in scriptParams:
        # create new Dataset...
        newDatasetName = scriptParams["New_Dataset_Name"]
        dataset = omero.gateway.DatasetWrapper(conn, obj=omero.model.DatasetI())
        dataset.setName(rstring(newDatasetName))
        dataset.save()
        # add to parent Project
        parentDs = images[0].getParent()
        project = parentDs is not None and parentDs.getParent() or None
        if project is not None and project.canLink():
            link = omero.model.ProjectDatasetLinkI()
            link.parent = omero.model.ProjectI(project.getId(), False)
            link.child = omero.model.DatasetI(dataset.getId(), False)
            conn.getUpdateService().saveAndReturnObject(link)

    # need to handle Datasets eventually - Just do images for now
    newImages = []
    links = []
    for iId in imageIds:
        newImg, link = newImageWithChannelOffsets(conn, iId, channel_offsets, dataset)
        if newImg is not None:
            newImages.append(newImg)
            if link is not None:
                links.append(link)
    
    if not newImages:
        message += "No image created."
    else:
        if len(newImages) == 1:
            if not link:
                linkMessage = " but could not be attached"
            else:
                linkMessage = ""
            message += "New image created%s: %s." % (linkMessage, newImages[0].getName())
        elif len(newImages) > 1:
            message += "%s new images created" % len(newImages)
            if not len(links) == len(newImages):
                message +=" but some of them could not be attached."
            else:
                message += "."

    return newImages, dataset, message
def run_processing(conn, script_params):
    """
    Collects params and starts the processing
    
    @param conn:          the BlitzGateWay connection
    @param script_params: the parameters collected from the script input
    """
    dataType = script_params["Data_Type"]
    updateService = conn.getUpdateService()

    file_anns = []
    message = ""

    if script_params['Convert_coordinates_to_nm']:
        cam_pix_size = script_params['Parent_Image_Pixel_Size']
    else:
        cam_pix_size = 1
    file_type = script_params['File_Type']
    rmax = script_params['Max_radius']

    file_ids = script_params['AnnotationIDs']

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, script_params)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    # Check for rectangular ROIs and filter images list
    images = [image for image in images if image.getROICount("Rect") > 0]
    if not images:
        message += "No rectangle ROI found."
        return None, message

    total_rois = sum([i.getROICount("Rect") for i in images])
    if total_rois > 10:
        message += "Cannot start batch processing - too many rois (maximum is 10)."
        return None, message

    image_ids = [i.getId() for i in images]

    ripley = []
    figures = []
    figure_ids = []
    for i, id in enumerate(image_ids):
        image = conn.getObject("Image", id)

        if not image:
            message = 'Could not find specified image'
            return message

        ann = conn.getObject("Annotation", file_ids[i])
        if not ann:
            message = 'Could not find specified annotation'
            return message

        path_to_ann = ann.getFile().getPath() + '/' + ann.getFile().getName()
        name, ext = os.path.splitext(path_to_ann)
        if ('txt' in ext) or ('csv' in ext):
            #download the localisations data file
            path_to_data = download_data(ann)

            #get all xy coords from the file
            coords = parse_sr_data(path_to_data, file_type, cam_pix_size)

            #determine the number of channels
            sizeC = len(coords)

            #get the regions of interest
            rectangles = get_rectangles(conn, id)

            #calculate the ripley function in each roi
            ripley_data, ripley_figure = process_data(conn, image, file_type,
                                                      sizeC, rectangles,
                                                      coords, rmax)
            ripley.append(ripley_data)
            figures.append(ripley_figure)
            figure_ids.append(ripley_figure.getId())

            create_containers(conn, image, ripley_figure)

            new_file_ann = attach_results(conn, ann, ripley_figure,
                                          ripley_data, sizeC, rectangles)

            if new_file_ann:
                file_anns.append(new_file_ann)

            if not file_anns:
                faMessage = "No Analysis files created. See 'Info' or 'Error' for"\
                    " more details"
            elif len(file_anns) > 1:
                faMessage = "Created %s csv (Excel) files" % len(file_anns)
            elif len(file_anns) == 1:
                faMessage = "Created a new csv (Excel) file and attached to image ID %s" \
                % ripley_figure.getId()

            # clean up
            delete_downloaded_data(ann)

            message += faMessage
        else:
            message = 'file annotation must be txt or csv'
            return message

    robj = (len(figures) > 0) and figures[0]._obj or None
    return robj, message
Beispiel #41
0
def makeThumbnailFigure(conn, scriptParams):
    """
    Makes the figure using the parameters in @scriptParams, attaches the
    figure to the parent Project/Dataset, and returns the file-annotation ID

    @ returns       Returns the id of the originalFileLink child. (ID object,
                    not value)
    """

    log("Thumbnail figure created by OMERO")
    log("")

    message = ""

    # Get the objects (images or datasets)
    objects, logMessage = scriptUtil.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, message

    # Get parent
    parent = None
    if "Parent_ID" in scriptParams and len(scriptParams["IDs"]) > 1:
        if scriptParams["Data_Type"] == "Image":
            parent = conn.getObject("Dataset", scriptParams["Parent_ID"])
        else:
            parent = conn.getObject("Project", scriptParams["Parent_ID"])

    if parent is None:
        parent = objects[0]  # Attach figure to the first object

    parentClass = parent.OMERO_CLASS
    log("Figure will be linked to %s%s: %s" %
        (parentClass[0].lower(), parentClass[1:], parent.getName()))

    tagIds = []
    if "Tag_IDs" in scriptParams:
        tagIds = scriptParams['Tag_IDs']
    if len(tagIds) == 0:
        tagIds = None

    showUntagged = False
    if (tagIds):
        showUntagged = scriptParams["Show_Untagged_Images"]

    thumbSize = scriptParams["Thumbnail_Size"]
    maxColumns = scriptParams["Max_Columns"]

    figHeight = 0
    figWidth = 0
    dsCanvases = []

    if scriptParams["Data_Type"] == "Dataset":
        for dataset in objects:
            log("Dataset: %s     ID: %d" %
                (dataset.getName(), dataset.getId()))
            images = list(dataset.listChildren())
            title = dataset.getName().decode('utf8')
            dsCanvas = paintDatasetCanvas(conn,
                                          images,
                                          title,
                                          tagIds,
                                          showUntagged,
                                          length=thumbSize,
                                          colCount=maxColumns)
            if dsCanvas is None:
                continue
            dsCanvases.append(dsCanvas)
            figHeight += dsCanvas.size[1]
            figWidth = max(figWidth, dsCanvas.size[0])
    else:
        imageCanvas = paintDatasetCanvas(conn,
                                         objects,
                                         "",
                                         tagIds,
                                         showUntagged,
                                         length=thumbSize,
                                         colCount=maxColumns)
        dsCanvases.append(imageCanvas)
        figHeight += imageCanvas.size[1]
        figWidth = max(figWidth, imageCanvas.size[0])

    if len(dsCanvases) == 0:
        message += "No figure created"
        return None, message

    figure = Image.new("RGB", (figWidth, figHeight), WHITE)
    y = 0
    for ds in dsCanvases:
        imgUtil.pasteImage(ds, figure, 0, y)
        y += ds.size[1]

    log("")
    figLegend = "\n".join(logLines)

    format = scriptParams["Format"]
    figureName = scriptParams["Figure_Name"]
    figureName = os.path.basename(figureName)
    output = "localfile"

    if format == 'PNG':
        output = output + ".png"
        figureName = figureName + ".png"
        figure.save(output, "PNG")
        mimetype = "image/png"
    elif format == 'TIFF':
        output = output + ".tiff"
        figureName = figureName + ".tiff"
        figure.save(output, "TIFF")
        mimetype = "image/tiff"
    else:
        output = output + ".jpg"
        figureName = figureName + ".jpg"
        figure.save(output)
        mimetype = "image/jpeg"

    namespace = NSCREATED + "/omero/figure_scripts/Thumbnail_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn,
        output,
        parent,
        output="Thumbnail figure",
        mimetype=mimetype,
        ns=namespace,
        desc=figLegend,
        origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
Beispiel #42
0
def dataset_to_platewell(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the datasets ID
    datasets, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    def has_images_linked_to_well(dataset):
        params = omero.sys.ParametersI()
        query = "select count(well) from Well as well "\
                "left outer join well.wellSamples as ws " \
                "left outer join ws.image as img "\
                "where img.id in (:ids)"
        params.addIds([i.getId() for i in dataset.listChildren()])
        n_wells = unwrap(conn.getQueryService().projection(
            query, params, conn.SERVICE_OPTS)[0])[0]
        if n_wells > 0:
            print "Dataset %s contains images linked to wells." \
                % dataset.getId()
            return True
        else:
            return False

    # Exclude datasets containing images already linked to a well
    nDatasets = len(datasets)
    datasets = [x for x in datasets if not has_images_linked_to_well(x)]
    if len(datasets) < nDatasets:
        message += "Excluded %s out of %s dataset(s). " \
            % (nDatasets - len(datasets), nDatasets)

    # Return if all input dataset are not found or excluded
    if not datasets:
        return message

    # Filter dataset IDs by permissions
    IDs = [ds.getId() for ds in datasets if ds.canLink()]
    if len(IDs) != len(datasets):
        permIDs = [str(ds.getId()) for ds in datasets if not ds.canLink()]
        message += "You do not have the permissions to add the images from"\
            " the dataset(s): %s." % ",".join(permIDs)
    if not IDs:
        return message

    plate = None
    newplate = None
    if "Plate" in scriptParams and len(scriptParams["Plate"]) > 0:
        sp = scriptParams["Plate"]
        # see if this is an ID of existing plate
        try:
            plateId = long(sp)
            plate = conn.getObject("Plate", plateId)
        except ValueError:
            pass
        # if not, create one
        if plate is None:
            newplate = omero.model.PlateI()
            newplate.name = rstring(sp)
            newplate.columnNamingConvention = rstring(
                str(scriptParams["Column_Names"]))
            # 'letter' or 'number'
            newplate.rowNamingConvention = rstring(
                str(scriptParams["Row_Names"]))
            newplate = updateService.saveAndReturnObject(newplate)
            plate = conn.getObject("Plate", newplate.id.val)

    deletes = []
    for datasetId in IDs:
        deleteHandle = dataset_to_well(conn, scriptParams, datasetId, plate.id)
        if deleteHandle is not None:
            deletes.append(deleteHandle)

    # wait for any deletes to finish
    for handle in deletes:
        cb = omero.callbacks.DeleteCallbackI(conn.c, handle)
        while True:  # ms
            if cb.block(100) is None:
                print "Waiting for delete"
            else:
                break
        err = handle.errors()
        if err > 0:
            print "Delete error", err
        else:
            print "Delete OK"

    if newplate:
        message += "New plate created: %s." % newplate.getName().val
        robj = newplate
    else:
        robj = None

    return robj, message
Beispiel #43
0
def datasets_to_plates(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the datasets ID
    datasets, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    def has_images_linked_to_well(dataset):
        params = omero.sys.ParametersI()
        query = "select count(well) from Well as well "\
                "left outer join well.wellSamples as ws " \
                "left outer join ws.image as img "\
                "where img.id in (:ids)"
        params.addIds([i.getId() for i in dataset.listChildren()])
        n_wells = unwrap(conn.getQueryService().projection(
            query, params, conn.SERVICE_OPTS)[0])[0]
        if n_wells > 0:
            print "Dataset %s contains images linked to wells." \
                % dataset.getId()
            return True
        else:
            return False

    # Exclude datasets containing images already linked to a well
    nDatasets = len(datasets)
    datasets = [x for x in datasets if not has_images_linked_to_well(x)]
    if len(datasets) < nDatasets:
        message += "Excluded %s out of %s dataset(s). " \
            % (nDatasets - len(datasets), nDatasets)

    # Return if all input dataset are not found or excluded
    if not datasets:
        return None, message

    # Filter dataset IDs by permissions
    IDs = [ds.getId() for ds in datasets if ds.canLink()]
    if len(IDs) != len(datasets):
        permIDs = [str(ds.getId()) for ds in datasets if not ds.canLink()]
        message += "You do not have the permissions to add the images from"\
            " the dataset(s): %s." % ",".join(permIDs)
    if not IDs:
        return None, message

    # find or create Screen if specified
    screen = None
    newscreen = None
    if "Screen" in scriptParams and len(scriptParams["Screen"]) > 0:
        s = scriptParams["Screen"]
        # see if this is ID of existing screen
        try:
            screenId = long(s)
            screen = conn.getObject("Screen", screenId)
        except ValueError:
            pass
        # if not, create one
        if screen is None:
            newscreen = omero.model.ScreenI()
            newscreen.name = rstring(s)
            newscreen = updateService.saveAndReturnObject(newscreen)
            screen = conn.getObject("Screen", newscreen.id.val)

    plates = []
    links = []
    deletes = []
    for datasetId in IDs:
        plate, link, deleteHandle = dataset_to_plate(conn, scriptParams,
                                                     datasetId, screen)
        if plate is not None:
            plates.append(plate)
        if link is not None:
            links.append(link)
        if deleteHandle is not None:
            deletes.append(deleteHandle)

    # wait for any deletes to finish
    for handle in deletes:
        cb = omero.callbacks.DeleteCallbackI(conn.c, handle)
        while True:  # ms
            if cb.block(100) is None:
                print "Waiting for delete"
            else:
                break
        err = handle.errors()
        if err > 0:
            print "Delete error", err
        else:
            print "Delete OK"

    if newscreen:
        message += "New screen created: %s." % newscreen.getName().val
        robj = newscreen
    elif plates:
        robj = plates[0]
    else:
        robj = None

    if plates:
        if len(plates) == 1:
            message += " New plate created: %s" % plates[0].name.val
        else:
            message += " %s plates created" % len(plates)
        if len(plates) == len(links):
            message += "."
        else:
            message += " but could not be attached."
    else:
        message += "No plate created."
    return robj, message
def processImages(client, conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, None, message

    # Concatenate images from datasets
    if scriptParams["Data_Type"] == "Image":
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    queryService = conn.getQueryService()
    roiService = conn.getRoiService()

    print "Showing X ; Y coordinates in micrometer"

    for image in images:

         print "---------- {0} ---------- ".format(image.getName())

         metadata = dict(image.loadOriginalMetadata()[1])
         
         params = ParametersI()
         params.addId(image.getId())
         
         roiResult = roiService.findByImage(image.getId(), None)

         for roi in roiResult.rois:
              for shape in roi.copyShapes():
                   if type(shape) != omero.model.PointI:
                        continue
                   
                   # From tem-hole-finder:XYpic2XYstage.m
                   # RotM=1e-9*[tt(1),tt(2);tt(3),tt(4)];
                   # Offset=1e-6*[tt(5),tt(6)];
                   # XYstageHoles=RotM*XYpixHolesOverview'+repmat(Offset',[1,l1]);

                   # TODO: Eval is of course not really safe...
                   tt = eval(metadata['Conversion matrix'])
                   
                   RotM = [x * 1e-9 for x in [tt[0], tt[1], tt[2], tt[3]]]
                   Offset = [x * 1e-6 for x in [tt[4], tt[5]]]

                   xRoi = int(shape.getCx().getValue())
                   yRoi = int(shape.getCy().getValue())

                   stageX = RotM[0] * xRoi + RotM[1] * yRoi + Offset[0]
                   stageY = RotM[2] * xRoi + RotM[3] * yRoi + Offset[1]
                   name = roi.getName().getValue() if roi.getName() is not None else "Unnamed"

                   print "{0} [ {1} ; {2} ] ".format(name, stageX * 1e6, stageY * 1e6)

         print "--------------------------------------"

    return "Finished calculating"
Beispiel #45
0
def processImages(conn, scriptParams):

    lineWidth = scriptParams['Line_Width']
    newKymographs = []
    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

    for image in images:
        if image.getSizeT() == 1:
            print "Image: %s is not a movie (sizeT = 1)"\
                " - Can't create Kymograph" % image.getId()
            continue
        newImages = []  # kymographs derived from the current image.
        cNames = []
        colors = []
        for ch in image.getChannels():
            cNames.append(ch.getLabel())
            colors.append(ch.getColor().getRGB())

        sizeT = image.getSizeT()
        pixels = image.getPrimaryPixels()

        dataset = image.getDataset()
        if dataset is not None and not dataset.canLink():
            dataset = None

        roiService = conn.getRoiService()
        result = roiService.findByImage(image.getId(), None)

        # kymograph strategy - Using Line and Polyline ROIs:
        # NB: Use ALL time points unless >1 shape AND 'use_all_timepoints' =
        # False
        # If > 1 shape per time-point (per ROI), pick one!
        # 1 - Single line. Use this shape for all time points
        # 2 - Many lines. Use the first one to fix length. Subsequent lines to
        # update start and direction
        # 3 - Single polyline. Use this shape for all time points
        # 4 - Many polylines. Use the first one to fix length.
        for roi in result.rois:
            lines = {}  # map of theT: line
            polylines = {}  # map of theT: polyline
            for s in roi.copyShapes():
                theZ = s.getTheZ() and s.getTheZ().getValue() or 0
                theT = s.getTheT() and s.getTheT().getValue() or 0
                # TODO: Add some filter of shapes. E.g. text? / 'lines' only
                # etc.
                if type(s) == omero.model.LineI:
                    x1 = s.getX1().getValue()
                    x2 = s.getX2().getValue()
                    y1 = s.getY1().getValue()
                    y2 = s.getY2().getValue()
                    lines[theT] = {
                        'theZ': theZ,
                        'x1': x1,
                        'y1': y1,
                        'x2': x2,
                        'y2': y2
                    }

                elif type(s) == omero.model.PolylineI:
                    points = pointsStringToXYlist(s.getPoints().getValue())
                    polylines[theT] = {'theZ': theZ, 'points': points}

            if len(lines) > 0:
                newImg = linesKymograph(conn, scriptParams, image, lines,
                                        lineWidth, dataset)
                newImages.append(newImg)
                lines = []
            elif len(polylines) > 0:
                newImg = polyLineKymograph(conn, scriptParams, image,
                                           polylines, lineWidth, dataset)
                newImages.append(newImg)
            else:
                print "ROI: %s had no lines or polylines" \
                    % roi.getId().getValue()

        # look-up the interval for each time-point
        tInterval = None
        infos = list(pixels.copyPlaneInfo(theC=0, theT=sizeT - 1, theZ=0))
        if len(infos) > 0:
            duration = infos[0].deltaT
            print "duration", duration
            if sizeT == 1:
                tInterval = duration
            else:
                tInterval = duration / (sizeT - 1)
        elif pixels.timeIncrement is not None:
            print "pixels.timeIncrement", pixels.timeIncrement
            tInterval = pixels.timeIncrement
        elif "Time_Increment" in scriptParams:
            tInterval = scriptParams["Time_Increment"]

        pixel_size = None
        if pixels.physicalSizeX is not None:
            pixel_size = pixels.physicalSizeX
        elif "Pixel_Size" in scriptParams:
            pixel_size = scriptParams['Pixel_Size']

        # Save channel names and colors for each new image
        for img in newImages:
            print "Applying channel Names:", cNames, " Colors:", colors
            for i, c in enumerate(img.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 = omero.rtypes.rint(r)
                cObj.green = omero.rtypes.rint(g)
                cObj.blue = omero.rtypes.rint(b)
                cObj.alpha = omero.rtypes.rint(255)
                conn.getUpdateService().saveObject(cObj)
            img.resetRDefs()  # reset based on colors above

            # If we know pixel sizes, set them on the new image
            if pixel_size is not None or tInterval is not None:
                px = conn.getQueryService().get("Pixels", img.getPixelsId())
                if pixel_size is not None:
                    px.setPhysicalSizeX(rdouble(pixel_size))
                if tInterval is not None:
                    t_per_pixel = tInterval / lineWidth
                    px.setPhysicalSizeY(rdouble(t_per_pixel))
                conn.getUpdateService().saveObject(px)
        newKymographs.extend(newImages)

    if not newKymographs:
        message += "No kymograph created. See 'Error' or 'Info' for details."
    else:
        if not dataset:
            linkMessage = " but could not be attached"
        else:
            linkMessage = ""

        if len(newImages) == 1:
            message += "New kymograph created%s: %s." \
                % (linkMessage, newImages[0].getName())
        elif len(newImages) > 1:
            message += "%s new kymographs created%s." \
                % (len(newImages), linkMessage)

    return newKymographs, message
Beispiel #46
0
def run_processing(conn, session, script_params):
    """
    Collects params and starts the processing
    
    @param conn:          the BlitzGateWay connection
    @param script_params: the parameters collected from the script input
    """

    global input_dir
    global output_dir

    input_dir = tempfile.mkdtemp(prefix='stitching_input')
    output_dir = tempfile.mkdtemp(prefix='stitching_output')

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

    message = ""

    dataType = script_params["Data_Type"]

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, script_params)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    image_ids = [i.getId() for i in images]
    if len(image_ids) > 10:
        message = 'Max number of datasets for batch exceeded (10)'
        return message

    new_images = []
    new_ids = []
    for image in conn.getObjects("Image", image_ids):
        # remove input and processed images
        empty_dir(input_dir)
        empty_dir(output_dir)
        theC = None
        if script_params['Single_Channel']:
            theC = script_params['Channel']

        theZ = None
        sizeZ = image.getSizeZ()
        if script_params['Single_Z']:
            theZ = script_params['Z_slice']
            sizeZ = 1
        if script_params['Range_Z']:
            zstart = script_params['Z_start']
            zstop = script_params['Z_stop']
            theZ = range(zstart, zstop + 1)
            sizeZ = len(theZ)

        # download the image
        image_names = download_tiles(conn, image, theC, theZ)

        results_file = str(image.getId()) + "_stitching.txt"

        stitching_args = (results_file, script_params['grid_x'],
                          script_params['grid_y'],
                          script_params['tile_overlap'],
                          script_params['fusion_method'],
                          script_params['regression_threshold'],
                          script_params['ave_displacement_threshold'],
                          script_params['abs_displacement_threshold'], sizeZ)

        # do the stitching using dockerized fiji
        new_image = run_stitching(conn, session, stitching_args)

        # if the new image is created
        # link it to the original omero image
        if new_image:
            create_containers(conn, image, new_image)
            new_images.append(new_image)
            new_ids.append(new_image.getId())

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

    if new_images:
        if len(new_images) > 1:
            message += "Created %s new images" % len(new_images)
        else:
            message += "Created a new image"
    else:
        message += "No image created"

    print(script_params['Email_Results'])
    # if new images were created email the omero user
    if script_params['Email_Results'] and new_images:
        email_results(conn, script_params, new_ids)

    # remove downloaded image and outputs
    shutil.rmtree(input_dir)
    shutil.rmtree(output_dir)

    robj = (len(new_images) > 0) and new_images[0]._obj or None
    return robj, message
Beispiel #47
0
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
Beispiel #48
0
def run_processing(conn, session, script_params):
    """
    Collects params and starts the processing
    
    @param conn:          the BlitzGateWay connection
    @param script_params: the parameters collected from the script input
    """

    global input_dir
    global output_dir

    input_dir = tempfile.mkdtemp(prefix='clearing_input')
    output_dir = tempfile.mkdtemp(prefix='clearing_output')

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

    message = ""

    dataType = script_params["Data_Type"]

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, script_params)
    message += logMessage
    if not objects:
        return None, message

    # Concatenate images from datasets
    if dataType == 'Image':
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    image_ids = [i.getId() for i in images]
    if len(image_ids) > 10:
        message = 'Max number of datasets for batch exceeded (10)'
        return message

    new_images = []
    new_ids = []
    for source in conn.getObjects("Image", image_ids):
        # remove input and processed images
        empty_dir(input_dir)
        empty_dir(output_dir)

        # get the polygon rois
        rects, polys = get_polygons_from_rois(conn, source)

        # download the image and write polys to metadata
        target, target_path = download_image(conn, source, polys)

        # run the clearing
        new_image = run_clearing(conn, session, source, target)

        # put images in datasets, datasets in projects
        create_containers(conn, source, new_image)

        if new_image:
            #             set_attributes(conn,image, new_image)
            new_images.append(new_image)
            new_ids.append(new_image.getId())

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

    if new_images:
        if len(new_images) > 1:
            message += "Created %s new images" % len(new_images)
        else:
            message += "Created a new image"
    else:
        message += "No image created"

    print script_params['Email_Results']
    if script_params['Email_Results'] and new_images:
        email_results(conn, script_params, new_ids)

    shutil.rmtree(input_dir)
    shutil.rmtree(output_dir)
    #
    robj = (len(new_images) > 0) and new_images[0]._obj or None
    return robj, message
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

    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, average x/t, speed(um/sec)"
        tableData = ""
        for roi in result.rois:
            for s in roi.copyShapes():
                if type(s) == omero.model.LineI:
                    tableData += "\nLine"
                    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
                    tableData += "\n"
                    tableData += ",".join([str(x) for x in (y1, x1, y2, x2, dy, dx, dxPerY, dxPerY, "")])
                    if micronsPerSec:
                        speed = dxPerY * micronsPerSec
                        tableData += "%s" % speed
            
                elif type(s) == omero.model.PolylineI:
                    tableData += "\nPolyline"
                    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))
                        tableData += "\n"
                        tableData += ",".join([str(x) for x in (y1, x1, y2, x2, dy, dx, dxPerY, avXperY, "")])
                        if micronsPerSec:
                            speed = dxPerY * micronsPerSec
                            tableData += "%s" % speed

        # write table data to csv...
        if len(tableData) > 0:
            tableString = "secsPerPixelY: %s" % secsPerPixelY
            tableString += '\nmicronsPerPixelX: %s' % micronsPerPixelX
            tableString += "\nmicronsPerSec: %s" % micronsPerSec
            tableString += "\n"
            tableString += colNames 
            tableString += tableData
            print tableString
            csvFileName = 'kymograph_velocities_%s.csv' % image.getId()
            csvFile = open(csvFileName, 'w')
            try:
                csvFile.write(tableString)
            finally:
                csvFile.close()

            fileAnn, faMessage = scriptUtil.createLinkFileAnnotation(conn, csvFileName, image, 
                output="Line Plot csv (Excel) file", mimetype="text/csv", desc=None)
            print fileAnn, faMessage
            if fileAnn:
                fileAnns.append(fileAnn)
        else:
            print "Found NO lines or polylines to analyse for Image"

    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
Beispiel #50
0
def processImages(conn, scriptParams):

    lineWidth = scriptParams['Line_Width']
    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

    for image in images:

        cNames = []
        colors = []
        for ch in image.getChannels():
            cNames.append(ch.getLabel())
            colors.append(ch.getColor().getRGB())

        sizeC = image.getSizeC()

        if 'Channels' in scriptParams:
            scriptParams['Channels'] = [
                i - 1 for i in scriptParams['Channels']
            ]
            # Convert user input from 1-based to 0-based
            for i in scriptParams['Channels']:
                print i, type(i)
        else:
            scriptParams['Channels'] = range(sizeC)

        # channelMinMax = []
        # for c in image.getChannels():
        #     minC = c.getWindowMin()
        #     maxC = c.getWindowMax()
        #     channelMinMax.append((minC, maxC))

        roiService = conn.getRoiService()
        result = roiService.findByImage(image.getId(), None)

        lines = []
        polylines = []

        for roi in result.rois:
            roiId = roi.getId().getValue()
            for s in roi.copyShapes():
                theZ = s.getTheZ() and s.getTheZ().getValue() or 0
                theT = s.getTheT() and s.getTheT().getValue() or 0
                # TODO: Add some filter of shapes. E.g. text? / 'lines' only
                # etc.
                if type(s) == omero.model.LineI:
                    x1 = s.getX1().getValue()
                    x2 = s.getX2().getValue()
                    y1 = s.getY1().getValue()
                    y2 = s.getY2().getValue()
                    lines.append({
                        'id': roiId,
                        'theT': theT,
                        'theZ': theZ,
                        'x1': x1,
                        'y1': y1,
                        'x2': x2,
                        'y2': y2
                    })

                elif type(s) == omero.model.PolylineI:
                    points = pointsStringToXYlist(s.getPoints().getValue())
                    polylines.append({
                        'id': roiId,
                        'theT': theT,
                        'theZ': theZ,
                        'points': points
                    })

        if len(lines) == 0 and len(polylines) == 0:
            print "Image: %s had no lines or polylines" % image.getId()
            continue

        # prepare column headers, including line-id if we are going to output
        # raw data.
        lineId = scriptParams['Sum_or_Average'] == 'Average, with raw data' \
            and 'Line, ' or ""
        colHeader = 'Image_ID, ROI_ID, Z, T, C, %sLine data %s of Line" \
            " Width %s\n' % (lineId, scriptParams['Sum_or_Average'],
                             scriptParams['Line_Width'])
        print 'colHeader', colHeader

        # prepare a csv file to write our data to...
        fileName = "Plot_Profile_%s.csv" % image.getId()
        try:
            f = open(fileName, 'w')
            f.write(colHeader)
            if len(lines) > 0:
                processLines(conn, scriptParams, image, lines, lineWidth, f)
            if len(polylines) > 0:
                processPolyLines(conn, scriptParams, image, polylines,
                                 lineWidth, f)
        finally:
            f.close()

        fileAnn, faMessage = scriptUtil.createLinkFileAnnotation(
            conn,
            fileName,
            image,
            output="Line Plot csv (Excel) file",
            mimetype="text/csv",
            desc=None)
        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
Beispiel #51
0
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("Rect") > 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
    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()
        iId = image.getId()
        imageDate = image.getAcquisitionDate()
        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()

    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))

    # the channels in the combined image,
    if "Merged_Channels" in commandArgs:
        # convert to 0-based
        mergedIndexes = [c-1 for c in commandArgs["Merged_Channels"]]
    else:
        mergedIndexes = range(sizeC)  # show all
    mergedIndexes.reverse()

    #  if no colours added, use existing rendering settings.
    mergedColours = {}
    # Actually, nicer to always use existing rendering settings.
    # if "Merged_Colours" in commandArgs:
    #     for i, c in enumerate(commandArgs["Merged_Colours"]):
    #         if c in COLOURS:
    #             mergedColours[i] = COLOURS[c]

    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 "Scalebar_Colour" in commandArgs:
        if commandArgs["Scalebar_Colour"] in OVERLAY_COLOURS:
            r, g, b, a = OVERLAY_COLOURS[commandArgs["Scalebar_Colour"]]
            overlayColour = (r, g, b)

    roiZoom = None
    if "Roi_Zoom" in commandArgs:
        roiZoom = float(commandArgs["Roi_Zoom"])
        if roiZoom == 0:
            roiZoom = None

    maxColumns = None
    if "Max_Columns" in commandArgs:
        maxColumns = commandArgs["Max_Columns"]

    showRoiDuration = False
    if "Show_ROI_Duration" in commandArgs:
        showRoiDuration = commandArgs["Show_ROI_Duration"]

    roiLabel = "FigureROI"
    if "Roi_Selection_Label" in commandArgs:
        roiLabel = commandArgs["Roi_Selection_Label"]

    spacer = (width/50) + 2

    print "showRoiDuration", showRoiDuration
    fig = getSplitView(
        conn, imageIds, pixelIds, mergedIndexes, mergedColours, width, height,
        imageLabels, spacer, algorithm, stepping, scalebar, overlayColour,
        roiZoom, maxColumns, showRoiDuration, roiLabel)

    # fig.show()        # bug-fixing only

    if fig is None:
        logMessage = "No figure produced"
        log("\n"+logMessage)
        message += logMessage
        return None, message
    figLegend = "\n".join(logStrings)

    # print figLegend    # bug fixing only
    format = commandArgs["Format"]

    figureName = "movieROIFigure"
    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/Movie_ROI_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn, output, omeroImage, output="Movie ROI figure",
        mimetype=mimetype, ns=namespace, desc=figLegend,
        origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
def datasets_to_plates(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the datasets ID
    datasets, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage

    def has_images_linked_to_well(dataset):
        params = omero.sys.ParametersI()
        query = "select count(well) from Well as well "\
                "left outer join well.wellSamples as ws " \
                "left outer join ws.image as img "\
                "where img.id in (:ids)"
        params.addIds([i.getId() for i in dataset.listChildren()])
        n_wells = unwrap(conn.getQueryService().projection(
            query, params, conn.SERVICE_OPTS)[0])[0]
        if n_wells > 0:
            print "Dataset %s contains images linked to wells." \
                % dataset.getId()
            return True
        else:
            return False

    # Exclude datasets containing images already linked to a well
    nDatasets = len(datasets)
    datasets = [x for x in datasets if not has_images_linked_to_well(x)]
    if len(datasets) < nDatasets:
        message += "Excluded %s out of %s dataset(s). " \
            % (nDatasets - len(datasets), nDatasets)

    # Return if all input dataset are not found or excluded
    if not datasets:
        return None, message

    # Filter dataset IDs by permissions
    IDs = [ds.getId() for ds in datasets if ds.canLink()]
    if len(IDs) != len(datasets):
        permIDs = [str(ds.getId()) for ds in datasets if not ds.canLink()]
        message += "You do not have the permissions to add the images from"\
            " the dataset(s): %s." % ",".join(permIDs)
    if not IDs:
        return None, message

    # find or create Screen if specified
    screen = None
    newscreen = None
    if "Screen" in scriptParams and len(scriptParams["Screen"]) > 0:
        s = scriptParams["Screen"]
        # see if this is ID of existing screen
        try:
            screenId = long(s)
            screen = conn.getObject("Screen", screenId)
        except ValueError:
            pass
        # if not, create one
        if screen is None:
            newscreen = omero.model.ScreenI()
            newscreen.name = rstring(s)
            newscreen = updateService.saveAndReturnObject(newscreen)
            screen = conn.getObject("Screen", newscreen.id.val)

    plates = []
    links = []
    deletes = []
    for datasetId in IDs:
        plate, link, deleteHandle = dataset_to_plate(conn, scriptParams,
                                                     datasetId, screen)
        if plate is not None:
            plates.append(plate)
        if link is not None:
            links.append(link)
        if deleteHandle is not None:
            deletes.append(deleteHandle)

    # wait for any deletes to finish
    for handle in deletes:
        cb = omero.callbacks.DeleteCallbackI(conn.c, handle)
        while True:  # ms
            if cb.block(100) is None:
                print "Waiting for delete"
            else:
                break
        err = handle.errors()
        if err > 0:
            print "Delete error", err
        else:
            print "Delete OK"

    if newscreen:
        message += "New screen created: %s." % newscreen.getName().val
        robj = newscreen
    elif plates:
        robj = plates[0]
    else:
        robj = None

    if plates:
        if len(plates) == 1:
            message += " New plate created: %s" % plates[0].name.val
        else:
            message += " %s plates created" % len(plates)
        if len(plates) == len(links):
            message += "."
        else:
            message += " but could not be attached."
    else:
        message += "No plate created."
    return robj, message
Beispiel #53
0
def processImages(conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""
    images, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not images:
        raise Exception("No images found")
    imageIds = sorted(set([i.getId() for i in images]))

    choice = scriptParams["Choice"]
    debug = bool(scriptParams.get("Debug", False))
    globalmin = defaultdict(list)
    globalmax = defaultdict(list)

    tb = TableBuilder("Context", "Channel", "Min", "Max")
    statsInfos = dict()
    for iId in imageIds:
        statsInfo = calcStatsInfo(conn, iId, choice, debug)
        statsInfos[iId] = statsInfo
        for c, si in sorted(statsInfo.items()):
            c_min, c_max = si
            globalmin[c].append(c_min)
            globalmax[c].append(c_max)
            tb.row("Image:%s" % iId, c, c_min, c_max)

    tb.row("", "", "", "")
    for c in globalmin:
        c_min = globalmin[c]
        c_max = globalmax[c]
        tb.row("Total: outer  ", c, min(c_min), max(c_max))
        tb.row("Total: inner  ", c, max(c_min), min(c_max))
        tb.row("Total: average", c, int(avg(c_min)), int(avg(c_max)))

    if scriptParams["DryRun"]:
        print str(tb.build())
    else:
        combine = scriptParams["Combine"]
        for iId in imageIds:
            img = conn.getObject("Image", iId)
            for c, ch in enumerate(img.getChannels(noRE=True)):
                si = ch.getStatsInfo()
                if si is None:
                    si = StatsInfoI()
                    action = "creating"
                else:
                    si = si._obj
                    action = "updating"

                if combine == "no":
                    si.globalMin = rdouble(statsInfos[iId][c][0])
                    si.globalMax = rdouble(statsInfos[iId][c][1])
                elif combine == "outer":
                    si.globalMin = rdouble(min(globalmin[c]))
                    si.globalMax = rdouble(max(globalmax[c]))
                elif combine == "inner":
                    si.globalMin = rdouble(max(globalmin[c]))
                    si.globalMax = rdouble(min(globalmax[c]))
                elif combine == "average":
                    si.globalMin = rdouble(avg(globalmin[c]))
                    si.globalMax = rdouble(avg(globalmax[c]))
                else:
                    raise Exception("unknown combine: %s" % combine)

                if debug:
                    print "Image:%s(c=%s) - %s StatsInfo(%s, %s)" % (
                        iId, c, action, si.globalMin.val, si.globalMax.val)
                ch._obj.statsInfo = si
                ch.save()

    count = sum(map(len, statsInfos.values()))
    message += "%s stats info object(s) processed" % count
    return message
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()
def movieFigure(conn, commandArgs):
    """
    Makes the figure using the parameters in @commandArgs, attaches the figure
    to the parent Project/Dataset, and returns the file-annotation ID

    @param session      The OMERO session
    @param commandArgs  Map of parameters for the script
    @ returns           Returns the id of the originalFileLink child. (ID
                        object, not value)
    """

    log("Movie figure created by OMERO on %s" % date.today())
    log("")

    timeLabels = {"SECS_MILLIS": "seconds",
                  "SECS": "seconds",
                  "MINS": "minutes",
                  "HOURS": "hours",
                  "MINS_SECS": "mins:secs",
                  "HOURS_MINS": "hours:mins"}
    timeUnits = "SECS"
    if "Time_Units" in commandArgs:
        timeUnits = commandArgs["Time_Units"]
        # convert from UI name to timeLabels key
        timeUnits = timeUnits.replace(" ", "_")
    if timeUnits not in timeLabels.keys():
        timeUnits = "SECS"
    log("Time units are in %s" % timeLabels[timeUnits])

    pixelIds = []
    imageIds = []
    imageLabels = []
    message = ""  # message to be returned to the client

    # 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

    # 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())

    # 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()
        iId = image.getId()
        imageDate = image.getAcquisitionDate()
        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()
    sizeT = omeroImage.getSizeT()

    tIndexes = []
    if "T_Indexes" in commandArgs:
        for t in commandArgs["T_Indexes"]:
            tIndexes.append(t)
        print "T_Indexes", tIndexes
    if len(tIndexes) == 0:      # if no t-indexes given, use all t-indices
        tIndexes = range(sizeT)

    zStart = -1
    zEnd = -1
    if "Z_Start" in commandArgs:
        zStart = commandArgs["Z_Start"]
    if "Z_End" in commandArgs:
        zEnd = commandArgs["Z_End"]

    width = sizeX
    if "Width" in commandArgs:
        width = commandArgs["Width"]

    height = sizeY
    if "Height" in commandArgs:
        height = commandArgs["Height"]

    spacer = (width/25) + 2

    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 "Scalebar_Colour" in commandArgs:
        r, g, b, a = OVERLAY_COLOURS[commandArgs["Scalebar_Colour"]]
        overlayColour = (r, g, b)

    maxColCount = 10
    if "Max_Columns" in commandArgs:
        maxColCount = commandArgs["Max_Columns"]

    figure = createMovieFigure(
        conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
        algorithm, stepping, scalebar, overlayColour, timeUnits, imageLabels,
        maxColCount)

    log("")
    figLegend = "\n".join(logLines)

    # print figLegend    # bug fixing only
    format = commandArgs["Format"]

    figureName = "movieFigure"
    if "Figure_Name" in commandArgs:
        figureName = str(commandArgs["Figure_Name"])
        figureName = os.path.basename(figureName)
    output = "localfile"
    if format == 'PNG':
        output = output + ".png"
        figureName = figureName + ".png"
        figure.save(output, "PNG")
        mimetype = "image/png"
    elif format == 'TIFF':
        output = output + ".tiff"
        figureName = figureName + ".tiff"
        figure.save(output, "TIFF")
        mimetype = "image/tiff"
    else:
        output = output + ".jpg"
        figureName = figureName + ".jpg"
        figure.save(output)
        mimetype = "image/jpeg"

    namespace = NSCREATED + "/omero/figure_scripts/Movie_Figure"
    fileAnnotation, faMessage = scriptUtil.createLinkFileAnnotation(
        conn, output, omeroImage, output="Movie figure", mimetype=mimetype,
        ns=namespace, desc=figLegend, origFilePathAndName=figureName)
    message += faMessage

    return fileAnnotation, message
def processImages(conn, scriptParams):

    lineWidth = scriptParams['Line_Width']
    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

    for image in images:

        cNames = []
        colors = []
        for ch in image.getChannels():
            cNames.append(ch.getLabel())
            colors.append(ch.getColor().getRGB())

        sizeC = image.getSizeC()

        if 'Channels' in scriptParams:
            scriptParams['Channels'] = [i-1 for i in scriptParams['Channels']]
            # Convert user input from 1-based to 0-based
            for i in scriptParams['Channels']:
                print i, type(i)
        else:
            scriptParams['Channels'] = range(sizeC)

        # channelMinMax = []
        # for c in image.getChannels():
        #     minC = c.getWindowMin()
        #     maxC = c.getWindowMax()
        #     channelMinMax.append((minC, maxC))

        roiService = conn.getRoiService()
        result = roiService.findByImage(image.getId(), None)

        lines = []
        polylines = []

        for roi in result.rois:
            roiId = roi.getId().getValue()
            for s in roi.copyShapes():
                theZ = s.getTheZ() and s.getTheZ().getValue() or 0
                theT = s.getTheT() and s.getTheT().getValue() or 0
                # TODO: Add some filter of shapes. E.g. text? / 'lines' only
                # etc.
                if type(s) == omero.model.LineI:
                    x1 = s.getX1().getValue()
                    x2 = s.getX2().getValue()
                    y1 = s.getY1().getValue()
                    y2 = s.getY2().getValue()
                    lines.append({'id': roiId, 'theT': theT, 'theZ': theZ,
                                  'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2})

                elif type(s) == omero.model.PolylineI:
                    points = pointsStringToXYlist(s.getPoints().getValue())
                    polylines.append({'id': roiId, 'theT': theT, 'theZ': theZ,
                                      'points': points})

        if len(lines) == 0 and len(polylines) == 0:
            print "Image: %s had no lines or polylines" % image.getId()
            continue

        # prepare column headers, including line-id if we are going to output
        # raw data.
        lineId = scriptParams['Sum_or_Average'] == 'Average, with raw data' \
            and 'Line, ' or ""
        colHeader = 'Image_ID, ROI_ID, Z, T, C, %sLine data %s of Line" \
            " Width %s\n' % (lineId, scriptParams['Sum_or_Average'],
                             scriptParams['Line_Width'])
        print 'colHeader', colHeader

        # prepare a csv file to write our data to...
        fileName = "Plot_Profile_%s.csv" % image.getId()
        try:
            f = open(fileName, 'w')
            f.write(colHeader)
            if len(lines) > 0:
                processLines(conn, scriptParams, image, lines, lineWidth, f)
            if len(polylines) > 0:
                processPolyLines(
                    conn, scriptParams, image, polylines, lineWidth, f)
        finally:
            f.close()

        fileAnn, faMessage = scriptUtil.createLinkFileAnnotation(
            conn, fileName, image, output="Line Plot csv (Excel) file",
            mimetype="text/csv", desc=None)
        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 writeMovie(commandArgs, conn):
    """
    Makes the movie.

    @ returns        Returns the file annotation
    """
    log("Movie created by OMERO")
    log("")

    message = ""

    session = conn.c.sf
    updateService = session.getUpdateService()
    rawFileStore = session.createRawFileStore()

    # Get the images
    images, logMessage = scriptUtil.getObjects(conn, commandArgs)
    message += logMessage
    if not images:
        return None, message
    # Get the first valid image (should be expanded to process the list)
    omeroImage = images[0]

    if commandArgs["RenderingDef_ID"] >= 0:
        omeroImage._prepareRenderingEngine(rdid=commandArgs["RenderingDef_ID"])
    pixels = omeroImage.getPrimaryPixels()
    pixelsId = pixels.getId()

    sizeX = pixels.getSizeX()
    sizeY = pixels.getSizeY()
    sizeZ = pixels.getSizeZ()
    sizeC = pixels.getSizeC()
    sizeT = pixels.getSizeT()

    if (sizeX is None or sizeY is None or sizeZ is None or sizeT is None or
            sizeC is None):
        return

    if (pixels.getPhysicalSizeX() is None):
        commandArgs["Scalebar"] = 0

    cRange = range(0, sizeC)
    cWindows = None
    cColours = None
    if "ChannelsExtended" in commandArgs and \
            validChannels(commandArgs["ChannelsExtended"], sizeC):
        cRange = []
        cWindows = []
        cColours = []
        for c in commandArgs["ChannelsExtended"]:
            m = re.match('^(?P<i>\d+)(\|(?P<ws>\d+)' +
                         '\:(?P<we>\d+))?(\$(?P<c>.+))?$', c)
            if m is not None:
                cRange.append(int(m.group('i'))-1)
                cWindows.append([float(m.group('ws')), float(m.group('we'))])
                cColours.append(m.group('c'))
    elif "Channels" in commandArgs and \
            validChannels(commandArgs["Channels"], sizeC):
        cRange = commandArgs["Channels"]

    tzList = calculateRanges(sizeZ, sizeT, commandArgs)

    timeMap = calculateAquisitionTime(conn, pixelsId, cRange, tzList)
    if (timeMap is None):
        commandArgs["Show_Time"] = False
    if (timeMap is not None):
        if (len(timeMap) == 0):
            commandArgs["Show_Time"] = False

    frameNo = 1
    omeroImage.setActiveChannels(map(lambda x: x+1, cRange),
                                 cWindows,
                                 cColours)
    renderingEngine = omeroImage._re

    overlayColour = (255, 255, 255)
    if "Overlay_Colour" in commandArgs:
        r, g, b, a = COLOURS[commandArgs["Overlay_Colour"]]
        overlayColour = (r, g, b)

    canvasColour = tuple(COLOURS[commandArgs["Canvas_Colour"]][:3])
    mw = commandArgs["Min_Width"]
    if mw < sizeX:
        mw = sizeX
    mh = commandArgs["Min_Height"]
    if mh < sizeY:
        mh = sizeY
    ovlpos = None
    canvas = None
    if sizeX < mw or sizeY < mh:
        ovlpos = ((mw-sizeX) / 2, (mh-sizeY) / 2)
        canvas = Image.new("RGBA", (mw, mh), canvasColour)

    format = commandArgs["Format"]
    fileNames = []

    # add intro...
    if "Intro_Slide" in commandArgs and commandArgs["Intro_Slide"].id:
        intro_duration = commandArgs["Intro_Duration"]
        intro_fileId = commandArgs["Intro_Slide"].id.val
        intro_filenames = write_intro_end_slides(
            conn, commandArgs, intro_fileId, intro_duration, mw, mh)
        fileNames.extend(intro_filenames)

    # prepare watermark
    if "Watermark" in commandArgs and commandArgs["Watermark"].id:
        watermark = prepareWatermark(conn, commandArgs, mw, mh)

    # add movie frames...
    for tz in tzList:
        t = tz[0]
        z = tz[1]
        plane = getPlane(renderingEngine, z, t)
        planeImage = numpy.array(plane, dtype='uint32')
        planeImage = planeImage.byteswap()
        planeImage = planeImage.reshape(sizeX, sizeY)
        image = Image.frombuffer('RGBA', (sizeX, sizeY), planeImage.data,
                                 'raw', 'ARGB', 0, 1)
        if ovlpos is not None:
            image2 = canvas.copy()
            image2.paste(image, ovlpos, image)
            image = image2

        if "Scalebar" in commandArgs and commandArgs["Scalebar"]:
            image = addScalebar(
                commandArgs["Scalebar"], image, pixels, commandArgs)
        planeInfo = "z:"+str(z)+"t:"+str(t)
        if "Show_Time" in commandArgs and commandArgs["Show_Time"]:
            time = timeMap[planeInfo]
            image = addTimePoints(time, pixels, image, overlayColour)
        if "Show_Plane_Info" in commandArgs and \
                commandArgs["Show_Plane_Info"]:
            image = addPlaneInfo(z, t, pixels, image, overlayColour)
        if "Watermark" in commandArgs and commandArgs["Watermark"].id:
            image = pasteWatermark(image, watermark)
        if format == QT:
            filename = str(frameNo) + '.png'
            image.save(filename, "PNG")
        else:
            filename = str(frameNo) + '.jpg'
            image.save(filename, "JPEG")
        fileNames.append(filename)
        frameNo += 1

    # add exit frames... "outro"
    # add intro...
    if "Ending_Slide" in commandArgs and commandArgs["Ending_Slide"].id:
        end_duration = commandArgs["Ending_Duration"]
        end_fileId = commandArgs["Ending_Slide"].id.val
        end_filenames = write_intro_end_slides(
            conn, commandArgs, end_fileId, end_duration, mw, mh)
        fileNames.extend(end_filenames)

    filelist = ",".join(fileNames)

    ext = formatMap[format]
    movieName = "Movie"
    if "Movie_Name" in commandArgs:
        movieName = commandArgs["Movie_Name"]
        movieName = os.path.basename(movieName)
    if not movieName.endswith(".%s" % ext):
        movieName = "%s.%s" % (movieName, ext)

    # spaces etc in file name cause problems
    movieName = re.sub("[$&\;|\(\)<>' ]", "", movieName)
    framesPerSec = 2
    if "FPS" in commandArgs:
        framesPerSec = commandArgs["FPS"]
    output = "localfile.%s" % ext
    buildAVI(mw, mh, filelist, framesPerSec, output, format)
    mimetype = formatMimetypes[format]

    if not os.path.exists(output):
        print "mencoder Failed to create movie file: %s" % output
        return None, "Failed to create movie file: %s" % output
    if not commandArgs["Do_Link"]:
        originalFile = scriptUtil.createFile(
            updateService, output, mimetype, movieName)
        scriptUtil.uploadFile(rawFileStore, originalFile, movieName)
        return originalFile, message

    namespace = NSCREATED + "/omero/export_scripts/Make_Movie"
    fileAnnotation, annMessage = scriptUtil.createLinkFileAnnotation(
        conn, output, omeroImage, ns=namespace,
        mimetype=mimetype, origFilePathAndName=movieName)
    message += annMessage
    return fileAnnotation._obj, message
Beispiel #58
0
def datasets_to_plates(conn, scriptParams):

    updateService = conn.getUpdateService()

    message = ""

    # Get the datasets ID
    datasets, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not datasets:
        return None, message
    IDs = [ds.getId() for ds in datasets]

    # find or create Screen if specified
    screen = None
    newscreen = None
    if "Screen" in scriptParams and len(scriptParams["Screen"]) > 0:
        s = scriptParams["Screen"]
        # see if this is ID of existing screen
        try:
            screenId = long(s)
            screen = conn.getObject("Screen", screenId)
        except ValueError:
            pass
        # if not, create one
        if screen is None:
            newscreen = omero.model.ScreenI()
            newscreen.name = rstring(s)
            newscreen = updateService.saveAndReturnObject(newscreen)
            screen = conn.getObject("Screen", newscreen.id.val)

    plates = []
    links = []
    deletes = []
    for datasetId in IDs:
        plate, link, deleteHandle = dataset_to_plate(conn, scriptParams,
                                                     datasetId, screen)
        if plate is not None:
            plates.append(plate)
        if link is not None:
            links.append(link)
        if deleteHandle is not None:
            deletes.append(deleteHandle)

    # wait for any deletes to finish
    for handle in deletes:
        cb = omero.callbacks.DeleteCallbackI(conn.c, handle)
        while True:  # ms
            if cb.block(100) is None:
                print "Waiting for delete"
            else:
                break
        err = handle.errors()
        if err > 0:
            print "Delete error", err
        else:
            print "Delete OK"

    if newscreen:
        message += "New screen created: %s." % newscreen.getName().val
        robj = newscreen
    elif plates:
        robj = plates[0]
    else:
        robj = None

    if plates:
        if len(plates) == 1:
            message += " New plate created: %s" % plates[0].name.val
        else:
            message += " %s plates created" % len(plates)
        if len(plates) == len(links):
            message += "."
        else:
            message += " but could not be attached."
    else:
        message += "No plate created."
    return robj, message