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

Change default for window size in EquivalentSourcesGB #487

Merged
merged 15 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Raise warning for data points less than or equal to 5e3.
Update test to check for data points less than 5e3 and the associated warning.
  • Loading branch information
indiauppal committed May 7, 2024
commit 3f0beeaf4c0481849df0d4551d35e48074fb51fa
11 changes: 9 additions & 2 deletions harmonica/_equivalent_sources/gradient_boosted.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .cartesian import EquivalentSources
from .utils import cast_fit_input, predict_numba_parallel
import warnings


class EquivalentSourcesGB(EquivalentSources):
Expand Down Expand Up @@ -275,7 +276,7 @@ def _create_windows(self, coordinates, shuffle=True):
coordinates : tuple
Arrays with the coordinates of each data point. Should be in the
following order: (``easting``, ``northing``, ``upward``).
shuffle_windows : bool
shuffle : bool
Enable or disable the random shuffling of windows order. It's is
highly recommended to enable shuffling for better fitting results.
This argument is mainly included for testing purposes. Default to
Expand All @@ -290,7 +291,7 @@ def _create_windows(self, coordinates, shuffle=True):
the same as the one in ``data_windows_nonempty``.
data_windows_nonempty : list
List containing arrays with the indices of the data points that
under each window. The order of the windows is randomly shuffled if
fall under each window. The order of the windows is randomly shuffled if
``shuffle_windows`` is True, although the order of the windows is
the same as the one in ``source_windows_nonempty``.
"""
Expand All @@ -302,6 +303,12 @@ def _create_windows(self, coordinates, shuffle=True):
if self.window_size == "default":
area = (region[1] - region[0]) * (region[3] - region[2])
ndata = coordinates[0].size
if ndata <= 5e3:
warnings.warn(f"Found {ndata} number of coordinates (<= 5e3). Only one window will be used.")
source_windows_nonempty = [np.arange(self.points_[0].size)]
data_windows_nonempty = [np.arange(ndata)]
self.window_size_ = None
return source_windows_nonempty, data_windows_nonempty
points_per_m2 = ndata / area
window_area = 5e3 / points_per_m2
self.window_size_ = np.sqrt(window_area)
Expand Down
6 changes: 3 additions & 3 deletions harmonica/tests/test_gradient_boosted_eqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ def test_window_size_less_than_5000():
eqs.points_ = eqs._build_points(
grid_coords
) # need to build sources first before creating windows.
eqs._create_windows(grid_coords)
expected_window_size = np.sqrt(5e3 / (64**2 / 10e3**2))
npt.assert_allclose(eqs.window_size_, expected_window_size)
with pytest.warns(UserWarning, match=f"Found {64**2} number of coordinates"):
eqs._create_windows(grid_coords)
assert eqs.window_size_ is None

def test_window_size():
region = (0, 10e3, -5e3, 5e3)
Expand Down
Loading