Skip to content

Commit

Permalink
format tests with black
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Apr 18, 2023
1 parent 577f52c commit c033463
Show file tree
Hide file tree
Showing 47 changed files with 801 additions and 961 deletions.
8 changes: 1 addition & 7 deletions tests/cases/add_affinities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class ExampleSource(BatchProvider):
def setup(self):

self.provides(
ArrayKeys.GT_LABELS,
ArraySpec(
Expand All @@ -28,7 +27,6 @@ def setup(self):
)

def provide(self, request):

batch = Batch()

roi = request[ArrayKeys.GT_LABELS].roi
Expand Down Expand Up @@ -70,9 +68,7 @@ def test_output():
)

with build(pipeline):

for i in range(10):

request = BatchRequest()
request.add(labels_key, (100, 16, 64))
request.add(mask_key, (100, 16, 64))
Expand All @@ -94,12 +90,10 @@ def test_output():
assert (len(neighborhood),) + labels.data.shape == affs.data.shape

voxel_roi = Roi((0, 0, 0), labels.data.shape)
for (z, y, x) in product(*[range(d) for d in labels.data.shape]):

for z, y, x in product(*[range(d) for d in labels.data.shape]):
p = Coordinate((z, y, x))

for n in range(len(neighborhood)):

pn = p + neighborhood[n]
if not voxel_roi.contains(pn):
continue
Expand Down
59 changes: 28 additions & 31 deletions tests/cases/add_boundary_distance_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,51 @@
from gunpowder.contrib import AddBoundaryDistanceGradients
import numpy as np

class ExampleSource(BatchProvider):

class ExampleSource(BatchProvider):
def setup(self):

self.provides(
ArrayKeys.GT_LABELS, ArraySpec(
ArrayKeys.GT_LABELS,
ArraySpec(
roi=Roi((-40, -40, -40), (160, 160, 160)),
voxel_size=(20, 4, 8),
interpolatable=False))
interpolatable=False,
),
)

def provide(self, request):

batch = Batch()

roi = request[ArrayKeys.GT_LABELS].roi
shape = (roi/self.spec[ArrayKeys.GT_LABELS].voxel_size).shape
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
data[shape[0] // 2 :, :, :] += 2
data[:, shape[1] // 2 :, :] += 4
data[:, :, shape[2] // 2 :] += 8
batch.arrays[ArrayKeys.GT_LABELS] = Array(data, spec)

return batch

class TestAddBoundaryDistanceGradients(ProviderTest):

class TestAddBoundaryDistanceGradients(ProviderTest):
def test_output(self):
ArrayKey("GT_BOUNDARY_DISTANCES")
ArrayKey("GT_BOUNDARY_GRADIENTS")

ArrayKey('GT_BOUNDARY_DISTANCES')
ArrayKey('GT_BOUNDARY_GRADIENTS')

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

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))
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))

batch = pipeline.request_batch(request)

Expand All @@ -59,12 +56,12 @@ def test_output(self):
gradients = batch.arrays[ArrayKeys.GT_BOUNDARY_GRADIENTS].data
shape = distances.shape

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:]
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 :]

# print labels
# print
Expand All @@ -81,7 +78,7 @@ def test_output(self):

