def listMostRecentAnnotations (conn, limit, eid=None):
    """
    Retrieve most recent annotations available
    
    @return:    Generator yielding BlitzObjectWrapper
    @rtype:     L{BlitzObjectWrapper} generator
    """
    
    tm = conn.getTimelineService()
    p = omero.sys.Parameters()
    p.map = {}
    f = omero.sys.Filter()
    if eid:
        f.ownerId = rlong(eid)
    else:
        f.groupId = rlong(conn.getEventContext().groupId)
    f.limit = rint(limit)
    p.theFilter = f
    anns = []
    
    # get ALL parent-types, child-types, namespaces:
    annTypes = ['LongAnnotation', 'TagAnnotation', 'CommentAnnotation'] # etc. Only query 1 at at time! 
    for at in annTypes:
        for a in tm.getMostRecentAnnotationLinks(None, [at], None, p):
            # TODO: maybe load parent for each link here
            wrapper = AnnotationWrapper._wrap(conn, a.child, a)
            anns.append(wrapper)
    return anns
    def testUpdateSessions(self, who):
        who, idlediff, livediff = who
        if who.startswith("root"):
            client = self.root
        else:
            client = self.client

        uuid = client.getSessionId()
        service = client.sf.getSessionService()
        obj_before = service.getSession(uuid)
        live = unwrap(obj_before.timeToLive)
        idle = unwrap(obj_before.timeToIdle)

        req = UpdateSessionTimeoutRequest()
        req.session = uuid
        if livediff is not None:
            req.timeToLive = rlong(live+livediff)
        if idlediff is not None:
            req.timeToIdle = rlong(idle+idlediff)
        try:
            cb = client.submit(req)
            cb.getResponse()
            cb.close(True)
            assert not who.startswith("bad")  # must throw
        except omero.CmdError, ce:
            if who.startswith("bad"):
                assert ce.err.name == "non-admin-increase"
                return
            else:
                print ce.err.parameters.get("stacktrace")
                raise Exception(ce.err.category,
                                ce.err.name)
    def _upload(self, client, filename, name, format):

        if not format:
            return

        filename = str(filename)  # Might be path.path
        sz = os.path.getsize(filename)
        if not sz:
            self.status("No %s" % name)
            return

        try:
            ofile = client.upload(filename, name=name, type=format)
            jobid = long(client.getProperty("omero.job"))
            link = omero.model.JobOriginalFileLinkI()
            if self.params is None:
                link.parent = omero.model.ParseJobI(rlong(jobid), False)
            else:
                link.parent = omero.model.ScriptJobI(rlong(jobid), False)
            link.child = ofile.proxy()
            client.getSession().getUpdateService().saveObject(link)
            self.status(
                "Uploaded %s bytes of %s to %s" %
                (sz, filename, ofile.id.val))
        except:
            self.logger.error(
                "Error on upload of %s for pid=%s (%s)",
                filename, self.pid, self.uuid, exc_info=True)
Example #4
0
def get_image_ids(conn, datasetId=None, groupId=-1, ownerId=None):
    """
    Retrieves a list of all image IDs in a Dataset or Orphaned
    (with owner specified by ownerId). The groupId can be specified
    as needed, particuarly when querying orphaned images.
    """
    qs = conn.getQueryService()
    p = omero.sys.ParametersI()
    so = deepcopy(conn.SERVICE_OPTS)
    so.setOmeroGroup(groupId)
    if datasetId is not None:
        p.add('did', rlong(datasetId))
        q = """select image.id from Image image
            join image.datasetLinks dlink where dlink.parent.id = :did
            order by lower(image.name), image.id"""
    else:
        p.add('ownerId', rlong(ownerId))
        q = """select image.id from Image image where
            image.details.owner.id = :ownerId and
            not exists (
                select dilink from DatasetImageLink as dilink
                where dilink.child = image.id

            )  and
            not exists (
                select ws from WellSample ws
                where ws.image.id = image.id
            )
            order by lower(image.name), image.id"""
    iids = [i[0].val for i in qs.projection(q, p, so)]
    return iids
    def testGeneral(self):
        uuid = self.ctx.sessionUuid
        timeline = self.sf.getTimelineService()

        im_ids = dict()
        for i in range(0, 10):
            # create image
            acquired = long(time.time() * 1000)
            img = self.make_image(name='test-img-%s' % uuid, date=acquired)
            im_ids[i] = [img.id.val, acquired]

        # Here we assume that this test is not run within the last 1 second
        start = acquired - 86400
        end = acquired + 1

        p = omero.sys.Parameters()
        p.map = {}
        f = omero.sys.Filter()
        f.ownerId = rlong(self.ctx.userId)
        f.groupId = rlong(self.ctx.groupId)
        p.theFilter = f

        M = timeline.countByPeriod
        A = rtime(long(start))
        B = rtime(long(end))
        counter = M(['Image'], A, B, p)
        assert counter['Image'] == 10
        # And with #9609
        counter = M(['Image'], A, B, p, {"omero.group": "-1"})
        assert counter['Image'] == 10

        p2 = omero.sys.Parameters()
        p2.map = {}
        f2 = omero.sys.Filter()
        f2.ownerId = rlong(self.ctx.userId)
        f2.groupId = rlong(self.ctx.groupId)
        f2.limit = rint(5)
        p2.theFilter = f2

        # p.map["start"] = rtime(long(start))
        # p.map["end"] = rtime(long(end))

        M = timeline.getMostRecentObjects
        res = M(['Image'], p2, False)["Image"]
        assert 5 == len(res)
        # And with #9609
        res = M(['Image'], p2, False, {"omero.group": "-1"})["Image"]
        assert 5 == len(res)

        # 1st element should be the 9th from the im_ids
        assert im_ids[9][0] == res[0].id.val
        # 2nd element should be the 8th from the im_ids
        assert im_ids[8][0] == res[1].id.val
        # 3rd element should be the 7th from the im_ids
        assert im_ids[7][0] == res[2].id.val
        # 4th element should be the 6th from the im_ids
        assert im_ids[6][0] == res[3].id.val
        # 5th element should be the 5th from the im_ids
        assert im_ids[5][0] == res[4].id.val
