-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore Cython code and make Rust impl. optional
Now, the GSTools-Core is used, if the package can be imported, but it can also be switched off, by setting the global var. `gstools.config.USE_RUST=False` during the runtime.
- Loading branch information
Showing
13 changed files
with
712 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,6 +127,7 @@ | |
""" | ||
# Hooray! | ||
from gstools import ( | ||
config, | ||
covmodel, | ||
field, | ||
krige, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
GStools subpackage providing global variables. | ||
.. currentmodule:: gstools.config | ||
""" | ||
# pylint: disable=W0611 | ||
try: | ||
import gstools_core | ||
|
||
USE_RUST = True | ||
except ImportError: | ||
USE_RUST = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This is the randomization method summator, implemented in cython. | ||
""" | ||
|
||
import numpy as np | ||
|
||
cimport cython | ||
|
||
from cython.parallel import prange | ||
|
||
cimport numpy as np | ||
from libc.math cimport cos, sin | ||
|
||
|
||
def summate( | ||
const double[:, :] cov_samples, | ||
const double[:] z_1, | ||
const double[:] z_2, | ||
const double[:, :] pos | ||
): | ||
cdef int i, j, d | ||
cdef double phase | ||
cdef int dim = pos.shape[0] | ||
|
||
cdef int X_len = pos.shape[1] | ||
cdef int N = cov_samples.shape[1] | ||
|
||
cdef double[:] summed_modes = np.zeros(X_len, dtype=float) | ||
|
||
for i in prange(X_len, nogil=True): | ||
for j in range(N): | ||
phase = 0. | ||
for d in range(dim): | ||
phase += cov_samples[d, j] * pos[d, i] | ||
summed_modes[i] += z_1[j] * cos(phase) + z_2[j] * sin(phase) | ||
|
||
return np.asarray(summed_modes) | ||
|
||
|
||
cdef (double) abs_square(const double[:] vec) nogil: | ||
cdef int i | ||
cdef double r = 0. | ||
|
||
for i in range(vec.shape[0]): | ||
r += vec[i]**2 | ||
|
||
return r | ||
|
||
|
||
def summate_incompr( | ||
const double[:, :] cov_samples, | ||
const double[:] z_1, | ||
const double[:] z_2, | ||
const double[:, :] pos | ||
): | ||
cdef int i, j, d | ||
cdef double phase | ||
cdef double k_2 | ||
cdef int dim = pos.shape[0] | ||
|
||
cdef double[:] e1 = np.zeros(dim, dtype=float) | ||
e1[0] = 1. | ||
cdef double[:] proj = np.empty(dim) | ||
|
||
cdef int X_len = pos.shape[1] | ||
cdef int N = cov_samples.shape[1] | ||
|
||
cdef double[:, :] summed_modes = np.zeros((dim, X_len), dtype=float) | ||
|
||
for i in range(X_len): | ||
for j in range(N): | ||
k_2 = abs_square(cov_samples[:, j]) | ||
phase = 0. | ||
for d in range(dim): | ||
phase += cov_samples[d, j] * pos[d, i] | ||
for d in range(dim): | ||
proj[d] = e1[d] - cov_samples[d, j] * cov_samples[0, j] / k_2 | ||
summed_modes[d, i] += proj[d] * (z_1[j] * cos(phase) + z_2[j] * sin(phase)) | ||
|
||
return np.asarray(summed_modes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This is a summator for the kriging routines | ||
""" | ||
|
||
import numpy as np | ||
|
||
cimport cython | ||
from cython.parallel import prange | ||
cimport numpy as np | ||
|
||
|
||
def calc_field_krige_and_variance( | ||
const double[:, :] krig_mat, | ||
const double[:, :] krig_vecs, | ||
const double[:] cond | ||
): | ||
|
||
cdef int mat_i = krig_mat.shape[0] | ||
cdef int res_i = krig_vecs.shape[1] | ||
|
||
cdef double[:] field = np.zeros(res_i) | ||
cdef double[:] error = np.zeros(res_i) | ||
cdef double krig_fac | ||
|
||
cdef int i, j, k | ||
|
||
# error = krig_vecs * krig_mat * krig_vecs | ||
# field = cond * krig_mat * krig_vecs | ||
for k in prange(res_i, nogil=True): | ||
for i in range(mat_i): | ||
krig_fac = 0.0 | ||
for j in range(mat_i): | ||
krig_fac += krig_mat[i, j] * krig_vecs[j, k] | ||
error[k] += krig_vecs[i, k] * krig_fac | ||
field[k] += cond[i] * krig_fac | ||
|
||
return np.asarray(field), np.asarray(error) | ||
|
||
|
||
def calc_field_krige( | ||
const double[:, :] krig_mat, | ||
const double[:, :] krig_vecs, | ||
const double[:] cond | ||
): | ||
|
||
cdef int mat_i = krig_mat.shape[0] | ||
cdef int res_i = krig_vecs.shape[1] | ||
|
||
cdef double[:] field = np.zeros(res_i) | ||
cdef double krig_fac | ||
|
||
cdef int i, j, k | ||
|
||
# field = cond * krig_mat * krig_vecs | ||
for k in prange(res_i, nogil=True): | ||
for i in range(mat_i): | ||
krig_fac = 0.0 | ||
for j in range(mat_i): | ||
krig_fac += krig_mat[i, j] * krig_vecs[j, k] | ||
field[k] += cond[i] * krig_fac | ||
|
||
return np.asarray(field) |
Oops, something went wrong.