-
Notifications
You must be signed in to change notification settings - Fork 2
/
.Rhistory
512 lines (512 loc) · 17.2 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
tr_roi@ymin <- tr_roi@ymax - 1200
tr_roi@ymax <- tr_roi@ymax + 1200
tr_roi@ymin <- max(c(tr_roi@ymin,orig.ext@ymin))
tr_roi@ymax <- min(c(tr_roi@ymax,orig.ext@ymax))
tr.image <- raster::crop(overview,tr_roi)
br_roi <- tr_roi
br_roi@ymin <- roi@ymin - 1200
br_roi@ymax <- roi@ymin + 1200
br_roi@ymin <- max(c(br_roi@ymin,orig.ext@ymin))
br_roi@ymax <- min(c(br_roi@ymax,orig.ext@ymax))
br.image <- raster::crop(overview,br_roi)
cmPerPixel <- pick_length_shiny(tr.image,br.image,roi)
}
cmPerPixelString <- glue::glue("cmPerPixel = {cmPerPixel}")
#export parameters for rerunning
# no spectra string right now, fix later
#normParams <- glue::glue("{dirString},\n{cmPerPixelString},\n{spectraString},\n{roiString},\n{outputdirString},\n{corenameString}")
#assign to global just incase it fails
#also fails
# assign("normParams",normParams,envir = .GlobalEnv)
#crop the image
if(is.finite(cmPerPixel) & cmPerPixel > 0){
scaleY <- seq(from = cmPerPixel/2,to = (dim(filen)[1]*cmPerPixel)-cmPerPixel/2,by = cmPerPixel)
}else{
#calculate length interval of each pixel (necessary for indices calculations)
scaleY <- coreLength(stripe = roi, length = length)
}
#crop_1 <- raster::crop
#test how slow the crop function is for .5 cm interval (6 times) or jst 3 cm interval
#chunk into .5 cm bits, normalizate against the white dark lines, average into a single spectra.
length.out <- 1
chunk <- 0.5
sub <- raster::extent(filen@extent@xmin,filen@extent@xmax,filen@extent@ymin,length(which(scaleY <= length.out)))
full_spectra <- raster::crop(filen,sub)
new_vals <- raster::aggregate(full_spectra,fact=c(1,(((full_spectra@extent@ymax-full_spectra@extent@ymin)/((length.out/chunk)-1)))),FUN=mean)
#load in the white and dark refs
whiteRef <- raster::brick(paths$whiteref)
darkRef <- raster::brick(paths$darkref)
white.ref <- processReference(whiteRef,stripe = full_spectra,spectra = names(whiteRef))
dark.ref <- processReference(darkRef,stripe = full_spectra,spectra = names(whiteRef))
#now normalize
normalized <- whiteDarkNormalize(stripe = full_spectra, white.ref = white.ref, dark.ref = dark.ref)
if(!dir.exists(file.path(output.dir))){
dir.create(file.path(output.dir))
}
if(!dir.exists(file.path(output.dir,corename))){
dir.create(file.path(output.dir,corename))
}
#save normalized core image
normalizedImage <- normalizeCoreImage(paths$overview)
imager::save.image(normalizedImage,file = file.path(output.dir,"normalizedCoreImage.png"))
raster::writeRaster(normalized,file.path(output.dir,"normalized.tif"),overwrite = TRUE)
#save normalized data for future reference
save(normalized,file = file.path(output.dir,"normalized.RData"))
#save paths for images too?
return(list(allbands = allbands,
#spectra = spectra,
wavelengths = wavelengthsOut,
normalized = normalized,
scaleY = scaleY,
# stripe = stripe,
cmPerPixel = cmPerPixel,
roi = roi,
corename = corename,
pngPath = paths$overview,
# normParams = normParams,
outputDir = output.dir))
}
dat <- full_spectra()
#folder name
# corename <- NA
if(is.na(corename)){
corename <- basename(dirname(paths$overview))
}
corenameString <- glue::glue("corename = '{corename}'")
#load overview
overview <- raster::brick(paths$overview)
#ADDED
# roi <- NA
#choose the ROI
if(!class(roi)=="Extent"){
roi <- pick_roi_shiny(overview)
}
roiString <- glue::glue("roi = raster::extent(matrix(c({roi@xmin},{roi@xmax},{roi@ymin},{roi@ymax}),nrow = 2,byrow = T))")
roi
roiString <- glue::glue("roi = raster::extent(matrix(c({roi@xmin},{roi@xmax},{roi@ymin},{roi@ymax}),nrow = 2,byrow = T))")
#record roi string
roi <- roi[[1]]
roiString <- glue::glue("roi = raster::extent(matrix(c({roi@xmin},{roi@xmax},{roi@ymin},{roi@ymax}),nrow = 2,byrow = T))")
source('~/Documents/GitHub/specimR/R/full_spectra.R')
dat <- full_spectra()
dat
source('~/Documents/GitHub/specimR/R/full_spectra.R')
new_vals
plot(new_vals)
dim(new_vals)
dim(full_spectra())
dim(full_spectra
#load in the white and dark refs
whiteRef <- raster::brick(paths$whiteref)
darkRef <- raster::brick(paths$darkref)
white.ref <- processReference(whiteRef,stripe = full_spectra,spectra = names(whiteRef))
dark.ref <- processReference(darkRef,stripe = full_spectra,spectra = names(whiteRef))
#now normalize
normalized <- whiteDarkNormalize(stripe = full_spectra, white.ref = white.ref, dark.ref = dark.ref)
if(!dir.exists(file.path(output.dir))){
dir.create(file.path(output.dir))
}
if(!dir.exists(file.path(output.dir,corename))){
dir.create(file.path(output.dir,corename))
}
#save normalized core image
normalizedImage <- normalizeCoreImage(paths$overview)
imager::save.image(normalizedImage,file = file.path(output.dir,"normalizedCoreImage.png"))
raster::writeRaster(normalized,file.path(output.dir,"normalized.tif"),overwrite = TRUE)
dim(full_spectra)
full_spectra <- raster::crop(filen,sub)
chunk <- 0.25
dim(full_spectra)
new_vals <- raster::aggregate(full_spectra,fact=c(1,(((full_spectra@extent@ymax-full_spectra@extent@ymin)/((length.out/chunk)-1)))),FUN=mean)
new_vals
plot(new_vals[[1]])
plot(new_vals[1])
plot(new_vals[1])
plot(new_vals[1,])
plot(new_vals[,])
source('~/Documents/GitHub/specimR/R/full_spectra.R')
darkRef
dim(new_vals)
dim(whiteRef)
devtools::document()
install.packages(c('raster','tcltk','rgdal','smoother','spatialEco','imager','magick','egg'))
install.packages("sf")
devtools::document()
install.packages(c('tcltk','imager'))
devtools::document()
install.packages(c('tcltk','imager'))
roxygen2::roxygenize()
install.packages("imager")
install.packages("tcltk")
library(tcltk)
library(specimR)
library(specimR)
library(specimR)
f <- fullSpectra(chunk.bot = NA, chunk.step = 1)
source("~/GitHub/specimR/fullSpectraWorkflow.R")
f
#get a list of all the prod.dirs
startDir <- "/Volumes/data/Lakes380/SurfaceSedimentScans/"
allRuns <- list.files(startDir)
indices <- c("RABD615", "RABD640655", "RABD660670", "RABD845", "R570R630", "R590R690","R950R970")
allRuns
gtr <- c()
for(r in allRuns){
tr <- list.files(file.path(startDir,r,"products"),full.names = TRUE)
wpds <- which(purrr::map_lgl(tr,dir.exists) & basename(tr) != "photos")
gtr <- c(gtr,tr[wpds])
}
gtr
startDir
source("~/Downloads/surfaceSedResults (1).R")
getNormWavelengthData()
normData
source("~/Downloads/surfaceSedResults.R")
length(gtr)
#process and export big.data
ggplot(big.data)+geom_point(aes(x = meanIndices,y = ,color = index))
View(big.data)
summary <- big.data %>%
group_by(index) %>%
summarize(across(everything(),mean))
cores <- big.data %>%
group_by(index) %>%
summarize(corr = cor(mean95,meanIndices))
#prep to add to table
forTable <- big.data %>%
select(-c(2,3,5,6)) %>%
rename(median = `50%`) %>%
pivot_wider(names_from = index,values_from = -last_col())
readr::write_csv(big.data,"C:/Users/hyprspec/Desktop/SurfaceSedOutputMaster.csv")
readr::write_csv(big.data,"~/Downloads/SurfaceSedOutputMaster.csv")
readr::write_csv(forTable,"~/Downloads/SurfaceSedOutputBySample.csv")
library(googlesheets4)
newdat <- read_sheets("1VskYZ8rCEMzoKNgfnvnp9psoLhN-RaP6DKlYVG3AukM")
newdat <- read_sheet("1VskYZ8rCEMzoKNgfnvnp9psoLhN-RaP6DKlYVG3AukM")
library(ggplot2)
names(newdat)
library(geoChronR)
ggplot(newdata) + geom_point(aes(x = gaussianize(mean95_RABD845), y= gaussianize(ConcB)))
ggplot(newdat) + geom_point(aes(x = gaussianize(mean95_RABD845), y= gaussianize(ConcB)))
ggplot(newdat) + geom_point(aes(x = mean95_RABD845, y= gaussianize(ConcB)))
newdat845 <- filter(newdat, mean95_RABD845 >= 1)
ggplot(newdatHi845) + geom_point(aes(x = mean95_RABD845, y= gaussianize(ConcB)))
newdatHi845 <- filter(newdat, mean95_RABD845 >= 1)
ggplot(newdatHi845) + geom_point(aes(x = mean95_RABD845, y= gaussianize(ConcB)))
ggplot(newdatHi845) + geom_point(aes(x = gaussianize(mean95_RABD845), y= gaussianize(ConcB)))
ggplot(newdat) + geom_point(aes(x = gaussianize(mean95_RABD660670), y= gaussianize(CaSpec)))
startDir <- "/Volumes/data/Lakes380/SurfaceSedimentScans/"
newIndices <- 'specimR:::fullSpectraWholeROI('
outDirSearch <- "/Volumes/data/Lakes380/SurfaceSedimentScans/Reprocess/"
outDirBase <- "/Volumes/data/Lakes380/SurfaceSedimentScans/FullSpectraSurface/"
torem <- c("clickDepths =",
"pixel =",
"cm = c",
"imageRoi =",
"overall.page.width =",
"individual.page.width =",
"plot.width =",
"page.width.multiplier =",
"page.length.multiplier =",
"core.width = ",
"wavelengths =",
"cmPerPixel")
newFileName <- "/Volumes/data/Lakes380/SurfaceSedimentScans/FullSpectraSurface/createFullSpectra.R"
start <- "#THIS FILE WAS CREATED PROGRAMMATICALLY BY createFullSpectraSurface.R\n\n" %>%
str_c("library(specimR)\n\n") %>%
str_c("library(tidyverse)\n\n") %>%
str_c("library(shiny)")
write(start,
file = newFileName,
append = FALSE)
write(start,
file = newFileName,
append = FALSE)
allRuns <- list.files(startDir)
allRuns
for(r in allRuns){
tr <- list.files(file.path(startDir,r,"products"),full.names = TRUE)
if(any(grepl(tr,pattern = "roi"))){
reproc <- readLines(file.path(startDir,r,"products","reprocess.R"))
ii <- which(grepl(reproc,pattern = "indices ="))
reproc[ii] <- newIndices
#output dir.
odi <- which(grepl(reproc,pattern = 'output.dir'))
odl <- reproc[odi]
odlNew <- str_replace(odl,outDirSearch,outDirBase)
newDir <- str_split(odlNew," = ")[[1]][2] %>% str_remove("',") %>% str_remove("'")
reproc[odi] <- odlNew
dir.create(dirname(newDir))
dir.create(newDir)
for(tri in torem){
wtri <- which(grepl(pattern = tri,reproc))
reproc <- reproc[-wtri]
}
#add new line
lastComma <- max(str_locate_all(reproc[length(reproc)],pattern = ",")[[1]])
substr(reproc[length(reproc)],lastComma,lastComma) <- ")"
write(reproc,
file = newFileName,
append = TRUE)
write("\n\n",
file = newFileName,
append = TRUE)
}
}
source("~/Downloads/createFullSpectraSurface.R")
R.version
sessionInfo()
library(tidyverse)
ff <- list.files("~/Download/drive-download-20210721T160335Z-001/",recursive = TRUE)
ff
ff <- list.files("~/Download/drive-download-20210721T160335Z-001/",recursive = TRUE,pattern = "depthTable.csv")
ff
path <- ff[22]
dat <- readr::read_csv(path)
ff <- list.files("~/Download/drive-download-20210721T160335Z-001/",
recursive = TRUE,
full.names = TRUE,
pattern = "depthTable.csv")
path <- ff[22]
dat <- readr::read_csv(path)
dat
dat <- readr::read_csv(path) %>%
dplyr::select(-pixel)
dat
dat <- readr::read_csv(path) %>%
dplyr::select(-pixel) %>%
tidyr::pivot_wider(names_from = position, values_from = cm)
dat
path
ff <- list.files("~/Download/DUNCA1 R Script HS Data/",
recursive = TRUE,
full.names = TRUE,
pattern = "depthTable.csv")
ff
path <- ff[5]
dat <- readr::read_csv(path) %>%
dplyr::select(-pixel) %>%
tidyr::pivot_wider(names_from = position, values_from = cm)
dat
#parse path to get core name
str_extract(path,"^/(?:\\.|[^/\\])*/")
#parse path to get core name
str_extract(path,"[^/(?:\\.|[^/\\])*/]")
#parse path to get core name
str_extract(path,"[^/(?:\\.|[^/\\])*/]")
#parse path to get core name
str_extract(path,"[^/(?:.|[^/])*/]")
path
#parse path to get core name
str_extract(path,"[^//(?:.|[^/])*//]")
#parse path to get core name
str_extract(path,"[^//*//]")
#parse path to get core name
str_extract(path,"[^//?*//]")
#parse path to get core name
str_extract(path,"[^//*?//]")
#parse path to get core name
str_extract(path,"[^////]")
#parse path to get core name
str_extract(path,"[^//]")
#parse path to get core name
str_extract(path,"[//]")
#parse path to get core name
str_extract(path,"[/*/]")
#parse path to get core name
str_extract(path,"^/")
#parse path to get core name
str_extract(path,"*^/")
#parse path to get core name
str_extract(path,"[*^/]")
#parse path to get core name
str_extract(path,"[^/*]")
#parse path to get core name
str_extract(path,"^[^\/]*\/[^\/]*")
#parse path to get core name
str_extract(path,"[^[^\/]*\/[^\/]*]")
#parse path to get core name
str_extract(path,"[^[^/]*\/[^/]*]")
#parse path to get core name
str_extract(path,"[^[^/]*/[^/]*]")
#parse path to get core name
str_extract(path,"[^[^/]*[^/]*]")
#parse path to get core name
str_extract(path,"^[^/]*[^/]*")
#parse path to get core name
str_extract(path,"^[^/]*[^/]*")
#parse path to get core name
str_extract(path,"[A-Z][A-Z}*")
#parse path to get core name
str_extract(path,"[A-Z][A-Z]*")
path
#parse path to get core name
str_extract(path,"^.*\/(.*)\/.*\/$")
#parse path to get core name
str_extract(path,"^.*\\/(.*)\\/.*\\/$")
#parse path to get core name
str_extract(path,"^.*/(.*)/.*/$")
#parse path to get core name
str_extract(path,"^.*/(.*)/.*/")
#parse path to get core name
str_extract(path,"^.*/(.*)/.*/$")
#parse path to get core name
str_extract(path,".*/(.*)/.*/$")
#parse path to get core name
str_extract(path,"*/(.*)/.*/$")
#parse path to get core name
str_extract(path,".*/.*/.*/$")
#parse path to get core name
str_extract(path,".*/.*/.*/")
#parse path to get core name
str_extract(path,"$.*/.*/.*/")
#parse path to get core name
str_extract(path,"^.+/\K[^/]+(?=/[^/]+/)")
#parse path to get core name
str_extract(path,"^.+/[^/]+(?=/[^/]+/)")
path <- "/Users/nicholas/Download/DUNCA1 R Script HS Data//DUNCA1_LC4U_1A/products/"
path
#parse path to get core name
str_extract(path,"^.+/[^/]+(?=/[^/]+/)")
path <- "/Users/nicholas/Download/DUNCA1 R Script HS Data/"
#parse path to get core name
str_extract(path,"^.+/[^/]+(?=/[^/]+/)")
#parse path to get core name
str_extract(path,"^.+/[^/]+(?=/[^/]+/)") %>%
dirname()
#parse path to get core name
str_extract(path,"^.+/[^/]+(?=/[^/]+/)") %>%
basename()
path <- ff[5]
path %>%
basename()
path %>%
dirname()
path %>%
dirname() %>%
dirname()
path %>%
dirname() %>%
dirname() %>%
basename()
dat <- readr::read_csv(path) %>%
dplyr::select(-pixel) %>%
tidyr::pivot_wider(names_from = position, values_from = cm)
#parse path to get core name
dat$corename <- path %>%
dirname() %>%
dirname() %>%
basename()
dat <- dplyr::select(corename,everything())
pullDepthData <- function(path){
dat <- readr::read_csv(path) %>%
dplyr::select(-pixel) %>%
tidyr::pivot_wider(names_from = position, values_from = cm)
#parse path to get core name
dat$corename <- path %>%
dirname() %>%
dirname() %>%
basename()
dat <- dplyr::select(corename,everything())
}
dat
dat <- dplyr::select(corename,everything())
#parse path to get core name
dat$corename <- path %>%
dirname() %>%
dirname() %>%
basename()
dat
dat <- dplyr::select(corename,everything())
dat <- dplyr::select(dat,corename,everything())
dat
source("~/.active-rstudio-document", echo=TRUE)
test
ff
ff <- str_detect(ff,"photos")
ff <- list.files("~/Download/DUNCA1 R Script HS Data/",
recursive = TRUE,
full.names = TRUE,
pattern = "depthTable.csv")
hp <- str_detect(ff,"photos")
hp
source("~/.active-rstudio-document", echo=TRUE)
test
#Analysis of temp data for NE N America during Early Holocene
library(lipdR) #to read and interact with LiPD data
library(geoChronR) #for plotting mostly
library(magrittr) #we'll be using the magrittr pipe ( %>% ) for simplicity
library(dplyr) #and dplyr for data.frame manipulation
library(ggplot2) #for plotting
library(compositeR) #remotes::install_github("nickmckay/compositeR")
library(foreach) #for parallel processing
library(doParallel)#for parallel processing
#First make a stack plot
D <- readLipd("https://lipdverse.org/Temp12k/1_0_2/Temp12k1_0_2.zip")
TS <- extractTs(D)
TS <- extractTs(D) %>% #extract to lipd-ts
as.lipdTsTibble()
View(TS)
TS <- extractTs(D) %>% #extract to lipd-ts
as.lipdTsTibble() %>% # and the then to lipd-ts-tibble for filtering
filter(between(geo_longitude,-90,-50)) %>% #only NE NA longitudes
filter(between(geo_latitude,35,55)) %>% #between 35 and 55 N
filter(interpretation1_variable == "T") %>% #only variables sensitive temperature
filter(paleoData_medianRes12k < 200) %>% #only time series at highres
filter(interpretation1_seasonalityGeneral == "summer+") %>% #only summer proxies
as.lipdTs() #back to TS for compositeR
L <- D$`117_723A.Godad.2011`
L$paleoData[[1]]$measurementTable[[1]]$age$values
ts <- extractTs(L) %>% ts2tibble()
View(ts)
library(magrittr)
library(ggplot2)
library(actR)
library(neotoma2)
library(tidyverse)
library(lipdR)
library(geoChronR)
remotes::install_github("neotomaDB/neotoma2")
library(magrittr)
library(ggplot2)
library(actR)
library(neotoma2)
library(tidyverse)
library(lipdR)
library(geoChronR)
L <- neotoma2::get_datasets(53327) %>% #Find the site of interest
neotoma2::get_downloads() #download the data for this site
L_lip=lipdR::neotoma2lipd(L) #convert the site object into a LiPD object
mapLipd(L_lip,map.type = "stamen",extend.range = 5)#visualize the location
mapLipd(L_lip,map.type = "stamen",extend.range = 5)#visualize the location
my_counts<- neotoma2::samples(L)
counts=pivot_wider(my_counts, names_from = variablename, values_from = value, values_fill = NA, id_cols=age)
lat=L_lip[["geo"]][["latitude"]]
L_lip$chronData[[1]]$measurementTable[[1]]$age14C
L_lip$chronData[[1]]$measurementTable[[1]]$age
L_lip <- geoChronR::runBacon(L_lip,
lab.id.var = 'neotomaChronConrolId',
age.14c.var = 'age14C',#for rest: 'age14C'
age.14c.uncertainty.var = 'age14CUnc',#for rest: 'age14CUnc'
age.var = 'age', #for bambili: 'age'
age.uncertainty.var = 'ageUnc',#for bambili 'ageUnc'
depth.var = 'depth',
cc=3,
reservoir.age.14c.var = NULL,
reservoir.age.14c.uncertainty.var = NULL,
rejected.ages.var = NULL,
plot.pdf = FALSE,
accept.suggestions = TRUE)
L_lip <- geoChronR::runBacon(L_lip,
lab.id.var = 'neotomaChronConrolId',
age.14c.var = 'age14C',#for rest: 'age14C'
age.14c.uncertainty.var = 'age14CUnc',#for rest: 'age14CUnc'
age.var = NULL, #for bambili: 'age'
age.uncertainty.var = NULL,#for bambili 'ageUnc'
depth.var = 'depth',
cc=3,
reservoir.age.14c.var = NULL,
reservoir.age.14c.uncertainty.var = NULL,
rejected.ages.var = NULL,
plot.pdf = FALSE,
accept.suggestions = TRUE)