Skip to content

Commit

Permalink
Unify tests using pytest. Remove unittest framework.
Browse files Browse the repository at this point in the history
Simplifies tests, no longer depending on custom temporary
file and directory implementations, instead just use the tmpdir
fixture from pytest.
Allows us to apply fixtures to all tests (startmethod "spawn" vs "fork") to make sure gunpowder works as expected.
Makes it less confusing for new users writing tests since existing tests are now consistent.

Squashed commit of the following:

commit 787e3c5
Author: William Patton <[email protected]>
Date:   Wed Apr 24 08:45:54 2024 -0700

    black formatting disagrees with ruff here

commit fe3202e
Author: William Patton <[email protected]>
Date:   Wed Apr 24 08:42:26 2024 -0700

    ruff fix imports

commit 70fb819
Author: William Patton <[email protected]>
Date:   Wed Apr 24 08:27:54 2024 -0700

    formatting tests

commit e1910b7
Author: William Patton <[email protected]>
Date:   Mon Apr 22 14:59:06 2024 -0700

    remove unittest base class

commit 09eadaa
Author: William Patton <[email protected]>
Date:   Mon Apr 22 14:58:17 2024 -0700

    add autouse fixture to test both fork and spawn start methods

commit 3298143
Author: William Patton <[email protected]>
Date:   Mon Apr 22 14:54:04 2024 -0700

    `shift_augment.py`

    convert to pytest

commit b6d266d
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:44:17 2024 -0700

    `specified_location.py`

    convert to pytest

commit 48a8e42
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:44:05 2024 -0700

    `snapshot.py`

    convert to pytest

commit 7368db7
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:43:56 2024 -0700

    `merge_provider.py`

    convert to pytest

commit 6cfee4e
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:43:45 2024 -0700

    `graph_keys.py`

    convert to pytest

commit 5d7231d
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:33:53 2024 -0700

    `unsqueeze.py`

    convert to pytest

commit fe3435c
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:31:57 2024 -0700

    `tensorflow_train.py`

    convert to pytest

commit c2b12b1
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:21:39 2024 -0700

    `squeeze.py`

    convert to pytest

commit ed7ccd9
Author: William Patton <[email protected]>
Date:   Mon Apr 22 12:19:29 2024 -0700

    `random_location.py`

    convert to pytest
    also slightly revise test. `test_output` test now passes, bug must have been fixed.
    `test_centered` test was broken but not noticed since it is an expected fail anyway.

commit eda2c5b
Author: William Patton <[email protected]>
Date:   Sun Apr 21 14:37:44 2024 -0700

    `random_location_graph.py`

    convert to pytest

commit 62903f7
Author: William Patton <[email protected]>
Date:   Sun Apr 21 14:33:18 2024 -0700

    `profiling.py`

    convert to pytest

commit d9fa603
Author: William Patton <[email protected]>
Date:   Sun Apr 21 14:28:01 2024 -0700

    `prepare_malis.py`

    convert to pytest

commit 19e4882
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:33:43 2024 -0700

    `normalize.py`

    convert to pytest

commit 7c69044
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:33:31 2024 -0700

    `placeholder_requests.py`

    convert to pytest

commit 42c8b7c
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:26:06 2024 -0700

    `noise_augment.py`

    convert to pytest

commit f079f62
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:22:50 2024 -0700

    `node_dependencies.py`

    convert to pytest

commit d0c97e9
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:19:48 2024 -0700

    `jax_train.py`

    convert to pytest

commit 5858b27
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:06:08 2024 -0700

    `iterate_locations.py`

    convert to pytest

commit c31b5b2
Author: William Patton <[email protected]>
Date:   Sat Apr 20 10:02:31 2024 -0700

    `hdf5_write.py`

    convert to pytest

commit ad48ad6
Author: William Patton <[email protected]>
Date:   Sat Apr 20 09:58:36 2024 -0700

    `hdf5_source.py`

    convert to pytest

