def query_image():
    """
    Query image.

    Returns:
        bytes, image binary content for UI to demonstrate.
    """
    train_id = get_train_id(request)
    if train_id is None:
        raise ParamMissError("train_id")
    image_path = request.args.get("path")
    if image_path is None:
        raise ParamMissError("path")
    image_type = request.args.get("type")
    if image_type is None:
        raise ParamMissError("type")
    if image_type not in ("original", "overlay", "outcome"):
        raise ParamValueError(
            f"type:{image_type}, valid options: 'original' 'overlay' 'outcome'"
        )

    encapsulator = DatafileEncap(EXPLAIN_MANAGER)
    image = encapsulator.query_image_binary(train_id, image_path, image_type)

    return image
Exemple #2
0
def cache_train_jobs():
    """ Cache train jobs."""
    data = request.get_json(silent=True)
    if data is None:
        raise ParamMissError('train_ids')

    train_ids = data.get('train_ids')
    if train_ids is None:
        raise ParamMissError('train_ids')

    processor = TrainTaskManager(DATA_MANAGER)
    cache_result = processor.cache_train_jobs(train_ids)

    return jsonify({'cache_result': cache_result})
def query_explain_job():
    """Query explain job meta-data."""
    train_id = get_train_id(request)
    if train_id is None:
        raise ParamMissError("train_id")
    encapsulator = ExplainJobEncap(EXPLAIN_MANAGER)
    metadata = encapsulator.query_meta(train_id)

    return jsonify(metadata)
Exemple #4
0
    def _is_node_exist(self, node_id=None, node_name=None):
        """Check node is exist."""
        if node_id is not None:
            return bool(self._node_id_map_name.get(node_id))

        if node_name is not None:
            return bool(self._normal_node_map.get(node_name))

        raise ParamMissError('Method requires an argument that is not None.')
def _get_query_sample_parameters(data):
    """
    Get parameter for query.

    Args:
        data (dict): Dict that contains request info.

    Returns:
        dict, key-value pairs to call backend query functions.

    Raises:
        ParamMissError: If train_id info is not in the request.
        ParamTypeError: If certain key is not in the expected type in the request.
        ParamValueError: If certain key does not have the expected value in the request.
    """

    train_id = data.get("train_id")
    if train_id is None:
        raise ParamMissError('train_id')

    labels = data.get("labels")
    if labels is not None:
        _validate_type(labels, "labels", list)
    if labels:
        for item in labels:
            _validate_type(item, "element of labels", str)

    limit = data.get("limit", 10)
    limit = Validation.check_limit(limit, min_value=1, max_value=100)
    offset = data.get("offset", 0)
    offset = Validation.check_offset(offset=offset)
    sorted_name = data.get("sorted_name", "")
    _validate_value(sorted_name, "sorted_name",
                    ('', 'confidence', 'uncertainty'))

    sorted_type = data.get("sorted_type", "descending")
    _validate_value(sorted_type, "sorted_type", ("ascending", "descending"))

    prediction_types = data.get("prediction_types")
    if prediction_types is not None:
        _validate_type(prediction_types, "element of labels", list)
    if prediction_types:
        for item in prediction_types:
            _validate_value(item, "element of prediction_types",
                            ('TP', 'FN', 'FP'))

    query_kwarg = {
        "train_id": train_id,
        "labels": labels,
        "limit": limit,
        "offset": offset,
        "sorted_name": sorted_name,
        "sorted_type": sorted_type,
        "prediction_types": prediction_types
    }
    return query_kwarg
def query_evaluation():
    """Query saliency explainer evaluation scores."""
    train_id = get_train_id(request)
    if train_id is None:
        raise ParamMissError("train_id")
    encapsulator = EvaluationEncap(EXPLAIN_MANAGER)
    scores = encapsulator.query_explainer_scores(train_id)
    return jsonify({
        "explainer_scores": scores,
    })
Exemple #7
0
    def _get_normal_node(self, node_id=None, node_name=None):
        """Query node by node id or node name."""
        if node_id is not None:
            name = self._node_id_map_name.get(node_id)
            node = self._normal_node_map.get(name)
            return node

        if node_name is not None:
            return self._normal_node_map.get(node_name)

        raise ParamMissError('Method requires an argument that is not None.')
