Skip to content

patient_to_df

Converts a Patient instance into a DataFrame suitable for prediction.

This function takes a Patient dataclass instance and flattens its attributes along with nested Tooth and Side instances to generate a DataFrame. Each row in the DataFrame corresponds to a side of a tooth, with all relevant patient, tooth, and side attributes in a single row.

Parameters:

Name Type Description Default
patient Patient

The Patient dataclass instance.

required

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame where each row represents a tooth side.

Example
patient = Patient(..)
patient_data = patient_to_df(patient=patient)
Source code in periomod/base.py
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
429
430
431
432
433
434
435
436
def patient_to_df(patient: Patient) -> pd.DataFrame:
    """Converts a Patient instance into a DataFrame suitable for prediction.

    This function takes a `Patient` dataclass instance and flattens its attributes
    along with nested `Tooth` and `Side` instances to generate a DataFrame. Each row
    in the DataFrame corresponds to a side of a tooth, with all relevant patient,
    tooth, and side attributes in a single row.

    Args:
        patient (Patient): The Patient dataclass instance.

    Returns:
        pd.DataFrame: DataFrame where each row represents a tooth side.

    Example:
        ```
        patient = Patient(..)
        patient_data = patient_to_df(patient=patient)
        ```
    """
    rows = []
    patient_dict = asdict(patient)

    for tooth in patient_dict["teeth"]:
        for side in tooth["sides"]:
            data = {
                **{k: v for k, v in patient_dict.items() if k != "teeth"},
                **{k: v for k, v in tooth.items() if k != "sides"},
                **side,
            }
            rows.append(data)

    return pd.DataFrame(rows)