def test_from_dict(): mock_dict_complete = {"strength": "strong"} sharpen_mock_dict_complete = RasterSharpener.from_dict(mock_dict_complete) assert sharpen_mock_dict_complete.strength == "strong" mock_dict_empty = {} sharpen_mock_dict_complete = RasterSharpener.from_dict(mock_dict_empty) assert sharpen_mock_dict_complete.strength == "medium"
def test_run(tmp_raster_fixture): """ Checks the raster processing for multiple images. """ _, out_path = tmp_raster_fixture # Set params via env vars and load them os.environ["UP42_TASK_PARAMETERS"] = """ { "strength": "light" } """ RasterSharpener().run() assert out_path.exists()
def test_raster_sharpening_expected_pixel_values(tmp_raster_fixture, strength, expected_mean): in_path, out_path = tmp_raster_fixture params_dict = {"strength": strength} RasterSharpener().from_dict(params_dict).sharpen_raster(in_path, out_path) assert out_path.exists() with rio.open(str(out_path), "r") as src: band_count = src.meta["count"] assert band_count == 4 img_array = np.stack(list(src.read(range(1, band_count + 1)))) assert img_array.shape == (4, 256, 256) assert img_array.mean() == expected_mean
def test_alpha_band_ignored(tmp_raster_fixture, test_array_fixture): """ Checks if alpha band has changed. """ in_path, out_path = tmp_raster_fixture params_dict = {"strength": "medium"} img_array = test_array_fixture RasterSharpener().from_dict(params_dict).sharpen_raster(in_path, out_path) assert out_path.exists() with rio.open(str(out_path), "r") as src: band_count = src.meta["count"] assert band_count == 4 sharpened = src.read() assert np.array_equal(img_array[-1], sharpened[-1])
def test_process_data_path(tmp_raster_fixture): """ Checks the raster processing for multiple images. """ in_path, _ = tmp_raster_fixture img_file_list = [in_path] feature_list: List[Feature] = [] for img_path in img_file_list: bbox = [2.5, 1.0, 4.0, 5.0] geom = box(*bbox) in_properties = { "up42.data_path": str(Path(*img_path.parts[-2:])), "acquisitionDate": "2018-10-16T10:39:43.431Z", } feature_list.append( Feature(geometry=geom, bbox=bbox, properties=in_properties)) input_fc = FeatureCollection(feature_list) output_fc = RasterSharpener().process(input_fc) # Check that all features are derived assert len(output_fc["features"]) == 1 for feature in output_fc.features: # Check that file paths in metadata are relative feature_file = feature["properties"]["up42.data_path"] assert feature["properties"]["up42.data_path"] assert Path(feature_file).root == "" # Check that metadata is propagated assert feature["properties"][ "acquisitionDate"] == "2018-10-16T10:39:43.431Z" # Check that feature outputs exist feature_path = Path("/tmp/output").joinpath(feature_file) assert feature_path.is_file() # Cleanup feature_path.unlink()
def test_sharpen_raster(tmp_raster_fixture): in_path, out_path = tmp_raster_fixture RasterSharpener().sharpen_raster(in_path, out_path) assert out_path.exists()
def test_sharpen_array(test_array_fixture): sharpened = RasterSharpener().sharpen_array(test_array_fixture) assert sharpened.shape == (4, 256, 256)