/// <summary> /// Reads a sequence of classification results from the file. /// </summary> /// <param name="fileName">A path to the file to read from.</param> /// <returns>A collection of <see cref="ClassificationResult{T}"/> structures created from the stream.</returns> public static IList <ClassificationResult <T> > Read(string fileName) { using (StreamReader reader = new StreamReader(fileName, System.Text.Encoding.UTF8)) { return(ClassificationResult <T> .Read(reader).ToList()); } }
/// <summary> /// Writes a sequence of classification results into the stream. /// </summary> /// <param name="writer">The stream to write to.</param> /// <param name="results">The sequence of <see cref="ClassificationResult{T}"/> structures to write.</param> public static void Write(TextWriter writer, IEnumerable <ClassificationResult <T> > results) { if (results == null) { throw new ArgumentNullException(nameof(results)); } foreach (ClassificationResult <T> result in results) { ClassificationResult <T> .Write(writer, result); } }
/// <summary> /// Initializes a new instance of the <see cref="ClassificationResult{T}" /> class that has the same property values as the specified <see cref="ClassificationResult{T}" />. /// </summary> /// <param name="other">The <see cref="ClassificationResult{T}" /> to copy property values from.</param> public ClassificationResult(ClassificationResult <T> other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } this.SourceId = other.SourceId; this.Predicted = other.Predicted; this.Expected = other.Expected; this.Confidence = other.Confidence; this.IsAccepted = other.IsAccepted; }
/// <summary> /// Writes class errors. /// </summary> /// <param name="writer">A stream to write the report to.</param> /// <param name="summary">Classification results for the class.</param> private static void WriteClassificationErrors(TextWriter writer, ClassSummary <T> summary) { int acceptedErrors = summary.Errors.Count(); if (acceptedErrors > 0) { writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", summary.Label, acceptedErrors)); writer.WriteLine(); ClassificationResult <T> .Write(writer, summary.Errors); writer.WriteLine("-----------------------------------------------------------------------------"); writer.WriteLine(); } }
/// <summary> /// Writes a classification result into the stream. /// </summary> /// <param name="writer">The stream to write to.</param> /// <param name="result">The <see cref="ClassificationResult{T}"/> structure to write.</param> public static void Write(TextWriter writer, ClassificationResult <T> result) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } writer.WriteLine(result.ToString()); }
/// <summary> /// Adds the classification result to the class summary. /// </summary> /// <param name="resut">The classification result.</param> public void Add(ClassificationResult <T> resut) { if (resut == null) { throw new ArgumentNullException(nameof(resut)); } // compare result with truth bool?isValid = object.Equals(resut.Expected, default(T)) ? null : (bool?)object.Equals(resut.Expected, resut.Predicted); // add to statistics this.Statistics.Add(resut.Confidence, resut.IsAccepted, isValid); // create an error record if (isValid.HasValue && !isValid.Value) { this.Errors.Add(new ClassificationResult <T>(resut)); } }
/// <summary> /// Writes a test report. /// </summary> /// <param name="writer">The writer used to write the report.</param> /// <param name="report">The report to write.</param> /// <param name="mode">Defines the components of the report to write.</param> public static void WriteReport(TextWriter writer, ClassificationReport <T> report, ClassificationReportMode mode) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (report == null) { throw new ArgumentNullException(nameof(report)); } IList <ClassSummary <T> > summaries = report.Classes.OrderBy(x => x.Label).ToList(); if (summaries.Any()) { // print report header if (mode.HasFlag(ClassificationReportMode.Summary)) { writer.WriteLine("============================================================================="); writer.WriteLine("SUMMARY"); int maxClassNameLength = Math.Max(summaries.Max(x => x.Label.ToString().Length), 8); writer.Write(string.Format(CultureInfo.InvariantCulture, "{{0,-{0}}},", maxClassNameLength), "Class"); writer.Write(ShortFormat, "Total", "%", "#"); writer.Write(LongFormat, "Total", "%", "#", "Error %", "Error #", "Valid %", "Valid #"); writer.WriteLine(); // print each class foreach (ClassSummary <T> summary in summaries) { ClassificationReportWriter <T> .WriteClassStatistics(writer, summary, maxClassNameLength); } // print summary writer.WriteLine(); ClassificationReportWriter <T> .WriteClassStatistics(writer, report.AllClasses, maxClassNameLength); } // print confusion matrix if (mode.HasFlag(ClassificationReportMode.ConfusionMatrix)) { writer.WriteLine(); writer.WriteLine("============================================================================="); writer.WriteLine("CONFUSION MATRIX"); writer.WriteLine(); ClassificationReportWriter <T> .WriteConfusionMatrix(writer, report.ConfusionMatrix); } // print reject curves if (mode.HasFlag(ClassificationReportMode.RejectCurves)) { writer.WriteLine(); writer.WriteLine("============================================================================="); writer.WriteLine("REJECT CURVES"); writer.WriteLine(); ClassificationReportWriter <T> .WriteRejectCurves(writer, report.AllClasses, summaries); } // print errors if (mode.HasFlag(ClassificationReportMode.Errors)) { writer.WriteLine(); writer.WriteLine("============================================================================="); writer.WriteLine("ERRORS"); writer.WriteLine(); foreach (ClassSummary <T> summary in summaries) { ClassificationReportWriter <T> .WriteClassificationErrors(writer, summary); } } } // print file results if (mode.HasFlag(ClassificationReportMode.Answers)) { writer.WriteLine(); writer.WriteLine("============================================================================="); writer.WriteLine("FILE RESULTS"); writer.WriteLine(); ClassificationResult <T> .Write(writer, report.Results); } }