Skip to content

Commit

Permalink
Merge pull request #25 from Nixtla/v2-version
Browse files Browse the repository at this point in the history
chore: release v0.6.0
  • Loading branch information
MMenchero authored Oct 8, 2024
2 parents e4834b5 + dedb2b0 commit bd3b0cf
Show file tree
Hide file tree
Showing 105 changed files with 151,157 additions and 316,148 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Imports:
ggplot2,
httr2,
lubridate,
purrr,
rlang,
tidyr,
tidyselect,
tsibble
tidyselect
Suggests:
httptest2,
knitr,
Expand Down
19 changes: 6 additions & 13 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(.generate_output_dates)
export(.get_api_key)
export(.get_model_params)
export(.level_from_quantiles)
export(.make_request)
export(.nixtla_client_cross_validation_distributed)
export(.nixtla_client_cross_validation_seq)
export(.nixtla_client_detect_anomalies_distributed)
export(.nixtla_client_detect_anomalies_seq)
export(.nixtla_client_forecast_distributed)
export(.nixtla_client_forecast_seq)
export(.nixtla_client_historic_distributed)
export(.nixtla_client_historic_seq)
export(.nixtla_data_prep)
export(.partition_payload)
export(.transform_output_dates)
export(.r_frequency)
export(.transient_errors)
export(.validate_exogenous)
export(date_conversion)
export(infer_frequency)
export(nixtla_client_cross_validation)
export(nixtla_client_detect_anomalies)
Expand All @@ -25,8 +16,10 @@ export(nixtla_client_historic)
export(nixtla_client_plot)
export(nixtla_set_api_key)
export(nixtla_validate_api_key)
importFrom(dplyr,all_of)
importFrom(dplyr,bind_rows)
importFrom(dplyr,group_by)
importFrom(dplyr,group_split)
importFrom(dplyr,inner_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
Expand Down Expand Up @@ -56,9 +49,9 @@ importFrom(httr2,request)
importFrom(httr2,resp_status)
importFrom(lubridate,ymd)
importFrom(lubridate,ymd_hms)
importFrom(purrr,map2_dfr)
importFrom(rlang,.data)
importFrom(tidyr,pivot_longer)
importFrom(tidyselect,everything)
importFrom(tidyselect,peek_vars)
importFrom(tidyselect,starts_with)
importFrom(tsibble,is_tsibble)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# nixtlar 0.6.0

- Development version. See release notes [here]()

# nixtlar 0.5.4

- Development version. See release notes [here](https://github.com/Nixtla/nixtlar/releases/tag/v0.5.4)
Expand Down
46 changes: 0 additions & 46 deletions R/date_conversion.R

This file was deleted.

49 changes: 49 additions & 0 deletions R/generate_output_dates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' Generate output dates for forecast method.
#' This is a private function of 'nixtlar'
#'
#' @param df_info A data frame that is created by the forecast method with the last dates of every unique id.
#' @param freq The frequency of the data, as a period or offset alias.
#' @param h The forecast horizon.
#'
#' @return A data frame with dates for the forecast.
#' @export
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' dates_df <- .generate_output_dates(df_info, freq, h)
#' }
#'
.generate_output_dates <- function(df_info, freq, h) {
new_dates <- lapply(1:nrow(df_info), function(i) {
start_date <- df_info$dates[i]
r_freq <- .r_frequency(freq)

if(freq == "QE") {
# End of quarter dates are: "YYY-03-31", "YYYY-06-30", "YYYY-09-30" and "YYYY-12-31".
dt <- seq(from = start_date, by = "quarter", length.out = h+1)
month <- lubridate::month(start_date)
if (month %in% c(3, 12)) {
dt <- ifelse(lubridate::month(dt) %in% c(7, 10), dt - lubridate::days(1), dt)
} else {
dt <- ifelse(lubridate::month(dt) %in% c(3, 12), dt + lubridate::days(1), dt)
}
} else if(freq == "ME") {
dt <- seq(from = start_date + lubridate::days(1), by = r_freq, length.out = h+1) - lubridate::days(1)
} else {
dt <- seq(from = start_date, by = r_freq, length.out = h+1)
}

dt[2:length(dt)]
})

dates_df <- data.frame(lapply(new_dates, as.POSIXct))

ids <- df_info$unique_id
if(inherits(df_info$unique_id, "numeric") | inherits(df_info$unique_id, "integer")){
ids <- as.character(df_info$unique_id)
}
names(dates_df) <- ids

return(dates_df)
}
46 changes: 46 additions & 0 deletions R/get_model_params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Retrieve parameters for 'TimeGPT' model
#' This is a private function of 'nixtlar'
#'
#' @param model Model to use, either "timegpt-1" or "timegpt-1-long-horizon".
#' @param freq Frequency of the data.
#'
#' @return A list with the model's input size and horizon
#' @export
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' .get_model_params()
#' }
#'
.get_model_params <- function(model, freq){

payload_params <- list(
model = model,
freq = freq
)

req <- httr2::request("https://api.nixtla.io/model_params") |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload_params) |>
httr2::req_retry(
max_tries = 6,
is_transient = .transient_errors
)

resp <- req |>
httr2::req_perform() |>
httr2::resp_body_json()

model_params <- list(
input_size = resp$detail$input_size,
horizon = resp$detail$horizon
)

return(model_params)
}
88 changes: 51 additions & 37 deletions R/infer_frequency.R
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
#' Infer frequency of a data frame.
#'
#' @param df A data frame with time series data.
#' @param freq The frequency of the data as specified by the user; NULL otherwise.
#'
#' @return The inferred frequency.
#' @export
#'
#' @examples
#' df <- nixtlar::electricity
#' infer_frequency(df)
#' freq <- NULL
#' infer_frequency(df, freq)
#'
infer_frequency <- function(df){
freq <- NULL
dates <- sort(unique(df$ds))

# Check if it's hourly data
nchrs <- lapply(as.character(dates), nchar)
ntable <- sort(table(unlist(nchrs)))
nmode <- ntable[length(ntable)]
nmode <- as.numeric(names(nmode))

if(nmode > 10){
freq <- "H" # We'll assume hourly data
message("Frequency chosen: H")
infer_frequency <- function(df, freq){
if(!is.null(freq)){
return(freq)
}

# If it's not hourly data, check the time differences in days
ddiff <- diff(as.Date(dates))
table <- sort(table(ddiff))
mode <- table[length(table)]
mode <- as.numeric(names(mode))

freq_list = list(
list(alias = "Y", value = c(365,366)),
list(alias = "Q", value = c(91,92)),
list(alias = "MS", value = c(30,31)),
list(alias = "W", value = c(7)),
list(alias = "D", value = c(1))
)

for(i in 1:length(freq_list)){
if(mode %in% freq_list[i][[1]]$value){
freq <- freq_list[i][[1]]$alias
num_chars <- nchar(as.character(df$ds[1]))

if(num_chars <= 10){
# assumes dates in format YYYY-MM-DD
dates <- lubridate::ymd(sort(unique(df$ds)))
dates_diff <- diff(dates)
dates_table <- table(dates_diff)
mode <- as.numeric(names(which.max(dates_table)))

freq_list = list(
list(alias = "Y", value = c(365,366)),
list(alias = "Q", value = c(91,92)),
list(alias = "MS", value = c(30,31)),
list(alias = "W", value = c(7)),
list(alias = "D", value = c(1))
)

for(item in freq_list){
if(mode %in% item$value){
freq <- item$alias
break
}
}

message(paste0("Frequency chosen: ", freq))
return(freq)

}else{
# assumes dates in format YYYY-MM-DD hh:mm:ss
dates <- lubridate::ymd_hms(sort(unique(df$ds)))
dates_diff <- diff(dates)
dates_table <- table(dates_diff)
mode <- as.numeric(names(which.max(dates_table)))

units <- attr(dates_diff, "units")

freq <- switch(
units,
"hours" = ifelse(mode == 1, "h", paste0(mode, "h")),
"mins" = ifelse(mode == 1, "min", paste0(mode, "min")),
"secs" = ifelse(mode == 1, "s", paste0(mode, "s"))
)

message(paste0("Frequency chosen: ", freq))
return(freq)
}

if(is.null(freq)){
freq <- "D"
message("I'm not sure about the frequency of the data. Will default to daily (D). Please provide it if you know it.")
stop("I can't figure out the frequency of the data. Please specify it with the `freq` parameter")
}

message(paste0("Frequency chosen: ", freq))

return(freq)
}

4 changes: 3 additions & 1 deletion R/nixtlaR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom dplyr all_of
#' @importFrom dplyr bind_rows
#' @importFrom dplyr group_by
#' @importFrom dplyr group_split
#' @importFrom dplyr inner_join
#' @importFrom dplyr mutate
#' @importFrom dplyr n
Expand Down Expand Up @@ -33,11 +35,11 @@
#' @importFrom httr2 resp_status
#' @importFrom lubridate ymd
#' @importFrom lubridate ymd_hms
#' @importFrom purrr map2_dfr
#' @importFrom rlang .data
#' @importFrom tidyr pivot_longer
#' @importFrom tidyselect everything
#' @importFrom tidyselect peek_vars
#' @importFrom tidyselect starts_with
#' @importFrom tsibble is_tsibble
## usethis namespace: end
NULL
Loading

0 comments on commit bd3b0cf

Please sign in to comment.