-
Notifications
You must be signed in to change notification settings - Fork 109
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
forward dataset statistics to ImageClass #531
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
|
@@ -256,6 +256,7 @@ class ImageData: | |
crs (rasterio.crs.CRS, optional): Coordinates Reference System of the bounds. | ||
metadata (dict, optional): Additional metadata. Defaults to `{}`. | ||
band_names (list, optional): name of each band. Defaults to `["1", "2", "3"]` for 3 bands image. | ||
dataset_statistics (list, optional): dataset statistics `[(min, max), (min, max)]` | ||
|
||
""" | ||
|
||
|
@@ -266,6 +267,7 @@ class ImageData: | |
crs: Optional[CRS] = attr.ib(default=None) | ||
metadata: Optional[Dict] = attr.ib(factory=dict) | ||
band_names: List[str] = attr.ib() | ||
dataset_statistics: Optional[Sequence[Tuple[float, float]]] = attr.ib(default=None) | ||
|
||
@data.validator | ||
def _validate_data(self, attribute, value): | ||
|
@@ -331,8 +333,21 @@ def create_from_list(cls, data: Sequence["ImageData"]): | |
) | ||
) | ||
|
||
stats = list( | ||
itertools.chain.from_iterable( | ||
[img.dataset_statistics for img in data if img.dataset_statistics] | ||
) | ||
) | ||
dataset_statistics = stats if len(stats) == len(band_names) else None | ||
|
||
return cls( | ||
arr, mask, assets=assets, crs=crs, bounds=bounds, band_names=band_names | ||
arr, | ||
mask, | ||
assets=assets, | ||
crs=crs, | ||
bounds=bounds, | ||
band_names=band_names, | ||
dataset_statistics=dataset_statistics, | ||
) | ||
|
||
def as_masked(self) -> numpy.ma.MaskedArray: | ||
|
@@ -391,6 +406,15 @@ def apply_color_formula(self, color_formula: Optional[str]): | |
def apply_expression(self, expression: str) -> "ImageData": | ||
"""Apply expression to the image data.""" | ||
blocks = get_expression_blocks(expression) | ||
|
||
stats = self.dataset_statistics | ||
if stats: | ||
res = [] | ||
for prod in itertools.product(*stats): # type: ignore | ||
res.append(apply_expression(blocks, self.band_names, numpy.array(prod))) | ||
|
||
stats = list(zip([min(r) for r in zip(*res)], [max(r) for r in zip(*res)])) | ||
|
||
return ImageData( | ||
apply_expression(blocks, self.band_names, self.data), | ||
self.mask, | ||
|
@@ -399,6 +423,7 @@ def apply_expression(self, expression: str) -> "ImageData": | |
bounds=self.bounds, | ||
band_names=blocks, | ||
metadata=self.metadata, | ||
dataset_statistics=stats, | ||
) | ||
|
||
def post_process( | ||
|
@@ -473,7 +498,7 @@ def render( | |
kwargs.update({"crs": self.crs}) | ||
|
||
data = self.data.copy() | ||
datatype_range = (dtype_ranges[str(data.dtype)],) | ||
datatype_range = self.dataset_statistics or (dtype_ranges[str(data.dtype)],) | ||
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. if present we use the dataset min/max for the auto rescaling! |
||
|
||
if not colormap: | ||
if img_format in ["PNG"] and data.dtype not in ["uint8", "uint16"]: | ||
|
This file contains 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 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
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.
This is the only real interesting part of the addition (because technically you can access the metadata directly with rasterio). Here we calculate the result of the expression on the min/max values so we get the min/max of the expression output