rm(list = ls())
library("data.table")
library("ggplot2")
setwd("~/Coding/khanna-research/cadre/python/output")

agent_dt <- fread("agent_log_5.csv")

nticks <- max(agent_dt$tick)

### Plot current smoking proportion for one year after release
released <- agent_dt[(current_incarceration_status != 1) & (last_release_tick > 0)]
released[,ticks_since_release := tick-last_release_tick]

## WITH BINS
#bin_size = 10
#time_horizon = 360
#bin_start <- seq(0, time_horizon-bin_size, bin_size)
#bin_end <- seq(bin_size, time_horizon, bin_size)
#bins <- data.frame(bin_start, bin_end)

#x <- c()

#for (row in 1:nrow(bins)) {
#  b_start <- bins$bin_start[row]
#  b_end <- bins$bin_end[row]
#  within_bin <- released[(ticks_since_release >= b_start) & (ticks_since_release < b_end)]
#  total = nrow(within_bin)
#  print(total)
#  current_smoking = within_bin[smoking_status=="Current", .N / total]
#  x <- append(x, current_smoking)
#}

#df <- data.frame(bin_start,x)

#ggplot(df, mapping = aes(x = bin_start + 5, y = x)) + 
#  geom_point()

time_horizon = 365
current <- c()
former <- c()

# WITH CONTINUOUS SAMPLING OF AGENTS
for (ticks_out in 1:time_horizon){
  at_tick <- released[ticks_since_release == ticks_out]
  total = nrow(at_tick)
  #print(total)
  current_smoking <- at_tick[smoking_status=="Current", .N / total]
  former_smoking <- at_tick[smoking_status=="Current", .N / total]
  current <- append(current, current_smoking)
  former <- append(former, former_smoking)
}

df <- data.frame(current, former, ticks_since_release=1:time_horizon)

ggplot(df, mapping = aes(x = ticks_since_release, y = current)) + 
  geom_point()

### Plot current smoking proportion of contacts of released
network_dt <- fread("network_log_5.csv")
network_dt
##            tick    p1    p2
##        1:     1     0   892
##        2:     1     0   948
##        3:     1     0  1241
##        4:     1     0  1368
##        5:     1     0  4220
##       ---                  
## 22158213: 10941 15543 15628
## 22158214: 10941 15547 15626
## 22158215: 10941 15567 15577
## 22158216: 10941 15591 15607
## 22158217: 10941 15630 15651
## for each released person, find contacts on release and add them to a data frame with the tick the principal was released

recently_released <- released[ticks_since_release==1]
length(recently_released$id) - length(unique(recently_released$id)) # repeat incarcerations
## [1] 534
contact_data <- data.table(contact=c(),principal_release_tick=c())
if (file.exists("contact_data.csv")){
  contact_data <- fread("contact_data.csv")
  contact_data[,V1:=NULL]
}else{
  for (row in 1:nrow(recently_released)){
    released_tick <- recently_released$last_release_tick[row]
    
    last_network_log_tick <- released_tick - (released_tick %% 10) + 1
    
    principal <- recently_released$id[row]
    
    contacts <- network_dt[(tick == last_network_log_tick) & ((p1 == principal) | (p2 == principal))]
    contact_ids <- c(contacts$p1, contacts$p2)
    contact_ids <- setdiff(contact_ids, principal)
    
    for (contact_id in contact_ids){
      contact_data <- rbindlist(list(contact_data, list(contact=contact_id, principal_release_tick=released_tick)))
    }
    if (row %% 10 == 0){
      print(paste("finished", row))
    }
  }
  
  write.csv(contact_data, "contact_data.csv")
}
contact_data
##       contact principal_release_tick
##    1:    1094                     66
##    2:    2383                     66
##    3:    6728                     66
##    4:    7472                     66
##    5:    8675                     66
##   ---                               
## 5177:   11870                  10925
## 5178:   12003                  10925
## 5179:   14324                  10925
## 5180:   14456                  10925
## 5181:   15190                  10925
## add ticks_since_tie_released column to contacts (in agent_dt)
follow_length <- 365

