如何修改daqapo::detect_value_range_violations函数输出指定日志统计?
修改daqapo::detect_value_range_violations函数输出格式
问题描述
现有如下activitylog格式的医院就诊日志数据:
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"))
使用默认调用方式时,函数返回tibble格式的违规结果:
hospital %>% detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))
需要修改该函数,使其输出如下指定格式的日志统计文本:
# Log of 0 events consisting of: 5 traces 6 cases 7 instances of 5 activities 4 resources
修改实现
由于daqapo包的detect_value_range_violations默认返回违规事件的tibble,我们需要重写该函数,保留核心的违规检测逻辑,新增日志统计计算,并输出指定格式的文本:
detect_value_range_violations <- function(log, ...) { # 调用原函数获取违规结果,统计违规事件数量 violations <- daqapo::detect_value_range_violations(log, ...) violation_count <- nrow(violations) # 从日志属性中提取关键列名 case_col <- attr(log, "case_id") activity_col <- attr(log, "activity_id") resource_col <- attr(log, "resource_id") # 计算各项统计指标 case_count <- length(unique(log[[case_col]])) activity_instances <- nrow(log) unique_activities <- length(unique(log[[activity_col]])) resource_count <- length(unique(log[[resource_col]])) # 计算轨迹数:按案例分组生成活动序列,去重后统计数量 traces <- log %>% dplyr::group_by(.data[[case_col]]) %>% dplyr::summarise(trace = paste(.data[[activity_col]], collapse = "->"), .groups = "drop") %>% dplyr::distinct(trace) %>% nrow() # 拼接目标格式文本并输出 output_text <- glue::glue("# Log of {violation_count} events consisting of: {traces} traces {case_count} cases {activity_instances} instances of {unique_activities} activities {resource_count} resources ") cat(output_text) # 可选:保留违规结果供后续调用,不主动显示 invisible(violations) }
关键说明
- 保留违规检测逻辑:调用原daqapo函数获取违规数据,确保核心功能不丢失
- 统计指标计算:
- 案例数:基于日志
case_id属性列统计唯一值数量 - 活动统计:分别计算总活动实例数(日志总行数)和唯一活动名称数量
- 资源数:基于
resource_id属性列统计唯一值数量 - 轨迹数:按案例生成活动序列字符串,去重后统计不同轨迹的数量
- 案例数:基于日志
- 格式输出:用
glue拼接指定格式的文本,通过cat直接输出;invisible函数可保留违规结果,不影响文本显示效果
使用修改后的函数调用时,将直接输出目标格式的统计文本。
内容的提问来源于stack exchange,提问作者firmo23
相关产品推荐
相关产品推荐