Example #6
0
 def test_marshal_plate_acquisition_name_no_start_no_end(self, mock_conn, owner_permissions):
     row = [rlong(1L), rstring("name"), rlong(1L), owner_permissions, None, None]
     expected = {
         "id": 1L,
         "ownerId": 1L,
         "name": "name",
         "permsCss": "canEdit canAnnotate canLink canDelete isOwned canChgrp",
     }
Example #7
0
 def test_marshal_plate_acquisition_no_name_start_end(self, mock_conn, owner_permissions, start_time, end_time):
     row = [rlong(1L), None, rlong(1L), owner_permissions, start_time, end_time]
     expected = {
         "id": 1L,
         "ownerId": 1L,
         "name": "2014-05-08 10:37:02 - 2014-05-08 10:38:30",
         "permsCss": "canEdit canAnnotate canLink canDelete isOwned canChgrp",
     }
    def testResuingClass(self):
        myLong = rlong(5)
        myLongFromString = rlong("5")
        assert myLong.val == myLongFromString.val

        ctor1 = myLong.__class__(5)
        ctor2 = myLongFromString.__class__("5")
        assert ctor1.val == ctor2.val
 def testOwnerId(self):
     p = ParametersI()
     self.assertNull(p.theFilter)
     p.exp(rlong(1))
     self.assertNotNull(p.theFilter)
     self.assertNotNull(p.theFilter.ownerId)
     assert rlong(1) == p.getExperimenter()
     self.assertNull(p.allExps().getExperimenter())
     self.assertNotNull(p.theFilter)
Example #10
0
 def test_marshal_dataset(self, mock_conn, owner_permissions):
     row = [rlong(1L), rstring("name"), rlong(1L), owner_permissions, rlong(1L)]
     expected = {
         "id": 1L,
         "ownerId": 1L,
         "name": "name",
         "permsCss": "canEdit canAnnotate canLink canDelete isOwned canChgrp",
         "childCount": 1,
     }
Example #11
0
 def test_marshal_plate_not_owner(self, mock_conn, owner_permissions):
     row = [rlong(1L), rstring("name"), rlong(2L), owner_permissions, 2]
     expected = {
         "id": 1L,
         "ownerId": 2L,
         "name": "name",
         "permsCss": "canEdit canAnnotate canLink canDelete",
         "childCount": 2,
     }
 def testGroupId(self):
     p = ParametersI()
     self.assertNull(p.theFilter)
     p.grp(rlong(1))
     self.assertNotNull(p.theFilter)
     self.assertNotNull(p.theFilter.groupId)
     assert rlong(1) == p.getGroup()
     self.assertNull(p.allGrps().getGroup())
     self.assertNotNull(p.theFilter)
Example #13
0
    def list_tagsets(self, args, tag):
        """
        Returns a TagCollection of just the tagsets.

        If tag is provided, will return the tagsets with those tags.
        """
        params = omero.sys.ParametersI()
        params.addString('ns', omero.constants.metadata.NSINSIGHTTAGSET)
        ice_map = dict()
        if args.admin:
            ice_map["omero.group"] = "-1"

        client = self.ctx.conn(args)
        session = client.getSession()
        q = session.getQueryService()

        tc = TagCollection()

        if tag:
            sql = """
                select a.id, a.description, a.textValue,
                a.details.owner.id, a.details.owner.firstName,
                a.details.owner.lastName
                from AnnotationAnnotationLink b
                inner join b.parent a
                where a.ns=:ns
                """
            sql += " and b.child.id = :tid"
            params.map['tid'] = rlong(long(tag))
        else:
            sql = """
                select a.id, a.description, a.textValue,
                a.details.owner.id, a.details.owner.firstName,
                a.details.owner.lastName
                from TagAnnotation a
                where a.ns=:ns
                """

        if args.uid:
            params.map["eid"] = rlong(long(args.uid))
            sql += " and a.details.owner.id = :eid"

        for element in q.projection(sql, params, ice_map):
            tag_id, description, text, owner, first, last = map(unwrap,
                                                                element)
            tc.tags[tag_id] = Tag(
                tag_id=tag_id,
                name=text,
                description=description,
                owner=owner
            )
            tc.owners[owner] = "%s %s" % (first, last)

        return tc
