Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit f220fd0

Browse files
authored
Feature slicing view visualization component. (#109)
1 parent 1baa5f0 commit f220fd0

File tree

3 files changed

+5376
-0
lines changed

3 files changed

+5376
-0
lines changed

datalab/mlalpha/commands/_mlalpha.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ def mlalpha(line, cell=None):
158158
package_parser.add_argument('--name', help='The name of the package.', required=True)
159159
package_parser.add_argument('--output', help='the output dir of the package.', required=True)
160160
package_parser.set_defaults(func=_package)
161+
162+
package_parser = parser.subcommand('feature-slice-view','View results of a ' +
163+
'FeatureSlicingPipeline, some eval metrics grouped by ' +
164+
'specified feature column values')
165+
package_parser.add_argument('--file', help='The results file from FeatureSlicingPipeline',
166+
required=True)
167+
package_parser.add_argument('--feature',
168+
help='Which feature to view. The feature must be specified ' +
169+
'in the FeatureSlicingPipeline. If not specified, all ' +
170+
'features will be listed.')
171+
package_parser.set_defaults(func=_feature_slice_view)
172+
161173
namespace = datalab.utils.commands.notebook_environment()
162174
return datalab.utils.commands.handle_magic_line(line, cell, parser, namespace=namespace)
163175

@@ -972,3 +984,25 @@ def _package(args, cell):
972984
google.cloud.ml.util._file.copy_file(package_path, dest)
973985
os.remove(package_path)
974986
print 'Package created at %s.' % dest
987+
988+
989+
def _feature_slice_view(args, cell):
990+
HTML_TEMPLATE = """<link rel="import" href="/nbextensions/gcpdatalab/extern/lantern-browser.html" >
991+
<lantern-browser id="%s"></lantern-browser>
992+
<script>
993+
var browser = document.querySelector('#%s');
994+
browser.metrics = %s;
995+
browser.data = %s;
996+
browser.sourceType = 'colab';
997+
browser.weightedExamplesColumn = 'totalWeightedExamples';
998+
browser.calibrationPlotUriFn = function(s) { return '/' + s; }
999+
</script>"""
1000+
with open(args['file']) as f:
1001+
data = map(json.loads, f)
1002+
if args['feature']:
1003+
data = [e for e in data if e['feature'].split(':')[0] == args['feature']]
1004+
metrics_str = str(map(str, data[0]['metricValues'].keys()))
1005+
data_str = str([{str(k): json.dumps(v) for k,v in elem.iteritems()} for elem in data])
1006+
html_id = 'l' + datalab.utils.commands.Html.next_id()
1007+
html = HTML_TEMPLATE % (html_id, html_id, metrics_str, data_str)
1008+
return IPython.core.display.HTML(html)

0 commit comments

Comments
 (0)