/
template_md_explainer.py
115 lines (89 loc) · 3.57 KB
/
template_md_explainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Markdown report with raster image chart explainer template which can be used to create explainer with global report explanations."""
from typing import List, Tuple
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from h2oaicore.mli.oss.byor.core.explainers import CustomExplainer
from h2oaicore.mli.oss.byor.core.explanations import (
CustomExplanation,
ReportExplanation,
)
from h2oaicore.mli.oss.byor.core.representations import MarkdownFormat
from h2oaicore.mli.oss.commons import ExplainerModel
class TemplateMarkdownExplainer(CustomExplainer):
"""Markdown report with raster image chart explainer template.
Use this template to create explainer with global report explanations.
"""
_display_name = "Template Markdown explainer"
_description = (
"Markdown report with raster image chart explainer template which can be used "
"to create explainer with global report explanations."
)
_regression = True
_binary = True
_global_explanation = True
_explanation_types = [ReportExplanation]
_keywords = [CustomExplainer.KEYWORD_TEMPLATE]
def setup(self, model: ExplainerModel, persistence, **kwargs):
CustomExplainer.setup(
self, model=model, persistence=persistence, **kwargs
)
def explain(self, X, y=None, explanations_types: list = None, **kwargs):
"""Create global and local (pre-computed/cached) explanations.
Template explainer returns MOCK explanation data - replace mock data
preparation with actual computation to create real explainer.
"""
# explanations list
explanations = list()
# global explanation
explanations.append(self.explain_global_markdown())
return explanations
def explain_global_markdown(self):
global_explanation = ReportExplanation(
explainer=self,
display_name="Template Markdown report",
display_category=CustomExplanation.DISPLAY_CAT_EXAMPLE,
)
# CALCULATION: Markdown report with image(s) in work directory
report_path, images_path = self._create_report()
# NORMALIZATION: Markdown report to Grammar of MLI format in Driverless AI UI
global_explanation.add_format(
MarkdownFormat(
explanation=global_explanation,
format_file=report_path,
extra_format_files=images_path,
)
)
return global_explanation
def _create_report(self) -> Tuple[str, List[str]]:
# save image
img_file_name = "image.png"
work_img_path = self.persistence.get_explainer_working_file(
img_file_name
)
TemplateMarkdownExplainer._create_report_image(work_img_path)
# save report
report_path = self.persistence.get_explainer_working_file("report.md")
with open(report_path, mode="w") as file:
file.write(MARKDOWN_TEMPLATE.format(img_file_name))
return report_path, [work_img_path]
@staticmethod
def _create_report_image(img_path: str):
ts = pd.Series(
np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000)
)
ts = ts.cumsum()
df = pd.DataFrame(
np.random.randn(1000, 4), index=ts.index, columns=list("ABCD")
)
df = df.cumsum()
plt.figure()
df.plot()
plt.savefig(img_path, dpi=300)
#
# Markdown report
#
MARKDOWN_TEMPLATE: str = """# Example Report
This is an example of **Markdown report** which can be created by explainer.
![image](./{})
"""