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

Commit bbe7e92

Browse files
committed
Feature slicing view visualization component. (#109)
1 parent 22e1875 commit bbe7e92

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
@@ -157,6 +157,18 @@ def mlalpha(line, cell=None):
157157
package_parser.add_argument('--name', help='The name of the package.', required=True)
158158
package_parser.add_argument('--output', help='the output dir of the package.', required=True)
159159
package_parser.set_defaults(func=_package)
160+
161+
package_parser = parser.subcommand('feature-slice-view','View results of a ' +
162+
'FeatureSlicingPipeline, some eval metrics grouped by ' +
163+
'specified feature column values')
164+
package_parser.add_argument('--file', help='The results file from FeatureSlicingPipeline',
165+
required=True)
166+
package_parser.add_argument('--feature',
167+
help='Which feature to view. The feature must be specified ' +
168+
'in the FeatureSlicingPipeline. If not specified, all ' +
169+
'features will be listed.')
170+
package_parser.set_defaults(func=_feature_slice_view)
171+
160172
namespace = datalab.utils.commands.notebook_environment()
161173
return datalab.utils.commands.handle_magic_line(line, cell, parser, namespace=namespace)
162174

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

0 commit comments

Comments
 (0)