Skip to content

Commit

Permalink
Merge pull request #147 from Zncl2222/release/v1.2.3
Browse files Browse the repository at this point in the history
Release/v1.2.3
  • Loading branch information
Zncl2222 committed Jun 10, 2023
2 parents aa9808a + a10d858 commit 269995e
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ cbuild/
randomfield*
variogram*
.coverage
*.o
*.exe
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repos:
rev: v2.2.0
hooks:
- id: setup-cfg-fmt
args: [--include-version-classifiers]
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.3.0
hooks:
Expand Down
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ enable_testing()

project(c_example)
set(CMAKE_VERBOSE_MAKEFILE ON)
# set true to compile the executable file (win -> .exe, linux -> .out)
set(IS_EXECUTE true)

# Set the option to compile the executable file
option(IS_EXECUTE "Compile the executable file" ON)

# set output directory
if(IS_EXECUTE)
Expand Down Expand Up @@ -52,7 +53,6 @@ if(IS_EXECUTE)
endif()
endif()


# unittest
if(IS_EXECUTE)
project(unittest)
Expand All @@ -62,11 +62,11 @@ if(IS_EXECUTE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test)

set(CTEST_COVERAGE_EXCLUDE
'/usr/include/*' # Exclude system headers
'${CMAKE_CURRENT_SOURCE_DIR}/test/utest.h' # Exclude a specific file
'${CMAKE_CURRENT_SOURCE_DIR}/src/c_array.c'
'${CMAKE_CURRENT_SOURCE_DIR}/src/c_array_mt.c'
'${CMAKE_CURRENT_SOURCE_DIR}/include/c_array.h'
'/usr/include/*' # Exclude system headers
'${CMAKE_CURRENT_SOURCE_DIR}/test/utest.h' # Exclude a specific file
'${CMAKE_CURRENT_SOURCE_DIR}/src/c_array.c'
'${CMAKE_CURRENT_SOURCE_DIR}/src/c_array_mt.c'
'${CMAKE_CURRENT_SOURCE_DIR}/include/c_array.h'
)
add_custom_target(
coverage
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ if __name__ == '__main__':
cov_model = Gaussian(bw_l, bw_s, k_range, sill)

# Create simulation and input the Cov model
sgsim_py = uc.UCSgsim(x, cov_model, nR) # run sgsim with python
sgsim_c = uc.UCSgsimDLL(x, cov_model, nR) # run sgsim with c
sgsim_py = uc.UCSgsim(x, nR, cov_model) # run sgsim with python
sgsim_c = uc.UCSgsimDLL(x, nR, cov_model) # run sgsim with c

# Start compute with n CPUs
sgsim_c.compute(n_process=2, randomseed=randomseed)
Expand All @@ -124,7 +124,7 @@ if __name__ == '__main__':
sgsim_c.hist_plot(x_location=10) # Hist
sgsim_c.variogram_compute(n_process=2) # Compute variogram before plotting
# Plot variogram and mean variogram for validation
sgsim_c.vario_plot()
sgsim.variogram_plot()
# Save random_field and variogram
sgsim_c.save_random_field('randomfields.csv', save_single=True)
sgsim_c.save_variogram('variograms.csv', save_single=True)
Expand Down
26 changes: 25 additions & 1 deletion cmake_build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
#!/bin/bash

is_execute=true

# Parse command-line arguments
while getopts ":s" opt; do
case $opt in
s)
is_execute=false
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done

mkdir cmake_build
cd cmake_build
cmake ..

if [ "$is_execute" = true ]; then
echo "Compile the code into an executable file !"
cmake -DIS_EXECUTE=ON ..
else
echo "Compile the code into a shared library (dynamic library) !"
cmake -DIS_EXECUTE=OFF ..
fi

make
make test
make memcheck
6 changes: 3 additions & 3 deletions python_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
cov_model = Gaussian(bw_l, bw_s, k_range, sill)

