Skip to content

Commit

Permalink
Updated package with new NixtlaClient class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MMenchero committed May 20, 2024
1 parent a80013a commit b87bf32
Show file tree
Hide file tree
Showing 47 changed files with 98,822 additions and 39,919 deletions.
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 1.0.0
Date: 2024-02-02 04:29:49 UTC
SHA: 16e8099d0af53baa187225d9bc33efad76e1cbca
Date: 2024-02-03 19:44:30 UTC
SHA: a80013ab126ba73c3c0a809177f4ad168e5365dd
18 changes: 9 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Generated by roxygen2: do not edit by hand

export(.get_token)
export(.timegpt_data_prep)
export(.get_api_key)
export(.nixtla_data_prep)
export(.validate_exogenous)
export(date_conversion)
export(infer_frequency)
export(nixtla_set_token)
export(nixtla_validate_token)
export(timegpt_anomaly_detection)
export(timegpt_cross_validation)
export(timegpt_forecast)
export(timegpt_historic)
export(timegpt_plot)
export(nixtla_client_anomaly_detection)
export(nixtla_client_cross_validation)
export(nixtla_client_forecast)
export(nixtla_client_historic)
export(nixtla_client_plot)
export(nixtla_set_api_key)
export(nixtla_validate_api_key)
importFrom(dplyr,bind_rows)
importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
Expand Down
27 changes: 27 additions & 0 deletions R/get_api_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Get NIXTLA_API_KEY from options or from .Renviron
#' This is a private function of nixtlar
#'
#' @return If available, the NIXTLA_API_KEY. Otherwise it returns an error and a message asking the user to set the API key.
#' @export
#' @keywords internal
#' @examples
#' \dontrun{
#' .get_api_key()
#' }
#'
.get_api_key <- function(){

# Get API key from options
api_key <- Sys.getenv("NIXTLA_API_KEY", unset = NA)
# If not available, get it from .Renviron
if(is.na(api_key)) {
api_key <- getOption("NIXTLA_API_KEY", default = NA)
}

# Return API key or, if not available, stop
if(!is.na(api_key)){
return(api_key)
}else{
stop("Please set NIXTLA_API_KEY. Use nixtla_set_api_key() or set it as an environment variable in .Renviron")
}
}
27 changes: 0 additions & 27 deletions R/get_token.R

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_anomaly_detection(df, id_col="unique_id")
#' fcst <- nixtlar::nixtla_client_anomaly_detection(df, id_col="unique_id")
#' }
#'
timegpt_anomaly_detection <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=c(99), clean_ex_first=TRUE, model="timegpt-1"){
nixtla_client_anomaly_detection <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=c(99), clean_ex_first=TRUE, model="timegpt-1"){

# Prepare data ----
names(df)[which(names(df) == time_col)] <- "ds"
Expand All @@ -35,7 +35,7 @@ timegpt_anomaly_detection <- function(df, freq=NULL, id_col=NULL, time_col="ds",
names(df)[which(names(df) == id_col)] <- "unique_id"
}

data <- .timegpt_data_prep(df, freq, id_col, time_col, target_col)
data <- .nixtla_data_prep(df, freq, id_col, time_col, target_col)
freq <- data$freq
y <- data$y

Expand Down Expand Up @@ -66,12 +66,12 @@ timegpt_anomaly_detection <- function(df, freq=NULL, id_col=NULL, time_col="ds",
timegpt_data[["level"]] <- level

# Make request ----
url_anomaly <- "https://dashboard.nixtla.io/api/timegpt_multi_series_anomalies"
url_anomaly <- "https://dashboard.nixtla.io/api/anomaly_detection_multi_series"
resp_anomaly <- httr2::request(url_anomaly) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_token())
"authorization" = paste("Bearer", .get_api_key())
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = timegpt_data) |>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_cross_validation(df, h = 8, id_col = "unique_id", n_windows = 5)
#' fcst <- nixtlar::nixtla_client_cross_validation(df, h = 8, id_col = "unique_id", n_windows = 5)
#' }
#'
timegpt_cross_validation <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, n_windows=1, step_size=NULL, finetune_steps=0, clean_ex_first=TRUE, model="timegpt-1"){
nixtla_client_cross_validation <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, n_windows=1, step_size=NULL, finetune_steps=0, clean_ex_first=TRUE, model="timegpt-1"){

# Prepare data ----
names(df)[which(names(df) == time_col)] <- "ds"
Expand All @@ -39,7 +39,7 @@ timegpt_cross_validation <- function(df, h=8, freq=NULL, id_col=NULL, time_col="
names(df)[which(names(df) == id_col)] <- "unique_id"
}

data <- .timegpt_data_prep(df, freq, id_col, time_col, target_col)
data <- .nixtla_data_prep(df, freq, id_col, time_col, target_col)
freq <- data$freq
y <- data$y

Expand Down Expand Up @@ -93,12 +93,12 @@ timegpt_cross_validation <- function(df, h=8, freq=NULL, id_col=NULL, time_col="
}

# Make request ----
url_cv <- "https://dashboard.nixtla.io/api/timegpt_multi_series_cross_validation"
url_cv <- "https://dashboard.nixtla.io/api/cross_validation_multi_series"
resp_cv <- httr2::request(url_cv) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_token())
"authorization" = paste("Bearer", .get_api_key())
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = timegpt_data) |>
Expand Down
14 changes: 7 additions & 7 deletions R/timegpt_forecast.R → R/nixtla_client_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' fcst <- nixtlar::nixtla_client_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' }
#'
timegpt_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, finetune_steps=0, clean_ex_first=TRUE, add_history=FALSE, model="timegpt-1"){
nixtla_client_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, finetune_steps=0, clean_ex_first=TRUE, add_history=FALSE, model="timegpt-1"){

# Prepare data ----
names(df)[which(names(df) == time_col)] <- "ds"
Expand All @@ -39,7 +39,7 @@ timegpt_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", tar
names(df)[which(names(df) == id_col)] <- "unique_id"
}

