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 behaviour of dtype on equivalent sources #516

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Next Next commit
Change behaviour of dtype on equivalent sources
Apply `dtype` argument only to the jacobian matrix and the predictions
for equivalent sources. Don't use the `dtype` for casting coordinates
and location of the sources.
  • Loading branch information
santisoler committed Jun 18, 2024
commit b2c6a1793afb241984e6a63b679d10018cce9571
17 changes: 4 additions & 13 deletions harmonica/_equivalent_sources/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class EquivalentSources(vdb.BaseGridder):
If True any predictions and Jacobian building is carried out in
parallel through Numba's ``jit.prange``, reducing the computation time.
If False, these tasks will be run on a single CPU. Default to True.
dtype : data-type
dtype : dtype, optional
The desired data-type for the predictions and the Jacobian matrix.
Default to ``"float64"``.

Expand Down Expand Up @@ -215,21 +215,14 @@ def fit(self, coordinates, data, weights=None):
Returns this estimator instance for chaining operations.
"""
coordinates, data, weights = vdb.check_fit_input(coordinates, data, weights)
coordinates, data, weights = cast_fit_input(
coordinates, data, weights, self.dtype
)
# Capture the data region to use as a default when gridding.
self.region_ = vd.get_region(coordinates[:2])
coordinates = vdb.n_1d_arrays(coordinates, 3)
if self.points is None:
self.points_ = tuple(
p.astype(self.dtype) for p in self._build_points(coordinates)
)
self.points_ = self._build_points(coordinates)
else:
self.depth_ = None # set depth_ to None so we don't leave it unset
self.points_ = tuple(
p.astype(self.dtype) for p in vdb.n_1d_arrays(self.points, 3)
)
self.points_ = vdb.n_1d_arrays(self.points, 3)
jacobian = self.jacobian(coordinates, self.points_)
self.coefs_ = vdb.least_squares(jacobian, data, weights, self.damping)
return self
Expand Down Expand Up @@ -326,9 +319,7 @@ def predict(self, coordinates):
check_is_fitted(self, ["coefs_"])
shape = np.broadcast(*coordinates[:3]).shape
size = np.broadcast(*coordinates[:3]).size
coordinates = tuple(
np.atleast_1d(i.astype(self.dtype)).ravel() for i in coordinates[:3]
)
coordinates = tuple(np.atleast_1d(i).ravel() for i in coordinates[:3])
data = np.zeros(size, dtype=self.dtype)
self._predict_kernel[self.parallel](
coordinates, self.points_, self.coefs_, data, self.greens_function
Expand Down
13 changes: 3 additions & 10 deletions harmonica/_equivalent_sources/gradient_boosted.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EquivalentSourcesGB(EquivalentSources):
If True any predictions and Jacobian building is carried out in
parallel through Numba's ``jit.prange``, reducing the computation time.
If False, these tasks will be run on a single CPU. Default to True.
dtype : data-type
dtype : dtype, optional
The desired data-type for the predictions and the Jacobian matrix.
Default to ``"float64"``.

Expand Down Expand Up @@ -205,9 +205,6 @@ def fit(self, coordinates, data, weights=None):
Returns this estimator instance for chaining operations.
"""
coordinates, data, weights = vdb.check_fit_input(coordinates, data, weights)
coordinates, data, weights = cast_fit_input(
coordinates, data, weights, self.dtype
)
# Capture the data region to use as a default when gridding.
self.region_ = get_region(coordinates[:2])
# Ravel coordinates, data and weights to 1d-arrays
Expand All @@ -217,13 +214,9 @@ def fit(self, coordinates, data, weights=None):
weights = weights.ravel()
# Build point sources
if self.points is None:
self.points_ = tuple(
p.astype(self.dtype) for p in self._build_points(coordinates)
)
self.points_ = self._build_points(coordinates)
else:
self.points_ = tuple(
p.astype(self.dtype) for p in vdb.n_1d_arrays(self.points, 3)
)
self.points_ = vdb.n_1d_arrays(self.points, 3)
# Initialize coefficients
self.coefs_ = np.zeros_like(self.points_[0])
# Fit coefficients through gradient boosting
Expand Down
Loading