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 Apr 19, 2024
1 parent 3490d07 commit 5b88650
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions formulaic/parser/types/term.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Iterable, TYPE_CHECKING
from typing import Any, Iterable, Optional, TYPE_CHECKING

if TYPE_CHECKING:
from .factor import Factor # pragma: no cover
Expand All @@ -15,10 +15,13 @@ class Term:
Attributes:
factors: The set of factors to be multiplied 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(dict.fromkeys(factors))
self.origin = origin
self._factor_key = tuple(factor.expr for factor in sorted(self.factors))
self._hash = hash(":".join(self._factor_key))

Expand Down

0 comments on commit 5b88650

Please sign in to comment.