protected void SendTelemetryMetric(Dictionary <string, IDataView>[] metricValues) { Dictionary <string, double> averageMetric = new Dictionary <string, double>(); foreach (Dictionary <string, IDataView> mValue in metricValues) { var data = mValue.First().Value; using (var cursor = data.GetRowCursorForAllColumns()) { while (cursor.MoveNext()) { for (int currentIndex = 0; currentIndex < cursor.Schema.Count; currentIndex++) { var nameOfMetric = "TLC_" + cursor.Schema[currentIndex].Name; var type = cursor.Schema[currentIndex].Type; if (type is NumberDataViewType) { var getter = RowCursorUtils.GetGetterAs <double>(NumberDataViewType.Double, cursor, currentIndex); double metricValue = 0; getter(ref metricValue); if (averageMetric.ContainsKey(nameOfMetric)) { averageMetric[nameOfMetric] += metricValue; } else { averageMetric.Add(nameOfMetric, metricValue); } } else { if (averageMetric.ContainsKey(nameOfMetric)) { averageMetric[nameOfMetric] = Double.NaN; } else { averageMetric.Add(nameOfMetric, Double.NaN); } } } } } } Dictionary <string, double> newAverageMetric = new Dictionary <string, double>(); foreach (var pair in averageMetric) { newAverageMetric.Add(pair.Key, pair.Value / metricValues.Length); } SendTelemetryMetricCore(Host, newAverageMetric); }
private static ValueGetter <Single> GetLabelGetterNotFloat(DataViewRow cursor, int labelIndex) { var type = cursor.Schema[labelIndex].Type; Contracts.Assert(type != NumberDataViewType.Single && type != NumberDataViewType.Double); // boolean type label mapping: True -> 1, False -> 0. if (type is BooleanDataViewType) { var getBoolSrc = cursor.GetGetter <bool>(labelIndex); return ((ref Single dst) => { bool src = default; getBoolSrc(ref src); dst = Convert.ToSingle(src); }); } if (!(type is KeyType keyType)) { throw Contracts.Except("Only floating point number, boolean, and key type values can be used as label."); } Contracts.Assert(TestGetLabelGetter(type) == null); ulong keyMax = (ulong)keyType.Count; if (keyMax == 0) { keyMax = ulong.MaxValue; } var getSrc = RowCursorUtils.GetGetterAs <ulong>(NumberDataViewType.UInt64, cursor, labelIndex); return ((ref Single dst) => { ulong src = 0; getSrc(ref src); if (0 < src && src <= keyMax) { dst = src - 1; } else { dst = Single.NaN; } }); }
private static ValueGetter <Single> GetLabelGetterNotFloat(Row cursor, int labelIndex) { var type = cursor.Schema[labelIndex].Type; Contracts.Assert(type != NumberType.R4 && type != NumberType.R8); // boolean type label mapping: True -> 1, False -> 0. if (type.IsBool) { var getBoolSrc = cursor.GetGetter <bool>(labelIndex); return ((ref Single dst) => { bool src = default; getBoolSrc(ref src); dst = Convert.ToSingle(src); }); } Contracts.Check(type.IsKey, "Only floating point number, boolean, and key type values can be used as label."); Contracts.Assert(TestGetLabelGetter(type) == null); ulong keyMax = (ulong)type.KeyCount; if (keyMax == 0) { keyMax = ulong.MaxValue; } var getSrc = RowCursorUtils.GetGetterAs <ulong>(NumberType.U8, cursor, labelIndex); return ((ref Single dst) => { ulong src = 0; getSrc(ref src); if (0 < src && src <= keyMax) { dst = src - 1; } else { dst = Single.NaN; } }); }
private protected override void PrintFoldResultsCore(IChannel ch, Dictionary <string, IDataView> metrics) { IDataView top; if (!metrics.TryGetValue(AnomalyDetectionEvaluator.TopKResults, out top)) { throw Host.Except("Did not find the top-k results data view"); } var sb = new StringBuilder(); using (var cursor = top.GetRowCursor(col => true)) { int index; if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Instance, out index)) { throw Host.Except("Data view does not contain the 'Instance' column"); } var instanceGetter = cursor.GetGetter <ReadOnlyMemory <char> >(index); if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.AnomalyScore, out index)) { throw Host.Except("Data view does not contain the 'Anomaly Score' column"); } var scoreGetter = cursor.GetGetter <Single>(index); if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Label, out index)) { throw Host.Except("Data view does not contain the 'Label' column"); } var labelGetter = cursor.GetGetter <Single>(index); bool hasRows = false; while (cursor.MoveNext()) { if (!hasRows) { sb.AppendFormat("{0} Top-scored Results", _topScored); sb.AppendLine(); sb.AppendLine("================================================="); sb.AppendLine("Instance Anomaly Score Labeled"); hasRows = true; } var name = default(ReadOnlyMemory <char>); Single score = 0; Single label = 0; instanceGetter(ref name); scoreGetter(ref score); labelGetter(ref label); sb.AppendFormat("{0,-10}{1,12:G4}{2,12}", name, score, label); sb.AppendLine(); } } if (sb.Length > 0) { ch.Info(MessageSensitivity.UserData, sb.ToString()); } IDataView overall; if (!metrics.TryGetValue(MetricKinds.OverallMetrics, out overall)) { throw Host.Except("No overall metrics found"); } // Find the number of anomalies, and the thresholds. int numAnomIndex; if (!overall.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies, out numAnomIndex)) { throw Host.Except("Could not find the 'NumAnomalies' column"); } int stratCol; var hasStrat = overall.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.StratCol, out stratCol); int stratVal; bool hasStratVals = overall.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.StratVal, out stratVal); Contracts.Assert(hasStrat == hasStratVals); long numAnomalies = 0; using (var cursor = overall.GetRowCursor(col => col == numAnomIndex || (hasStrat && col == stratCol))) { var numAnomGetter = cursor.GetGetter <long>(numAnomIndex); ValueGetter <uint> stratGetter = null; if (hasStrat) { var type = cursor.Schema[stratCol].Type; stratGetter = RowCursorUtils.GetGetterAs <uint>(type, cursor, stratCol); } bool foundRow = false; while (cursor.MoveNext()) { uint strat = 0; if (stratGetter != null) { stratGetter(ref strat); } if (strat > 0) { continue; } if (foundRow) { throw Host.Except("Found multiple non-stratified rows in overall results data view"); } foundRow = true; numAnomGetter(ref numAnomalies); } } var kFormatName = string.Format(FoldDrAtKFormat, _k); var pFormatName = string.Format(FoldDrAtPFormat, _p); var numAnomName = string.Format(FoldDrAtNumAnomaliesFormat, numAnomalies); (string Source, string Name)[] cols =