Skip to content

Commit 7ebcfbc

Browse files
authored
fix: locate a workbook cell that does not fit a reading among its outliers (#194)
`TypeHypothesis.Outliers` promises where the values a reading does not account for stand. For a cell whose type the workbook states, that promise was not kept. A number in a column of xlsx or ods dates, for example Excel's phantom 29 February 1900, which reads as its number since #189, was counted in `UnmatchedCount`, but no outlier gave its row. The profiler located only text cells that failed to parse. Native cells went to `AcceptNativeNumber`/`AcceptNativeDate` and were never located for the readings they did not fit. In a column the file declared entirely, `HypothesisBuilder` also passed empty outlier lists. Noticed while fixing serial 60 for #184. What changes: - `ColumnProfiler`, for each culture: - a native number is a date outlier; - a native date is a numeric outlier; - a native boolean is both; - each is recorded with its row and text, up to `OutlierSampleSize`, in row order with the text cells. - `HypothesisBuilder.AddDeclared` passes the date and numeric outliers to the date and number readings instead of empty lists. This also helps mixed columns of text and native cells. Their parsed readings now list the native cells that do not fit as well. Cost: one count comparison per culture for each native cell, once the first `OutlierSampleSize` are taken. Benchmark: see the comment below. Tests in `ColumnProfilerTests`: - dates with a number and a boolean list the right rows for both readings, and the date hypothesis lists them too; - the outlier limit holds.
1 parent 9cebf23 commit 7ebcfbc

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎src/TriasDev.Tabular/Analysis/ColumnProfiler.cs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,27 @@ public void Accept(in RawCell cell, int rowNumber)
132132
}
133133

134134
// A workbook says what a cell is; a csv file does not. Where the file has already decided,
135-
// every culture agrees with it, and no parsing is attempted or needed.
135+
// every culture agrees with it, and no parsing is attempted or needed. A cell of one kind is
136+
// still located as an outlier of the readings it does not fit — a number in a date column —
137+
// or the profile would count a value that does not fit without being able to say where.
136138
switch (cell.Kind)
137139
{
138140
case RawCellKind.Boolean:
139141
_booleanCount++;
142+
143+
foreach (CultureAccumulator culture in _cultures)
144+
{
145+
culture.NoteNoNumber(rowNumber, text);
146+
culture.NoteNoDate(rowNumber, text);
147+
}
148+
140149
return;
141150

142151
case RawCellKind.Number:
143152
foreach (CultureAccumulator culture in _cultures)
144153
{
145154
culture.AcceptNativeNumber(cell.Number);
155+
culture.NoteNoDate(rowNumber, text);
146156
}
147157

148158
return;
@@ -151,6 +161,7 @@ public void Accept(in RawCell cell, int rowNumber)
151161
foreach (CultureAccumulator culture in _cultures)
152162
{
153163
culture.AcceptNativeDate(cell.Date);
164+
culture.NoteNoNumber(rowNumber, text);
154165
}
155166

156167
return;
@@ -610,6 +621,26 @@ public void AcceptNativeDate(DateTime value)
610621
Widen(value);
611622
}
612623

624+
/// <summary>Locates a value the file stated as something other than a number.</summary>
625+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
626+
public void NoteNoNumber(int rowNumber, string text)
627+
{
628+
if (_numericOutliers.Count < outlierLimit)
629+
{
630+
_numericOutliers.Add(new ValueLocation { RowNumber = rowNumber, RawValue = text });
631+
}
632+
}
633+
634+
/// <summary>Locates a value the file stated as something other than a date.</summary>
635+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
636+
public void NoteNoDate(int rowNumber, string text)
637+
{
638+
if (_dateOutliers.Count < outlierLimit)
639+
{
640+
_dateOutliers.Add(new ValueLocation { RowNumber = rowNumber, RawValue = text });
641+
}
642+
}
643+
613644
/// <summary>Whether this culture reads numbers exactly as <paramref name="other"/> does.</summary>
614645
/// <remarks>
615646
/// Everything the two parses below and the grouping rule consult: the separators, the group

