Last active
September 12, 2022 23:56
-
-
Save jlmelville/339dfeb80c3e836e887d70a37679b244 to your computer and use it in GitHub Desktop.
Reading Isomap data into R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Swiss Roll | |
# Install the R.matlab package: | |
# install.packages("R.matlab") | |
# downloads and reads Swiss Roll data | |
# returns a list: | |
# X.data a 3 x 20000 matrix of the column-stored X, Y, Z data | |
# Y.data a 2 x 20000 matrix of what I assume is the unrolled data coordinates | |
read_isoswiss <- function(url = "http://web.mit.edu/cocosci/isomap/swiss_roll_data.mat") { | |
if (!requireNamespace("R.matlab", quietly = TRUE)) { | |
stop("Library 'R.matlab' needed for this function to work. Please install it.", | |
call. = FALSE) | |
} | |
f <- gzcon(base::url(url, "rb")) | |
res <- R.matlab::readMat(f) | |
close(f) | |
res | |
} | |
# install the snedata package and make use of a private function | |
# remotes::install_github("jlmelville/snedata") | |
# Returns data frame: | |
# each row is an observation, with x, y, z coordinate and color string value (helpful for visualization) | |
read_isoswiss_df <- function(url = "http://web.mit.edu/cocosci/isomap/swiss_roll_data.mat") { | |
if (!requireNamespace("snedata", quietly = TRUE)) { | |
stop("Github library 'snedata' needed for this function to work. Please install it.", | |
call. = FALSE) | |
} | |
swiss_list <- read_isoswiss() | |
swiss_roll <- data.frame(t(swiss_list$X.data)) | |
swiss_roll <- cbind(swiss_roll, snedata:::linear_color_map(swiss_list$Y.data[1, ])) | |
colnames(swiss_roll) <- c("x", "y", "z", "color") | |
swiss_roll | |
} | |
# plot the cross-section | |
plot(isoswiss_roll$x, isoswiss_roll$y, col = isoswiss_roll$color, cex = 0.1) | |
### Faces | |
# Two extra problems here: | |
# 1. Dataset no longer exists on the Stanford website | |
# 2. Data was Z compressed, which is a pain to uncompress via R (you need to use an external program). | |
# Procedure: | |
# 1. Download the dataset via the wayback machine: | |
# https://web.archive.org/web/20160913051505/http://isomap.stanford.edu/face_data.mat.Z | |
# 2. uncompress manually (e.g. using 7-zip) | |
# 3. Run the code below | |
library(R.matlab) | |
facesl <- readMat(con = "wherever-you-downloaded-and-decompressed/face_data.mat") | |
isofaces <- t(facesl$images) | |
colnames(isofaces) <- paste0('px', 1:4096) | |
isofaces <- data.frame(isofaces) | |
# pose1 and pose2 relate to the angle of the head, either could be used to color the manifold the faces lie on | |
isofaces <- cbind(isofaces, pose1 = facesl$poses[1, ], pose2 = facesl$poses[2, ]) | |
# displays face in row i of the dataframe | |
view_isoface <- function(df, i) { | |
graphics::image(matrix(as.numeric(df[i, 1:4096]), nrow = 64, ncol = 64, byrow = TRUE)[, 64:1], | |
col = grDevices::gray(1:255 / 255)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment