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
def testConversionMethod(self): assert None == rtype(None) assert rlong(1) == rtype(rlong(1)) # Returns self assert rbool(True) == rtype(True) # Unsupported # assert rdouble(0) == rtype(Double.valueOf(0)) assert rfloat(0) == rtype(float(0)) if sys.version_info < (3, 0, 0): assert rlong(0) == rtype(long(0)) else: assert rint(0) == rtype(long(0)) assert rint(0) == rtype(int(0)) assert rstring("string") == rtype("string") # Unsupported # assert rtime(time) == rtype(new Timestamp(time)) rtype(omero.model.ImageI()) rtype(omero.grid.JobParams()) rtype(set([rlong(1)])) rtype(list([rlong(2)])) rtype({}) # Unsupported # rtype(array) try: rtype(()) assert False, "Shouldn't be able to handle this yet" except omero.ClientError: pass
def get_meta_map(self): self.__initcheck() metadata = {} attr = self.__mea.attrs keys = list(self.__mea.attrs._v_attrnamesuser) for key in keys: val = attr[key] if isinstance(val, float): val = rfloat(val) elif isinstance(val, TABLES_METADATA_INT_TYPES): val = rlong(val) elif isinstance(val, basestring): val = rstring(val) else: raise omero.ValidationException("BAD TYPE: %s" % type(val)) metadata[key] = val return metadata
def get_meta_map(self): self.__initcheck() metadata = {} attr = self.__mea.attrs keys = list(self.__mea.attrs._v_attrnamesuser) for key in keys: val = attr[key] if isinstance(val, float): val = rfloat(val) elif isinstance(val, int): val = rint(val) elif isinstance(val, long): val = rlong(val) elif isinstance(val, str): val = rstring(val) else: raise omero.ValidationException("BAD TYPE: %s" % type(val)) metadata[key] = val return metadata
def test2855MetadataMethods(self): """ Tests the various metadata methods for a table """ grid = self.client.sf.sharedResources() table = grid.newTable(1, "/test") assert table def clean(m): """ Unwraps the RTypes for easier processing and gets rid of auto-generated values for easier testing. """ m = unwrap(m) del m["initialized"] del m["version"] return m try: print table.getOriginalFile().id.val lc = columns.LongColumnI('lc', 'desc', [1]) table.initialize([lc]) assert len(clean(table.getAllMetadata())) == 0 # Set a string table.setMetadata("s", rstring("b")) assert "b" == unwrap(table.getMetadata("s")) assert {"s": "b"} == clean(table.getAllMetadata()) # Set an int table.setMetadata("i", rint(1)) assert 1 == unwrap(table.getMetadata("i")) assert {"s": "b", "i": 1} == clean(table.getAllMetadata()) # Set a float table.setMetadata("f", rfloat(1)) assert 1 == unwrap(table.getMetadata("f")) assert {"s": "b", "i": 1, "f": 1} == clean(table.getAllMetadata()) finally: table.close()
def testConversionMethod(self): assert None == rtype(None) assert rlong(1) == rtype(rlong(1)) # Returns self assert rbool(True) == rtype(True) # Unsupported # assert rdouble(0) == rtype(Double.valueOf(0)) assert rfloat(0) == rtype(float(0)) assert rlong(0) == rtype(long(0)) assert rint(0) == rtype(int(0)) assert rstring("string") == rtype("string") # Unsupported # assert rtime(time) == rtype(new Timestamp(time)) rtype(omero.model.ImageI()) rtype(omero.grid.JobParams()) rtype(set([rlong(1)])) rtype(list([rlong(2)])) rtype({}) # Unsupported # rtype(array) try: rtype(()) assert False, "Shouldn't be able to handle this yet" except omero.ClientError: pass
def testUnwrap(self): # NUMS plain assert 0 == unwrap(0) assert 1 == unwrap(1) assert 0.0 == unwrap(0.0) assert 1.0 == unwrap(1.0) # NUMS rtyped assert 0 == unwrap(rint(0)) assert 0 == unwrap(rlong(0)) assert 1 == unwrap(rint(1)) assert 1 == unwrap(rlong(1)) assert 0.0 == unwrap(rfloat(0.0)) assert 0.0 == unwrap(rdouble(0.0)) assert 1.0 == unwrap(rfloat(1.0)) assert 1.0 == unwrap(rdouble(1.0)) # STRINGS assert "" == unwrap("") assert "str" == unwrap("str") # BOOL assert True == unwrap(True) assert False == unwrap(False) assert True == unwrap(rbool(True)) assert False == unwrap(rbool(False)) # TIME # Bit odd, do we want the long for time, or transformed? assert 0 == unwrap(rtime(0)) assert 1 == unwrap(rtime(1)) # CLASS # same for class, should we map it? assert "k" == unwrap(rclass("k")) # INTERNAL color = omero.Color() assert color == unwrap(rinternal(color)) # OBJECT image = omero.model.ImageI() assert image == unwrap(robject(image)) # COLLECTIONS # empty assert [] == unwrap([]) assert {} == unwrap({}) assert set() == unwrap(set()) # plain in collection assert [1] == unwrap([1]) # rtype in collection assert [1] == unwrap([rint(1)]) assert {"a": 1} == unwrap({"a": 1}) # plain in rcollection ILLEGAL # assert [1] == unwrap(rlist([1])) # assert {"a":1} == unwrap(rmap({"a":1})) # rtype in rcollection assert [1] == unwrap(rlist([rint(1)])) assert {"a": 1} == unwrap(rmap({"a": rint(1)})) # rtype keys ILLEGAL # assert {"a":1} == unwrap(rmap({rstring("a"):rint(1)})) # recursion, ticket:1977 m1 = rmap({"a": rint(1)}) m1.val["m1"] = m1 m2 = {"a": 1} m2["m1"] = m2 unwrap(m1) assert m2["a"] == unwrap(m1)["a"] # Can't compare directly "maximum recursion depth exceeded in cmp" assert type(m2["m1"]) == type(unwrap(m1)["m1"])
def testPassThroughNoneAndRTypes(self): """ To prevent having to check for isintance(int,...) or isintance(RInt,...) all over the place, the static methods automatically check for acceptable types and simply pass them through. Similarly, the primitive types all check for None and return a null RType if necessary. """ # Bool assert None == rbool(None) assert rbool(True) == rbool(rbool(True)) assert rbool(True) == rbool(1) assert rbool(False) == rbool(0) # Double assert None == rdouble(None) assert rdouble(0.0) == rdouble(rdouble(0.0)) assert rdouble(0.0) == rdouble(rdouble(0)) assert rdouble(0.0) == rdouble(rdouble("0.0")) pytest.raises(ValueError, lambda: rdouble("string")) # Float assert None == rfloat(None) assert rfloat(0.0) == rfloat(rfloat(0.0)) assert rfloat(0.0) == rfloat(rfloat(0)) assert rfloat(0.0) == rfloat(rfloat("0.0")) pytest.raises(ValueError, lambda: rfloat("string")) # Long assert None == rlong(None) assert rlong(0) == rlong(rlong(0)) assert rlong(0) == rlong(rlong(0.0)) assert rlong(0) == rlong(rlong("0")) pytest.raises(ValueError, lambda: rlong("string")) # Time assert None == rtime(None) assert rtime(0) == rtime(rtime(0)) assert rtime(0) == rtime(rtime(0.0)) assert rtime(0) == rtime(rtime("0")) pytest.raises(ValueError, lambda: rtime("string")) # Int assert None == rint(None) assert rint(0) == rint(rint(0)) assert rint(0) == rint(rint(0.0)) assert rint(0) == rint(rint("0")) pytest.raises(ValueError, lambda: rint("string")) # # Starting here handling of null is different. # # String assert rstring("") == rstring(None) assert rstring("a") == rstring(rstring("a")) assert rstring("0") == rstring(0) # Class assert rclass("") == rclass(None) assert rclass("c") == rclass(rclass("c")) pytest.raises(ValueError, lambda: rclass(0)) # Internal internal = omero.Internal() assert rinternal(None) == rinternal(None) assert rinternal(internal) == rinternal(rinternal(internal)) pytest.raises(ValueError, lambda: rinternal("string")) # Object obj = omero.model.ImageI() assert robject(None) == robject(None) assert robject(obj) == robject(robject(obj)) pytest.raises(ValueError, lambda: robject("string")) # # Same does not hold for collections # # Array assert rarray([]) == rarray(None) # assert rarray(obj) == rarray(rarray(obj)) # pytest.raises(ValueError, lambda : rarray("string")) # List assert rlist([]) == rlist(None) # assert rlist(obj) == rlist(rlist(obj)) # pytest.raises(ValueError, lambda : rlist("string")) # Set assert rset([]) == rset(None) # assert rset(obj) == rset(rset(obj)) # pytest.raises(ValueError, lambda : rset("string")) # Map assert rmap({}) == rmap(None)
def _testCreateAllColumnsAndMetadata(self): grid = self.client.sf.sharedResources() repoMap = grid.repositories() repoObj = repoMap.descriptions[0] table = grid.newTable(repoObj.id.val, "/test") assert table # Supported metadata types # https://github.com/ome/omero-py/blob/v5.5.1/src/omero/hdfstorageV2.py#L466 metadata = { 'string': rstring('a'), 'int': rint(1), 'long': rlong(1), 'double': rfloat(0.1), } fcol = columns.FileColumnI('filecol', 'file col') fcol.values = [10, 20] icol = columns.ImageColumnI('imagecol', 'image col') icol.values = [30, 40] rcol = columns.RoiColumnI('roicol', 'roi col') rcol.values = [50, 60] wcol = columns.WellColumnI('wellcol', 'well col') wcol.values = [70, 80] pcol = columns.PlateColumnI('platecol', 'plate col') pcol.values = [90, 100] bcol = columns.BoolColumnI('boolcol', 'bool col') bcol.values = [True, False] dcol = columns.DoubleColumnI('doublecol', 'double col') dcol.values = [0.25, 0.5] lcol = columns.LongColumnI('longcol', 'long col') lcol.values = [-1, -2] scol = columns.StringColumnI('stringcol', 'string col', 3) scol.values = ["abc", "de"] larr = columns.LongArrayColumnI('longarr', 'longarr col', 2) larr.values = [[-2, -1], [1, 2]] farr = columns.FloatArrayColumnI('floatarr', 'floatarr col', 2) farr.values = [[-0.25, -0.5], [0.125, 0.0625]] darr = columns.DoubleArrayColumnI('doublearr', 'doublearr col', 2) darr.values = [[-0.25, -0.5], [0.125, 0.0625]] dscol = columns.DatasetColumnI('datasetcol', 'dataset col') dscol.values = [110, 120] mask = self.createMaskCol() cols = [ fcol, icol, rcol, wcol, pcol, bcol, dcol, lcol, scol, mask, larr, farr, darr, dscol ] table.initialize(cols) table.setAllMetadata(metadata) table.addData(cols) data = table.readCoordinates([0, 1]) testf = data.columns[0].values assert 10 == testf[0] assert 20 == testf[1] testi = data.columns[1].values assert 30 == testi[0] assert 40 == testi[1] testr = data.columns[2].values assert 50 == testr[0] assert 60 == testr[1] testw = data.columns[3].values assert 70 == testw[0] assert 80 == testw[1] testp = data.columns[4].values assert 90 == testp[0] assert 100 == testp[1] testb = data.columns[5].values assert testb[0] assert not testb[1] testd = data.columns[6].values assert 0.25 == testd[0] assert 0.5 == testd[1] testl = data.columns[7].values assert -1 == testl[0] assert -2 == testl[1] tests = data.columns[8].values assert "abc" == tests[0] assert "de" == tests[1] testm = data.columns[9] self.checkMaskCol(testm) testla = data.columns[10].values assert [-2, -1] == testla[0] assert [1, 2] == testla[1] testfa = data.columns[11].values assert [-0.25, -0.5] == testfa[0] assert [0.125, 0.0625] == testfa[1] testda = data.columns[12].values assert [-0.25, -0.5] == testda[0] assert [0.125, 0.0625] == testda[1] testds = data.columns[13].values assert 110 == testds[0] assert 120 == testds[1] ofile = table.getOriginalFile() print("Created OriginalFile:", ofile.getId().val) return table
def testCreateAllColumnsAndMetadata_5_3_4(self): """ Call this method to create the reference HDF5 table under a 5.3.4 Python 2.7 server. The OriginalFile ID of the table will be printed, and can be used to find the file under ${omero.data.dir}/Files/. Alternatively download it using ``omero download OriginalFile:FileID output.h5`` To run manually goto ``components/tools/OmeroPy``, and run: ``pytest test/integration/tablestest/test_backwards_compatibility.py\ -s -k testCreateAllColumnsAndMetadata_5_3_4`` """ grid = self.client.sf.sharedResources() repoMap = grid.repositories() repoObj = repoMap.descriptions[0] table = grid.newTable(repoObj.id.val, "/test") assert table # Supported metadata types # https://github.com/ome/omero-py/blob/v5.5.1/src/omero/hdfstorageV2.py#L466 metadata = { 'string': rstring('a'), 'int': rint(1), 'long': rlong(1), 'double': rfloat(0.1), } fcol = columns.FileColumnI('filecol', 'file col') fcol.values = [10, 20] icol = columns.ImageColumnI('imagecol', 'image col') icol.values = [30, 40] rcol = columns.RoiColumnI('roicol', 'roi col') rcol.values = [50, 60] wcol = columns.WellColumnI('wellcol', 'well col') wcol.values = [70, 80] pcol = columns.PlateColumnI('platecol', 'plate col') pcol.values = [90, 100] bcol = columns.BoolColumnI('boolcol', 'bool col') bcol.values = [True, False] dcol = columns.DoubleColumnI('doublecol', 'double col') dcol.values = [0.25, 0.5] lcol = columns.LongColumnI('longcol', 'long col') lcol.values = [-1, -2] scol = columns.StringColumnI('stringcol', 'string col', 3) scol.values = ["abc", "de"] larr = columns.LongArrayColumnI('longarr', 'longarr col', 2) larr.values = [[-2, -1], [1, 2]] farr = columns.FloatArrayColumnI('floatarr', 'floatarr col', 2) farr.values = [[-0.25, -0.5], [0.125, 0.0625]] darr = columns.DoubleArrayColumnI('doublearr', 'doublearr col', 2) darr.values = [[-0.25, -0.5], [0.125, 0.0625]] # DatasetColumn is broken! mask = self.createMaskCol() cols = [ fcol, icol, rcol, wcol, pcol, bcol, dcol, lcol, scol, mask, larr, farr, darr ] table.initialize(cols) table.setAllMetadata(metadata) table.addData(cols) data = table.readCoordinates([0, 1]) testf = data.columns[0].values assert 10 == testf[0] assert 20 == testf[1] testi = data.columns[1].values assert 30 == testi[0] assert 40 == testi[1] testr = data.columns[2].values assert 50 == testr[0] assert 60 == testr[1] testw = data.columns[3].values assert 70 == testw[0] assert 80 == testw[1] testp = data.columns[4].values assert 90 == testp[0] assert 100 == testp[1] testb = data.columns[5].values assert testb[0] assert not testb[1] testd = data.columns[6].values assert 0.25 == testd[0] assert 0.5 == testd[1] testl = data.columns[7].values assert -1 == testl[0] assert -2 == testl[1] tests = data.columns[8].values assert "abc" == tests[0] assert "de" == tests[1] testm = data.columns[9] self.checkMaskCol(testm) testla = data.columns[10].values assert [-2, -1] == testla[0] assert [1, 2] == testla[1] testfa = data.columns[11].values assert [-0.25, -0.5] == testfa[0] assert [0.125, 0.0625] == testfa[1] testda = data.columns[12].values assert [-0.25, -0.5] == testda[0] assert [0.125, 0.0625] == testda[1] ofile = table.getOriginalFile() print("Created OriginalFile:", ofile.getId().val) table.close()
def testObjectCreationEqualsAndHash(self): # RBool true1 = rbool(True) true2 = rbool(True) false1 = rbool(False) false2 = rbool(False) assert true1 == true2 assert false1 == false2 assert true1.getValue() assert not false1.getValue() assert true1 == true2 assert true1 != false1 # RDouble double_zero1 = rdouble(0.0) double_zero2 = rdouble(0.0) double_notzero1 = rdouble(1.1) double_notzero1b = rdouble(1.1) double_notzero2 = rdouble(2.2) assert double_zero1.getValue() == 0.0 assert double_notzero1.getValue() == 1.1 assert double_zero1 == double_zero2 assert double_zero1 != double_notzero1 assert double_notzero1 == double_notzero1b assert double_notzero1 != double_notzero2 # RFloat float_zero1 = rfloat(0.0) float_zero2 = rfloat(0.0) float_notzero1 = rfloat(1.1) float_notzero1b = rfloat(1.1) float_notzero2 = rfloat(2.2) assert float_zero1.getValue() == 0.0 assert float_notzero1.getValue() == 1.1 assert float_zero1 == float_zero2 assert float_zero1 != float_notzero1 assert float_notzero1 == float_notzero1b assert float_notzero1 != float_notzero2 # RInt int_zero1 = rint(0) int_zero2 = rint(0) int_notzero1 = rint(1) int_notzero1b = rint(1) int_notzero2 = rint(2) assert int_zero1.getValue() == 0 assert int_notzero1.getValue() == 1 assert int_zero1 == int_zero2 assert int_zero1 != int_notzero1 assert int_notzero1 == int_notzero1b assert int_notzero1 != int_notzero2 # RLong long_zero1 = rlong(0) long_zero2 = rlong(0) long_notzero1 = rlong(1) long_notzero1b = rlong(1) long_notzero2 = rlong(2) assert long_zero1.getValue() == 0 assert long_notzero1.getValue() == 1 assert long_zero1 == long_zero2 assert long_zero1 != long_notzero1 assert long_notzero1 == long_notzero1b assert long_notzero1 != long_notzero2 # RTime time_zero1 = rtime(0) time_zero2 = rtime(0) time_notzero1 = rtime(1) time_notzero1b = rtime(1) time_notzero2 = rtime(2) assert time_zero1.getValue() == 0 assert time_notzero1.getValue() == 1 assert time_zero1 == time_zero2 assert time_zero1 != time_notzero1 assert time_notzero1 == time_notzero1b assert time_notzero1 != time_notzero2 # RInternal internal_null1 = rinternal(None) internal_null2 = rinternal(None) internal_notnull1 = rinternal(omero.grid.JobParams()) internal_notnull2 = rinternal(omero.grid.JobParams()) assert internal_null1 == internal_null2 assert internal_null1 == internal_null2 assert internal_null1 != internal_notnull2 assert internal_notnull1 == internal_notnull1 assert internal_notnull1 != internal_notnull2 # RObject object_null1 = robject(None) object_null2 = robject(None) object_notnull1 = robject(omero.model.ImageI()) object_notnull2 = robject(omero.model.ImageI()) assert object_null1 == object_null2 assert object_null1 == object_null2 assert object_null1 != object_notnull2 assert object_notnull1 == object_notnull1 assert object_notnull1 != object_notnull2 # RString string_null1 = rstring(None) string_null2 = rstring(None) string_notnull1 = rstring("str1") string_notnull1b = rstring("str1") string_notnull2 = rstring("str2") assert string_null1 == string_null2 assert string_null1 == string_null2 assert string_null1 != string_notnull2 assert string_notnull1 == string_notnull1 assert string_notnull1 != string_notnull2 assert string_notnull1 == string_notnull1b # RClass class_null1 = rclass(None) class_null2 = rclass(None) class_notnull1 = rclass("str1") class_notnull1b = rclass("str1") class_notnull2 = rclass("str2") assert class_null1 == class_null2 assert class_null1 == class_null2 assert class_null1 != class_notnull2 assert class_notnull1 == class_notnull1 assert class_notnull1 != class_notnull2 assert class_notnull1 == class_notnull1b
def test2855MetadataMethods(self): """ Tests the various metadata methods for a table """ grid = self.client.sf.sharedResources() table = grid.newTable(1, "/test") assert table def clean(m): """ Unwraps the RTypes for easier processing and gets rid of auto-generated values for easier testing. """ m = unwrap(m) assert "__initialized" in m assert "__version" in m del m["__initialized"] del m["__version"] return m try: print table.getOriginalFile().id.val lc = columns.LongColumnI('lc', 'desc', [1]) table.initialize([lc]) assert len(clean(table.getAllMetadata())) == 0 # Set a string table.setMetadata("s", rstring("b")) assert "b" == unwrap(table.getMetadata("s")) assert {"s": "b"} == clean(table.getAllMetadata()) # Set an int table.setMetadata("i", rint(1)) assert 1 == unwrap(table.getMetadata("i")) assert {"s": "b", "i": 1} == clean(table.getAllMetadata()) # Set a float table.setMetadata("f", rfloat(1)) assert 1 == unwrap(table.getMetadata("f")) assert {"s": "b", "i": 1, "f": 1} == clean(table.getAllMetadata()) # Replace all user-metadata table.setAllMetadata({"s2": rstring("b2"), "l2": rlong(3)}) assert {"s2": "b2", "l2": 3} == clean(table.getAllMetadata()) assert table.getMetadata("s") is None table.setAllMetadata({}) assert {} == clean(table.getAllMetadata()) table.setMetadata("z", rint(1)) with pytest.raises(omero.ApiUsageException): table.setMetadata("__z", rint(2)) assert {"z": 1} == clean(table.getAllMetadata()) with pytest.raises(omero.ValidationException): table.setMetadata("z", rint(None)) finally: table.delete() table.close()
def test2855MetadataMethods(self): """ Tests the various metadata methods for a table """ grid = self.client.sf.sharedResources() table = grid.newTable(1, "/test") assert table def clean(m): """ Unwraps the RTypes for easier processing and gets rid of auto-generated values for easier testing. """ m = unwrap(m) assert "__initialized" in m assert "__version" in m del m["__initialized"] del m["__version"] return m try: print(table.getOriginalFile().id.val) lc = columns.LongColumnI('lc', 'desc', [1]) table.initialize([lc]) assert len(clean(table.getAllMetadata())) == 0 # Set a string table.setMetadata("s", rstring("b")) assert "b" == unwrap(table.getMetadata("s")) assert {"s": "b"} == clean(table.getAllMetadata()) # Set an int table.setMetadata("i", rint(1)) assert 1 == unwrap(table.getMetadata("i")) assert {"s": "b", "i": 1} == clean(table.getAllMetadata()) # Set a float table.setMetadata("f", rfloat(1)) assert 1 == unwrap(table.getMetadata("f")) assert {"s": "b", "i": 1, "f": 1} == clean(table.getAllMetadata()) # Replace all user-metadata table.setAllMetadata({"s2": rstring("b2"), "l2": rlong(3)}) assert {"s2": "b2", "l2": 3} == clean(table.getAllMetadata()) assert table.getMetadata("s") is None table.setAllMetadata({}) assert {} == clean(table.getAllMetadata()) table.setMetadata("z", rint(1)) with pytest.raises(omero.ApiUsageException): table.setMetadata("__z", rint(2)) assert {"z": 1} == clean(table.getAllMetadata()) with pytest.raises(omero.ValidationException): table.setMetadata("z", rint(None)) finally: table.delete() table.close()