-
Notifications
You must be signed in to change notification settings - Fork 255
Wrap fitcircle #1550
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
base: main
Are you sure you want to change the base?
Wrap fitcircle #1550
Changes from all commits
7c1ae68
41a3ded
7e2dfee
a239841
d526eab
f02466e
0355a7d
9bf8331
0360abc
1e86759
e5b7a91
3cde6d8
c199d09
899a93f
d131d2f
3e57da6
d4eebb7
913a70b
f0033fa
d41d90e
0c11f2c
a6f6265
8ab458a
bbbb84d
00ffffd
af7c4f9
8803d43
d166335
9227dbe
a10ed09
d75863d
bfa8fa6
fa0aa4b
e885b54
c890477
bc1017a
ea1646d
8bcc2ff
68a12b0
7cb916c
6f0e501
0e196f8
eff642e
4cd6702
ff4c77c
3be5bac
5fdfede
a6777d8
6bd302a
cdf4ef9
59afc1f
ec5deec
c0d0423
696a96d
5856e64
dd714ee
961ad92
efbe10d
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 |
|---|---|---|
|
|
@@ -129,6 +129,7 @@ Operations on tabular data | |
| blockmedian | ||
| blockmode | ||
| filter1d | ||
| fitcircle | ||
| nearneighbor | ||
| project | ||
| select | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| config, | ||
| dimfilter, | ||
| filter1d, | ||
| fitcircle, | ||
| grd2cpt, | ||
| grd2xyz, | ||
| grdclip, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,142 @@ | ||||||
| """ | ||||||
| fitcircle - Find mean position and great or small circle fit to points on sphere. | ||||||
| """ | ||||||
|
|
||||||
| from typing import Literal | ||||||
|
|
||||||
| from pygmt._typing import PathLike, TableLike | ||||||
| from pygmt.alias import Alias, AliasSystem | ||||||
| from pygmt.clib import Session | ||||||
| from pygmt.helpers import build_arg_list, fmt_docstring | ||||||
| from pygmt.helpers.utils import is_given | ||||||
|
|
||||||
|
|
||||||
| @fmt_docstring | ||||||
| def fitcircle( | ||||||
| data: PathLike | TableLike | None = None, | ||||||
| x=None, | ||||||
| y=None, | ||||||
| norm: Literal["absolutes", "squares"] = "squares", | ||||||
| small_circle: bool | float = False, | ||||||
| verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] | ||||||
| | bool = False, | ||||||
| **kwargs, | ||||||
| ) -> dict[str, tuple[float, float] | float]: | ||||||
| """ | ||||||
| Find mean position and great or small circle fit to points on sphere. | ||||||
|
|
||||||
| This method takes (longitude, latitude) values and converts them to Cartesian | ||||||
| three-vectors on the unit sphere. Then two locations are found: the mean | ||||||
| of the input positions, and the pole to the great circle which best fits | ||||||
| the input positions. | ||||||
|
|
||||||
| Setting ``norm`` to ``"absolutes"`` approximates the minimization of the | ||||||
| sum of absolute values of cosines of angular distances. This solution | ||||||
| finds the mean position as the Fisher average of the data, and the pole | ||||||
| position as the Fisher average of the cross-products between the mean | ||||||
| and the data. Averaging cross-products gives weight to points in | ||||||
| proportion to their distance from the mean, analogous to the "leverage" | ||||||
| of distant points in linear regression in the plane. | ||||||
|
|
||||||
| Setting ``norm`` to ``"squares"`` approximates the minimization of the | ||||||
| sum of squares of cosines of angular distances. It creates a 3 by 3 | ||||||
| matrix of sums of squares of components of the data vectors. The | ||||||
| eigenvectors of this matrix give the mean and pole locations. This | ||||||
| method may be more subject to roundoff errors when there are thousands | ||||||
| of data. The pole is given by the eigenvector corresponding to the | ||||||
| smallest eigenvalue; it is the least-well represented factor in the data | ||||||
| and is not easily estimated by either method. | ||||||
|
Comment on lines
+33
to
+48
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm re-reading the GMT documentation and feel that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Truth be told, I don't know how |
||||||
|
|
||||||
| When the data are closely grouped along a great circle both solutions | ||||||
| are similar. If the data have large dispersion, the pole to the great | ||||||
| circle will be less well determined than the mean. Compare both | ||||||
| solutions as a qualitative check by calling :func:`pygmt.fitcircle` | ||||||
| twice, once for each ``norm``. | ||||||
|
|
||||||
| Takes a matrix, (x, y) pairs, or a file name as input. | ||||||
|
|
||||||
| Must provide either ``data`` or ``x`` and ``y``. | ||||||
|
|
||||||
| Full GMT docs at :gmt-docs:`fitcircle.html`. | ||||||
|
|
||||||
| **Aliases:** | ||||||
|
|
||||||
| .. hlist:: | ||||||
| :columns: 3 | ||||||
|
|
||||||
| - L = norm | ||||||
| - S = small_circle | ||||||
| - V = verbose | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| data | ||||||
| Pass in (longitude, latitude) values by providing a file name to an ASCII data | ||||||
| table, a 2-D $table_classes. | ||||||
| x/y : 1-D arrays | ||||||
| Arrays of x and y coordinates of the data points. | ||||||
| norm | ||||||
| Specify the desired norm, either ``"absolutes"`` or ``"squares"`` | ||||||
| [Default is ``"squares"``]. | ||||||
| small_circle | ||||||
| Attempt to fit a small circle instead of a great circle. The pole will be | ||||||
| constrained to lie on the great circle connecting the pole of the best-fit great | ||||||
| circle and the mean location of the data. Optionally set the desired fixed | ||||||
| latitude of the small circle [Default will determine the optimal latitude]. | ||||||
| $verbose | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| ret | ||||||
| A dictionary with the following keys, each mapping to a | ||||||
| ``(longitude, latitude)`` tuple: | ||||||
|
|
||||||
| - ``"flat_mean"``: the flat Earth mean position | ||||||
| - ``"mean"``: the mean position (Fisher or eigenvalue method, | ||||||
| depending on ``norm``) | ||||||
| - ``"north_pole"``: the north hemisphere great circle pole | ||||||
| - ``"south_pole"``: the south hemisphere great circle pole | ||||||
|
|
||||||
| If ``small_circle`` is set, two more keys are added: | ||||||
|
|
||||||
| - ``"small_circle_pole"``: the small circle pole | ||||||
| - ``"small_circle_distance"``: the colatitude/distance in degrees | ||||||
| from the small circle pole to the small circle (a ``float``, not a | ||||||
| tuple) | ||||||
| """ | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment https://github.com/GenericMappingTools/pygmt/pull/1550/changes#r3829465312. Need to check if |
||||||
| aliasdict = AliasSystem( | ||||||
| L=Alias(norm, name="norm", mapping={"absolutes": 1, "squares": 2}), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment https://github.com/GenericMappingTools/pygmt/pull/1550/changes#r3829465312
Suggested change
|
||||||
| S=Alias(small_circle, name="small_circle"), | ||||||
| ).add_common( | ||||||
| V=verbose, | ||||||
| ) | ||||||
| aliasdict.merge(kwargs) | ||||||
|
|
||||||
| # "c" (small-circle pole and colatitude) is only valid with -S; GMT errors | ||||||
| # ("Cannot select c without setting -S") if "c" is requested without it. | ||||||
| aliasdict["F"] = "fmnsc" if is_given(small_circle) else "fmns" | ||||||
|
|
||||||
| with Session() as lib: | ||||||
| with ( | ||||||
| lib.virtualfile_in( | ||||||
| check_kind="vector", data=data, x=x, y=y, mincols=2 | ||||||
| ) as vintbl, | ||||||
| lib.virtualfile_out(kind="dataset") as vouttbl, | ||||||
| ): | ||||||
| lib.call_module( | ||||||
| module="fitcircle", | ||||||
| args=build_arg_list(aliasdict, infile=vintbl, outfile=vouttbl), | ||||||
| ) | ||||||
| row = lib.virtualfile_to_dataset(vfname=vouttbl, output_type="numpy")[0] | ||||||
| values = [float(value) for value in row] | ||||||
|
|
||||||
| solution: dict[str, tuple[float, float] | float] = { | ||||||
| "flat_mean": (values[0], values[1]), | ||||||
| "mean": (values[2], values[3]), | ||||||
| "north_pole": (values[4], values[5]), | ||||||
| "south_pole": (values[6], values[7]), | ||||||
| } | ||||||
| if is_given(small_circle): | ||||||
| solution["small_circle_pole"] = (values[8], values[9]) | ||||||
| solution["small_circle_distance"] = values[10] | ||||||
| return solution | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| Test pygmt.fitcircle. | ||
| """ | ||
|
|
||
| import numpy.testing as npt | ||
| import pandas as pd | ||
| import pytest | ||
| from pygmt import fitcircle | ||
| from pygmt.src import which | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", name="data") | ||
| def fixture_data(): | ||
| """ | ||
| Load the sample data from the @sat_03 remote file. | ||
| """ | ||
| fname = which("@sat_03.txt", download="c") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| return pd.read_csv( | ||
| fname, header=None, skiprows=1, sep="\t", names=["longitude", "latitude", "z"] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| def test_fitcircle_absolutes(data): | ||
| """ | ||
| Test fitcircle with norm="absolutes". | ||
| """ | ||
| result = fitcircle(data=data, norm="absolutes") | ||
| assert isinstance(result, dict) | ||
| assert set(result.keys()) == {"flat_mean", "mean", "north_pole", "south_pole"} | ||
| npt.assert_allclose(result["flat_mean"], (330.243649573, -18.3910128205)) | ||
| npt.assert_allclose(result["mean"], (330.16313328, -18.4067771888)) | ||
| npt.assert_allclose(result["north_pole"], (52.7434273422, 21.2085369093)) | ||
| npt.assert_allclose(result["south_pole"], (232.743427342, -21.2085369093)) | ||
|
|
||
|
|
||
| def test_fitcircle_squares(data): | ||
| """ | ||
| Test fitcircle with norm="squares", which is also the default. | ||
| """ | ||
| result = fitcircle(data=data, norm="squares") | ||
| assert isinstance(result, dict) | ||
| assert set(result.keys()) == {"flat_mean", "mean", "north_pole", "south_pole"} | ||
| npt.assert_allclose(result["flat_mean"], (330.243649573, -18.3910128205)) | ||
| npt.assert_allclose(result["mean"], (330.163207808, -18.4067882988)) | ||
| npt.assert_allclose(result["north_pole"], (52.7449849947, 21.2046833116)) | ||
| npt.assert_allclose(result["south_pole"], (232.744984995, -21.2046833116)) | ||
| assert fitcircle(data=data) == result # norm="squares" is the default | ||
|
|
||
|
|
||
| def test_fitcircle_small_circle(data): | ||
| """ | ||
| Test that fitcircle can fit a small circle instead of a great circle, and | ||
| that the returned dict includes the small-circle keys. | ||
| """ | ||
| result = fitcircle(data=data, norm="squares", small_circle=True) | ||
| assert isinstance(result, dict) | ||
| assert set(result.keys()) == { | ||
| "flat_mean", | ||
| "mean", | ||
| "north_pole", | ||
| "south_pole", | ||
| "small_circle_pole", | ||
| "small_circle_distance", | ||
| } | ||
| npt.assert_allclose(result["small_circle_distance"], 87.6072781238) | ||
|
|
||
|
|
||
| def test_fitcircle_input_xy(data): | ||
| """ | ||
| Run fitcircle by passing in x/y as input. | ||
| """ | ||
| result = fitcircle(x=data.longitude, y=data.latitude, norm="absolutes") | ||
| assert isinstance(result, dict) | ||
| npt.assert_allclose(result["flat_mean"], (330.243649573, -18.3910128205)) | ||
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.
Please move this file to Line 130.