Esempio n. 1
0
    def __init__(self, iterable):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic List; the item type must be specified (like `List[Int]`)"
            )

        if isinstance(iterable, type(self)):
            self.graft = client.apply_graft("list.copy", iterable)
        elif isinstance(iterable, List):
            raise ProxyTypeError(
                "Cannot convert {} to {}, since they have different value types"
                .format(type(iterable).__name__,
                        type(self).__name__))
        else:
            if not isinstance(iterable, abc.Iterable):
                raise ProxyTypeError(
                    "Expected an iterable, got {}".format(iterable))
            value_type = self._type_params[0]

            def checker_promoter(i, x):
                try:
                    return value_type._promote(x)
                except ProxyTypeError:
                    raise ProxyTypeError(
                        "{}: Expected iterable values of type {}, but for item {}, got {!r}"
                        .format(type(self).__name__, value_type, i, x))

            iterable = tuple(
                checker_promoter(i, x) for i, x in enumerate(iterable))
            self.graft = client.apply_graft("list", *iterable)
Esempio n. 2
0
    def __init__(self, arr):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic Array; "
                "the dtype and dimensionality must be specified (like `Array[Float, 3]`). "
                "Alternatively, Arrays can be instantiated with `from_numpy` "
                "(like `Array.from_numpy(my_array)`).")

        if isinstance(arr, np.ndarray):
            if arr.dtype.kind != WF_TO_DTYPE_KIND[self.dtype]:
                raise TypeError("Invalid dtype {} for an {}".format(
                    arr.dtype,
                    type(self).__name__))
            if arr.ndim != self.ndim:
                raise ValueError(
                    "Cannot instantiate a {}-dimensional Workflows Array from a "
                    "{}-dimensional NumPy array".format(self.ndim, arr.ndim))

            arr_list = arr.tolist()
            self.graft = client.apply_graft("array.create", arr_list)

        else:
            list_type = functools.reduce(
                lambda accum, cur: List[accum],
                range(self._type_params[1]),
                self._type_params[0],
            )
            try:
                arr = list_type._promote(arr)
            except ProxyTypeError:
                raise ValueError(
                    "Cannot instantiate an Array from {!r}".format(arr))

            self.graft = client.apply_graft("array.create", arr)
Esempio n. 3
0
    def __init__(self, imgs, func):
        """
        You should construct `ImageCollectionGroupby` from `.ImageCollection.groupby` in most cases.

        Parameters
        ----------
        imgs: ImageCollection
            `ImageCollection` to group
        func: Function
            Key function which takes an `Image` and returns which group the `Image` belongs to.
            Must return an instance of ``self.key_type``.

        Returns
        -------
        grouped: ImageCollectionGroupby
        """
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic {}; the item type must be specified".format(
                    type(self).__name__
                )
            )

        self.graft = client.apply_graft("ImageCollectionGroupby.create", imgs, func)

        # store this once so that repeated calls to `.groups` refer to the same object in the graft
        self._groups = Dict[self.key_type, ImageCollection]._from_apply(
            "ImageCollectionGroupby.groups", self
        )
Esempio n. 4
0
    def __init__(self, iterable):
        value_types = self._type_params
        if value_types is None:
            raise TypeError(
                "Cannot instantiate a generic Tuple; the item types must be specified (like `Tuple[Int, Float]`)"
            )
        # TODO: copy constructor
        if not isinstance(iterable, abc.Iterable):
            raise ProxyTypeError(
                "Expected an iterable, got {}".format(iterable))

        try:
            length = len(iterable)
        except TypeError:
            iterable = tuple(iterable)
            length = len(iterable)

        if length != len(self._type_params):
            raise ProxyTypeError(
                "To construct {}, expected an iterable of {} items, "
                "but got {} items".format(
                    type(self).__name__, len(self._type_params), length))

        def checker_promoter(i, x):
            cls = value_types[i]
            try:
                return cls._promote(x)
            except ProxyTypeError:
                raise ProxyTypeError(
                    "While constructing {}, expected {} for tuple element {}, but got {!r}"
                    .format(type(self).__name__, cls, i, x))

        iterable = tuple(
            checker_promoter(i, x) for i, x in enumerate(iterable))
        self.graft = client.apply_graft("tuple", *iterable)
