public static void ExtractForPlotlyTables(string fid) { Console.WriteLine(DateTime.Now); DataFeatureExtractor.InitializeMetadata("en"); Console.WriteLine($"After InitializeMetadata: {DateTime.Now}"); PlotlyTable plotlyTable = PlotlyTable.GetInstance(fid); List <PlotlyChart> plotlyCharts = PlotlyChart.GetInstance(fid, plotlyTable); Console.WriteLine($"After GetInstance: {DateTime.Now}"); Common.FileInfo plotlyTableInfo = DataFeatureExtractor.ExtractTableFeatures(plotlyTable, out List <Dictionary <int, Dictionary <string, float[]> > > headerEmbeddings, out SourceFeatures sf); Console.WriteLine($"After FE: {DateTime.Now}"); string uid = plotlyTable.TUid.Substring(0, plotlyTable.TUid.Length - 3); Helpers.DumpJson($"{plotlyTable.TUid}.DF.json", sf, DataSerializer.Instance); Helpers.DumpJson($"{plotlyTable.TUid}.table.json", plotlyTable, DataSerializer.Instance); Helpers.DumpJson($"{uid}.EMB.json", headerEmbeddings, DataSerializer.Instance); Helpers.DumpJson($"{uid}.index.json", plotlyTableInfo, DataSerializer.Instance); Helpers.DumpCsv($"{plotlyTable.TUid}.csv", plotlyTable); for (int i = 0; i < plotlyCharts.Count; i++) { PlotlyChart plotlyChart = plotlyCharts[i]; Helpers.DumpJson($"{plotlyChart.CUid}.json", plotlyChart, DataSerializer.Instance); } Console.WriteLine($"After Dump: {DateTime.Now}"); }
public static PlotlyChart BuildHistogram(string groupName, ICollection <TimeSeriesAnalysis> analysisResults) { IEnumerable <Graph.Histogram> traces = BuildTraces(analysisResults); PlotlyChart chart = BuildChart(groupName, traces); return(chart); }
public static void ExtractForPlotlyTablesAll(string plotlyTableFolder) { Console.WriteLine($"Extracting PlotlyTable features from {plotlyTableFolder}."); PlotlyList plotlyList = LoadPlotlyTablesAll(plotlyTableFolder); List <PlotlyTable> plotlyTables = plotlyList.PlotlyTables; List <PlotlyChart> plotlyCharts = plotlyList.PlotlyCharts; // Initialize some models from MetadataRecoSvr to support the two Bayesian features. DataFeatureExtractor.InitializeMetadata("en"); // Run features for each table and store embeddings for each plotly table. for (int i = 0; i < plotlyTables.Count; i++) { PlotlyTable plotlyTable = plotlyTables[i]; Common.FileInfo plotlyTableInfo = DataFeatureExtractor.ExtractTableFeatures(plotlyTable, out List <Dictionary <int, Dictionary <string, float[]> > > headerEmbeddings, out SourceFeatures sf); string uid = plotlyTable.TUid.Substring(0, plotlyTable.TUid.Length - 3); Helpers.DumpJson($"{plotlyTable.TUid}.DF.json", sf, DataSerializer.Instance); Helpers.DumpJson($"{plotlyTable.TUid}.table.json", plotlyTable, DataSerializer.Instance); Helpers.DumpJson($"{uid}.EMB.json", headerEmbeddings, DataSerializer.Instance); Helpers.DumpJson($"{uid}.index.json", plotlyTableInfo, DataSerializer.Instance); Helpers.DumpCsv($"{plotlyTable.TUid}.csv", plotlyTable); } for (int i = 0; i < plotlyCharts.Count; i++) { PlotlyChart plotlyChart = plotlyCharts[i]; Helpers.DumpJson($"{plotlyChart.CUid}.json", plotlyChart, DataSerializer.Instance); } }
public static PlotlyChart BuildChart(TimeSeriesAnalysis analysis) { IEnumerable <Graph.Trace> traces = BuildTraces(analysis); PlotlyChart chart = BuildPlotlyChart(analysis.TimeSeries.Name, traces); return(chart); }
public static IHtmlContent GetHtml(this PlotlyChart chart) { var divElement = div[style : $"width: {chart.Width}px; height: {chart.Height}px;", id : chart.Id](); var jsElement = chart.GetInlineJS().Replace("<script>", string.Empty).Replace("</script>", string.Empty); return(new HtmlString($@"{divElement} {GetScriptElementWithRequire(jsElement)}")); }
public void Returns_the_html_with_div() { var chart = new PlotlyChart(); var html = PlotlyChartExtensions.GetHtml(chart); var document = new HtmlDocument(); document.LoadHtml(html); document.DocumentNode.SelectSingleNode("//div").InnerHtml.Should().NotBeNull(); document.DocumentNode.SelectSingleNode("//div").Id.Should().NotBeNullOrEmpty(); }
public void Returns_the_html_with_div() { var chart = new PlotlyChart(); var html = chart.ToDisplayString(HtmlFormatter.MimeType); var document = new HtmlDocument(); document.LoadHtml(html.ToString()); document.DocumentNode.SelectSingleNode("//div").InnerHtml.Should().NotBeNull(); document.DocumentNode.SelectSingleNode("//div").Id.Should().NotBeNullOrEmpty(); }
public void Returns_the_html_with_script_containing_require_config() { var chart = new PlotlyChart(); var html = PlotlyChartExtensions.GetHtml(chart); var document = new HtmlDocument(); document.LoadHtml(html); document.DocumentNode.SelectSingleNode("//script") .InnerHtml .Should() .Contain("var xplotRequire = require.config({context:'xplot-3.0.1',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}}) || require;"); }
public void Returns_the_html_with_script_containing_require_config() { var chart = new PlotlyChart(); var html = chart.ToDisplayString(HtmlFormatter.MimeType); var document = new HtmlDocument(); document.LoadHtml(html.ToString()); document.DocumentNode.SelectSingleNode("//script") .InnerHtml .Should() .Contain("var xplotRequire = require.config({context:'xplot-3.0.1',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}}) || require;"); }
public async Task PlayerChart(string name) { using (var c = new BarcabotDatabaseConnection()) { var playerObject = c.GetPlayerByName(name); if (playerObject == null) { await Context.Channel.SendMessageAsync( $":warning: Error: Could not find player `{name}`. Are you sure they exist and are a FCB player?\nIf you think there is a player missing from the database please report it to the creator of BarcaBot `Trace#8994`."); } else { var convertedName = NameConverter.ConvertName(playerObject.Name); var stats = playerObject.Per90Stats; var x = new ArrayList { "Shots", "Shots on Target", "Key Passes", "Tackles", "Blocks", "Interceptions", "Duels Won", "Dribbles Attempted", "Dribbles Won", "Fouls Drawn", "Fouls Committed" }; var y = new ArrayList { stats.Shots.Total, stats.Shots.OnTarget, stats.Passes.KeyPasses, stats.Tackles.TotalTackles, stats.Tackles.Blocks, stats.Tackles.Interceptions, stats.Duels.Won, stats.Dribbles.Attempted, stats.Dribbles.Won, stats.Fouls.Drawn, stats.Fouls.Committed }; var chart = new PlotlyChart { Figure = new Figure { Data = new ArrayList { new BarTrace { X = x, Y = y, Name = convertedName } }, Layout = GetLayout($"{convertedName} Per 90 Stats", false) }, Height = 500, Width = 1000 }; var chartAsBytes = await _plotlyClient.GetChartAsByteArray(chart); var stream = new MemoryStream(chartAsBytes); await Context.Channel.SendFileAsync(stream, "chart.png"); } } }
public string GetChartHtml(PlotlyChart chart) { var document = new HtmlDocument(); document.LoadHtml(chart.GetInlineHtml()); var divNode = document.DocumentNode.SelectSingleNode("//div"); var scriptNode = document.DocumentNode.SelectSingleNode("//script"); var newHtmlDocument = new HtmlDocument(); newHtmlDocument.DocumentNode.ChildNodes.Add(divNode); newHtmlDocument.DocumentNode.ChildNodes.Add(GetScriptNodeWithRequire(scriptNode)); return(newHtmlDocument.DocumentNode.WriteContentTo()); }
private static PlotlyChart BuildChart(string groupName, IEnumerable <Graph.Histogram> traces) { PlotlyChart chart = Chart.Plot(traces); var layout = new Layout.Layout { title = $"{groupName} RMSE", barmode = "overlay" }; chart.WithLayout(layout); chart.Width = 800; chart.Height = 500; chart.WithLegend(true); return(chart); }
private static PlotlyChart BuildPlotlyChart(string chartTitle, IEnumerable <Graph.Trace> traces) { PlotlyChart chart = Chart.Plot(traces); var layout = new Layout.Layout { title = chartTitle }; chart.WithLayout(layout); chart.Width = 800; chart.Height = 500; chart.WithXTitle("Date"); chart.WithYTitle("Value"); chart.WithLegend(true); return(chart); }
private static void ShowCharts(TimeSeriesAnalysis[] analysisResults) { var charts = new List <PlotlyChart>(); foreach (TimeSeriesAnalysis analysis in analysisResults) { PlotlyChart chart = ChartBuilder.BuildChart(analysis); charts.Add(chart); } foreach (IGrouping <string, TimeSeriesAnalysis> analysisGroup in analysisResults.ToLookup(a => a.TimeSeries.Group)) { PlotlyChart histogram = HistogramBuilder.BuildHistogram(analysisGroup.Key, analysisGroup.ToList()); charts.Add(histogram); } Chart.ShowAll(charts); }
public void Returns_the_html_with_script_containing_require_plotly_and_call_to_new_plot_function() { var chart = new PlotlyChart(); var html = chart.ToDisplayString(HtmlFormatter.MimeType); var document = new HtmlDocument(); document.LoadHtml(html.ToString()); var divId = document.DocumentNode.SelectSingleNode("//div").Id; document.DocumentNode .SelectSingleNode("//script") .InnerHtml.Split("\n") .Select(item => item.Trim()) .Where(item => !string.IsNullOrWhiteSpace(item)) .Should() .ContainInOrder(@"xplotRequire(['plotly'], function(Plotly) {", "var data = null;", @"var layout = """";", $"Plotly.newPlot('{divId}', data, layout);"); }
public static PlotlyList LoadPlotlyTablesAll(string dataFolder) { Console.WriteLine($"Start loading Plotly tables from {dataFolder}."); TextReader tr = new StreamReader(dataFolder + @"\plotly_data_dedup.tsv"); string line; var processedLines = 0; List <PlotlyTable> plotlyTables = new List <PlotlyTable>(); List <PlotlyChart> plotlyCharts = new List <PlotlyChart>(); line = tr.ReadLine();//The fist line while ((line = tr.ReadLine()) != null) { if (processedLines % 100 == 0) { var logLine = string.Format("[Info][{0}] ================ Processed Lines: {1} ================", DateTime.Now.ToString("HH:mm:ss"), processedLines); Console.WriteLine(logLine); } processedLines++; var items = line.Split('\t'); PlotlyTable plotlyTable = PlotlyTable.GetInstance(items[0], dataFolder + @"\data_origin\"); List <PlotlyChart> plotlyChart = PlotlyChart.GetInstance(items[0], plotlyTable, dataFolder + @"\data_origin\"); if (plotlyChart.Count != 0) { plotlyTables.Add(plotlyTable); plotlyCharts = plotlyCharts.Concat(plotlyChart).ToList <PlotlyChart>(); } } Console.WriteLine($"{processedLines} plotly tables loaded."); return(new PlotlyList { PlotlyTables = plotlyTables, PlotlyCharts = plotlyCharts }); }
public async Task <byte[]> GetStatsBarChart(IList <Player> players) { var chart = new PlotlyChart { Figure = new Figure { Data = new ArrayList() }, Height = 500, Width = 1000 }; var chartLayout = GetDefaultLayout(); var x = new ArrayList { "Shots", "Shots on Target", "Key Passes", "Tackles", "Blocks", "Interceptions", "Duels Won", "Dribbles Attempted", "Dribbles Won", "Fouls Drawn", "Fouls Committed" }; foreach (var player in players) { var stats = player.Statistics; var converter = new ToPer90Converter(stats.Games.MinutesPlayed); var y = new ArrayList { converter.ToPer90(stats.Shots.Total), converter.ToPer90(stats.Shots.OnTarget), converter.ToPer90(stats.Passes.Key), converter.ToPer90(stats.Tackles.Total), converter.ToPer90(stats.Tackles.Blocks), converter.ToPer90(stats.Tackles.Interceptions), converter.ToPer90(stats.Duels.Won), converter.ToPer90(stats.Dribbles.Attempts), converter.ToPer90(stats.Dribbles.Success), converter.ToPer90(stats.Fouls.Drawn), converter.ToPer90(stats.Fouls.Committed) }; var trace = new BarTrace { X = x, Y = y, Name = player.Name }; chart.Figure.Data.Add(trace); } if (players.Count == 1) { chartLayout.Title.Text = $"{players[0].Name} per 90 statistics"; chartLayout.ShowLegend = false; } else { chartLayout.Title.Text = "Per 90 statistics comparison"; chartLayout.ShowLegend = true; } chart.Figure.Layout = chartLayout; return(await _plotlyClient.GetChartAsByteArray(chart)); }
static async Task Main(string[] args) { /* * Example 1 * * Example of how to generate a chart using the built-in PlotlyChart type * and save it to a PNG file */ // very basic example, hardcoding credentials is HIGHLY not recommended PlotlyCredentials Func() => new PlotlyCredentials { Username = "******", Token = "plotly_token" }; // create a plotly client that will communicate with Plotly API PlotlyClient plotlyClient = new PlotlyClient(Client, Func); // create a chart PlotlyChart myChart = new PlotlyChart { Figure = new Figure { Data = new ArrayList { new BarTrace { X = new ArrayList { 1, 2, 3 }, Y = new ArrayList { 1, 2, 3 }, }, new BarTrace { X = new ArrayList { 1, 2, 3 }, Y = new ArrayList { 1, 2, 3 }, } }, }, Height = 1000, Width = 1000 }; // send the chart to Plotly to compute an image and receive it as a PNG file as byte array byte[] myImg = await plotlyClient.GetChartAsByteArray(myChart); // save it to a file var imgStream = new MemoryStream(myImg); using (var fileStream = File.Create("example1.png")) { imgStream.CopyTo(fileStream); } Thread.Sleep(2000); /* * Example 2 * * Example of how to generate a chart using a JSON object string * and save it to a PNG file */ // must be escaped var escapedJsonObjectString = "{\"figure\": {\"data\": [{\"y\": [10, 10, 2, 20]}], \"layout\": {\"width\": 700}}, \"width\": 1000, \"height\": 500, \"format\": \"png\", \"encoded\": false}"; byte[] myImg2 = await plotlyClient.GetChartAsByteArray(escapedJsonObjectString); // save it to a file MemoryStream imgStream2 = new MemoryStream(myImg2); using (FileStream fileStream = File.Create("example2.png")) { imgStream2.CopyTo(fileStream); } }
public static void Plot(string pattern, bool generateHtml = false) { var iterationCounts = new List <int> { 1, 10, 25, 50, 100, 250, 500, 1000, 2000, 3000, 4000, 5000 }; var regexTimer = new RegexTimer(); List <RegexTimer.RegexTimerResult> timesStandard = iterationCounts .Select(iterationCount => regexTimer.Standard(pattern, iterations: iterationCount)) .ToList(); List <RegexTimer.RegexTimerResult> timesStandardReused = iterationCounts .Select(iterationCount => regexTimer.StandardReused(pattern, iterations: iterationCount)) .ToList(); List <RegexTimer.RegexTimerResult> timesCompiled = iterationCounts .Select(iterationCount => regexTimer.Compiled(pattern, iterations: iterationCount)) .ToList(); var graph1 = new Graph.Scatter { x = timesStandard.Select(x => x.IterationCount), y = timesStandard.Select(x => x.ElapsedTicks), name = "Standard Regex" }; var graph2 = new Graph.Scatter { x = timesStandardReused.Select(x => x.IterationCount), y = timesStandardReused.Select(x => x.ElapsedTicks), name = "Reused Regex" }; var graph3 = new Graph.Scatter { x = timesCompiled.Select(x => x.IterationCount), y = timesCompiled.Select(x => x.ElapsedTicks), name = "Compiled Regex" }; PlotlyChart chart = Chart.Plot( new[] { graph1, graph2, graph3 } ); chart.WithXTitle("Iteration Count"); chart.WithYTitle("Ticks"); chart.WithTitle("Regex on 100 characters"); chart.Show(); if (generateHtml) { string html = chart.GetHtml(); using var outFile = new StreamWriter( Path.Combine(PathUtil.GetSolutionBasePath(), "Plots", "PlotByIterationCount.html") ); outFile.Write(html); } }