Skip to content

Commit

Permalink
slightly refactor some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AdeelH committed Nov 27, 2024
1 parent 488deb6 commit 73cd57f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
42 changes: 21 additions & 21 deletions tests/core/data/crs_transformer/test_rasterio_crs_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class TestRasterioCRSTransformer(unittest.TestCase):
def setUp(self):
self.im_path = data_file_path('3857.tif')
self.im_dataset = rasterio.open(self.im_path)
self.crs_trans = RasterioCRSTransformer.from_dataset(self.im_dataset)
self.crs_tf = RasterioCRSTransformer.from_dataset(self.im_dataset)
self.lon_lat = (-115.306372, 36.126825)
self.pix_point = (51, 62)

def test_map_to_pixel_point(self):
# w/o bbox
map_point = self.lon_lat
pix_point = self.crs_trans.map_to_pixel(map_point)
pix_point = self.crs_tf.map_to_pixel(map_point)
self.assertEqual(pix_point, self.pix_point)

# w/ bbox
bbox = Box(20, 20, 80, 80)
map_point = self.lon_lat
pix_point = self.crs_trans.map_to_pixel(map_point, bbox=bbox)
pix_point = self.crs_tf.map_to_pixel(map_point, bbox=bbox)
pix_x, pix_y = self.pix_point
pix_point_expected = (pix_x - 20, pix_y - 20)
self.assertEqual(pix_point, pix_point_expected)
Expand All @@ -37,8 +37,8 @@ def test_map_to_pixel_array(self):
self.lon_lat,
self.lon_lat,
])
pix_point = self.crs_trans.map_to_pixel((map_point[:, 0],
map_point[:, 1]))
pix_point = self.crs_tf.map_to_pixel((map_point[:, 0],
map_point[:, 1]))
pix_point_expected = np.array([
self.pix_point,
self.pix_point,
Expand All @@ -52,7 +52,7 @@ def test_map_to_pixel_array(self):
self.lon_lat,
self.lon_lat,
])
pix_point = self.crs_trans.map_to_pixel(
pix_point = self.crs_tf.map_to_pixel(
(map_point[:, 0], map_point[:, 1]), bbox=bbox)
pix_point_expected = np.array([
self.pix_point,
Expand All @@ -66,7 +66,7 @@ def test_map_to_pixel_box(self):
# w/o bbox
map_x, map_y = self.lon_lat
map_box = Box(map_y, map_x, map_y, map_x)
pix_box = self.crs_trans.map_to_pixel(map_box)
pix_box = self.crs_tf.map_to_pixel(map_box)
pix_x, pix_y = self.pix_point
pix_box_expected = Box(pix_y, pix_x, pix_y, pix_x)
self.assertEqual(pix_box, pix_box_expected)
Expand All @@ -75,34 +75,34 @@ def test_map_to_pixel_box(self):
bbox = Box(20, 20, 80, 80)
map_x, map_y = self.lon_lat
map_box = Box(map_y, map_x, map_y, map_x)
pix_box = self.crs_trans.map_to_pixel(map_box, bbox=bbox)
pix_box = self.crs_tf.map_to_pixel(map_box, bbox=bbox)
pix_x, pix_y = self.pix_point
pix_box_expected = Box(pix_y - 20, pix_x - 20, pix_y - 20, pix_x - 20)
self.assertEqual(pix_box, pix_box_expected)

def test_map_to_pixel_shapely(self):
# w/o bbox
map_geom = Point(self.lon_lat)
pix_geom = self.crs_trans.map_to_pixel(map_geom)
pix_geom = self.crs_tf.map_to_pixel(map_geom)
pix_geom_expected = Point(self.pix_point)
self.assertEqual(pix_geom, pix_geom_expected)

# w/ bbox
bbox = Box(20, 20, 80, 80)
map_geom = Point(self.lon_lat)
pix_geom = self.crs_trans.map_to_pixel(map_geom, bbox=bbox)
pix_geom = self.crs_tf.map_to_pixel(map_geom, bbox=bbox)
pix_x, pix_y = self.pix_point
pix_geom_expected = Point((pix_x - 20, pix_y - 20))
self.assertEqual(pix_geom, pix_geom_expected)

def test_map_to_pixel_invalid_input(self):
self.assertRaises(TypeError,
lambda: self.crs_trans.map_to_pixel((1, 2, 3)))
lambda: self.crs_tf.map_to_pixel((1, 2, 3)))

def test_pixel_to_map_point(self):
# w/o bbox
pix_point = self.pix_point
map_point = self.crs_trans.pixel_to_map(pix_point)
map_point = self.crs_tf.pixel_to_map(pix_point)
map_point_expected = self.lon_lat
np.testing.assert_almost_equal(
map_point, map_point_expected, decimal=3)
Expand All @@ -111,7 +111,7 @@ def test_pixel_to_map_point(self):
bbox = Box(20, 20, 80, 80)
pix_x, pix_y = self.pix_point
pix_point = (pix_x - 20, pix_y - 20)
map_point = self.crs_trans.pixel_to_map(pix_point, bbox=bbox)
map_point = self.crs_tf.pixel_to_map(pix_point, bbox=bbox)
map_point_expected = self.lon_lat
np.testing.assert_almost_equal(
map_point, map_point_expected, decimal=3)
Expand All @@ -122,8 +122,8 @@ def test_pixel_to_map_array(self):
self.pix_point,
self.pix_point,
])
map_point = self.crs_trans.pixel_to_map((pix_point[:, 0],
pix_point[:, 1]))
map_point = self.crs_tf.pixel_to_map((pix_point[:, 0],
pix_point[:, 1]))
map_point_expected = np.array([
self.lon_lat,
self.lon_lat,
Expand All @@ -140,7 +140,7 @@ def test_pixel_to_map_array(self):
self.pix_point,
])
pix_point -= 20
map_point = self.crs_trans.pixel_to_map(
map_point = self.crs_tf.pixel_to_map(
(pix_point[:, 0], pix_point[:, 1]), bbox=bbox)
map_point_expected = np.array([
self.lon_lat,
Expand All @@ -155,7 +155,7 @@ def test_pixel_to_map_box(self):
# w/o bbox
pix_x, pix_y = self.pix_point
pix_box = Box(pix_y, pix_x, pix_y, pix_x)
map_box = self.crs_trans.pixel_to_map(pix_box)
map_box = self.crs_tf.pixel_to_map(pix_box)
map_x, map_y = self.lon_lat
map_box_expected = Box(map_y, map_x, map_y, map_x)
np.testing.assert_almost_equal(
Expand All @@ -167,7 +167,7 @@ def test_pixel_to_map_box(self):
bbox = Box(20, 20, 80, 80)
pix_x, pix_y = self.pix_point
pix_box = Box(pix_y - 20, pix_x - 20, pix_y - 20, pix_x - 20)
map_box = self.crs_trans.pixel_to_map(pix_box, bbox=bbox)
map_box = self.crs_tf.pixel_to_map(pix_box, bbox=bbox)
map_x, map_y = self.lon_lat
map_box_expected = Box(map_y, map_x, map_y, map_x)
np.testing.assert_almost_equal(
Expand All @@ -178,7 +178,7 @@ def test_pixel_to_map_box(self):
def test_pixel_to_map_shapely(self):
# w/o bbox
pix_geom = Point(self.pix_point)
map_geom = self.crs_trans.pixel_to_map(pix_geom)
map_geom = self.crs_tf.pixel_to_map(pix_geom)
map_geom_expected = Point(self.lon_lat)
np.testing.assert_almost_equal(
np.concatenate(map_geom.xy).reshape(-1),
Expand All @@ -189,7 +189,7 @@ def test_pixel_to_map_shapely(self):
bbox = Box(20, 20, 80, 80)
pix_x, pix_y = self.pix_point
pix_geom = Point((pix_x - 20, pix_y - 20))
map_geom = self.crs_trans.pixel_to_map(pix_geom, bbox=bbox)
map_geom = self.crs_tf.pixel_to_map(pix_geom, bbox=bbox)
map_geom_expected = Point(self.lon_lat)
np.testing.assert_almost_equal(
np.concatenate(map_geom.xy).reshape(-1),
Expand All @@ -198,7 +198,7 @@ def test_pixel_to_map_shapely(self):

def test_pixel_to_map_invalid_input(self):
self.assertRaises(TypeError,
lambda: self.crs_trans.pixel_to_map((1, 2, 3)))
lambda: self.crs_tf.pixel_to_map((1, 2, 3)))

def test_from_dataset(self):
# default map_crs
Expand Down
8 changes: 4 additions & 4 deletions tests/core/data/utils/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def setUp(self) -> None:
self.crs_tf = self.raster_source.crs_transformer

self.bbox_ls = Box(0, 0, 12, 12)
geoms = [b.to_shapely() for b in self.bbox_ls.get_windows(2, 2)]
geoms = [self.crs_tf.pixel_to_map(g) for g in geoms]
properties = [dict(class_id=0) for _ in geoms]
geojson = geoms_to_geojson(geoms, properties)
geoms_pixel = [b.to_shapely() for b in self.bbox_ls.get_windows(2, 2)]
geoms_map = [self.crs_tf.pixel_to_map(g) for g in geoms_pixel]
properties = [dict(class_id=0) for _ in geoms_map]
geojson = geoms_to_geojson(geoms_map, properties)
self._tmp_dir = get_tmp_dir()
self.tmp_dir = self._tmp_dir.name
uri = join(self.tmp_dir, 'labels.json')
Expand Down
8 changes: 3 additions & 5 deletions tests/core/data/vector_transformer/test_shift_transformer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import unittest

from shapely.geometry import (Point, mapping, shape)
from shapely.geometry import Point, shape

from rastervision.core.data.crs_transformer import RasterioCRSTransformer
from rastervision.core.data.vector_transformer import (ShiftTransformer,
ShiftTransformerConfig)
from rastervision.core.data.utils import (geometry_to_feature,
geometries_to_geojson)
from rastervision.core.data.utils import geoms_to_geojson

from tests import data_file_path

Expand All @@ -26,8 +25,7 @@ def setUp(self) -> None:
def test_transform(self):
"""This only tests the directionality of the change."""
geom_in = Point(0, 0)
feat_in = geometry_to_feature(mapping(geom_in))
geojson_in = geometries_to_geojson([feat_in])
geojson_in = geoms_to_geojson([geom_in])

# x_shift = 1, y_shift = 0
tf = ShiftTransformer(x_shift=1, y_shift=0)
Expand Down

0 comments on commit 73cd57f

Please sign in to comment.