Example #14
0
def screen_with_plates(screen):
    for plate_id in range(5, 7):
        o = PlateI()
        o.id = rlong(plate_id)
        o.name = rstring('plate_name_%d' % plate_id)
        o.description = rstring('plate_description_%d' % plate_id)
        o.columnNamingConvention = rstring('number')
        o.rowNamingConvention = rstring('letter')
        o.columns = rint(12)
        o.rows = rint(8)
        o.defaultSample = rint(0)
        o.externalIdentifier = rstring('external_identifier_%d' % plate_id)
        o.status = rstring('status_%d' % plate_id)
        o.wellOriginX = LengthI(0.1, UnitsLength.REFERENCEFRAME)
        o.wellOriginY = LengthI(1.1, UnitsLength.REFERENCEFRAME)
        screen.linkPlate(o)
        for well_id in range(7, 9):
            well = WellI()
            well.id = rlong(well_id)
            well.column = rint(2)
            well.row = rint(1)
            well.externalDescription = \
                rstring('external_description_%d' % well_id)
            well.externalIdentifier = \
                rstring('external_identifier_%d' % well_id)
            well.type = rstring('the_type')
            well.alpha = rint(0)
            well.red = rint(255)
            well.green = rint(0)
            well.blue = rint(0)
            well.status = rstring('the_status')
            o.addWell(well)
            plateacquisition = PlateAcquisitionI()
            plateacquisition.id = rlong(well_id)
            plateacquisition.name = rstring(
                'plateacquisition_name_%d' % well_id)
            plateacquisition.description = rstring(
                'plateacquisition_description_%d' % well_id)
            plateacquisition.maximumFieldCount = rint(1)
            plateacquisition.startTime = rtime(1L)
            plateacquisition.endTime = rtime(2L)
            for wellsample_id in range(9, 11):
                wellsample = WellSampleI()
                wellsample.setPlateAcquisition(plateacquisition)
                wellsample.id = rlong(wellsample_id)
                wellsample.posX = LengthI(1.0, UnitsLength.REFERENCEFRAME)
                wellsample.posY = LengthI(2.0, UnitsLength.REFERENCEFRAME)
                wellsample.timepoint = rtime(1L)
                wellsample.image = create_image(1L)
                well.addWellSample(wellsample)

    return screen
    def test1184(self):
        uuid = self.uuid()
        client = self.new_client(perms="rw----")

        share = client.sf.getShareService()
        query = client.sf.getQueryService()
        update = client.sf.getUpdateService()
        admin = client.sf.getAdminService()
        cont = client.sf.getContainerService()

        ds = DatasetI()
        ds.setName(rstring('test1184-ds-%s' % (uuid)))

        for i in range(1,2001):
            img = ImageI()
            img.setName(rstring('img1184-%s' % (uuid)))
            img.setAcquisitionDate(rtime(time.time()))
            # Saving in one go
            #dil = DatasetImageLinkI()
            #dil.setParent(ds)
            #dil.setChild(img)
            #update.saveObject(dil)
            ds.linkImage(img)
        ds = update.saveAndReturnObject(ds)

        c = cont.getCollectionCount(ds.__class__.__name__, ("imageLinks"), [ds.id.val], None)
        assert c[ds.id.val] == 2000

        page = 1
        p = omero.sys.Parameters()
        p.map = {}
        p.map["eid"] = rlong(admin.getEventContext().userId)
        p.map["oid"] = rlong(ds.id.val)
        if page is not None:
            f = omero.sys.Filter()
            f.limit = rint(24)
            f.offset = rint((int(page)-1)*24)
            p.theFilter = f

        sql = "select im from Image im join fetch im.details.owner join fetch im.details.group " \
              "left outer join fetch im.datasetLinks dil left outer join fetch dil.parent d " \
              "where d.id = :oid and im.details.owner.id=:eid order by im.id asc"

        start = time.time()
        res = query.findAllByQuery(sql,p)
        assert 24 ==  len(res)
        end = time.time()
        elapsed = end - start
        assert elapsed < 3.0,\
            "Expected the test to complete in < 3 seconds, took: %f" % elapsed
    def testCollaborativeTimeline(self):
        """
        Create some images as one user - test if another user
        can see these events in timeline.
        """

        client2, user2 = self.new_client_and_user(group=self.group)

        # log in as first user & create images
        timeline2 = client2.sf.getTimelineService()

        im_ids = dict()
        for i in range(0, 10):
            # create image
            acquired = long(time.time() * 1000)
            img = self.make_image(name='test-img-%s' % client2.sf,
                                  date=acquired, client=client2)
            im_ids[i] = [img.id.val, acquired]

        # Here we assume that this test is not run within the last 1 second
        start = acquired - 86400
        end = acquired + 1

        ownerId = rlong(user2.id.val)
        groupId = rlong(self.group.id.val)

        def assert_timeline(timeline, start, end, ownerId=None, groupId=None):
            p = omero.sys.Parameters()
            p.map = {}
            f = omero.sys.Filter()
            if ownerId is not None:
                f.ownerId = ownerId
            if groupId is not None:
                f.groupId = groupId
            p.theFilter = f

            counter = timeline.countByPeriod(
                ['Image'], rtime(long(start)), rtime(long(end)), p)
            assert 10 == counter['Image']
            data = timeline.getByPeriod(
                ['Image'], rtime(long(start)), rtime(long(end)), p, False)
            assert 10 == len(data['Image'])

        assert_timeline(timeline2, start, end, ownerId, groupId)

        # now log in as another user (default group is same as user-created
        # images above)
        assert_timeline(
            self.sf.getTimelineService(), start, end, ownerId, groupId)
 def testGetAttrWorks(self):
     rbool(True).val
     rdouble(0.0).val
     rfloat(0.0).val
     rint(0).val
     rlong(0).val
     rtime(0).val
     rinternal(None).val
     robject(None).val
     rstring("").val
     rclass("").val
     rarray().val
     rlist().val
     rset().val
     rmap().val
Example #18
0
 def testExperimenter(self):
     p = ParametersI()
     p.exp(rlong(1))
     assert p.isExperimenter()
     assert p.getExperimenter().getValue() == 1L
     p.allExps()
     assert not p.isExperimenter()
Example #19
0
def externalInfo():
    o = ExternalInfoI()
    o.entityId = rlong(123)
    o.entityType = rstring('test')
    o.lsid = rstring('ABCDEF')
    o.uuid = rstring('f90a1fd5-275c-4d14-82b3-87b5ef0f07de')
    return o
Example #20
0
    def test1173(self):
        uuid = self.root.sf.getAdminService().getEventContext().sessionUuid
        timeline = self.root.sf.getTimelineService()

        # create image
        ds = self.make_dataset(name='test1173-ds-%s' % uuid, client=self.root)
        ds.unload()

        # Here we assume that this test is not run within the last 1 second
        start = long(time.time() * 1000 - 86400)
        end = long(time.time() * 1000 + 86400)

        p = omero.sys.Parameters()
        p.map = {}
        f = omero.sys.Filter()
        f.ownerId = rlong(self.new_user().id.val)
        p.theFilter = f

        M = timeline.getEventLogsByPeriod
        A = rtime(long(start))
        B = rtime(long(end))

        rv = M(A, B, p)
        assert rv > 0

        # And now for #9609
        rv = M(A, B, p, {"omero.group": "-1"})
        assert rv > 0