Esempio n. 5
0
    def _update_value(self, change):
        if not self.trait_has_value("xyz_obj"):
            # avoids crazy tracebacks in __init__ when given bad arguments,
            # and `hold_trait_notifications` tries to fire notifiers _before_
            # reraising the exception: `xyz_obj` might not be set yet,
            # and accessing it would cause its own spew of confusing traitlets errors.
            return

        if len(self.xyz_obj.params) > 0:
            try:
                # attept to promote parameters as the Function's arguments
                args, kwargs = self.xyz_obj.object._promote_arguments(
                    **self.parameters.to_dict())
            except Exception:
                # when arguments are invalid (currently, only if a LayerPicker has no layer selected),
                # `value` is None
                self.set_trait("value", None)
                return
        else:
            args, kwargs = (), {}

        graft = graft_client.apply_graft("XYZ.use", self.xyz_obj.id, *args,
                                         **kwargs)

        self.set_trait(
            "value",
            self.imagery._from_graft(graft),
        )
Esempio n. 6
0
 def __init__(self, type_):
     if isinstance(type_, np.dtype):
         val = type_.char
     elif type_ in (
         int,
         np.int,
         np.int_,
         np.int8,
         np.int16,
         np.int32,
         np.int64,
         Int,
         float,
         np.float,
         np.float_,
         np.float32,
         np.float64,
         Float,
         bool,
         np.bool,
         np.bool_,
         Bool,
     ):
         val = type_.__name__.lower()
     else:
         raise ValueError(
             "Cannot construct a DType object from {}. Must be a "
             "NumPy dtype, NumPy type, Python type, or proxy type.".format(type_)
         )
     self.graft = client.apply_graft("wf.dtype.create", val)
    def __init__(self, data, mask=False, fill_value=None):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic MaskedArray; "
                "the dtype and dimensionality must be specified (like `MaskedArray[Float, 3]`)"
            )
        if isinstance(data, np.ndarray):
            if data.dtype.kind != WF_TO_DTYPE_KIND[self._type_params[0]]:
                raise TypeError("Invalid dtype {} for {}".format(
                    data.dtype,
                    type(self).__name__))
            if data.ndim != self.ndim:
                raise ValueError(
                    "Cannot instantiate a {}-dimensional Workflows MaskedArray from a "
                    "{}-dimensional NumPy data array".format(
                        self.ndim, data.ndim))

            data = data.tolist()
        else:
            data_list_type = functools.reduce(lambda accum, cur: List[accum],
                                              range(self.ndim), self.dtype)
            try:
                data = data_list_type._promote(data)
            except ProxyTypeError:
                raise ValueError(
                    "Cannot instantiate the data Array from {!r}".format(data))

        if isinstance(mask, (bool, np.bool_, Bool)):
            mask = Bool._promote(mask)
        elif isinstance(mask, np.ndarray):
            if mask.dtype.kind != "b":
                raise TypeError(
                    "Invalid dtype {} for a mask array, should be boolean".
                    format(mask.dtype))
            if mask.ndim != self.ndim:
                raise ValueError(
                    "Cannot instantiate a {}-dimensional Workflows MaskedArray with a "
                    "{}-dimensional NumPy mask array".format(
                        self.ndim, mask.ndim))

            mask = mask.tolist()
        else:
            # TODO(Clark): Support mask broadcasting to data shape?  This could be done
            # client-side or server-side.
            mask_list_type = functools.reduce(lambda accum, cur: List[accum],
                                              range(self._type_params[1]),
                                              Bool)
            try:
                mask = mask_list_type._promote(mask)
            except ProxyTypeError:
                raise ValueError(
                    "Cannot instantiate the mask Array from {!r}".format(mask))

        fill_value = _promote_fill_value(self, fill_value)

        self.graft = client.apply_graft("maskedarray.create", data, mask,
                                        fill_value)
