Skip to content
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

Refactor Figure.meca by adding the private _FocalMechanismConvention class #3551

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 23, 2024

Functions convention_code, convention_name, convention_params are defined in meca.py and they can also be used when wrapping coupe (#2019). So it's better to move these shared functions into pygmt/src/_common.py as proposed in #3357.

Instead of simply moving the functions, this PR adds the private _FocalMechanismConvention class which does the same thing as the three functions. With this new class, the Figure.meca source codes can be simplified.

It's likely the Figure.meca method can be further simplified, but will leave it to future PRs to make this PR small for reviewing.

@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Oct 23, 2024
@seisman seisman added this to the 0.14.0 milestone Oct 23, 2024
@seisman seisman force-pushed the refactor/meca-convention branch from af5ba53 to c67368c Compare October 23, 2024 13:02
@seisman seisman force-pushed the refactor/meca-convention branch from c67368c to cc554e9 Compare October 23, 2024 13:08
@seisman seisman added the run/benchmark Trigger the benchmark workflow in PRs label Oct 23, 2024
Comment on lines 196 to 215
if convention in self._conventions:
# Convention is given via 'convention' and 'component' parameters.
if component not in {"full", "deviatoric", "dc"}:
msg = (
f"Invalid component '{component}' for focal mechanism convention "
f"'{convention}'."
)
raise GMTInvalidInput(msg)

self.convention = convention
self.code = self._conventions[convention]
if isinstance(self.code, dict):
self.code = self.code[component]
elif convention in self._codes:
# Convention is given as a single-letter code.
self.code = convention
self.convention = self._codes[convention]
else:
msg = f"Invalid focal mechanism convention '{convention}'."
raise GMTInvalidInput(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if some of the pattern matching on 'convention' long names (e.g. akl) or single-letter aliases (e.g. a) could be simplified using StrEnum, but not sure how to handle the extra logic around 'component's. Haven't looked into it too closely, but there's supposedly ways to extend Enums with extra methods - https://realpython.com/python-enum/#adding-and-tweaking-member-methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using StrEnum can simplify the code logic. The _FocalMechanismConvention class has been refactored in c59b8a3.

@seisman seisman removed the needs review This PR has higher priority and needs review. label Oct 25, 2024
@seisman seisman marked this pull request as draft October 25, 2024 03:16
@seisman seisman self-assigned this Oct 30, 2024
@seisman seisman modified the milestones: 0.14.0, 0.15.0 Dec 17, 2024
@seisman seisman changed the title Add private _FocalMechanismConvention class for handling focal mechanism conventions and refactor Figure.meca Refactor Figure.meca by adding the private _FocalMechanismConvention class Dec 20, 2024
@seisman seisman marked this pull request as ready for review December 20, 2024 16:15
Copy link

codspeed-hq bot commented Dec 21, 2024

CodSpeed Performance Report

Merging #3551 will not alter performance

Comparing refactor/meca-convention (ebe7f38) with main (e0f8e84)

Summary

✅ 104 untouched benchmarks

@seisman seisman added this to the 0.15.0 milestone Jan 4, 2025
@seisman seisman added the needs review This PR has higher priority and needs review. label Jan 4, 2025
pygmt/src/_common.py Outdated Show resolved Hide resolved
pygmt/src/_common.py Outdated Show resolved Hide resolved
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature needs review This PR has higher priority and needs review. run/benchmark Trigger the benchmark workflow in PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants