# workshop_script.R # Peter Bubenik # January 2017 # script for computer workshop: Topological Data Analysis with R # # Data: triangulated 3d figures in nonrigid3d.zip from # http://tosca.cs.technion.ac.il/book/resources_data.html # We filter these triangulations and calculate persistent homology using # Perseus from http://www.sas.upenn.edu/~vnanda/perseus/ # We calculate persistence landscapes using the Persistence Landscapes Toolbox from # https://www.math.upenn.edu/~dlotko/persistenceLandscape.html # We compare figure types pairwise and perform (multiclass) classification. # Here we set some user-defined parameters - you can change these #main_directory <- "C:/Users/Leo/Desktop/R/workshop" # sample directory for windows users main_directory <- "~/R/workshop" # sample directory for Mac/linux users operating_system <- "Mac" # PC Mac or Linux perseus_directory <- "perseus_output" pl_directory <- "pl_output" animation_directory <- "animation" binary_directory <- "bin" data_directory <- "nonrigid3d" grid_size <- 200 # grid used for Perseus and Persistence Landscape Toolbox max_depth <- 200 # max number of persistence landscape functions max_filtration_value <- grid_size pl_param_vals <- seq(0,grid_size,0.5) # values at which to evaluate the persistence landscape max_pose_num <- 1 # 50 # maximum pose number for one figure, starts at 0 #figures_to_use <- c("cat","centaur","david","dog","gorilla","horse","lioness","michael","seahorse","shark","victoria","wolf") # cat,centaur,david,dog,gorilla,horse,lioness,michael,seahorse,shark,victoria,wolf figures_to_use <- c("cat","dog") figures_to_compare <- c("cat","dog") #num_frames <- 100 # number of frames in animation of filtration #animation_delay <- 5 # delay time between frames N <- 100 # number of repeats in permutation test cost <- 10 # lower: softer margin, higher: harder margin, default=1 num_folds <- 10 # number of folds for cross validation show_individual_figures <- TRUE # draw filtered simplicial complexes, persistence diagrams, persistence landscapes pca_coords_to_use <- 1:10 # number of pca coordinates to use for classification using pca degrees_to_use <- 0:1 # which homological degrees are used for classification # now the main part # install the necessary packages if not yet installed list.of.packages <- c('scatterplot3d','rgl','FNN','e1071','kernlab') new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages) library(scatterplot3d) # needed for 3d plotting library(rgl) # needed for 3d graphics library(FNN) # needed for k-nearest neighbor library(e1071) # needed for PCA, SVM library(kernlab) # needed for SVM, kernel SVM # set up the needed directories and files setwd(main_directory) # change to the main directory dir.create(file.path(main_directory, perseus_directory)) # comment this line after running once dir.create(file.path(main_directory, pl_directory)) # comment this line after running once if (!file.exists(file.path(main_directory,pl_directory,"configure"))) file.copy(file.path(main_directory,binary_directory,"configure"),file.path(main_directory,pl_directory)) # PlotOfLandscapes needs the configure file to be in its working directory source("tda_functions.R") # load Peter Bubenik's topological data analysis functions. # get list of figures figures <- vector() # initialize vector for (figure_name in figures_to_use) # for each type of figure for (i in 0:max_pose_num) if (file.exists(paste(file.path(data_directory,figure_name),i,".vert",sep=""))) figures <- rbind(figures, c(figure_name,i)) num_figures <- dim(figures)[1] source("persistence_script.R") # load, filter, visualize, persistence landscapes saved to text files load("nonrigid3d_landscape.RData") # use this after running persistence_script.R once # Pairwise Comparison of figures_to_compare vector_length <- length(pl_param_vals) # use for plotting pl_comparison(plm_list, figures[,1], vector_length, figures_to_compare, N, cost, degrees=0:1, figures_to_use) # pval= # Classification of figures_to_use if (num_figures < length(pca_coords_to_use)) pca_coords_to_use <- 1:3 pl_classification(plm_list, figures[,1], vector_length, figures_to_use, degrees_to_use, num_folds, cost, pca_coords_to_use)