Added standard deviation
This commit is contained in:
@@ -4,13 +4,44 @@ public static class EnumerableExtensions
|
||||
{
|
||||
public static double Median(this IEnumerable<double> values)
|
||||
{
|
||||
var tValues = values.ToArray();
|
||||
return tValues
|
||||
.OrderBy(x => x)
|
||||
.Skip(tValues.Length / 2)
|
||||
.First();
|
||||
var tValues = values.OrderBy(v => v).ToArray();
|
||||
if (!tValues.Any())
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"The list must contain at least one value for calculating the median");
|
||||
}
|
||||
|
||||
if (tValues.Length % 2 != 0)
|
||||
{
|
||||
return tValues[tValues.Length / 2];
|
||||
}
|
||||
|
||||
var center = tValues.Length / 2;
|
||||
return (tValues[center - 1] + tValues[center]) / 2.0;
|
||||
}
|
||||
|
||||
public static double Median<T>(this IEnumerable<T> items, Func<T, double> selector) =>
|
||||
items.Select(selector).Median();
|
||||
public static double Median(this IEnumerable<double> values, out double deviation)
|
||||
{
|
||||
var tValues = values.ToArray();
|
||||
var median = tValues.Median();
|
||||
deviation = tValues.Select(value => Math.Abs(value - median)).Median();
|
||||
return median;
|
||||
}
|
||||
|
||||
public static double Average(this IEnumerable<double> values, out double deviation)
|
||||
{
|
||||
var tValues = values?.ToArray();
|
||||
if (tValues is null || tValues.Length < 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"The list must contain at least two values for calculating standard deviation");
|
||||
}
|
||||
|
||||
var average = tValues.Average();
|
||||
|
||||
var diffSquaredSum = tValues.Sum(value => Math.Pow(value - average, 2));
|
||||
deviation = Math.Sqrt(diffSquaredSum / (tValues.Length - 1));
|
||||
|
||||
return average;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ internal class CollapsibleHtmlTableGenerator : HtmlTableGenerator
|
||||
/// <inheritdoc />
|
||||
protected override void OnOpen()
|
||||
{
|
||||
Writer.Write($"<details class={DetailsClass}>");
|
||||
Writer.Write($"<details class=\"{DetailsClass}\" open>");
|
||||
Writer.Write(HtmlTools.Wrap("summary", Summary));
|
||||
base.OnOpen();
|
||||
}
|
||||
|
||||
@@ -23,9 +23,17 @@ internal static class Program
|
||||
|
||||
using var document = new HtmlDocumentGenerator(path);
|
||||
using var report = new ReportGenerator("OCR Report", document, scans)
|
||||
.AddComparison("Processing summary (Average)", v => v.Average())
|
||||
.AddComparison("Processing summary (Average)", v =>
|
||||
{
|
||||
var result = v.Average(out var deviation);
|
||||
return (result, deviation);
|
||||
})
|
||||
// .AddComparison("Processing summary (Cumulative)", v => v.Sum())
|
||||
.AddComparison("Processing summary (Median)", v => v.Median())
|
||||
.AddComparison("Processing summary (Median)", v =>
|
||||
{
|
||||
var result = v.Median(out var deviation);
|
||||
return (result, deviation);
|
||||
})
|
||||
// .AddProcessorStats("Processor Stats")
|
||||
.AddImageStatsFull("Scan Results");
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ public class ReportGenerator : IDisposable
|
||||
|
||||
public ReportGenerator AddComparison(
|
||||
string title,
|
||||
Func<IEnumerable<double>, double> evaluationFunc
|
||||
Func<IEnumerable<double>, (double, double)> evaluationFunc
|
||||
)
|
||||
{
|
||||
var lookup = Images
|
||||
@@ -153,28 +153,44 @@ public class ReportGenerator : IDisposable
|
||||
Document.AppendHeading(2, title);
|
||||
|
||||
var byWer = lookup
|
||||
.Select(g => (
|
||||
Name: g.Key,
|
||||
Value: evaluationFunc(g.Select(p => p.Distance)) * 100
|
||||
))
|
||||
.Select(g =>
|
||||
{
|
||||
var (value, deviation) = evaluationFunc(g.Select(p => p.Distance * 100));
|
||||
return (
|
||||
Name: g.Key,
|
||||
Value: value,
|
||||
Deviation: deviation
|
||||
);
|
||||
})
|
||||
.OrderBy(g => g.Value);
|
||||
Document.AppendHeading(3, "WER");
|
||||
AppendComparison(("Error", "%"), byWer);
|
||||
|
||||
var byCer = lookup
|
||||
.Select(g => (
|
||||
Name: g.Key,
|
||||
Value: evaluationFunc(g.SelectMany(p => p.Words, (_, word) => word.Distance))
|
||||
))
|
||||
.Select(g =>
|
||||
{
|
||||
var (value, deviation) =
|
||||
evaluationFunc(g.SelectMany(p => p.Words, (_, word) => word.Distance));
|
||||
return (
|
||||
Name: g.Key,
|
||||
Value: value,
|
||||
Deviation: deviation
|
||||
);
|
||||
})
|
||||
.OrderBy(g => g.Value);
|
||||
Document.AppendHeading(3, "CER");
|
||||
AppendComparison(("Changes", string.Empty), byCer);
|
||||
|
||||
var byTime = lookup
|
||||
.Select(g => (
|
||||
Name: g.Key,
|
||||
Value: evaluationFunc(g.Select(p => p.ProcessingTime)) * 1000
|
||||
))
|
||||
.Select(g =>
|
||||
{
|
||||
var (value, deviation) = evaluationFunc(g.Select(p => p.ProcessingTime * 1000));
|
||||
return (
|
||||
Name: g.Key,
|
||||
Value: value,
|
||||
Deviation: deviation
|
||||
);
|
||||
})
|
||||
.OrderBy(g => g.Value);
|
||||
Document.AppendHeading(3, "Time");
|
||||
AppendComparison(("Time", "ms"), byTime);
|
||||
@@ -185,7 +201,7 @@ public class ReportGenerator : IDisposable
|
||||
|
||||
private void AppendComparison(
|
||||
(string name, string unit)? valueInfo,
|
||||
IEnumerable<(string, double)> values
|
||||
IEnumerable<(string, double, double)> values
|
||||
)
|
||||
{
|
||||
const int context = 5;
|
||||
@@ -193,10 +209,15 @@ public class ReportGenerator : IDisposable
|
||||
var tValues = values.ToArray();
|
||||
var tContext = Math.Min(tValues.Length / 2, context);
|
||||
|
||||
Document.AppendTable(2, table =>
|
||||
Document.AppendTable(3, table =>
|
||||
{
|
||||
table
|
||||
.AppendHeader(new[] { "Processor", valueInfo?.name ?? "Value" })
|
||||
.AppendHeader(new[]
|
||||
{
|
||||
"Processor",
|
||||
valueInfo?.name ?? "Value",
|
||||
"Deviation"
|
||||
})
|
||||
.AppendRows(tValues
|
||||
.Take(tContext)
|
||||
.Select(MakeRow))
|
||||
@@ -209,8 +230,13 @@ public class ReportGenerator : IDisposable
|
||||
|
||||
return;
|
||||
|
||||
string[] MakeRow((string, double) v) =>
|
||||
new[] { v.Item1, v.Item2.ToString("F2") + valueInfo?.unit };
|
||||
string[] MakeRow((string, double, double) v) =>
|
||||
new[]
|
||||
{
|
||||
v.Item1,
|
||||
v.Item2.ToString("F2") + valueInfo?.unit,
|
||||
v.Item3.ToString("F2") + valueInfo?.unit
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user