matthewwardrop / formulaic

A high-performance implementation of Wilkinson formulas for Python.
MIT License
345 stars 25 forks source link

Linear constraint parsing without advanced rules #211

Open bashtage opened 1 day ago

bashtage commented 1 day ago

It is possible to use a simpler parser when parsing linear constraints? I am having problems with existing code that has variable names that were produced from formulaic, e.g.,

import numpy as np
from formulaic.utils.constraints import LinearConstraints

cons = "ylocus_of_control_prog[T.general]"

variable_names = [
    "ylocus_of_control_Intercept",
    "ylocus_of_control_read",
    "ylocus_of_control_write",
    "ylocus_of_control_science",
    "ylocus_of_control_prog[T.general]",
    "ylocus_of_control_prog[T.vocational]",
    "yself_concept_Intercept",
    "yself_concept_read",
    "yself_concept_write",
    "yself_concept_science",
    "yself_concept_prog[T.general]",
    "yself_concept_prog[T.vocational]",
    "ymotivation_Intercept",
    "ymotivation_read",
    "ymotivation_write",
    "ymotivation_science",
    "ymotivation_prog[T.general]",
    "ymotivation_prog[T.vocational]",
]

lc = LinearConstraints.from_spec(cons, variable_names=variable_names)

The raises due to the presence of the [T.general] from the categorical expansion.