Skip to content

Commit

Permalink
#131: allow relative paths when saving a model
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmelville committed Dec 14, 2024
1 parent fd5f337 commit b91ab76
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Authors@R: c(
person("Aaron", "Lun", role = "ctb"),
person("Mohamed Nadhir", "Djekidel", role = "ctb"),
person("Yuhan", "Hao", role = "ctb"),
person("Dirk", "Eddelbuettel", role = "ctb")
person("Dirk", "Eddelbuettel", role = "ctb"),
person("Wouter", "van der Bijl", role = "ctb")
)
Description: An implementation of the Uniform Manifold Approximation and
Projection dimensionality reduction by McInnes et al. (2018)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ been.
continued to return the fuzzy graph in transposed form. Thank you
[PedroMilanezAlmeida](https://github.com/PedroMilanezAlmeida) for
reopening the issue (<https://github.com/jlmelville/uwot/issues/118>).
* Relative paths could not be used to save a model. Thank you
[Wouter van der Bijl](https://github.com/Ax3man) for the bug report
(<https://github.com/jlmelville/uwot/issues/131>) and the suggested fix.

# uwot 0.2.2

Expand Down
2 changes: 1 addition & 1 deletion R/uwot.R
Original file line number Diff line number Diff line change
Expand Up @@ -3956,9 +3956,9 @@ save_uwot <- function(model, file, unload = FALSE, verbose = FALSE) {

# archive the files under the temp dir into the single target file
# change directory so the archive only contains one directory
tmp_model_file <- abspath(file)
tsmessage("Changing to ", mod_dir)
setwd(mod_dir)
tmp_model_file <- abspath(file)
tsmessage("Creating ", tmp_model_file)

# #109: Windows 7 tar needs "--force-local" to avoid interpreting colon
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test_saveload.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,42 @@ test_that("save-load nndescent", {
unlink(mod_fname2)
}
})

# 131: can't use a relative path for saving
test_that("save-load relative path", {
set.seed(1337)
model <- umap(iris10,
n_neighbors = 4, n_epochs = 2, init = "spca",
metric = "euclidean", verbose = FALSE, n_threads = 0,
ret_model = TRUE
)
# remember to go back to the original working directory
old_wd <- getwd()
on.exit(setwd(old_wd), add = TRUE)

# move to a temp directory for this test
test_dir <- tempfile("test_relative_path_")
dir.create(test_dir)
setwd(test_dir)

# Test 1: relative path no sub-folders
rel_path <- "model.uwot"
model <- save_uwot(model, file = rel_path)
expect_true(file.exists(rel_path))

modelload <- load_uwot(file = rel_path)
set.seed(1337)
resload_trans <- umap_transform(iris10, modelload)
expect_ok_matrix(resload_trans)

# Test 2: Relative path within a sub-folder
dir.create("my_folder")
rel_sub_path <- file.path("my_folder", "model.uwot")
model <- save_uwot(model, file = rel_sub_path)
expect_true(file.exists(rel_sub_path))

modelload <- load_uwot(file = rel_sub_path)
set.seed(1337)
resload_trans <- umap_transform(iris10, modelload)
expect_ok_matrix(resload_trans)
})

0 comments on commit b91ab76

Please sign in to comment.