‎src/TriasDev.Tabular/Analysis/HypothesisBuilder.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ private static void AddDeclared(ColumnFacts facts, List<TypeHypothesis> hypothes
9898
CultureParseCounts counts = facts.ParseCounts[0];
9999

100100
Add(hypotheses, ColumnType.Boolean, null, Native(facts, RawCellKind.Boolean), facts.NonEmptyCount, []);
101-
Add(hypotheses, ColumnType.Date, null, Native(facts, RawCellKind.Date), facts.NonEmptyCount, []);
101+
Add(hypotheses, ColumnType.Date, null, Native(facts, RawCellKind.Date), facts.NonEmptyCount, counts.DateOutliers);
102102

103103
int numbers = Native(facts, RawCellKind.Number);
104104

105105
if (numbers > 0)
106106
{
107107
// A whole number is also a decimal, so the narrower reading is offered only when every
108108
// value bears it.
109-
Add(hypotheses, ColumnType.Decimal, null, numbers, facts.NonEmptyCount, []);
109+
Add(hypotheses, ColumnType.Decimal, null, numbers, facts.NonEmptyCount, counts.NumericOutliers);
110110

111111
if (counts.Integer == numbers)
112112
{
113-
Add(hypotheses, ColumnType.Integer, null, numbers, facts.NonEmptyCount, []);
113+
Add(hypotheses, ColumnType.Integer, null, numbers, facts.NonEmptyCount, counts.NumericOutliers);
114114
}
115115
}
116116
}

‎tests/TriasDev.Tabular.Tests/Analysis/ColumnProfilerTests.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,40 @@ public void ReportsTheRangeOfAWorkbooksOwnDates()
320320
Assert.Equal(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Unspecified), facts.MaxDate);
321321
Assert.All(facts.ParseCounts, c => Assert.Equal(2, c.Date));
322322
}
323+
324+
/// <summary>
325+
/// A workbook cell of one kind in a column of another — a number among dates, as Excel's phantom
326+
/// 29 February 1900 reads — is located as an outlier of the readings it does not fit, as a text
327+
/// cell is; it used to be counted as unmatched with no row to show for it.
328+
/// </summary>
329+
[Fact]
330+
public void LocatesAWorkbookCellThatDoesNotFitAReading()
331+
{
332+
static RawCell Day(int day) => RawCell.FromDate(new DateTime(2024, 1, day, 0, 0, 0, DateTimeKind.Unspecified));
333+
334+
ColumnFacts facts = ProfileCells([Day(1), RawCell.FromNumber(60), Day(3), RawCell.FromBoolean(true), Day(5)]);
335+
336+
Assert.All(facts.ParseCounts, c =>
337+
{
338+
Assert.Equal([2, 4], c.DateOutliers.Select(o => o.RowNumber));
339+
Assert.Equal([1, 3, 4, 5], c.NumericOutliers.Select(o => o.RowNumber));
340+
});
341+
Assert.Equal("60", For(facts, "de-DE").DateOutliers[0].RawValue);
342+
343+
TypeHypothesis date = HypothesisBuilder.Build(facts).Single(h => h.Type == ColumnType.Date);
344+
Assert.Equal(2, date.UnmatchedCount);
345+
Assert.Equal([2, 4], date.Outliers.Select(o => o.RowNumber));
346+
}
347+
348+
[Fact]
349+
public void LocatesWorkbookCellsThatDoNotFitOnlyUpToTheOutlierLimit()
350+
{
351+
AnalysisOptions options = AnalysisOptions.Default with { OutlierSampleSize = 3 };
352+
RawCell[] cells = [RawCell.FromNumber(1), .. Enumerable.Range(0, 10).Select(i => RawCell.FromDate(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)))];
353+
354+
ColumnFacts facts = ProfileCells(cells, options);
355+
356+
Assert.Equal([2, 3, 4], For(facts, "de-DE").NumericOutliers.Select(o => o.RowNumber));
357+
Assert.Equal([1], For(facts, "de-DE").DateOutliers.Select(o => o.RowNumber));
358+
}
323359
}

0 commit comments

Comments
 (0)