Skip to content

Commit

Permalink
Fixed bugs in zooming spectrograms
Browse files Browse the repository at this point in the history
Work for #120

- First bug: scales that are not integer factors of the base scale were silently not compressed properly! 😠
  - Changed default scales so that they are integer factors
  - Added checks and errors in case the config is set using non-integer factors
  - Fixed associated tests
- Second bug: The correct amount of padding preceding tiles was not being included. This is needed to align tiles to absolute coordinates.
  - Added functionality to correctly align tiles to absolute tile positions
  - Wrote tests
- Also renamed `MinuteOffset` to `AnalysisStartOffset` to better reflect what the property means
  • Loading branch information
atruskie committed Nov 11, 2017
1 parent 37e747c commit 53e49b3
Show file tree
Hide file tree
Showing 18 changed files with 406 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,17 @@ public void TestGenerateTiles()
var filesProduced = zoomOutput.EnumerateFiles().ToArray();

// there are 11 zoom levels in the default config, but the test recording is only 2min long
// we're also split right at a natural boundary, so many tiles won't be full, but will be split
// at scale 240, we're rendering <1px of content and that tile is not generated
// for scales 120-1.0 only one tile is produced (subtotal: 10)
// for 0.5: 2 tiles (subtotal: 12)
// for 0.25: 3 tiles (subtotal: 15)
// for 0.125: 6 tiles (subtotal: 21)
Assert.AreEqual(21, filesProduced.Length);
Assert.AreEqual(6, filesProduced.Count(x => x.Name.Contains("0.125")));
// for scales 120-30 only one tile is produced (subtotal: 3)
// for 15, 7.5, 3.2, 1.6, 0.8: 2 tiles (subtotal: 13)
// for 0.4: 3 tiles (subtotal: 16)
// for 0.2: 4 tiles (subtotal: 20)
// for 0.1: 8 tiles (subtotal: 28)
Assert.AreEqual(28, filesProduced.Length);

// 6.66 tiles (1200px / 180px tiles) - with padding either side -> 8
Assert.AreEqual(8, filesProduced.Count(x => x.Name.Contains("0.1")));

// not sure what else to test - generally exceptions should be thrown if anything goes wrong
}
Expand Down Expand Up @@ -157,13 +161,17 @@ public void TestGenerateTilesSqlite()
var filesProduced = fs.EnumerateFiles(UPath.Root).ToArray();

// there are 11 zoom levels in the default config, but the test recording is only 2min long
// we're also split right at a natural boundary, so many tiles won't be full, but will be split
// at scale 240, we're rendering <1px of content and that tile is not generated
// for scales 120-1.0 only one tile is produced (subtotal: 10)
// for 0.5: 2 tiles (subtotal: 12)
// for 0.25: 3 tiles (subtotal: 15)
// for 0.125: 6 tiles (subtotal: 21)
Assert.AreEqual(21, filesProduced.Length);
Assert.AreEqual(6, filesProduced.Count(x => x.GetName().Contains("0.125")));
// for scales 120-30 only one tile is produced (subtotal: 3)
// for 15, 7.5, 3.2, 1.6, 0.8: 2 tiles (subtotal: 13)
// for 0.4: 3 tiles (subtotal: 16)
// for 0.2: 4 tiles (subtotal: 20)
// for 0.1: 8 tiles (subtotal: 28)
Assert.AreEqual(28, filesProduced.Length);

// 6.66 tiles (1200px / 180px tiles) - with padding either side -> 8
Assert.AreEqual(8, filesProduced.Count(x => x.GetName().Contains("0.1")));
}

// not sure what else to test - generally exceptions should be thrown if anything goes wrong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ZoomTiledSpectrogramTests
[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("2014-05-29T08:13:58.000+10:00", 180, 0.10, "2014-05-29T08:13:48.000+10:00")]
public void TestGetPreviousTileBoundary(string startDate, int tileWidth, double scale, string expectedDate)
{
var start = DateTimeOffset.Parse(startDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Acoustics.Test.AudioAnalysisTools.TileImage
using System.Drawing;
using System.IO;
using System.Linq;

using global::AudioAnalysisTools.LongDurationSpectrograms;
using global::AudioAnalysisTools.LongDurationSpectrograms.Zooming;
using global::AudioAnalysisTools.TileImage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;
Expand Down Expand Up @@ -66,6 +69,87 @@ public void TestNamingPattern()
Assert.AreEqual("Tag_20150409T173015.123Z_60", profile2.GetFileBaseName(this.tiler.CalculatedLayers, this.tiler.CalculatedLayers.First(), new Point(0, 0)));
}

[TestMethod]
public void TestLeftPaddingInLowerLayers()
{
const int TileWidth = 180;
var startDate = new DateTimeOffset(2014, 05, 29, 08,13, 58, TimeSpan.FromHours(10));
var boundary = ZoomTiledSpectrograms.GetPreviousTileBoundary(TileWidth, 0.1, startDate);
var padding = startDate - boundary;

var profile = new AbsoluteDateTilingProfile(
"Filename",
"Tile",
boundary,
256,
TileWidth);

var tiler = new Tiler(
this.outputDirectory.ToDirectoryEntry(),
profile,
new SortedSet<double>() { 60.0, 0.1 },
60.0,
1440,
new SortedSet<double>() { 1, 1 },
1.0,
256);

var testBitmap = new Bitmap(1200, 256);
using (var graphics = Graphics.FromImage(testBitmap))
{
var points = new[]
{
new Point(0, 0), new Point(180, 256), new Point(360, 0), new Point(540, 256), new Point(720, 0),
new Point(900, 256), new Point(1080, 0), new Point(1260, 256),
};
graphics.DrawLines(
Pens.Red,
points);
}

var superTile = new TimeOffsetSingleLayerSuperTile(
padding,
SpectrogramType.Index,
0.1.Seconds(),
testBitmap,
0.Seconds());

tiler.Tile(superTile);

////Debug.WriteLine(this.outputDirectory.FullName);
////Debug.WriteLine(this.outputDirectory.GetFiles().Length);
var actualFiles = this.outputDirectory.GetFiles().OrderBy(x => x.Name).ToArray();

Assert.AreEqual(8, actualFiles.Length);

var expectedFiles = new[]
{
"Filename__Tile_20140528T221348Z_0.1.png",
"Filename__Tile_20140528T221406Z_0.1.png",
"Filename__Tile_20140528T221424Z_0.1.png",
"Filename__Tile_20140528T221442Z_0.1.png",
"Filename__Tile_20140528T221500Z_0.1.png",
"Filename__Tile_20140528T221518Z_0.1.png",
"Filename__Tile_20140528T221536Z_0.1.png",
"Filename__Tile_20140528T221554Z_0.1.png",
};
var expectedImages =
expectedFiles
.OrderBy(x => x)
.Select((x, i) => testBitmap.Crop(new Rectangle((i * TileWidth) - 100, 0, TileWidth, 256)))
.ToArray();

for (var i = 0; i < expectedImages.Length; i++)
{
Assert.AreEqual(expectedFiles[i], actualFiles[i].Name);

var expectedImage = expectedImages[i];
var actualImage = Image.FromFile(actualFiles[i].FullName);
var areEqual = TilerTests.BitmapEquals((Bitmap)expectedImage, (Bitmap)actualImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedFiles[i], actualFiles[i]);
}
}

[TestMethod]
public void TestItShouldCutAndPadRightWithTransparency()
{
Expand Down
Loading

0 comments on commit 53e49b3

Please sign in to comment.