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 meca to use virtualfile_from_data #1613

Closed
wants to merge 5 commits into from

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Nov 7, 2021

Description of proposed changes

Part 1/3 of meca refactor as mentioned at #1129 (comment). Focus of this Pull Request is to clean up the pygmt.meca code and add some unit tests so that more refactorings can take place later to fix bugs. The meca module could use a lot of refactoring in general, so feel free to suggest changes if it simplifies/improves the code!

Notes:

  1. This Pull Request will neither resolve the offset bug in pygmt.Figure.meca offset not working #1129 nor allow beachballs to be plotted with string labels, c.f. Wrap meca #516 (comment). Those will be done in a separate PR to keep this one small.
  2. I'm not a seismologist, so don't trust what I'm doing 😜

Part of #949. Towards resolving the bug at #1129 and enhancement request at #1466.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

Need to ensure that the spec numpy.array is a 2D
matrix for now, so that test_meca_spec_dictionary
and test_meca_spec_1d_array works.
@weiji14 weiji14 added the enhancement Improving an existing feature label Nov 7, 2021
@weiji14 weiji14 added this to the 0.6.0 milestone Nov 7, 2021
@weiji14 weiji14 added this to In progress in Release v0.6.x via automation Nov 7, 2021
pygmt/src/meca.py Outdated Show resolved Hide resolved
Remove huge chunk of if-then code block by
converting Python dict to pandas.DataFrame,
and handle the spec formatting using one route
only.
Comment on lines -396 to +338
# or assemble the array for the case of pd.DataFrames
elif isinstance(dict_type_pointer, np.ndarray):
# Convert single int, float data to List[int, float] data
_spec = {
"longitude": np.atleast_1d(longitude),
"latitude": np.atleast_1d(latitude),
"depth": np.atleast_1d(depth),
}
_spec.update({key: np.atleast_1d(val) for key, val in spec.items()})
spec = pd.DataFrame.from_dict(_spec)

assert isinstance(spec, pd.DataFrame)
dict_type_pointer = spec.values

# Assemble the array for the case of pd.DataFrames
if isinstance(dict_type_pointer, np.ndarray):
Copy link
Member Author

Choose a reason for hiding this comment

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

Just to explain this change (clearer if you look at commit e223057), previously the spec could be formatted in 3 different paths depending on the type:

  1. if isinstance(dict_type_pointer, (int, float)): (i.e. a dictionary of int/float like {"depth": 0}
  2. elif isinstance(dict_type_pointer, list): (i.e. a dictionary of lists like {"depth": [0, 1, 2]}
  3. elif isinstance(dict_type_pointer, np.ndarray): (i.e. a pandas.DataFrame with np.ndarray columns)

There was a lot of duplication in these 3 code paths, so I've removed paths 1 and 2 by converting any spec provided by the user in dictionary format into pandas.DataFrame. So now, everything happens via path 3 only.

Another benefit of handling everything using pandas.DataFrame is that columns can have different data types, so this sets things up for the ability to pass in string labels later in a follow up PR.

@weiji14 weiji14 changed the title WIP Refactor meca to use virtualfile_from_data Refactor meca to use virtualfile_from_data Nov 8, 2021
@@ -247,6 +240,29 @@ def update_pointers(data_pointers):
):
Copy link
Member

Choose a reason for hiding this comment

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

Lines 237-241 are unnecessary because line 236 already checks spec is dict or pd.DataFrame. Am I right?

Copy link
Member Author

Choose a reason for hiding this comment

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

You might be right, let me recheck the logic.

Copy link
Member

Choose a reason for hiding this comment

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

@weiji14 Please see my new PR #1784.

@weiji14 weiji14 modified the milestones: 0.6.0, 0.6.1 Mar 13, 2022
@seisman seisman modified the milestones: 0.6.1, 0.7.0 Mar 18, 2022
@weiji14 weiji14 removed this from In progress in Release v0.6.x May 5, 2022
@weiji14 weiji14 added this to In progress in Release v0.7.x via automation May 5, 2022
@weiji14
Copy link
Member Author

weiji14 commented Jun 5, 2022

Closing this in favour of #1784.

@weiji14 weiji14 closed this Jun 5, 2022
Release v0.7.x automation moved this from In progress to Done Jun 5, 2022
@weiji14 weiji14 removed this from Done in Release v0.7.x Jun 5, 2022
@weiji14 weiji14 removed this from the 0.7.0 milestone Jun 5, 2022
@weiji14 weiji14 deleted the virtualfile_from_data/meca branch June 6, 2022 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants