computedobject.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python
  2. """A representation of an Earth Engine computed object."""
  3. # Using lowercase function naming to match the JavaScript names.
  4. # pylint: disable=g-bad-name
  5. # pylint: disable=g-bad-import-order
  6. from . import data
  7. from . import ee_exception
  8. from . import encodable
  9. from . import serializer
  10. class ComputedObjectMetaclass(type):
  11. """A meta-class that makes type coercion idempotent.
  12. If an instance of a ComputedObject subclass is instantiated by passing
  13. another instance of that class as the sole argument, this short-circuits
  14. and returns that argument.
  15. """
  16. def __call__(cls, *args, **kwargs):
  17. """Creates a computed object, catching self-casts."""
  18. if len(args) == 1 and not kwargs and isinstance(args[0], cls):
  19. # Self-casting returns the argument unchanged.
  20. return args[0]
  21. else:
  22. return type.__call__(cls, *args, **kwargs)
  23. class ComputedObject(encodable.Encodable, metaclass=ComputedObjectMetaclass):
  24. """A representation of an Earth Engine computed object.
  25. This is a base class for most API objects.
  26. The class itself is not abstract as it is used to wrap the return values of
  27. algorithms that produce unrecognized types with the minimal functionality
  28. necessary to interact well with the rest of the API.
  29. ComputedObjects come in two flavors:
  30. 1. If func != null and args != null, the ComputedObject is encoded as an
  31. invocation of func with args.
  32. 2. If func == null and args == null, the ComputedObject is a variable
  33. reference. The variable name is stored in its varName member. Note that
  34. in this case, varName may still be null; this allows the name to be
  35. deterministically generated at a later time. This is used to generate
  36. deterministic variable names for mapped functions, ensuring that nested
  37. mapping calls do not use the same variable name.
  38. """
  39. # Tell pytype not to worry about dynamic attributes.
  40. _HAS_DYNAMIC_ATTRIBUTES = True
  41. def __init__(self, func, args, opt_varName=None):
  42. """Creates a computed object.
  43. Args:
  44. func: The ee.Function called to compute this object, either as an
  45. Algorithm name or an ee.Function object.
  46. args: A dictionary of arguments to pass to the specified function.
  47. Note that the caller is responsible for promoting the arguments
  48. to the correct types.
  49. opt_varName: A variable name. If not None, the object will be encoded
  50. as a reference to a CustomFunction variable of this name, and both
  51. 'func' and 'args' must be None. If all arguments are None, the
  52. object is considered an unnamed variable, and a name will be
  53. generated when it is included in an ee.CustomFunction.
  54. """
  55. if opt_varName and (func or args):
  56. raise ee_exception.EEException(
  57. 'When "opt_varName" is specified, "func" and "args" must be null.')
  58. self.func = func
  59. self.args = args
  60. self.varName = opt_varName
  61. def __eq__(self, other):
  62. # pylint: disable=unidiomatic-typecheck
  63. return (type(self) == type(other) and
  64. self.__dict__ == other.__dict__)
  65. def __ne__(self, other):
  66. return not self.__eq__(other)
  67. def __hash__(self):
  68. return hash(ComputedObject.freeze(self.__dict__))
  69. def getInfo(self):
  70. """Fetch and return information about this object.
  71. Returns:
  72. The object can evaluate to anything.
  73. """
  74. return data.computeValue(self)
  75. def encode(self, encoder):
  76. """Encodes the object in a format compatible with Serializer."""
  77. if self.isVariable():
  78. return {
  79. 'type': 'ArgumentRef',
  80. 'value': self.varName
  81. }
  82. else:
  83. # Encode the function that we're calling.
  84. func = encoder(self.func)
  85. # Built-in functions are encoded as strings under a different key.
  86. key = 'functionName' if isinstance(func, str) else 'function'
  87. # Encode all arguments recursively.
  88. encoded_args = {}
  89. for name, value in self.args.items():
  90. if value is not None:
  91. encoded_args[name] = encoder(value)
  92. return {
  93. 'type': 'Invocation',
  94. 'arguments': encoded_args,
  95. key: func
  96. }
  97. def encode_cloud_value(self, encoder):
  98. if self.isVariable():
  99. ref = self.varName
  100. if ref is None and isinstance(
  101. getattr(encoder, '__self__'), serializer.Serializer):
  102. ref = encoder.__self__.unbound_name
  103. if ref is None:
  104. # We are trying to call getInfo() or make some other server call inside
  105. # a function passed to collection.map() or .iterate(), and the call uses
  106. # one of the function arguments. The argument will be unbound outside of
  107. # the map operation and cannot be evaluated. See the Count Functions
  108. # case in customfunction.py for details on the unbound_name mechanism.
  109. raise ee_exception.EEException(
  110. 'A mapped function\'s arguments cannot be used in client-side operations'
  111. )
  112. return {'argumentReference': ref}
  113. else:
  114. if isinstance(self.func, str):
  115. invocation = {'functionName': self.func}
  116. else:
  117. invocation = self.func.encode_cloud_invocation(encoder)
  118. # Encode all arguments recursively.
  119. encoded_args = {}
  120. for name in sorted(self.args):
  121. value = self.args[name]
  122. if value is not None:
  123. encoded_args[name] = {'valueReference': encoder(value)}
  124. invocation['arguments'] = encoded_args
  125. return {'functionInvocationValue': invocation}
  126. def serialize(
  127. self,
  128. opt_pretty=False,
  129. for_cloud_api=True
  130. ):
  131. """Serialize this object into a JSON string.
  132. Args:
  133. opt_pretty: A flag indicating whether to pretty-print the JSON.
  134. for_cloud_api: Whether the encoding should be done for the Cloud API
  135. or the legacy API.
  136. Returns:
  137. The serialized representation of this object.
  138. """
  139. return serializer.toJSON(
  140. self,
  141. opt_pretty,
  142. for_cloud_api=for_cloud_api
  143. )
  144. def __str__(self):
  145. """Writes out the object in a human-readable form."""
  146. return 'ee.%s(%s)' % (self.name(), serializer.toReadableJSON(self))
  147. def isVariable(self):
  148. """Returns whether this computed object is a variable reference."""
  149. # We can't just check for varName != null, since we allow that
  150. # to remain null until for CustomFunction.resolveNamelessArgs_().
  151. return self.func is None and self.args is None
  152. def aside(self, func, *var_args):
  153. """Calls a function passing this object as the first argument.
  154. Returns the object itself for chaining. Convenient e.g. when debugging:
  155. c = (ee.ImageCollection('foo').aside(logging.info)
  156. .filterDate('2001-01-01', '2002-01-01').aside(logging.info)
  157. .filterBounds(geom).aside(logging.info)
  158. .aside(addToMap, {'min': 0, 'max': 142})
  159. .select('a', 'b'))
  160. Args:
  161. func: The function to call.
  162. *var_args: Any extra arguments to pass to the function.
  163. Returns:
  164. The same object, for chaining.
  165. """
  166. func(self, *var_args)
  167. return self
  168. @classmethod
  169. def name(cls):
  170. """Returns the name of the object, used in __str__()."""
  171. return 'ComputedObject'
  172. @classmethod
  173. def _cast(cls, obj):
  174. """Cast a ComputedObject to a new instance of the same class as this.
  175. Args:
  176. obj: The object to cast.
  177. Returns:
  178. The cast object, and instance of the class on which this method is called.
  179. """
  180. if isinstance(obj, cls):
  181. return obj
  182. else:
  183. result = cls.__new__(cls)
  184. result.func = obj.func
  185. result.args = obj.args
  186. result.varName = obj.varName
  187. return result
  188. @staticmethod
  189. def freeze(obj):
  190. """Freeze a list or dict so it can be hashed."""
  191. if isinstance(obj, dict):
  192. return frozenset(
  193. (key, ComputedObject.freeze(val)) for key, val in obj.items())
  194. elif isinstance(obj, list):
  195. return tuple(map(ComputedObject.freeze, obj))
  196. else:
  197. return obj