class TableStoreTestHelper(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user(perms='rwra--') def teardown_class(self): self.ua.close() def setup_method(self, method): self.clis = [] self.sess = self.create_client_session(self.user) self.name = UserAccount.uuid() ns = UserAccount.uuid() self.ft_space = ns + '/features' self.ann_space = ns + '/source' def teardown_method(self, method): for cli in self.clis: cli.closeSession() def create_user_same_group(self): g = self.sess.getAdminService().getDefaultGroup(self.user.id.val) return self.ua.new_user(group=g) def create_client_session(self, user): cli = omero.client() self.clis.append(cli) un = unwrap(user.getOmeName()) sess = cli.createSession(un, un) return sess
class TableStoreTestHelper(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user(perms="rwra--") def teardown_class(self): self.ua.close() def setup_method(self, method): self.clis = [] self.sess = self.create_client_session(self.user) self.name = UserAccount.uuid() ns = UserAccount.uuid() self.ft_space = ns + "/features" self.ann_space = ns + "/source" def teardown_method(self, method): for cli in self.clis: cli.closeSession() def create_user_same_group(self): g = self.sess.getAdminService().getDefaultGroup(self.user.id.val) return self.ua.new_user(group=g) def create_client_session(self, user): cli = omero.client() self.clis.append(cli) un = unwrap(user.getOmeName()) sess = cli.createSession(un, un) return sess
class TestMapAnnotations(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user() def teardown_class(self): self.ua.close() def setup_method(self, method): self.cli = omero.client() un = unwrap(self.user.getOmeName()) self.sess = self.cli.createSession(un, un) def teardown_method(self, method): self.cli.closeSession() @staticmethod def assert_equal_any_order(a, b): assert ((a[0] == b[0] and a[1] == b[1]) or (a[0] == b[1] and a[1] == b[0])) def test_create_map_ann(self): ns = UserAccount.uuid() ma = OmeroMetadata.MapAnnotations(self.sess, namespace=ns) d = {'a': '1', 'bb': 'cc'} aid = ma.create_map_ann({'a': '1', 'bb': 'cc'}) params = omero.sys.ParametersI() params.addLong('id', aid) mr = self.sess.getQueryService().findByQuery( 'from MapAnnotation ann join fetch ann.mapValue where id=:id', params) assert unwrap(mr.getNs()) == ns assert unwrap(mr.getMapValue()) == d @pytest.mark.parametrize('projection', [True, False, ('bb',)]) def test_query_by_map_ann(self, projection): ns = UserAccount.uuid() ma = OmeroMetadata.MapAnnotations(self.sess, namespace=ns) d1 = {'a': '1', 'bb': 'cc'} d1f = {'bb': 'cc'} d2 = {'a': '1', 'bb': 'd'} d2f = {'bb': 'd'} m1 = omero.model.MapAnnotationI() m1.setNs(wrap(ns)) m1.setMapValue(wrap(d1).val) m2 = omero.model.MapAnnotationI() m2.setNs(wrap(ns)) m2.setMapValue(wrap(d2).val) m1 = self.sess.getUpdateService().saveAndReturnObject(m1) m2 = self.sess.getUpdateService().saveAndReturnObject(m2) rs = ma.query_by_map_ann(d1, projection) assert len(rs) == 1 if projection: if projection is True: assert rs.values()[0] == d1 else: assert rs.values()[0] == d1f else: assert unwrap(rs[0].getNs()) == ns assert unwrap(rs[0].getMapValue()) == d1 rs = ma.query_by_map_ann({'a': '1'}, projection) assert len(rs) == 2 if projection: rmv1, rmv2 = rs.values() if projection is True: self.assert_equal_any_order((rmv1, rmv2), (d1, d2)) else: self.assert_equal_any_order((rmv1, rmv2), (d1f, d2f)) else: assert unwrap(rs[0].getNs()) == ns assert unwrap(rs[1].getNs()) == ns rmv1 = unwrap(rs[0].getMapValue()) rmv2 = unwrap(rs[1].getMapValue()) self.assert_equal_any_order((rmv1, rmv2), (d1, d2))
class TestUtils(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user() def teardown_class(self): self.ua.close() def setup_method(self, method): self.cli = omero.client() un = unwrap(self.user.getOmeName()) self.sess = self.cli.createSession(un, un) def teardown_method(self, method): self.cli.closeSession() def create_image(self): szx, szy, szz, szc, szt = 4, 5, 6, 7, 8 def planegen(): for count in xrange(szz * szc * szt): yield numpy.ones((szy, szx), numpy.uint16) * count conn = omero.gateway.BlitzGateway(client_obj=self.cli) im = conn.createImageFromNumpySeq(planegen(), __name__, szz, szc, szt) return im._obj @pytest.mark.parametrize('robject', [True, False]) def test_create_roi_for_plane(self, robject): im = self.create_image() r = utils.create_roi_for_plane( self.sess, unwrap(im.getId()), 2, 4, 6, robject=robject) if not robject: p = omero.sys.ParametersI() p.addId(r) r = self.sess.getQueryService().findByQuery( 'FROM Roi r JOIN FETCH r.shapes where r.id=:id', p) assert r.getImage().getId() == im.getId() assert r.sizeOfShapes() == 1 s = r.getShape(0) assert unwrap(s.getX()) == 0 assert unwrap(s.getY()) == 0 assert unwrap(s.getWidth()) == 4 assert unwrap(s.getHeight()) == 5 assert unwrap(s.getTheZ()) == 2 assert unwrap(s.getTheC()) == 4 assert unwrap(s.getTheT()) == 6 @pytest.mark.parametrize('projection', [True, False]) def test_find_rois_for_plane(self, projection): def getIds(objs, robject=True): if robject: return sorted(unwrap(o.getId()) for o in objs) return sorted(objs) im = self.create_image() iid = unwrap(im.getId()) r1 = utils.create_roi_for_plane( self.sess, unwrap(im.getId()), 2, 3, 4, robject=True) r2 = utils.create_roi_for_plane( self.sess, unwrap(im.getId()), 2, 4, 6, robject=True) r3 = utils.create_roi_for_plane( self.sess, unwrap(im.getId()), 2, 4, 8, robject=True) r3.getShape(0).setWidth(rdouble(1)) r3 = self.sess.getUpdateService().saveAndReturnObject(r3) r4 = utils.create_roi_for_plane( self.sess, unwrap(im.getId()), 2, 4, 8, robject=True) r4.addShape(omero.model.RectI()) r4 = self.sess.getUpdateService().saveAndReturnObject(r4) rs = utils.find_rois_for_plane(self.sess, iid=iid, z=1000) assert rs == [] rs = utils.find_rois_for_plane( self.sess, iid=iid, fullplane=True, projection=projection) assert getIds(rs, not projection) == getIds([r1, r2]) rs = utils.find_rois_for_plane( self.sess, iid=iid, singleshape=False, projection=projection) assert getIds(rs, not projection) == getIds([r1, r2, r4]) rs = utils.find_rois_for_plane( self.sess, iid=iid, fullplane=False, projection=projection) assert getIds(rs, not projection) == getIds([r1, r2, r3]) rs = utils.find_rois_for_plane( self.sess, iid=iid, c=4, fullplane=False, singleshape=False, projection=projection) assert getIds(rs, not projection) == getIds([r2, r3, r4])
class TestFeatureSetTableStore(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user() def teardown_class(self): self.ua.close() def setup_method(self, method): self.cli = omero.client() un = unwrap(self.user.getOmeName()) self.sess = self.cli.createSession(un, un) self.fsmeta = {'fsname': 'a'} ns = UserAccount.uuid() self.nsc = ns + '/c' self.nsr = ns + '/r' def teardown_method(self, method): self.cli.closeSession() @pytest.mark.parametrize('exists', [True, False]) def test_get_table(self, exists): store = FeatureSetTableStoreProxy( self.sess, self.nsc, self.nsr, self.fsmeta) if exists: tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) table = store.get_table() assert table and table == store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) else: with pytest.raises(OmeroTablesFeatureStore.NoTableMatchException): store.get_table() def test_new_table(self): tcols = [ omero.grid.LongArrayColumn('Integers', '', 1), omero.grid.LongArrayColumn('Longs', '', 2), omero.grid.DoubleArrayColumn('Floats', '', 3), omero.grid.StringColumn('String', '', 4), ] desc = [ (int, 'Integers', 1), (long, 'Longs', 2), (float, 'Floats', 3), (str, 'String', 4), ] store = FeatureSetTableStoreProxy( self.sess, self.nsc, self.nsr, self.fsmeta) store.new_table(desc) assert store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) tid = unwrap(store.table.getOriginalFile().getId()) params = omero.sys.ParametersI() params.addString('ns', self.nsc) ann = self.sess.getQueryService().findByQuery( 'from MapAnnotation ann join fetch ann.mapValue where ann.ns=:ns', params) assert unwrap(ann.getNs()) == self.nsc m = unwrap(ann.getMapValue()) assert len(m) == 2 assert m['fsname'] == 'a' assert m['_tableid'] == str(tid) store.close() def test_open_table(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) store = FeatureSetTableStoreProxy( self.sess, self.nsc, self.nsr, self.fsmeta) store.open_table(tid) assert store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) def test_store1(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) store = OmeroTablesFeatureStore.FeatureSetTableStore( self.sess, self.nsc, self.nsr, self.fsmeta) TableStoreHelper.assert_coltypes_equal(store.cols, tcols) assert store.table.getNumberOfRows() == 0 rowmeta = {'objectid': 4} values = [[1, 2], [-1.0]] store.store1(rowmeta, values) assert store.table.getNumberOfRows() == 1 data = store.table.readCoordinates([0]) assert data.columns[0].values == [values[0]] assert data.columns[1].values == [values[1]] params = omero.sys.ParametersI() params.addString('ns', self.nsr) ann = self.sess.getQueryService().findAllByQuery( 'from MapAnnotation ann join fetch ann.mapValue where ann.ns=:ns', params) assert len(ann) == 1 ann = ann[0] assert unwrap(ann.getNs()) == self.nsr m = unwrap(ann.getMapValue()) assert len(m) == 3 assert m['objectid'] == '4' assert m['_tableid'] == str(tid) assert m['_offset'] == '0' store.close() def test_store(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) store = OmeroTablesFeatureStore.FeatureSetTableStore( self.sess, self.nsc, self.nsr, self.fsmeta) TableStoreHelper.assert_coltypes_equal(store.cols, tcols) assert store.table.getNumberOfRows() == 0 rowmetas = [{'objectid': 4}, {'objectid': 5}] valuess = [([1, 2], [-1.0]), ([3, 4], [-2.0])] store.store(rowmetas, valuess) assert store.table.getNumberOfRows() == 2 data = store.table.readCoordinates([0, 1]) assert data.columns[0].values == [valuess[0][0], valuess[1][0]] assert data.columns[1].values == [valuess[0][1], valuess[1][1]] params = omero.sys.ParametersI() params.addString('ns', self.nsr) ann = self.sess.getQueryService().findAllByQuery( 'from MapAnnotation ann join fetch ann.mapValue where ann.ns=:ns', params) assert len(ann) == 2 ann1 = ann[0] ann2 = ann[1] assert unwrap(ann1.getNs()) == self.nsr m1 = unwrap(ann1.getMapValue()) assert len(m1) == 3 assert m1['objectid'] == '4' assert m1['_tableid'] == str(tid) assert m1['_offset'] == '0' assert unwrap(ann2.getNs()) == self.nsr m2 = unwrap(ann2.getMapValue()) assert len(m2) == 3 assert m2['objectid'] == '5' assert m2['_tableid'] == str(tid) assert m2['_offset'] == '1' store.close() def test_fetch(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) store = OmeroTablesFeatureStore.FeatureSetTableStore( self.sess, self.nsc, self.nsr, self.fsmeta) TableStoreHelper.assert_coltypes_equal(store.cols, tcols) assert store.table.getNumberOfRows() == 0 rowmetas = [{'objectid': '4'}, {'objectid': '5'}] valuess = [([1, 2], [-1.0]), ([3, 4], [-2.0])] store.store(rowmetas, valuess) rowquery = {'objectid': '4'} ra, rv = store.fetch(rowquery) assert len(ra) == 1 assert len(rv) == 1 assert ra[0]['objectid'] == '4' assert rv[0] == valuess[0] rowquery = {'objectid': ['4', '5']} ra, rv = store.fetch(rowquery) # Could be either way round assert len(ra) == 2 assert len(rv) == 2 assert compare_2_2([ra[0]['objectid'], ra[1]['objectid']], ['4', '5']) assert compare_2_2(rv, valuess) store.close()
class TestFeatureTableStore(object): def setup_class(self): self.ua = UserAccount() self.user = self.ua.new_user() def teardown_class(self): self.ua.close() def setup_method(self, method): self.cli = omero.client() un = unwrap(self.user.getOmeName()) self.sess = self.cli.createSession(un, un) self.fsmeta = {'fsname': 'a', 'version': '1'} self.rowmetas = [{'id': '1'}, {'id': '2'}] self.valuess = [[1.0, 2.0], [-1.0, -2.0]] self.ns = UserAccount.uuid() self.nsc = self.ns + '/featureset' self.nsr = self.ns + '/sample' def teardown_method(self, method): self.cli.closeSession() def test_create_feature_set(self): fts = OmeroTablesFeatureStore.FeatureTableStore( self.sess, namespace=self.ns, cachesize=1) with pytest.raises(OmeroTablesFeatureStore.NoTableMatchException): store = fts.get_feature_set(self.fsmeta) col_desc = [(int, 'x', 1)] fts.create_feature_set(self.fsmeta, col_desc) store = fts.get_feature_set(self.fsmeta) expected_cols = [omero.grid.LongArrayColumn('x', '', 1)] TableStoreHelper.assert_coltypes_equal(store.cols, expected_cols) with pytest.raises(OmeroTablesFeatureStore.TooManyTablesException): fts.create_feature_set(self.fsmeta, col_desc) fts.close() def test_get_feature_set(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) fts = OmeroTablesFeatureStore.FeatureTableStore( self.sess, namespace=self.ns, cachesize=1) store = fts.get_feature_set(self.fsmeta) assert store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) fts.close() def test_store_fetch(self): tid, tcols = TableStoreHelper.create_table( self.sess, self.nsc, self.fsmeta) fts = OmeroTablesFeatureStore.FeatureTableStore( self.sess, namespace=self.ns, cachesize=1) rowmetas = [{'objectid': 4}, {'objectid': 5}] valuess = [([1, 2], [-1.0]), ([3, 4], [-2.0])] fts.store(self.fsmeta, rowmetas, valuess) assert len(fts.fss) == 1 store = fts.get_feature_set(self.fsmeta) assert store.table TableStoreHelper.assert_coltypes_equal(store.cols, tcols) rowquery = {'objectid': ['4', '5']} ra, rv = store.fetch(rowquery) # Could be either way round assert len(ra) == 2 assert len(rv) == 2 assert compare_2_2([ra[0]['objectid'], ra[1]['objectid']], ['4', '5']) assert compare_2_2(rv, valuess) fts.close()