data <- .timegpt_data_prep(df, freq, id_col, time_col, target_col)
data <- .nixtla_data_prep(df, freq, id_col, time_col, target_col)
freq <- data$freq
y <- data$y

Expand Down Expand Up @@ -87,12 +87,12 @@ timegpt_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", tar
}

# Make request ----
url <- "https://dashboard.nixtla.io/api/timegpt_multi_series"
url <- "https://dashboard.nixtla.io/api/forecast_multi_series"
resp <- httr2::request(url) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_token())
"authorization" = paste("Bearer", .get_api_key())
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = timegpt_data) |>
Expand Down Expand Up @@ -145,7 +145,7 @@ timegpt_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", tar

# Generate fitted values ----
if(add_history){
fitted <- timegpt_historic(df, freq=freq, id_col=id_col, time_col=time_col, target_col=target_col, level=level, finetune_steps=finetune_steps, clean_ex_first=clean_ex_first)
fitted <- nixtla_client_historic(df, freq=freq, id_col=id_col, time_col=time_col, target_col=target_col, level=level, finetune_steps=finetune_steps, clean_ex_first=clean_ex_first)
if(tsibble::is_tsibble(df)){
fcst <- dplyr::bind_rows(fitted, fcst)
}else{
Expand Down
12 changes: 6 additions & 6 deletions R/timegpt_historic.R → R/nixtla_client_historic.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_historic(df, id_col="unique_id", level=c(80,95))
#' fcst <- nixtlar::nixtla_client_historic(df, id_col="unique_id", level=c(80,95))
#' }
#'
timegpt_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=NULL, finetune_steps=0, clean_ex_first=TRUE){
nixtla_client_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=NULL, finetune_steps=0, clean_ex_first=TRUE){

# Prepare data ----
names(df)[which(names(df) == time_col)] <- "ds"
Expand All @@ -35,7 +35,7 @@ timegpt_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_c
names(df)[which(names(df) == id_col)] <- "unique_id"
}

data <- .timegpt_data_prep(df, freq, id_col, time_col, target_col)
data <- .nixtla_data_prep(df, freq, id_col, time_col, target_col)
freq <- data$freq
y <- data$y

Expand Down Expand Up @@ -65,12 +65,12 @@ timegpt_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_c
}

