Skip to content

Dar roberd10, Update to v1 #179

Merged
merged 5 commits into from Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
224 changes: 118 additions & 106 deletions StudentData/comprehensive_LIBS.Rmd → AppRelated/comprehensive_LIBS.Rmd
Expand Up @@ -28,10 +28,7 @@ if (!require("rmarkdown")) {
install.packages("rmarkdown")
library(rmarkdown)
}
if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}

if (!require("stringr")) {
install.packages("stringr")
library(stringr)
Expand Down Expand Up @@ -96,6 +93,11 @@ if(!require("RColorBrewer")){
install.packages("RColorBrewer")
library(RColorBrewer)
}

if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}
```

# Comprehensive LIBS Report
Expand All @@ -107,8 +109,8 @@ if(!require("RColorBrewer")){

```{r}
# Raw, original LIBS dataset
libs.df <- readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/Data/supercam_libs_moc_loc.Rds")
libs.df$point <- as.numeric(libs.df$point)
libs.df <- readRDS("~/DAR-Mars-F24/StudentData/v1_libs.Rds")
libs.df$Point <- as.numeric(libs.df$Point)

# LIBS dataset w/o stdevs, other extra columns
libs_trim.df <- libs.df %>%
Expand All @@ -122,21 +124,22 @@ libs_earth.df <- readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/Data/LIBS_traini
Also included is PIXL, for some analyses
```{r}
# PIXL data added
pixl.df <- readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/Data/samples_pixl_wide.Rds")
pixl.df[sapply(pixl.df, is.character)] <- lapply(pixl.df[sapply(pixl.df, is.character)],
as.factor)
pixl.df <- readRDS("~/DAR-Mars-F24/StudentData/v1_pixl.Rds")
pixl.df <- pixl.df[2:16,] #Excluding first, atmospheric sample
# Make the matrix of just mineral percentage measurements
pixl.matrix <- pixl.df[,2:14]
pixl.matrix.scaled <- pixl.df[,2:14] %>% scale()
# Sample meta data
meta_full.df <- readRDS("~/DAR-Mars-F24/StudentData/v1_sample_meta.Rds")
meta.df <- meta_full.df[2:16,]
```

# LIBS Samples Plots

## Bray-Curtis Dissimilarity
```{r}
# Matrix contain only the libs measurements for each mineral, Scaled
libs.matrix.scaled <- as.matrix(libs.df[,6:13]) %>% scale(center=FALSE,scale=TRUE)
libs.matrix.scaled <- as.matrix(libs.df[,7:22]) %>% scale(center=FALSE,scale=TRUE)

# K-means clustering data w/o metadata, then adding targets back
km<-kmeans(libs.matrix.scaled,5)
Expand Down Expand Up @@ -191,7 +194,7 @@ ggtern(libs_ternary_clustered, ggtern::aes(x=x,y=y,z=z,cluster=cluster)) +

## LIBS Graphed by Location
```{r}
libs_loc <- libs.df %>% select(c(SiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, lat, lon))
libs_loc <- libs.df %>% select(c(SiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, Lat, Lon))
libs_loc_ternary <- libs_loc %>%
mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>%
select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O)) %>%
Expand All @@ -204,7 +207,7 @@ tern.km <- kmeans(libs_ternary, k)
libs_loc_ternary <- cbind(libs_loc_ternary, cluster=as.factor(tern.km$cluster))
tern_clusters <- libs_ternary_clustered$cluster

ggplot(libs_loc_ternary, aes(x=lon, y=lat, colour=cluster)) +
ggplot(libs_loc_ternary, aes(x=Lon, y=Lat, colour=cluster)) +
geom_point() +
ggtitle("Clustered LIBS Data Graphed by Location")
```
Expand All @@ -213,10 +216,10 @@ ggplot(libs_loc_ternary, aes(x=lon, y=lat, colour=cluster)) +
```{r}
suppressWarnings(
libs.uniquetar <-
aggregate(libs.df, list(Target = libs.df$target), mean))
aggregate(libs.df, list(Target = libs.df$Target), mean))