if (file.exists("contact_statuses_after_principal_release.csv")){
  contacts <- fread("contact_statuses_after_principal_release.csv")
  contacts[,V1:=NULL]
}else {
  ever_contacts <- agent_dt[id %in% contact_data$contact, .(tick, id, alc_use_status, smoking_status, exit_at_tick)]
  ever_contacts[,ticks_since_tie_released := -1]
  ever_contacts
  for (i in 1:nrow(contact_data)){
    exit_check <- ever_contacts[(id == contact_data$contact[i]) & (exit_at_tick != -1)]
    hard_stop <- if(nrow(exit_check) > 0) exit_check$exit_at_tick else nticks 
    end_tick <- min(contact_data$principal_release_tick[i] + follow_length, hard_stop)
    last_follow <- nrow(ever_contacts[(id == contact_data$contact[i]) & 
                    (tick >= contact_data$principal_release_tick[i]) & (tick <= end_tick)]) - 1
    ever_contacts[(id == contact_data$contact[i]) & 
               (tick >= contact_data$principal_release_tick[i]) & (tick <= end_tick)]$ticks_since_tie_released <- seq(0, last_follow)
    if (i %% 10 == 0){
      print(paste("finished", i))
    }
  }
  contacts <- ever_contacts[ticks_since_tie_released != -1]
  write.csv(contacts, "contact_statuses_after_principal_release.csv")
}
contacts
##           tick    id alc_use_status smoking_status exit_at_tick
##       1:    66  1094              2          Never           -1
##       2:    66  2383              1          Never           -1
##       3:    66  6728              1          Never           -1
##       4:    66  7472              2          Never           -1
##       5:    66  8675              2          Never           -1
##      ---                                                       
## 1741511: 10950 15233              1          Never           -1
## 1741512: 10950 15375              0          Never           -1
## 1741513: 10950 15393              1          Never           -1
## 1741514: 10950 15442              1         Former           -1
## 1741515: 10950 15500              1          Never           -1
##          ticks_since_tie_released
##       1:                        0
##       2:                        0
##       3:                        0
##       4:                        0
##       5:                        0
##      ---                         
## 1741511:                       63
## 1741512:                       80
## 1741513:                      257
## 1741514:                       80
## 1741515:                      148
#current <- c()
#former <- c()
for (ticks_out in 1:follow_length){
  at_tick <- contacts[ticks_since_tie_released == ticks_out]
  total = nrow(at_tick)
  print(total)
  current_smoking <- at_tick[smoking_status=="Current", .N / total]
  former_smoking <- at_tick[smoking_status=="Current", .N / total]
  current <- append(current, current_smoking)
  former <- append(former, former_smoking)
}
## [1] 5179
## [1] 5177
## [1] 5177
## [1] 5177
## [1] 5177
## [1] 5177
## [1] 5177
## [1] 5176
## [1] 5175
## [1] 5175
## [1] 5175
## [1] 5174
## [1] 5174
## [1] 5174
## [1] 5173
## [1] 5172
## [1] 5172
## [1] 5172
## [1] 5171
## [1] 5171
## [1] 5171
## [1] 5167
## [1] 5164
## [1] 5157
## [1] 5155
## [1] 5155
## [1] 5145
## [1] 5139
## [1] 5133
## [1] 5127
## [1] 5127
## [1] 5127
## [1] 5123
## [1] 5123
## [1] 5121
## [1] 5115
## [1] 5106
## [1] 5106
## [1] 5106
## [1] 5102
## [1] 5088
## [1] 5085
## [1] 5085
## [1] 5083
## [1] 5076
## [1] 5072
## [1] 5071
## [1] 5070
## [1] 5070
## [1] 5064
## [1] 5064
## [1] 5064
## [1] 5063
## [1] 5063
## [1] 5061
## [1] 5061
## [1] 5057
## [1] 5057
## [1] 5057
## [1] 5056
## [1] 5056
## [1] 5055
## [1] 5048
## [1] 5043
## [1] 5041
## [1] 5038
## [1] 5038
## [1] 5038
## [1] 5037
## [1] 5037
## [1] 5037
## [1] 5037
## [1] 5036
## [1] 5036
## [1] 5036
## [1] 5031
## [1] 5025
## [1] 5024
## [1] 5019
## [1] 5013
## [1] 5005
## [1] 5004
## [1] 4998
## [1] 4994
## [1] 4994
## [1] 4993
## [1] 4993
## [1] 4993
## [1] 4991
## [1] 4991
## [1] 4986
## [1] 4986
## [1] 4983
## [1] 4983
## [1] 4983
## [1] 4976
## [1] 4976
## [1] 4968
## [1] 4968
## [1] 4968
## [1] 4968
## [1] 4968
## [1] 4964
## [1] 4961
## [1] 4960
## [1] 4957
## [1] 4956
## [1] 4956
## [1] 4952
## [1] 4949
## [1] 4948
## [1] 4948
## [1] 4948
## [1] 4947
## [1] 4947
## [1] 4947
## [1] 4947
## [1] 4947
## [1] 4946
## [1] 4945
## [1] 4945
## [1] 4938
## [1] 4937
## [1] 4937
## [1] 4933
## [1] 4933
## [1] 4933
## [1] 4923
## [1] 4914
## [1] 4913
## [1] 4913
## [1] 4901
## [1] 4901
## [1] 4895
## [1] 4889
## [1] 4885
## [1] 4884
## [1] 4876
## [1] 4871
## [1] 4870
## [1] 4870
## [1] 4865
## [1] 4863
## [1] 4856
## [1] 4854
## [1] 4847
## [1] 4846
## [1] 4846
## [1] 4837
## [1] 4836
## [1] 4836
## [1] 4832
## [1] 4831
## [1] 4830
## [1] 4829
## [1] 4829
## [1] 4829
## [1] 4829
## [1] 4829
## [1] 4829
## [1] 4825
## [1] 4813
## [1] 4808
## [1] 4807
## [1] 4807
## [1] 4807
## [1] 4805
## [1] 4805
## [1] 4800
## [1] 4797
## [1] 4797
## [1] 4797
## [1] 4791
## [1] 4790
## [1] 4790
## [1] 4783
## [1] 4783
## [1] 4783
## [1] 4777
## [1] 4775
## [1] 4772
## [1] 4768
## [1] 4765
## [1] 4765
## [1] 4758
## [1] 4756
## [1] 4756
## [1] 4750
## [1] 4737
## [1] 4737
## [1] 4737
## [1] 4737
## [1] 4737
## [1] 4736
## [1] 4728
## [1] 4727
## [1] 4725
## [1] 4725
## [1] 4725
## [1] 4725
## [1] 4721
## [1] 4717
## [1] 4713
## [1] 4713
## [1] 4713
## [1] 4713
## [1] 4713
## [1] 4711
## [1] 4707
## [1] 4706
## [1] 4706
## [1] 4706
## [1] 4699
## [1] 4699
## [1] 4698
## [1] 4694
## [1] 4691
## [1] 4682
## [1] 4682
## [1] 4676
## [1] 4672
## [1] 4671
## [1] 4665
## [1] 4661
## [1] 4655
## [1] 4653
## [1] 4651
## [1] 4651
## [1] 4650
## [1] 4649
## [1] 4649
## [1] 4647
## [1] 4647
## [1] 4635
## [1] 4635
## [1] 4635
## [1] 4635
## [1] 4626
## [1] 4623
## [1] 4623
## [1] 4621
## [1] 4621
## [1] 4621
## [1] 4620
## [1] 4619
## [1] 4616
## [1] 4612
## [1] 4611
## [1] 4599
## [1] 4593
## [1] 4593
## [1] 4593
## [1] 4592
## [1] 4588
## [1] 4582
## [1] 4582
## [1] 4579
## [1] 4574
## [1] 4572
## [1] 4572
## [1] 4569
## [1] 4567
## [1] 4567
## [1] 4567
## [1] 4559
## [1] 4557
## [1] 4556
## [1] 4556
## [1] 4555
## [1] 4555
## [1] 4555
## [1] 4545
## [1] 4540
## [1] 4539
## [1] 4539
## [1] 4538
## [1] 4529
## [1] 4529
## [1] 4528
## [1] 4528
## [1] 4528
## [1] 4527
## [1] 4524
## [1] 4521
## [1] 4521
## [1] 4521
## [1] 4519
## [1] 4510
## [1] 4510
## [1] 4510
## [1] 4502
## [1] 4502
## [1] 4502
## [1] 4494
## [1] 4493
## [1] 4489
## [1] 4479
## [1] 4478
## [1] 4458
## [1] 4448
## [1] 4447
## [1] 4447
## [1] 4442
## [1] 4433
## [1] 4427
## [1] 4427
## [1] 4423
## [1] 4420
## [1] 4420
## [1] 4420
## [1] 4420
## [1] 4420
## [1] 4414
## [1] 4399
## [1] 4399
## [1] 4390
## [1] 4389
## [1] 4388
## [1] 4388
## [1] 4382
## [1] 4381
## [1] 4381
## [1] 4381
## [1] 4381
## [1] 4381
## [1] 4381
## [1] 4369
## [1] 4369
## [1] 4362
## [1] 4362
## [1] 4362
## [1] 4362
## [1] 4358
## [1] 4357
## [1] 4356
## [1] 4355
## [1] 4352
## [1] 4351
## [1] 4351
## [1] 4351
## [1] 4344
## [1] 4332
## [1] 4329
## [1] 4329
## [1] 4327
## [1] 4327
## [1] 4321
## [1] 4317
## [1] 4317
## [1] 4317
## [1] 4314
## [1] 4314
## [1] 4313
## [1] 4313
## [1] 4312
## [1] 4311
## [1] 4311
## [1] 4310
## [1] 4310
## [1] 4307
## [1] 4307
## [1] 4307
## [1] 4305
## [1] 4304
## [1] 4303
df <- data.frame(current, former, ticks_since_release=1:follow_length)

ggplot(df, mapping = aes(x = ticks_since_release, y = current)) + 
  geom_point()