Skip to content

Commit

Permalink
Add support for sub-formula (useful in IV contexts).
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Sep 27, 2022
1 parent e368c8f commit 36ab8b8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions formulaic/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .algos.tokenize import tokenize
from .types import (
Factor,
FormulaParser,
Operator,
OperatorResolver,
Expand Down Expand Up @@ -162,6 +163,20 @@ def power(arg: Set[Term], power: Set[Term]) -> Set[Term]:
for term in itertools.product(*[arg] * int(power_term.factors[0].expr))
}

def sub_formula(lhs, rhs):
def get_terms(terms):
return [
Term(factors=[Factor(str(t) + "_hat", eval_method="lookup")])
for t in terms
]

if isinstance(lhs, Structured):
lhs_hat = lhs._update(root=get_terms(lhs.root))
else:
lhs_hat = get_terms(lhs)

return Structured(lhs_hat, deps=(Structured(lhs=lhs, rhs=rhs),))

return [
Operator(
"~",
Expand All @@ -182,6 +197,15 @@ def power(arg: Set[Term], power: Set[Term]) -> Set[Term]:
accepts_context=lambda context: len(context) == 0,
structural=True,
),
Operator(
"~",
arity=2,
precedence=-100,
associativity=None,
to_terms=sub_formula,
accepts_context=lambda context: context and context[-1] == "[",
structural=True,
),
Operator(
"|",
arity=2,
Expand Down

0 comments on commit 36ab8b8

Please sign in to comment.