Exemple #8
0
def check_params_exist(params: list, config):
    """Check params exist."""
    miss_param_list = ''
    for param in params:
        if not config.get(param) or not config[param]:
            miss_param_list = ', '.join(
                (miss_param_list, param)) if miss_param_list else param

    if miss_param_list:
        error = ParamMissError(miss_param_list)
        log.error(str(error))
        raise error
def set_recommended_watch_points(session_id):
    """Set recommended watch points."""
    body = _read_post_request(request)
    request_body = body.get('requestBody')
    if request_body is None:
        raise ParamMissError('requestBody')

    set_recommended = request_body.get('set_recommended')
    reply = _wrap_reply(
        SessionManager.get_instance().get_session(
            session_id).set_recommended_watch_points, set_recommended)
    return reply
Exemple #10
0
def set_recommended_watch_points(train_id):
    """set recommended watch points."""
    body = request.stream.read()
    try:
        body = json.loads(body if body else "{}")
    except json.JSONDecodeError:
        raise ParamValueError("Json data parse failed.")

    request_body = body.get('requestBody')
    if request_body is None:
        raise ParamMissError('requestBody')

    set_recommended = request_body.get('set_recommended')
    reply = _wrap_reply(BACKEND_SERVER.set_recommended_watch_points, set_recommended, train_id)
    return reply
def query_explain_job():
    """
    Query explain job meta-data.

    Returns:
        Response, contains dict that stores metadata of the requested job.

    Raises:
        ParamMissError: If train_id info is not in the request.
    """
    train_id = get_train_id(request)
    if train_id is None:
        raise ParamMissError("train_id")
    encapsulator = ExplainJobEncap(EXPLAIN_MANAGER)
    metadata = encapsulator.query_meta(train_id)
    return jsonify(metadata)
    def check_param_empty(cls, **kwargs):
        """
        Check param.

        Args:
            kwargs (Any): Check if arg is truthy.

        Raises:
            ParamMissError: When param missing.
        """
        for key, value in kwargs.items():
            # When value is 0, 0.0 or False, it is not empty.
            if isinstance(value, Number):
                continue

            if not value:
                raise ParamMissError(key)
def query_evaluation():
    """
    Query saliency explainer evaluation scores.

    Returns:
        Response, contains dict that stores evaluation scores.

    Raises:
        ParamMissError: If train_id info is not in the request.
    """
    train_id = get_train_id(request)
    if train_id is None:
        raise ParamMissError("train_id")
    encapsulator = EvaluationEncap(EXPLAIN_MANAGER)
    scores = encapsulator.query_explainer_scores(train_id)
    return jsonify({
        "explainer_scores": scores,
    })
Exemple #14
0
def query_saliency():
    """Query saliency map related results."""

    data = _read_post_request(request)

    train_id = data.get("train_id")
    if train_id is None:
        raise ParamMissError('train_id')

    labels = data.get("labels")
    explainers = data.get("explainers")
    limit = data.get("limit", 10)
    limit = Validation.check_limit(limit, min_value=1, max_value=100)
    offset = data.get("offset", 0)
    offset = Validation.check_offset(offset=offset)
    sorted_name = data.get("sorted_name", "")
    sorted_type = data.get("sorted_type", "descending")

    if sorted_name not in ("", "confidence", "uncertainty"):
        raise ParamValueError(
            f"sorted_name: {sorted_name}, valid options: '' 'confidence' 'uncertainty'"
        )
    if sorted_type not in ("ascending", "descending"):
        raise ParamValueError(
            f"sorted_type: {sorted_type}, valid options: 'confidence' 'uncertainty'"
        )

    encapsulator = SaliencyEncap(_image_url_formatter, EXPLAIN_MANAGER)
    count, samples = encapsulator.query_saliency_maps(train_id=train_id,
                                                      labels=labels,
                                                      explainers=explainers,
                                                      limit=limit,
                                                      offset=offset,
                                                      sorted_name=sorted_name,
                                                      sorted_type=sorted_type)

    return jsonify({"count": count, "samples": samples})