Skip to content

Commit

Permalink
some pylint changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrifoster committed Apr 19, 2024
1 parent a3dd198 commit 5163a3f
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 157 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Thumbs.db
*.dvi
*.toc

# Folders created with unit/functional tests
_build/
_run/


# Old Files
*~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ def plot_allometry_var(data, varname, units, save_fig, plot_dir=None):
save_fig (bool): whether or not to write out plot
plot_dir (str): if saving figure, where to write to
"""
df = pd.DataFrame({'dbh': np.tile(data.dbh, len(data.pft)),
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()})

maxdbh = df['dbh'].max()
maxvar = round_up(df[data.name].max())
maxdbh = data_frame['dbh'].max()
maxvar = round_up(data_frame[data.name].max())

colors = get_color_pallete()

plt.figure(figsize=(7, 5))
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
axis = plt.subplot(111)
axis.spines["top"].set_visible(False)
axis.spines["bottom"].set_visible(False)
axis.spines["right"].set_visible(False)
axis.spines["left"].set_visible(False)

ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()

plt.xlim(0.0, maxdbh)
plt.ylim(0.0, maxvar)
Expand All @@ -47,16 +47,16 @@ def plot_allometry_var(data, varname, units, save_fig, plot_dir=None):

inc = (int(maxvar) - 0)/20
for i in range(0, 20):
y = 0.0 + i*inc
y_val = 0.0 + i*inc
plt.plot(range(math.floor(0), math.ceil(maxdbh)),
[y] * len(range(math.floor(0), math.ceil(maxdbh))),
[y_val] * len(range(math.floor(0), math.ceil(maxdbh))),
"--", lw=0.5, color="black", alpha=0.3)

plt.tick_params(bottom=False, top=False, left=False, right=False)

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

Expand All @@ -66,7 +66,7 @@ def plot_allometry_var(data, varname, units, save_fig, plot_dir=None):
plt.legend(loc='upper left', title='PFT')

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

def plot_total_biomass(data, save_fig, plot_dir):
Expand All @@ -75,24 +75,25 @@ def plot_total_biomass(data, save_fig, plot_dir):
Args:
data (xarray DataSet): the allometry dataset
"""
df = pd.DataFrame({'dbh': np.tile(data.dbh, len(data.pft)),
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()})

colors = get_color_pallete()

plt.figure(figsize=(7, 5))
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
axis = plt.subplot(111)
axis.spines["top"].set_visible(False)
axis.spines["bottom"].set_visible(False)
axis.spines["right"].set_visible(False)
axis.spines["left"].set_visible(False)

ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()

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

plt.xlim(0.0, maxbiomass)
plt.ylim(0.0, maxbiomass)
Expand All @@ -101,9 +102,9 @@ def plot_total_biomass(data, save_fig, plot_dir):
plt.xticks(fontsize=10)
plt.tick_params(bottom=False, top=False, left=False, right=False)

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

Expand All @@ -117,6 +118,14 @@ def plot_total_biomass(data, save_fig, plot_dir):
plt.savefig(fig_name)

def plot_allometry_dat(run_dir, out_file, save_figs, plot_dir):
"""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, out_file))
Expand Down Expand Up @@ -172,8 +181,8 @@ def plot_allometry_dat(run_dir, out_file, save_figs, plot_dir):
},

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

plot_total_biomass(allometry_dat, save_figs, plot_dir)
plot_total_biomass(allometry_dat, save_figs, plot_dir)
69 changes: 34 additions & 35 deletions functional_unit_testing/build_fortran_tests.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""
Builds/compiles any tests within the FATES repository
"""
import os
import sys
import shutil

_FATES_PYTHON = os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(1, _FATES_PYTHON)

from utils import add_cime_lib_to_path

add_cime_lib_to_path()

from CIME.utils import get_src_root, run_cmd_no_fail, expect, stringify_bool
from CIME.build import CmakeTmpBuildDir
from CIME.XML.machines import Machines
from CIME.BuildTools.configure import configure, FakeCase
from CIME.XML.env_mach_specific import EnvMachSpecific
from CIME.utils import get_src_root, run_cmd_no_fail, expect, stringify_bool # pylint: disable=wrong-import-position,import-error,wrong-import-order
from CIME.build import CmakeTmpBuildDir # pylint: disable=wrong-import-position,import-error,wrong-import-order
from CIME.XML.machines import Machines # pylint: disable=wrong-import-position,import-error,wrong-import-order
from CIME.BuildTools.configure import configure, FakeCase # pylint: disable=wrong-import-position,import-error,wrong-import-order
from CIME.XML.env_mach_specific import EnvMachSpecific # pylint: disable=wrong-import-position,import-error,wrong-import-order

_CIMEROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../../cime")