commit 96526cc
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:37:18 2024 -0700

    `intensity_augment.py`

    convert to pytest

commit 64c342f
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:31:30 2024 -0700

    `graph_source.py`

    convert to pytest

commit 0f9cc2c
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:27:20 2024 -0700

    `graph.py`

    convert to pytest

commit daeaa7a
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:16:59 2024 -0700

    `elastic_augment.py`

    convert to pytest

commit 85f4f24
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:14:13 2024 -0700

    `elastic_augment_points.py`

    convert to pytest

commit 2fd6e2e
Author: William Patton <[email protected]>
Date:   Fri Apr 19 21:03:34 2024 -0700

    `dvid_source.py`

    convert to pytest

commit 12d3b9f
Author: William Patton <[email protected]>
Date:   Fri Apr 19 20:59:22 2024 -0700

    `crop.py`

    convert to pytest and simplify

commit 5c11bf0
Author: William Patton <[email protected]>
Date:   Fri Apr 19 20:59:10 2024 -0700

    `balance_labels.py`

    convert to pytest and simplify

commit c9df12f
Author: William Patton <[email protected]>
Date:   Fri Apr 19 20:43:16 2024 -0700

    `astype.py`

    convert to pytest and simplify test

commit 440c88a
Author: William Patton <[email protected]>
Date:   Fri Apr 19 18:40:24 2024 -0700

    Add TODO to simplify this test

commit 3e394f9
Author: William Patton <[email protected]>
Date:   Fri Apr 19 18:38:30 2024 -0700

    `add_vector_map.py`

    convert test from unittest to pytest

commit 2c33630
Author: William Patton <[email protected]>
Date:   Fri Apr 19 18:29:18 2024 -0700

    `add_boundary_distance_gradients.py`

    simplify and convert to pytest
  • Loading branch information
pattonw committed Apr 24, 2024
1 parent 4792aad commit fa5d9f3
Show file tree
Hide file tree
Showing 52 changed files with 3,306 additions and 3,429 deletions.
16 changes: 15 additions & 1 deletion tests/cases/add_affinities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
from gunpowder import *
from itertools import product

import numpy as np

from gunpowder import (
AddAffinities,
Array,
ArrayKey,
ArrayKeys,
ArraySpec,
Batch,
BatchProvider,
BatchRequest,
Coordinate,
Roi,
build,
)


class ExampleSource(BatchProvider):
def setup(self):
Expand Down
108 changes: 39 additions & 69 deletions tests/cases/add_boundary_distance_gradients.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,54 @@
from .provider_test import ProviderTest
from gunpowder import *
from gunpowder.contrib import AddBoundaryDistanceGradients
import numpy as np

from gunpowder import Array, ArrayKey, ArraySpec, BatchRequest, Roi, build
from gunpowder.contrib import AddBoundaryDistanceGradients

class ExampleSource(BatchProvider):
def setup(self):
self.provides(
ArrayKeys.GT_LABELS,
ArraySpec(
roi=Roi((-40, -40, -40), (160, 160, 160)),
voxel_size=(20, 4, 8),
interpolatable=False,
),
)

def provide(self, request):
batch = Batch()

roi = request[ArrayKeys.GT_LABELS].roi
shape = (roi / self.spec[ArrayKeys.GT_LABELS].voxel_size).shape