Example #21
0
    def test1088(self):
        admin = self.root.sf.getAdminService()
        q = self.root.sf.getQueryService()
        cx = admin.getEventContext()

        p = omero.sys.Parameters()
        p.map = {}
        p.map["uid"] = rlong(cx.userId)
        p.map['start'] = start = rtime(1218529874000)
        p.map['end'] = end = rtime(1221121874000)

        sql1 = "select el from EventLog el left outer join fetch el.event ev " \
               "where el.entityType in ('ome.model.core.Pixels', 'ome.model.core.Image', " \
               "'ome.model.containers.Dataset', 'ome.model.containers.Project') " \
               "and ev.id in (select id from Event where experimenter.id=:uid and time > :start and time < :end)"


        sql2 = "select el from EventLog el left outer join fetch el.event ev " \
               "where el.entityType in ('ome.model.core.Pixels', 'ome.model.core.Image', " \
               "'ome.model.containers.Dataset', 'ome.model.containers.Project') " \
               "and ev.experimenter.id=:uid and ev.time > :start and ev.time < :end"

        import time
        sql1_start = time.time()
        l = q.findAllByQuery(sql1, p)
        sql1_stop = time.time()
        # print "\nSQL1: %s objects in %s seconds" % (str(len(l)), str(sql1_stop - sql1_start))

        sql2_start = time.time()
        l = q.findAllByQuery(sql2, p)
        sql2_stop = time.time()
Example #22
0
    def test1175(self):
        uuid = self.root.sf.getAdminService().getEventContext().sessionUuid
        update = self.root.sf.getUpdateService()
        timeline = self.root.sf.getTimelineService()

        # create dataset
        ds = self.make_dataset(name='test1154-ds-%s' % (uuid),
                               client=self.root)
        ds.unload()

        # create tag
        ann = omero.model.TagAnnotationI()
        ann.textValue = rstring('tag-%s' % (uuid))
        ann.setDescription(rstring('tag-%s' % (uuid)))
        t_ann = omero.model.DatasetAnnotationLinkI()
        t_ann.setParent(ds)
        t_ann.setChild(ann)
        update.saveObject(t_ann)

        p = omero.sys.Parameters()
        p.map = {}
        f = omero.sys.Filter()
        f.ownerId = rlong(0)
        f.limit = rint(10)
        p.theFilter = f

        M = timeline.getMostRecentAnnotationLinks
        res = M(None, ['TagAnnotation'], None, p)
        assert len(res) > 0

        # And now for #9609
        res = M(None, ['TagAnnotation'], None, p, {"omero.group": "-1"})
        assert len(res) > 0
Example #23
0
    def test1173(self):
        client2, user2 = self.new_client_and_user(group=self.group)
        timeline = client2.sf.getTimelineService()
        uuid = client2.sf.getAdminService().getEventContext().sessionUuid

        # create dataset
        ds = self.make_dataset(name='test1173-ds-%s' % uuid, client=client2)
        ds.unload()

        # Here we assume that this test is not run within the last 1 second
        start = int(time.time() * 1000 - 86400)
        end = int(time.time() * 1000 + 86400)

        p = omero.sys.Parameters()
        p.map = {}
        f = omero.sys.Filter()
        f.ownerId = rlong(user2.id.val)
        p.theFilter = f

        M = timeline.getEventLogsByPeriod
        A = rtime(start)
        B = rtime(end)

        rv = M(A, B, p)
        assert len(rv) > 0

        # And now for #9609
        rv = M(A, B, p, {"omero.group": "-1"})
        assert len(rv) > 0
Example #24
0
    def testBasicUsage(self):
        gateway = self.client.sf.createGateway()
        gateway.getProjects([0], False)

        try:
            # query below does not find image if created with
            # self.createTestImage() even though it uses 'identical' code to
            # createTestImage(self.client.sf), which uses script_utils
            # iid = self.createTestImage().getId().getValue()
            iid = createTestImage(self.client.sf)
            print iid, type(iid)
            query = self.client.sf.getQueryService()

            params = omero.sys.Parameters()
            params.map = {}
            params.map["oid"] = rlong(iid)
            params.theFilter = omero.sys.Filter()
            params.theFilter.offset = rint(0)
            params.theFilter.limit = rint(1)
            pixel = query.findByQuery(
                "select p from Pixels as p left outer join\
                 fetch p.image as i where i.id=:oid", params)
            print pixel
            imgid = pixel.image.id.val
            print imgid
            gateway.getRenderedImage(pixel.id.val, 0, 0)
        except omero.ValidationException:
            print "testBasicUsage - createTestImage has failed.\
                   This fixture method needs to be fixed."

        gateway.close()
def getTimes(conn, image, theC=0):
    """
    Get a dict of tIndex:time (seconds) for the first plane (Z = 0) at
    each time-point for the defined image and Channel.

    @param conn:        BlitzGateway connection
    @param image:       ImageWrapper
    @return:            A map of tIndex: timeInSecs
    """

    queryService = conn.getQueryService()
    pixelsId = image.getPixelsId()

    params = omero.sys.ParametersI()
    params.add("theC", rint(theC))
    params.add("theZ", rint(0))
    params.add("pixelsId", rlong(pixelsId))

    query = "from PlaneInfo as Info where Info.theZ=:theZ and" \
        " Info.theC=:theC and pixels.id=:pixelsId"
    infoList = queryService.findAllByQuery(query, params, conn.SERVICE_OPTS)

    timeMap = {}
    for info in infoList:
        tIndex = info.theT.getValue()
        time = info.deltaT.getValue()
        timeMap[tIndex] = time
    return timeMap
Example #26
0
def get_managed_groups(conn):
    """
    List of groups that the user can administer
    All groups for admins
    For other users, only groups of which they are the owner
    """

    service_opts = deepcopy(conn.SERVICE_OPTS)
    service_opts.setOmeroGroup(-1)
    params = omero.sys.ParametersI()

    qs = conn.getQueryService()
    q = """
        SELECT grp.id,
               grp.name,
               grp.details.permissions
        FROM ExperimenterGroup grp
        """

    if conn.isAdmin() is not True:
        user = conn.getUser()
        params.add('mid', rlong(user.getId()))
        q += """
                JOIN grp.groupExperimenterMap grexp
                WHERE grp.name != 'user'
                AND grexp.child.id = :mid
                AND grexp.owner = true
                ORDER BY lower(grp.name)
             """

    groups = []
    for e in qs.projection(q, params, service_opts):
        groups.append(_marshal_group(conn, e[0:3]))
    return groups
