Repository navigation
Pretty _repr_html_ of cube dimension metadata
#955
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
Draft
soxofaan
wants to merge
7
commits into
master
Choose a base branch
from
issue827-cube-metadata-viz
base: master
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.
+2,811
−1,561
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ead4ed7
Initial table-based repr_html of cube dimension metadata #827
soxofaan f9d7fd4
Handle filter_temporal in CubeMetadata, down to TemporalDimension
soxofaan e28a735
Handle `filter_temporal` end-exclusiveness in DateTimeInterval #955
soxofaan deccd7a
Add `axis` attribute to SpatialDimension
soxofaan e2cc888
Reorganise test_metadata.py
soxofaan b4e334e
Basic handling of filter_bbox in CubeMetadata, down to SpatialDimension
soxofaan fc76003
Exclude antimeridian handling from metadata filter_bbox for now
soxofaan 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
Some comments aren't visible on the classic Files Changed page.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import typing | ||
|
|
||
|
|
||
| class _NonConcreteExtent: | ||
| """ | ||
| Special marker class for non-concrete spatial, temporal, or other extents. | ||
| There can be several reasons for an extent to be non-concrete at a given time: | ||
| being generally unknown, being undefined or unspecified by the user, | ||
| being (unintendedly) unavailable/unresolvable, being dynamic or parameterized, etc. | ||
|
|
||
| The essential aspects this class signals: | ||
| - it is known that there is an extent | ||
| - but one or more actual bounds are non-concrete in some sense (unknown, dynamic, ...) | ||
|
|
||
| Note that this is different from, so this class should not be used for: | ||
| - the absence of an extent (e.g. no limits) or partial open bounds | ||
| - an empty extent | ||
| """ | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "<_NonConcreteExtent>" | ||
|
|
||
| def __eq__(self, value: object, /) -> bool: | ||
| if isinstance(value, _NonConcreteExtent): | ||
| # Non-concrete extents are not equal to each other | ||
| # even not to themselves (because they are non-concrete). | ||
| return False | ||
| # Still let third-party equality tooling hook in (e.g. dirty_equals.IsInstance) | ||
| return NotImplemented | ||
|
|
||
|
|
||
| class _EmptyExtent: | ||
| """ | ||
| Special marker class for an empty extent (spatial, temporal, or other), like the empty set. | ||
| The essential aspects this class signals: there is a known, concrete extent, | ||
| but has no coverage: nothing matches or falls within it. | ||
|
|
||
| Typical result of successive filter operations that have no overlap. | ||
|
|
||
| Note that this is different from: | ||
| - the absence of an extent (e.g. no limits) or partial open bounds | ||
| - a non-concrete extent (which is known to have coverage, but the actual bounds are unknown, dynamic, etc.) | ||
| """ | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "<_EmptyExtent>" | ||
|
|
||
| def __eq__(self, other) -> bool: | ||
| if isinstance(other, _EmptyExtent): | ||
| return True | ||
| return NotImplemented | ||
|
|
||
|
|
||
| class NumericalInterval(typing.NamedTuple): | ||
| """ | ||
| Simple numerical interval defined by a start and end point | ||
| each of type float/int; | ||
| or None to indicate being open-ended in that direction (no limit). | ||
| """ | ||
|
|
||
| start: float | int | None | ||
| end: float | int | None | ||
|
|
||
| @classmethod | ||
| def try_from(cls, x: typing.Any) -> NumericalInterval | None: | ||
| if isinstance(x, (list, tuple)) and len(x) == 2 and all(isinstance(v, (int, float)) or v is None for v in x): | ||
| return NumericalInterval(*x) | ||
| return None | ||
|
|
||
| def intersection(self, other: NumericalInterval) -> NumericalInterval | _EmptyExtent: | ||
| """ | ||
| Compute the intersection of this interval with another interval. | ||
| If the intervals do not overlap, return an _EmptyExtent. | ||
| """ | ||
| if self.start is None: | ||
| start = other.start | ||
| elif other.start is None: | ||
| start = self.start | ||
| else: | ||
| start = max(self.start, other.start) | ||
|
|
||
| if self.end is None: | ||
| end = other.end | ||
| elif other.end is None: | ||
| end = self.end | ||
| else: | ||
| end = min(self.end, other.end) | ||
|
|
||
| if start is not None and end is not None and end < start: | ||
| return _EmptyExtent() | ||
|
|
||
| return NumericalInterval(start, end) |
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """ | ||
| HTML rendering helpers for metadata models (e.g. ``CubeMetadata``), used for Jupyter's ``_repr_html_``. | ||
|
|
||
| Kept in a separate module (instead of directly in ``openeo.metadata``) to keep the core metadata | ||
| data model free of rendering/presentation concerns. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import html | ||
| from typing import List | ||
|
|
||
| from openeo.metadata import ( | ||
| BandDimension, | ||
| Dimension, | ||
| SpatialDimension, | ||
| TemporalDimension, | ||
| ) | ||
|
|
||
|
|
||
| def repr_html_dimensions(dimensions: List[Dimension]) -> str: | ||
| """ | ||
| Render a compact HTML table with an overview of cube dimensions | ||
| (name, type, size, labels/extent), based on available (client-side) metadata. | ||
| """ | ||
|
|
||
| header = ["Dimension", "Type", "Size", "Labels / Extent"] | ||
| rows = [] | ||
| for dim in dimensions: | ||
| row = [dim.name, str(dim.type)] | ||
| if isinstance(dim, BandDimension): | ||
| labels = ", ".join(str(name) if name else "?" for name in dim.band_names) | ||
| row.extend([str(len(dim.bands)), "bands: " + labels]) | ||
| elif isinstance(dim, SpatialDimension): | ||
| row.extend(["?", f"extent: {repr(dim.extent)}"]) | ||
| elif isinstance(dim, TemporalDimension): | ||
| row.extend(["?", f"extent: {repr(dim.extent)}"]) | ||
| else: | ||
| row.extend(["?", ""]) | ||
| rows.append("<tr>" + "".join(f"<td>{html.escape(c)}</td>" for c in row) + "</tr>") | ||
|
|
||
| thead = "<tr>" + "".join(f"<th>{html.escape(c)}</th>" for c in header) + "</tr>" | ||
| tbody = "".join(rows) | ||
|
|
||
| return f"""<table> | ||
| <thead>{thead}</thead> | ||
| <tbody>{tbody}</tbody> | ||
| </table>""" | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.