def testIteration(self): """Verifies the behavior of the iterate() method.""" collection = ee.ImageCollection('foo') first = ee.Image(0) algorithm = lambda img, prev: img.addBands(ee.Image(prev)) result = collection.iterate(algorithm, first) self.assertEquals(ee.ApiFunction.lookup('Collection.iterate'), result.func) self.assertEquals(collection, result.args['collection']) self.assertEquals(first, result.args['first']) # Need to do a serialized comparison for the function body because # variables returned from CustomFunction.variable() do not implement # __eq__. sig = { 'returns': 'Object', 'args': [{ 'name': '_MAPPING_VAR_0_0', 'type': 'Image' }, { 'name': '_MAPPING_VAR_0_1', 'type': 'Object' }] } expected_function = ee.CustomFunction(sig, algorithm) self.assertEquals(expected_function.serialize(), result.args['function'].serialize())
def testMapping(self): lst = ee.List(['foo', 'bar']) body = lambda s: ee.String(s).cat('bar') mapped = lst.map(body) self.assertIsInstance(mapped, ee.List) self.assertEqual(ee.ApiFunction.lookup('List.map'), mapped.func) self.assertEqual(lst, mapped.args['list']) # Need to do a serialized comparison for the function body because # variables returned from CustomFunction.variable() do not implement # __eq__. sig = { 'returns': 'Object', 'args': [{ 'name': '_MAPPING_VAR_0_0', 'type': 'Object' }] } expected_function = ee.CustomFunction(sig, body) self.assertEqual(expected_function.serialize(), mapped.args['baseAlgorithm'].serialize()) self.assertEqual( expected_function.serialize(for_cloud_api=True), mapped.args['baseAlgorithm'].serialize(for_cloud_api=True))
def testSerialization(self): """Verifies a complex serialization case.""" class ByteString(ee.Encodable): """A custom Encodable class that does not use invocations. This one is actually supported by the EE API encoding. """ def __init__(self, value): """Creates a bytestring with a given string value.""" self._value = value def encode(self, unused_encoder): # pylint: disable-msg=g-bad-name return { 'type': 'Bytes', 'value': self._value } call = ee.ComputedObject('String.cat', {'string1': 'x', 'string2': 'y'}) body = lambda x, y: ee.CustomFunction.variable(None, 'y') sig = {'returns': 'Object', 'args': [ {'name': 'x', 'type': 'Object'}, {'name': 'y', 'type': 'Object'}]} custom_function = ee.CustomFunction(sig, body) to_encode = [ None, True, 5, 7, 3.4, 2.5, 'hello', ee.Date(1234567890000), ee.Geometry(ee.Geometry.LineString(1, 2, 3, 4), 'SR-ORG:6974'), ee.Geometry.Polygon([ [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[5, 6], [7, 6], [7, 8], [5, 8]], [[1, 1], [2, 1], [2, 2], [1, 2]] ]), ByteString('aGVsbG8='), { 'foo': 'bar', 'baz': call }, call, custom_function ] self.assertEquals(apitestcase.ENCODED_JSON_SAMPLE, json.loads(serializer.toJSON(to_encode)))
def testCallAndApply(self): """Verifies library initialization.""" # Use a custom set of known functions. def MockSend(path, params, unused_method=None, unused_raw=None): if path == '/algorithms': return { 'fakeFunction': { 'type': 'Algorithm', 'args': [ {'name': 'image1', 'type': 'Image'}, {'name': 'image2', 'type': 'Image'} ], 'returns': 'Image' }, 'Image.constant': apitestcase.BUILTIN_FUNCTIONS['Image.constant'] } else: raise Exception('Unexpected API call to %s with %s' % (path, params)) ee.data.send_ = MockSend ee.Initialize(None) image1 = ee.Image(1) image2 = ee.Image(2) expected = ee.Image(ee.ComputedObject( ee.ApiFunction.lookup('fakeFunction'), {'image1': image1, 'image2': image2})) applied_with_images = ee.apply( 'fakeFunction', {'image1': image1, 'image2': image2}) self.assertEquals(expected, applied_with_images) applied_with_numbers = ee.apply('fakeFunction', {'image1': 1, 'image2': 2}) self.assertEquals(expected, applied_with_numbers) called_with_numbers = ee.call('fakeFunction', 1, 2) self.assertEquals(expected, called_with_numbers) # Test call and apply() with a custom function. sig = {'returns': 'Image', 'args': [{'name': 'foo', 'type': 'Image'}]} func = ee.CustomFunction(sig, lambda foo: ee.call('fakeFunction', 42, foo)) expected_custom_function_call = ee.Image( ee.ComputedObject(func, {'foo': ee.Image(13)})) self.assertEquals(expected_custom_function_call, ee.call(func, 13)) self.assertEquals(expected_custom_function_call, ee.apply(func, {'foo': 13})) # Test None promotion. called_with_null = ee.call('fakeFunction', None, 1) self.assertEquals(None, called_with_null.args['image1'])
def testMapping(self): """Verifies the behavior of the map() method.""" collection = ee.ImageCollection('foo') mapped = collection.map(lambda img: img.select('bar')) self.assertTrue(isinstance(mapped, ee.ImageCollection)) self.assertEquals(ee.ApiFunction.lookup('Collection.map'), mapped.func) self.assertEquals(collection, mapped.args['collection']) # Need to do a serialized comparison for the function body because # variables returned from CustomFunction.variable() do not implement # __eq__. sig = { 'returns': 'Image', 'args': [{'name': '_MAPPING_VAR_0_0', 'type': 'Image'}] } expected_function = ee.CustomFunction(sig, lambda img: img.select('bar')) self.assertEquals(expected_function.serialize(), mapped.args['baseAlgorithm'].serialize())
def testSerialization(self): """Verifies a complex serialization case.""" class ByteString(ee.Encodable): """A custom Encodable class that does not use invocations. This one is actually supported by the EE API encoding. """ def __init__(self, value): """Creates a bytestring with a given string value.""" self._value = value def encode(self, unused_encoder): # pylint: disable-msg=g-bad-name return {'type': 'Bytes', 'value': self._value} def encode_cloud_value(self, unused_encoder): # Proto3 JSON embedding of "bytes" values uses base64 encoding, which # this already is. return {'bytesValue': self._value} call = ee.ComputedObject('String.cat', { 'string1': 'x', 'string2': 'y' }) body = lambda x, y: ee.CustomFunction.variable(None, 'y') sig = { 'returns': 'Object', 'args': [{ 'name': 'x', 'type': 'Object' }, { 'name': 'y', 'type': 'Object' }] } custom_function = ee.CustomFunction(sig, body) to_encode = [ None, True, 5, 7, 3.4, 112233445566778899, 'hello', ee.Date(1234567890000), ee.Geometry(ee.Geometry.LineString(1, 2, 3, 4), 'SR-ORG:6974'), ee.Geometry.Polygon([[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[5, 6], [7, 6], [7, 8], [5, 8]], [[1, 1], [2, 1], [2, 2], [1, 2]]]), ByteString('aGVsbG8='), { 'foo': 'bar', 'baz': call }, call, custom_function ] self.assertEqual( apitestcase.ENCODED_JSON_SAMPLE, json.loads(serializer.toJSON(to_encode, for_cloud_api=False))) encoded = serializer.encode(to_encode, for_cloud_api=True) self.assertEqual(apitestcase.ENCODED_CLOUD_API_JSON_SAMPLE, encoded) pretty_encoded = serializer.encode(to_encode, is_compound=False, for_cloud_api=True) self.assertEqual(apitestcase.ENCODED_CLOUD_API_JSON_SAMPLE_PRETTY, pretty_encoded) encoded_json = serializer.toJSON(to_encode, for_cloud_api=True) decoded_encoded_json = json.loads(encoded_json) self.assertEqual(encoded, decoded_encoded_json)