Example #27
0
def api_thumbnails(request, conn=None, **kwargs):
    """
    Return data like
    { project-1: {thumbnail: base64data, image: {id:1}} }
    """
    project_ids = request.GET.getlist('project')
    screen_ids = request.GET.getlist('screen')

    image_ids = {}
    for obj_type, ids in zip(['project', 'screen'], [project_ids, screen_ids]):
        for obj_id in ids:
            images = _get_study_images(conn, obj_type, obj_id)
            if len(images) > 0:
                image_ids[images[0].id.val] = "%s-%s" % (obj_type, obj_id)

    thumbnails = conn.getThumbnailSet([rlong(i) for i in image_ids.keys()], 96)
    rv = {}
    for i, obj_id in image_ids.items():
        rv[obj_id] = {"image": {'id': i}}
        try:
            t = thumbnails[i]
            if len(t) > 0:
                # replace thumbnail urls by base64 encoded image
                rv[obj_id]["thumbnail"] = ("data:image/jpeg;base64,%s" %
                                           base64.b64encode(t).decode("utf-8"))

        except KeyError:
            logger.error("Thumbnail not available. (img id: %d)" % i)
    return rv
Example #28
0
File: tag.py Project: mtbc/omero-py
    def createset(self, args):
        """
        Create a tag set command.
        """
        tags = []
        if args.tag:
            if type(args.tag) is list:
                tags = args.tag
            else:
                tags = [args.tag]
        else:
            # Should not happen
            self.ctx.err("Missing tag parameter")
            sys.exit(1)

        tag = self.create_tag(args.name, args.desc, text="tag set")
        tag.ns = rstring(omero.constants.metadata.NSINSIGHTTAGSET)
        links = []
        for t in tags:
            link = AnnotationAnnotationLinkI()
            link.parent = tag
            link.child = TagAnnotationI(rlong(int(t)), False)
            links.append(link)
        client = self.ctx.conn(args)
        session = client.getSession()
        update_service = session.getUpdateService()
        try:
            links = update_service.saveAndReturnArray(links)
            self.ctx.out("TagAnnotation:%s" % links[0].parent.id.val)
        except omero.ValidationException as e:
            self.ctx.err(e.message)
            self.ctx.err("Check that tag '%s' exists." % t)
            sys.exit(1)
Example #29
0
 def getDatasetMap(self):
     """
     Convert unique list of dataset names to a map
     (dataset_name, dataset_object).
     """
     dataset_map = {}
     params = omero.sys.ParametersI()
     params.add("pid", rlong(self.target_project_id))
     for name in self.target_dataset_names:
         params.add("dname", rstring(name))
         dataset = self.query_service.findByQuery(self.dataset_query,
                                                  params)
         if dataset is None:
             print("Creating new datset")
             dataset = omero.model.DatasetI()
             dataset.setName(rstring(name))
             dataset = \
                 self.update_service.saveAndReturnObject(dataset)
             datasetId = dataset.getId().getValue()
             print(("\tNew dataset ID:", datasetId))
             link = omero.model.ProjectDatasetLinkI()
             print(("\tLinking dataset to:", self.target_project_id))
             link.parent = omero.model.ProjectI(self.target_project_id,
                                                False)
             link.child = omero.model.DatasetI(datasetId, False)
             self.update_service.saveObject(link)
             dataset = self.query_service.findByQuery(
                 self.dataset_query, params)
         dataset_map[name] = dataset
     return dataset_map
Example #30
0
    def timeout(self, args):
        client = self.ctx.conn(args)
        svc = client.sf.getSessionService()

        uuid = args.session
        if uuid is None:
            uuid = self.ctx.get_event_context().sessionUuid
        try:
            obj = svc.getSession(uuid)
        except:
            self.ctx.dbg(traceback.format_exc())
            self.ctx.die(557, "cannot get session: %s" % uuid)

        if args.seconds is None:
            # Query only
            secs = unwrap(obj.timeToIdle) / 1000.0
            self.ctx.out(secs)
            return secs

        req = omero.cmd.UpdateSessionTimeoutRequest()
        req.session = uuid
        req.timeToIdle = rlong(args.seconds * 1000)
        try:
            cb = client.submit(req)  # Response is "OK"
            cb.close(True)
        except omero.CmdError, ce:
            self.ctx.dbg(str(ce.err))
            self.ctx.die(558, "CmdError: %s" % ce.err.name)
def runAsScript():
    """
    The main entry point of the script, as called by the client via the
    scripting service, passing the required parameters.
    """
    printDuration(False)    # start timer
    dataTypes = [rstring('Dataset'), rstring('Image')]

    client = scripts.client(
        'Images_From_ROIs.py',
        """Create new Images from the regions defined by Rectangle ROIs on \
other Images.
Designed to work with single-plane images (Z=1 T=1) with multiple ROIs per \
image.
If you choose to make an image stack from all the ROIs, this script \
assumes that all the ROIs on each Image are the same size.""",

        scripts.String(
            "Data_Type", optional=False, grouping="1",
            description="Choose Images via their 'Dataset' or directly by "
            " 'Image' IDs.", values=dataTypes, default="Image"),

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

        scripts.String(
            "Container_Name", grouping="3",
            description="Option: put Images in new Dataset with this name"
            " OR use this name for new Image stacks, if 'Make_Image_Stack')",
            default="From_ROIs"),

        scripts.Bool(
            "Make_Image_Stack", grouping="4", default=False,
            description="If true, make a single Image (stack) from all the"
            " ROIs of each parent Image"),

        version="4.2.0",
        authors=["William Moore", "OME Team"],
        institutions=["University of Dundee"],
        contact="*****@*****.**",
    )

    try:
        parameterMap = client.getInputs(unwrap=True)
        print parameterMap

        # create a wrapper so we can use the Blitz Gateway.
        conn = BlitzGateway(client_obj=client)

        robj, message = makeImagesFromRois(conn, parameterMap)

        client.setOutput("Message", rstring(message))
        if robj is not None:
            client.setOutput("Result", robject(robj))

    finally:
        client.closeSession()
        printDuration()
