Skip to content

Commit

Permalink
Keep track of original term for reverse lookup during model evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Sep 27, 2022
1 parent 36ab8b8 commit b3575c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion formulaic/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def power(arg: Set[Term], power: Set[Term]) -> Set[Term]:
def sub_formula(lhs, rhs):
def get_terms(terms):
return [
Term(factors=[Factor(str(t) + "_hat", eval_method="lookup")])
Term(factors=[Factor(str(t) + "_hat", eval_method="lookup")], origin=t)
for t in terms
]

Expand Down
9 changes: 7 additions & 2 deletions formulaic/parser/types/term.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Iterable, TYPE_CHECKING
from __future__ import annotations

from typing import Iterable, Optional, TYPE_CHECKING

if TYPE_CHECKING:
from .factor import Factor # pragma: no cover
Expand All @@ -13,10 +15,13 @@ class Term:
Attributes:
factors: The set of factors to be multipled to form the term.
origin: If this `Term` has been derived from another `Term`, for example
in subformulae, a reference to the original term.
"""

def __init__(self, factors: Iterable["Factor"]):
def __init__(self, factors: Iterable["Factor"], origin: Optional[Term] = None):
self.factors = tuple(sorted(set(factors)))
self.origin = origin
self._factor_exprs = tuple(factor.expr for factor in self.factors)
self._hash = hash(repr(self))

Expand Down

0 comments on commit b3575c8

Please sign in to comment.