Skip to content
Snippets Groups Projects
Commit 962d7c0c authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Updated imagewrapper test to test empty coverages

parent f2259781
No related branches found
No related tags found
No related merge requests found
...@@ -74,6 +74,9 @@ def random_slices(coverage, shape, mode): ...@@ -74,6 +74,9 @@ def random_slices(coverage, shape, mode):
# Assuming that coverage is same for each volume # Assuming that coverage is same for each volume
lowCover = coverage[0, dim, 0] lowCover = coverage[0, dim, 0]
highCover = coverage[1, dim, 0] highCover = coverage[1, dim, 0]
if (np.isnan(lowCover) or np.isnan(highCover)) and mode in ('in', 'overlap'):
raise RuntimeError('Can\'t generate in/overlapping slices on an empty coverage')
# Generate some slices that will # Generate some slices that will
# be contained within the coverage # be contained within the coverage
...@@ -94,9 +97,15 @@ def random_slices(coverage, shape, mode): ...@@ -94,9 +97,15 @@ def random_slices(coverage, shape, mode):
elif mode == 'out': elif mode == 'out':
# No coverage, anything that
# we generate will be outside
if np.isnan(lowCover) or np.isnan(highCover):
lowSlice = np.random.randint(0, size)
highSlice = np.random.randint(lowSlice + 1, size + 1)
# The coverage is full, so we can't # The coverage is full, so we can't
# generate an outside range # generate an outside range
if lowCover == 0 and highCover == size: elif lowCover == 0 and highCover == size:
lowSlice = np.random.randint(lowCover, highCover) lowSlice = np.random.randint(lowCover, highCover)
highSlice = np.random.randint(lowSlice + 1, highCover + 1) highSlice = np.random.randint(lowSlice + 1, highCover + 1)
...@@ -165,11 +174,14 @@ def test_adjustCoverage(): ...@@ -165,11 +174,14 @@ def test_adjustCoverage():
# TODO Randomise # TODO Randomise
n = np.nan
# Each test is a tuple of (coverage, expansion, expectedResult) # Each test is a tuple of (coverage, expansion, expectedResult)
tests = [([[3, 5], [2, 6]], [[6, 7], [8, 10]], [[3, 7], [2, 10]]), tests = [([[3, 5], [2, 6]], [[6, 7], [8, 10]], [[3, 7], [2, 10]]),
([[0, 0], [0, 0]], [[1, 2], [3, 5]], [[0, 2], [0, 5]]), ([[0, 0], [0, 0]], [[1, 2], [3, 5]], [[0, 2], [0, 5]]),
([[2, 3], [0, 6]], [[1, 5], [4, 10]], [[1, 5], [0, 10]]), ([[2, 3], [0, 6]], [[1, 5], [4, 10]], [[1, 5], [0, 10]]),
([[0, 1], [0, 1]], [[0, 7], [19, 25], [0, 1]], [[0, 7], [0, 25]]), ([[0, 1], [0, 1]], [[0, 7], [19, 25], [0, 1]], [[0, 7], [0, 25]]),
([[n, n], [n, n]], [[0, 7], [19, 25], [0, 1]], [[0, 7], [19, 25]]),
] ]
for coverage, expansion, result in tests: for coverage, expansion, result in tests:
...@@ -183,7 +195,7 @@ def test_adjustCoverage(): ...@@ -183,7 +195,7 @@ def test_adjustCoverage():
def test_sliceCovered(): def test_sliceCovered():
# A bunch of random coverages # A bunch of random coverages
for i in range(500): for i in range(250):
# 2D, 3D or 4D? # 2D, 3D or 4D?
# ndims is the number of dimensions # ndims is the number of dimensions
...@@ -200,24 +212,23 @@ def test_sliceCovered(): ...@@ -200,24 +212,23 @@ def test_sliceCovered():
# Generate some slices that should # Generate some slices that should
# be contained within the coverage # be contained within the coverage
for j in range(500): for j in range(250):
slices = random_slices(coverage, shape, 'in') slices = random_slices(coverage, shape, 'in')
assert imagewrap.sliceCovered(slices, coverage, shape) assert imagewrap.sliceCovered(slices, coverage, shape)
# Generate some slices that should # Generate some slices that should
# overlap with the coverage # overlap with the coverage
for j in range(500): for j in range(250):
slices = random_slices(coverage, shape, 'overlap') slices = random_slices(coverage, shape, 'overlap')
assert not imagewrap.sliceCovered(slices, coverage, shape) assert not imagewrap.sliceCovered(slices, coverage, shape)
# Generate some slices that should # Generate some slices that should
# be outside of the coverage # be outside of the coverage
for j in range(500): for j in range(250):
slices = random_slices(coverage, shape, 'out') slices = random_slices(coverage, shape, 'out')
assert not imagewrap.sliceCovered(slices, coverage, shape) assert not imagewrap.sliceCovered(slices, coverage, shape)
# The sum of the coverage ranges + the # The sum of the coverage ranges + the
# expansion ranges should be equal to # expansion ranges should be equal to
# the coverage, expanded to include the # the coverage, expanded to include the
...@@ -232,66 +243,93 @@ def _test_expansion(coverage, slices, volumes, expansions): ...@@ -232,66 +243,93 @@ def _test_expansion(coverage, slices, volumes, expansions):
print print
print 'Slice: "{}"'.format(" ".join(["{:2d} {:2d}".format(l, h) for l, h in slices])) print 'Slice: "{}"'.format(" ".join(["{:2d} {:2d}".format(l, h) for l, h in slices]))
# Figure out what the adjusted
# coverage should look like (assumes
# that adjustCoverage is working, and
# the coverage is the same on all
# volumes)
oldCoverage = coverage[..., 0]
newCoverage = imagewrap.adjustCoverage(oldCoverage, slices)
nc = newCoverage
# We're goint to test that every point
# in the expected (expanded) coverage
# is contained either in the original
# coverage, or in one of the expansions.
dimranges = []
for d in range(ndims):
dimranges.append(np.arange(nc[0, d], nc[1, d]))
points = it.product(*dimranges)
# Bin the expansions by volume # Bin the expansions by volume
expsByVol = collections.defaultdict(list) expsByVol = collections.defaultdict(list)
for vol, exp in zip(volumes, expansions): for vol, exp in zip(volumes, expansions):
print ' {:3d}: "{}"'.format(vol, " ".join(["{:2d} {:2d}".format(l, h) for l, h in exp])) print ' {:3d}: "{}"'.format(vol, " ".join(["{:2d} {:2d}".format(l, h) for l, h in exp]))
expsByVol[vol].append(exp) expsByVol[vol].append(exp)
for vol, exps in expsByVol.items(): for point in points:
# Figure out what the adjusted # Is this point in the old coverage?
# coverage should look like (assumes covered = True
# that adjustCoverage is working). for dim in range(ndims):
oldCoverage = coverage[..., vol] covLow, covHigh = oldCoverage[:, dim]
newCoverage = imagewrap.adjustCoverage(oldCoverage, slices)
if np.isnan(covLow) or \
nc = newCoverage np.isnan(covHigh) or \
point[dim] < covLow or \
dimranges = [] point[dim] > covHigh:
for d in range(ndims): covered = False
dimranges.append(np.arange(nc[0, d], nc[1, d]))
points = it.product(*dimranges)
for point in points:
# Is this point in the old coverage?
covered = True
for dim in range(ndims):
covLow, covHigh = oldCoverage[:, dim]
if point[dim] < covLow or point[dim] > covHigh:
covered = False
break
if covered:
break break
if covered:
continue
for vol, exps in expsByVol.items():
# Is this point in any of the expansions # Is this point in any of the expansions
covered = [False] * len(exps) coveredInExp = [False] * len(exps)
for i, exp in enumerate(exps): for i, exp in enumerate(exps):
covered[i] = True coveredInExp[i] = True
for dim in range(ndims): for dim in range(ndims):
expLow, expHigh = exp[dim] expLow, expHigh = exp[dim]
if point[dim] < expLow or point[dim] > expHigh: if point[dim] < expLow or point[dim] > expHigh:
covered[i] = False coveredInExp[i] = False
break break
if not any(covered): if not (covered or any(coveredInExp)):
raise AssertionError(point) raise AssertionError(point)
def test_calcSliceExpansion(): def test_calcExpansionNoCoverage():
for i in range(500): for i in range(500):
ndims = random.choice((2, 3, 4)) - 1
shape = np.random.randint(5, 100, size=ndims + 1)
shape[-1] = np.random.randint(1, 8)
coverage = np.zeros((2, ndims, shape[-1]))
coverage[:] = np.nan
ndims = 3 # random.choice((2, 3, 4)) - 1 print
shape = np.random.randint(5, 100, size=ndims + 1) print '-- Out --'
coverage = random_coverage(shape) for j in range(250):
slices = random_slices(coverage, shape, 'out')
vols, exps = imagewrap.calcExpansion(slices, coverage)
_test_expansion(coverage, slices, vols, exps)
def test_calcExpansion():
for i in range(250):
ndims = random.choice((2, 3, 4)) - 1
shape = np.random.randint(5, 60, size=ndims + 1)
shape[-1] = np.random.randint(1, 6)
coverage = random_coverage(shape)
cov = [(lo, hi) for lo, hi in coverage[:, :, 0].T] cov = [(lo, hi) for lo, hi in coverage[:, :, 0].T]
...@@ -303,7 +341,10 @@ def test_calcSliceExpansion(): ...@@ -303,7 +341,10 @@ def test_calcSliceExpansion():
for j in range(250): for j in range(250):
slices = random_slices(coverage, shape, 'in') slices = random_slices(coverage, shape, 'in')
vols, exps = imagewrap.calcExpansion(slices, coverage) vols, exps = imagewrap.calcExpansion(slices, coverage)
_test_expansion(coverage, slices, vols, exps)
# There should be no expansions for a
# slice that's inside the coverage
assert len(vols) == 0 and len(exps) == 0
print print
print '-- Overlap --' print '-- Overlap --'
...@@ -318,3 +359,4 @@ def test_calcSliceExpansion(): ...@@ -318,3 +359,4 @@ def test_calcSliceExpansion():
slices = random_slices(coverage, shape, 'out') slices = random_slices(coverage, shape, 'out')
vols, exps = imagewrap.calcExpansion(slices, coverage) vols, exps = imagewrap.calcExpansion(slices, coverage)
_test_expansion(coverage, slices, vols, exps) _test_expansion(coverage, slices, vols, exps)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment