Outline Data Collection
Morphometrics Lesson 2
Loading Packages and Scripts
Locate Data
We will quantify outlines of belemnite hooks from .tif images in the folder “BelemniteHooks”
#Find all .tif files in the BelemniteHooks subfolder.
Image.files<-list.files(path = "./BelemniteHooks", pattern=".tif")
length(Image.files)[1] 87
87 images is too many to work on in this exercise. We can randomly choose a subset of the image list. You can change the number in set.seed() to choose a different subset. Change the ‘size’-parameter in sample() to pick a different number of images.
Collect data
Let’s try collecting outline data from the first specimen with different numbers of points subdividing the outline. We will do this with 50, 100, 150, and 200 points each and then plot the results to compare. Note the data this function collects will be written to a file in your image folder.
This command should open an interactive window that will prompt you to find the scale bar in the image and indicate the starting point of the outline.
Note: if the interactive plot doesnt work, you might just need to restart R Studio.
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"PointNumberTest_50"),
Specimen.Labels=NULL, OutlinePoints=50,
Smoothing=1, Scale=TRUE,
RawVersion=TRUE)
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"PointNumberTest_100"),
Specimen.Labels=NULL, OutlinePoints=100,
Smoothing=1, Scale=TRUE,
RawVersion=TRUE)
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"PointNumberTest_150"),
Specimen.Labels=NULL,
OutlinePoints=150,
Smoothing=1, Scale=TRUE,
RawVersion=TRUE)
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"PointNumberTest_200"),
Specimen.Labels=NULL, OutlinePoints=200,
Smoothing=1, Scale=TRUE,
RawVersion=TRUE)Now load in the outlines you collected (they were saved as .nts files) and plot them all.
Outline.50 <- readland.nts(paste0(output_folder,"PointNumberTest_50_Raw.nts"))
Outline.100 <- readland.nts(paste0(output_folder,"PointNumberTest_100_Raw.nts"))
Outline.150 <- readland.nts(paste0(output_folder,"PointNumberTest_150_Raw.nts"))
Outline.200 <- readland.nts(paste0(output_folder,"PointNumberTest_200_Raw.nts"))
layout(matrix(1:4, 2, 2, byrow=TRUE))
plot(Outline.50[,1,1], Outline.50[,2,1],
type="p", xlab="x", ylab="y", asp=1,
pch=16, cex=0.6, main="50 points")
lines(Outline.50[,1,1], Outline.50[,2,1], lwd=0.5, col="grey50")
plot(Outline.100[,1,1], Outline.100[,2,1],
type="p", xlab="x", ylab="y", asp=1,
pch=16, cex=0.6, main="100 points")
lines(Outline.100[,1,1], Outline.100[,2,1], lwd=0.5, col="grey50")
plot(Outline.150[,1,1], Outline.150[,2,1],
type="p", xlab="x", ylab="y", asp=1,
pch=16, cex=0.6, main="150 points")
lines(Outline.150[,1,1], Outline.150[,2,1], lwd=0.5, col="grey50")
plot(Outline.200[,1,1], Outline.200[,2,1],
type="p", xlab="x", ylab="y", asp=1,
pch=16, cex=0.6, main="200 points")
lines(Outline.200[,1,1], Outline.200[,2,1], lwd=0.5, col="grey50")Smoothing
When we collected the outline data for this specimen moments ago, we chose the option “Smoothing = 1”. There can be some noise in the outline of your black/white thresholded image that represents non-biological variation that we aren.t really interested in. For this reason, outline morphometrics software like SHAPE and this OutlineExtraction function have options for smoothing. By default, OutlineExtraction saves the raw landmarks (Smoothing = 0) and the data smoothed to your specified level.
Let’s run the data collection again with Smoothing = 2 and Smoothing =3 and compare to 0 and 1:
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"SmoothTest_2"),
Specimen.Labels=NULL, OutlinePoints=100,
Smoothing=2, Scale=TRUE,
RawVersion=FALSE)
OutlineExtraction(Images=paste0(image_folder,Image.work[1]),
Output=paste0(output_folder,"SmoothTest_3"),
Specimen.Labels=NULL, OutlinePoints=100,
Smoothing=3, Scale=TRUE,
RawVersion=FALSE)And then compare visually
Outline.Raw<-Read.NTS(paste0(output_folder,"PointNumberTest_100_Raw.nts"))
Outline.1<-Read.NTS(paste0(output_folder,"PointNumberTest_100.nts"))
Outline.2<-Read.NTS(paste0(output_folder,"SmoothTest_2.nts"))
Outline.3<-Read.NTS(paste0(output_folder,"SmoothTest_3.nts"))
layout(matrix(1:4, 2, 2, byrow=TRUE))
plot(Outline.Raw[,"x",1], Outline.Raw[,"y",1],
type="l", xlab="x", ylab="y", asp=1,
main="No smoothing")
plot(Outline.1[,"x",1], Outline.1[,"y",1],
type="l", xlab="x", ylab="y", asp=1,
main="1 iteration")
plot(Outline.2[,"x",1], Outline.2[,"y",1],
type="l", xlab="x", ylab="y", asp=1,
main="2 iterations")
plot(Outline.3[,"x",1], Outline.3[,"y",1],
type="l", xlab="x", ylab="y", asp=1,
main="3 iterations")So which options look the best? We need to balance the fidelity, noise, and the cost of increased dimensions
Create Ellipitical Fourier Analysis
Let’s load in all the data for all the belemnite hooks. Note that we are reading in a single .nts file. A nice feature of the .nts file format for landmark data is that all data is stored in a single file, with one specimen per row. We are using the readland.nts function from the geomorph package to read the data.
Belemnite.Full<-readland.nts("./Data/Belemnite_SmoothedOutline.nts")
dim(Belemnite.Full)[1] 70 2 174
Using the function dim, we can see have 70 outline points in two dimensions (x and y) and 174 observations. Actually what we have is 87 specimens that were each measured twice to help us examine user error/measurement error. The next step is to separate the two batches. The simple way to do that is that we know that the first 87 specimens are replicate 1 and the second 87 specimens are replicate two, so we could simply do the following:
Belemnite.R1<-Belemnite.Full[,,1:87]
Belemnite.R2<-Belemnite.Full[,,88:174]However, there is a better way: we can use the names of the specimens to sort them. This protects us from mistakes, especially when we have many hundreds or even thousands of specimens. It is always a good idea to name your files in a clear and methodical way. In this case we have added R1 or R2 to each file name to indicate which replicate they are associated with. Now we can programmatically separate the two replicates
Specimens<-dimnames(Belemnite.Full)[[3]]
Belemnite.R1<-Belemnite.Full[,,str_detect(Specimens, "R1")]
Belemnite.R2<-Belemnite.Full[,,str_detect(Specimens, "R2")]EFA
Now we are ready to carry out the normalized elliptical fourier analysis, decomposing out 70 semilandmarks for each specimen into harmomics. How can we do this? Ordinarily we would use the efourier function in the Momocs package. The problem is that Momocs is currently in a state of flux and is being retired for the new Momocs2 package that is not currently fully operational. Thus, we will use Momocs for plotting but not for the initial Fourier and inverse Fourier analyses. Instead we will use custom code written by Manuel F. G. Weinkauf.
The NEF function will translate our landmarks into harmonics. For this example, we will specify that we want to describe each outline with 15 harmonics
#make an empty list to hold our Fourier analysis results
EFA<-list()
#create a vector of specimen names which are the
Spec.Names<-strsplit(dimnames(Belemnite.R1)[[3]],
split=".",
fixed=TRUE)
for (i in 1:dim(Belemnite.R1)[3]) {
EFA[[i]]<-NEF(Belemnite.R1[,,i], Harmonics=15)
names(EFA)[i]<-Spec.Names[[i]][1]
}Examine the output, an object called EFA. What does it contain?
Now we can assess how well the EFA describes the data. To do this, we will use the inverse Fourier function (iefourier) to convert the harmonics back in to landmarks.
#Reconstruct outlines
Recon.Outlines<-list()
for (i in 1:length(EFA)) {
Coords<-iefourier(an=EFA[[i]]$A,
bn=EFA[[i]]$B,
cn=EFA[[i]]$C,
dn=EFA[[i]]$D,
Harmonics=15,
Points=70)
Recon.Outlines[[i]]<-matrix(c(Coords$x, Coords$y),
length(Coords$x), 2)
names(Recon.Outlines)[[i]]<-names(EFA)[[i]]
}
#Out is a function from the Momocs package that turns a matrix of coordindates
#in a closed outline into an object of class Coo, which is useful for
Recon.Out<-Out(Recon.Outlines)Looks pretty good! notice that as part of the normalized EFA algorithm the specimens have been rotated, positioned, and scaled.
Another cool way to look at our data is to use the mosaic function in Momocs to plot all the specimens at the same time
mosaic(Recon.Out)Calculate Power
We have used 15 harmonics and each harmonic has 4 parameters, so we have 60 variables per specimen right now.
Okay, so it looks like 8 harmonics give us 99% of the detail in the original shape. Lets have a look at that by reconstructing the outline of one specimen from increasing numbers of harmonics
Recon.Outlines<-list()
for (i in 1:4) {
harms_temp<-c(2,4,6,8)[i]
Coords<-iefourier(an=EFA[[1]]$A,
bn=EFA[[1]]$B,
cn=EFA[[1]]$C,
dn=EFA[[1]]$D,
Harmonics=harms_temp,
Points=70)
Recon.Outlines[[i]]<-matrix(c(Coords$x,
Coords$y),
length(Coords$x), 2)
}
harm_recons<-Out(Recon.Outlines)
#plot
layout(matrix(1:4, 2, 2, byrow=TRUE))
coo_plot(harm_recons$coo[[1]],
border="mediumblue",
xlim=c(-1.2, 1.2),
ylim=c(-1, 1),
lwd = 2,
main = "2 Harmonics")
coo_plot(harm_recons$coo[[2]],
border="mediumblue",
xlim=c(-1.2, 1.2),
ylim=c(-1, 1),
lwd = 2,
main = "4 Harmonics")
coo_plot(harm_recons$coo[[3]],
border="mediumblue",
xlim=c(-1.2, 1.2),
ylim=c(-1, 1),
lwd = 2,
main = "6 Harmonics")
coo_plot(harm_recons$coo[[4]],
border="mediumblue",
xlim=c(-1.2, 1.2),
ylim=c(-1, 1),
lwd = 2,
main = "8 Harmonics")OK that is great and all but we have actually only assessed power for the first specimen. Now lets see how it looks for all specimens.
Harm.Power <- matrix(NA, length(EFA), 14)
Cutoff <- vector(mode="numeric", length=length(EFA))
for (i in 1:length(EFA)) {
Harm.Power[i,]<-Power(an=EFA[[i]]$A,
bn=EFA[[i]]$B,
cn=EFA[[i]]$C,
dn=EFA[[i]]$D,
first=FALSE,
last=14)
Cutoff[i]<-min(which(Harm.Power[i,]>=0.99))+1
}
Harm.Power<-Harm.Power*100
SD<-apply(Harm.Power, MARGIN=2, FUN=sd)
MEAN<-apply(Harm.Power, MARGIN=2, FUN=mean)
plot(x=2:(ncol(Harm.Power)+1),
y=MEAN,
xlab="Harmonic",
ylab="Power (%)",
type="b")
for (i in 1:length(MEAN)) {
if (SD[i]>0) {
arrows(x0=i+1, y0=MEAN[i],
x1=i+1, y1=MEAN[i]+SD[i],
length=0.1, angle=90,
col="grey50")
arrows(x0=i+1, y0=MEAN[i],
x1=i+1, y1=MEAN[i]-SD[i],
length=0.1, angle=90, col="grey50")
}
}
abline(h=99, col="blue", lwd=2)Examine the plot generated with the last code chunk. How many harmonics should we use?