Example #32
0
def get_managed_groups(conn):
    """
    List of groups that the user can administer
    All groups for admins
    For other users, only groups of which they are the owner
    """

    service_opts = deepcopy(conn.SERVICE_OPTS)
    service_opts.setOmeroGroup(-1)
    params = omero.sys.ParametersI()

    qs = conn.getQueryService()
    q = """
        SELECT grp.id,
               grp.name,
               grp.details.permissions
        FROM ExperimenterGroup grp
        """

    if conn.isAdmin() is not True:
        user = conn.getUser()
        params.add('mid', rlong(user.getId()))
        q += """
                JOIN grp.groupExperimenterMap grexp
                WHERE grp.name != 'user'
                AND grexp.child.id = :mid
                AND grexp.owner = true
                ORDER BY lower(grp.name)
             """

    groups = []
    for e in qs.projection(q, params, service_opts):
        groups.append(_marshal_group(conn, e[0:3]))
    return groups
Example #33
0
    def createAnnotationsLinks(self, atype, tids, oids):
        """
        Links existing annotations to 1 or more objects

        @param atype:       Annotation type E.g. "tag", "file"
        @param tids:        Annotation IDs
        @param oids:        Dict of Objects and IDs. E.g. {"Image": [1,2,3],
                            "Dataset", [6]}
        """
        atype = str(atype).lower()
        if not atype.lower() in ("tag", "comment", "file"):
            raise AttributeError("Object type must be: tag, comment, file.")

        new_links = list()
        annotations = list(self.conn.getObjects("Annotation", tids))
        parent_objs = []
        for k in oids:
            if len(oids[k]) > 0:
                if k.lower() == 'acquisition':
                    parent_type = 'PlateAcquisition'
                else:
                    parent_type = k.lower().title()
                parent_ids = [o.id for o in oids[k]]
                # check for existing links belonging to Current user
                params = omero.sys.Parameters()
                params.theFilter = omero.sys.Filter()
                params.theFilter.ownerId = rlong(self.conn.getUserId())
                links = self.conn.getAnnotationLinks(parent_type,
                                                     parent_ids=parent_ids,
                                                     ann_ids=tids,
                                                     params=params)
                pcLinks = [(link.parent.id.val, link.child.id.val)
                           for link in links]
                # Create link between each object and annotation
                for obj in self.conn.getObjects(parent_type, parent_ids):
                    parent_objs.append(obj)
                    for a in annotations:
                        if (obj.id, a.id) in pcLinks:
                            continue  # link already exists
                        l_ann = getattr(omero.model,
                                        parent_type + "AnnotationLinkI")()
                        l_ann.setParent(obj._obj)
                        l_ann.setChild(a._obj)
                        new_links.append(l_ann)
        failed = 0
        saved_links = []
        try:
            # will fail if any of the links already exist
            saved_links = self.conn.getUpdateService().saveAndReturnArray(
                new_links, self.conn.SERVICE_OPTS)
        except omero.ValidationException:
            for link in new_links:
                try:
                    saved_links.append(
                        self.conn.getUpdateService().saveAndReturnObject(
                            link, self.conn.SERVICE_OPTS))
                except Exception:
                    failed += 1

        return tids
Example #34
0
def well():
    """Basic Well for testing Color. Full test of Well below."""
    well = WellI()
    well.id = rlong(1)
    well.column = rint(0)
    well.row = rint(0)
    return well
Example #35
0
    def testOddMessage(self):
        store = self.client.sf.createRawFileStore()

        images = list()
        for i in range(0, 10):
            img = self.make_image()
            iid = img.getId().getValue()

            oFile = omero.model.OriginalFileI()
            oFile.setName(rstring('companion_file.txt'))
            oFile.setPath(rstring('/my/path/to/the/file/'))
            oFile.setSize(rlong(7471))
            oFile.setHash(rstring("pending"))
            oFile.setMimetype(rstring('Companion/Deltavision'))

            ofid = self.update.saveAndReturnObject(oFile).id.val

            store.setFileId(ofid)
            binary = 'aaa\naaaa\naaaaa'
            store.write(binary, 0, 0)
            of = store.save()

            fa = omero.model.FileAnnotationI()
            fa.setNs(rstring(omero.constants.namespaces.NSCOMPANIONFILE))
            fa.setFile(of)

            self.link(img, fa)
            images.append(iid)

        command = Delete2(targetObjects={"Image": images})

        handle = self.client.sf.submit(command)
        callback = self.wait_on_cmd(self.client, handle)
        callback.close(True)
Example #36
0
def opt_unit_label(identity_transform):
    o = LabelI()
    populate_shape(o, identity_transform, False)
    o.x = rdouble(1.0)
    o.y = rdouble(2.0)
    o.id = rlong(7)
    return o
Example #37
0
    def test_marshal_plate_not_owner(self, mock_conn, owner_permissions):
        row = [
            rlong(1L),
            rstring('name'),
            rlong(2L),
            owner_permissions,
        ]
        expected = {
            'id': 1L,
            'name': 'name',
            'permsCss': 'canEdit canAnnotate canLink canDelete',
            'plateAcquisitions': list()
        }

        marshaled = marshal_plate(mock_conn, row)
        assert marshaled == expected
