Skip to content

Utils

Classes

Functions

get_evt_log_with_frosenset_evt(log, list_ignored_evt=None)

Transform the log to group concomitant events and keep only those.

This prepares the data for analysis by identifying events occurring at the same time for the same patient and replacing their set of events with a frozenset.

Parameters:

Name Type Description Default
log DataFrame

DataFrame with columns "ID_PATIENT", "EVT" and "TIMESTAMP"

required
list_ignored_evt list[str] | None

events that should never be considered concomitant (e.g., 'in', 'out', 'death')

None

Returns:

Type Description
Series

Series of only concomitant events (same patient and timestamp) where values are the frozenset of events

Source code in opentak/utils_events/utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_evt_log_with_frosenset_evt(
    log: pd.DataFrame,
    list_ignored_evt: list[str] | None = None,
) -> pd.Series:
    """Transform the log to group concomitant events and keep only those.

    This prepares the data for analysis by identifying events occurring at the
    same time for the same patient and replacing their set of events with a
    ``frozenset``.

    :param log: DataFrame with columns "ID_PATIENT", "EVT" and "TIMESTAMP"
    :param list_ignored_evt: events that should never be considered concomitant
        (e.g., 'in', 'out', 'death')
    :return: Series of only concomitant events (same patient and timestamp) where
        values are the ``frozenset`` of events
    """
    if list_ignored_evt is not None:
        log = log.query("EVT not in @list_ignored_evt")

    log_duplicated = log[log.duplicated(subset=["ID_PATIENT", "TIMESTAMP"], keep=False)]

    log_duplicated_grouped = log_duplicated.groupby(["ID_PATIENT", "TIMESTAMP"])[
        "EVT"
    ].agg(frozenset)

    return log_duplicated_grouped

regroup_evt(log, list_ignored_evt=None, list_fzset_to_be_regrouped=None, sep=' + ')

Group concomitant events into a single event label like "A + B".

Parameters:

Name Type Description Default
log DataFrame

DataFrame with columns "ID_PATIENT", "EVT" and "TIMESTAMP"

required
list_ignored_evt list[str] | None

events that should never be considered concomitant (e.g., 'in', 'out', 'death')

None
list_fzset_to_be_regrouped list | None

list of frozensets describing which combinations to group (e.g., [frozenset({"A", "B"}), frozenset({"B", "D"})]). If None, all concomitant events are grouped.

None
sep str

separator used when concatenating events (e.g., sep=" + " turns events "A" and "B" into the single event label "A + B")

' + '

Returns:

Type Description
DataFrame

DataFrame where targeted concomitant events belonging to list_fzset_to_be_regrouped are grouped into a single event label

Source code in opentak/utils_events/utils.py
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
def regroup_evt(
    log: pd.DataFrame,
    list_ignored_evt: list[str] | None = None,
    list_fzset_to_be_regrouped: list | None = None,
    sep: str = " + ",
) -> pd.DataFrame:
    """Group concomitant events into a single event label like "A + B".

    :param log: DataFrame with columns "ID_PATIENT", "EVT" and "TIMESTAMP"
    :param list_ignored_evt: events that should never be considered concomitant
        (e.g., 'in', 'out', 'death')
    :param list_fzset_to_be_regrouped: list of frozensets describing which
        combinations to group (e.g., [frozenset({"A", "B"}),
        frozenset({"B", "D"})]). If None, all concomitant events are grouped.
    :param sep: separator used when concatenating events (e.g., sep=" + "
        turns events "A" and "B" into the single event label "A + B")
    :return: DataFrame where targeted concomitant events belonging to list_fzset_to_be_regrouped are grouped into a
        single event label
    """
    log_cop = log.copy()

    frozen_set = pd.DataFrame(get_evt_log_with_frosenset_evt(log_cop, list_ignored_evt))

    if list_fzset_to_be_regrouped:
        if list_ignored_evt:
            elmt_of_list_fzset_to_be_regrouped = {
                x for fzset in list_fzset_to_be_regrouped for x in fzset
            }
            list_elmt_pb = elmt_of_list_fzset_to_be_regrouped & set(list_ignored_evt)
            if len(list_elmt_pb):
                logger.warning(
                    "Some elements from list_ignored_evt appear in list_fzset_to_be_regrouped; they will be ignored (%s)",
                    list_elmt_pb,
                )

        frozen_set = frozen_set[frozen_set["EVT"].isin(list_fzset_to_be_regrouped)]

    if len(frozen_set):
        if list_ignored_evt is None:
            list_ignored_evt = []
        log_ignored = log_cop[log_cop["EVT"].isin(list_ignored_evt)].set_index(
            ["ID_PATIENT", "TIMESTAMP"]
        )
        log_not_ignored = log_cop[~log_cop["EVT"].isin(list_ignored_evt)].set_index(
            ["ID_PATIENT", "TIMESTAMP"]
        )

        frozen_set["EVT"] = frozen_set["EVT"].apply(list).apply(sorted).apply(sep.join)
        log_not_ignored.loc[frozen_set.index, "EVT"] = frozen_set["EVT"]

        log_tot = (
            pd.concat([log_ignored, log_not_ignored])
            .reset_index()
            .drop_duplicates(subset=["EVT", "TIMESTAMP", "ID_PATIENT"])
            .reset_index(drop=True)
        )

        log_tot = order_in_first_out_last(log_tot)

    else:
        log_tot = log_cop

    return log_tot

