Пример #1
0
class Connection(object):
    """
    A wrapper for managing a client session context
    """

    def __init__(self, user = None, passwd = None, host = None, client = None):
        """
        Create a new client session, either by specifying user and passwd or by
        providing a client object (for scripts)
        @param user Username
        @param passwd Password
        @param host The server hostname
        @param client Client object with an active session
        """

        self.log = logging.getLogger(__name__)
        #self.log.setLevel(logging.DEBUG)

        if not client:
            client = omero.client(host)
            sess = client.createSession(user, passwd)
            client.enableKeepAlive(60)
        else:
            sess = client.getSession()

        self.conn = BlitzGateway(client_obj = client)

        self.res = sess.sharedResources()
        if (not self.res.areTablesEnabled()):
            raise TableConnectionError('OMERO.tables not enabled')

    def __enter__(self):
        self.log.debug('Entering Connection')
        return self

    def __exit__(self, type, value, traceback):
        self.log.debug('Exiting Connection')
        self.close()

    def close(self):
        """
        Child classes can override this method but must explcitly call this
        method to ensure the client session is cleaned
        """
        self.log.debug('Closing Connection')
        self.conn._closeSession()
Пример #2
0
halfRecovery = (recoveryValue + bleachValue)/2

# quick & dirty - pick the first timepoint where we exceed half recovery
recoveryValues = valueList[bleachTindex:]   # just the values & times after bleach time
recoveryTimes = timeList[bleachTindex:]
for t, v in zip(recoveryTimes, recoveryValues):
    if v >= halfRecovery:
        tHalf = t - bleachTime
        break

print "tHalf: %0.2f seconds" % tHalf


csvLines = [ 
    "Time (secs)," + ",".join([str(t) for t in timeList]),
    "\n",
    "Average pixel value," + ",".join([str(v) for v in valueList]),
    "\n",
    "tHalf (secs), %0.2f seconds" % tHalf,
    "mobileFraction, %0.2f" % mobileFraction
    ]

f = open("FRAP.csv", "w")
f.writelines(csvLines)
f.close()

# Close connection:
# =================================================================
# When you are done, close the session to free up server resources.
conn._closeSession()
Пример #3
0
print "\nProject:", project.getName()

# Delete Project
# =================================================================
# You can delete a number of objects of the same type at the same
# time. In this case 'Project'. Use deleteChildren=True if you are
# deleting a Project and you want to delete Datasets and Images.
obj_ids = [projectId]
deleteChildren = False
handle = conn.deleteObjects("Project", obj_ids,\
        deleteAnns=True, deleteChildren=deleteChildren)

# Retrieve callback and wait until delete completes
# =================================================================
# This is not necessary for the Delete to complete. Can be used
# if you want to know when delete is finished or if there were any errors
cb = omero.callbacks.CmdCallbackI(conn.c, handle)
print "Deleting, please wait."
while not cb.block(500):
    print "."
err = isinstance(cb.getResponse(), omero.cmd.ERR)
print "Error?", err
if err:
    print cb.getResponse()
cb.close(True)  # close handle too