Example #38
0
def runAsScript():
    """
    The main entry point of the script, as called by the client via the
    scripting service, passing the required parameters.
    """
    printDuration(False)    # start timer
    dataTypes = [rstring('Dataset'), rstring('Image')]

    client = scripts.client(
        'Images_From_ROIs.py',
        """Create new Images from the regions defined by Rectangle ROIs on \
other Images.
Designed to work with single-plane images (Z=1 T=1) with multiple ROIs per \
image.
If you choose to make an image stack from all the ROIs, this script \
assumes that all the ROIs on each Image are the same size.""",

        scripts.String(
            "Data_Type", optional=False, grouping="1",
            description="Choose Images via their 'Dataset' or directly by "
            " 'Image' IDs.", values=dataTypes, default="Image"),

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

        scripts.String(
            "Container_Name", grouping="3",
            description="Option: put Images in new Dataset with this name"
            " OR use this name for new Image stacks, if 'Make_Image_Stack')",
            default="From_ROIs"),

        scripts.Bool(
            "Make_Image_Stack", grouping="4", default=False,
            description="If true, make a single Image (stack) from all the"
            " ROIs of each parent Image"),

        version="4.2.0",
        authors=["William Moore", "OME Team"],
        institutions=["University of Dundee"],
        contact="*****@*****.**",
    )

    try:
        parameterMap = client.getInputs(unwrap=True)
        print parameterMap

        # create a wrapper so we can use the Blitz Gateway.
        conn = BlitzGateway(client_obj=client)

        robj, message = makeImagesFromRois(conn, parameterMap)

        client.setOutput("Message", rstring(message))
        if robj is not None:
            client.setOutput("Result", robject(robj))

    finally:
        client.closeSession()
        printDuration()
Example #39
0
def getTimes(conn, image, theC=0):
    """
    Get a dict of tIndex:time (seconds) for the first plane (Z = 0) at 
    each time-point for the defined image and Channel.
    
    @param conn:        BlitzGateway connection
    @param image:       ImageWrapper
    @return:            A map of tIndex: timeInSecs
    """

    queryService = conn.getQueryService()
    pixelsId = image.getPixelsId()

    params = omero.sys.ParametersI()
    params.add("theC", rint(theC))
    params.add("theZ", rint(0))
    params.add("pixelsId", rlong(pixelsId))

    query = "from PlaneInfo as Info where Info.theZ=:theZ and Info.theC=:theC and pixels.id=:pixelsId"
    infoList = queryService.findAllByQuery(query, params, conn.SERVICE_OPTS)

    timeMap = {}
    for info in infoList:
        tIndex = info.theT.getValue()
        time = info.deltaT.getValue() 
        timeMap[tIndex] = time
    return timeMap    
Example #40
0
 def testGroup(self):
     p = ParametersI()
     p.grp(rlong(1))
     assert p.isGroup()
     assert p.getGroup().getValue() == 1L
     p.allGrps()
     assert not p.isGroup()
Example #41
0
def label():
    o = LabelI()
    populate_shape(o)
    o.x = rdouble(1.0)
    o.y = rdouble(2.0)
    o.id = rlong(7L)
    return o
Example #42
0
def opt_unit_label():
    o = LabelI()
    populate_shape(o, False)
    o.x = rdouble(1.0)
    o.y = rdouble(2.0)
    o.id = rlong(7L)
    return o
def point():
    o = PointI()
    populate_shape(o)
    o.cx = rdouble(1.0)
    o.cy = rdouble(2.0)
    o.id = rlong(3L)
    return o
Example #44
0
def externalInfo():
    o = ExternalInfoI()
    o.entityId = rlong(123L)
    o.entityType = rstring("test")
    o.lsid = rstring("ABCDEF")
    o.uuid = rstring("f90a1fd5-275c-4d14-82b3-87b5ef0f07de")
    return o
 def testExperimenter(self):
     p = ParametersI()
     p.exp(rlong(1))
     assert p.isExperimenter()
     assert p.getExperimenter().getValue() == 1L
     p.allExps()
     assert not p.isExperimenter()
    def test1088(self):
        admin = self.root.sf.getAdminService()
        q = self.root.sf.getQueryService()
        cx = admin.getEventContext()

        p = omero.sys.Parameters()
        p.map = {}
        p.map["uid"] = rlong(cx.userId)
        p.map['start'] = start = rtime(1218529874000)
        p.map['end'] = end = rtime(1221121874000)

        sql1 = "select el from EventLog el left outer join fetch el.event ev " \
               "where el.entityType in ('ome.model.core.Pixels', 'ome.model.core.Image', " \
               "'ome.model.containers.Dataset', 'ome.model.containers.Project') " \
               "and ev.id in (select id from Event where experimenter.id=:uid and time > :start and time < :end)"


        sql2 = "select el from EventLog el left outer join fetch el.event ev " \
               "where el.entityType in ('ome.model.core.Pixels', 'ome.model.core.Image', " \
               "'ome.model.containers.Dataset', 'ome.model.containers.Project') " \
               "and ev.experimenter.id=:uid and ev.time > :start and ev.time < :end"

        import time
        sql1_start = time.time()
        l = q.findAllByQuery(sql1, p)
        sql1_stop = time.time()
        # print "\nSQL1: %s objects in %s seconds" % (str(len(l)), str(sql1_stop - sql1_start))

        sql2_start = time.time()
        l = q.findAllByQuery(sql2, p)
        sql2_stop = time.time()
 def testGroup(self):
     p = ParametersI()
     p.grp(rlong(1))
     assert p.isGroup()
     assert p.getGroup().getValue() == 1L
     p.allGrps()
     assert not p.isGroup()
Example #48
0
    def createset(self, args):
        """
        Create a tag set command.
        """
        tags = []
        if args.tag:
            if type(args.tag) is list:
                tags = args.tag
            else:
                tags = [args.tag]
        else:
            # Should not happen
            self.ctx.err("Missing tag parameter")
            sys.exit(1)

        tag = self.create_tag(args.name, args.desc, text="tag set")
        tag.ns = rstring(omero.constants.metadata.NSINSIGHTTAGSET)
        links = []
        for t in tags:
            link = AnnotationAnnotationLinkI()
            link.parent = tag
            link.child = TagAnnotationI(rlong(long(t)), False)
            links.append(link)
        client = self.ctx.conn(args)
        session = client.getSession()
        update_service = session.getUpdateService()
        try:
            links = update_service.saveAndReturnArray(links)
            self.ctx.out("TagAnnotation:%s" % links[0].parent.id.val)
        except omero.ValidationException as e:
            self.ctx.err(e.message)
            self.ctx.err("Check that tag '%s' exists." % t)
            sys.exit(1)
