-
Notifications
You must be signed in to change notification settings - Fork 250
Fix data dimension swapping and axis errors #1335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhayrajjais01
wants to merge
2
commits into
weecology:main
Choose a base branch
from
abhayrajjais01:fix-data-dimensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,60 +2,99 @@ | |
| import os | ||
|
|
||
| import numpy as np | ||
| from PIL import Image | ||
| import pytest | ||
| from PIL import Image | ||
|
|
||
| from deepforest import get_data | ||
| from deepforest.datasets.prediction import TiledRaster, SingleImage, MultiImage, FromCSVFile, PredictionDataset | ||
| from deepforest.datasets.prediction import ( | ||
| FromCSVFile, | ||
| MultiImage, | ||
| SingleImage, | ||
| TiledRaster, | ||
| ) | ||
|
|
||
|
|
||
| def test_TiledRaster(): | ||
| tile_path = get_data("test_tiled.tif") | ||
| ds = TiledRaster(path=tile_path, | ||
| patch_size=300, | ||
| patch_overlap=0) | ||
| ds = TiledRaster(path=tile_path, patch_size=300, patch_overlap=0) | ||
| assert len(ds) == 16 | ||
|
|
||
| # assert crop shape | ||
| assert ds[1].shape == (3, 300, 300) | ||
|
|
||
|
|
||
| def test_TiledRaster_non_square(tmp_path): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m having difficulty seeing how this test is necessary given the changes made. |
||
| import rasterio as rio | ||
| from rasterio.transform import from_origin | ||
|
|
||
| transform = from_origin(0, 0, 1, 1) | ||
| raster_path = str(tmp_path / "non_square.tif") | ||
| # create a 3 band, 500 height, 800 width raster | ||
| with rio.open( | ||
| raster_path, | ||
| "w", | ||
| driver="GTiff", | ||
| height=500, | ||
| width=800, | ||
| count=3, | ||
| dtype=np.uint8, | ||
| crs="+proj=latlong", | ||
| transform=transform, | ||
| tiled=True, | ||
| blockxsize=256, | ||
| blockysize=256, | ||
| ) as dst: | ||
| dst.write(np.zeros((3, 500, 800), dtype=np.uint8)) | ||
|
|
||
| ds = TiledRaster(path=raster_path, patch_size=400, patch_overlap=0) | ||
|
|
||
| # width 800 -> 2 patches. height 500 -> 2 patches. Total = 4 patches | ||
| assert len(ds) == 4 | ||
|
|
||
|
|
||
| def test_SingleImage_path(): | ||
| ds = SingleImage( | ||
| path=get_data("OSBS_029.png"), | ||
| patch_size=300, | ||
| patch_overlap=0) | ||
| ds = SingleImage(path=get_data("OSBS_029.png"), patch_size=300, patch_overlap=0) | ||
|
|
||
| assert len(ds) == 4 | ||
| assert ds[0].shape == (3, 300, 300) | ||
|
|
||
| for i in range(len(ds)): | ||
| assert ds.get_crop(i).shape == (3, 300, 300) | ||
|
|
||
|
|
||
| def test_invalid_image_shape(): | ||
| # Not 3 channels | ||
| test_data = (np.random.rand(300, 300, 4) * 255).astype(np.uint8) | ||
| with pytest.raises(ValueError): | ||
| SingleImage(image=Image.fromarray(test_data)) | ||
|
|
||
|
|
||
| def test_valid_image(): | ||
| # 8-bit, HWC | ||
| test_data = np.random.randint(0, 256, (300,300,3)).astype(np.uint8) | ||
| test_data = np.random.randint(0, 256, (300, 300, 3)).astype(np.uint8) | ||
| SingleImage(image=Image.fromarray(test_data)) | ||
|
|
||
|
|
||
| def test_valid_array(): | ||
| # 8-bit, HWC | ||
| test_data = np.random.randint(0, 256, (300,300,3)).astype(np.uint8) | ||
| test_data = np.random.randint(0, 256, (300, 300, 3)).astype(np.uint8) | ||
| SingleImage(image=test_data) | ||
|
|
||
|
|
||
| def test_MultiImage(): | ||
| ds = MultiImage(paths=[get_data("OSBS_029.png"), get_data("OSBS_029.png")], | ||
| patch_size=300, | ||
| patch_overlap=0) | ||
| ds = MultiImage( | ||
| paths=[get_data("OSBS_029.png"), get_data("OSBS_029.png")], | ||
| patch_size=300, | ||
| patch_overlap=0, | ||
| ) | ||
| # 2 windows each image 2 * 2 = 4 | ||
| assert len(ds) == 2 | ||
| assert ds[0][0].shape == (3, 300, 300) | ||
|
|
||
|
|
||
| def test_FromCSVFile(): | ||
| ds = FromCSVFile(csv_file=get_data("example.csv"), | ||
| root_dir=os.path.dirname(get_data("example.csv"))) | ||
| ds = FromCSVFile( | ||
| csv_file=get_data("example.csv"), | ||
| root_dir=os.path.dirname(get_data("example.csv")), | ||
| ) | ||
| assert len(ds) == 1 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take some time to understand this part of the code, you may be able to make more meaningful improvements to the logic.