Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Record print methods #22

Merged
merged 10 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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

* Refactored the internal class data structures for better modularity and extensibility (PR #8).
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
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
Loading