# Make request ----
url_historic <- "https://dashboard.nixtla.io/api/timegpt_multi_series_historic"
url_historic <- "https://dashboard.nixtla.io/api/historic_forecast_multi_series"
resp_hist <- httr2::request(url_historic) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_token())
"authorization" = paste("Bearer", .get_api_key())
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = timegpt_data) |>
Expand Down
6 changes: 3 additions & 3 deletions R/timegpt_plot.R → R/nixtla_client_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' fcst <- nixtlar::nixtla_client_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' nixtlar::timegpt_plot(df, fcst, h=8, id_col="unique_id")
#' }
#'
timegpt_plot <- function(df, fcst=NULL, h=NULL, id_col=NULL, time_col="ds", target_col="y", unique_ids = NULL, max_insample_length=NULL, plot_anomalies=FALSE){
nixtla_client_plot <- function(df, fcst=NULL, h=NULL, id_col=NULL, time_col="ds", target_col="y", unique_ids = NULL, max_insample_length=NULL, plot_anomalies=FALSE){

if(!tsibble::is_tsibble(df) & !is.data.frame(df)){
stop("Only tsibbles or data frames are allowed.")
Expand Down
4 changes: 2 additions & 2 deletions R/timegpt_data_prep.R → R/nixtla_data_prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#'
#' @examples
#' df <- nixtlar::electricity
#' data <- .timegpt_data_prep(df, freq="H")
#' data <- .nixtla_data_prep(df, freq="H")
#'
.timegpt_data_prep <- function(df, freq, id_col, time_col, target_col){
.nixtla_data_prep <- function(df, freq, id_col, time_col, target_col){

if(!tsibble::is_tsibble(df) & !is.data.frame(df)){
stop("Only tsibbles or data frames are allowed.")
Expand Down
16 changes: 16 additions & 0 deletions R/nixtla_set_api_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Set API key in global environment
#'
#' @param api_key The user's API key. Get yours here: https://dashboard.nixtla.io/
#'
#' @return A message indicating the API key has been set in the global environment.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' }
#'
nixtla_set_api_key <- function(api_key) {
options("NIXTLA_API_KEY"=api_key)
message("API key has been set for the current session.")
}
16 changes: 0 additions & 16 deletions R/nixtla_set_token.R

This file was deleted.

30 changes: 30 additions & 0 deletions R/nixtla_validate_api_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Validate API key
#'
#' @return A status code and a message indicating whether the API key is valid.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' nixtlar::nixtla_validate_api_key
#' }
#'
nixtla_validate_api_key <- function(){

api_key <- .get_api_key()

url <- "https://dashboard.nixtla.io/api/forecast"
resp <- httr2::request(url) |>
httr2::req_headers(
Authorization = paste("Bearer", api_key)
) |>
httr2::req_user_agent("nixtla-r") |>
httr2::req_perform()

status_code <- httr2::resp_status(resp)
if(status_code >= 200 & status_code < 300){
message("API key validation successful. Happy forecasting! :) \nIf you have questions or need support, please email [email protected]")
}else{
message("API key validation failed. Please go https://dashboard.nixtla.io/ to get a valid API key.")
}
}
30 changes: 0 additions & 30 deletions R/nixtla_validate_token.R

This file was deleted.

4 changes: 4 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Resubmission 9 Feb 2024
This is a resubmission. In this version I have:


## Resubmission 3 Feb 2024
This is a resubmission. In this version I have:

Expand Down
Loading

0 comments on commit b87bf32

Please sign in to comment.