Skip to content

gp3bayespy.contracts

1 public function in this module.

← API reference hub

Create an approved backend-independent Bayesian model contract.

Source code in src/gp3bayespy/contracts.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
def create_model_contract(
    family: str,
    outcome_col: str,
    participant_col: str,
    item_col: str | None = None,
    trial_col: str | None = None,
    condition_col: str | None = None,
    time_col: str | None = None,
    predictors: Sequence[str] = (),
    interaction: Sequence[str] | None = None,
    random_slope: bool = False,
    outcome_unit: str | None = None,
    notes: Sequence[str] = (),
) -> ModelContract:
    """Create an approved backend-independent Bayesian model contract."""
    family_value = _match_contract_family(family)
    mappings: dict[str, str | None] = {
        "outcome": _nonempty_name(outcome_col, "outcome_col"),
        "participant": _nonempty_name(participant_col, "participant_col"),
        "item": _nonempty_name(item_col, "item_col", optional=True),
        "trial": _nonempty_name(trial_col, "trial_col", optional=True),
        "condition": _nonempty_name(condition_col, "condition_col", optional=True),
        "time": _nonempty_name(time_col, "time_col", optional=True),
    }

    predictor_values = _unique_strings(predictors, "predictors")
    note_values = _unique_strings(notes, "notes")

    interaction_values: tuple[str, str] | None = None
    if interaction is not None:
        values = _unique_strings(interaction, "interaction")
        if len(values) != 2:
            raise GP3BayesError("`interaction` must contain exactly two declared variables.")
        available = {
            value
            for value in (
                mappings["condition"],
                mappings["time"],
                *predictor_values,
            )
            if value is not None
        }
        if not set(values).issubset(available):
            raise GP3BayesError(
                "Every interaction variable must be declared through "
                "`condition_col`, `time_col`, or `predictors`."
            )
        interaction_values = (values[0], values[1])

    if not isinstance(random_slope, bool):
        raise GP3BayesError("`random_slope` must be TRUE or FALSE.")
    if random_slope and mappings["condition"] is None:
        raise GP3BayesError("`condition_col` must be supplied when `random_slope = TRUE`.")

    declared = [value for value in mappings.values() if value is not None] + list(predictor_values)
    seen: set[str] = set()
    duplicates: list[str] = []
    for value in declared:
        if value in seen and value not in duplicates:
            duplicates.append(value)
        seen.add(value)
    if duplicates:
        raise GP3BayesError(
            "Column mappings and predictors must be unique. Duplicated: "
            + ", ".join(duplicates)
            + "."
        )

    if family_value == "binary":
        if outcome_unit is not None:
            raise GP3BayesError("`outcome_unit` must be NULL for the binary family.")
        outcome_unit_value = None
        template = _BINARY_TEMPLATE
    else:
        outcome_unit_value = _nonempty_name(outcome_unit, "outcome_unit")
        template = _DURATION_TEMPLATE

    return ModelContract(
        contract_version="0.1",
        family=family_value,
        model_family=_MODEL_FAMILIES[family_value],
        mappings=mappings,
        predictors=predictor_values,
        interaction=interaction_values,
        random_slope=random_slope,
        outcome_unit=outcome_unit_value,
        notes=note_values,
        template=template,
    )