From 5b8865076dc223ddb8313b6b968b197756c1fe42 Mon Sep 17 00:00:00 2001 From: Matthew Wardrop Date: Mon, 26 Sep 2022 20:59:26 -0700 Subject: [PATCH] Keep track of original term for reverse lookup during model evaluation. --- formulaic/parser/types/term.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/formulaic/parser/types/term.py b/formulaic/parser/types/term.py index 1cb575d..56a388f 100644 --- a/formulaic/parser/types/term.py +++ b/formulaic/parser/types/term.py @@ -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 @@ -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))