# Create simulation and input the Cov model
# sgsim = uc.UCSgsim(x, cov_model, nR)
sgsim = uc.UCSgsimDLL(x, cov_model, nR)
# sgsim = uc.UCSgsim(x, nR, cov_model)
sgsim = uc.UCSgsimDLL(x, nR, cov_model)

# Start compute with n CPUs
sgsim.compute(n_process=2, randomseed=randomseed)
Expand All @@ -37,7 +37,7 @@
sgsim.hist_plot(x_location=10) # Hist
sgsim.variogram_compute(n_process=2) # Compute variogram before plotting
# Plot variogram and mean variogram for validation
sgsim.vario_plot()
sgsim.variogram_plot()
# Save random_field and variogram
sgsim.save_random_field('randomfields.csv', save_single=True)
sgsim.save_variogram('variograms.csv', save_single=True)
Expand Down
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = uc_sgsim
version = 1.2.2
version = 1.2.3
description = Random Field Generation
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -12,8 +12,12 @@ license_file = LICENSE.md
classifiers =
Development Status :: 5 - Production/Stable
License :: OSI Approved :: MIT License
Programming Language :: C
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Topic :: Scientific/Engineering :: Mathematics

[options]
Expand Down
20 changes: 10 additions & 10 deletions tests/uc_sgsim_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,71 +36,71 @@ def sgsim_save(self, sgsim: uc.UCSgsim) -> None:
sgsim.save_variogram('variogram/', save_single=False)

def test_uc_sgsim_gaussian_py_single_process(self):
sgsim = uc.UCSgsim(self.X, self.gaussian, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, self.gaussian)
sgsim.compute(n_process=1, randomseed=454)
sgsim.variogram_compute(n_process=1)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_gaussian_py_multi_process(self):
sgsim = uc.UCSgsim(self.X, self.gaussian, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, self.gaussian)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_gaussian_nugget_py(self):
model = Gaussian(35, 1, 17.32, 1, 0.01)
sgsim = uc.UCSgsim(self.X, model, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, model)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_spherical_py(self):
sgsim = uc.UCSgsim(self.X, self.spherical, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, self.spherical)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_spherical_nugget_py(self):
model = Spherical(35, 1, 17.32, 1, 0.01)
sgsim = uc.UCSgsim(self.X, model, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, model)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_exponential_py(self):
sgsim = uc.UCSgsim(self.X, self.exponential, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, self.exponential)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_exponential_nugget_py(self):
model = Exponential(35, 1, 17.32, 1, 0.01)
sgsim = uc.UCSgsim(self.X, model, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, model)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_gaussian_c(self):
sgsim = uc.UCSgsimDLL(self.X, self.gaussian, self.nR)
sgsim = uc.UCSgsimDLL(self.X, self.nR, self.gaussian)
sgsim.compute(n_process=2, randomseed=454)
sgsim.variogram_compute(n_process=2)
self.sgsim_plot(sgsim)
self.sgsim_save(sgsim)

def test_uc_sgsim_varigram_exception(self):
sgsim = uc.UCSgsimDLL(self.X, self.gaussian, self.nR)
sgsim = uc.UCSgsimDLL(self.X, self.nR, self.gaussian)
sgsim.compute(n_process=1, randomseed=454)
with pytest.raises(VariogramDoesNotCompute):
sgsim.variogram_plot()

sgsim = uc.UCSgsim(self.X, self.gaussian, self.nR)
sgsim = uc.UCSgsim(self.X, self.nR, self.gaussian)
sgsim.compute(n_process=1, randomseed=454)
with pytest.raises(VariogramDoesNotCompute):
sgsim.variogram_plot()
Expand Down
1 change: 0 additions & 1 deletion uc_sgsim/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions uc_sgsim/c_core/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions uc_sgsim/c_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ endif()
# Include header files
include_directories(include/)

# Set compiler optimization flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")

