################################################################
#########################    Code    ###########################
################################################################

source_filepath    = "Code/Beiwe-Analysis"
data_filepath      = "Studies/John_Schizophrenia/Data"
output_filepath    = "Studies/John_Schizophrenia/Output"

data <- readRDS(paste(output_filepath,"/Processed_Data/Group/accelerometer_bursts.rds",sep=""))


### Constants:

acc_binsize=5 # width of bin in minutes used for combining accelerometer data
acc_millisecond_divider = 30*1000
gps_millisecond_divider = 30*1000
acc_frequency = 10
gps_frequency = 1
acc_burst_duration = 60
gps_burst_duration = 60
acc_break_duration = 60
gps_break_duration = 60*10

# Source all Beiwe Analysis files
source(paste(source_filepath, "Utility/Initialize.R",sep="/"))


###################################
### individual patient analysis ###
###################################

for(patient_name in patient_names){
  print(patient_name)
  
  # Preprocess Data
  
  surveys_preprocessing(patient_name)
  text_preprocessing(patient_name)
  calls_preprocessing(patient_name)
  powerstate_preprocessing(patient_name)
  accelerometer_preprocessing(patient_name, minutes = acc_binsize)
  GPS_preprocessing(patient_name)
  
  find_bursts(patient_name, stream = "accelerometer", acc_millisecond_divider)
  find_bursts(patient_name, stream = "GPS",           gps_millisecond_divider)
  # Process Data
  
  GPS_imputation(patient_name,nreps=1)
  CreateMobilityFeatures(patient_name)
  
  # Results
  
  ContinuousDataCollectionTracks(patient_name, acc_binsize)
  
}

#####################################
#### combined patient processing ####
#####################################


daily_features()
fill_in_NAs()
find_questions()
survey_responsiveness()
summarize_data_quality(stream = "accelerometer", acc_burst_duration, acc_break_duration, acc_frequency)
summarize_data_quality(stream = "gps",           gps_burst_duration, gps_break_duration, gps_frequency)

coverage_over_time("accelerometer")
coverage_over_time("gps")

filters = list(
  All_Questions = get_questions(),
  Warning_Signs = c("Unable to cope with stress", "Feeling tired", "Feeling depressed or sad", "Feeling Confused or Puzzled", "Don't feel rested after waking up",  "Feeling nervous, scared, or anxious", "Little interest or pleasure in things", "Trouble concentrating"),
  Mood_and_PHQ8 = c("Little interest or pleasure in things", "Poor appetite or overeating", "Feeling bad or guilty about yourself", "Trouble concentrating",  "Feeling tired", "Feeling depressed or sad", "Trouble relaxing", "Don't feel rested after waking up"),
  Negative_and_Anhedonic = c("Feeling Confused or Puzzled","Little interest or pleasure in things","Feeling bad or guilty about yourself","Feeling depressed or sad","Trouble concentrating","Difficulty thinking clearly","Withdrawing from social interaction"),
  Sleep = c("Feeling tired","Waking up too early","Difficulty falling asleep", "Don't feel rested after waking up", "Difficulty staying asleep", "Don't feel rested after waking up"),
  Cognitive = c ("Trouble concentrating", "Difficulty thinking clearly", "Feeling Confused or Puzzled","Missing Doses of Medications"),
  Psychosis = c("Hearing voices or seeing things", "Feeling suspicious", "Difficulty thinking clearly", "Withdrawing from social interaction"),
  Anxiety_and_GAD7 = c("Unable to cope with stress", "Feeling nervous, scared, or anxious", "Worrying too much",  "Feeling bad or guilty about yourself", "Trouble relaxing",   "Easily annoyed or irritated")#"Feeling Confused or Puzzled", 
)

plot_data_quality_predictiveness(filters)


### Continuous Data plots

anonymize(patient_names)

anonymized_names = function(names){as.list(setNames(paste("Subject", 1:length(names)), names))}
anonymize = function(names){unlist(anonymized_names(names))}

plot_data_quality("gps",           gps_frequency, gps_burst_duration, gps_break_duration, legend=FALSE, study_length = 90, f=0.05,title=FALSE)
plot_data_quality("accelerometer", acc_frequency, acc_burst_duration, acc_break_duration, legend=FALSE, study_length = 90, f=0.05,title=FALSE)

plot_survey_responsiveness(legend = FALSE, study_length = 90, f=0.05, title = FALSE)
plot_survey_completion(legend = FALSE, by_quality = TRUE, study_length = 90, f=0.05, title=FALSE)


# model, plot data quality

plot_data_quality_predictiveness(filters, lags = 5, study_length = 90, xlab="Lag (Weeks)")

survey_completions = surveys_week %>% group_by(patient) %>%
  summarize(follow_up = max(week)+1, total_surveys = sum(num_surveys)) %>%
  mutate(completion_rates = total_surveys/follow_up/3) %>%
  dplyr::select(patient, completion_rates) %>% arrange(completion_rates) %>% data.frame

