Skip to content

Commit

Permalink
refactored circular model
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMolinsky committed Aug 22, 2022
1 parent 0be5380 commit be261db
Show file tree
Hide file tree
Showing 17 changed files with 7,412 additions and 7,218 deletions.
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion pyinterpolate/distance/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _calculate_block_to_block_distance(block_1: np.ndarray, block_2: np.ndarray)
return distances_sum


def calc_point_to_point_distance(points_a, points_b=None):
def calc_point_to_point_distance(points_a, points_b=None, allow_normalization=False):
"""Function calculates distances between two group of points of a single group to itself.
Parameters
Expand Down
88 changes: 61 additions & 27 deletions pyinterpolate/kriging/point_kriging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import os
import numpy as np
import dask
from dask.diagnostics import ProgressBar
pbar = ProgressBar()
pbar.register()

from tqdm import tqdm

from pyinterpolate.kriging.models.ordinary_kriging import ordinary_kriging
from pyinterpolate.kriging.models.simple_kriging import simple_kriging
Expand Down Expand Up @@ -91,30 +96,59 @@ def kriging(observations: np.ndarray,

results = []

for point in points:
prediction = [np.nan, np.nan, np.nan, np.nan]
if how == 'ok':
prediction = dask.delayed(model)(
theoretical_model,
observations,
point,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
elif how == 'sk':
prediction = dask.delayed(model)(
theoretical_model,
observations,
point,
sk_mean,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
results.append(prediction)
predictions = dask.delayed()(results)
predictions = np.array(predictions.compute(num_workers=number_of_workers))
return np.array(predictions)
if number_of_workers == 1:
# Don't use dask
for point in tqdm(points):
prediction = [np.nan, np.nan, np.nan, np.nan]
if how == 'ok':
prediction = model(
theoretical_model,
observations,
point,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
elif how == 'sk':
prediction = model(
theoretical_model,
observations,
point,
sk_mean,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
results.append(prediction)
predictions = np.array(results)
else:
# Use dask
for point in points:
prediction = [np.nan, np.nan, np.nan, np.nan]
if how == 'ok':
prediction = dask.delayed(model)(
theoretical_model,
observations,
point,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
elif how == 'sk':
prediction = dask.delayed(model)(
theoretical_model,
observations,
point,
sk_mean,
neighbors_range=neighbors_range,
min_no_neighbors=min_no_neighbors,
max_no_neighbors=max_no_neighbors,
allow_approximate_solutions=allow_approx_solutions
)
results.append(prediction)
predictions = dask.delayed()(results)
predictions = np.array(predictions.compute(num_workers=number_of_workers))
return predictions
6 changes: 0 additions & 6 deletions pyinterpolate/kriging/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ def get_predictions(theoretical_model: TheoreticalVariogram,
Returns
-------
: List[predictions - unknown point, predictions - point to point, prepared Kriging data]
Raises
------
VariogramModelNotSetError : Semivariogram model has not been set (it doesn't have a name)
"""
# Check if variogram model is valid
validate_theoretical_variogram(theoretical_model)

# Check range
if neighbors_range is None:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 13 additions & 11 deletions pyinterpolate/test/profile/kriging/point/profile_kriging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
from pyinterpolate.variogram.theoretical.semivariogram import TheoreticalVariogram


points = read_txt('../../samples/pl_dem.txt')
points = read_txt('../../samples/pl_dem_epsg2180.txt')
pts_size = len(points)
train_size = int(pts_size / 2)
train_size = int(pts_size * 0.5)
idxs = np.arange(0, pts_size)
train_idxs = np.random.choice(idxs, size=train_size, replace=False)

train_set = points[train_idxs]
test_set = points[~train_idxs]
test_points = test_set[:, :-1]

ex = build_experimental_variogram(train_set, step_size=0.2, max_range=4)
ex = build_experimental_variogram(train_set, step_size=1_000, max_range=10_000)
tv = TheoreticalVariogram()
tv.autofit(experimental_variogram=ex)
tv.autofit(experimental_variogram=ex, model_types=['circular'])


def krigeme():
_ = kriging(observations=train_set,
theoretical_model=tv,
points=test_points,
min_no_neighbors=4,
number_of_workers=4)
return 0
predictions = kriging(observations=train_set,
theoretical_model=tv,
points=test_points,
min_no_neighbors=4,
number_of_workers=1)
mse = np.mean((predictions[:, 0] - test_points[:, -1])**2)
rmse = np.sqrt(mse)
return rmse


if __name__ == '__main__':
cProfile.run('krigeme()', filename='kmultprofile_v0.3.0.profile')
cProfile.run('krigeme()', filename='kmultprofile_v0.3.0.clean.profile')
Loading

0 comments on commit be261db

Please sign in to comment.