Skip to content

eds_scikit.biology.viz.aggregate

aggregate_measurement

aggregate_measurement(measurement: DataFrame, stats_only: bool, overall_only: bool, value_column: str, unit_column: str, category_columns = [], debug = False)

Aggregates measurement dataframe in three descriptive and synthetic dataframe : - measurement_stats - measurement_volumetry - measurement_distribution

Useful function before plotting.

PARAMETER DESCRIPTION
measurement

description

TYPE: DataFrame

stats_only

description

TYPE: bool

overall_only

description

TYPE: bool

category_columns

description, by default []

TYPE: list, optional DEFAULT: []

RETURNS DESCRIPTION
_type_

description

Source code in eds_scikit/biology/viz/aggregate.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def aggregate_measurement(
    measurement: DataFrame,
    stats_only: bool,
    overall_only: bool,
    value_column: str,
    unit_column: str,
    category_columns=[],
    debug=False,
):
    """Aggregates measurement dataframe in three descriptive and synthetic dataframe :
      - measurement_stats
      - measurement_volumetry
      - measurement_distribution

    Useful function before plotting.

    Parameters
    ----------
    measurement : DataFrame
        _description_
    stats_only : bool
        _description_
    overall_only : bool
        _description_
    category_columns : list, optional
        _description_, by default []

    Returns
    -------
    _type_
        _description_
    """

    check_columns(
        df=measurement,
        required_columns=[
            "measurement_id",
            unit_column,
            "measurement_date",
            value_column,
        ]
        + category_columns,
        df_name="measurement",
    )

    measurement.shape

    # Truncate date
    measurement["measurement_month"] = (
        measurement["measurement_date"].astype("datetime64").dt.strftime("%Y-%m")
    )
    measurement = measurement.drop(columns=["measurement_date"])

    # Filter measurement with missing values
    filtered_measurement, missing_value = filter_missing_values(measurement)

    # Compute measurement statistics by code
    measurement_stats = _describe_measurement_by_code(
        filtered_measurement,
        overall_only,
        value_column,
        unit_column,
        category_columns,
        debug,
    )

    if stats_only:
        return {"measurement_stats": measurement_stats}

    # Count measurement by care_site and by code per each month
    measurement_volumetry = _count_measurement_by_category_and_code_per_month(
        filtered_measurement,
        missing_value,
        value_column,
        unit_column,
        category_columns,
        debug,
    )

    # Bin measurement values by care_site and by code
    measurement_distribution = _bin_measurement_value_by_category_and_code(
        filtered_measurement, value_column, unit_column, category_columns, debug
    )

    return {
        "measurement_stats": measurement_stats,
        "measurement_volumetry": measurement_volumetry,
        "measurement_distribution": measurement_distribution,
    }

add_mad_minmax

add_mad_minmax(measurement: DataFrame, category_cols: List[str], value_column: str = 'value_as_number', unit_column: str = 'unit_source_value') -> DataFrame

Add min_value, max_value column to measurement based on MAD criteria.

PARAMETER DESCRIPTION
measurement

measurement dataframe

TYPE: DataFrame

category_cols

measurement category columns to perform the groupby on when computing MAD

TYPE: List[str]

value_column

measurement value column on which MAD will be computed

TYPE: str DEFAULT: 'value_as_number'

RETURNS DESCRIPTION
DataFrame

measurement dataframe with added columns min_value, max_value

Source code in eds_scikit/biology/viz/aggregate.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
def add_mad_minmax(
    measurement: DataFrame,
    category_cols: List[str],
    value_column: str = "value_as_number",
    unit_column: str = "unit_source_value",
) -> DataFrame:
    """Add min_value, max_value column to measurement based on MAD criteria.

    Parameters
    ----------
    measurement : DataFrame
        measurement dataframe
    category_cols : List[str]
        measurement category columns to perform the groupby on when computing MAD
    value_column : str
        measurement value column on which MAD will be computed

    Returns
    -------
    DataFrame
        measurement dataframe with added columns min_value, max_value
    """
    measurement_median = (
        measurement[category_cols + [value_column]]
        .groupby(
            category_cols,
            as_index=False,
            dropna=False,
        )
        .median()
        .rename(columns={value_column: "median"})
    )

    # Add median column to the measurement table
    measurement_median = measurement_median.merge(
        measurement[
            category_cols
            + [
                value_column,
            ]
        ],
        on=category_cols,
    )

    # Compute median deviation for each measurement
    measurement_median["median_deviation"] = abs(
        measurement_median["median"] - measurement_median[value_column]
    )

    # Compute MAD per care site and code
    measurement_mad = (
        measurement_median[
            category_cols
            + [
                "median",
                "median_deviation",
            ]
        ]
        .groupby(
            category_cols
            + [
                "median",
            ],
            as_index=False,
            dropna=False,
        )
        .median()
        .rename(columns={"median_deviation": "MAD"})
    )

    measurement_mad["MAD"] = 1.48 * measurement_mad["MAD"]

    # Add MAD column to the measurement table
    measurement = measurement_mad.merge(
        measurement,
        on=category_cols,
    )

    # Compute binned value
    measurement["max_value"] = measurement["median"] + 4 * measurement["MAD"]
    measurement["min_value"] = measurement["median"] - 4 * measurement["MAD"]

    return measurement
Back to top