cleverly.bases.Modifiers#

class cleverly.bases.Modifiers(columns: Sequence[str], domains: Mapping[str, tuple[float, float]] | tuple[tuple[str, tuple[float, float]], ...] = (), levels: Mapping[str, Sequence[Any]] | tuple[tuple[str, tuple[Any, ...]], ...] = (), outside: Literal['refuse', 'clip'] = 'refuse')[source]#

The modifier columns, with the domain or the level set each one is declared on.

A continuous modifier may carry a domain (low, high), which a basis that reads a domain needs and which the rows are checked against. A categorical modifier carries its declared levels, in the order an Indicators basis reports them; the first level is the reference. No column has both.

outside says what happens to a row whose continuous modifier lies outside its domain, or whose categorical modifier holds an undeclared level. "refuse" raises. "clip" maps a continuous value to the nearest end of its domain, a known fixed transform, so the working model is then a function of the clipped modifier; an undeclared level still raises. "clip" needs a domain for every continuous modifier.

The schema checks rows against what it declares. It does not know the adjustment set: cleverly.ConditionalContrast checks that each modifier is an adjustment column.

Parameters:
columnssequence of str

The modifier columns, in the order a basis reads them.

domainsmapping of str to (float, float), optional

The declared domain of a continuous modifier.

levelsmapping of str to sequence, optional

The declared levels of a categorical modifier, reference level first.

outside{“refuse”, “clip”}

What a continuous value outside its domain does.

See also

cleverly.ConditionalContrast

The estimand that declares the modifiers.

Linear

A basis that needs no domain.

Examples

>>> from cleverly.bases import Modifiers
>>> modifiers = Modifiers(["age", "sex"], domains={"age": (18, 90)}, levels={"sex": ("f", "m")})
>>> modifiers.domain("age"), modifiers.levels_of("sex")
((18.0, 90.0), ('f', 'm'))
>>> modifiers.continuous
('age',)

Methods#

Modifiers.domain(column: str) → tuple[float, float] | None[source]#

The declared domain of column, or None.

Parameters:
columnstr

A modifier column.

Returns:
tuple of float or None

(low, high), or None when no domain is declared.

Modifiers.levels_of(column: str) → tuple[Any, ...] | None[source]#

The declared levels of column, or None.

Parameters:
columnstr

A modifier column.

Returns:
tuple or None

The levels, reference first, or None when none are declared.

Modifiers.values(frame: Any, columns: Sequence[str] | None = None) → dict[str, ndarray][source]#

Read and check the modifier columns of frame.

A continuous modifier comes back as floats, clipped under outside="clip"; a categorical one as its raw values, each checked against the declared levels.

Parameters:
framedataframe or mapping

Rows holding the modifier columns, in pandas, polars or a mapping of arrays.

columnssequence of str, optional

The modifiers to read. Every declared modifier when omitted.

Returns:
dict of str to ndarray

One array per modifier.

Raises:
DataError

If a column is missing, a continuous value is not a finite number, a value lies outside its domain under outside="refuse", or a categorical value is not a declared level.

Attributes#

Modifiers.categorical#

The modifiers with a declared level set, in column order.

Modifiers.continuous#

The modifiers with no declared level set, in column order.

Modifiers.domains: Mapping[str, tuple[float, float]] | tuple[tuple[str, tuple[float, float]], ...] = ()#
Modifiers.levels: Mapping[str, Sequence[Any]] | tuple[tuple[str, tuple[Any, ...]], ...] = ()#
Modifiers.outside: Literal['refuse', 'clip'] = 'refuse'#
Modifiers.columns: Sequence[str]#