# Close connection:
# =================================================================
# When you are done, close the session to free up server resources.
conn._closeSession()
Пример #4
0
class TableConnection(object):
    """
    A basic client-side wrapper for OMERO.tables which handles opening
    and closing tables.
    """

    def __init__(self, user = None, passwd = None, host = 'localhost',
                 client = None, tableName = None, tableId = None):
        """
        Create a new table handler, either by specifying user and passwd or by
        providing a client object (for scripts)
        @param user Username
        @param passwd Password
        @param host The server hostname
        @param client Client object with an active session
        @param tableName The name of the table file
        @param tableId The OriginalFile ID of the table file
        """
        if not client:
            client = omero.client(host)
            sess = client.createSession(user, passwd)
            client.enableKeepAlive(60)
        else:
             sess = client.getSession()

        self.conn = BlitzGateway(client_obj = client)

        self.res = sess.sharedResources()
        if (not self.res.areTablesEnabled()):
            raise TableConnectionError('OMERO.tables not enabled')

        repos = self.res.repositories()
        self.rid = repos.descriptions[0].id.val

        self.tableName = tableName
        self.tableId = tableId
        self.table = None

    def __enter__(self):
        print 'Entering Connection'
        return self

    def __exit__(self, type, value, traceback):
        print 'Exiting Connection'
        self.close()

    def close(self):
        print 'Closing Connection'
        try:
            self.closeTable()
        finally:
            self.conn._closeSession()


    def openTable(self, tableId = None, tableName = None):
        """
        Opens an existing table by ID or name.
        If there are multiple tables with the same name this throws an error
        (should really use an annotation to keep track of this).
        If tableId is supplied it will be used in preference to tableName
        @param tableName The name of the table file
        @param tableId The OriginalFile ID of the table file
        @return handle to the table
        """
        if not tableId and not tableName:
            tableId = self.tableId
            tableName = self.tableName

        if not tableId:
            if not tableName:
                tableName = self.tableName
            attrs = {'name': tableName}
            ofiles = list(
                self.conn.getObjects("OriginalFile", attributes = attrs))
            if len(ofiles) > 1:
                raise TableConnectionError(
                    'Multiple tables with name:%s found' % tableName)
            if not ofiles:
                raise TableConnectionError(
                    'No table found with name:%s' % tableName)
            ofile = ofiles[0]
        else:
            attrs = {'id': long(tableId)}
            if tableName:
                attrs['name'] = tableName
            ofile = self.conn.getObject("OriginalFile", attributes = attrs)

        if not ofile:
            raise TableConnectionError('No table found with name:%s id:%s' %
                                       (tableName, tableId))

        if self.tableId == ofile.getId():
            print 'Using existing connection to table name:%s id:%d' % \
                (tableName, tableId)
        else:
            self.closeTable()
            self.table = self.res.openTable(ofile._obj)
            self.tableId = ofile.getId()
            print 'Opened table name:%s id:%d' % (tableName, self.tableId)

        try:
            print '\t%d rows %d columns' % \
                (self.table.getNumberOfRows(), len(self.table.getHeaders()))
        except omero.ApiUsageException:
            pass

        self.tableId = tableId
        return self.table


    def deleteAllTables(self):
        """
        Delete all tables with self.tableName
        Will fail if there are any annotation links
        """
        ofiles = self.conn.getObjects("OriginalFile", \
            attributes = {'name': self.tableName})
        ids = [f.getId() for f in ofiles]
        print 'Deleting ids:%s' % ids
        self.conn.deleteObjects('OriginalFile', ids)


    def dumpTable(self, table):
        """
        Print out the table
        """
        headers = table.getHeaders()
        print ', '.join([t.name for t in headers])
        nrows = table.getNumberOfRows()
        #data = table.readCoordinates(xrange(table.getNumberOfRows))

        for r in xrange(nrows):
            data = table.read(range(len(headers)), r, r + 1)
            print ', '.join(['%.2f' % c.values[0] for c in data.columns])


    def closeTable(self):
        """
        Close the table if open, and set table and tableId to None
        """
        try:
            if self.table:
                self.table.close()
        finally:
            self.table = None
            self.tableId = None


    def newTable(self, schema):
        """
        Create a new uninitialised table
        @param schema the table description
        @return A handle to the table
        """
        self.closeTable()

        self.table = self.res.newTable(self.rid, self.tableName)
        ofile = self.table.getOriginalFile()
        self.tableId = ofile.getId().getValue()

        try:
            self.table.initialize(schema)
            print "Initialised '%s' (%d)" % (self.tableName, self.tableId)
        except Exception as e:
            print "Failed to create table: %s" % e
            try:
                self.table.delete
            except Exception as ed:
                print "Failed to delete table: %s" % ed

            self.table = None
            self.tableId = None
            raise e

        return self.table


    def chunkedRead(self, colNumbers, start, stop, chunk):
        """
        Split a call to table.read(), into multiple chunks to limit the number
        of rows returned in one go.
        @param colNumbers A list of columns indices to be read
        @param start The first row to be read
        @param stop The last + 1 row to be read
        @param chunk The maximum number of rows to read in each call
        @return a data object, note lastModified will be set to the timestamp
        the first chunked call
        """
        p = start
        q = min(start + chunk, stop)
        data = self.table.read(colNumbers, p, q)
        p, q = q, min(q + chunk, stop)

        while p < stop:
            data2 = self.table.read(colNumbers, p, q)
            data.rowNumbers.extend(data2.rowNumbers)
            for (c, c2) in izip(data.columns, data2.columns):
                c.values.extend(c2.values)
            p, q = q, min(q + chunk, stop)

        return data
Пример #5
0
params.addId(image_id_src)