Esempio n. 8
0
 def __init__(self, obj):
     if isinstance(obj, type(self)):
         self.graft = obj.graft
     elif isinstance(obj, (np.int64, np.float64, np.bool, np.bool_)):
         cast = PY_TYPE[type(obj)](obj)
         self.graft = client.apply_graft("wf.scalar.create", cast)
     else:
         raise ProxyTypeError(
             "Cannot instantiate a Scalar from {}.".format(obj))
Esempio n. 9
0
 def __init__(self, start=None, stop=None, step=None):
     if not ((isinstance(start, Int) and start.literal_value is None) or
             (isinstance(stop, Int) and stop.literal_value is None) or
             (isinstance(step, Int) and step.literal_value is None)):
         self._literal_value = slice(start.literal_value,
                                     stop.literal_value, step.literal_value)
     self.graft = client.apply_graft("wf.Slice.create",
                                     start=start,
                                     stop=stop,
                                     step=step)
Esempio n. 10
0
    def __init__(self, data, mask=False, fill_value=None):
        mask_literal_value = mask.literal_value
        if (isinstance(mask_literal_value, np.ndarray)
                and mask_literal_value.dtype.kind != "b"):
            raise TypeError("Invalid dtype {} for a mask array, "
                            "should be boolean".format(
                                mask_literal_value.dtype))

        self.graft = client.apply_graft("maskedarray.create", data, mask,
                                        fill_value)
Esempio n. 11
0
    def _from_apply(cls, function, *args, **kwargs):
        """
        Construct a new instance of `cls` from a graft function application.

        Like ``cls._from_graft(client.apply_graft(function, *args, **kwargs))``,
        with parameter merging of the inputs.
        """
        return cls._from_graft(
            client.apply_graft(function, *args, **kwargs),
            params=merge_params(function, *args, *kwargs.values()),
        )
Esempio n. 12
0
    def __init__(self, **kwargs):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic {}; the item types must be specified "
                "(like Struct[{}])".format(
                    type(self).__name__, "{'a': Str, 'b': Int}"))
        promoted = self._promote_kwargs(kwargs,
                                        optional=self._optional,
                                        read_only=self._read_only)
        self.graft = client.apply_graft(self._constructor, **promoted)

        self._items_cache = promoted
Esempio n. 13
0
    def __init__(self, arr):
        if isinstance(arr, np.generic):
            arr = arr.tolist()
        if isinstance(arr, (int, float, bool)):
            self._literal_value = arr
            self.graft = client.apply_graft("wf.array.create", arr)
        elif isinstance(arr, (Int, Float, Bool, List)):
            self.graft = client.apply_graft("wf.array.create", arr)
        else:
            if not isinstance(arr, np.ndarray):
                try:
                    arr = np.asarray(arr)
                except Exception:
                    raise ValueError("Cannot construct Array from {!r}".format(arr))

            if arr.dtype.kind not in ("b", "i", "f"):
                raise TypeError("Invalid dtype {} for an Array".format(arr.dtype))

            self._literal_value = arr
            arr_list = arr.tolist()
            self.graft = client.apply_graft("wf.array.create", arr_list)
Esempio n. 14
0
    def __init__(self, **kwargs):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic {}; the item types must be specified "
                "(like Struct[{}])".format(type(self).__name__, "{'a': Str, 'b': Int}")
            )
        promoted = self._promote_kwargs(
            kwargs, optional=self._optional, read_only=self._read_only
        )
        self.graft = client.apply_graft(self._constructor, **promoted)
        self.params = merge_params(self._constructor, *kwargs.values())
        # ^ NOTE: this would need to include the keys as well if proxytypes ever become hashable

        self._items_cache = promoted
Esempio n. 15
0
    def __init__(self, obj):
        from .string import Str

        if (isinstance(obj, Number) and not self._is_generic()
                or isinstance(obj, (Bool, Str))):
            self.params = obj.params
            if isinstance(obj, type(self)):
                self.graft = obj.graft
            else:
                self.graft = client.apply_graft(
                    "wf.{}.cast".format(self.__class__.__name__), obj)
                self.params = obj.params
        else:
            if isinstance(obj, np.generic):
                obj = obj.tolist()
            super(Number, self).__init__(obj)