#Aggregate data
libs.uniquetar <- libs.uniquetar %>% select(!c(target,point))
libs.uniquetar <- libs.uniquetar %>% select(!c(Target,Point))
libs.uniquetar <- libs.uniquetar %>%
mutate(y = (FeOT + MgO) / 100, z = (CaO+Na2O+K2O) / 100, x = (SiO2 + Al2O3) / 100)
libs_ternplot2 <- libs.uniquetar %>% select(c(x,y,z))
Expand Down Expand Up @@ -355,7 +358,7 @@ From the plot of the original data, we see that Cluster 1 (red) mostly falls in

## LIBS Samples Heatmap (clustered by k=5)
```{r}
libs.matrix <- as.matrix(libs_trim.df[,6:13])
libs.matrix <- as.matrix(libs_trim.df[,7:14])
libs.scaled <- libs.matrix %>% scale()
set.seed(500)
k <- 5
Expand All @@ -366,8 +369,8 @@ pheatmap(km$centers,scale="none")

## LIBS PCA Plot
```{r}
lib.matrix.scaled.pca <- prcomp(libs.scaled, scale=FALSE)
ggbiplot::ggbiplot(lib.matrix.scaled.pca,
libs.matrix.scaled.pca <- prcomp(libs.scaled, scale=FALSE)
ggbiplot::ggbiplot(libs.matrix.scaled.pca,
groups = as.factor(km$cluster)) +
xlim(-5,2.5) + ylim(-2.5,7.5) +
theme_bw()
Expand Down Expand Up @@ -399,7 +402,7 @@ ggtern(libs_target_ternary, ggtern::aes(x=x,y=y,z=z)) +

## Calibration Targets Violin Plot
```{r}
libs_earth_trim <- libs.df %>% select(c(target, SiO2, TiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, sol)) %>%
libs_earth_trim <- libs.df %>% select(c(Target, SiO2, TiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, Sol)) %>%
rowwise() %>% mutate(Si= (SiO2-libs_earth.df[3,2])/(libs_earth.df[4,2] - libs_earth.df[2,2]),
Ti= (TiO2-libs_earth.df[3,3])/(libs_earth.df[4,3] - libs_earth.df[2,3]),
Al= (Al2O3-libs_earth.df[3,4])/(libs_earth.df[4,4] - libs_earth.df[2,4]),
Expand All @@ -408,7 +411,7 @@ Mg= (MgO-libs_earth.df[3,6])/(libs_earth.df[4,6] - libs_earth.df[2,6]),
Ca= (CaO-libs_earth.df[3,7])/(libs_earth.df[4,7] - libs_earth.df[2,7]),
Na= (Na2O-libs_earth.df[3,8])/(libs_earth.df[4,8] - libs_earth.df[2,8]),
K= (K2O-libs_earth.df[3,9])/(libs_earth.df[4,9] - libs_earth.df[2,9])) %>%
select(!c(SiO2, TiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, target, sol))
select(!c(SiO2, TiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O, Target, Sol))

ggplot() +
geom_violin(aes(y = libs_earth_trim$Si$SiO2, x = "Si"), fill = "blue") +
Expand Down Expand Up @@ -442,9 +445,10 @@ labs(title="Biplot of PCA on Earth Scaled LIBS")
Clustered LIBS with PIXL samples clustered by campaign.
```{r}
new_pixl_trim <- pixl.df %>%
dplyr::select(c("Na20","Mgo","Al203","Si02", "K20","Cao","FeO-T", campaign, type)) %>%
rename("Na2O"="Na20","MgO"="Mgo","Al2O3"="Al203","SiO2"="Si02","K2O"="K20",
"CaO"="Cao","FeOT"="FeO-T")
dplyr::select(c("Na2O","MgO","Al2O3","SiO2", "K2O","CaO","FeOT"))
Campaign <- meta.df$Campaign
Type <- meta.df$Type
new_pixl_trim <- cbind(new_pixl_trim,Campaign,Type)
pixl_ternary <- new_pixl_trim %>%
mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>%
select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O)) %>%
Expand All @@ -465,11 +469,11 @@ z="Ca+Na2+K2") +
#suppress warnings here because of some warning with aes()
#add PIXL samples - atmospheric onto the ternary plot
suppressWarnings(geom_point(
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=campaign, shape=campaign),
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=Campaign, shape=Campaign),
size = 2)) +
#Add labels to PIXL data corresponding to sample number
suppressWarnings(geom_text(data=pixl_ternary,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=campaign,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=Campaign,
hjust = ifelse(x > 0.43, 1, -0.1), # Horizontal adjust to avoid overlap
vjust = ifelse(x == 0.3668, 1.3,
ifelse(x == 0.375, 1, ifelse(x > 0.43, 1.5, -0.3))),
Expand All @@ -490,11 +494,11 @@ z="Ca+Na2+K2") +
#suppress warnings here because of some warning with aes()
#add PIXL samples - atmospheric onto the ternary plot
suppressWarnings(geom_point(
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=type, shape=type),
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=Type, shape=Type),
size = 2)) +
#Add labels to PIXL data corresponding to sample number
suppressWarnings(geom_text(data=pixl_ternary,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=type,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=Type,
hjust = ifelse(x > 0.43, 1, -0.1), # Horizontal adjust to avoid overlap
vjust = ifelse(x == 0.3668, 1.3,
ifelse(x == 0.375, 1, ifelse(x > 0.43, 1.5, -0.3))),
Expand All @@ -508,10 +512,10 @@ Clustered LIBS, aggregated by target, with PIXL samples clustered by rock type
#but thats is fine as we have the target anyways and point is no longer relevant
suppressWarnings(
libs.uniquetar <-
aggregate(libs.df, list(Target = libs.df$target), mean))
aggregate(libs.df, list(Target = libs.df$Target), mean))

#drop target and point from the data frame
libs.uniquetar <- libs.uniquetar %>% select(!c(target,point))
libs.uniquetar <- libs.uniquetar %>% select(!c(Target,Point))
libs.uniquetar <- libs.uniquetar %>%
mutate(y = (FeOT + MgO) / 100, z = (CaO+Na2O+K2O) / 100, x = (SiO2 + Al2O3) / 100)
libs_ternplot2 <- libs.uniquetar %>% select(c(x,y,z))
Expand All @@ -529,10 +533,10 @@ ggtern(libs_ternplot2, ggtern::aes(x=x, y=y, z=z,cluster=cluster)) +
y="Fe+Mg",
z="Ca+Na2+K2") +
suppressWarnings(geom_point(
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=type, shape=type),
data=pixl_ternary, ggtern::aes(x=x, y=y, z=z, cluster=Type, shape=Type),
size = 2.5)) +
suppressWarnings(geom_text(data=pixl_ternary,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=type,
ggtern::aes(x=x, y=y, z=z, label=Sample_display, cluster=Type,
hjust = ifelse(x > 0.43, 1, -0.1), # Horizontal adjust to avoid overlap
vjust = ifelse(x == 0.3668, 1.3,
ifelse(x == 0.375, 1, ifelse(x > 0.43, 1.5, -0.3))),
Expand All @@ -542,75 +546,80 @@ ggtern(libs_ternplot2, ggtern::aes(x=x, y=y, z=z,cluster=cluster)) +

## LIBS data plotted by Location, w/ PIXL
```{r}
pixllibs <- readRDS("~/DAR-Mars-F24/StudentData/PIXL_LIBS_Combined.Rds")

#make a filtered data frame that picks the max point out of all libs samples at a certain target
# for simplicity
df_filtered <- pixllibs %>%
group_by(lat, lon) %>%
filter(point == max(point)) %>%
ungroup()
df_filtered <- na.omit(df_filtered)
df_filtered$lat <- as.numeric(df_filtered$lat)
df_filtered$lon <- as.numeric(df_filtered$lon)

#fix variable type and N/A issues in data frame
pixllibs$lat <- as.numeric(pixllibs$lat)
pixllibs$lon <- as.numeric(pixllibs$lon)
pixllibs$Long <- as.numeric(pixllibs$Long)
pixllibs_no_na <- na.omit(df_filtered)

#make a data frame with the unique pixl coordinates since they are in pairs of identical lat/lon
unique_pixl <- pixllibs_no_na %>%
select(lat, Long, name) %>% distinct()
unique_pixl$Long <- as.numeric(unique_pixl$Long)

#was using this to work on making a key for the plot, couldn't get it to work correctly.
unique_pixl_with_label <- unique_pixl %>%
mutate(source = "PIXL")
pixl_libs_with_label <- pixllibs %>%
mutate(source = "LIBS")

#plot of libs and pixl data by lat/lon
ggplot(data = pixllibs) +
geom_point(mapping = aes(x = lat, y = lon), colour = 'blue') +
geom_point(mapping = aes(x = lat, y = Long), data = unique_pixl, colour = 'red') +
geom_text_repel(mapping = aes(x = lat, y = Long, label = name), data = unique_pixl,
vjust = 2, colour = 'red') +
labs(title = "Libs and PIXL by Location",
x = "Latitude",
y = "Longitude",
color = "Point Type")
# pixllibs <- readRDS("~/DAR-Mars-F24/StudentData/v1_libs_to_sample.Rds")
#
# ## IMPORTANT: THIS AREA DOESN'T WORK BECAUSE MARGO HAS COMPLETELY SHIFTED WHAT THE FILE LOOKS LIKE SO THAT INSTEAD OF MENTIONING JUST WHICH IS CLOSEST WITHIN A RANGE, IT TELLS YOU WHAT THE DISTANCE BETWEEN EACH LIBS AND ITS CLOSEST PIXL. CHOOSE A THRESHOLD OF <= 7 AND REDO THIS AREA.
#
# #make a filtered data frame that picks the max point out of all libs samples at a certain target
# # for simplicity
# df_filtered <- pixllibs %>%
# group_by(Lat, Lon) %>%
# filter(point == max(Point)) %>%
# ungroup()
# df_filtered <- na.omit(df_filtered)
# df_filtered$Lat <- as.numeric(df_filtered$Lat)
# df_filtered$Lon <- as.numeric(df_filtered$Lon)
#
# #fix variable type and N/A issues in data frame
# pixllibs$lat <- as.numeric(pixllibs$Lat)
# pixllibs$lon <- as.numeric(pixllibs$Lon)
# pixllibs$Long <- as.numeric(pixllibs$Lon)
# pixllibs_no_na <- na.omit(df_filtered)
#
# #make a data frame with the unique pixl coordinates since they are in pairs of identical lat/lon
# unique_pixl <- pixllibs_no_na %>%
# select(lat, Long, name) %>% distinct()
# unique_pixl$Long <- as.numeric(unique_pixl$Long)
#
# #was using this to work on making a key for the plot, couldn't get it to work correctly.
# unique_pixl_with_label <- unique_pixl %>%
# mutate(source = "PIXL")
# pixl_libs_with_label <- pixllibs %>%
# mutate(source = "LIBS")
#
# #plot of libs and pixl data by lat/lon
# ggplot(data = pixllibs) +
# geom_point(mapping = aes(x = lat, y = lon), colour = 'blue') +
# geom_point(mapping = aes(x = lat, y = Long), data = unique_pixl, colour = 'red') +
# geom_text_repel(mapping = aes(x = lat, y = Long, label = name), data = unique_pixl,
# vjust = 2, colour = 'red') +
# labs(title = "Libs and PIXL by Location",
# x = "Latitude",
# y = "Longitude",
# color = "Point Type")
```

## LIBS and PIXL Sample Types Over Time
```{r}
pixl.sol.coordinates<-readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/StudentData/pixl_sol_coordinates.Rds")
pixl.sol<-pixl.sol.coordinates[,c(3,18,22)]
pixl.sol<-meta_full.df[1:16,c(3,2,6)]
pixl.sol<-cbind(pixl.sol,"PIXL")

libs.sol<-libs.df[,c(1,4,5)]
libs.sol<-libs.df[,c(3,1,2)]

libs.sol<- libs.sol %>%
group_by(sol, target) %>%
summarise(across(point,max))
group_by(Sol, Target) %>%
summarise(across(Point,max))

libs.sol<-cbind(libs.sol,"type"="type")

libs.sol<-libs.sol %>%
mutate(type = ifelse(grepl("scct", target),"scct", type)) %>%
mutate(type= ifelse(grepl("scam", target) & type=="type","scam",type)) %>%
mutate(type= ifelse(grepl("aegis", target) & type=="type","aegis",type)) %>%
mutate(type=ifelse(grepl("[0-9]",target) & type=="type","num",type)) %>%
mutate(type= ifelse(grepl("scam", Target) & type=="type","scam",type)) %>%
mutate(type= ifelse(grepl("aegis", Target) & type=="type","aegis",type)) %>%
mutate(type=ifelse(grepl("[0-9]", Target) & type=="type","num",type)) %>%
mutate(type=ifelse(type=="type","other",type))

libs_scct.df <- readRDS("~/DAR-Mars-F24/StudentData/v1_libs_earth_references.Rds")
libs_scct.sol <- cbind(libs_scct.df[,c(3,1,2)],"scct")

colnames(libs.sol)<-c("Sol","Target_Name","Points_Abrasion","Type")
colnames(pixl.sol)<-c("Sol","Target_Name","Points_Abrasion","Type")
colnames(libs_scct.sol)<-c("Sol","Target_Name","Points_Abrasion","Type")

pixl.sol$Sol<-as.numeric(pixl.sol$Sol)
libs.sol$Points_Abrasion<-as.character(libs.sol$Points_Abrasion)
libs_scct.sol$Points_Abrasion<-as.character(libs_scct.sol$Points_Abrasion)

libsandpixl<-rbind(libs.sol,pixl.sol)
libsandpixl<-rbind(libs.sol,libs_scct.sol,pixl.sol)
libsandpixl$Type<-as.factor(libsandpixl$Type)

ggplot()+
Expand Down Expand Up @@ -656,31 +665,34 @@ grid.arrange(plot1, plot2, plot3, plot4,plot5,plot6,ncol=1)

## LIBS Data according to its nearest PIXL cluster
```{r}
libsandpixl<-readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/StudentData/PIXL_LIBS_Combined.Rds")
libsandpixl<-na.omit(libsandpixl)
libsandpixl<-cbind("index"=0,libsandpixl)
libsandpixl$index<-rownames(libsandpixl)

libs_trim.df<-cbind("index"=0,libs_trim.df)
libs_trim.df$index<-rownames(libs_trim.df)
libs_trim.df<-libs_trim.df[libs_trim.df$index %in% libsandpixl$index,]

libs.matrix <- as.matrix(libs_trim.df[,7:14])

libs.tern <- as.data.frame(libs.matrix) %>%
mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>%
select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O,TiO2))

libs.tern<-cbind(libs.tern,"Abrasion"=as.factor(libsandpixl$abrasion))

ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) +
geom_point(data=libs.tern,aes(color=Abrasion,alpha=0.5)) +
theme_rgbw() +
labs(title="Mars LIBS Corresponding to PIXL",
x="Si+Al",
y="Fe+Mg",
z="Ca+Na+K")+theme(legend.position="right") +
geom_point(data=libs.tern,
aes(color=Abrasion,alpha=0.5)) +
guides(alpha="none")
## IMPORTANT: Same problem as before, "PIXL_LIBS_Combined.Rds" has been changed and you should be using "v1_libs_to_sample.Rds". Additionally, I notice that you are calling this "libsandpixl" even though in the previous section you had a completely different data frame "libsandpixl" (containing completely different types of data). Also, this is the second time you are importing "PIXL_LIBS_Combined.Rds". If you get it working in the previous notes code block, you can probably just use that again here.


