-
Notifications
You must be signed in to change notification settings - Fork 28
fix: more testing and label support #258
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
Merged
Merged
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
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 |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| import sys | ||
| import typing | ||
| from typing import Any, Iterator, List, Tuple, Union | ||
| from typing import Any, Iterator, List, Tuple, TypeVar, Union | ||
|
|
||
| from .basehist import BaseHist | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from mplhep.plot import Hist1DArtists | ||
|
|
||
| __all__ = ("Stack",) | ||
|
|
||
| T = TypeVar("T", bound="Stack") | ||
|
|
||
|
|
||
| class Stack: | ||
| def __init__( | ||
|
|
@@ -30,35 +33,48 @@ def __init__( | |
| if first_axes != a.axes: | ||
| raise ValueError("The Histogram axes don't match") | ||
|
|
||
| def __repr__(self) -> str: | ||
| str_stack = ", ".join(repr(h) for h in self._stack) | ||
| return f"{self.__class__.__name__}({str_stack})" | ||
| @typing.overload | ||
| def __getitem__(self, val: int) -> BaseHist: | ||
| ... | ||
|
|
||
| @typing.overload | ||
| def __getitem__(self: T, val: slice) -> T: | ||
| ... | ||
|
|
||
| def __getitem__(self: T, val: Union[int, slice]) -> Union[BaseHist, T]: | ||
| if isinstance(val, slice): | ||
| return self.__class__(*self._stack.__getitem__(val)) | ||
|
|
||
| def __getitem__( | ||
| self, val: Union[int, slice] | ||
| ) -> Union[BaseHist, Tuple[BaseHist, ...]]: | ||
| return self._stack.__getitem__(val) | ||
|
|
||
| def __iter__(self) -> Iterator[BaseHist]: | ||
| return iter(self._stack) | ||
|
|
||
| def plot(self, *, overlay: None = None, **kwargs: Any) -> "List[Hist1DArtists]": | ||
| def __len__(self) -> int: | ||
|
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. Forgot this earlier. |
||
| return len(self._stack) | ||
|
|
||
| def __repr__(self) -> str: | ||
| str_stack = ", ".join(repr(h) for h in self) | ||
| return f"{self.__class__.__name__}({str_stack})" | ||
|
|
||
| def plot(self, **kwargs: Any) -> "List[Hist1DArtists]": | ||
| """ | ||
| Plot method for Stack object. | ||
| """ | ||
| if overlay is not None: | ||
| raise NotImplementedError("Currently overlay is not supported") | ||
|
|
||
| if self._stack[0].ndim != 1: | ||
| import hist.plot | ||
|
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. Besides piggy-backing on our existing mechanism for replacing this, it also makes it easy to monkeypatch in tests, too! |
||
|
|
||
| if self[0].ndim != 1: | ||
| raise NotImplementedError("Please project to 1D before calling plot") | ||
|
|
||
| try: | ||
| import mplhep.plot | ||
| except ModuleNotFoundError: | ||
| print( | ||
| f"{self.__class__.__name__}.plot() requires mplhep to plot, either install hist[plot] or mplhep", | ||
| file=sys.stderr, | ||
| ) | ||
| raise | ||
| if "label" not in kwargs: | ||
| # TODO: add .name to static typing. And runtime, for that matter. | ||
| if all(getattr(h, "name", None) is not None for h in self): | ||
| kwargs["label"] = [h.name for h in self] # type: ignore | ||
|
|
||
| return hist.plot.histplot(list(self), **kwargs) # type: ignore | ||
|
|
||
|
|
||
| return mplhep.plot.histplot(list(self._stack), **kwargs) # type: ignore | ||
| # Python 3.7 only | ||
| def __dir__() -> Tuple[str, ...]: | ||
| return __all__ | ||
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
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.
I think this is safe. Let's try it, anyway; it saves quite a few (15-20) seconds for me.