lags = 5
study_length = 90
for(filter in 1:length(names(filters))){
  name_filter = gsub("_"," ",names(filters)[filter])
  print(name_filter)
  coefs=t(sapply(1:lags-1,function(x){y=summary(model_data_quality_predictiveness(filters[[filter]], x, study_length))$coef[,3];sign(y)*-log10((1-pnorm(abs(y)))*2)}))
}

print_summary(filters)


############### Table showing differences for different phone makes and demographics 

weekly_coverage = function(stream, shift, ...){
  data = readRDS(paste(output_filepath, "/Preprocessed_Data/Group/", stream, "_bursts.rds", sep="")) %>% data.frame %>% subset(complete.cases(.))
  
  data[,"zeroed_week"] = data[,"zeroed"] %/% 7 + shift
  return(
    data %>% group_by(patient, zeroed_week) %>% summarize(
      mean_num_bursts_coverage = mean(num_bursts_coverage),
      mean_within_burst_length_coverage = mean(within_burst_length_coverage),
      mean_within_burst_frequency_coverage = mean(within_burst_frequency_coverage),
      mean_total_coverage = sum(total_coverage)/7
    )
  )
}
create_zeroed_columns = function(data, shift){
  data[,"zeroed"] = 0
  mins = data %>% group_by(patient) %>% summarise(min_date = min(numeric_date))
  for(pat in patient_names){
    sub = which(data[,"patient"]==pat)
    data[sub,"zeroed"] = data[sub,"numeric_date"]-unlist((mins %>% filter(patient == pat))[,"min_date"])
  }
  data[,"zeroed_week"] = data[,"zeroed"] %/% 7 + shift
  return(data)
}


weekly_accelerometer = weekly_coverage("accelerometer", shift = SHIFT)
weekly_gps = weekly_coverage("gps", shift = SHIFT)

timings = readRDS(paste(output_filepath, "/Processed_Data/Group/survey_timings.rds", sep="")) %>% data.frame %>% subset(complete.cases(.))
timings[,"numeric_date"] =as.numeric(as.Date(as.POSIXct(timings[,"Notified"],origin="1970-01-01")))
colnames(timings)[which(colnames(timings) == "Person")] = "patient"
timings = timings %>% data.frame
timings[,"time_to_present"] = log10(timings[,"Present"]-timings[,"Notified"])
timings[,"time_to_submitted"] = log10(timings[,"Submitted"]-timings[,"Present"])
timings = create_zeroed_columns(timings, shift = SHIFT)
timings = timings[complete.cases(timings)&(!is.infinite(timings[,"time_to_present"]))&(!is.infinite(timings[,"time_to_submitted"])),]

weekly_timings = timings %>% group_by(patient, zeroed_week) %>% summarize(
  mean_time_to_present = mean(time_to_present),
  mean_time_to_submitted = mean(time_to_submitted)
) %>% data.frame

surveys = list()
for(patient_name in patient_names){
  patient_survey_filename = paste(output_filepath, "/Preprocessed_Data/Individual/",patient_name, "/survey_data.rds",sep="")
  if(file.exists(patient_survey_filename))
    surveys[[patient_name]] = readRDS(patient_survey_filename) %>%
      dplyr::filter(question.text %in% unlist(filters)) %>%
      group_by(survey_id, timestamp) %>%
      summarize(count=n(), mean_score = mean(as.numeric(answer),na.rm=T), completion = sum(!is.na(as.numeric(answer)))) %>%
      data.frame %>% mutate(patient = patient_name)
}
surveys = do.call(rbind, surveys)
surveys[,"date"] = as.factor(as.Date(as.POSIXct(surveys[,"timestamp"],origin="1970-01-01")))
surveys[,"numeric_date"] = as.numeric(surveys[,"date"])
surveys = create_zeroed_columns(surveys, shift = 0)
weekly_surveys = surveys %>% group_by(patient, zeroed_week) %>% summarize(mean_score = mean(mean_score), completion = sum(completion))



data=Reduce(function(...) merge(..., all = TRUE, by = c("patient", "zeroed_week")), 
            list(weekly_accelerometer, weekly_gps, weekly_timings, weekly_surveys))

clinic_data = read.xlsx("Studies/John_Schizophrenia/Documents/Clinic_Data.xlsx")
clinic_data %<>% data.frame %>% dplyr::select("Phone.ID", "Phone.Type") %>% slice(1:18) %>% data.frame
phone_type_dict = as.list(setNames(clinic_data[,"Phone.Type"], clinic_data[,"Phone.ID"]))
data[,"Phone_Type"] = phone_type_dict[data[,"patient"]] %>% unlist()

summary_data = data %>% group_by(Phone_Type) %>% summarize(
  mean_coverage.acc = mean(mean_total_coverage.acc, na.rm=T),
  mean_coverage.gps = mean(mean_total_coverage.gps, na.rm=T),
  mean_time_to_present   = mean(mean_time_to_present, na.rm=T),
  mean_time_to_submitted = mean(mean_time_to_submitted, na.rm=T),
  n = n_distinct(patient)
) %>% ungroup %>% data.frame

































