-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeSensorLStatistics.R
More file actions
213 lines (165 loc) · 7.47 KB
/
Copy pathComputeSensorLStatistics.R
File metadata and controls
213 lines (165 loc) · 7.47 KB
1
2
3
4
5
6
7
8
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
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
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
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
###############################################################################
# Title: Sensor L Left/Right Delta and Anomaly-Filtered Statistics
#
# Purpose:
# Parallel sensor branch for left/right SensorL data. This script:
# • Reads consolidated 60-second telemetry stores
# • Pulls left and right Sensor L label streams within event windows
# • Applies anomaly filtering
# • Computes DeltaSensorL = RightSensorL - LeftSensorL
# • Derives per-record statistics and rate-of-change metrics
# • Outputs cleaned left/right streams and delta statistics
#
# This advances the data by constructing an additional sensor family whose
# cross-channel behavior may correlate with analysis-causing events.
#
# Skills shown:
# • Dual-sensor delta computation and summary stats
# • Domain-specific artifact removal (bounce filtering)
# • Structured output for multi-sensor analysis
#
# Inputs:
# • Consolidated telemetry stores:
# $WORKDIR/Projects/telemetry_event_correlation/TelemetryStores/Consolidated_*.db
# (tables: ConsolidatedRecords)
# • HeaderData.RData
# Contains file names for each record
# • AnomalyLog.RData
# Contains log of anomaly records
#
# Outputs:
# • Delta and stats:
# SensorL/SensorLDelta.db (table StoresFiltered)
# SensorL/SensorLDeltaStats.csv
# • Cleaned left/right streams:
# SensorL/LeftSensorL.db (table StoresFiltered)
# SensorL/RightSensorL.db (table StoresFiltered)
#
# Author: Skylar Furey
###############################################################################
## Packages/Libraries ##########################################################
packages <- c("RSQLite", "dplyr")
installed_packages <- packages %in% rownames(installed.packages())
if (any(!installed_packages)) install.packages(packages[!installed_packages])
invisible(lapply(packages, library, character.only = TRUE))
## Inputs ######################################################################
cmdArgs <- commandArgs()
output_root <- file.path(system("echo $HOME", intern = TRUE), "Projects/telemetry_event_correlation/")
data_root <- file.path(system("echo $WORKDIR", intern = TRUE), "Projects/telemetry_event_correlation/TelemetryStores/")
# Major events causing analysis
EventFilterTable <- data.frame(
RecordID = c(1001,1002,1003,1004),
FilterTimestamp = c(1620030000,1621040000,1622050000,1623060000)
)
# Load lookup/reference data
# Must include: (RecordID, FileName)
load(file.path(data_root, "CACHED_USER_DATA/Lookup/HeaderData.RData"))
# Load anomaly log to remove anomalies
load(file.path(data_root, "CACHED_USER_DATA/telemetry_event_correlation/AnomalyLog.RData"))
# Identify consolidated telemetry stores built from earlier pipeline scripts
db_files <- list.files(
path = data_root,
pattern = paste0("^Consolidated_.*\\.db$"),
full.names = TRUE
)
## Functions ###################################################################
# Remove windows associated with anomaly sequences
removeAnomalies <- function(df, anomaly_log) {
merged <- merge(df, anomaly_log, by = "RecordID", all.x = TRUE, all.y = TRUE)
flagged <- merged[
merged$EventTimestamp >= merged$StartAnomaly &
merged$EventTimestamp <= merged$EndAnomaly &
!is.na(merged$AssetID) &
!is.na(merged$StartAnomaly),
c("RecordID", "AssetID")
]
if (nrow(flagged) > 0) {
flagged$IsAnomaly <- TRUE
df <- merge(df, flagged, by = c("RecordID", "AssetID"), all.x = TRUE)
df <- df[is.na(df$IsAnomaly), ]
df$IsAnomaly <- NULL
}
return(df)
}
## Body ########################################################################
dfSensorLDelta <- data.frame()
dfSensorLDeltaStats <- data.frame()
dfLeftSensorL <- data.frame()
dfRightSensorL <- data.frame()
for (db in db_files) {
message("Processing: ", db)
con <- dbConnect(SQLite(), dbname = db, flags = SQLITE_RO)
# Pull left/right SensorL label streams
dfLeftDB <- dbGetQuery(con,
"SELECT RecordID, AssetID, EventTimestamp, Timestamp, CAST(SensorValue AS DECIMAL) AS LeftSensorL
FROM ConsolidatedRecords
WHERE SensorName = 'LeftSensorL'
ORDER BY RecordID, EventTimestamp;"
)
dfRightDB <- dbGetQuery(con,
"SELECT RecordID, AssetID, EventTimestamp, Timestamp, CAST(SensorValue AS DECIMAL) AS RightSensorL
FROM ConsolidatedRecords
WHERE T.SensorName = 'RightSensorL'
ORDER BY RecordID, EventTimestamp;"
)
dbDisconnect(con)
rm(con)
## Delta Computation --------------------------------------------------------
dfLeftDB <- distinct(dfLeftDB)
dfRightDB <- distinct(dfRightDB)
dfDelta <- merge(dfLeftDB, dfRightDB, by = c("RecordID", "AssetID", "EventTimestamp", "Timestamp"), all = TRUE)
dfDelta$DeltaSensorL <- dfDelta$RightSensorL - dfDelta$LeftSensorL
dfDelta <- merge(
dfDelta[, c("RecordID", "AssetID", "EventTimestamp", "Timestamp", "DeltaSensorL")],
HeaderData[, c("RecordID", "Filename")],
by = "RecordID",
all.x = TRUE
)
## Summary Stats ------------------------------------------------------------
dfStats <- dfDelta %>%
group_by(RecordID, AssetID, EventTimestamp, Filename) %>%
summarise(
minDeltaSensorL = min(DeltaSensorL),
maxDeltaSensorL = max(DeltaSensorL),
medianDeltaSensorL = median(DeltaSensorL),
meanDeltaSensorL = mean(DeltaSensorL),
firstDeltaSensorL = first(DeltaSensorL),
lastDeltaSensorL = last(DeltaSensorL),
firstTS = first(EventTimestamp),
lastTS = last(EventTimestamp),
.groups = "drop"
)
dfStats$RateOfChange <- (dfStats$lastDeltaSensorL - dfStats$firstDeltaSensorL) /
(dfStats$lastTS - dfStats$firstTS)
# Remove anomalies ---------------------------------------------------------
dfStats <- removeAnomalies(dfStats, anomaly_log)
dfDelta <- removeAnomalies(dfDelta, anomaly_log)
dfLeftDB <- removeAnomalies(dfLeftDB, anomaly_log)
dfRightDB <- removeAnomalies(dfRightDB, anomaly_log)
# Store results
dfSensorLDelta <- bind_rows(dfSensorLDelta, dfDelta)
dfSensorLDeltaStats <- bind_rows(dfSensorLDeltaStats, dfStats)
dfLeftSensorL <- bind_rows(dfLeftSensorL, dfLeftDB)
dfRightSensorL <- bind_rows(dfRightSensorL, dfRightDB)
rm(dfDelta, dfStats, dfLeftDB, dfRightDB, anomaly_log)
}
## Deduplicate ###############################################################
dfSensorLDelta <- distinct(dfSensorLDelta)
dfSensorLDeltaStats <- distinct(dfSensorLDeltaStats)
dfLeftSensorL <- distinct(dfLeftSensorL)
dfRightSensorL <- distinct(dfRightSensorL)
## Outputs ###################################################################
con <- dbConnect(SQLite(), file.path(output_root, "SensorL/SensorLDelta.db"))
dbWriteTable(con, "StoresFiltered", dfSensorLDelta, overwrite = TRUE)
dbDisconnect(con)
write.table(
dfSensorLDeltaStats,
file = file.path(output_root, "SensorL/SensorLDeltaStats.csv"),
sep = ",", row.names = FALSE
)
con <- dbConnect(SQLite(), file.path(output_root, "SensorL/LeftSensorL.db"))
dbWriteTable(con, "StoresFiltered", dfLeftSensorL, overwrite = TRUE)
dbDisconnect(con)
con <- dbConnect(SQLite(), file.path(output_root, "SensorL/RightSensorL.db"))
dbWriteTable(con, "StoresFiltered", dfRightSensorL, overwrite = TRUE)
dbDisconnect(con)