-
Notifications
You must be signed in to change notification settings - Fork 234
Add the Box class for specifying the box of GMT embellishments #3995
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
Changes from 19 commits
b73a6e2
9f08d1b
01b563c
d2ae10c
ec1af1d
48702b5
210292f
8e5321e
926a690
b1c6330
bc31c7c
de1545f
44b0be3
1d14eb5
6ddcf4e
84ca37e
0313330
40a47a8
457abed
16c3d6c
ab7ad67
3c1ee05
9d60637
c4a19a5
5ec1180
86d0a32
4e98acf
22fb1eb
a83f21d
dce0e47
078dffb
9b6ae97
e5ef5cf
8250eb2
81b2f4f
4efc0f3
21a47b5
1229204
30a795e
3a4b369
9eb95fa
c6bd16f
5bc36c7
e26873d
f298379
bd57dc0
a337503
7582165
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """ | ||
| Classes for common parameters in PyGMT. | ||
| """ | ||
|
|
||
| from pygmt.params.box import Box |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||
| """ | ||||||||||||
| Base class for common parameters shared in PyGMT. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class BaseParam: | ||||||||||||
|
||||||||||||
| class BaseParam: | |
| from abc import ABC, abstractmethod, abstractproperty | |
| class BaseParam(ABC): |
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've applied the suggestions but also made some changes in c6bd16f. The main changes are:
abstractpropertyis deprecated https://docs.python.org/3/library/abc.html#abc.abstractproperty. Now use@propertyand@abstractmethod.@abstractmethodmeans subclasses must implement the method/property, so theNotImplementedErroris not needed and can be removed.- The
_validatemethod is meant to be optional, so we can't use@abstractmethod. Then ruff reports theB027error, and it's ignored by adding thenoqaflag.
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
seisman marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """ | ||
| The Box class for specifying the box around GMT embellishments. | ||
| """ | ||
|
|
||
| import dataclasses | ||
| from collections.abc import Sequence | ||
|
|
||
| from pygmt.alias import Alias | ||
| from pygmt.exceptions import GMTValueError | ||
| from pygmt.params.base import BaseParam | ||
|
|
||
|
|
||
| @dataclasses.dataclass(repr=False) | ||
| class Box(BaseParam): | ||
| """ | ||
| Class for specifying the box around GMT embellishments. | ||
| Attributes | ||
| ---------- | ||
| clearance | ||
| Set clearances between the embellishment and the box border. It can be either a | ||
| scalar value or a sequence of two/four values. | ||
| - a scalar value means a uniform clearance in all four directions. | ||
| - a sequence of two values means separate clearances in x- and y-directions. | ||
| - a sequence of four values means separate clearances for left/right/bottom/top. | ||
| fill | ||
| Fill for the box. Default is no fill. | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pen | ||
| Pen attributes for the box outline. | ||
| radius | ||
| Draw a rounded rectangular border instead of sharp. Passing a value with unit | ||
| to control the corner radius [Default is ``"6p"``]. | ||
| inner_gap | ||
| Gap between the outer and inner borders. Default is ``"2p"``. | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| inner_pen | ||
| Pen attributes for the inner border. Default to :gmt-term:`MAP_DEFAULT_PEN`. | ||
| shading_offset | ||
| Place an offset background shaded region behind the box. A sequence of two | ||
| values (dx, dy) indicates the shift relative to the foreground frame. Default is | ||
| ``("4p", "-4p")``. | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| shading_fill | ||
| Fill for the shading region. Default is ``"gray50"``. | ||
|
||
| Examples | ||
| -------- | ||
| >>> from pygmt.params import Box | ||
| >>> str(Box(fill="red@20")) | ||
| '+gred@20' | ||
| >>> str(Box(clearance=(0.2, 0.2), fill="red@20", pen="blue")) | ||
| '+c0.2/0.2+gred@20+pblue' | ||
| >>> str(Box(clearance=(0.2, 0.2), pen="blue", radius=True)) | ||
| '+c0.2/0.2+pblue+r' | ||
| >>> str(Box(clearance=(0.1, 0.2, 0.3, 0.4), pen="blue", radius="10p")) | ||
| '+c0.1/0.2/0.3/0.4+pblue+r10p' | ||
| >>> str( | ||
| ... Box( | ||
| ... clearance=0.2, | ||
| ... pen="blue", | ||
| ... radius="10p", | ||
| ... shading_offset=("5p", "5p"), | ||
| ... shading_fill="lightred", | ||
| ... ) | ||
| ... ) | ||
| '+c0.2+pblue+r10p+s5p/5p/lightred' | ||
| >>> str(Box(clearance=0.2, inner_gap="2p", inner_pen="1p,red", pen="blue")) | ||
| '+c0.2+i2p/1p,red+pblue' | ||
| >>> str(Box(clearance=0.2, shading_offset=("5p", "5p"), shading_fill="lightred")) | ||
| '+c0.2+s5p/5p/lightred' | ||
| """ | ||
|
|
||
| clearance: float | str | Sequence[float | str] | None = None | ||
| fill: str | None = None | ||
| inner_gap: float | str | None = None | ||
| inner_pen: str | None = None | ||
| pen: str | None = None | ||
| radius: str | bool = False | ||
| shading_offset: Sequence[float | str] | None = None | ||
| shading_fill: str | None = None | ||
|
|
||
| def _validate(self): | ||
| """ | ||
| Validate the parameters. | ||
| """ | ||
| # shading_offset must be a sequence of two values or None. | ||
| if self.shading_offset and ( | ||
| not isinstance(self.shading_offset, Sequence) | ||
| or len(self.shading_offset) != 2 | ||
| ): | ||
| raise GMTValueError( | ||
| self.shading_offset, | ||
| description="value for parameter 'shading_offset'", | ||
| reason="Must be a sequence of two values (dx, dy) or None.", | ||
| ) | ||
|
|
||
| @property | ||
| def _innerborder(self) -> list[str | float] | None: | ||
| """ | ||
| Inner border of the box, formatted as a list of 1-2 values, or None. | ||
| """ | ||
| return [v for v in (self.inner_gap, self.inner_pen) if v is not None] or None | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @property | ||
| def _shading(self) -> list[str | float] | None: | ||
| """ | ||
| Shading for the box, formatted as a list of 1-3 values, or None. | ||
| """ | ||
| _shading_offset = self.shading_offset or [] | ||
| return [ | ||
| v for v in (*_shading_offset, self.shading_fill) if v is not None | ||
| ] or None | ||
|
|
||
| @property | ||
| def _aliases(self): | ||
| """ | ||
| Aliases for the parameter. | ||
| """ | ||
| return [ | ||
| Alias(self.clearance, prefix="+c", sep="/", size=(2, 4)), | ||
| Alias(self.fill, prefix="+g"), | ||
| Alias(self._innerborder, prefix="+i", sep="/", size=(1, 2)), | ||
| Alias(self.pen, prefix="+p"), | ||
| Alias(self.radius, prefix="+r"), | ||
| Alias(self._shading, prefix="+s", sep="/", size=(1, 2, 3)), | ||
| ] | ||
Uh oh!
There was an error while loading. Please reload this page.