
## load the necessary packages
library(data.table)
library(dplyr)
require(lubridate)
require(stringr)
require(Metrics)
require(zoo)


## load data
xtrain <- fread('../input/ashrae-energy-prediction/train.csv')

# the column we need to know what to drop 
xtrain$tokeep <- 0

## detect the strings of zeroes ----

# list of unique buildings 
building_list <- unique(xtrain$building_id)

# loop over buildings - done in a loop for convenience (I wanted to check results as they came without waiting for a complete job 
# to finish. also, parallelization in data.table on Mac does not work out of the box)

for (bb in building_list)
{
  index0 <- which(xtrain$building_id == bb)
  x0 <- xtrain[index0,]
  x0[, `:=`(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),
         by= list(building_id, timestamp)]
  # catch non-negative indices
  idx <- which(x0$rmx > 0)
  # start of a first contiguous block
  cutoff <- which(diff(idx) == 1)
  start_here <- idx[cutoff[1]]
  # mark rows to drop
  x0$tokeep[start_here:nrow(x0)] <- 1
  xtrain$tokeep[index0][start_here:nrow(x0)] <- 1
  
  prop_retained = round(1 - start_here/nrow(x0),2)
  print(paste(bb, ': ', prop_retained, ' kept', sep = ''))
}

## save trimmed version ----
xtr <- xtrain[xtrain$tokeep == 1,]
xtr$tokeep <- NULL
write.csv(xtr, 'train_trimmed.csv', row.names = F, quote = F)