spec = self.spec[ArrayKeys.GT_LABELS].copy()
spec.roi = roi
data = np.ones(shape)
data[shape[0] // 2 :, :, :] += 2
data[:, shape[1] // 2 :, :] += 4
data[:, :, shape[2] // 2 :] += 8
batch.arrays[ArrayKeys.GT_LABELS] = Array(data, spec)
from .helper_sources import ArraySource

return batch

def test_output():
labels_key = ArrayKey("LABELS")
dist_key = ArrayKey("BOUNDARY_DISTANCES")
grad_key = ArrayKey("BOUNDARY_GRADIENTS")

class TestAddBoundaryDistanceGradients(ProviderTest):
def test_output(self):
ArrayKey("GT_BOUNDARY_DISTANCES")
ArrayKey("GT_BOUNDARY_GRADIENTS")
labels_spec = ArraySpec(
roi=Roi((0, 0, 0), (120, 16, 64)),
voxel_size=(20, 4, 8),
interpolatable=False,
)
shape = (labels_spec.roi / labels_spec.voxel_size).shape
labels_data = np.ones(shape)
labels_data[shape[0] // 2 :, :, :] += 2
labels_data[:, shape[1] // 2 :, :] += 4
labels_data[:, :, shape[2] // 2 :] += 8
labels_array = Array(labels_data, labels_spec)

pipeline = ExampleSource() + AddBoundaryDistanceGradients(
label_array_key=ArrayKeys.GT_LABELS,
distance_array_key=ArrayKeys.GT_BOUNDARY_DISTANCES,
gradient_array_key=ArrayKeys.GT_BOUNDARY_GRADIENTS,
)
labels_source = ArraySource(labels_key, labels_array)

with build(pipeline):
request = BatchRequest()
request.add(ArrayKeys.GT_LABELS, (120, 16, 64))
request.add(ArrayKeys.GT_BOUNDARY_DISTANCES, (120, 16, 64))
request.add(ArrayKeys.GT_BOUNDARY_GRADIENTS, (120, 16, 64))
pipeline = labels_source + AddBoundaryDistanceGradients(
label_array_key=labels_key,
distance_array_key=dist_key,
gradient_array_key=grad_key,
)

batch = pipeline.request_batch(request)
with build(pipeline):
request = BatchRequest()
request.add(labels_key, (120, 16, 64))
request.add(dist_key, (120, 16, 64))
request.add(grad_key, (120, 16, 64))

labels = batch.arrays[ArrayKeys.GT_LABELS].data
distances = batch.arrays[ArrayKeys.GT_BOUNDARY_DISTANCES].data
gradients = batch.arrays[ArrayKeys.GT_BOUNDARY_GRADIENTS].data
shape = distances.shape
batch = pipeline.request_batch(request)

l_001 = labels[: shape[0] // 2, : shape[1] // 2, shape[2] // 2 :]
l_101 = labels[shape[0] // 2 :, : shape[1] // 2, shape[2] // 2 :]
d_001 = distances[: shape[0] // 2, : shape[1] // 2, shape[2] // 2 :]
d_101 = distances[shape[0] // 2 :, : shape[1] // 2, shape[2] // 2 :]
g_001 = gradients[:, : shape[0] // 2, : shape[1] // 2, shape[2] // 2 :]
g_101 = gradients[:, shape[0] // 2 :, : shape[1] // 2, shape[2] // 2 :]
distances = batch.arrays[dist_key].data
gradients = batch.arrays[grad_key].data
shape = distances.shape

# print labels
# print
# print distances
# print
# print l_001
# print l_101
# print
# print d_001
# print d_101
# print
# print g_001
# print g_101
g_001 = gradients[:, : shape[0] // 2, : shape[1] // 2, shape[2] // 2 :]
g_101 = gradients[:, shape[0] // 2 :, : shape[1] // 2, shape[2] // 2 :]

self.assertTrue((g_001 == g_101).all())
assert (g_001 == g_101).all()

top = gradients[:, 0 : shape[0] // 2, :]
bot = gradients[:, shape[0] : shape[0] // 2 - 1 : -1, :]
top = gradients[:, 0 : shape[0] // 2, :]
bot = gradients[:, shape[0] : shape[0] // 2 - 1 : -1, :]

self.assertTrue((top == bot).all())
assert (top == bot).all()
Loading

0 comments on commit fa5d9f3

Please sign in to comment.