Exemple #1
0
    def enable(self, monitored_entity, environment=None, wait=False):
        if wait:
            return self._blocking_enable(monitored_entity, environment)

        profiler_id = self.id
        try:
            monitored_entity_id = monitored_entity.id
        except:
            monitored_entity_id = monitored_entity
        environment = environment.copy() if environment else dict()
        environment["PROFILER_ID"] = self.id
        key_values = [
            KeyValue(key=k,
                     value=_utils.python_to_val_proto(v,
                                                      allow_collection=True))
            for k, v in environment.items()
        ]

        msg = EnableProfilerRequest(profiler_id=profiler_id,
                                    monitored_entity_id=monitored_entity_id,
                                    environment=key_values)
        endpoint = "/api/v1/monitored_entity/enableProfiler"
        response = self._conn.make_proto_request("POST", endpoint, body=msg)
        status = self._conn.must_proto_response(
            response, EnableProfilerRequest.Response).status
        return status
    def add_attributes(self, attrs):
        """
        Adds potentially multiple attributes to this dataset version.

        Parameters
        ----------
        attributes : dict of str to {None, bool, float, int, str, list, dict}
            Attributes.

        """
        # validate all keys first
        for key in six.viewkeys(attrs):
            _utils.validate_flat_key(key)

        # build KeyValues
        attribute_keyvals = []
        for key, value in six.viewitems(attrs):
            attribute_keyvals.append(
                _CommonCommonService.KeyValue(
                    key=key,
                    value=_utils.python_to_val_proto(value,
                                                     allow_collection=True)))

        Message = _DatasetVersionService.AddDatasetVersionAttributes
        msg = Message(id=self.id, attributes=attribute_keyvals)
        endpoint = "/api/v1/modeldb/dataset-version/addDatasetVersionAttributes"
        self._update(msg, Message.Response, endpoint, "POST")
Exemple #3
0
    def enable(self, monitored_entity, environment=None, wait=False):
        """Build and deploy this uploaded profiler.

        This method instructs Verta Services to build and deploy a docker image
        for the profiler which was uploaded. Environment variables for this
        deployment can be specified in the `environment` keyword argument as
        a dictionary from strings to strings.

        By default this method will issue a command to Verta Services to build
        an image, or to deploy the built image if the build is ready. In order
        to fully build and deploy an image, and to block on the completion of
        this process, users should specify ``wait=True``.

        Parameters
        ----------
        monitored_entity : :class:`~verta.monitoring.monitored_entity.MonitoredEntity`
            The monitored entity for which this profiler should be enabled.
        environment : dict, optional
            Dictionary from strings to strings specifying environment variables.
        wait: bool, optional
            Whether to block on completion of the full build-and-deploy process.
            Defaults to False.

        Returns
        -------
        ai.verta.monitoring.ProfilerStatus
            Deployment and build status of this profiler
        """
        if wait:
            return self._blocking_enable(monitored_entity, environment)

        profiler_id = self.id
        monitored_entity_id = arg_handler.extract_id(monitored_entity)
        environment = environment.copy() if environment else dict()
        environment["PROFILER_ID"] = profiler_id
        key_values = [
            KeyValue(key=k,
                     value=_utils.python_to_val_proto(v,
                                                      allow_collection=True))
            for k, v in environment.items()
        ]

        msg = EnableProfilerRequest(
            profiler_id=profiler_id,
            monitored_entity_id=monitored_entity_id,
            environment=key_values,
        )
        endpoint = "/api/v1/monitored_entity/enableProfiler"
        response = self._conn.make_proto_request("POST", endpoint, body=msg)
        status = self._conn.must_proto_response(
            response, EnableProfilerRequest.Response).status
        return status  # TODO: wrap this in a nicer type
