Skip to content

Commit

Permalink
Had to downcast events specifically
Browse files Browse the repository at this point in the history
The CSV parser is being really annoying and not automatically downcasting base type arrays.
This "fix" is unfortunately quite ugly and will likely need to be applied to many other `WriteToCsv` calls in the future. 😠
Original hash: 92be685
  • Loading branch information
atruskie committed Dec 8, 2017
1 parent 9c72787 commit 2cb84c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 22 additions & 0 deletions Acoustics/Acoustics.Test/Shared/CsvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,28 @@ public void TestBaseTypesAreSerializedAsEnumerable()
Assert.AreEqual(childText, baseText);
}

/// <summary>
/// For some inane reason CsvHelper does not downcast to derived types!
/// </summary>
[TestMethod]
public void TestBaseTypesAreSerializedAsEnumerableAcousticEvent()
{
var exampleEvent = new AcousticEvent(100.Seconds(), 15, 4, 100, 3000);
var exampleEvent2 = new AcousticEvent(100.Seconds(), 15, 4, 100, 3000);
AcousticEvent[] childArray = { exampleEvent, exampleEvent2 };
EventBase[] baseArray = { exampleEvent, exampleEvent2 };

Csv.WriteToCsv(this.testFile, childArray);

var childText = File.ReadAllText(this.testFile.FullName);

Csv.WriteToCsv(this.testFile, baseArray);

var baseText = File.ReadAllText(this.testFile.FullName);

Assert.AreNotEqual(childText, baseText);
}

[TestMethod]
public void TestInvariantCultureIsUsed()
{
Expand Down
6 changes: 4 additions & 2 deletions AudioAnalysis/AnalysisPrograms/KoalaMale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace AnalysisPrograms
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using Acoustics.Shared;
using Acoustics.Shared.Contracts;
using Acoustics.Shared.Csv;
Expand Down Expand Up @@ -288,7 +289,8 @@ public static KoalaMaleResults Analysis(AudioRecording recording, IDictionary<st
maxDuration,
out scores,
out events,
out hits);
out hits,
segmentStartOffset);

// remove isolated koala events - this is to remove false positive identifications
events = FilterMaleKoalaEvents(events);
Expand Down Expand Up @@ -506,7 +508,7 @@ public override void SummariseResults(

public override void WriteEventsFile(FileInfo destination, IEnumerable<EventBase> results)
{
Csv.WriteToCsv(destination, results);
Csv.WriteToCsv(destination, results.Cast<AcousticEvent>());
}

public override List<FileInfo> WriteSpectrumIndicesFiles(DirectoryInfo destination, string fileNameBase, IEnumerable<SpectralIndexBase> results)
Expand Down

0 comments on commit 2cb84c0

Please sign in to comment.