Skip to content

Figure.meca: Add the private _FocalMechanismConvention class to simplify codes #3551

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 23 commits into from
Feb 19, 2025

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 (2f0f7fd) with main (699d64b)

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
Co-authored-by: Michael Grund <[email protected]>
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Feb 4, 2025
@seisman seisman added needs review This PR has higher priority and needs review. and removed final review call This PR requires final review and approval from a second reviewer labels Feb 10, 2025
@seisman seisman changed the title Refactor Figure.meca by adding the private _FocalMechanismConvention class Figure.meca: Add the private _FocalMechanismConvention class to simplify codes Feb 18, 2025
@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog final review call This PR requires final review and approval from a second reviewer and removed run/benchmark Trigger the benchmark workflow in PRs enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Feb 18, 2025
Copy link
Member

@yvonnefroehlich yvonnefroehlich left a comment

Choose a reason for hiding this comment

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

Just some minor formulation and highlighting suggestions.

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Feb 19, 2025
@seisman seisman merged commit 8e1200f into main Feb 19, 2025
23 checks passed
@seisman seisman deleted the refactor/meca-convention branch February 19, 2025 11:32
@jhtong33 jhtong33 mentioned this pull request Feb 21, 2025
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants