diff --git a/test/test_imagewrapper.py b/test/test_imagewrapper.py index 4c38ff2978c7a1006fdd12d2ba86b0c32fa6e5c6..f235e006cad346298c6f43d537700b40d621db27 100644 --- a/test/test_imagewrapper.py +++ b/test/test_imagewrapper.py @@ -74,6 +74,9 @@ def random_slices(coverage, shape, mode): # Assuming that coverage is same for each volume lowCover = coverage[0, 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 # be contained within the coverage @@ -94,9 +97,15 @@ def random_slices(coverage, shape, mode): 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 - # generate an outside range - if lowCover == 0 and highCover == size: + # generate an outside range + elif lowCover == 0 and highCover == size: lowSlice = np.random.randint(lowCover, highCover) highSlice = np.random.randint(lowSlice + 1, highCover + 1) @@ -165,11 +174,14 @@ def test_adjustCoverage(): # TODO Randomise + n = np.nan + # Each test is a tuple of (coverage, expansion, expectedResult) - 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]]), - ([[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]]), + 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]]), + ([[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]]), + ([[n, n], [n, n]], [[0, 7], [19, 25], [0, 1]], [[0, 7], [19, 25]]), ] for coverage, expansion, result in tests: @@ -183,7 +195,7 @@ def test_adjustCoverage(): def test_sliceCovered(): # A bunch of random coverages - for i in range(500): + for i in range(250): # 2D, 3D or 4D? # ndims is the number of dimensions @@ -200,24 +212,23 @@ def test_sliceCovered(): # Generate some slices that should # be contained within the coverage - for j in range(500): + for j in range(250): slices = random_slices(coverage, shape, 'in') assert imagewrap.sliceCovered(slices, coverage, shape) # Generate some slices that should # overlap with the coverage - for j in range(500): + for j in range(250): slices = random_slices(coverage, shape, 'overlap') assert not imagewrap.sliceCovered(slices, coverage, shape) # Generate some slices that should # be outside of the coverage - for j in range(500): + for j in range(250): slices = random_slices(coverage, shape, 'out') assert not imagewrap.sliceCovered(slices, coverage, shape) - # The sum of the coverage ranges + the # expansion ranges should be equal to # the coverage, expanded to include the @@ -232,66 +243,93 @@ def _test_expansion(coverage, slices, volumes, expansions): print 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 expsByVol = collections.defaultdict(list) for vol, exp in zip(volumes, expansions): print ' {:3d}: "{}"'.format(vol, " ".join(["{:2d} {:2d}".format(l, h) for l, h in exp])) expsByVol[vol].append(exp) - - for vol, exps in expsByVol.items(): - - # Figure out what the adjusted - # coverage should look like (assumes - # that adjustCoverage is working). - oldCoverage = coverage[..., vol] - newCoverage = imagewrap.adjustCoverage(oldCoverage, slices) - - nc = newCoverage - - dimranges = [] - for d in range(ndims): - 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: + + for point in points: + + # Is this point in the old coverage? + covered = True + for dim in range(ndims): + covLow, covHigh = oldCoverage[:, dim] + + if np.isnan(covLow) or \ + np.isnan(covHigh) or \ + point[dim] < covLow or \ + point[dim] > covHigh: + covered = False break + if covered: + continue + + for vol, exps in expsByVol.items(): + # Is this point in any of the expansions - covered = [False] * len(exps) + coveredInExp = [False] * len(exps) for i, exp in enumerate(exps): - covered[i] = True + coveredInExp[i] = True for dim in range(ndims): expLow, expHigh = exp[dim] if point[dim] < expLow or point[dim] > expHigh: - covered[i] = False + coveredInExp[i] = False break - if not any(covered): - raise AssertionError(point) + if not (covered or any(coveredInExp)): + raise AssertionError(point) - -def test_calcSliceExpansion(): + +def test_calcExpansionNoCoverage(): 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 - shape = np.random.randint(5, 100, size=ndims + 1) - coverage = random_coverage(shape) + print + print '-- Out --' + 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] @@ -303,7 +341,10 @@ def test_calcSliceExpansion(): for j in range(250): slices = random_slices(coverage, shape, 'in') 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 '-- Overlap --' @@ -318,3 +359,4 @@ def test_calcSliceExpansion(): slices = random_slices(coverage, shape, 'out') vols, exps = imagewrap.calcExpansion(slices, coverage) _test_expansion(coverage, slices, vols, exps) +