order_in_first_out_last(log)

Reorder log according to ID_PATIENT and TIMESTAMP, with in first and out last when several EVT.

Parameters:

Name Type Description Default
log DataFrame

df to be reordored

required

Returns:

Type Description
DataFrame

log reordored

Source code in opentak/utils_events/utils.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def order_in_first_out_last(log: pd.DataFrame) -> pd.DataFrame:
    """Reorder log according to ID_PATIENT and TIMESTAMP, with in first and out last when several EVT.

    :param log: df to be reordored
    :return: log reordored
    """
    log_in = log[log["EVT"].eq("in")]
    log_out = log[log["EVT"].eq("out")]
    log_other = log[~log["EVT"].isin(["in", "out"])].sort_values(
        ["ID_PATIENT", "TIMESTAMP"]
    )
    log_reorder = stable_sort(pd.concat([log_in, log_other, log_out]))

    return log_reorder

deal_with_double_delivrance(base, reset_index=True)

Swap the order of rows in an event log for specific double-delivery cases.

When a patient has a treatment subsequence like A-B-A with B and A given at the same TIMESTAMP, the B-A pair at that timestamp is swapped to A-B. Likewise for A-B-A where A-B are given at the same timestamp: the pair is reordered to A-A then B at the next timestamp, to match the treatment logic.

Parameters:

Name Type Description Default
base

event log with columns ID_PATIENT, TIMESTAMP and EVT

required
reset_index

reset the index before returning (rows may be permuted) !!! note |ID_PATIENT|TIMESTAMP|EVT| | :--: | :--: | :--: | |...|...|...| |10|x|A| |10|y|B| |10|y|A| |...|...|...| with x < y, becomes |ID_PATIENT|TIMESTAMP|EVT| | :--: | :--: | :--: | |...|...|...| |10|x|A| |10|y|A| |10|y|B| |...|...|...| and |ID_PATIENT|TIMESTAMP|EVT| | :--: | :--: | :--: | |...|...|...| |10|x|A| |10|x|B| |10|y|A| |...|...|...| with x < y, becomes |ID_PATIENT|TIMESTAMP|EVT| | :--: | :--: | :--: | |...|...|...| |10|x|A| |10|x|A| |10|y|B| |...|...|...|

True

Returns:

Type Description
DataFrame

event log where rows were reordered to reflect the patient's treatment logic

Source code in opentak/utils_events/utils.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
214
def deal_with_double_delivrance(base, reset_index=True) -> pd.DataFrame:
    """Swap the order of rows in an event log for specific double-delivery cases.

    When a patient has a treatment subsequence like A-B-A with B and A given at
    the same ``TIMESTAMP``, the B-A pair at that timestamp is swapped to A-B.
    Likewise for A-B-A where A-B are given at the same timestamp: the pair is
    reordered to A-A then B at the next timestamp, to match the treatment logic.

    :param base: event log with columns ID_PATIENT, TIMESTAMP and EVT
    :param reset_index: reset the index before returning (rows may be permuted)

    !!! note

        |ID_PATIENT|TIMESTAMP|EVT|
        | :--: | :--: | :--: |
        |...|...|...|
        |10|x|A|
        |10|y|B|
        |10|y|A|
        |...|...|...|

        with x < y, becomes

        |ID_PATIENT|TIMESTAMP|EVT|
        | :--: | :--: | :--: |
        |...|...|...|
        |10|x|A|
        |10|y|A|
        |10|y|B|
        |...|...|...|

        and

        |ID_PATIENT|TIMESTAMP|EVT|
        | :--: | :--: | :--: |
        |...|...|...|
        |10|x|A|
        |10|x|B|
        |10|y|A|
        |...|...|...|

        with x < y, becomes

        |ID_PATIENT|TIMESTAMP|EVT|
        | :--: | :--: | :--: |
        |...|...|...|
        |10|x|A|
        |10|x|A|
        |10|y|B|
        |...|...|...|

    :return: event log where rows were reordered to reflect the patient's treatment logic
    """
    base_cop = base.copy()
    try:
        pd.testing.assert_frame_equal(
            base_cop,
            base_cop.sort_values(["ID_PATIENT", "TIMESTAMP"], kind="mergesort"),
        )
    except AssertionError:
        logger.warning(
            "The base was not sorted by 'ID_PATIENT' and 'TIMESTAMP', it has been sorted \
                       To silence the warning, use base.sort_values(['ID_PATIENT', 'TIMESTAMP'], kind='mergesort')\
                        before this function"
        )

    conditions = (
        base_cop["ID_PATIENT"].eq(base_cop["ID_PATIENT"].shift(-1))
        & base_cop["TIMESTAMP"].eq(base_cop["TIMESTAMP"].shift(-1))
        & base_cop["EVT"].ne(base_cop["EVT"].shift(-1))
        & (
            base_cop["EVT"].eq(base_cop["EVT"].shift(-2))
            | base_cop["EVT"].shift(+1).eq(base_cop["EVT"].shift(-1))
        )
        & (
            (base_cop["EVT"] != base_cop["EVT"].shift(+1))
            & (base_cop["EVT"].shift(-1) != (base_cop["EVT"].shift(-2)))
        )
    )

    conditions = conditions.reset_index(name="ttmt_to_move_down")
    conditions["new_index"] = conditions["index"]
    conditions["ttmt_to_move_up"] = (
        conditions["ttmt_to_move_down"].shift(+1).fillna(False)
    )
    conditions.loc[conditions["ttmt_to_move_down"], "new_index"] = conditions.loc[
        conditions["ttmt_to_move_up"], "index"
    ].to_numpy()
    conditions.loc[conditions["ttmt_to_move_up"], "new_index"] = conditions.loc[
        conditions["ttmt_to_move_down"], "index"
    ].to_numpy()
    base_cop = base_cop.reindex(conditions["new_index"].values)

    if reset_index:
        base_cop = base_cop.reset_index(drop=True)

    return base_cop

