1- import pandas as pd
2- from typing import Dict
3- from copy import deepcopy
1+ import matplotlib . figure
2+ from matplotlib . gridspec import GridSpec
3+ from matplotlib . figure import Figure
44
55from ads .feature_store .response .response_builder import ResponseBuilder
6- from ads .jobs .builders .base import Builder
7- from ads .common import utils
6+
7+ import matplotlib .pyplot as plt
8+ import matplotlib .font_manager as fm
9+ import json
10+
11+
12+ def add_plots_for_stat (fig : Figure , feature : str , stat : dict ):
13+ freq_dist = stat .get (Statistics .CONST_FREQUENCY_DISTRIBUTION )
14+ top_k = stat .get (Statistics .CONST_TOP_K_FREQUENT )
15+ probability = stat .get (Statistics .CONST_PROBABILITY_DISTRIBUTION )
16+
17+ def subplot_generator ():
18+ plot_count = 0
19+ if stat .get (Statistics .CONST_FREQUENCY_DISTRIBUTION ) is not None :
20+ plot_count += 1
21+ # if stat.get(Statistics.CONST_TOP_K_FREQUENT) is not None:
22+ # plot_count += 1
23+ if stat .get (Statistics .CONST_PROBABILITY_DISTRIBUTION ) is not None :
24+ plot_count += 1
25+
26+ for i in range (0 , plot_count ):
27+ yield fig .add_subplot (1 , plot_count , i + 1 )
28+
29+ subplots = subplot_generator ()
30+ if freq_dist is not None :
31+ axs = next (subplots )
32+ fig .suptitle (feature , fontproperties = fm .FontProperties (weight = "bold" ))
33+ axs .hist (
34+ x = freq_dist .get ("bins" ),
35+ weights = freq_dist .get ("frequency" ),
36+ cumulative = False ,
37+ color = "teal" ,
38+ mouseover = True ,
39+ animated = True ,
40+ )
41+
42+ axs .set_xlabel (
43+ "Bins" , fontdict = {"fontproperties" : fm .FontProperties (size = "xx-small" )}
44+ )
45+ axs .set_ylabel (
46+ "Frequency" , fontdict = {"fontproperties" : fm .FontProperties (size = "xx-small" )}
47+ )
48+ axs .set_title (
49+ "Frequency Distribution" ,
50+ fontdict = {"fontproperties" : fm .FontProperties (size = "small" )},
51+ )
52+ axs .set_xticks (freq_dist .get ("bins" ))
53+ if probability is not None :
54+ axs = next (subplots )
55+ fig .suptitle (feature , fontproperties = fm .FontProperties (weight = "bold" ))
56+ axs .bar (
57+ probability .get ("bins" ),
58+ probability .get ("density" ),
59+ color = "teal" ,
60+ mouseover = True ,
61+ animated = True ,
62+ )
63+ axs .set_xlabel (
64+ "Bins" , fontdict = {"fontproperties" : fm .FontProperties (size = "xx-small" )}
65+ )
66+ axs .set_ylabel (
67+ "Density" , fontdict = {"fontproperties" : fm .FontProperties (size = "xx-small" )}
68+ )
69+ axs .set_title (
70+ "Probability Distribution" ,
71+ fontdict = {"fontproperties" : fm .FontProperties (size = "small" )},
72+ )
73+ axs .set_xticks (probability .get ("bins" ))
74+
75+
76+ def subfigure_generator (count : int , fig : Figure ):
77+ rows = count
78+ subfigs = fig .subfigures (rows , 1 )
79+ for i in range (0 , rows ):
80+ yield subfigs [i ]
881
982
1083class Statistics (ResponseBuilder ):
1184 """
1285 Represents statistical information.
1386 """
1487
88+ CONST_FREQUENCY_DISTRIBUTION = "FrequencyDistribution"
89+ CONST_PROBABILITY_DISTRIBUTION = "ProbabilityDistribution"
90+ CONST_TOP_K_FREQUENT = "TopKFrequentElements"
91+
1592 @property
1693 def kind (self ) -> str :
1794 """
@@ -23,3 +100,26 @@ def kind(self) -> str:
23100 The kind of the statistics object, which is always "statistics".
24101 """
25102 return "statistics"
103+
104+ def to_viz (self ):
105+ if self .content is not None :
106+ stats : dict = json .loads (self .content )
107+ fig : Figure = plt .figure (figsize = (20 , 20 ), dpi = 150 )
108+ plt .subplots_adjust (hspace = 3 )
109+
110+ stats = {
111+ feature : stat
112+ for feature , stat in stats .items ()
113+ if Statistics .__graph_exists__ (stat )
114+ }
115+ subfigures = subfigure_generator (len (stats ), fig )
116+ for feature , stat in stats .items ():
117+ sub_figure = next (subfigures )
118+ add_plots_for_stat (sub_figure , feature , stat )
119+
120+ @staticmethod
121+ def __graph_exists__ (stat : dict ):
122+ return (
123+ stat .get (Statistics .CONST_FREQUENCY_DISTRIBUTION ) != None
124+ or stat .get (Statistics .CONST_PROBABILITY_DISTRIBUTION ) != None
125+ )
0 commit comments