|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8; -*- |
| 3 | +# Copyright (c) 2023 Oracle and/or its affiliates. |
| 4 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 5 | + |
| 6 | +from ads.common.decorator.runtime_dependency import OptionalDependency |
| 7 | +from ads.feature_store.statistics.charts.abstract_feature_stat import AbsFeatureStat |
| 8 | +from ads.feature_store.statistics.generic_feature_value import GenericFeatureValue |
| 9 | + |
| 10 | +try: |
| 11 | + from plotly.graph_objs import Figure |
| 12 | +except ModuleNotFoundError: |
| 13 | + raise ModuleNotFoundError( |
| 14 | + f"The `plotly` module was not found. Please run `pip install " |
| 15 | + f"{OptionalDependency.FEATURE_STORE}`." |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +class BoxPlot(AbsFeatureStat): |
| 20 | + CONST_MIN = "Min" |
| 21 | + CONST_MAX = "Max" |
| 22 | + CONST_QUARTILES = "Quartiles" |
| 23 | + CONST_SD = "StandardDeviation" |
| 24 | + CONST_MEAN = "Mean" |
| 25 | + CONST_BOX_PLOT_TITLE = "Box Plot" |
| 26 | + |
| 27 | + class Quartiles: |
| 28 | + CONST_Q1 = "q1" |
| 29 | + CONST_Q2 = "q2" |
| 30 | + CONST_Q3 = "q3" |
| 31 | + |
| 32 | + def __init__(self, q1: float, q2: float, q3: float): |
| 33 | + self.q1 = q1 |
| 34 | + self.q2 = q2 |
| 35 | + self.q3 = q3 |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def from_json(cls, json_dict: dict) -> "BoxPlot.Quartiles": |
| 39 | + if json_dict is not None: |
| 40 | + return cls( |
| 41 | + json_dict.get(cls.CONST_Q1), |
| 42 | + json_dict.get(cls.CONST_Q2), |
| 43 | + json_dict.get(cls.CONST_Q3), |
| 44 | + ) |
| 45 | + else: |
| 46 | + return None |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + mean: float, |
| 51 | + median: float, |
| 52 | + sd: float, |
| 53 | + q1: float, |
| 54 | + q3: float, |
| 55 | + min: float, |
| 56 | + max: float, |
| 57 | + ): |
| 58 | + self.mean = mean |
| 59 | + self.median = median |
| 60 | + self.q1 = q1 |
| 61 | + self.q3 = q3 |
| 62 | + self.sd = sd |
| 63 | + self.min = min |
| 64 | + self.max = max |
| 65 | + |
| 66 | + def add_to_figure(self, fig: Figure, xaxis: int, yaxis: int): |
| 67 | + xaxis_str, yaxis_str, x_str, y_str = self.get_x_y_str_axes(xaxis, yaxis) |
| 68 | + fig.add_box( |
| 69 | + mean=[self.mean], |
| 70 | + median=[self.median], |
| 71 | + q1=[self.q1], |
| 72 | + q3=[self.q3], |
| 73 | + sd=[self.sd], |
| 74 | + upperfence=[self.max], |
| 75 | + lowerfence=[self.min], |
| 76 | + xaxis=x_str, |
| 77 | + yaxis=y_str, |
| 78 | + ) |
| 79 | + fig.layout.annotations[xaxis].text = self.CONST_BOX_PLOT_TITLE |
| 80 | + fig.layout[yaxis_str]["title"] = "Values" |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def from_json(cls, json_dict: dict) -> "BoxPlot": |
| 84 | + if type(json_dict) is dict and json_dict.get(cls.CONST_QUARTILES) is not None: |
| 85 | + quartiles = cls.Quartiles.from_json(json_dict.get(cls.CONST_QUARTILES)) |
| 86 | + return cls( |
| 87 | + mean=GenericFeatureValue.from_json(json_dict.get(cls.CONST_MEAN)).val, |
| 88 | + median=quartiles.q2, |
| 89 | + sd=GenericFeatureValue.from_json(json_dict.get(cls.CONST_SD)).val, |
| 90 | + q1=quartiles.q1, |
| 91 | + q3=quartiles.q3, |
| 92 | + min=GenericFeatureValue.from_json(json_dict.get(cls.CONST_MIN)).val, |
| 93 | + max=GenericFeatureValue.from_json(json_dict.get(cls.CONST_MAX)).val, |
| 94 | + ) |
| 95 | + else: |
| 96 | + return None |
0 commit comments