Esempio n. 16
0
 def __init__(
     self,
     days=0,
     seconds=0,
     microseconds=0,
     milliseconds=0,
     minutes=0,
     hours=0,
     weeks=0,
 ):
     self.graft = client.apply_graft(
         "timedelta.from_components",
         days=days,
         seconds=seconds,
         microseconds=microseconds,
         milliseconds=milliseconds,
         minutes=minutes,
         hours=hours,
         weeks=weeks,
     )
Esempio n. 17
0
    def __init__(self, images):
        "Construct an ImageCollection from a sequence of Images"

        self.graft = client.apply_graft("ImageCollection.from_images", images)
Esempio n. 18
0
 def __init__(self, x):
     self.graft = graft_client.apply_graft("foo", x=x)
Esempio n. 19
0
 def _from_apply(cls, function, *args, **kwargs):
     "Shorthand for ``cls._from_graft(client.apply_graft(function, *args, **kwargs))``"
     return cls._from_graft(client.apply_graft(function, *args, **kwargs))
Esempio n. 20
0
    def __init__(self, *dct, **kwargs):
        if self._type_params is None:
            raise TypeError(
                "Cannot instantiate a generic Dict; the key and value types must be specified (like `Dict[Str, Bool]`)"
            )

        if len(dct) > 1:
            raise TypeError("Dict expected at most 1 arguments, got {}".format(
                len(dct)))
        if len(dct) == 0:
            dct = kwargs
            kwargs = {}
        else:
            dct = dct[0]

        kt, vt = self._type_params
        if isinstance(dct, BaseDict):
            other_kt, other_vt = dct.key_type, dct.value_type
            if not (issubclass(other_kt, kt) and issubclass(other_vt, vt)):
                raise ProxyTypeError(
                    "Cannot convert {} to {}, their element types are different"
                    .format(type(dct).__name__,
                            type(self).__name__))
            self.graft = dct.graft
            self.params = dct.params
            if len(kwargs) > 0:
                raise NotImplementedError(
                    "Don't have key merging onto a proxy dict yet.")
        else:
            if not isinstance(dct, abc.Mapping):
                raise ProxyTypeError("Expected a mapping, got {}".format(dct))

            dct = dct.copy()
            dct.update(kwargs)
            # TODO(gabe): numer of copies of source dict could definitely be reduced here

            is_str_dict = issubclass(kt, Str)
            promoted = {} if is_str_dict else []
            for key, val in six.iteritems(dct):
                try:
                    promoted_key = kt._promote(key)
                except ProxyTypeError:
                    raise ProxyTypeError(
                        "Expected Dict keys of type {}, but got {}".format(
                            kt, key))
                try:
                    promoted_val = vt._promote(val)
                except ProxyTypeError:
                    raise ProxyTypeError(
                        "Expected Dict values of type {}, but got {}".format(
                            vt, val))
                if is_str_dict:
                    promoted[key] = promoted_val
                    # note we use the unpromoted key, which should be a string
                    # this is an optimization that produces a cleaner graph for the case of string-keyed dicts
                    # FIXME this logic would break on Str proxytype keys, if proxytypes every become hashable
                else:
                    promoted += [promoted_key, promoted_val]
                    # for non-string dicts, we just give varargs of key, value, key, value, ...
                    # since that's a much simpler graft representation than constructing a list
                    # of tuples

            if is_str_dict:
                self.graft = client.apply_graft("wf.dict.create", **promoted)
                self.params = merge_params(*dct.values())
            else:
                self.graft = client.apply_graft("wf.dict.create", *promoted)
                self.params = merge_params(*(x for kv in dct.items()
                                             for x in kv))
Esempio n. 21
0
 def __init__(self, x):
     self.graft = graft_client.apply_graft("bar", x=x)
     self.params = getattr(x, "params", ())
Esempio n. 22
0
 def __init__(self, x):
     self.graft = client.apply_graft("bar", x=x)