Modify daqapo::detect_value_range_violations to generate different result

58 Views Asked by At

I have the activitylog object below:

hospital<-structure(list(patient_visit_nr = c(510, 512, 510, 512, 512, 
                                              510, 517, 518, 518, 518, 519, 519, 520, 519, 521, 521, 519, 522, 
                                              522, 523, 524, 525, 526, 523, 527, 527, 527, 528, 528, 523), 
                         activity = c("registration", "Registration", "Triage", "Triage", 
                                      "Clinical exam", "Clinical exam", "Triage", "Registration", 
                                      "Registration", "Registration", "Registration", "Triage", 
                                      "Trage", "Clinical exam", "Triage", "Registration", "Treatment", 
                                      "Registration", "Triaga", "Registration", "Triage", "Triage", 
                                      "Triage", "Triage", "Registration", "Triage", "Clinical exam", 
                                      "Registration", "Triage", "Clinical exam"), originator = c("Clerk 9", 
                                                                                                 "Clerk 12", "Nurse 27", "Nurse 27", "Doctor 7", "Doctor 7", 
                                                                                                 "Nurse 17", "Clerk 12", "Clerk 6", "Clerk 9", "Clerk 12", 
                                                                                                 "Nurse 17", "Nurse 17", "Doctor 4", "Nurse 17", "Clerk 3", 
                                                                                                 "Nurse 17", "Clerk 3", "Nurse 5", "Clerk 9", "Nurse 5", "Nurse 5", 
                                                                                                 "Nurse 5", "Nurse 27", "Clerk 6", "Nurse 5", "Doctor 4", 
                                                                                                 "Nurse 6", "Nurse 6", "Doctor 4"), start = structure(c(1511173097, 
                                                                                                                                                        1511173994, 1511174048, 1511174652, 1511177232, 1511177701, 
                                                                                                                                                        1511264116, 1511264716, 1511264716, 1511264716, 1511265581, 
                                                                                                                                                        1511270783, 1511271796, 1511272354, 1511272924, 1511273120, 
                                                                                                                                                        1511275132, 1511277137, 1511277325, 1511282528, 1511283843, 
                                                                                                                                                        1511283853, 1511283855, 1511285190, 1511287330, 1511287331, 
                                                                                                                                                        1511287333, 1511287817, 1511288105, 1511288209), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                  "POSIXt")), complete = structure(c(1511173206, 1511174220, 
                                                                                                                                                                                                                                                                     1511174508, 1511175017, 1511177637, 1511177769, 1511264340, 
                                                                                                                                                                                                                                                                     1511263336, 1511263336, 1511263336, 1511265952, 1511271138, 
                                                                                                                                                                                                                                                                     1511271540, 1511272954, 1511273169, 1511290800, 1511275761, 
                                                                                                                                                                                                                                                                     1511277499, 1511277484, 1511283119, 1511283965, 1511283968, 
                                                                                                                                                                                                                                                                     1511283970, 1511287261, 1511287447, 1511287448, 1511287450, 
                                                                                                                                                                                                                                                                     1511288104, 1511288412, 1511288528), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                                                                                                                   "POSIXt")), triagecode = c(3, 3, 3, 3, 3, NA, 3, 4, 4, 4, 
                                                                                                                                                                                                                                                                                                                                                              4, 4, 5, 4, 2, 2, 4, 2, 2, 3, 3, 3, 4, 3, 2, 2, 2, 3, 3, 
                                                                                                                                                                                                                                                                                                                                                              3), specialization = c("TRAU", "URG", "TRAU", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "PED", "PED", "PED", "PED", "URG", "PED", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "URG", "URG", "URG", "URG", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "URG", "URG", "TRAU", "TRAU", "URG"), .order = 1:30), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       -30L), class = c("activitylog", "log", "tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), case_id = "patient_visit_nr", activity_id = "activity", resource_id = "originator", timestamps = c("start", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "complete"))

and I want to modify the function daqapo::detect_value_range_violations in order to give me as final result the :

# Log of 0 events consisting of:
5 traces 
6 cases 
7 instances of 5 activities 
4 resources 

instead of the tibble that is generated with

hospital %>%
  detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))

. How can i modify this function?

1

There are 1 best solutions below

3
I_O On BEST ANSWER

Package {bupaR} has a summary method specific for objects of class activitylog. You can inspect what's happening inside this internal (to bupaR) function with:

    bupaR:::summary.activitylog ## triple colon, no parentheses

(output not shown)

Next, you could copy over that function to your own custom summary function, say, summary_events:

summary_events <- bupaR:::summary.activitylog

Then remove the trailing NextMethod(object) from your function's code. That will return more or less the desired event log but without the rest:

my_activitylog <- 
  hospital |>
  detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))

my_activitylog |>  summary_events()

output:

## Number of events:  2
## Number of cases:  1
## Number of traces:  1
## Number of distinct activities:  1
## Average trace length:  2
## 
## Start eventlog:  2017-11-20 11:35:01
## End eventlog:  2017-11-20 11:36:09

... or you could just pick the interesting functions (like case_list or n_events) from the original summary method's code to roll your custom function to apply after showing your object. Example:

event_summary  <- function(x){
    ca <- case_list(x)
     counts <- c(events = n_events(x),
                 cases = nrow(ca),
                 traces = length(unique(ca$trace_id)),
                 activities = nrow(activities(x))
                 )
    data.frame(category = names(counts),
               counts, row.names = NULL)
}

use:

    violations <- 
        hospital |>
        detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))
    
    > violations |> event_summary()
        category counts
    1     events      2
    2      cases      1
    3     traces      1
    4 activities      1