Skip to content

Commit

Permalink
Better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner-andrulis committed Apr 1, 2024
1 parent 0cb545e commit 7714e3f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
15 changes: 11 additions & 4 deletions timeloopfe/common/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def cast_check_type(self, value: Any, node: "Node", key: str) -> Any:
new_exc = ParseError(
f'Error calling cast function "{callname}" '
f'for value "{value}" in {node.get_name()}[{key}]. '
f"{self.removed_by_str()}{estr}"
f"{self.removed_by_str()}{estr}. {exc}"
)
new_exc._last_non_node_exception = last_non_node_exception
raise new_exc from exc
Expand Down Expand Up @@ -532,7 +532,7 @@ def _parse_elem(
except Exception as exc:
raise ValueError(
f'Could not combine values in indices "{key}" and '
f'"{newkey}" in {self.get_name()}. ' # type: ignore
f'"{newkey}" in {self.get_name()}. {exc}' # type: ignore
) from exc
else:
self[key] = v
Expand Down Expand Up @@ -964,7 +964,14 @@ def search(node: Node):

def __str__(self):
"""Return the name of this node."""
return self.get_name()
as_str = ""
if isinstance(self, dict):
as_str = str(dict(self))
if isinstance(self, list):
as_str = str(list(self))
as_str = as_str if len(as_str) < 50 else as_str[:50] + "..."

return self.get_name() + "=" + as_str

def __format__(self, format_spec):
"""Formats the name of this node."""
Expand Down Expand Up @@ -1077,7 +1084,7 @@ def parse_expressions(
except Exception as exc:
raise TypeError(
f'Could not parse expression "{self[i]}" in '
f'"{self.get_name()}[{i}]".'
f'"{self.get_name()}[{i}]". {exc}'
) from exc
else:
checker.check_type(self[i], self, i)
Expand Down
69 changes: 50 additions & 19 deletions timeloopfe/v4/processors/constraint_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,40 +182,57 @@ def debug_message(x, kind):
) is not None:
debug_message(factors, "factors_only")
factors: Factors = factors
constraint.factors.combine(factors) # type: ignore
try:
constraint.factors.combine(factors) # type: ignore
except Exception as e:
raise ValueError(
f"Failed to combine factors_only constraint {factors} with "
f"existing factors {constraint.factors}. {e}"
) from e

for p in prob_dimensions:
constraint.factors.add_eq_factor_iff_not_exists(p, 1)

if (ds := constraint.pop("no_iteration_over_dataspaces", None)) is not None:
debug_message(ds, "no_iteration_over_dataspaces")
dataspaces = [prob_shape.name2dataspace(d) for d in ds]
constraint.factors.combine( # type: ignore
Factors(
list(
f"{f}=1"
for d in dataspaces
for f in d.factors
if f in spec.problem.shape.dimensions
)
factors = Factors(
list(
f"{f}=1"
for d in dataspaces
for f in d.factors
if f in spec.problem.shape.dimensions
)
)
try:
constraint.factors.combine(factors) # type: ignore
except Exception as e:
raise ValueError(
f"Failed to combine no_iteration_over_dataspaces constraint {ds}->{factors} "
f"with existing factors {constraint.factors}. {e}"
) from e

if (ds := constraint.pop("must_iterate_over_dataspaces", None)) is not None:
debug_message(ds, "must_iterate_over_dataspaces")
dataspaces = [prob_shape.name2dataspace(d) for d in ds]
allfactors = set()
for d in dataspaces:
allfactors.update(d.factors)
notfactors = set(prob_dimensions) - allfactors

constraint.factors.combine( # type: ignore
Factors(
list(
f"{f}=1"
for f in notfactors
if f in spec.problem.shape.dimensions
)
factors = Factors(
list(
f"{f}=1"
for f in notfactors
if f in spec.problem.shape.dimensions
)
)
try:
constraint.factors.combine(factors) # type: ignore
except Exception as e:
raise ValueError(
f"Failed to combine must_iterate_over_dataspaces constraint {ds}->{factors} "
f"with existing factors {constraint.factors}. {e}"
) from e

for constraint in spec.get_nodes_of_type(Dataspace):
constraint: Dataspace = constraint # type: ignore
Expand All @@ -225,13 +242,27 @@ def debug_message(x, kind):
debug_message(ds, "keep_only")
keep = ds
bypass = list(set(prob_data_spaces) - set(ds))
constraint.combine(ctype(bypass=bypass, keep=keep))
try:
constraint.combine(ctype(bypass=bypass, keep=keep))
except Exception as e:
raise ValueError(
f"Failed to combine keep_only constraint {ds} with "
f"existing keep constraint {constraint.keep} and "
f"bypass constraint {constraint.bypass}. {e}"
) from e

if (ds := constraint.pop("bypass_only", None)) is not None:
debug_message(ds, "bypass_only")
keep = list(set(prob_data_spaces) - set(ds))
bypass = ds
constraint.combine(ctype(bypass=bypass, keep=keep))
try:
constraint.combine(ctype(bypass=bypass, keep=keep))
except Exception as e:
raise ValueError(
f"Failed to combine bypass_only constraint {ds} with "
f"existing keep constraint {constraint.keep} and "
f"bypass constraint {constraint.bypass}. {e}"
) from e

unconstrained = self.get_unconstrained_dims(spec)

Expand Down

0 comments on commit 7714e3f

Please sign in to comment.