|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.AspNetCore.Html; |
| 6 | +using Microsoft.Data.Analysis; |
| 7 | +using Microsoft.DotNet.Interactive; |
| 8 | +using Microsoft.DotNet.Interactive.Commands; |
| 9 | +using Microsoft.DotNet.Interactive.Formatting; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.IO; |
| 12 | +using System.Linq; |
| 13 | +using System.Text.Json; |
| 14 | +using System.Threading.Tasks; |
| 15 | +using Plotly.NET.CSharp; |
| 16 | +using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags; |
| 17 | + |
| 18 | + |
| 19 | +namespace Microsoft.ML.AutoML |
| 20 | +{ |
| 21 | + public class AutoMLMonitorKernelExtension : IKernelExtension |
| 22 | + { |
| 23 | + public async Task OnLoadAsync(Kernel kernel) |
| 24 | + { |
| 25 | + Formatter.Register<NotebookMonitor>((monitor, writer) => |
| 26 | + { |
| 27 | + WriteSummary(monitor, writer); |
| 28 | + WriteChart(monitor, writer); |
| 29 | + WriteTable(monitor, writer); |
| 30 | + }, "text/html"); |
| 31 | + |
| 32 | + if (Kernel.Root?.FindKernel("csharp") is { } csKernel) |
| 33 | + { |
| 34 | + await LoadExtensionApiAsync(csKernel); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static async Task LoadExtensionApiAsync(Kernel cSharpKernel) |
| 39 | + { |
| 40 | + await cSharpKernel.SendAsync(new SubmitCode($@"#r ""{typeof(AutoMLMonitorKernelExtension).Assembly.Location}"" |
| 41 | +using {typeof(NotebookMonitor).Namespace};")); |
| 42 | + } |
| 43 | + |
| 44 | + private static void WriteSummary(NotebookMonitor monitor, TextWriter writer) |
| 45 | + { |
| 46 | + |
| 47 | + var summary = new List<IHtmlContent>(); |
| 48 | + |
| 49 | + if (monitor.BestTrial != null) |
| 50 | + { |
| 51 | + var bestTrialParam = JsonSerializer.Serialize(monitor.BestTrial.TrialSettings.Parameter, new JsonSerializerOptions() { WriteIndented = true, }); |
| 52 | + summary.Add(h3("Best Trial")); |
| 53 | + summary.Add(p($"Id: {monitor.BestTrial.TrialSettings.TrialId}")); |
| 54 | + summary.Add(p($"Trainer: {monitor.BestTrial.TrialSettings.Pipeline}".Replace("Unknown=>", ""))); |
| 55 | + summary.Add(p($"Parameters: {bestTrialParam}")); |
| 56 | + } |
| 57 | + if (monitor.ActiveTrial != null) |
| 58 | + { |
| 59 | + |
| 60 | + var activeTrialParam = JsonSerializer.Serialize(monitor.ActiveTrial.Parameter, new JsonSerializerOptions() { WriteIndented = true, }); |
| 61 | + |
| 62 | + summary.Add(h3("Active Trial")); |
| 63 | + summary.Add(p($"Id: {monitor.ActiveTrial.TrialId}")); |
| 64 | + summary.Add(p($"Trainer: {monitor.ActiveTrial.Pipeline}".Replace("Unknown=>", ""))); |
| 65 | + summary.Add(p($"Parameters: {activeTrialParam}")); |
| 66 | + } |
| 67 | + |
| 68 | + writer.Write(div(summary)); |
| 69 | + } |
| 70 | + |
| 71 | + private static void WriteChart(NotebookMonitor monitor, TextWriter writer) |
| 72 | + { |
| 73 | + var x = monitor.CompletedTrials.Select(x => x.TrialSettings.TrialId); |
| 74 | + var y = monitor.CompletedTrials.Select(x => x.Metric); |
| 75 | + |
| 76 | + var chart = Chart.Point<int, double, string>(x, y, "Plot Metrics over Trials.") |
| 77 | + .WithTraceInfo(ShowLegend: false) |
| 78 | + .WithXAxisStyle<double, double, string>(TitleText: "Trial", ShowGrid: false) |
| 79 | + .WithYAxisStyle<double, double, string>(TitleText: "Metric", ShowGrid: false); |
| 80 | + |
| 81 | + var chartHeader = new List<IHtmlContent>(); |
| 82 | + chartHeader.Add(h3("Plot Metrics over Trials")); |
| 83 | + writer.Write(div(chartHeader)); |
| 84 | + |
| 85 | + |
| 86 | + Formatter.GetPreferredFormatterFor(typeof(Plotly.NET.GenericChart.GenericChart), "text/html").Format(chart, writer); |
| 87 | + |
| 88 | + // Works around issue with earlier versions of Plotly.NET - https://github.com/plotly/Plotly.NET/pull/305 |
| 89 | + if (writer.ToString().EndsWith("</div \r\n")) |
| 90 | + { |
| 91 | + writer.Write(">"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void WriteTable(NotebookMonitor notebookMonitor, TextWriter writer) |
| 96 | + { |
| 97 | + var tableHeader = new List<IHtmlContent>(); |
| 98 | + tableHeader.Add(h3("All Trials Table")); |
| 99 | + writer.Write(div(tableHeader)); |
| 100 | + Formatter.GetPreferredFormatterFor(typeof(DataFrame), "text/html").Format(notebookMonitor.TrialData, writer); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments