-
Notifications
You must be signed in to change notification settings - Fork 36
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
Improve typing and docstrings #87
Conversation
Renamed * `WaveletTransformReturn3d` to `WaveletCoeffDetailDict`, * `WaveletTransformReturn2d` to `WaveletCoeffDetailTuple2d` and added the type aliases * `WaveletDetailDict` for `dict[str, Tensor]` * `WaveletDetailTuple2d` for `tuple[Tensor, Tensor, Tensor]`
Thanks a lot, @felixblanke. Since it's older, I will start with #84 and then move on to this PR. |
src/ptwt/_stationary_transform.py
Outdated
@@ -28,7 +29,7 @@ def _swt( | |||
level (Optional[int], optional): The number of levels to compute | |||
|
|||
Returns: | |||
List[torch.Tensor]: Same as wavedec. | |||
list[torch.Tensor]: Same as wavedec. |
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.
as you go through these, it's better to remove the redundant type annotations inside the docstrings rather than update them
list[torch.Tensor]: Same as wavedec. | |
: Same as wavedec. |
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.
Should we keep the leading colons? The examples in Google's styleguide show none.
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.
if the documentation works without it, then no! I am not usually using this code style so I don't know all the specifics
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 went through the docstrings and cleaned them up. This should be addressed now.
The styleguide also writes about function arguments: "The description should include required type(s) if the code does not contain a corresponding type annotation." Opinions on this matter?
src/ptwt/_util.py
Outdated
@@ -29,6 +31,15 @@ def __len__(self) -> int: | |||
return len(self.dec_lo) | |||
|
|||
|
|||
WaveletDetailTuple2d = tuple[torch.Tensor, torch.Tensor, torch.Tensor] |
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.
it's useful to add a docstring to each of these tuples to give some additional context about what they are and where they're used
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.
also, this could be a place to use a namedtuple to make it even more explicit
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.
Thats a good idea 👍
I just noticed that that the type aliases like |
Fixates some type aliases such that the API documentation shows the type alias name instead of its resolved value. For this feature to work we need to import the 'annotations' feature from __future__
I moved the type aliases into the from __future__ import annotations in all modules using the type aliases. Further, as suggested by @cthoyt I changed |
Rename * `WaveletCoeffDetailDict` to `WaveletCoeffNd` * `WaveletCoeffDetailTuple2d` to `WaveletCoeff2d`
This PR changes the returned collection for higher dimensional wavelet transforms from list to tuple to enable better type hints. This is not done for the 1d case where we still return |
https://pywavelets.readthedocs.io/en/latest/ref/dwt-discrete-wavelet-transform.html#multilevel-decomposition-using-wavedec also returns a list, although consistency would, of course, be nice. While I lean towards leaving things as is, I would accept the change for consistency. However, if someone comes from |
Make the functions creating the natural and frequency packet order for WaveletPacket2d static methods of WaveletPacket2d. This changes * `get_natural_order` from instance to static function * `get_frequency_order` from separate func to static function in the scope of WaveletPacket2d Further, to make both methods consistent, `get_frequency_order` now returns concatenated strings instead of a tuple of single char strings.
I also went through the examples. In consequence, I refactored the functions creating the natural and frequency packet order for |
do we add |
okay. I think we are done here. Thanks a lot @felixblanke and @cthoyt . I will merge this and close #85 . Feel free to reopen if we are still missing something. |
This addresses #85 and changes type hints throughout the code base.
Notably, I added two type aliases as return types for wavelet transforms:
WaveletCoeffNd
which corresponds to(Tensor, *(WaveletDetailDict, ...))
WaveletCoeff2d
which corresponds to(Tensor, *(WaveletDetailTuple2d, ...))
which make use of the following two new type aliases for detail coefficients:
WaveletDetailDict
which corresponds todict[str, Tensor]
WaveletDetailTuple2d
which corresponds to(Tensor, Tensor, Tensor)
.I also added the type alias
WaveletCoeff2dSeparable
which is an alias forWaveletCoeffNd
to emphasize the different return type in case of the 2d separable transform(I would be very much open to suggestions how to name those differently)
Importantly: The collection that contains the wavelet coefficients changes from
list
totuple
. Therefore, this changes the return types of our wavelet coefficients, not "only" for type hinting but also with possible impact on user code (e.g. we change the collection from mutable to immutable).Further changes:
typing.List
to the builtinlist
(and the same fordict
andtuple
). This was introduced in python 3.9 (our minimum version)Important note for checking the type hints:
The overloads for
_map_result
introduced in the_util
module currently lead to a crash in mypy (relevant issue). Thus, running the nox typing session will fail.Update: A fix has been merged to mypy's master branch (relevant PR). To install from the git repository, replace in
setup.cfg:70
the linemypy
by