-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
512 lines (512 loc) · 25.3 KB
/
.Rhistory
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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
population_2_parameters$N[1] <- 0
populations_parameters <- list(population_1_parameters, population_2_parameters)
# Setup lab dose rate + sequence
dose_rate = 0.1
sequence <- Get_sequence(file_path = paste0(data_path, "/SEQ_file.SEQ"),
lab_dose_rate = dose_rate)[1:8]
# Display parameters of population 1
print(populations_parameters[1])
# --------- Generate OSL-TL curves and retrive metrics ---------
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = population_1_parameters,
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_1 <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "S1",
records_to_extract_data_from = NULL)
# Source 2
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = population_2_parameters,
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_2 <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "S2",
records_to_extract_data_from = NULL)
# --------- Define population ratios in the mix sample + mix them using sandbox package ---------
pop_ratios_iteration <- c(0.25, 0.75)
## Create rulebook with population ratios and model parameters as rules in RuleBook
book_osl <- Make_luminescence_rulebook(populations_parameters = populations_parameters,
pop_ratios = pop_ratios_iteration)
# --------- Make samples of the mixtures (mix of source 1 and 2 with ratios defined in "pop_ratios_iteration") ---------
# Set seed for reproducibility
set.seed(2021)
# Take a sample
sample_osl <- sandbox::make_Sample(book=book_osl,
depth=2,
geometry="cylinder",
radius=0.0005,
length=0.001)
# Make aliquots of sample
sample_osl_aliquots <- sandbox::prepare_Aliquot(sample = sample_osl, diameter = 0.5)
# Make a single df from all aliquots
stacked_aliquots <- data.frame()
for (name in names(sample_osl_aliquots)) {
stacked_aliquots <- rbind(stacked_aliquots, sample_osl_aliquots[[name]])
}
# Generate signals based on parameters of mixture
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = prep_parameters_from_aliquot(aliquot = stacked_aliquots),
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_mixed <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "Mixed",
records_to_extract_data_from = NULL)
nb_pop_1 <- nrow(stacked_aliquots[stacked_aliquots$population == 1, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
nb_pop_2 <- nrow(stacked_aliquots[stacked_aliquots$population == 2, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
frac_pop_1 <- nb_pop_1/(nb_pop_1 + nb_pop_2)
frac_pop_2 <- nb_pop_2/(nb_pop_1 + nb_pop_2)
print(frac_pop_1)
print(frac_pop_2)
# --------- Prepare results for unmixing ---------
# Stack the Data Frames on top of each other, filling missing columns with NA
stacked_df <- dplyr::bind_rows(rbind(metrics_1, metrics_1), rbind(metrics_2, metrics_2), metrics_mixed)
# Replace NA values with 0
stacked_df[is.na(stacked_df)] <- 0
# fingerPro (used for unmixing) expects at least two different samples from each source. Artificially create two almost identical ones
stacked_df[2, -1] <- stacked_df[2, -1]*1.001
stacked_df[4, -1] <- stacked_df[4, -1]*1.001
# Add expected population fractions (stemming from the mixing of both sources through sandbox)
stacked_df$Pop_fraction <- c(rep(frac_pop_1, 2), rep(frac_pop_2, 2), rep(NaN, 1))
print(stacked_df)
# Save the Data Frame
#filename <- paste0(as.character(pop_ratios_iteration[1]),"-", as.character(pop_ratios_iteration[2]))
#write.csv(stacked_df, file = paste0(data_path, "/", filename, ".csv"))
# --------- If you want to make a single df for each aliquot, change the above code accordingly: ---------
## make aliquots of sample
sample_osl_aliquots <- prepare_Aliquot(sample = sample_osl, diameter = 1.6)
print(glue("Total number of aliquots: {length(names(sample_osl_aliquots))}"))
metrics_mixed_all <- data.frame()
for (name in names(sample_osl_aliquots)) {
stacked_aliquots <- sample_osl_aliquots[[name]]
print(glue("Aliquot {name}"))
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = prep_parameters_from_aliquot(aliquot = stacked_aliquots),
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
nb_pop_1 <- nrow(stacked_aliquots[stacked_aliquots$population == 1, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
nb_pop_2 <- nrow(stacked_aliquots[stacked_aliquots$population == 2, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
frac_pop_1 <- nb_pop_1/(nb_pop_1 + nb_pop_2)
frac_pop_2 <- nb_pop_2/(nb_pop_1 + nb_pop_2)
metrics_mixed <- Single_record_TL_OSL_metrics_df(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
record_num = keys(metrics_output$TL_metrics)[[1]])
metrics_mixed <- cbind("sources" = rep("Mixed", nrow(metrics_mixed)), metrics_mixed)
metrics_mixed_all <- rbind(metrics_mixed_all, metrics_mixed)
}
library(RLumUnmix)
library(sandbox)
library(dplyr)
library(RLumModel)
data_path <- system.file("extdata", package="RLumUnmix")
# ---------- Set parameters for each source population ----------
population_1_parameters <- RLumModel::.set_pars("Bailey2001")
population_1_parameters$N[2] <- population_1_parameters$N[2]*10
population_2_parameters <- RLumModel::.set_pars("Bailey2001")
population_2_parameters$N[1] <- 0
population_3_parameters <- RLumModel::.set_pars("Bailey2001")
populations_parameters <- list(population_1_parameters, population_2_parameters, population_3_parameters)
# Setup lab dose rate + sequence
dose_rate = 0.1
sequence <- Get_sequence(file_path = paste0(data_path, "/SEQ_file.SEQ"),
lab_dose_rate = dose_rate)[1:8]
# Display parameters of population 1
print(populations_parameters[1])
# --------- Generate OSL-TL curves and retrieve metrics -----------
# Source 1
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = population_1_parameters,
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_1 <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "S1",
records_to_extract_data_from = NULL)
# Source 2
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = population_2_parameters,
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_2 <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "S2",
records_to_extract_data_from = NULL)
# Source 3
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = population_3_parameters,
own_state_parameters = RLumModel::.set_pars("Bailey2001")$n)
metrics_3 <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "S3",
records_to_extract_data_from = NULL)
# ---------- Define population ratios in the mix sample + mix them using sandbox package ----------
pop_ratios_iteration <- c(0.25, 0.5, 0.25)
## Create rulebook with population ratios and model parameters as rules in RuleBook
book_osl <- Make_luminescence_rulebook(populations_parameters = populations_parameters,
pop_ratios = pop_ratios_iteration)
# ---------- Make samples of the mixtures (mix of source 1 and 2 with ratios defined in "pop_ratios_iteration") ----------
# Set seed for reproducibility
set.seed(2021)
# Take a sample
sample_osl <- sandbox::make_Sample(book=book_osl,
depth=2,
geometry="cylinder",
radius=0.0005,
length=0.001)
# Make aliquots of sample
sample_osl_aliquots <- sandbox::prepare_Aliquot(sample = sample_osl, diameter = 0.5)
# Make a single df from all aliquots
stacked_aliquots <- data.frame()
for (name in names(sample_osl_aliquots)) {
stacked_aliquots <- rbind(stacked_aliquots, sample_osl_aliquots[[name]])
}
# Generate signals based on parameters of mixture
metrics_output <- Make_custom_model_and_retrieve_OSL_TL_metrics(
sequence,
dose_rate,
own_parameters = prep_parameters_from_aliquot(aliquot = stacked_aliquots),
own_state_parameters = .set_pars("Bailey2001")$n)
# Example of using a single record (instead of all of them as done in vignette "Preparation of 2 synthetic sources for unmixing using sandbox")
metrics_mixed <- Extract_OSL_TL_metrics_for_all_records(TL_hash = metrics_output$TL_metrics,
OSL_hash = metrics_output$OSL_metrics,
source_number = "Mixed",
records_to_extract_data_from = "Record_1")
nb_pop_1 <- nrow(stacked_aliquots[stacked_aliquots$population == 1, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
nb_pop_2 <- nrow(stacked_aliquots[stacked_aliquots$population == 2, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
nb_pop_3 <- nrow(stacked_aliquots[stacked_aliquots$population == 3, c('population', 'osl_N1', 'osl_N2', 'osl_N3', 'osl_N4')])
frac_pop_1 <- nb_pop_1/(nb_pop_1 + nb_pop_2 + nb_pop_3)
frac_pop_2 <- nb_pop_2/(nb_pop_1 + nb_pop_2 + nb_pop_3)
frac_pop_3 <- nb_pop_3/(nb_pop_1 + nb_pop_2 + nb_pop_3)
print(frac_pop_1)
print(frac_pop_2)
print(frac_pop_3)
# ----------- Prepare results for unmixing ----------
# Stack the Data Frames on top of each other, filling missing columns with NA
stacked_df <- dplyr::bind_rows(rbind(metrics_1, metrics_1), rbind(metrics_2, metrics_2), rbind(metrics_3, metrics_3), metrics_mixed)
# Replace NA values with 0
stacked_df[is.na(stacked_df)] <- 0
# fingerPro (used for unmixing) expects at least two different samples from each source. Artificially create two almost identical ones
stacked_df[2, -1] <- stacked_df[2, -1]*1.001
stacked_df[4, -1] <- stacked_df[4, -1]*1.001
stacked_df[6, -1] <- stacked_df[6, -1]*1.001
# Add expected population fractions (stemming from the mixing of both sources through sandbox)
stacked_df$Pop_fraction <- c(rep(frac_pop_1, 2), rep(frac_pop_2, 2), rep(frac_pop_3, 2), rep(NaN, 1))
print(stacked_df)
# Save the Data Frame
#filename <- paste0(as.character(pop_ratios_iteration[1]),"-", as.character(pop_ratios_iteration[2]),"-", as.character(pop_ratios_iteration[3]))
#write.csv(stacked_df, file = paste0(data_path, "/", filename, ".csv"))
library(RLumUnmix)
library(hash)
library(robustbase)
data_path <- paste0(system.file("extdata", package="RLumUnmix"),"/unmixing_samples/")
# ---------- Get sequence + samples information ----------
#We use two very different pure samples, Fontainebleau (source 1) and Morat (source 2). We then created three mixed samples of different proportions (based on weight), that were each divided into 10 aliquots. We hence have 50 measurements in total (10 for each source and mixtures).
sequence <- Get_sequence(file_path = paste0(data_path, "SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.SEQ"), lab_dose_rate = 0.2)[1:4]
mixed_samples_information <- hash()
# Origin of the sources
mixed_samples_information[["Sources"]][["Source_1"]] <- "FBQ"
mixed_samples_information[["Sources"]][["Source_2"]] <- "Morat"
# Definition of our samples (mixtures): position on carousel + expected proportions
mixed_samples_information[["Sample_1"]][["Positions"]] <- seq(1, 10)
mixed_samples_information[["Sample_1"]][["Proportions"]] <- c(0.5, 0.5)
mixed_samples_information[["Sample_2"]][["Positions"]] <- seq(11, 20)
mixed_samples_information[["Sample_2"]][["Proportions"]] <- c(0.75, 0.25)
mixed_samples_information[["Sample_3"]][["Positions"]] <- seq(21, 30)
mixed_samples_information[["Sample_3"]][["Proportions"]] <- c(0.34, 0.66)
# ---------- Preparation of source data ----------
# Read binx file of the sources
bin_file_SOURCE <- read_BIN2R(file = paste0(data_path,
"SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.binx"),
fastForward = FALSE)
library(RLumUnmix)
library(hash)
library(Luminescence)
library(robustbase)
data_path <- paste0(system.file("extdata", package="RLumUnmix"),"/unmixing_samples/")
# ---------- Get sequence + samples information ----------
#We use two very different pure samples, Fontainebleau (source 1) and Morat (source 2). We then created three mixed samples of different proportions (based on weight), that were each divided into 10 aliquots. We hence have 50 measurements in total (10 for each source and mixtures).
sequence <- Get_sequence(file_path = paste0(data_path, "SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.SEQ"), lab_dose_rate = 0.2)[1:4]
mixed_samples_information <- hash()
# Origin of the sources
mixed_samples_information[["Sources"]][["Source_1"]] <- "FBQ"
mixed_samples_information[["Sources"]][["Source_2"]] <- "Morat"
# Definition of our samples (mixtures): position on carousel + expected proportions
mixed_samples_information[["Sample_1"]][["Positions"]] <- seq(1, 10)
mixed_samples_information[["Sample_1"]][["Proportions"]] <- c(0.5, 0.5)
mixed_samples_information[["Sample_2"]][["Positions"]] <- seq(11, 20)
mixed_samples_information[["Sample_2"]][["Proportions"]] <- c(0.75, 0.25)
mixed_samples_information[["Sample_3"]][["Positions"]] <- seq(21, 30)
mixed_samples_information[["Sample_3"]][["Proportions"]] <- c(0.34, 0.66)
# ---------- Preparation of source data ----------
# Read binx file of the sources
bin_file_SOURCE <- read_BIN2R(file = paste0(data_path,
"SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.binx"),
fastForward = FALSE)
print(bin_file_SOURCE)
# ---------- Extract metrics of the source data ----------
OSL_metrics <- hash()
TL_metrics <- hash()
metrics <- data.frame(matrix(NA, nrow = 0, ncol = 0))
for (sample_position_on_disk in unique(as.data.frame(bin_file_SOURCE)$POSITION)) {
if ((sample_position_on_disk >= 1)&(sample_position_on_disk <= 10)) {
source_num <- "S1"
} else if ((sample_position_on_disk >= 11)&(sample_position_on_disk <= 20)) {
source_num <- "S2"
} else {
print("ERROR - sample_position_on_disk is out of range")
}
if ('OSL' %in% unique(data.frame(bin_file_SOURCE)[, "LTYPE"])) {
OSL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_SOURCE, pos = sample_position_on_disk, ltype = "OSL")
OSL_metrics[sample_position_on_disk] <- Compute_OSL_metrics(sequence = sequence, data = OSL_data)
}
if ('TL' %in% unique(data.frame(bin_file_SOURCE)[, "LTYPE"])) {
TL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_SOURCE, pos = sample_position_on_disk, ltype = "TL")
TL_metrics[sample_position_on_disk] <- Compute_TL_metrics(sequence = sequence,
data = TL_data,
automatic_peak_finder = TRUE)
}
metrics_temp <- Extract_OSL_TL_metrics_for_all_records(TL_hash = TL_metrics[[as.character(sample_position_on_disk)]],
OSL_hash = OSL_metrics[[as.character(sample_position_on_disk)]],
source_number = source_num,
records_to_extract_data_from = NULL)
metrics_temp <- cbind(disk_position = sample_position_on_disk, metrics_temp)
metrics <- bind_rows(metrics, metrics_temp)
}
library(RLumUnmix)
library(hash)
library(Luminescence)
library(robustbase)
library(dplyr)
data_path <- paste0(system.file("extdata", package="RLumUnmix"),"/unmixing_samples/")
# ---------- Get sequence + samples information ----------
#We use two very different pure samples, Fontainebleau (source 1) and Morat (source 2). We then created three mixed samples of different proportions (based on weight), that were each divided into 10 aliquots. We hence have 50 measurements in total (10 for each source and mixtures).
sequence <- Get_sequence(file_path = paste0(data_path, "SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.SEQ"), lab_dose_rate = 0.2)[1:4]
mixed_samples_information <- hash()
# Origin of the sources
mixed_samples_information[["Sources"]][["Source_1"]] <- "FBQ"
mixed_samples_information[["Sources"]][["Source_2"]] <- "Morat"
# Definition of our samples (mixtures): position on carousel + expected proportions
mixed_samples_information[["Sample_1"]][["Positions"]] <- seq(1, 10)
mixed_samples_information[["Sample_1"]][["Proportions"]] <- c(0.5, 0.5)
mixed_samples_information[["Sample_2"]][["Positions"]] <- seq(11, 20)
mixed_samples_information[["Sample_2"]][["Proportions"]] <- c(0.75, 0.25)
mixed_samples_information[["Sample_3"]][["Positions"]] <- seq(21, 30)
mixed_samples_information[["Sample_3"]][["Proportions"]] <- c(0.34, 0.66)
# ---------- Preparation of source data ----------
# Read binx file of the sources
bin_file_SOURCE <- read_BIN2R(file = paste0(data_path,
"SOURCE/R3_unmixing_2_sources_21.11.24_SOURCE.binx"),
fastForward = FALSE)
print(bin_file_SOURCE)
# ---------- Extract metrics of the source data ----------
OSL_metrics <- hash()
TL_metrics <- hash()
metrics <- data.frame(matrix(NA, nrow = 0, ncol = 0))
for (sample_position_on_disk in unique(as.data.frame(bin_file_SOURCE)$POSITION)) {
if ((sample_position_on_disk >= 1)&(sample_position_on_disk <= 10)) {
source_num <- "S1"
} else if ((sample_position_on_disk >= 11)&(sample_position_on_disk <= 20)) {
source_num <- "S2"
} else {
print("ERROR - sample_position_on_disk is out of range")
}
if ('OSL' %in% unique(data.frame(bin_file_SOURCE)[, "LTYPE"])) {
OSL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_SOURCE, pos = sample_position_on_disk, ltype = "OSL")
OSL_metrics[sample_position_on_disk] <- Compute_OSL_metrics(sequence = sequence, data = OSL_data)
}
if ('TL' %in% unique(data.frame(bin_file_SOURCE)[, "LTYPE"])) {
TL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_SOURCE, pos = sample_position_on_disk, ltype = "TL")
TL_metrics[sample_position_on_disk] <- Compute_TL_metrics(sequence = sequence,
data = TL_data,
automatic_peak_finder = TRUE)
}
metrics_temp <- Extract_OSL_TL_metrics_for_all_records(TL_hash = TL_metrics[[as.character(sample_position_on_disk)]],
OSL_hash = OSL_metrics[[as.character(sample_position_on_disk)]],
source_number = source_num,
records_to_extract_data_from = NULL)
metrics_temp <- cbind(disk_position = sample_position_on_disk, metrics_temp)
metrics <- bind_rows(metrics, metrics_temp)
}
# Replace NA values with 0
metrics[is.na(metrics)] <- 0
print(metrics)
# Save the Data Frame
#write.csv(metrics, file = paste0(data_folder, "SOURCE/metrics_SOURCE.csv"))
# ---------- Preparation of mix data ----------
# Read binx file of the mixes
bin_file_MIX <- read_BIN2R(file = paste0(data_path,
"MIX/R3_unmixing_2_sources_22.11.24_MIX.binx"),
fastForward = FALSE)
print(bin_file_MIX)
# ---------- Extract metrics of mixed data ----------
OSL_metrics <- hash()
TL_metrics <- hash()
metrics <- data.frame(matrix(NA, nrow = 0, ncol = 0))
for (sample_position_on_disk in unique(as.data.frame(bin_file_MIX)$POSITION)) {
if ('OSL' %in% unique(data.frame(bin_file_MIX)[, "LTYPE"])) {
OSL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_MIX, pos = sample_position_on_disk, ltype = "OSL")
OSL_metrics[sample_position_on_disk] <- Compute_OSL_metrics(sequence = sequence, data = OSL_data)
}
if ('TL' %in% unique(data.frame(bin_file_MIX)[, "LTYPE"])) {
TL_data <- Risoe.BINfileData2RLum.Analysis(bin_file_MIX, pos = sample_position_on_disk, ltype = "TL")
TL_metrics[sample_position_on_disk] <- Compute_TL_metrics(sequence = sequence,
data = TL_data,
automatic_peak_finder = TRUE)
}
metrics_temp <- Extract_OSL_TL_metrics_for_all_records(TL_hash = TL_metrics[[as.character(sample_position_on_disk)]],
OSL_hash = OSL_metrics[[as.character(sample_position_on_disk)]],
source_number = "Mixed",
records_to_extract_data_from = NULL)
metrics_temp <- cbind(disk_position = sample_position_on_disk, metrics_temp)
metrics <- bind_rows(metrics, metrics_temp)
}
# Replace NA values with 0
metrics[is.na(metrics)] <- 0
# Save Data Frame
#write.csv(metrics, file = paste0(data_folder, "MIX/metrics_MIX.csv"))
# ---------- Prepare Data Frame that will be used for the unmixing ----------
# From previous steps:
metrics_SOURCE <- read.csv(file = paste0(data_path, "SOURCE/metrics_SOURCE.csv"))
metrics_MIX <- read.csv(file = paste0(data_path, "MIX/metrics_MIX.csv"))
## Sample 1
sample <- "Sample_1"
stacked_sample_1 <- bind_rows(metrics_SOURCE, metrics_MIX[metrics_MIX$disk_position %in% mixed_samples_information[[sample]][["Positions"]], ])
# Replace NA values with 0
stacked_sample_1[is.na(stacked_sample_1)] <- 0
# Add expected proportions - not mandatory
stacked_sample_1$Pop_fraction <- c(rep(mixed_samples_information[[sample]][["Proportions"]][1],
nrow(stacked_sample_1[stacked_sample_1$sources == "S1", ])),
rep(mixed_samples_information[[sample]][["Proportions"]][2],
nrow(stacked_sample_1[stacked_sample_1$sources == "S2", ])),
rep(NaN, nrow(stacked_sample_1[stacked_sample_1$sources == "Mixed", ])))
# Remove column named 'disk_position'
stacked_sample_1 <- stacked_sample_1 %>% select(-disk_position)
# Save
#filename <- paste0(as.character(mixed_samples_information[[sample]][["Proportions"]][1]),"-", as.character(mixed_samples_information[[sample]][["Proportions"]][2]))
#write.csv(stacked_sample_1, file = paste0(data_path, sample, "/", filename, ".csv"))
########################################################################################
## Sample 2
sample <- "Sample_2"
stacked_sample_2 <- bind_rows(metrics_SOURCE, metrics_MIX[metrics_MIX$disk_position %in% mixed_samples_information[[sample]][["Positions"]], ])
# Replace NA values with 0
stacked_sample_2[is.na(stacked_sample_2)] <- 0
# Add expected proportions
stacked_sample_2$Pop_fraction <- c(rep(mixed_samples_information[[sample]][["Proportions"]][1],
nrow(stacked_sample_2[stacked_sample_2$sources == "S1", ])),
rep(mixed_samples_information[[sample]][["Proportions"]][2],
nrow(stacked_sample_2[stacked_sample_2$sources == "S2", ])),
rep(NaN, nrow(stacked_sample_2[stacked_sample_2$sources == "Mixed", ])))
# Remove column named 'disk_position'
stacked_sample_2 <- stacked_sample_2 %>% select(-disk_position)
# Save
#filename <- paste0(as.character(mixed_samples_information[[sample]][["Proportions"]][1]),"-", as.character(mixed_samples_information[[sample]][["Proportions"]][2]))
#write.csv(stacked_sample_2, file = paste0(data_path, sample, "/", filename, ".csv"))
########################################################################################
## Sample 3
sample <- "Sample_3"
stacked_sample_3 <- bind_rows(metrics_SOURCE, metrics_MIX[metrics_MIX$disk_position %in% mixed_samples_information[[sample]][["Positions"]], ])
# Replace NA values with 0
stacked_sample_3[is.na(stacked_sample_3)] <- 0
# Add expected proportions
stacked_sample_3$Pop_fraction <- c(rep(mixed_samples_information[[sample]][["Proportions"]][1],
nrow(stacked_sample_3[stacked_sample_3$sources == "S1", ])),
rep(mixed_samples_information[[sample]][["Proportions"]][2],
nrow(stacked_sample_3[stacked_sample_3$sources == "S2", ])),
rep(NaN, nrow(stacked_sample_3[stacked_sample_3$sources == "Mixed", ])))
# Remove column named 'disk_position'
stacked_sample_3 <- stacked_sample_3 %>% select(-disk_position)
# Save
#filename <- paste0(as.character(mixed_samples_information[[sample]][["Proportions"]][1]),"-", as.character(mixed_samples_information[[sample]][["Proportions"]][2]))
#write.csv(stacked_sample_3, file = paste0(data_path, sample, "/", filename, ".csv"))
# ---------- Perform unmixing ----------
# Choose sample to work with
sample <- "Sample_2"
pop_ratios_iteration <- mixed_samples_information[[sample]][["Proportions"]]
filename <- paste0(as.character(mixed_samples_information[[sample]][["Proportions"]][1]), "-", as.character(mixed_samples_information[[sample]][["Proportions"]][2]))
data_path <- paste0(data_path, sample, "/", filename, ".csv")
# Same procedure as for synthetic signals (vignette "Unmixing of 2 synthetic sources")
output <- Prepare_data_for_tracer_selection(data_path)
sgeo <- output$sgeo
mgeo <- output$mgeo
Population_fraction <- output$Population_fraction
source_list <- output$source_list
stacked_df_OG <- output$stacked_df_OG
#It seemed that the unmixing performed better when using a single mix sample. We hence use the median value of all mix samples for every tracer:
mgeo <- as.data.frame(cbind(id = "M1", t(robustbase::colMedians(as.matrix(mgeo[, -1]), na.rm = TRUE))))
mgeo <- type.convert(mgeo, as.is = TRUE)# Modify column classes
# Some values where 0, fingerPro does not like that...
sgeo[sgeo == 0] <- 1e-15
mgeo[mgeo == 0] <- 1e-15
## Consensus ranking
crgeo <- cr_ns(source=sgeo, mixture = mgeo, maxiter = 1000, seed = 1234567)
library(RLumUnmix)
library(hash)
library(Luminescence)
library(robustbase)
library(dplyr)
library(fingerPro)
## Consensus ranking
crgeo <- cr_ns(source=sgeo, mixture = mgeo, maxiter = 1000, seed = 1234567)
head(crgeo)
# ---------- Complete unmixing routine ----------
proportions_all <- data.frame()
ratio_differences_all <- data.frame()
metrics_all <- data.frame()
for (nb_tracer in 2:15) {
output <- Complete_unmixing_routine(mgeo,
sgeo,
crgeo,
source_list,
Population_fraction,
filename,
score_threshold = 80,
nb_tracer = nb_tracer)
proportions_all <- rbind(proportions_all, output$proportions)
metrics_all <- rbind(metrics_all, output$metrics)
ratio_differences_all <- rbind(ratio_differences_all, output$ratio_differences)
}
## Look at results
head(proportions_all)
head(ratio_differences_all)
head(metrics_all)
# ---------- Look at best result ----------
best_results <- head(metrics_all[order(metrics_all$Manhattan_dist_median),],1)
output <- Complete_unmixing_routine(mgeo,
sgeo,
crgeo,
source_list,
Population_fraction,
filename,
score_threshold = 80,
nb_tracer = best_results$nb_tracer)
head(best_results)
library(devtools)
use_vignette("test")
devtools::load_all(".")
devtools::load_all(".")
use_gpl3_license()
use_gpl3_license
use_gpl_license(version = 3, include_future = TRUE)
library(usethis)
use_gpl3_license()
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
devtools::load_all(".")
system.file("extdata", package="RLumUnmix")
devtools::load_all(".")
devtools::load_all(".")