Skip to content

Commit

Permalink
Fixed the method for padding tile boudaries
Browse files Browse the repository at this point in the history
The tile boundary padding method was incorrect. It has been updated to match the behaviour of baw-client and a bunch more tests were added.
  • Loading branch information
atruskie committed Mar 2, 2018
1 parent c908265 commit a5e9c39
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,18 @@ private static (Dictionary<string, double[,]>, Dictionary<string, IndexPropertie

public static DateTimeOffset GetPreviousTileBoundary(int tileWidth, double scale, DateTimeOffset recordingStartDate)
{
// if recording does not start on an absolutely aligned hour of the day
// align it, then adjust where the tiling starts from, and calculate the offset for the super tile (the gap)
var timeOfDay = recordingStartDate.TimeOfDay;
var previousAbsoluteHour =
TimeSpan.FromSeconds(
Math.Floor(timeOfDay.TotalSeconds / (scale * tileWidth))
* (scale * tileWidth));
var gap = timeOfDay - previousAbsoluteHour;
var tilingStartDate = recordingStartDate - gap;
return tilingStartDate;
// if recording does not start on an absolutely aligned tile boundary, then we align it.
// "midnight" is 00:00 UTC - that way all recordings have a common tiling start point
var utcDate = recordingStartDate.UtcDateTime;
var utcMidnight = recordingStartDate.Date;
var delta = utcDate - utcMidnight;
var tileDuration = scale * tileWidth;
var numberOfTilesBeforeStartDate = Math.Floor(delta.TotalSeconds / tileDuration);

var paddingDuration = TimeSpan.FromSeconds(numberOfTilesBeforeStartDate * tileDuration);
var previous = new DateTimeOffset(utcMidnight + paddingDuration, TimeSpan.Zero);

return previous;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,58 @@ namespace Acoustics.Test.AudioAnalysisTools.LongDurationSpectrograms.Zooming
[TestClass]
public class ZoomTiledSpectrogramTests
{
/// <summary>
/// This method tests how far back we have to pad the first tile of a recording.
/// It's pretty complicated.
/// All tiles start from midnight UTC each day. Then a natural number of tiles
/// fits within the day. The number of tiles with a day changes accoding to scale.
///
/// And then you add the subtly of timezone offsets (and rolling back past midnight).
///
/// We're only test a tile width of 180px because that is only what is used in prod
/// at the moment.
/// </summary>
[DataTestMethod]
[DataRow("2017-11-08T13:33:33.123+10:00", 180, 60.0, "2017-11-08T12:00:00.000+10:00")]
[DataRow("2014-05-29T08:13:58.000+10:00", 180, 60.0, "2014-05-29T06:00:00.000+10:00")]
[DataRow("2014-05-29T08:13:58.000+10:00", 180, 01.0, "2014-05-29T08:12:00.000+10:00")]
[DataRow("2017-11-08T13:33:33.123+10:00", 180, 60.0, "2017-11-08T13:00:00.000+10:00")]
[DataRow("2014-05-29T08:13:58.000+10:00", 180, 60.0, "2014-05-29T07:00:00.000+10:00")]
[DataRow("2014-05-29T00:00:00.000+10:00", 180, 60.0, "2014-05-28T22:00:00.000+10:00")]

[DataRow("2014-05-29T08:13:58.000+10:00", 180, 1.0, "2014-05-29T08:12:00.000+10:00")]

[DataRow("2014-05-29T08:13:58.000+10:00", 180, 0.10, "2014-05-29T08:13:48.000+10:00")]

[DataRow("2012-10-19T00:00:00.000+10:00", 180, 1.6, "2012-10-18T14:00:00.000+00:00")]
[DataRow("2012-10-19T04:00:00.000+10:00", 180, 1.6, "2012-10-18T18:00:00.000+00:00")]
[DataRow("2012-10-19T08:00:00.000+10:00", 180, 1.6, "2012-10-18T22:00:00.000+00:00")]
[DataRow("2012-10-19T12:00:00.000+10:00", 180, 1.6, "2012-10-19T02:00:00.000+00:00")]
[DataRow("2012-10-19T16:00:00.000+10:00", 180, 1.6, "2012-10-19T06:00:00.000+00:00")]
[DataRow("2012-10-19T20:00:00.000+10:00", 180, 1.6, "2012-10-19T10:00:00.000+00:00")]

[DataRow("2012-10-19T10:00:00.000+10:00", 180, 1.6, "2012-10-19T00:00:00.000+00:00")]

[DataRow("2012-10-19T13:57:36.000+10:00", 180, 1.6, "2012-10-19T13:55:12.000+10:00")]
[DataRow("2012-10-19T13:59:59.000+10:00", 180, 1.6, "2012-10-19T13:55:12.000+10:00")]


[DataRow("2014-05-27T02:13:58.000+10:00", 180, 3.2, "2014-05-26T16:09:36.000+00:00")]
[DataRow("2014-05-27T02:09:58.000+10:00", 180, 3.2, "2014-05-26T16:09:36.000+00:00")]
[DataRow("2014-05-27T02:30:00.000+10:00", 180, 3.2, "2014-05-26T16:28:48.000+00:00")]
[DataRow("2014-05-27T03:45:00.000+10:00", 180, 3.2, "2014-05-26T17:36:00.000+00:00")]
[DataRow("2014-05-27T03:45:36.000+10:00", 180, 3.2, "2014-05-26T17:45:36.000+00:00")]
[DataRow("2014-05-27T03:45:37.000+10:00", 180, 3.2, "2014-05-26T17:45:36.000+00:00")]

[DataRow("2014-05-27T05:37:20.000+10:00", 180, 7.5, "2014-05-26T19:30:00.000+00:00")]
[DataRow("2014-05-27T12:00:00.000+10:00", 180, 7.5, "2014-05-27T01:52:30.000+00:00")]
[DataRow("2014-05-27T01:00:00.000+10:00", 180, 7.5, "2014-05-26T15:00:00.000+00:00")]

[DataRow("2012-10-19T14:00:00.000+10:00", 180, 240, "2012-10-19T10:00:00.000+10:00")]
[DataRow("2012-10-19T03:00:00.000+10:00", 180, 240, "2012-10-18T22:00:00.000+10:00")]
[DataRow("2012-10-19T04:00:00.000+00:00", 180, 240, "2012-10-19T00:00:00.000+00:00")]

[DataRow("2012-10-19T12:22:30.000+10:00", 180, 120, "2012-10-19T00:00:00.000+00:00")]
[DataRow("2012-10-19T17:01:00.000+10:00", 180, 120, "2012-10-19T06:00:00.000+00:00")]
[DataRow("2012-10-19T03:59:59.999+10:00", 180, 120, "2012-10-18T12:00:00.000+00:00")]
[DataRow("2012-10-19T07:00:00.000+10:00", 180, 120, "2012-10-18T18:00:00.000+00:00")]
public void TestGetPreviousTileBoundary(string startDate, int tileWidth, double scale, string expectedDate)
{
var start = DateTimeOffset.Parse(startDate);
Expand All @@ -26,6 +73,7 @@ public void TestGetPreviousTileBoundary(string startDate, int tileWidth, double

var expected = DateTimeOffset.Parse(expectedDate);
Assert.AreEqual(expected, actual);

}
}
}

0 comments on commit a5e9c39

Please sign in to comment.