TNO Intern

Commit edd7b295 authored by Hen Brett's avatar Hen Brett 🐔
Browse files

Fixing the doc examples to match the xarray way of working

parent 8a6ee07a
Loading
Loading
Loading
Loading
+23 −24
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ This example corresponds to test case `test_example5` in `test_doc_examples.py`
directory of the repository.

```python
from pythermogis import calculate_doublet_performance, calculate_pos4npv_pvalues, calculate_pos4npv_pvalues_singlelocation
from pythermogis import calculate_doublet_performance, calculate_pos, calculate_pos_pvalues_singlelocation
from pygridsio import read_grid, plot_grid, resample_xarray_grid
from matplotlib import pyplot as plt
import numpy as np
@@ -40,7 +40,6 @@ import xarray as xr
from pathlib import Path
from os import path


# the location of the input data: the data can be found in the resources/example_data directory of the repo
input_data_path = Path(path.dirname(__file__), "resources") / "test_input" / "example_data"

@@ -83,7 +82,7 @@ else:
# plot net-present value at a single location as a function of p-value and find the probability of success
x, y = 125e3, 525e3  # define location
results_loc = results.sel(x=x, y=y, method="nearest")
pos = calculate_pos4npv_pvalues_singlelocation(results_loc.npv, results_loc.p_value)
pos = calculate_pos_pvalues_singlelocation(results_loc.npv, results_loc.p_value)
# plot npv versus p-value and a map showing the location of interest
fig, axes = plt.subplots(ncols=2, figsize=(10, 5))
results_loc.npv.plot(y="p_value", ax=axes[0])
@@ -94,7 +93,7 @@ axes[0].legend()
axes[0].set_xlabel("net-present-value [Million €]")
axes[0].set_ylabel("p-value [%]")

pos = calculate_pos4npv_pvalues(results)
pos = calculate_pos(results)
plot_grid(pos, axes=axes[1], add_netherlands_shapefile=True)
axes[1].scatter(x, y, marker="x", color="tab:red")

+92 −94
Original line number Diff line number Diff line
@@ -34,12 +34,13 @@ This example corresponds to test case `test_example6` in `test_doc_examples.py`
directory of the repository.

```python
    from pythermogis import calculate_doublet_performance,  calculate_pos4npv_pvalues_singlelocation
    from pythermogis import calculate_doublet_performance, calculate_pos_pvalues_singlelocation
from pygridsio import read_grid, plot_grid, resample_xarray_grid
from matplotlib import pyplot as plt
import xarray as xr
from pathlib import Path
from os import path

input_data_path = Path(path.dirname(__file__), "resources") / "test_input" / "example_data"
# create a directory to write the output files to
output_data_path = Path(path.dirname(__file__), "resources") / "test_output" / "example_data"
@@ -57,7 +58,6 @@ directory of the repository.
portfolioloc = [(125e3, 525e3), (100e3, 525e3), (110e3, 525e3), (125e3, 515e3),
                (125e3, 520e3)]  # define location


fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(10, 10))
colors = plt.cm.tab10.colors
ic = 0
@@ -70,7 +70,7 @@ directory of the repository.
    results_loc['npv'] = results_loc.npv.clip(min=AEC)

    # probability of success is defined as the p-value where npv = 0.0, use interpolation to find pos:
        pos = calculate_pos4npv_pvalues_singlelocation(results_loc.npv,
    pos = calculate_pos_pvalues_singlelocation(results_loc.npv,
                                               results_loc.p_value)  # The order of the npv data has to be reversed as np.interp requires values to increase to function properly

    # plot npv versus p-value and a map showing the location of interest
@@ -101,8 +101,6 @@ directory of the repository.
    axe.set_xlabel("kH [Dm]")
    axe.set_ylabel("p-value [%]")



    axe = axes[1, 0]
    results_loc.power.plot(y="p_value", ax=axe, color=colors[ic])
    axe.set_title(f"Aquifer: ROSL_ROSLU\n Power")
+2 −1
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@ from pythermogis.thermogis_classes.doublet import *
from pythermogis.thermogis_classes.utc_properties import *
from pythermogis.transmissivity.calculate_thick_perm_trans import *
from pythermogis.tables.utc_properties_table import *
from pythermogis.postprocessing.pos import *
 No newline at end of file
+0 −0

Empty file added.

+44 −0
Original line number Diff line number Diff line
import xarray as xr
import numpy as np

def calculate_pos_pvalues_singlelocation(npv: xr.DataArray, p_values: xr.DataArray) -> float:
    """
    Calculate the probability of success (POS) from net present value (NPV) and p-values.

    Parameters
    ----------
    npv : xr.DataArray
        Net present value.
    p_values : xr.DataArray
        P-values corresponding to the NPV.

    Returns
    -------
    float
        The probability of success (POS) as a percentage.
    """
    # Ensure that npv and p_values are 1D arrays
    if npv.ndim != 1 or p_values.ndim != 1:
        raise ValueError("Both npv and p_values must be 1D arrays.")

    # Reverse the order of npv and p_values for interpolation
    pos = np.interp(0.0, npv[::-1], p_values[::-1])

    return pos

def calculate_pos(results:xr.Dataset) -> xr.Dataset:
    """
    Calculate the probability of success (POS) for each p-value in the results dataset based on the net present value (NPV).
    """
    # alternatively calculate pos map, by xr.apply_ufunc and plot ma
    pos = xr.apply_ufunc(
        calculate_pos_pvalues_singlelocation,
        results.npv,
        results.p_value,
        input_core_dims=[['p_value'], ['p_value']],
        vectorize=True,
        dask='parallelized',
        output_dtypes=[float]
    )
    pos.name = "POS"  # set name for the pos variable
    return pos
 No newline at end of file
Loading