TNO Intern

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

Adding information in the readme and in the tests

parent 36968946
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -236,6 +236,35 @@ results = calculate_doublet_performance(input_data, utc_properties=utc_propertie
print(results)
```

### plotting the results of a pythermoigs simulation
It is expected that the user knows how to plot using matplotlib and xarrays, such that they can make plots of the inputs and outputs to suit their needs.
That being said, a plotting function is provided which will produce a plot of a results DataSet, provided it either:
 - has dimensions = ["p_value"] and processes more than 1 p_value
 - has dimensions = ["p_value", "x", "y"], in which case the results will be plotted as maps
Check out the definition of `plot_doublet_performance_results` for more details.

```python
from pythermogis import calculate_doublet_performance, instantiate_utc_properties_builder, plot_doublet_performance_results
import xarray as xr

input_data = xr.Dataset({
    "thickness_mean": ((), 300),
    "thickness_sd": ((), 50),
    "ntg": ((), 0.5),
    "porosity": ((), 0.5),
    "depth": ((), 5000),
    "ln_permeability_mean": ((), 5),
    "ln_permeability_sd": ((), 0.5),
})

utc_properties = instantiate_utc_properties_builder().setUseHeatPump(True).build()
results = calculate_doublet_performance(input_data, utc_properties=utc_properties)
plot_doublet_performance_results(results, outfile = "path/to/outfile")

```



## ThermoGIS Scenario Configuration Files

If you have a valid configuration file, you can parse a utc_properties class using the method: `instantiate_utc_properties_from_xml`.
+2 −1
Original line number Diff line number Diff line
from thermogis_classes.doublet import *
from thermogis_classes.utc_properties import *
from plotting.plot_doublet_performance_results import plot_doublet_performance_results
 No newline at end of file
+0 −0

Empty file added.

+85 −0
Original line number Diff line number Diff line
import math

import xarray as xr
from pygridsio import plot_grid
import matplotlib.pyplot as plt
from pathlib import Path

def plot_doublet_performance_results(results: xr.Dataset, outfile: Path | str = None, title: str =None) -> None:
    """
    This will make plots of the results, with two modes;
    1. If the results DataSet has a dimension of [p_value] line plots of all variables with p_value as the x-axis will be made
    2. If he dimensions are [p_value, x, y] then maps of all variables will be made with the variables along the row of the plot and p_value along the column

    if no outfile is provided the plots will be shown using plt.show()
    :param title:
    :param outfile:
    :param results:
    :return:
    """
    dims = list(results.dims)
    if dims == ['p_value'] and len(results.p_value) > 1:
        plot_maps = False
    elif set(dims) == {'p_value', 'x', 'y'}:
        plot_maps = True
    else:
        raise TypeError(f"Results Dataset has dims: {results.dims}.plot_doublet_performance_results() only accepts:\n1. a results DataSet with dims=[p_value] (where their is more than 1 p_value)\n2. or dims=[p_value, x, y]\nInstead, write a custom plotting function for your dataset.")

    if plot_maps:
        _plot_doublet_performance_results_maps(results, outfile = outfile, title = title)
    else:
        _plot_doublet_performance_results_pvalue(results, outfile = outfile, title = title)

def _plot_doublet_performance_results_pvalue(results: xr.Dataset, outfile: Path | str = None, title: str = None) -> None:
    """
    Given a results DataSet with the dimensions of p_value (and *only* p_value) then make a plot of every variable in that results dataset varying as a function of p_value
    :param results:
    :param outfile:
    :param title:
    :return:
    """
    n_variables = len(results.variables)
    rounded_up = int(math.ceil(n_variables / 5) * 5)
    ncols = 5
    nrows = rounded_up / 5

    fig, axes = plt.subplots(nrows=int(nrows), ncols=int(ncols), figsize=(5 * ncols, 5 * nrows), sharex=True)
    axes_flat = axes.flatten()

    [results[variable].plot(ax=axes_flat[i_variable], x = "p_value") for i_variable, variable in enumerate(results.variables)]

    [ax.set_visible(False) for ax in axes_flat[n_variables:]]   # set unused axes to invisible
    plt.suptitle(title)

    if outfile is not None:
        plt.savefig(outfile)
        plt.close()
        return

    plt.show()

def _plot_doublet_performance_results_maps(results: xr.Dataset, outfile: Path | str = None, title: str = None) -> None:
    """
    Given a results DataSet with the dimensions of p_value, x, y then make a map plot of every variable in that results dataset with variables changing per-row and p_value changing per-column
    :param results:
    :param outfile:
    :param title:
    :return:
    """
    n_variables = len(results.variables) - 3 # don't plot the p_value, x or y variables
    n_p_values = len(results.p_value)

    fig, axes = plt.subplots(nrows=n_variables, ncols=n_p_values, figsize=(5 * n_p_values, 5 * n_variables), sharex=True, sharey=True)
    for i_p_value, p_value in enumerate(results.p_value):
        results_p_value = results.isel(p_value=i_p_value)
        for i_variable, variable in enumerate(results.variables):
            if variable in ["p_value", "x", "y"]: continue
            plot_grid(results_p_value[variable], axes=axes[i_variable - 3, i_p_value])
    plt.suptitle(title)

    if outfile is not None:
        plt.savefig(outfile)
        plt.close()
        return

    plt.show()
 No newline at end of file
+2 −1
Original line number Diff line number Diff line
from thermogis_classes.doublet import *
from thermogis_classes.utc_properties import *
from plotting.plot_doublet_performance_results import plot_doublet_performance_results
 No newline at end of file
Loading