Skip to content

eds_scikit.plot.table_viz

map_column

map_column(table: DataFrame, mapping: dict, src_column: str, target_column: str, drop: bool = True) -> DataFrame

Map a dataframe column given a mapping dictionnary (of regex). If src_column == target_column, src_column will be renamed.

Parameter"

table : DataFrame mapping : dict EXAMPLE: {"column name" : {"CR" : r"^CR", "CRH" : r"^CRH"}} src_column : str target_column : str

RETURNS DESCRIPTION
DataFrame

Dataframe with mapped column

Source code in eds_scikit/plot/table_viz.py
15
16
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
def map_column(
    table: DataFrame,
    mapping: dict,
    src_column: str,
    target_column: str,
    drop: bool = True,
) -> DataFrame:
    """Map a dataframe column given a mapping dictionnary (of regex).
    If ```src_column == target_column```, src_column will be renamed.

    Parameter"
    ----------
    table : DataFrame
    mapping : dict
        **EXAMPLE**: `{"column name" : {"CR" : r"^CR", "CRH" : r"^CRH"}}`
    src_column : str
    target_column : str

    Returns
    -------
    DataFrame
        Dataframe with mapped column
    """
    check_columns(
        table,
        required_columns=[src_column],
    )

    remove_columns = []

    if src_column == target_column:
        table[src_column + "_src"] = table[src_column]
        src_column = src_column + "_src"
        remove_columns += [src_column]
    table[target_column] = "Other"
    table.loc[table[src_column].isna(), target_column] = "NaN"
    for target, regex in mapping.items():
        table.loc[
            table[src_column].str.contains(regex, case=False, regex=True, na=False),
            target_column,
        ] = target

    if drop:
        table = table[set(table.columns).difference(remove_columns)]

    return table

preprocess_table

preprocess_table(table: DataFrame, category_columns: List[str], date_column: str, start_date: str, end_date: str, mapper: dict = None) -> DataFrame
PARAMETER DESCRIPTION
table

Input dataframe to be reduced.

TYPE: DataFrame

category_columns

Columns to perform reduction on.

TYPE: List[str]

date_column

Date column.

TYPE: str

start_date

start date

TYPE: str

end_date

end date

TYPE: str

mapper

EXAMPLE: {"column 1" : {"CR" : r"^CR", "CRH" : r"^CRH"}, "column 2" : {"code a" : r"^A", "code b" : r"^B"}}

TYPE: dict DEFAULT: None

RETURNS DESCRIPTION
DataFrame

Formated and preprocessed table

RAISES DESCRIPTION
ValueError
Source code in eds_scikit/plot/table_viz.py
 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def preprocess_table(
    table: DataFrame,
    category_columns: List[str],
    date_column: str,
    start_date: str,
    end_date: str,
    mapper: dict = None,
) -> DataFrame:
    """

    Parameters
    ----------
    table : DataFrame
        Input dataframe to be reduced.
    category_columns : List[str]
        Columns to perform reduction on.
    date_column : str
        Date column.
    start_date : str
        start date
    end_date : str
        end date
    mapper : dict
        **EXAMPLE**: `{"column 1" : {"CR" : r"^CR", "CRH" : r"^CRH"}, "column 2" : {"code a" : r"^A", "code b" : r"^B"}}`

    Returns
    -------
    DataFrame
        Formated and preprocessed table

    Raises
    ------
    ValueError
    """

    # Check and format to string category columns

    remove_colums = []

    for col in category_columns:
        if not (col in table.columns):
            logger.info(f"Column {col} not in table.")
            remove_colums += [col]
        else:
            table[col] = table[col].astype(str)

    for col in remove_colums:
        category_columns.remove(col)

    if category_columns == []:
        raise Exception("No columns from category_columns in input table.")

    category_columns = [*category_columns, date_column]

    table = table[category_columns]

    # Filter table on dates

    framework = get_framework(table)

    table = table[(table[date_column] >= start_date) & (table[date_column] <= end_date)]
    table["datetime"] = framework.to_datetime(table[date_column].dt.strftime("%Y-%m"))
    table = table.drop(columns=[date_column])

    # Map category columns

    if mapper:
        for col, mapping in mapper.items():
            if col in category_columns:
                table = map_column(table, mapping, col, col)

    return table

reduce_table

reduce_table(table: DataFrame, start_date: str, end_date: str, category_columns: List[str], date_column: str, mapper: dict = None) -> DataFrame

Reduce input table by counting each cartesian product values (col1, col2, ..., date_col) for each columns in category_columns and each date. Columns values must be under 50 . Use mapper to reduce this size.

PARAMETER DESCRIPTION
table

Input dataframe to be reduced.

TYPE: DataFrame

start_date

start date

TYPE: str

end_date

end date

TYPE: str

category_columns

Columns to perform reduction on.

TYPE: List[str]

date_column

Date column.

TYPE: str

mapper

EXAMPLE: {"column 1" : {"CR" : r"^CR", "CRH" : r"^CRH"}, "column 2" : {"code a" : r"^A", "code b" : r"^B"}}

TYPE: dict DEFAULT: None

RETURNS DESCRIPTION
DataFrame

Reducted DataFrame with columns category_columns, date_column and count.

