2_1_regain_parameters_parallel.R 18.5 KB
Newer Older
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
# Working directory with temperature data
path_home <- 'C:/Users/luroth/PycharmProjects/htfp_wheat_canopy_height_processing'
path_simulation <- 'E:/Simulation/Runs'
setwd(path_home)


# Libraries to use
library(readr)
library(tidyr)
library(purrr)
library(ggplot2)
library(lubridate)
library(gridExtra)
library(broom)
library(zoo)
library(plyr)
library(stringr)
library(SpATS)

library(foreach)
library(dplyr)


source("R/Model/Spline_QMER.R")
source("R/Model/Dose_response.R")
#source("R/Model/FitREML.R")
source("R/Model/FitSpATS.R")
source("R/Model/Graphs.R")

luroth's avatar
luroth committed
30
31
start_run <-  1
max_runs <- 1
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
number_of_cpus <- 35

sigma_error <- 10

# Plot control overview
plot_ids <- c(
  "FIP20150006",
  "FIP20160002",
  "FIP20170009",
  "FIP20180013",
  "FIP20190003")



## Measurement frequencies
measurement_dates_freq_3d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 3), "%m%d")
)

measurement_dates_sets <- list(
                               "3 d" = measurement_dates_freq_3d
                               )

# Read data
df_designs <- read_csv('Simulation/designs.csv')
df_designs <- df_designs %>% mutate(plot.discrete_x = plot.row * if_else(plot.replication > 1, 2, 1),
                                    plot.discrete_y = plot.range * if_else(plot.replication > 1, 2, 1))

df_genotypes <- read_csv('Simulation/genotypes.csv')
df_temp <- read_csv('Simulation/covariate_temp.csv')

