Skip to content

Commit

Permalink
Added runtime error testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Dec 17, 2023
1 parent 882a112 commit aea88e0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
9 changes: 6 additions & 3 deletions chi_pet/chi_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def make_subnodes(self, overwrite: bool = False) -> None:
cp for cp in self._chi_params if cp._level == self._level]

# TODO POSSIBLE make this it's own function
###
# Make list of all 'matched' chi-params matched methods of varying parameters
matched_params = [
cp for cp in current_level_chi_params if cp._alg is 'match']
Expand All @@ -118,7 +119,6 @@ def make_subnodes(self, overwrite: bool = False) -> None:
matched_grps = {}
for mcp in matched_params:
if not mcp._param_grp:
# TODO Create test for this runtime error
raise RuntimeError(
f"Matched param {mcp._name} does not have an associated group. Please set the 'param_grp' argument of this ChiParam object.")
# If grp name does not exists, start list of parameters
Expand All @@ -127,8 +127,10 @@ def make_subnodes(self, overwrite: bool = False) -> None:
continue

matched_grps[mcp._param_grp] += [mcp]
###

# TODO make this it's own testable function
###
val_number_lst = []
# Get number of param variations for each group.
for grp_name, grp_lst in matched_grps.items():
Expand All @@ -140,7 +142,7 @@ def make_subnodes(self, overwrite: bool = False) -> None:
# make sure each chi-param in a group contains the same param variation number
if num_vals != cp.get_number_of_values():
raise RuntimeError(
f"Not all params in matched param group {grp_name} has the same number of values. Check to make sure all values have or generate the same length parameter value lists.")
f"Some params in matched param group {grp_name} have the same number of values. Check to make sure all values have or generate the same length parameter value lists.")
val_number_lst += [num_vals]

# Loop over all none matched parameters and add their parameter value list lengths to val_number_lst to carry out combinatorics.
Expand All @@ -151,8 +153,9 @@ def make_subnodes(self, overwrite: bool = False) -> None:

# If multiple chi-params have the same level carry out the combinatorics
index_combinations = ind_recurse(val_number_lst)
###

# loop over indices, choosing the right chi-param value for each index
# Loop over indices, choosing the right chi-param value for each index
num_grps = len(matched_grps.keys())
for ind_list in index_combinations:

Expand Down
71 changes: 67 additions & 4 deletions tests/test_chi_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,18 @@ def test_chi_node_combinatorics_subnode_creation(mock_yaml_dict, mock_create_opt


def test_chi_node_matched_subnode_creation(mock_yaml_dict, mock_create_opts):
# Set path for creating node
root_path = Path.cwd() / 'tests/mock_root'

# Set up yaml dictionary
yaml_dict = deepcopy(mock_yaml_dict)
# Change first chi-param to be a matched parameter
yaml_dict[MOCK_CHI_PARAM_DICT_PATH]['var_param'] = (
MOCK_CHI_PARAM_STR[:-1] + ", alg='match', param_grp='alpha')")
# Add another ChiParam at a lower level

# Make second chi-param a matched parameter
yaml_dict[MOCK_PARAM_DICT_PATH][
'chi_param_2'] = "ChiParam(name='pB', format_str='pB{:.1f}', values=[.1,.2,.3], alg='match', param_grp='alpha')"

chi_dict = ChiDict(param_dict=yaml_dict)
cnode = ChiNode(root_path, chi_dict, opts=mock_create_opts)

Expand All @@ -142,5 +145,65 @@ def test_chi_node_matched_subnode_creation(mock_yaml_dict, mock_create_opts):
assert (yaml.safe_load(
(p_comb_dir / MOCK_PARAM_DICT_PATH).open('r'))['chi_param_2'] == pb)

# TODO NTEST do match at combinatorics creation
# TODO Make sure the system fails if two matched parameters do not have the same number of values

def test_chi_node_matched_scanned_subnode_creation(mock_yaml_dict, mock_create_opts):
# Set path for creating node
root_path = Path.cwd() / 'tests/mock_root'

# Set up yaml dictionary
yaml_dict = deepcopy(mock_yaml_dict)
# Change first chi-param to be a matched parameter
yaml_dict[MOCK_CHI_PARAM_DICT_PATH]['var_param'] = (
MOCK_CHI_PARAM_STR[:-1] + ", alg='match', param_grp='alpha')")
# Make second chi-param a matched parameter
yaml_dict[MOCK_PARAM_DICT_PATH][
'chi_param_2'] = "ChiParam(name='pB', format_str='pB{:.1f}', values=[.1,.2,.3], alg='match', param_grp='alpha')"
# Make third chi-param a scanned parameter
yaml_dict[MOCK_PARAM_DICT_PATH][
'chi_param_3'] = "ChiParam(name='pC', format_str='pC{:d}', values=[11,22,33], alg='scan')"

chi_dict = ChiDict(param_dict=yaml_dict)
cnode = ChiNode(root_path, chi_dict, opts=mock_create_opts)

cnode.make_node_dir(root_path)
cnode.make_subnodes()
for pa, pb in zip([10, 20, 30], [.1, .2, .3]):
for pc in [11, 22, 33]:
p_comb_dir = Path(
f'tests/mock_root/subnodes/pA{pa}_pB{pb}_pC{pc}')
assert p_comb_dir.exists()
assert (p_comb_dir / MOCK_PARAM_DICT_PATH).exists()
assert (p_comb_dir / MOCK_CHI_PARAM_DICT_PATH).exists()
assert (p_comb_dir / 'data').exists()

assert (yaml.safe_load(
(p_comb_dir / MOCK_CHI_PARAM_DICT_PATH).open('r'))['var_param'] == pa)
assert (yaml.safe_load(
(p_comb_dir / MOCK_PARAM_DICT_PATH).open('r'))['chi_param_2'] == pb)
assert (yaml.safe_load(
(p_comb_dir / MOCK_PARAM_DICT_PATH).open('r'))['chi_param_3'] == pc)


@pytest.mark.parametrize("chi_param_2_str",
["ChiParam(name='pB', format_str='pB{:.1f}', values=[.1,.2,.3,.4], alg='match', param_grp='alpha')",
"ChiParam(name='pB', format_str='pB{:.1f}', values=[.1,.2,.3], alg='match')"])
def test_chi_node_matched_creation_rt_error(mock_yaml_dict, mock_create_opts, chi_param_2_str):
# Set path for creating node
root_path = Path.cwd() / 'tests/mock_root'

# Set up yaml dictionary
yaml_dict = deepcopy(mock_yaml_dict)
# Change first chi-param to be a matched parameter
yaml_dict[MOCK_CHI_PARAM_DICT_PATH]['var_param'] = (
MOCK_CHI_PARAM_STR[:-1] + ", alg='match', param_grp='alpha')")
# Make second chi-param a matched parameter but with different number of values (should throw error)
yaml_dict[MOCK_PARAM_DICT_PATH][
'chi_param_2'] = chi_param_2_str
# Make third chi-param a scanned parameter

chi_dict = ChiDict(param_dict=yaml_dict)
cnode = ChiNode(root_path, chi_dict, opts=mock_create_opts)

cnode.make_node_dir(root_path)
with pytest.raises(RuntimeError):
cnode.make_subnodes()

0 comments on commit aea88e0

Please sign in to comment.