# libsandpixl<-readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/StudentData/PIXL_LIBS_Combined.Rds")
# libsandpixl<-na.omit(libsandpixl)
# libsandpixl<-cbind("index"=0,libsandpixl)
# libsandpixl$index<-rownames(libsandpixl)
#
# libs_trim.df<-cbind("index"=0,libs_trim.df)
# libs_trim.df$index<-rownames(libs_trim.df)
# libs_trim.df<-libs_trim.df[libs_trim.df$index %in% libsandpixl$index,]
#
# libs.matrix <- as.matrix(libs_trim.df[,7:14])
#
# libs.tern <- as.data.frame(libs.matrix) %>%
# mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>%
# select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O,TiO2))
#
# libs.tern<-cbind(libs.tern,"Abrasion"=as.factor(libsandpixl$abrasion))
#
# ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) +
# geom_point(data=libs.tern,aes(color=Abrasion,alpha=0.5)) +
# theme_rgbw() +
# labs(title="Mars LIBS Corresponding to PIXL",
# x="Si+Al",
# y="Fe+Mg",
# z="Ca+Na+K")+theme(legend.position="right") +
# geom_point(data=libs.tern,
# aes(color=Abrasion,alpha=0.5)) +
# guides(alpha="none")
```
Binary file added AppRelated/comprehensive_LIBS.pdf
Binary file not shown.
Binary file removed StudentData/comprehensive_LIBS.pdf
Binary file not shown.