Skip to content

Commit

Permalink
Merge pull request #34 from comet-ml/cm-7191-log-artifact
Browse files Browse the repository at this point in the history
[CM-7191] create log artifact REST API
  • Loading branch information
yaricom committed Oct 10, 2023
2 parents 8933af9 + 22ed5f6 commit 0bef6f9
Show file tree
Hide file tree
Showing 37 changed files with 23,344 additions and 173 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^cometr\.log$
^codecov\.yml$
^cometlog\.log$
^\.github$
4 changes: 2 additions & 2 deletions .github/workflows/r.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
r-version: ["3.6.3", "4.1.1"]
r-version: ["4.3.1"]

steps:
- uses: actions/checkout@v2
- name: Set up R ${{ matrix.r-version }}
uses: r-lib/actions/setup-r@f57f1301a053485946083d7a45022b278929a78a
uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.r-version }}
- name: Install dependencies
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.Rhistory
.RData
.Ruserdata
.Renviron
.comet.yml
cometr.log
cometlog.log

.DS_Store
14 changes: 10 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
Package: cometr
Title: 'Comet' API for R
Version: 0.2.0
Version: 0.3.0
Authors@R: c(
person(given = "Dean",
family = "Attali",
role = c("aut"),
email = "daattali@gmail.com"),
person(given = "Iaroslav",
family = "Omelianenko",
role = c("aut"),
email = "iaroslav@comet.com",
comment = c(ORCID = "0000-0002-2190-5664")),
person(given = "Nimrod",
family = "Lahav",
role = c("cre"),
email = "nimrod@comet.ml"))
email = "nimrod@comet.com"),
person("Comet ML, Inc.", role = c("cph", "fnd")))
Description: A convenient 'R' wrapper to the 'Comet' API, which is a cloud
platform allowing you to track, compare, explain and optimize machine
learning experiments and models. Experiments can be viewed on the 'Comet'
online dashboard at <https://www.comet.ml>.
online dashboard at <https://www.comet.com>.
URL: https://github.com/comet-ml/cometr
BugReports: https://github.com/comet-ml/cometr/issues
Imports:
Expand All @@ -37,4 +43,4 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.2.3
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(Artifact)
export(ArtifactAsset)
export(Experiment)
export(LoggedArtifact)
export(LoggedArtifactAsset)
export(call_api)
export(create_experiment)
export(create_project)
Expand All @@ -13,3 +17,5 @@ export(get_experiments)
export(get_multi_metric_chart)
export(get_projects)
export(get_workspaces)
import(R6)
import(utils)
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# cometr 0.3.0

* Added support for logging Comet Artifacts `Experiment$log_artifact(artifact)`
* Added experiment method to get workspace name `get_workspace_name()`
* Added experiment method to get project name `get_project_name()`

# cometr 0.2.0

* Added `get_experiment("PREVIOUS-EXPERIMENT-KEY")`
Expand Down
46 changes: 33 additions & 13 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,64 @@ call_api <- function(endpoint, method = c("GET", "POST"), params = list(), respo
tryCatch({
if (endpoint == "/write/experiment/upload-asset" ||
endpoint == "/write/experiment/git/patch") {
LOG_INFO("API call: ", endpoint)
body_params <- list(file = httr::upload_file(params$file))
params$file <- NULL
body_params <- list()
if (!is.null(params[["file"]])) {
# local assets
body_params$file <- httr::upload_file(params[["file"]])
params[["file"]] <- NULL
}
if (!is.null(params$remote_uri)) {
# remote assets
body_params$link = params$remote_uri
params[["remote_uri"]] <- NULL
}

if (!is.null(params$metadata)) {
body_params$metadata <- jsonlite::toJSON(params$metadata)
params$metadata <- NULL
body_params$metadata <- encode_metadata(params$metadata)
params[["metadata"]] <- NULL
}

url <- httr::modify_url(url, query = params)
LOG_INFO("API call: ", method, " ", url, ", params: ", params, ", body_params ", body_params)
response <- httr::POST(url, auth, agent, encode = "multipart", body = body_params, timeout)
} else if (method == "GET") {
url <- httr::modify_url(url, query = params)
LOG_INFO("API call: ", method, " ", url)
LOG_INFO("API call: ", method, " ", url, ", params: ", params)
response <- httr::GET(url, auth, agent, timeout)
} else if (method == "POST") {
LOG_INFO("API call: ", method, " ", url, " ", params)
LOG_INFO("API call: ", method, " ", url, ", params: ", params)
response <- httr::POST(url, auth, agent, encode = "json", body = params, timeout)
}
}, error = function(err) {
comet_stop("Error calling Comet API: ", err$message)
})

check_response(response)
check_response(res = response, params = params)

parsed <- parse_response(response, response_json = response_json)
LOG_INFO("Parsed response: ", parsed)
parsed
}

check_response <- function(res) {
check_response <- function(res, params = NULL) {
tryCatch({
if (httr::status_code(res) != 200) {
code <- httr::status_code(res)
res <- try(parse_response(res), silent = TRUE)
if (is.list(res) && !is.null(res[["msg"]])) {
stop(res[["msg"]])
} else {
stop("Comet API response status was not OK (", code, ")")
if (is.list(res)) {
if (!is.null(res[["sdk_error_code"]])) {
sdk_error_code <- as.integer(res[["sdk_error_code"]])
if (sdk_error_code == 624523) {
comet_stop("Artifact not found with: ", params)
} else if (sdk_error_code == 90403 || sdk_error_code == 90402) {
comet_stop("Artifact is not in a finalized state and cannot be accessed with: ", params)
}
}
if (!is.null(res[["msg"]])) {
stop(res[["msg"]])
}
}
stop("Comet API response status was not OK (", code, ")")
}
}, error = function(err) {
comet_stop("Error with Comet API response status: ", err$message)
Expand Down
Loading

0 comments on commit 0bef6f9

Please sign in to comment.