self.assertTrue((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())
36 changes: 7 additions & 29 deletions tests/cases/add_vector_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

class AddVectorMapTestSource(BatchProvider):
def setup(self):

for identifier in [ArrayKeys.RAW, ArrayKeys.GT_LABELS]:

self.provides(
identifier,
ArraySpec(
Expand All @@ -37,18 +35,15 @@ def setup(self):
)

for identifier in [GraphKeys.PRESYN, GraphKeys.POSTSYN]:

self.provides(
identifier, GraphSpec(roi=Roi((1000, 1000, 1000), (400, 400, 400)))
)

def provide(self, request):

batch = Batch()

# have the pixels encode their position
if ArrayKeys.RAW in request:

# the z,y,x coordinates of the ROI
roi = request[ArrayKeys.RAW].roi
roi_voxel = roi // self.spec[ArrayKeys.RAW].voxel_size
Expand All @@ -66,9 +61,7 @@ def provide(self, request):

if ArrayKeys.GT_LABELS in request:
roi = request[ArrayKeys.GT_LABELS].roi
roi_voxel_shape = (
roi // self.spec[ArrayKeys.GT_LABELS].voxel_size
).shape
roi_voxel_shape = (roi // self.spec[ArrayKeys.GT_LABELS].voxel_size).shape
data = np.ones(roi_voxel_shape)
data[roi_voxel_shape[0] // 2 :, roi_voxel_shape[1] // 2 :, :] = 2
data[roi_voxel_shape[0] // 2 :, -(roi_voxel_shape[1] // 2) :, :] = 3
Expand All @@ -86,7 +79,7 @@ def provide(self, request):
)

voxel_size_points = self.spec[ArrayKeys.RAW].voxel_size
for (graph_key, spec) in request.graph_specs.items():
for graph_key, spec in request.graph_specs.items():
if graph_key == GraphKeys.PRESYN:
data = data_presyn
if graph_key == GraphKeys.POSTSYN:
Expand All @@ -98,7 +91,6 @@ def provide(self, request):
return batch

def __get_pre_and_postsyn_locations(self, roi):

presyn_locs, postsyn_locs = {}, {}
min_dist_between_presyn_locs = 250
voxel_size_points = self.spec[ArrayKeys.RAW].voxel_size
Expand All @@ -120,15 +112,9 @@ def __get_pre_and_postsyn_locations(self, roi):
while presyn_loc_too_close:
presyn_location = np.asarray(
[
np.random.randint(
low=roi.begin[0], high=roi.end[0]
),
np.random.randint(
low=roi.begin[1], high=roi.end[1]
),
np.random.randint(
low=roi.begin[2], high=roi.end[2]
),
np.random.randint(low=roi.begin[0], high=roi.end[0]),
np.random.randint(low=roi.begin[1], high=roi.end[1]),
np.random.randint(low=roi.begin[2], high=roi.end[2]),
]
)
# ensure that partner locations of diff presyn locations are not overlapping
Expand Down Expand Up @@ -186,7 +172,6 @@ def __get_pre_and_postsyn_locations(self, roi):

class TestAddVectorMap(ProviderTest):
def test_output_min_distance(self):

voxel_size = Coordinate((20, 2, 2))

ArrayKey("GT_VECTORS_MAP_PRESYN")
Expand All @@ -212,7 +197,6 @@ def test_output_min_distance(self):
)

with build(pipeline_min_distance):

request = BatchRequest()
raw_roi = pipeline_min_distance.spec[ArrayKeys.RAW].roi
gt_labels_roi = pipeline_min_distance.spec[ArrayKeys.GT_LABELS].roi
Expand All @@ -231,15 +215,12 @@ def test_output_min_distance(self):
presyn_locs = {n.id: n for n in batch.graphs[GraphKeys.PRESYN].nodes}
postsyn_locs = {n.id: n for n in batch.graphs[GraphKeys.POSTSYN].nodes}
vector_map_presyn = batch.arrays[ArrayKeys.GT_VECTORS_MAP_PRESYN].data
offset_vector_map_presyn = request[
ArrayKeys.GT_VECTORS_MAP_PRESYN
].roi.offset
offset_vector_map_presyn = request[ArrayKeys.GT_VECTORS_MAP_PRESYN].roi.offset

self.assertTrue(len(presyn_locs) > 0)
self.assertTrue(len(postsyn_locs) > 0)

for loc_id, point in presyn_locs.items():

if request[ArrayKeys.GT_VECTORS_MAP_PRESYN].roi.contains(
Coordinate(point.location)
):
Expand Down Expand Up @@ -314,15 +295,12 @@ def test_output_min_distance(self):
presyn_locs = {n.id: n for n in batch.graphs[GraphKeys.PRESYN].nodes}
postsyn_locs = {n.id: n for n in batch.graphs[GraphKeys.POSTSYN].nodes}
vector_map_presyn = batch.arrays[ArrayKeys.GT_VECTORS_MAP_PRESYN].data
offset_vector_map_presyn = request[
ArrayKeys.GT_VECTORS_MAP_PRESYN
].roi.offset
offset_vector_map_presyn = request[ArrayKeys.GT_VECTORS_MAP_PRESYN].roi.offset

self.assertTrue(len(presyn_locs) > 0)
self.assertTrue(len(postsyn_locs) > 0)

for loc_id, point in presyn_locs.items():

if request[ArrayKeys.GT_VECTORS_MAP_PRESYN].roi.contains(
Coordinate(point.location)
):
Expand Down
Loading

0 comments on commit c033463

Please sign in to comment.