Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into add-usage-vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Oct 11, 2024
2 parents ca09664 + 560cc22 commit 5dd080d
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

- name: Install lamindb
run: |
pip install lamindb[bionty]
pip install lamindb[bionty,wetlab]
- name: Log in to Lamin
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:

- name: Install lamindb
run: |
pip install lamindb[bionty]
pip install lamindb[bionty,wetlab]
- name: Log in to Lamin
run: |
Expand All @@ -60,7 +60,7 @@ jobs:

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
uses: JamesIves/github-pages-deploy-action@v4
with:
clean: false
branch: gh-pages
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.quarto
*.Rproj

experiments
docs
16 changes: 10 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* Read user settings from env file created by lamin Python package (PR #2, PR #8).

* Render a pkgdown website (PR #13).
* Add `to_string()` and `print()` methods to the `Record` class and (incomplete) `describe()` method to the `Artifact()` class (PR #22).

## MAJOR CHANGES

Expand All @@ -21,17 +21,21 @@

## MINOR CHANGES

* Update `README` with new set up instructions and simplify (PR #14).

* Do not complain when foreign keys are not found in a record, but also do not complain when they are (PR #13).

* Further simplify the `README`, and move the detailed usage description to a separate vignette (PR #13).
* Define a current user and current instance with lamin-cli prior to testing and generating documentation in the CI (PR #23).

* Add a simple unit test which queries laminlabs/lamindata (PR #27).

## DOCUMENTATION

* Update `README` with new set up instructions and simplify (PR #14).

* Add a `pkgdown` website to the project (PR #13).

* Generate vignettes using Quarto (PR #13).
* Further simplify the `README`, and move the detailed usage description to a separate vignette (PR #13).

* Define a current user and current instance with lamin-cli prior to testing and generating documentation in the CI (PR #23).
* Generate vignettes using Quarto (PR #13).

* Add vignette to showcase laminr usage (PR #18).

Expand Down
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: laminr
Title: LaminDB interface in R
Version: 0.1.0.9000
Version: 0.1.0.9001
Authors@R:
c(person("Robrecht", "Cannoodt",
role = c("aut", "cre"),
Expand All @@ -26,5 +26,8 @@ Imports:
Suggests:
anndata,
quarto,
s3 (>= 1.1.0)
s3 (>= 1.1.0),
testthat (>= 3.0.0),
withr
VignetteBuilder: quarto
Config/testthat/edition: 3
49 changes: 49 additions & 0 deletions R/Artifact.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,55 @@ ArtifactRecord <- R6::R6Class( # nolint object_name_linter
} else {
cli_abort(paste0("Unsupported storage type: ", artifact_storage$type))
}
},
describe = function(style = TRUE) {
provenance_fields <- c(
storage = "root",
transform = "name",
run = "started_at",
created_by = "handle",
test = "fail"
)

output_strings <- character()

provenance_strings <- purrr::map_chr(
names(provenance_fields),
function(.field) {
field_name <- try(self[[.field]][[provenance_fields[.field]]])
if (inherits(field_name, "try-error") || is.null(field_name)) {
return(NA_character_)
}

if (is.character(field_name)) {
field_name <- paste0("'", field_name, "'")
}

paste0(
cli::col_blue(" $", .field),
cli::col_br_blue(" = "),
cli::col_yellow(field_name)
)
}
) |>
purrr::discard(is.na)

if (length(provenance_strings) > 0) {
output_strings <- c(
output_strings,
cli::style_italic(cli::col_br_magenta(" Provenance")),
provenance_strings
)
}

self$print(style)
for (.line in output_strings) {
if (isFALSE(style)) {
.line <- cli::ansi_strip(.line)
}

cli::cat_line(.line)
}
}
)
)
56 changes: 56 additions & 0 deletions R/Record.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ Record <- R6::R6Class( # nolint object_name_linter
)
)
}
},
print = function(style = TRUE) {
cli::cat_line(self$to_string(style))
},
to_string = function(style = FALSE) {
important_fields <- c(
"uid",
"handle",
"name",
"root",
"description",
"key"
)

record_fields <- private$.api$get_record(
module_name = private$.registry$module$name,
registry_name = private$.registry$name,
id_or_uid = private$.data[["uid"]],
include_foreign_keys = TRUE
)

# Get the important fields that are in the record
important_fields <- intersect(important_fields, names(record_fields))
# Put important fields before all other fields
field_names <- c(
important_fields, setdiff(names(record_fields), important_fields)
)

field_strings <- purrr::map_chr(field_names, function(.name) {
value <- record_fields[[.name]]

if (is.null(value)) {
return(NA_character_)
}

if (is.character(value)) {
value <- paste0("'", value, "'")
}

paste0(
cli::col_blue(.name), cli::col_br_blue("="), cli::col_yellow(value)
)
}) |>
purrr::discard(is.na)

string <- paste0(
cli::style_bold(cli::col_green(private$.registry$class_name)), "(",
paste(field_strings, collapse = ", "),
")"
)

if (isFALSE(style)) {
string <- cli::ansi_strip(string)
}

return(string)
}
),
private = list(
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ Get an artifact:

``` r
artifact <- db$Artifact$get("KBW89Mf7IGcekja2hADu")
artifact
```

Artifact(uid='KBW89Mf7IGcekja2hADu', description='Myeloid compartment', key='cell-census/2024-07-01/h5ads/fe52003e-1460-4a65-a213-2bb1a508332f.h5ad', version='2024-07-01', _accessor='AnnData', id=3659, transform_id=22, size=691757462, is_latest=TRUE, created_by_id=1, _hash_type='md5-n', type='dataset', created_at='2024-07-12T12:34:10.345829+00:00', n_observations=51552, updated_at='2024-07-12T12:40:48.837026+00:00', run_id=27, suffix='.h5ad', visibility=1, _key_is_virtual=FALSE, hash='SZ5tB0T4YKfiUuUkAL09ZA', storage_id=2)

Access some of its fields:

``` r
Expand Down
1 change: 1 addition & 0 deletions README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Get an artifact:

```{r get_artifact}
artifact <- db$Artifact$get("KBW89Mf7IGcekja2hADu")
artifact
```

Access some of its fields:
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(laminr)

test_check("laminr")
42 changes: 42 additions & 0 deletions tests/testthat/helper-setup_lamindata_instance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local_setup_lamindata_instance <- function(env = parent.frame()) {
root_dir <- withr::local_file(tempfile(), .local_envir = env)
withr::local_envvar(c(LAMIN_SETTINGS_DIR = root_dir), .local_envir = env)

# create a temporary directory for the settings
lamin_dir <- file.path(root_dir, ".lamin")
dir.create(lamin_dir, recursive = TRUE, showWarnings = FALSE)

# generate user settings
user_settings <- list(
email = "null",
password = "null",
access_token = "null",
api_key = "null",
uid = "00000000",
uuid = "null",
handle = "anonymous",
name = "null"
)
user_lines <- paste0("lamin_user_", names(user_settings), "=", unlist(user_settings))
writeLines(user_lines, file.path(lamin_dir, "current_user.env"))

# generate instance settings
instance_settings <- list(
owner = "laminlabs",
name = "lamindata",
api_url = "https://us-east-1.api.lamin.ai",
storage_root = "s3://lamindata",
storage_region = "us-east-1",
db = "null",
schema_str = "bionty,wetlab",
schema_id = "097186c3e91c01ced47aa3e01a3c1515",
id = "037ba1e08d804f91a90275a47735076a",
git_repo = "null",
keep_artifacts_local = "False"
)
instance_lines <- paste0("lamindb_instance_", names(instance_settings), "=", unlist(instance_settings))
writeLines(instance_lines, file.path(lamin_dir, "current_instance.env"))
writeLines(instance_lines, file.path(lamin_dir, "instance--laminlabs--lamindata.env"))

root_dir
}
21 changes: 21 additions & 0 deletions tests/testthat/test-connect_lamindata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
skip_if_offline()

test_that("Connecting to lamindata works", {
local_setup_lamindata_instance()

# try to connect to lamindata
db <- connect("laminlabs/lamindata")

# check whether schema was parsed and classes were created
expect_equal(db$Artifact$name, "artifact")

# try to fetch a record
artifact <- db$Artifact$get("mePviem4DGM4SFzvLXf3")

expect_equal(artifact$uid, "mePviem4DGM4SFzvLXf3")
expect_equal(artifact$suffix, ".csv")

# try to fetch linked records
created_by <- artifact$created_by
expect_equal(created_by$handle, "sunnyosun")
})

0 comments on commit 5dd080d

Please sign in to comment.