Exemple #4
0
    def _create_proto(cls, conn, *args, **kwargs):
        tags = kwargs.pop('tags', None)
        if tags is not None:
            kwargs['tags'] = _utils.as_list_of_str(tags)

        attrs = kwargs.pop('attrs', None)
        if attrs is not None:
            for key, value in six.viewitems(attrs):
                if isinstance(value, data_types._VertaDataType):
                    attrs[key] = value._as_dict()

            kwargs['attrs'] = [
                _CommonCommonService.KeyValue(
                    key=key,
                    value=_utils.python_to_val_proto(value,
                                                     allow_collection=True))
                for key, value in six.viewitems(attrs)
            ]

        return cls._create_proto_internal(conn, *args, **kwargs)
Exemple #5
0
    def add_attributes(self, attrs, overwrite=False):
        """
        Adds potentially multiple attributes to this Model Version.

        Parameters
        ----------
        attrs : dict of str to {None, bool, float, int, str, list, dict}
            Attributes.
        overwrite : bool, default False
            Whether to allow overwriting an existing attribute with key `key`.

        """
        # validate all keys first
        for key in six.viewkeys(attrs):
            _utils.validate_flat_key(key)
        # convert data_types to dicts
        for key, value in attrs.items():
            if isinstance(value, data_types._VertaDataType):
                attrs[key] = value._as_dict()

        # build KeyValues
        attribute_keyvals = []
        existing_attrs = self.get_attributes()
        for key, value in six.viewitems(attrs):
            if key in existing_attrs and not overwrite:
                warnings.warn("skipping attribute {} which already exists;"
                              " set `overwrite=True` to overwrite".format(key))
                continue

            attribute_keyvals.append(
                _CommonCommonService.KeyValue(
                    key=key,
                    value=_utils.python_to_val_proto(value,
                                                     allow_collection=True),
                ))

        self._update(self.ModelVersionMessage(attributes=attribute_keyvals))
Exemple #6
0
    def find(self, *args):
        """
        Gets the results from this collection that match input predicates.

        A predicate is a string containing a simple boolean expression consisting of:

            - a dot-delimited property such as ``metrics.accuracy``
            - a Python boolean operator such as ``>=``
            - a literal value such as ``.8``

        Parameters
        ----------
        *args : strs
            Predicates specifying results to get.

        Returns
        -------
        The same type of object given in the input.

        Examples
        --------
        .. code-block:: python

            runs.find("hyperparameters.hidden_size == 256",
                       "metrics.accuracy >= .8")
            # <ExperimentRuns containing 3 runs>
            # alternatively:
            runs.find(["hyperparameters.hidden_size == 256",
                       "metrics.accuracy >= .8"])
            # <ExperimentRuns containing 3 runs>

        """
        if len(args) == 1 and isinstance(args[0], (list, tuple)):
            # to keep backward compatibility, in case user pass in a list or tuple
            return self.find(*args[0])
        elif not all(isinstance(predicate, six.string_types) for predicate in args):
            raise TypeError("predicates must all be strings")

        new_list = copy.deepcopy(self)
        for predicate in args:
            # split predicate
            try:
                key, operator, value = map(
                    lambda token: token.strip(),
                    self._OP_PATTERN.split(predicate, maxsplit=1),
                )
            except ValueError:
                six.raise_from(
                    ValueError(
                        "predicate `{}` must be a two-operand comparison".format(
                            predicate
                        )
                    ),
                    None,
                )

            if key.split(".")[0] not in self._VALID_QUERY_KEYS:
                raise ValueError(
                    "key `{}` is not a valid key for querying;"
                    " currently supported keys are: {}".format(
                        key, self._VALID_QUERY_KEYS
                    )
                )

            # cast operator into protobuf enum variant
            operator = self._OP_MAP[operator]

            try:
                value = float(value)
            except ValueError:  # not a number, so process as string
                # maintain old behavior where input would be wrapped in quotes
                if (value.startswith("'") and value.endswith("'")) or (
                    value.startswith('"') and value.endswith('"')
                ):
                    value = value[1:-1]

            new_list._msg.predicates.append(  # pylint: disable=no-member
                _CommonCommonService.KeyValueQuery(
                    key=key,
                    value=_utils.python_to_val_proto(value),
                    operator=operator,
                )
            )

        return new_list