Skip to content

Commit

Permalink
Type check + Lint in CI (#470)
Browse files Browse the repository at this point in the history
* Try lint and typecheck in CI workflow

* update

* nit

* continue on MyPy errors

* test

* correct

* correct

* correct

Co-authored-by: Arie Matsliah <[email protected]>
  • Loading branch information
arie-matsliah and arie-matsliah authored Feb 4, 2021
1 parent 5f95150 commit 56660c0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 49 deletions.
34 changes: 33 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,39 @@ on:
- 'tests/**'

jobs:
build:
type_check:
name: Type Check
runs-on: "ubuntu-18.04"
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.6
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Install Dependencies
run: |
pip install mypy
- name: Run MyPy
# TODO: remove this once all MyPy errors get fixed
continue-on-error: true
run: |
mypy --follow-imports=skip --ignore-missing-imports sleap tests
lint:
name: Lint
runs-on: "ubuntu-18.04"
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.6
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Install Dependencies
run: |
pip install black
- name: Run Black
run: |
black --check sleap tests
tests:
name: Tests (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
Expand Down
3 changes: 2 additions & 1 deletion sleap/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ def _node_to_index(self, node: Union[str, Node]) -> int:
return self.skeleton.node_to_index(node)

def __getitem__(
self, node: Union[List[Union[str, Node, int]], Union[str, Node, int], np.ndarray]
self,
node: Union[List[Union[str, Node, int]], Union[str, Node, int], np.ndarray],
) -> Union[List[Point], Point, np.ndarray]:
"""
Get the Points associated with particular skeleton node(s).
Expand Down
4 changes: 3 additions & 1 deletion sleap/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ def symmetries(self) -> List[Tuple[Node, Node]]:
if edge_type == EdgeType.SYMMETRY
]
# Get rid of duplicates
symmetries = list(set([tuple(sorted(e, key=operator.attrgetter("name"))) for e in symmetries]))
symmetries = list(
set([tuple(sorted(e, key=operator.attrgetter("name"))) for e in symmetries])
)
return symmetries

@property
Expand Down
99 changes: 56 additions & 43 deletions tests/nn/data/test_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,49 +52,44 @@ def test_random_cropper(min_labels):
assert "crop_bbox" in example
offset = tf.stack([example["crop_bbox"][0, 1], example["crop_bbox"][0, 0]], axis=-1)
assert tf.reduce_all(
example["instances"] == (
example_preaug["instances"] - tf.expand_dims(offset, axis=0)))
example["instances"]
== (example_preaug["instances"] - tf.expand_dims(offset, axis=0))
)


def test_flip_instances_lr():
insts = tf.cast([
[[0, 1], [2, 3]],
[[4, 5], [6, 7]],
], tf.float32)
insts = tf.cast(
[
[[0, 1], [2, 3]],
[[4, 5], [6, 7]],
],
tf.float32,
)

insts_flip = augmentation.flip_instances_lr(insts, 8)
np.testing.assert_array_equal(insts_flip, [
[[7, 1], [5, 3]],
[[3, 5], [1, 7]]
])
np.testing.assert_array_equal(insts_flip, [[[7, 1], [5, 3]], [[3, 5], [1, 7]]])

insts_flip1 = augmentation.flip_instances_lr(insts, 8, [[0, 1]])
insts_flip2 = augmentation.flip_instances_lr(insts, 8, [[1, 0]])
np.testing.assert_array_equal(insts_flip1, [
[[5, 3], [7, 1]],
[[1, 7], [3, 5]]
])
np.testing.assert_array_equal(insts_flip1, [[[5, 3], [7, 1]], [[1, 7], [3, 5]]])
np.testing.assert_array_equal(insts_flip1, insts_flip2)


def test_flip_instances_ud():
insts = tf.cast([
[[0, 1], [2, 3]],
[[4, 5], [6, 7]],
], tf.float32)
insts = tf.cast(
[
[[0, 1], [2, 3]],
[[4, 5], [6, 7]],
],
tf.float32,
)

insts_flip = augmentation.flip_instances_ud(insts, 8)
np.testing.assert_array_equal(insts_flip, [
[[0, 6], [2, 4]],
[[4, 2], [6, 0]]
])
np.testing.assert_array_equal(insts_flip, [[[0, 6], [2, 4]], [[4, 2], [6, 0]]])

insts_flip1 = augmentation.flip_instances_ud(insts, 8, [[0, 1]])
insts_flip2 = augmentation.flip_instances_ud(insts, 8, [[1, 0]])
np.testing.assert_array_equal(insts_flip1, [
[[2, 4], [0, 6]],
[[6, 0], [4, 2]]
])
np.testing.assert_array_equal(insts_flip1, [[[2, 4], [0, 6]], [[6, 0], [4, 2]]])
np.testing.assert_array_equal(insts_flip1, insts_flip2)


Expand All @@ -103,53 +98,71 @@ def test_random_flipper():
"tests/data/json_format_v1/centered_pair_low_quality.mp4"
)
skel = sleap.Skeleton.from_names_and_edge_inds(["A", "BL", "BR"], [[0, 1], [0, 2]])
labels = sleap.Labels([sleap.LabeledFrame(video=vid, frame_idx=0, instances=[
sleap.Instance.from_pointsarray([[25, 50], [50, 25], [25, 25]], skeleton=skel),
sleap.Instance.from_pointsarray([[125, 150], [150, 125], [125, 125]], skeleton=skel),
])])

labels = sleap.Labels(
[
sleap.LabeledFrame(
video=vid,
frame_idx=0,
instances=[
sleap.Instance.from_pointsarray(
[[25, 50], [50, 25], [25, 25]], skeleton=skel
),
sleap.Instance.from_pointsarray(
[[125, 150], [150, 125], [125, 125]], skeleton=skel
),
],
)
]
)

p = labels.to_pipeline()
p += sleap.nn.data.augmentation.RandomFlipper.from_skeleton(
skel, horizontal=True, probability=1.)
skel, horizontal=True, probability=1.0
)
ex = p.peek()
np.testing.assert_array_equal(ex["image"], vid[0][0][:, ::-1])
np.testing.assert_array_equal(
ex["instances"],
[[[358., 50.], [333., 25.], [358., 25.]],
[[258., 150.], [233., 125.], [258., 125.]]]
[
[[358.0, 50.0], [333.0, 25.0], [358.0, 25.0]],
[[258.0, 150.0], [233.0, 125.0], [258.0, 125.0]],
],
)

skel.add_symmetry("BL", "BR")

p = labels.to_pipeline()
p += sleap.nn.data.augmentation.RandomFlipper.from_skeleton(
skel, horizontal=True, probability=1.)
skel, horizontal=True, probability=1.0
)
ex = p.peek()
np.testing.assert_array_equal(ex["image"], vid[0][0][:, ::-1])
np.testing.assert_array_equal(
ex["instances"],
[[[358., 50.], [358., 25.], [333., 25.]],
[[258., 150.], [258., 125.], [233., 125.]]]
[
[[358.0, 50.0], [358.0, 25.0], [333.0, 25.0]],
[[258.0, 150.0], [258.0, 125.0], [233.0, 125.0]],
],
)

p = labels.to_pipeline()
p += sleap.nn.data.augmentation.RandomFlipper.from_skeleton(
skel, horizontal=True, probability=0.)
skel, horizontal=True, probability=0.0
)
ex = p.peek()
np.testing.assert_array_equal(ex["image"], vid[0][0])
np.testing.assert_array_equal(
ex["instances"],
[[[25, 50], [50, 25], [25, 25]],
[[125, 150], [150, 125], [125, 125]]]
[[[25, 50], [50, 25], [25, 25]], [[125, 150], [150, 125], [125, 125]]],
)

p = labels.to_pipeline()
p += sleap.nn.data.augmentation.RandomFlipper.from_skeleton(
skel, horizontal=False, probability=1.)
skel, horizontal=False, probability=1.0
)
ex = p.peek()
np.testing.assert_array_equal(ex["image"], vid[0][0][::-1, :])
np.testing.assert_array_equal(
ex["instances"],
[[[25, 333], [25, 358], [50, 358]],
[[125, 233], [125, 258], [150, 258]]]
[[[25, 333], [25, 358], [50, 358]], [[125, 233], [125, 258], [150, 258]]],
)
5 changes: 2 additions & 3 deletions tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ def test_instance_node_multi_get_set_item(skeleton):

assert np.allclose(x_values, [1, 2, 3])
assert np.allclose(y_values, [4, 5, 6])

np.testing.assert_array_equal(
instance1[np.array([0, 2, 4])],
[[1, 4], [np.nan, np.nan], [3, 6]]
instance1[np.array([0, 2, 4])], [[1, 4], [np.nan, np.nan], [3, 6]]
)

instance1[np.array([0, 1])] = [[1, 2], [3, 4]]
Expand Down

0 comments on commit 56660c0

Please sign in to comment.