add_evt_duration(base, check=True)

Add a column with the delay until the next event (treatment duration).

Parameters:

Name Type Description Default
base DataFrame

DataFrame — one row per event for a patient; columns: 'ID_PATIENT', 'EVT', 'TIMESTAMP'

required
check

set to False to skip validations (e.g., if there is neither 'in' nor 'out')

True

Returns:

Type Description
DataFrame

copy of the input with an additional evt_duration column .. note:: Regardless of check being True or False, the dataset is reordered because it is required for correct computation.

Source code in opentak/utils_events/utils.py
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
def add_evt_duration(base: pd.DataFrame, check=True) -> pd.DataFrame:
    """Add a column with the delay until the next event (treatment duration).

    :param base: DataFrame — one row per event for a patient; columns:
        'ID_PATIENT', 'EVT', 'TIMESTAMP'
    :param check: set to False to skip validations (e.g., if there is neither
        'in' nor 'out')
    :return: copy of the input with an additional ``evt_duration`` column

    .. note:: Regardless of ``check`` being True or False, the dataset is
        reordered because it is required for correct computation.
    """
    base_copy = _check_or_not(base, check)

    base_copy["evt_duration"] = base_copy["TIMESTAMP"].shift(-1).diff().copy()

    base_copy["evt_duration"].iloc[0] = (
        base_copy["TIMESTAMP"].iloc[1] - base_copy["TIMESTAMP"].iloc[0]
    )

    conditions = (
        (base_copy["EVT"].shift(+1) != "in")
        & (base_copy["EVT"].shift(+1) != "start")
        & base_copy["evt_duration"].shift(+1).eq(0)
        & (base_copy["EVT"] != "out")
        & (base_copy["EVT"] != "end")
        & (base_copy["EVT"] != "death")
    )
    durees_totales = base_copy.loc[conditions, "evt_duration"].to_numpy()
    if len(durees_totales):
        warn1 = "Careful ! Some EVT (other than in, out and death) appeared at the same TIMESTAMP for a same ID_PATIENT. "
        warn2 = (
            "For these co-occuring EVT, the duration of each of these 2 EVT will be half the "
            "duration separating them from the next EVT of this patient. "
        )
        warn3 = (
            "For example, if a patient has T1=A, T1=B, T6=C, then the duration will be "
            "A: 2T, B: 3T, and the timeline from D1 will be A-A-B-B-B-C"
        )
        logger.warning(warn1 + warn2 + warn3)
    durees_divisees_par_deux = np.ceil(
        base_copy.loc[conditions, "evt_duration"].to_numpy() / 2
    )
    complementaire_des_durees = [
        tot - div_par_deux
        for tot, div_par_deux in zip(
            durees_totales, durees_divisees_par_deux, strict=False
        )
    ]
    base_copy.loc[conditions, "evt_duration"] = durees_divisees_par_deux
    conditions2 = (
        (base_copy["EVT"] != "in")
        & (base_copy["EVT"] != "start")
        & base_copy["evt_duration"].eq(0)
        & (base_copy["EVT"].shift(-1) != "out")
        & (base_copy["EVT"].shift(-1) != "end")
        & (base_copy["EVT"].shift(-1) != "death")
    )
    try:
        base_copy.loc[conditions2, "evt_duration"] = complementaire_des_durees
    except ValueError as exc:
        pat = base_copy.loc[conditions2, "ID_PATIENT"].unique()
        raise ValueError(
            f"Treatment durations are inconsistent for patients {pat}"
        ) from exc

    base_copy.loc[
        base_copy["ID_PATIENT"].ne(base_copy["ID_PATIENT"].shift(-1)),
        "evt_duration",
    ] = np.nan

    return base_copy