RAISES DESCRIPTION
ValueError
Source code in eds_scikit/plot/table_viz.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def reduce_table(
    table: DataFrame,
    start_date: str,
    end_date: str,
    category_columns: List[str],
    date_column: str,
    mapper: dict = None,
) -> DataFrame:
    """
    Reduce input table by counting each cartesian product values (col1, col2, ..., date_col) for each columns in category_columns and each date.
    Columns values must be under 50 . Use mapper to reduce this size.

    Parameters
    ----------
    table : DataFrame
        Input dataframe to be reduced.
    start_date : str
        start date
    end_date : str
        end date
    category_columns : List[str]
        Columns to perform reduction on.
    date_column : str
        Date column.
    mapper : dict
        **EXAMPLE**: `{"column 1" : {"CR" : r"^CR", "CRH" : r"^CRH"}, "column 2" : {"code a" : r"^A", "code b" : r"^B"}}`

    Returns
    -------
    DataFrame
        Reducted DataFrame with columns category_columns, date_column and count.

    Raises
    ------
    ValueError
    """

    check_columns(
        table,
        required_columns=[date_column],
    )
    table = preprocess_table(
        table, category_columns, date_column, start_date, end_date, mapper
    )
    # to prevent computation issues
    shape = table.shape  # noqa

    # raise error it too much categorical values
    nunique = table.nunique()
    oversized_columns = nunique[(nunique.index != "datetime") & (nunique > 50)].index
    if len(oversized_columns) > 0:
        raise ValueError(
            f"Input table columns can't have more then 50 values. Consider using eds_scikit.plot.map_column. Oversized columns: {oversized_columns}",
        )
    # compute reducted table
    table_count = (
        table.fillna("NaN")
        .groupby(["datetime", *category_columns])
        .size()
        .reset_index(name="count")
    )

    # to prevent computation issues
    shape = table_count.shape  # noqa

    # final formatting
    table_count = to("pandas", table_count)
    table_count["datetime"] = pd.to_datetime(table_count["datetime"])
    date_dataframe = pd.DataFrame(
        pd.date_range(start=start_date, end=end_date, freq="MS"), columns=["datetime"]
    )
    table_count = table_count.merge(date_dataframe, on="datetime", how="right")

    table_count["count"] = table_count["count"].fillna(0)
    table_count = table_count.fillna("NaN")

    return table_count

visualize_table

visualize_table(table_count: DataFrame, title: str = 'table exploration dashboard', description = True) -> alt.Chart

Generate reduced table dashboard.

PARAMETER DESCRIPTION
table_count

Output from eds_scikit.plot.table_viz.reduce_table

TYPE: DataFrame

title

Chart title

TYPE: str DEFAULT: 'table exploration dashboard'

RETURNS DESCRIPTION
alt.Chart

reduce_table dashboard

RAISES DESCRIPTION
ValueError

description

Source code in eds_scikit/plot/table_viz.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def visualize_table(
    table_count: DataFrame,
    title: str = "table exploration dashboard",
    description=True,
) -> alt.Chart:

    """Generate reduced table dashboard.

    Parameters
    ----------
    table_count : DataFrame
        Output from eds_scikit.plot.table_viz.reduce_table
    title : str
        Chart title

    Returns
    -------
    alt.Chart
        reduce_table dashboard

    Raises
    ------
    ValueError
        _description_
    """

    check_columns = ["count", "datetime"]
    for check_column in check_columns:
        if not (check_column in table_count.columns):
            raise ValueError(f"Input table must have a {check_column} column.")

    selections = {}
    columns = [col for col in table_count.columns if not (col in ["datetime", "count"])]
    for col in columns:
        selections[col] = alt.selection_point(
            fields=[col], on="click", bind="legend", clear="dblclick"
        )

    charts = []

    width, height = 300, 50

    # Two charts per column
    for i, col in enumerate(columns):
        color_scale = generate_color_map(table_count, col)
        selection_col = [selections[s] for s in selections if (s != col)]
        # Global volumetry chart
        chart = (
            alt.Chart(table_count)
            .mark_bar()
            .encode(
                x=col + ":N",
                y="sum(count):Q",
                color=alt.Color(col + ":N", scale=color_scale),
                opacity=alt.condition(selections[col], alt.value(1), alt.value(0.3)),
                tooltip=[col],
            )
            .add_params(selections[col])
        )

        if len(selection_col) > 0:
            chart = chart.add_params(selections[col]).transform_filter(
                reduce(
                    (lambda x, y: x & y),
                    selection_col,
                )
            )

        # Temporal volumetry chart
        base_t = (
            alt.Chart(table_count)
            .mark_line()
            .encode(
                x=alt.X("yearmonth(datetime):T"),
                y=alt.Y("sum(count):Q", axis=alt.Axis(format="s")),
                color=alt.Color(col + ":N", scale=color_scale),
                opacity=alt.condition(selections[col], alt.value(1), alt.value(0.3)),
            )
            .add_params(selections[col])
        )
        if len(selection_col) > 0:
            base_t = base_t.transform_filter(
                reduce(
                    (lambda x, y: x & y),
                    selection_col,
                )
            )
        base_t = base_t.properties(width=width, height=height)
        chart = chart.properties(width=width, height=height)

        chart = (chart | base_t).properties(title=col)

        charts.append(chart)

    charts = alt.vconcat(*charts).resolve_scale(color="independent")

    if description:
        title = {
            "text": [title],
            "subtitle": [
                "ALT + SHIFT to select multiple categories",
                "Double-click on legend to unselect",
                "Reduce table column and values size for better interactivity",
            ],
            "fontSize": 25,
            "subtitleFontSize": 15,
            "offset": 30,
            "subtitlePadding": 20,
        }

        charts = charts.properties(
            padding={"left": 50, "top": 50, "right": 50, "bottom": 50},
            title=title,
        ).configure_legend(columns=4, symbolLimit=0)

    return charts
Back to top