#run <- 1
#for(run in start_run:max_runs) {
  
  
cl <- parallel::makeCluster(number_of_cpus)
doParallel::registerDoParallel(cl)
foreach(run = start_run:max_runs, .verbose = TRUE,
        .packages = c("readr", "tidyr", "purrr", "ggplot2", "lubridate", "gridExtra", "plyr", "stringr", "SpATS", "dplyr", "scam")
) %dopar% {

  # Validation data
  df_genotypes_yearsite_true <- read_csv(paste0(path_simulation, "/", run, "/genotype_yearsite_params.csv"))
  df_genotypes_yearsite_true <- df_genotypes_yearsite_true %>% mutate(
    bplm_slope = bplm_Asym / (bplm_cOpt- bplm_c0))
  df_genotypes_yearsite_true <- df_genotypes_yearsite_true %>%
    pivot_longer(c(start_growth, stop_growth, bplm_c0, bplm_cOpt, bplm_Asym, final_height, bplm_slope), names_to = "parameter")
  
  df_genotypes_true <- read_csv(paste0(path_simulation, "/", run, "/genotypes_params.csv"))
  df_genotypes_true <- df_genotypes_true %>%
    pivot_longer(c(start_growth, stop_growth, bplm_c0, bplm_cOpt, bplm_Asym), names_to = "parameter")

  print(paste0("Run ", run))
  
  df_trait_values <- read_csv(paste0(path_simulation, "/", run, "/trait_values.csv"), col_types = cols(
    plot.UID = col_character(),
    method.id = col_double(),
    method.name = col_character(),
    trait.id = col_double(),
    trait.name = col_character(),
    trait.label = col_character(),
    responsible = col_character(),
    timestamp = col_datetime(format = ""),
    value = col_double()))
  

    ### Merge design and plot information
    df_values_for_fit_orig <- inner_join(inner_join(df_trait_values, df_designs, by="plot.UID"), df_genotypes, by="genotype.id")

    i <- length(measurement_dates_sets)
    i <- 1
    for(i in length(measurement_dates_sets):1) {
      
      measurement_dates_set_name = names(measurement_dates_sets)[[i]]
      measurement_dates_set = measurement_dates_sets[[i]]
      set <- paste0("set", str_replace_all(measurement_dates_set_name, " ", ""))
      print(paste0("Set: ", measurement_dates_set_name))
      
      files <- Sys.glob(paste0(path_simulation, "/", run, "/", set, "_plot_true_versus_predict_mod2.csv"))
      
      if(length(files)==0) {
        
        #try({
        
        df_values_for_fit <- df_values_for_fit_orig %>%
          filter(format(timestamp, "%m%d") %in% measurement_dates_set)

        # First stage BLUEs calculation
        df_primary_trait_BLUEs <- df_values_for_fit %>%
          mutate(year_site.UID_ = year_site.UID) %>%
          group_by(year_site.UID_, timestamp) %>%
          nest() %>%
          partition(cluster) %>%
          mutate(BLUEs = map(data, fit_SpATS, paste(model, parameter), use_weights =FALSE, use_checks=TRUE)) %>%
          collect()
        df_primary_trait_BLUEs <- df_primary_trait_BLUEs %>% unnest(BLUEs) %>% unnest(BLUE)
        df_primary_trait_BLUEs
        
        # Add timepoint of preliminar measurement and value delta (growth) to each timepoint
        df_values_for_fit <- df_values_for_fit %>% group_by(plot.UID) %>%
          arrange(timestamp) %>% mutate(lag_timestamp = lag(timestamp), value_delta = value - lag(value))
        # Remove first measurements, they do not suite as delta measurement
        df_values_for_fit <- df_values_for_fit %>% filter(!is.na(lag_timestamp))
        # Calcualte growth rate
        df_values_for_fit <- df_values_for_fit %>% group_by(plot.UID) %>%
          mutate(time_diff =  difftime(timestamp, lag_timestamp, units = "hours"))
        
        
        ###############################################
        ### Regain parameters from simulated data
        
        df_values_original <- df_values_for_fit
        
        
        ### Fit p-splines
        print("Fit spline")
        df_spline_model <- df_values_for_fit %>%
          ungroup() %>%
          group_by(plot.UID, year_site.UID) %>%
          nest() %>%
          mutate(spline_model = map(data,
                                    ~fit_scam_spline(.$timestamp, .$value)))
        
        df_spline_model <- df_spline_model %>% select(-data)
        
        # Predict value with spline model
        time_interval <- 60*60*12
        prediction_timepoints <- df_values_for_fit %>%
          group_by(year_site.UID) %>%
          filter(timestamp > min(timestamp) & timestamp < max(timestamp)) %>%
          summarize(prediction_timepoint = list(seq(round_any(min(timestamp), time_interval, f= ceiling),
                                                    round_any(max(timestamp) -days(5), time_interval, f= floor),
                                                    time_interval)))
        predictions <- inner_join(df_spline_model, prediction_timepoints, by="year_site.UID")
        
        df_spline_predicts <- predictions %>%
          ungroup() %>%
          group_by(plot.UID) %>%
          do(spline_predicts = tibble(
            predict = predict_scam_spline(.$spline_model[[1]], list(x=.$prediction_timepoint[[1]])),
            predict_se = predict_scam_spline(.$spline_model[[1]], list(x=.$prediction_timepoint[[1]]), se = T),
            predict_deriv = predict_scam_spline(.$spline_model[[1]], list(x=.$prediction_timepoint[[1]]), deriv = T),
            timestamp = .$prediction_timepoint[[1]]))

        
        # Find start/stop and maximum height
        print("Find start/stop")
        df_growth_phase_predicts <- df_spline_predicts %>% ungroup() %>% group_by(plot.UID) %>%
          mutate(growth_phase_params = map(spline_predicts, find_start_stop_growth_phase))
        
        # Extract predicted values
        df_growth_phase_predicts <- df_growth_phase_predicts %>%
          unnest(growth_phase_params)
        
        measurements <- df_values_for_fit %>% filter(plot.UID %in% plot_ids)
        predicts <- df_growth_phase_predicts  %>% filter(plot.UID %in% plot_ids) %>% unnest(spline_predicts)
        predicts <- inner_join(predicts, df_designs %>% select(plot.UID, genotype.id), by="plot.UID")
        predicts <- inner_join(predicts, df_genotypes, by="genotype.id")
        
        # Add predicted parameters to plots to perform two-stage processing
        df_growth_phase_predicts_ <- df_growth_phase_predicts %>%
          mutate(
            predict_final_height = predict_final_value,
            predict_final_height_se = predict_final_value_se,
            predict_start_growth = yday(predict_start_growth),
            predict_stop_growth = yday(predict_stop_growth)) %>%
          select(plot.UID,
                 predict_final_height, predict_final_height_se,
                 predict_start_growth, predict_start_growth_se,
                 predict_stop_growth, predict_stop_growth_se)
        df_growth_phase_predicts_values <- df_growth_phase_predicts_ %>%
          select(plot.UID, predict_start_growth, predict_stop_growth, predict_final_height) %>%
          pivot_longer(
            cols= c(predict_final_height, predict_start_growth, predict_stop_growth), names_to = "parameter", values_to = "predict")
        df_growth_phase_predicts_se <- df_growth_phase_predicts_ %>%
          select(plot.UID, predict_final_height_se,
                 predict_start_growth_se, predict_stop_growth_se) %>%
          pivot_longer(
            cols= c(
              predict_start_growth_se, predict_stop_growth_se, predict_final_height_se), names_to = "parameter", values_to = "se") %>%
          mutate(parameter = str_sub(parameter, 1, -4))
        
        df_growth_phase_predicts_ <- inner_join(df_growth_phase_predicts_values, df_growth_phase_predicts_se, by=c("plot.UID", "parameter"))
        
        # Comparison with true values
        df_genotype_predicts <- df_growth_phase_predicts_ %>% ungroup() %>%
          select(plot.UID, parameter, predict, se) %>%
          mutate(parameter = str_remove(parameter, "predict_")) %>%
          inner_join(df_genotypes_yearsite_true, by=c("plot.UID", "parameter"))
        
        ## Percentile predictions
        # Add predicted parameters to plots to perform two-stage processing
        df_growth_phase_predicts_ <- df_growth_phase_predicts %>%
          mutate(
            predict_p15 = yday(predict_p15),
            predict_p95 = yday(predict_p95)) %>%
          select(plot.UID,
                 predict_p15, predict_p15_se,
                 predict_p95, predict_p95_se)
        df_growth_phase_predicts_values <- df_growth_phase_predicts_ %>%
          select(plot.UID, predict_p15, predict_p95) %>%
          pivot_longer(
            cols= c(predict_p15, predict_p95), names_to = "parameter", values_to = "predict")
        df_growth_phase_predicts_se <- df_growth_phase_predicts_ %>%
          select(plot.UID, 
                 predict_p15_se, predict_p95_se) %>%
          pivot_longer(
            cols= c(
              predict_p15_se, predict_p95_se), names_to = "parameter", values_to = "se") %>%
          mutate(parameter = str_sub(parameter, 1, -4))
        
        df_growth_phase_predicts_ <- inner_join(df_growth_phase_predicts_values, df_growth_phase_predicts_se, by=c("plot.UID", "parameter"))
        
        # Comparison with true values
        df_genotype_predicts_percentile <- df_growth_phase_predicts_ %>% ungroup() %>%
          select(plot.UID, parameter, predict, se) %>%
          mutate(parameter = str_remove(parameter, "predict_")) %>%
          mutate(parameter = case_when(
            parameter == "p15" ~ "start_growth",
            parameter == "p95" ~ "stop_growth",
            TRUE ~"unknown"
          )) %>%
          inner_join(df_genotypes_yearsite_true, by=c("plot.UID", "parameter"))
        
        ########################################
        ## Fit growth response curves
        print("Fit growth curves")
        
        # Filter measurement data for growth period
        df_growth_fit <- inner_join(df_values_original, df_growth_phase_predicts, by="plot.UID")
        
        # Filter for growth period
        df_growth_fit <- df_growth_fit %>%
          filter(timestamp >= predict_start_growth) %>%
          filter(timestamp <= predict_stop_growth)
        
        ## Add temperature course to PH measurements
        # Generate temperature course lookup table
        df_covar_course_lookup <- df_growth_fit %>% ungroup() %>%
          select(timestamp, lag_timestamp) %>% unique() %>%
          mutate(temp_course_id = seq(n()))
        df_growth_fit <- inner_join(df_growth_fit, df_covar_course_lookup, by=c("timestamp", "lag_timestamp"))
        # Fill with values
        extract_covar <- function(df, from, to) {
          df_ <- df %>% filter(timestamp > from, timestamp <= to)
          return(df_)
        }
        df_temp_course_lookup <- df_covar_course_lookup %>% group_by(temp_course_id) %>%
          nest() %>%
          mutate(temperature_course =
                   map(data, ~ extract_covar(df_temp, .$lag_timestamp, .$timestamp))) %>%
          unnest(data) %>%
          select(-timestamp, -lag_timestamp)
        
        df_temp_course_lookup <- df_temp_course_lookup %>%
          group_by(temp_course_id) %>%
          mutate(mean_temp = map_dbl(temperature_course, ~mean(.$value)))
        
        # Join temperature course to PH measurements
        df_growth_fit <- inner_join(df_growth_fit, df_temp_course_lookup, by="temp_course_id")
        remove(df_temp_course_lookup)
        
        df_growth_fit <- df_growth_fit %>% mutate(temps = map(temperature_course, function (df) df$value))
        
        # Calcualte growth rate
        df_growth_fit_ <- df_growth_fit %>%
          group_by(plot.UID) %>%
          mutate(time_diff = difftime(timestamp, lag_timestamp, units = "hours"))
        df_growth_fit_ <- df_growth_fit_ %>% mutate(value_rate = value_delta / as.numeric(time_diff))
        df_growth_fit_ <- df_growth_fit_ %>% mutate(value_delta = value_rate, temps = mean_temp)
        
        # Simple lm
        models_simple_lm <- df_growth_fit_ %>%
          group_by(year_site.UID, plot.UID, method.id, trait.label) %>%
          nest() %>%
          mutate(model = map(data, ~lm(value_rate ~ mean_temp + 1, data = .)))
        
        models_simple_lm <- models_simple_lm %>%
          mutate(coefs = map(model, ~coef(.)),
                 coefs_se = map(model, ~coef(summary(.))[, "Std. Error"]))
        
        # Extract parameters
        params_simple_lm <- models_simple_lm %>%
          unnest_wider(coefs) %>%
          rename(lm_intercept = "(Intercept)", lm_slope = mean_temp) %>%
          unnest_wider(coefs_se) %>%
          rename(lm_intercept_se = "(Intercept)", lm_slope_se = mean_temp) %>%
          mutate(lm_intercept = - lm_intercept / lm_slope)
        
        # Convert to long format
        params_simple_lm_long <- params_simple_lm %>%
          pivot_longer(c(lm_intercept, lm_slope), names_to = "parameter", values_to = "value") %>%
          select(-data) %>%
          mutate(se = if_else(parameter == "lm_intercept", lm_intercept_se, lm_slope_se)) %>%
          select(-lm_intercept_se, -lm_slope_se)
        
        ## Simple bplm
        
        print("Simple bplm")
        models_simple_bplm <- df_growth_fit_ %>%
          group_by(year_site.UID, plot.UID) %>%
          do(model = bplm_fit(.,  fixed_par = c(sigma_error = sigma_error)))
        
        params_simple_bplm_long <- models_simple_bplm %>% unnest(cols = c(model))
        
        print("Simple asym")
        models_simple_asym <- df_growth_fit_ %>%
          group_by(year_site.UID, plot.UID) %>%
          do(model = asym_fit(.,  fixed_par = c(sigma_error = sigma_error)))
        
        params_simple_asym_long <- models_simple_asym %>% unnest(cols = c(model))      
        
        
        # Fit asym
        print("Course asym")
        models_course_asym <- df_growth_fit %>%
          group_by(year_site.UID, plot.UID) %>%
          do(model = asym_fit(.,  fixed_par = c(sigma_error = sigma_error)))
        
        params_course_asym_long <- models_course_asym %>% unnest(cols = c(model))    
        
        # Fit bplm
        print("Course bplm")
        models_course_bplm <- df_growth_fit %>%
          group_by(year_site.UID, plot.UID) %>%
          do(model = bplm_fit(.,  fixed_par = c(sigma_error = sigma_error)))
        params_course_bplm_long <- models_course_bplm %>% unnest(cols = c(model))
        
        
        # Comparison with true values
        df_genotype_predicts_simple <- params_simple_bplm_long %>% ungroup() %>%
          select(plot.UID, parameter, value, se) %>%
          rename(predict = value) %>%
          inner_join(df_genotypes_yearsite_true)
        
        df_genotype_predicts_asym_simple <- params_simple_asym_long %>% ungroup() %>%
          select(plot.UID, parameter, value, se) %>%
          mutate(parameter = case_when(
            parameter == "asym_c0" ~ "bplm_c0",
            parameter == "asym_Asym" ~ "bplm_Asym",
            TRUE ~ "bplm_slope"
          )) %>%        
          rename(predict = value) %>%
          inner_join(df_genotypes_yearsite_true)      
        
        df_genotype_predicts_course <- params_course_bplm_long %>% ungroup() %>%
          select(plot.UID, parameter, value, se) %>%
          rename(predict = value) %>%
          inner_join(df_genotypes_yearsite_true)
        
        df_genotype_predicts_asym_course <- params_course_asym_long %>% ungroup() %>%
          select(plot.UID, parameter, value, se) %>%
          mutate(parameter = case_when(
            parameter == "asym_c0" ~ "bplm_c0",
            parameter == "asym_Asym" ~ "bplm_Asym",
            TRUE ~ "bplm_slope"
          )) %>%
          rename(predict = value) %>%
          inner_join(df_genotypes_yearsite_true)     
        
        df_genotype_predicts_lm_simple <- params_simple_lm_long %>% ungroup() %>%
          select(plot.UID, parameter, value, se) %>%
          mutate(parameter = case_when(
            parameter == "lm_intercept" ~ "bplm_c0",
            TRUE ~ "bplm_slope"
          )) %>%
          rename(predict = value) %>%
          inner_join(df_genotypes_yearsite_true)
        
        
        df_genotype_predicts_lm_simple$model <- "lm_simple"
        df_genotype_predicts_asym_simple$model <- "asym_simple"
        df_genotype_predicts_simple$model <- "bplm_simple"
        df_genotype_predicts_course$model <- "bplm_course"
        df_genotype_predicts_asym_course$model <- "asym_course"
        df_genotype_predicts$model <- "spline"
        df_genotype_predicts_percentile$model <- "percentile"
        
        
        df_all_params <- bind_rows(df_genotype_predicts_lm_simple, df_genotype_predicts_asym_simple,
                                   df_genotype_predicts_simple, df_genotype_predicts_course, df_genotype_predicts_asym_course, 
                                   df_genotype_predicts, df_genotype_predicts_percentile)
        df_all_params <- inner_join(df_all_params, df_designs, by= c("plot.UID", "year_site.UID", "genotype.id"))
        df_all_params <- df_all_params %>% mutate(label = paste(parameter, model, year_site.UID, sep = "_"))
        
        # 
        # plot <- ggplot(data=df_all_params,
        #   aes(x=value, y=predict)) +
        #    geom_point() +
        #    facet_wrap(~ label, scales="free") +
        #   ggtitle(paste("Run:", run, set))
        # plot(plot)
        
        df_all_params$set <- set
        write_csv(df_all_params, paste0(path_simulation, "/", run, "/", set, "_plot_true_versus_predict_mod2.csv"))
        
        
      } else {
        print("Results present, skip")
      }
      
  }
}