# Link each file
add_library(
uc_sgsim ${LIB_TYPE}
Expand Down
2 changes: 1 addition & 1 deletion uc_sgsim/c_core/src/krige.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void simple_kriging(double* array, sampling_state* _sampling, mt19937_state* rng
}

if (_sampling->neighbor >= 2) {
quicksort2d(array2d_temp.data, 0, _sampling->currlen - 1);
quickselect2d(array2d_temp.data, 0, _sampling->currlen - 1, _sampling->neighbor);
}

for (int j = 0; j < _sampling->neighbor; j++) {
Expand Down
14 changes: 14 additions & 0 deletions uc_sgsim/c_core/src/sort_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ void quicksort2d(double** array, int front, int end) {
quicksort2d(array, pivot + 1, end);
}
}

void quickselect2d(double** array, int front, int end, int k) {
if (front < end) {
int pivot = partition2d(array, front, end);

if (pivot == k - 1) {
return;
} else if (pivot > k - 1) {
quickselect2d(array, front, pivot, k);
} else {
quickselect2d(array, pivot + 1, end, k);
}
}
}
Binary file modified uc_sgsim/c_core/uc_sgsim.dll
Binary file not shown.
Binary file modified uc_sgsim/c_core/uc_sgsim.so
Binary file not shown.
1 change: 0 additions & 1 deletion uc_sgsim/cov_model/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion uc_sgsim/krige/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion uc_sgsim/plot/.gitignore

This file was deleted.

46 changes: 28 additions & 18 deletions uc_sgsim/random_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@


class RandomField:
def __init__(self, x: int, model: CovModel, realization_number: int):
self.__model = model
def __init__(self, x: int, realization_number: int):
self.__realization_number = realization_number
self.__bandwidth_step = model.bandwidth_step
self.__bandwidth = model.bandwidth
self.__create_grid(x)
self._create_grid(x)

def __create_grid(self, x: int, y: int = 0) -> None:
def _create_grid(self, x: int, y: int = 0) -> None:
self.__x = range(x)
self.__y = range(y)
self.__x_size = len(self.__x)
Expand All @@ -36,10 +33,6 @@ def y(self) -> int:
def y_size(self) -> int:
return self.__y_size

@property
def model(self) -> CovModel:
return self.__model

@property
def realization_number(self) -> int:
return self.__realization_number
Expand All @@ -48,14 +41,6 @@ def realization_number(self) -> int:
def realization_number(self, val: int):
self.__realization_number = val

@property
def bandwidth(self) -> np.array:
return self.__bandwidth

@property
def bandwidth_step(self) -> int:
return self.__bandwidth_step

def save_random_field(
self,
path: str,
Expand Down Expand Up @@ -106,3 +91,28 @@ def save_variogram(self, path: str, file_type: str = 'csv', save_single: bool =
)
else:
save_as_one_file(path, self.variogram)


class SgsimField(RandomField):
def __init__(
self,
x: int,
realization_number: int,
model: CovModel,
):
super().__init__(x, realization_number)
self.__model = model
self.__bandwidth_step = model.bandwidth_step
self.__bandwidth = model.bandwidth

@property
def model(self) -> CovModel:
return self.__model

@property
def bandwidth(self) -> np.array:
return self.__bandwidth

@property
def bandwidth_step(self) -> int:
return self.__bandwidth_step

Check warning on line 118 in uc_sgsim/random_field.py

View check run for this annotation

Codecov / codecov/patch

uc_sgsim/random_field.py#L118

Added line #L118 was not covered by tests
4 changes: 2 additions & 2 deletions uc_sgsim/sgsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import numpy as np
from uc_sgsim.exception import VariogramDoesNotCompute
from uc_sgsim.krige import SimpleKrige
from uc_sgsim.random_field import RandomField
from uc_sgsim.random_field import SgsimField
from uc_sgsim.plot.plot import Visualize
from uc_sgsim.cov_model.base import CovModel
from uc_sgsim.utils import CovModelStructure, SgsimStructure

BASE_DIR = Path(__file__).resolve().parent


class UCSgsim(RandomField):
class UCSgsim(SgsimField):
def __init__(
self,
x: int,
Expand Down

0 comments on commit 269995e

Please sign in to comment.