Skip to content

Commit

Permalink
create classes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrifoster committed Jun 20, 2024
1 parent e74262f commit 2791487
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 309 deletions.
2 changes: 1 addition & 1 deletion testing/build_fortran_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def clean_cmake_files():
or file.startswith(".env_mach_specific")
):
os.remove(file)

def get_extra_cmake_args(build_dir:str, mpilib:str) -> str:
"""Makes a fake case to grab the required cmake arguments
Args:
Expand Down
187 changes: 0 additions & 187 deletions testing/functional_testing/allometry/allometry_plotting.py

This file was deleted.

162 changes: 158 additions & 4 deletions testing/functional_testing/allometry/allometry_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,159 @@
from testing_classes import FunctionalTest
import os
import math
import xarray as xr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from utils import round_up, get_color_palette, blank_plot
from testing_classes import Heuristic

class AllometryTest(FunctionalTest):
def __init__()

class AllometryTest(Heuristic):

name = 'allometry'

def __init__(self, test_dict):
super().__init__(AllometryTest.name, test_dict['test_dir'],
test_dict['test_exe'], test_dict['out_file'],
test_dict['use_param_file'],
test_dict['other_args'])

def plot_output(self, run_dir: str, save_figs: bool, plot_dir: str):
"""Plots all allometry plots
Args:
run_dir (str): run directory
out_file (str): output file name
save_figs (bool): whether or not to save the figures
plot_dir (str): plot directory to save the figures to
"""

# read in allometry data
allometry_dat = xr.open_dataset(os.path.join(run_dir, self.out_file))

plot_dict = {
'height': {
'varname': 'height',
'units': 'm',
},
'bagw': {
'varname': 'aboveground biomass',
'units': 'kgC',
},
'blmax': {
'varname': 'maximum leaf biomass',
'units': 'kgC',
},
'crown_area': {
'varname': 'crown area',
'units': 'm$^2$',
},
'sapwood_area': {
'varname': 'sapwood area',
'units': 'm$^2$',
},
'bsap': {
'varname': 'sapwood biomass',
'units': 'kgC',
},
'bbgw': {
'varname': 'belowground biomass',
'units': 'kgC',
},
'fineroot_biomass': {
'varname': 'fineroot biomass',
'units': 'kgC',
},
'bstore': {
'varname': 'storage biomass',
'units': 'kgC',
},
'bdead': {
'varname': 'deadwood biomass',
'units': 'kgC',
},
'total_biomass_parts': {
'varname': 'total biomass (calculated from parts)',
'units': 'kgC',
},
'total_biomass_tissues': {
'varname': 'total biomass (calculated from tissues)',
'units': 'kgC',
},

}
for plot, attributes in plot_dict.items():
self.plot_allometry_var(allometry_dat[plot], attributes['varname'],
attributes['units'], save_figs, plot_dir)

self.plot_total_biomass(allometry_dat, save_figs, plot_dir)

@staticmethod
def plot_allometry_var(data: xr.Dataset, varname: str, units: str,
save_fig: bool, plot_dir :str=None):
"""Plot an allometry variable
Args:
data (xarray DataArray): the data array of the variable to plot
var (str): variable name (for data structure)
varname (str): variable name for plot labels
units (str): variable units for plot labels
save_fig (bool): whether or not to write out plot
plot_dir (str): if saving figure, where to write to
"""
data_frame = pd.DataFrame({'dbh': np.tile(data.dbh, len(data.pft)),
'pft': np.repeat(data.pft, len(data.dbh)),
data.name: data.values.flatten()})

max_dbh = data_frame['dbh'].max()
max_var = round_up(data_frame[data.name].max())

blank_plot(max_dbh, 0.0, max_var, 0.0, draw_horizontal_lines=True)

pfts = np.unique(data_frame.pft.values)
colors = get_color_palette(len(pfts))
for rank, pft in enumerate(pfts):
dat = data_frame[data_frame.pft == pft]
plt.plot(dat.dbh.values, dat[data.name].values, lw=2, color=colors[rank],
label=pft)

plt.xlabel('DBH (cm)', fontsize=11)
plt.ylabel(f'{varname} ({units})', fontsize=11)
plt.title(f"Simulated {varname} for input parameter file", fontsize=11)
plt.legend(loc='upper left', title='PFT')

if save_fig:
fig_name = os.path.join(plot_dir, f"allometry_plot_{data.name}.png")
plt.savefig(fig_name)

@staticmethod
def plot_total_biomass(data: xr.Dataset, save_fig: bool, plot_dir: str):
"""Plot two calculations of total biomass against each other
Args:
data (xarray DataSet): the allometry dataset
"""
data_frame = pd.DataFrame({'dbh': np.tile(data.dbh, len(data.pft)),
'pft': np.repeat(data.pft, len(data.dbh)),
'total_biomass_parts': data.total_biomass_parts.values.flatten(),
'total_biomass_tissues': data.total_biomass_tissues.values.flatten()})

max_biomass = np.maximum(data_frame['total_biomass_parts'].max(),
data_frame['total_biomass_tissues'].max())

blank_plot(max_biomass, 0.0, max_biomass, 0.0, draw_horizontal_lines=False)

pfts = np.unique(data_frame.pft.values)
colors = get_color_palette(len(pfts))
for rank, pft in enumerate(pfts):
data = data_frame[data_frame.pft == pft]
plt.scatter(data.total_biomass_parts.values, data.total_biomass_parts.values,
color=colors[rank], label=pft)

plt.xlabel('Total biomass (kgC) from parts', fontsize=11)
plt.ylabel('Total biomass (kgC) from tissues', fontsize=11)
plt.title("Simulated total biomass for input parameter file", fontsize=11)
plt.legend(loc='upper left', title='PFT')

if save_fig:
fig_name = os.path.join(plot_dir, "allometry_plot_total_biomass_compare.png")
plt.savefig(fig_name)
Loading

0 comments on commit 2791487

Please sign in to comment.