count = 0

for mask_src in query_service.findAllByQuery(query, params):
    mask_dst = MaskI()
    mask_dst.x = mask_src.x
    mask_dst.y = mask_src.y
    mask_dst.width = mask_src.width
    mask_dst.height = mask_src.height
    mask_dst.theZ = mask_src.theZ
    mask_dst.theC = mask_src.theC
    mask_dst.theT = mask_src.theT
    mask_dst.bytes = mask_src.bytes
    mask_dst.fillColor = mask_src.fillColor
    mask_dst.transform = mask_src.transform
    roi_dst = RoiI()
    roi_dst.description = rstring(
        "created by copy-masks script for original mask #{}".format(
            mask_src.id.val))
    roi_dst.image = ImageI(image_id_dst, False)
    roi_dst.addShape(mask_dst)
    update_service.saveObject(roi_dst)
    count += 1

idr._closeSession()
local._closeSession()

print("from image #{} to #{}, mask count = {}".format(image_id_src,
                                                      image_id_dst, count))
Пример #6
0
class OMEROConnection(object):
    def __init__(self, args, configs):
        self.args = args
        self.configs = configs
        self.connect_with = self.configs['CONNECT_WITH']
        self.host = self.configs['OMERO_{}_HOST'.format(self.connect_with)]
        self.user = self.configs['OMERO_{}_USER'.format(self.connect_with)]
        self.password = self.configs['OMERO_{}_PASSWORD'.format(
            self.connect_with)]
        self.port = self.configs['OMERO_{}_PORT'.format(self.connect_with)]

    def __enter__(self):
        from omero.gateway import BlitzGateway  # @UnresolvedImport
        self.conn = BlitzGateway(host=self.host,
                                 username=self.user,
                                 passwd=self.password,
                                 port=self.port)
        self.connected = self.conn.connect()
        # failed to connect
        if not self.connected:
            print_date(
                "Unable to connect to host '{}' on port {} using user '{}'".
                format(self.host, self.port, self.user))
            print_date("Check that server is up")
            sys.exit(1)
        # keepalive
        self.conn.c.enableKeepAlive(5)
        self.omero_user = self.conn.getUser()
        self.userId = self.omero_user.getId()
        self.updateService = self.conn.getUpdateService()
        self.roiService = self.conn.getRoiService()
        return self

    def __exit__(self, exc_type, exc_value, traceback):  # @UnusedVariable
        self.conn._closeSession()
        if self.args.verbose:
            print_date('Connection closed.')
        self.connected = False

    def list(self):
        """List images or ROIs
        
        :return int count: the number of images/ROIs
        """
        if self.args.images:
            print_date("Listing images...")
            images = self.images()
            if self.args.summary:
                pass
            else:
                print_date("Structuring output...")
                image_view = ImageView(images)
                print(image_view)
            return len(images)
        elif self.args.rois:
            print_date("Listing ROIs...")
            rois = self.rois()
            if self.args.summary:
                pass
            else:
                print_date("Structuring output...")
                roi_view = ROIView(rois)
                print(roi_view)
            return len(rois)

    @property
    def projects(self):
        if self.args.project is not None:
            projects = self.conn.searchObjects(["Project"], self.args.project)
            return projects
        else:
            return self.conn.listProjects(self.userId)

    def datasets(self, project=None):
        """List the datasets associated with the current user
        
        :param project: a project
        :param bool in_project: are these datasets contained within a project?
        """
        datasets = list()
        if self.args.dataset is not None:
            datasets += self.conn.searchObjects(["Dataset"], self.args.dataset)
        else:
            if project is not None:
                projects = self.conn.searchObjects(["Project"], project)
                for p in projects:
                    datasets += p.listChildren()
            else:
                params = omero.sys.ParametersI()  # @UndefinedVariable
                params.exp(self.userId)
                datasets += self.conn.getObjects("Dataset", params=params)
        return datasets

    def images(self, project=None, dataset=None):
        """The list of images associated with the current user
        
        If the image ID is specified only the required image is returned. (The project and dataset are ignored.)
        
        Otherwise:
        
        - If a project object is provided all images in all datasets in the project are returned. (The dataset is ignored.)
        - If a project object and dataset object are provided then only those images in the project and dataset are return.
        - If no project object is provided but a dataset object is provided then only those images in the dataset are returned. 
         
        :param str project: OMERO project name
        :param str dataset: OMERO dataset name
        :return list images: a list of OMERO ``Image`` objects
        """
        # assertions
        images = list()
        print_date("Retrieving images...")
        if self.args.image_id is not None:
            try:
                assert isinstance(self.args.image_id, int) or isinstance(
                    self.args.image_id, long)
            except AssertionError:
                print_date("Invalid type for image ID: {}".format(
                    type(self.args.image_id)))
                sys.exit(1)
            image = self.getImage(self.args.image_id)
            if image is not None:
                images.append(image)
        elif self.args.image_name is not None:
            images = self.conn.searchObjects(["Image"], self.args.image_name)
        else:
            if project is not None:  # project specified
                print_date(
                    "Searching for images in project '{}'".format(project))
                # get all projects matching
                projects = self.conn.searchObjects(["Project"], project)
                # get all datasets in projects matching
                datasets_in_projects = dict()
                for p in projects:
                    for d in p.listChildren():
                        datasets_in_projects[d.getName()] = d
                print_date("Found {} datasets in project '{}'".format(
                    len(datasets_in_projects), project))
                # dataset specified
                if dataset is not None:
                    print_date(
                        "Searching for images in dataset '{}'".format(dataset))
                    if dataset in datasets_in_projects.keys():
                        images += datasets_in_projects[dataset].listChildren()
                else:  # dataset not specified
                    print_date(
                        "Searching for images in all {} datasets".format(
                            len(datasets_in_projects)))
                    for dataset in datasets_in_projects.keys():
                        images += datasets_in_projects[dataset].listChildren()
            else:  # project not specified
                # dataset specified
                if dataset is not None:
                    print_date(
                        "Searching for images in dataset '{}'".format(dataset))
                    datasets = self.conn.searchObjects(["Dataset"], dataset)
                    for dataset in datasets:
                        images += dataset.listChildren()
                else:
                    datasets = self.datasets()
                    print_date(
                        "Searching for images in all {} datasets".format(
                            len(datasets)))
                    for dataset in datasets:
                        images += dataset.listChildren()
        print_date("Found {} image(s).".format(len(images)))
        return images

    def getImage(self, image_id):
        """Get the image with the image ID specified on the command line
        
        :param int image_id: command line arguments
        :return image: an image
        :rtype image: ``OMEROImage``
        """
        return self.conn.getObject("Image", image_id)

    def rois(self, project=None, dataset=None):
        """Get an iterator over the ROIs associated with the specified image ID
        
        :param int image_id: image ID
        """
        rois = list()
        print_date("Retrieving ROIs...")
        if self.args.image_id is not None:
            return self.getROIs(self.args.image_id)
        else:
            for image in self.images(project, dataset):
                if image.getROICount() > 0:
                    rois.append((image, self.getROIs(image.getId())))
        roi_count = sum(map(lambda r: len(r[1]), rois))
        print_date("Found {:,} ROIs in {:,} images.".format(
            roi_count, len(rois)))
        return rois

    def getROIs(self, image_id):
        result = self.roiService.findByImage(image_id, None)
        return result.rois

    def attachRois(self, omero_rois):
        """Attach the rois from the iterable"""
        non_rois = filter(lambda r: not isinstance(r, OMEROROI), omero_rois)
        try:
            assert len(non_rois) == 0
        except AssertionError:
            print_date("Found {:,} non-ROI objects".format(len(non_rois)))
            return 1
        for roi in omero_rois:
            self.saveRoi(roi)
        return os.EX_OK

    # save
    def saveRoi(self, roi):
        """Save the given ROI
        
        :param roi: an ROI object
        :type roi: `omero.model.Roi`
        """
        import Ice
        try:
            self.updateService.saveObject(roi)
        except Ice.MemoryLimitException as e:  # @UndefinedVariable
            print_date(str(e))
            sys.exit(1)

    # delete
    def deleteRoi(self, roi_id):
        """
        Delete the given ROI
        
        :param roi: an ROI object
        :type roi: `omero.model.Roi`
        """
        from omero.callbacks import CmdCallbackI  # @UnresolvedImport

        handle = self.conn.deleteObjects("Roi", [roi_id],
                                         deleteAnns=True,
                                         deleteChildren=True)
        callback = CmdCallbackI(self.conn.c, handle)

        while not callback.block(500):
            if self.args.verbose:
                print_date(".", newline=False, incl_date=False)
                time.sleep(2)

        callback.close(True)