Expand All @@ -30,7 +29,8 @@ def run_cmake(name, test_dir, pfunit_path, netcdf_c_path, netcdf_f_path, cmake_a
print(f"Running cmake for {name}.")

# directory with cmake modules
cmake_module_dir = os.path.abspath(os.path.join(_CIMEROOT, "CIME", "non_py", "src", "CMake"))
cmake_module_dir = os.path.abspath(os.path.join(_CIMEROOT, "CIME", "non_py",
"src", "CMake"))

# directory with genf90
genf90_dir = os.path.join(_CIMEROOT, "CIME", "non_py", "externals", "genf90")
Expand Down Expand Up @@ -64,7 +64,8 @@ def find_library(caseroot, cmake_args, lib_string):
Args:
caseroot (str): Directory with pfunit macros
cmake_args (str): The cmake args used to invoke cmake (so that we get the correct makefile vars)
cmake_args (str): The cmake args used to invoke cmake
(so that we get the correct makefile vars)
"""
with CmakeTmpBuildDir(macroloc=caseroot) as cmaketmp:
all_vars = cmaketmp.get_makefile_vars(cmake_args=cmake_args)
Expand Down Expand Up @@ -92,7 +93,7 @@ def prep_build_dir(build_dir, clean):
# create the build directory
build_dir_path = os.path.abspath(build_dir)
if not os.path.isdir(build_dir_path):
os.mkdir(build_dir_path)
os.mkdir(build_dir_path)

# change into that directory
os.chdir(build_dir_path)
Expand All @@ -116,12 +117,12 @@ def clean_cmake_files():

# Clear contents to do with cmake cache
for file in cwd_contents:
if (
file in ("Macros.cmake", "env_mach_specific.xml")
or file.startswith("Depends")
or file.startswith(".env_mach_specific")
):
os.remove(file)
if (
file in ("Macros.cmake", "env_mach_specific.xml")
or file.startswith("Depends")
or file.startswith(".env_mach_specific")
):
os.remove(file)

def get_extra_cmake_args(build_dir, mpilib):
"""Makes a fake case to grab the required cmake arguments
Expand Down Expand Up @@ -152,23 +153,22 @@ def get_extra_cmake_args(build_dir, mpilib):
os_,
unit_testing=True,
)
machspecific = EnvMachSpecific(build_dir, unit_testing=True)
EnvMachSpecific(build_dir, unit_testing=True)

# make a fake case
fake_case = FakeCase(compiler, mpilib, True, "nuopc", threading=False)

cmake_args = (
"{}-DOS={} -DMACH={} -DCOMPILER={} -DDEBUG={} -DMPILIB={} -Dcompile_threaded={} -DCASEROOT={}".format(
"",
os_,
machobj.get_machine_name(),
compiler,
stringify_bool(True),
mpilib,
stringify_bool(False),
build_dir
)
)
FakeCase(compiler, mpilib, True, "nuopc", threading=False)

cmake_args_list = [
f"-DOS={os_}",
f"-DMACH={machobj.get_machine_name()}",
f"-DCOMPILER={compiler}",
f"-DDEBUG={stringify_bool(True)}",
f"-DMPILIB={mpilib}",
f"-Dcompile_threaded={stringify_bool(False)}",
f"-DCASEROOT={build_dir}"
]

cmake_args = " ".join(cmake_args_list)

return cmake_args

Expand Down Expand Up @@ -246,4 +246,3 @@ def build_unit_tests(build_dir, name, cmake_directory, make_j, clean=False):
# run cmake and make
run_cmake(name, cmake_directory, pfunit_path, netcdf_c_path, netcdf_f_path, cmake_args)
run_make(name, make_j, clean=clean)

34 changes: 0 additions & 34 deletions functional_unit_testing/math_utils/MathUtils.py

This file was deleted.

52 changes: 52 additions & 0 deletions functional_unit_testing/math_utils/math_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Utility functions for allometry functional unit tests
"""
import os
import math
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt

from utils import get_color_pallete

def plot_quadratic_dat(run_dir, out_file, save_figs, plot_dir):
"""Reads in and plots quadratic formula test output
Args:
run_dir (str): run directory
out_file (str): output file
save_figs (bool): whether or not to save the figures
plot_dir (str): plot directory
"""

# read in quadratic data
quadratic_dat = xr.open_dataset(os.path.join(run_dir, out_file))

# plot output
plot_quad_and_roots(quadratic_dat.a.values, quadratic_dat.b.values,
quadratic_dat.c.values, quadratic_dat.root1.values,
quadratic_dat.root2.values)
if save_figs:
fig_name = os.path.join(plot_dir, "quadratic_test.png")
plt.savefig(fig_name)

def plot_quad_and_roots(a_coeff, b_coeff, c_coeff, root1, root2):
"""Plots a set of quadratic formulas (ax**2 + bx + c) and their two roots
Args:
a (float array): set of a coefficients
b (float array): set of b coefficients
c (float array): set of b coefficients
r1 (float array): set of first real roots
r2 (float array): set of second real roots
"""

colors = get_color_pallete()

plt.figure(figsize=(7, 5))
x_vals = np.linspace(-10.0, 10.0, num=20)

for i in range(len(a_coeff)):
y_vals = a_coeff[i]*x_vals**2 + b_coeff[i]*x_vals + c_coeff[i]
plt.plot(x_vals, y_vals, lw=2, color=colors[i])
plt.scatter(root1[i], root2[i], color=colors[i], s=50)
plt.axhline(y=0.0, color='k', linestyle='dotted')
Loading

0 comments on commit 5163a3f

Please sign in to comment.