Example #49
0
    def timeout(self, args):
        client = self.ctx.conn(args)
        svc = client.sf.getSessionService()

        uuid = args.session
        if uuid is None:
            uuid = self.ctx.get_event_context().sessionUuid
        try:
            obj = svc.getSession(uuid)
        except:
            self.ctx.dbg(traceback.format_exc())
            self.ctx.die(557, "cannot get session: %s" % uuid)

        if args.seconds is None:
            # Query only
            secs = unwrap(obj.timeToIdle)/1000.0
            self.ctx.out(secs)
            return secs

        req = omero.cmd.UpdateSessionTimeoutRequest()
        req.session = uuid
        req.timeToIdle = rlong(args.seconds * 1000)
        try:
            cb = client.submit(req)  # Response is "OK"
            cb.close(True)
        except omero.CmdError, ce:
            self.ctx.dbg(str(ce.err))
            self.ctx.die(558, "CmdError: %s" % ce.err.name)
Example #50
0
 def _getProcessor(self):
     scripts = self.root.getSession().getScriptService()
     id = scripts.uploadOfficialScript("/tests/cli/%s.py" % self.uuid(),
                                       SCRIPT)
     j = omero.model.ScriptJobI()
     j.linkOriginalFile(omero.model.OriginalFileI(rlong(id), False))
     p = self.client.sf.sharedResources().acquireProcessor(j, 100)
     return p
Example #51
0
 def testPingViaISCript(self):
     p = self._getProcessor()
     input = rmap({})
     input.val["a"] = rlong(2)
     input.val["b"] = rstring("d")
     process = p.execute(input)
     output = self.assertSuccess(p, process)
     assert output.val["a"].val == 2
Example #52
0
 def testInsightPolygonMarshal(self):
     shape = omero.model.PolygonI()
     shape.id = rlong(self.DEFAULT_ID)
     shape.points = rstring('points[1,2 2,3 4,5] points1[1,2 2,3 4,5] points2[1,2 2,3 4,5] mask[0,0,0]')
     marshaled = shapeMarshal(shape)
     self.assertEqual('Polygon', marshaled['type'])
     self.assertEqual(self.DEFAULT_ID, marshaled['id'])
     self.assertEquals('M 1 2 L 2 3 L 4 5 z' , marshaled['points'])
Example #53
0
    def test_marshal_plate_acquisition_name_no_start_no_end(
            self, mock_conn, owner_permissions):
        row = [
            rlong(1),
            rstring("name"),
            rlong(1), owner_permissions, None, None
        ]
        expected = {
            "id": 1,
            "ownerId": 1,
            "name": "name",
            "permsCss":
            "canEdit canAnnotate canLink canDelete canChgrp isOwned",
        }

        marshaled = _marshal_plate_acquisition(mock_conn, row)
        assert marshaled == expected
Example #54
0
    def test_marshal_dataset_not_owner(self, mock_conn, owner_permissions):
        row = [
            rlong(1),
            rstring("name"),
            rlong(2), owner_permissions,
            rlong(1)
        ]
        expected = {
            "id": 1,
            "ownerId": 2,
            "name": "name",
            "permsCss": "canEdit canAnnotate canLink canDelete canChgrp",
            "childCount": 1,
        }

        marshaled = _marshal_dataset(mock_conn, row)
        assert marshaled == expected
Example #55
0
    def test_marshal_plate_acquisition_name_no_start_no_end(
            self, mock_conn, owner_permissions):
        row = [
            rlong(1),
            rstring('name'),
            rlong(1), owner_permissions, None, None
        ]
        expected = {
            'id': 1,
            'ownerId': 1,
            'name': 'name',
            'permsCss':
            'canEdit canAnnotate canLink canDelete canChgrp isOwned'
        }

        marshaled = _marshal_plate_acquisition(mock_conn, row)
        assert marshaled == expected
Example #56
0
def project_with_datasets(project):
    for dataset_id in range(1, 3):
        o = DatasetI()
        o.id = rlong(dataset_id)
        o.name = rstring('dataset_name_%d' % dataset_id)
        o.description = rstring('dataset_description_%d' % dataset_id)
        project.linkDataset(o)
    return project
Example #57
0
def basic_polyline(request, default_id):
    points = request.param
    shape = omero.model.PolylineI()
    shape.id = rlong(default_id)
    shape.points = rstring(points)
    # r=17,g=34,b=51,a=0
    shape.strokeColor = rint(ctypes.c_int(0x11223300).value)
    return shape
Example #58
0
 def testOmeXmlPolygonMarshal(self):
     shape = omero.model.PolygonI()
     shape.id = rlong(self.DEFAULT_ID)
     shape.points = rstring('1,2 2,3 4,5')
     marshaled = shapeMarshal(shape)
     self.assertEqual('Polygon', marshaled['type'])
     self.assertEqual(self.DEFAULT_ID, marshaled['id'])
     self.assertEquals('M 1 2 L 2 3 L 4 5 z' , marshaled['points'])
Example #59
0
    def test_marshal_dataset_not_owner(self, mock_conn, owner_permissions):
        row = [
            rlong(1),
            rstring('name'),
            rlong(2), owner_permissions,
            rlong(1)
        ]
        expected = {
            'id': 1,
            'ownerId': 2,
            'name': 'name',
            'permsCss': 'canEdit canAnnotate canLink canDelete canChgrp',
            'childCount': 1
        }

        marshaled = _marshal_dataset(mock_conn, row)
        assert marshaled == expected
Example #60
0
 def testInsightPolyLineFloatMarshal(self):
     shape = omero.model.PolylineI()
     shape.id = rlong(self.DEFAULT_ID)
     shape.points = rstring('points[1.5,2.5 2,3 4.1,5.1] points1[1.5,2.5 2,3 4.1,5.1] points2[1.5,2.5 2,3 4.1,5.1] mask[0,0,0]')
     marshaled = shapeMarshal(shape)
     self.assertEqual('PolyLine', marshaled['type'])
     self.assertEqual(self.DEFAULT_ID, marshaled['id'])
     self.assertEquals('M 1.5 2.5 L 2 3 L 4.1 5.1' , marshaled['points'])