|
| 1 | +using AnomalyDetection.Common.DataModels; |
| 2 | +using Microsoft.ML.Runtime.Api; |
| 3 | +using Microsoft.ML.Runtime.Data; |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.IO.Compression; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | +namespace AnomalyDetection.Common |
| 10 | +{ |
| 11 | + public static class ConsoleHelpers |
| 12 | + { |
| 13 | + public static void ConsoleWriteHeader(params string[] lines) |
| 14 | + { |
| 15 | + var defaultColor = Console.ForegroundColor; |
| 16 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 17 | + Console.WriteLine(" "); |
| 18 | + foreach (var line in lines) |
| 19 | + { |
| 20 | + Console.WriteLine(line); |
| 21 | + } |
| 22 | + var maxLength = lines.Select(x => x.Length).Max(); |
| 23 | + Console.WriteLine(new string('#', maxLength)); |
| 24 | + Console.ForegroundColor = defaultColor; |
| 25 | + } |
| 26 | + |
| 27 | + public static void ConsoleWriterSection(params string[] lines) |
| 28 | + { |
| 29 | + var defaultColor = Console.ForegroundColor; |
| 30 | + Console.ForegroundColor = ConsoleColor.Blue; |
| 31 | + Console.WriteLine(" "); |
| 32 | + foreach (var line in lines) |
| 33 | + { |
| 34 | + Console.WriteLine(line); |
| 35 | + } |
| 36 | + var maxLength = lines.Select(x => x.Length).Max(); |
| 37 | + Console.WriteLine(new string('-', maxLength)); |
| 38 | + Console.ForegroundColor = defaultColor; |
| 39 | + } |
| 40 | + |
| 41 | + public static void ConsolePressAnyKey() |
| 42 | + { |
| 43 | + var defaultColor = Console.ForegroundColor; |
| 44 | + Console.ForegroundColor = ConsoleColor.Green; |
| 45 | + Console.WriteLine(" "); |
| 46 | + Console.WriteLine("Press any key to finish."); |
| 47 | + Console.ReadKey(); |
| 48 | + } |
| 49 | + |
| 50 | + public static void ConsoleWriteException(params string[] lines) |
| 51 | + { |
| 52 | + var defaultColor = Console.ForegroundColor; |
| 53 | + Console.ForegroundColor = ConsoleColor.Red; |
| 54 | + const string exceptionTitle = "EXCEPTION"; |
| 55 | + Console.WriteLine(" "); |
| 56 | + Console.WriteLine(exceptionTitle); |
| 57 | + Console.WriteLine(new string('#', exceptionTitle.Length)); |
| 58 | + Console.ForegroundColor = defaultColor; |
| 59 | + foreach (var line in lines) |
| 60 | + { |
| 61 | + Console.WriteLine(line); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void ConsoleWriteWarning(params string[] lines) |
| 66 | + { |
| 67 | + var defaultColor = Console.ForegroundColor; |
| 68 | + Console.ForegroundColor = ConsoleColor.DarkMagenta; |
| 69 | + const string warningTitle = "WARNING"; |
| 70 | + Console.WriteLine(" "); |
| 71 | + Console.WriteLine(warningTitle); |
| 72 | + Console.WriteLine(new string('#', warningTitle.Length)); |
| 73 | + Console.ForegroundColor = defaultColor; |
| 74 | + foreach (var line in lines) |
| 75 | + { |
| 76 | + Console.WriteLine(line); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static string GetAssetsPath(params string[] paths) |
| 81 | + { |
| 82 | + |
| 83 | + FileInfo _dataRoot = new FileInfo(typeof(ConsoleHelpers).Assembly.Location); |
| 84 | + if (paths == null || paths.Length == 0) |
| 85 | + return null; |
| 86 | + |
| 87 | + return Path.Combine(paths.Prepend(_dataRoot.Directory.FullName).ToArray()); |
| 88 | + } |
| 89 | + |
| 90 | + public static string DeleteAssets(params string[] paths) |
| 91 | + { |
| 92 | + var location = GetAssetsPath(paths); |
| 93 | + |
| 94 | + if (!string.IsNullOrWhiteSpace(location) && File.Exists(location)) |
| 95 | + File.Delete(location); |
| 96 | + return location; |
| 97 | + } |
| 98 | + |
| 99 | + public static void InspectData(LocalEnvironment env, IDataView data) |
| 100 | + { |
| 101 | + // lets inspect data |
| 102 | + //ConsoleWriteHeader("Show 4"); |
| 103 | + ShowVectorModel(env, data, label: true); |
| 104 | + ShowVectorModel(env, data, label: false); |
| 105 | + } |
| 106 | + |
| 107 | + public static void InspectScoredData(LocalEnvironment env, IDataView data) |
| 108 | + { |
| 109 | + // lets inspect data |
| 110 | + //ConsoleWriteHeader("Show 4"); |
| 111 | + ShowEstimatorModel(env, data, label: true); |
| 112 | + ShowEstimatorModel(env, data, label: false); |
| 113 | + } |
| 114 | + |
| 115 | + public static void ShowVectorModel(LocalEnvironment env, IDataView data, bool label = true, int count = 2) |
| 116 | + { |
| 117 | + data |
| 118 | + // Convert to an enumerable of user-defined type. |
| 119 | + .AsEnumerable<TransactionVectorModel>(env, reuseRowObject: false) |
| 120 | + .Where(x => x.Label == label) |
| 121 | + // Take a couple values as an array. |
| 122 | + .Take(count) |
| 123 | + .ToList() |
| 124 | + // print to console |
| 125 | + .ForEach(row => { row.PrintToConsole(); }); |
| 126 | + } |
| 127 | + |
| 128 | + public static void ShowEstimatorModel(LocalEnvironment env, IDataView data, bool label = true, int count = 2) |
| 129 | + { |
| 130 | + data |
| 131 | + // Convert to an enumerable of user-defined type. |
| 132 | + .AsEnumerable<TransactionEstimatorModel>(env, reuseRowObject: false) |
| 133 | + .Where(x => x.Label == label) |
| 134 | + // Take a couple values as an array. |
| 135 | + .Take(count) |
| 136 | + .ToList() |
| 137 | + // print to console |
| 138 | + .ForEach(row => { row.PrintToConsole(); }); |
| 139 | + } |
| 140 | + |
| 141 | + public static void UnZipDataSet(string zipDataSet, string destinationFile) |
| 142 | + { |
| 143 | + if (!File.Exists(destinationFile)) |
| 144 | + { |
| 145 | + var destinationDirectory = Path.GetDirectoryName(destinationFile); |
| 146 | + ZipFile.ExtractToDirectory(zipDataSet, $"{destinationDirectory}"); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments