### Forecasting with Moirai-MoE-1.0-R Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-moe-1/README.md This example demonstrates how to load a pre-trained Moirai-MoE model, create a predictor, generate forecasts on a test dataset, and visualize the results. Ensure necessary libraries like matplotlib and gluonts are installed. ```python import matplotlib.pyplot as plt from gluonts.dataset.repository import dataset_recipes from uni2ts.eval_util.data import get_gluonts_test_dataset from uni2ts.eval_util.plot import plot_next_multi from uni2ts.model.moirai_moe import MoiraiMoEForecast, MoiraiMoEModule SIZE = "small" # model size: choose from {'small', 'base'} CTX = 1000 # context length: any positive integer BSZ = 32 # batch size: any positive integer # Load dataset test_data, metadata = get_gluonts_test_dataset( "electricity", prediction_length=None, regenerate=False ) # Uncomment the below line to find other datasets # print(sorted(dataset_recipes.keys())) # Prepare model model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained( f"Salesforce/moirai-moe-1.0-R-{SIZE}", ), prediction_length=metadata.prediction_length, context_length=CTX, patch_size=16, num_samples=100, target_dim=metadata.target_dim, feat_dynamic_real_dim=metadata.feat_dynamic_real_dim, past_feat_dynamic_real_dim=metadata.past_feat_dynamic_real_dim, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) # Visualize forecasts fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(25, 10)) plot_next_multi( axes, input_it, label_it, forecast_it, context_length=200, intervals=(0.5, 0.9), dim=None, name="pred", show_label=True, ) ``` -------------------------------- ### Clone and Install uni2ts in Development Mode Source: https://github.com/salesforceairesearch/uni2ts/blob/main/CONTRIBUTING.md Clone the repository and install the library with development dependencies. This sets up the project for local development and testing. ```shell git clone https://github.com/your-username-here/uni2ts.git cd uni2ts pip install -e '.[dev]' ``` -------------------------------- ### Build Uni2TS from Source Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Install Uni2TS and its notebook dependencies by building from the source code. ```shell pip install -e '.[notebook]' ``` -------------------------------- ### Install Uni2TS via PyPI Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Install the Uni2TS library directly from the Python Package Index. ```shell pip install uni2ts ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/salesforceairesearch/uni2ts/blob/main/CONTRIBUTING.md Install the pre-commit hooks to automatically format code using Black and isort. Ensure code quality and consistency before committing. ```shell pre-commit install ``` -------------------------------- ### Install Dependencies Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/gift_eval/README.md Installs the necessary Python packages from the requirements file. Ensure you have Python 3.12 or newer. ```sh pip install -r requirements.txt ``` -------------------------------- ### Start Moirai Agent Tools Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/ctx_forecast/README.md Launches the necessary tools for the Moirai Agent, including a Python sandbox and a timeseries foundation model. ```bash bash run_tools.sh ``` -------------------------------- ### Download VN1 Competition Data Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/vn1_competition/README.md Use this make command to download the raw dataset required for the competition. Ensure you have Make installed. ```bash make download_data ``` -------------------------------- ### Install VisionTS Package Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/benchmarks/README.md Install the VisionTS Python package using pip. This is a prerequisite before testing or running VisionTS. ```bash pip install visionts ``` -------------------------------- ### Prepare Input and Label Data Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Prepares input and label dictionaries for time series forecasting. The input contains the target values for the context window and the start date, while the label contains the target values for the prediction window and its start date. ```python # create a sample for the hourly data, using one week data as context window and predicting the next two days. inp = { "target": df["target"].to_numpy()[:168], # 168 = 24 * 7 "start": df.index[0].to_period(freq="H"), } label = { "target": df["target"].to_numpy()[168:216], # 48 = 24 * 2 "start": df.index[168].to_period(freq="H"), } ``` -------------------------------- ### Zero-Shot Time Series Forecasting with Uni2TS Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Perform zero-shot time series forecasting using a pre-trained Uni2TS model. This example demonstrates data loading, preparation with GluonTS, model configuration, prediction, and visualization. ```python import torch import matplotlib.pyplot as plt import pandas as pd from gluonts.dataset.pandas import PandasDataset from gluonts.dataset.split import split from huggingface_hub import hf_hub_download from uni2ts.eval_util.plot import plot_single from uni2ts.model.moirai import MoiraiForecast, MoiraiModule from uni2ts.model.moirai_moe import MoiraiMoEForecast, MoiraiMoEModule MODEL = "moirai2" # model name: choose from {'moirai', 'moirai-moe', 'moirai2'} SIZE = "small" # model size: choose from {'small', 'base', 'large'} PDT = 20 # prediction length: any positive integer CTX = 200 # context length: any positive integer PSZ = "auto" # patch size: choose from {"auto", 8, 16, 32, 64, 128} BSZ = 32 # batch size: any positive integer TEST = 100 # test set length: any positive integer # Read data into pandas DataFrame url = ( "https://gist.githubusercontent.com/rsnirwan/c8c8654a98350fadd229b00167174ec4" "/raw/a42101c7786d4bc7695228a0f2c8cea41340e18f/ts_wide.csv" ) df = pd.read_csv(url, index_col=0, parse_dates=True) # Convert into GluonTS dataset ds = PandasDataset(dict(df)) # Split into train/test set train, test_template = split( ds, offset=-TEST ) # assign last TEST time steps as test set # Construct rolling window evaluation test_data = test_template.generate_instances( prediction_length=PDT, # number of time steps for each prediction windows=TEST // PDT, # number of windows in rolling window evaluation distance=PDT, # number of time steps between each window - distance=PDT for non-overlapping windows ) # Prepare pre-trained model by downloading model weights from huggingface hub if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-{SIZE}"), prediction_length=PDT, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-{SIZE}"), prediction_length=PDT, context_length=CTX, patch_size=16, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.from_pretrained( f"Salesforce/moirai-2.0-R-small", ), prediction_length=100, context_length=1680, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) inp = next(input_it) label = next(label_it) forecast = next(forecast_it) plot_single( inp, label, forecast, context_length=200, name="pred", show_label=True, ) plt.show() ``` -------------------------------- ### Create and Save Dataset with Multiprocessing Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Generates a Hugging Face Dataset using a sharded generator function and multiprocessing for faster data preparation. The `gen_kwargs` parameter is used to pass the list of examples to the sharded generator, and `num_proc` specifies the number of worker processes. ```python hf_dataset = datasets.Dataset.from_generator( sharded_example_gen_func, features=features, gen_kwargs={"examples": [i for i in range(len(df.columns))]}, num_proc=2, ) hf_dataset.save_to_disk(Path("example_dataset_2")) ``` -------------------------------- ### Inspect Time Series Features Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Examines the keys (features) available when indexing into a time series from each dataset. Confirms the presence of 'target', 'start', 'freq', and 'item_id'. ```python ds1[0].keys(), ds2[0].keys(), ds_multi[0].keys() ``` -------------------------------- ### Get Dataset Lengths Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Retrieves the number of time series in each loaded dataset. Used to verify the expected count of univariate and multivariate series. ```python len(ds1), len(ds2), len(ds_multi) ``` -------------------------------- ### Create PandasDataset with Dynamic Features Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Creates a PandasDataset object from a long-format DataFrame, specifying which features are past dynamic real and which are future dynamic real. This setup is crucial for Moirai's ability to handle time-varying covariates. ```python ds = PandasDataset.from_long_dataframe( df, item_id="item_id", past_feat_dynamic_real=["past_dynamic_real_2"], feat_dynamic_real=["dynamic_real_1"], ) # Split into train/test set train, test_template = split( ds, offset=-TEST ) # assign last TEST time steps as test set # Construct rolling window evaluation test_data = test_template.generate_instances( prediction_length=PDT, # number of time steps for each prediction windows=TEST // PDT, # number of windows in rolling window evaluation distance=PDT, # number of time steps between each window - distance=PDT for non-overlapping windows ) ``` -------------------------------- ### Download LOTSA Dataset and Configure Environment Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Use these commands to download the LOTSA dataset and set the environment variable for its path. Ensure you have a .env file in your project directory. ```shell huggingface-cli download Salesforce/lotsa_data --repo-type=dataset --local-dir PATH_TO_SAVE echo "LOTSA_V1_PATH=PATH_TO_SAVE" >> .env ``` -------------------------------- ### Create Virtual Environment Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Create and activate a Python virtual environment for project dependencies. ```shell virtualenv venv . venv/bin/activate ``` -------------------------------- ### Process ProEnFo Datasets Source: https://github.com/salesforceairesearch/uni2ts/blob/main/src/uni2ts/data/builder/lotsa_v1/README.md Run this command to process the ProEnFo datasets using the uni2ts builder. Ensure the ProEnFo datasets are downloaded and the path is correctly set in .env. ```bash python -m uni2ts.builder.lotsa_v1 proenfo ``` -------------------------------- ### Generate Dataset Image Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/ctx_forecast/README.md Prepares the dataset by plotting historical data and saving it as an image file. This step is optional but recommended for maximizing agent performance. ```bash python gen_image.py --in_file gift_ctx.parquet --out_file gift_ctx_image.parquet --img_root img ``` -------------------------------- ### Sharded Generator Function for Multiprocessing Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Implements a sharded generator function that can be used with multiprocessing. It accepts a list of example indices to process, allowing the `datasets` library to distribute the workload efficiently across multiple processes. ```python def sharded_example_gen_func(examples: list[int]) -> Generator[dict[str, Any]]: for i in examples: yield { "target": df.iloc[:, i].to_numpy(), "start": df.index[0], "freq": pd.infer_freq(df.index), "item_id": f"item_{i}", } ``` -------------------------------- ### Create .env File Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Create an empty .env file for environment variables. ```shell touch .env ``` -------------------------------- ### Run Pre-training Job Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Execute the pre-training script with specified configuration. Customize parameters like run_name, model, and data source as needed. ```shell python -m cli.train \ -cp conf/pretrain \ run_name=first_run \ model=moirai_small \ data=lotsa_v1_unweighted ``` -------------------------------- ### Initialize Moirai or Moirai-MoE Model Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Prepare and initialize the Moirai or Moirai-MoE forecasting model. Ensure necessary parameters like prediction length, context length, and target dimensions are correctly set. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-small"), prediction_length=PDT, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=len(ds), feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-small"), prediction_length=PDT, context_length=CTX, patch_size=16, num_samples=100, target_dim=len(ds), feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) ``` -------------------------------- ### Generator Function for Individual Time Series Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Creates a generator that yields individual time series from a DataFrame. Each yielded item includes the target series, start timestamp, frequency, and an item ID. This is suitable for smaller datasets or when multiprocessing is not required. ```python def example_gen_func() -> Generator[dict[str, Any]]: for i in range(len(df.columns)): yield { "target": df.iloc[:, i].to_numpy(), # array of shape (time,) "start": df.index[0], "freq": pd.infer_freq(df.index), "item_id": f"item_{i}", } ``` -------------------------------- ### Prepare Moirai Forecast Model Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Initializes a Moirai forecast model based on the specified MODEL configuration ('moirai', 'moirai-moe', or 'moirai2'). Requires pre-trained modules and configuration parameters like prediction length and context length. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-small"), prediction_length=PDT, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-small"), prediction_length=PDT, context_length=CTX, patch_size=16, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.from_pretrained( f"Salesforce/moirai-2.0-R-small", ), prediction_length=100, context_length=1680, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) ``` -------------------------------- ### Register ProEnFo Path Source: https://github.com/salesforceairesearch/uni2ts/blob/main/src/uni2ts/data/builder/lotsa_v1/README.md Add the PROENFO_PATH environment variable to your .env file to specify the location of the ProEnFo datasets. ```bash echo "PROENFO_PATH=ADD_YOUR_PATH" >> .env ``` -------------------------------- ### Prepare Moirai Model Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Initializes a Moirai forecasting model based on the specified model type (e.g., 'moirai', 'moirai-moe', 'moirai2'). Requires pre-trained modules and configuration parameters like prediction length and context length. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-small"), prediction_length=PDT, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-small"), prediction_length=PDT, context_length=CTX, patch_size=16, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.from_pretrained( f"Salesforce/moirai-2.0-R-small", ), prediction_length=100, context_length=1680, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) ``` -------------------------------- ### LOTSA v1 Dataset Builder Configuration Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/lotsa_v1_weighting.ipynb This snippet demonstrates the general structure for configuring LOTSA v1 dataset builders. It iterates through a list of builder classes, printing their target, dataset list retrieval, and weight map configuration. ```python for builder_cls in [ Buildings900KDatasetBuilder, BuildingsBenchDatasetBuilder, CloudOpsTSFDatasetBuilder, CMIP6DatasetBuilder, ERA5DatasetBuilder, GluonTSDatasetBuilder, LargeSTDatasetBuilder, LibCityDatasetBuilder, OthersLOTSADatasetBuilder, ProEnFoDatasetBuilder, SubseasonalDatasetBuilder, ]: print(f"- _target_: uni2ts.data.builder.lotsa_v1.{builder_cls.__name__}") print(" datasets: ${cls_getattr:${._target_},dataset_list}") print(" weight_map:") for dataset in builder_cls.dataset_list: print(f" {dataset}: {reweights[dataset]}") print(" sample_time_series:") print(" _target_: uni2ts.data.dataset.SampleTimeSeriesType") print(' _args_: ["proportional"]') ``` -------------------------------- ### Initialize Moirai Model with Dynamic Feature Dimensions Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Initializes either the Moirai or Moirai-MoE model, configuring it with the correct dimensions for dynamic real features based on the created dataset. Ensure model parameters like prediction_length, context_length, and patch_size are set appropriately. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-small"), prediction_length=PDT, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-small"), prediction_length=PDT, context_length=CTX, patch_size=16, num_samples=100, target_dim=1, feat_dynamic_real_dim=ds.num_feat_dynamic_real, past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) ``` -------------------------------- ### Run Moirai Agent for Evaluation Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/ctx_forecast/README.md Executes the Moirai Agent with default settings to replicate reported results on the GIFT-CTX benchmark. Minor differences in results may occur due to LLM non-determinism. ```bash bash run.sh ``` -------------------------------- ### Initialize Moirai Forecasting Model Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Initializes the Moirai forecasting model based on the specified MODEL configuration ('moirai', 'moirai-moe', or 'moirai2'). Sets prediction length, context length, patch size, number of samples, and target dimensions. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.1-R-small"), prediction_length=48, context_length=168, patch_size=32, num_samples=100, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained(f"Salesforce/moirai-moe-1.0-R-small"), prediction_length=48, context_length=168, patch_size=16, num_samples=100, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.from_pretrained( f"Salesforce/moirai-2.0-R-small", ), prediction_length=100, context_length=1680, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) # Time series values. Shape: (batch, time, variate) past_target = rearrange( torch.as_tensor(inp["target"], dtype=torch.float32), "t -> 1 t 1" ) # 1s if the value is observed, 0s otherwise. Shape: (batch, time, variate) past_observed_target = torch.ones_like(past_target, dtype=torch.bool) # 1s if the value is padding, 0s otherwise. Shape: (batch, time) past_is_pad = torch.zeros_like(past_target, dtype=torch.bool).squeeze(-1) if MODEL in ["moirai", "moirai-moe"]: forecast = model( past_target=past_target, past_observed_target=past_observed_target, past_is_pad=past_is_pad, ) elif MODEL == "moirai2": forecast = model.predict(past_target) ``` -------------------------------- ### Configure Custom Data Path Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/vn1_competition/README.md After downloading and preparing the raw dataset, add the path to the processed dataset to your .env file. Replace PATH_TO_SAVE with the actual directory path. ```bash echo "CUSTOM_DATA_PATH=PATH_TO_SAVE" >> .env ``` -------------------------------- ### Load and Prepare Data with Dynamic Features Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Loads a time series dataset from a URL and adds new dynamic real features. Ensure pandas and numpy are imported. ```python # Load dataframe url = ( "https://gist.githubusercontent.com/rsnirwan/a8b424085c9f44ef2598da74ce43e7a3" "/raw/b6fdef21fe1f654787fa0493846c546b7f9c4df2/ts_long.csv" ) df = pd.read_csv(url, index_col=0, parse_dates=True) T = df.shape[0] df["dynamic_real_1"] = np.random.normal(size=T) df["past_dynamic_real_2"] = np.random.normal(size=T) ``` -------------------------------- ### Run Moirai Agent with Custom Arguments Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/ctx_forecast/README.md Runs the Moirai Agent with customizable input path, configuration path, output directory, input mode (text only or text with image), and parallelism. ```bash bash run.sh [your_parquet_path] [your_config_path] [output_dir] [input_mode] [#jobs] ``` -------------------------------- ### Process Dataset with Simple Builder Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Processes a dataset into the Uni2TS format using the simple builder script. Specify the dataset type (wide, long, wide_multivariate). ```shell python -m uni2ts.data.builder.simple ETTh1 dataset/ETT-small/ETTh1.csv --dataset_type wide ``` -------------------------------- ### Fine-tune Model with Hydra Configuration Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Runs the fine-tuning script using Hydra configurations for training and validation data. Specifies model and data parameters like patch size, context length, and prediction length. ```shell python -m cli.train \ -cp conf/finetune \ exp_name=example_lsf \ run_name=example_run \ model=moirai_1.0_R_small \ model.patch_size=32 \ model.context_length=1000 \ model.prediction_length=96 \ data=etth1 \ data.patch_size=32 \ data.context_length=1000 \ data.prediction_length=96 \ data.mode=S \ val_data=etth1 \ val_data.patch_size=32 \ val_data.context_length=1000 \ val_data.prediction_length=96 \ val_data.mode=S ``` -------------------------------- ### Fine-tune Moirai-base Model Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/vn1_competition/README.md Run the training script to fine-tune the Moirai-base model. Ensure the pretrained_model_name_or_path variable in the configuration file is updated with your model's path. ```bash python -m cli.train -cp ../project/vn1_competition/fine_tune run_name=run1 ``` -------------------------------- ### Load Datasets with ArrowTableIndexer Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Loads datasets from disk using ArrowTableIndexer and sets the format to numpy. Ensure the dataset paths are correct. ```python # Load datasets with ArrowTableIndexer ds1 = datasets.load_from_disk("example_dataset_1").with_format("numpy") ds2 = datasets.load_from_disk("example_dataset_2").with_format("numpy") ds_multi = datasets.load_from_disk("example_dataset_multi").with_format("numpy") ``` -------------------------------- ### Run Gift-Eval Replication Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/gift_eval/README.md Executes the evaluation script to generate results on the Gift-Eval dataset. Metrics are saved to a CSV file in the specified output directory. For large datasets, consider parallelization or vLLM for faster inference. ```sh python eval.py --out_dir results --out_name all_results.csv ``` -------------------------------- ### Initialize Moirai Model Variants Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast.ipynb Initializes different Moirai model variants (Moirai, MoiraiMoE, Moirai2) based on the MODEL configuration. It sets up the model with specified parameters like prediction length, context length, and dimensions. ```python # Prepare model if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.from_pretrained( f"Salesforce/moirai-1.1-R-{SIZE}", ), prediction_length=metadata.prediction_length, context_length=CTX, patch_size=PSZ, num_samples=100, target_dim=metadata.target_dim, feat_dynamic_real_dim=metadata.feat_dynamic_real_dim, past_feat_dynamic_real_dim=metadata.past_feat_dynamic_real_dim, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.from_pretrained( f"Salesforce/moirai-moe-1.0-R-{SIZE}", ), prediction_length=metadata.prediction_length, context_length=CTX, patch_size=16, num_samples=100, target_dim=metadata.target_dim, feat_dynamic_real_dim=metadata.feat_dynamic_real_dim, past_feat_dynamic_real_dim=metadata.past_feat_dynamic_real_dim, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.from_pretrained( f"Salesforce/moirai-2.0-R-small", ), prediction_length=100, context_length=1680, target_dim=1, feat_dynamic_real_dim=0, past_feat_dynamic_real_dim=0, ) predictor = model.create_predictor(batch_size=BSZ) forecasts = predictor.predict(test_data.input) input_it = iter(test_data.input) label_it = iter(test_data.label) forecast_it = iter(forecasts) ``` -------------------------------- ### Create and Save Hugging Face Dataset Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Creates a Hugging Face Dataset from the generator function and saves it to disk. This prepares the data for subsequent pre-training or fine-tuning. ```python hf_dataset = datasets.Dataset.from_generator( multivar_example_gen_func, features=features ) hf_dataset.save_to_disk("example_dataset_multi") ``` -------------------------------- ### Import Libraries for Moirai Forecasting Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast.ipynb Imports necessary libraries for Moirai forecasting, including utilities for data loading, plotting, and the Moirai model implementations. ```python %load_ext autoreload %autoreload 2 import matplotlib.pyplot as plt from gluonts.dataset.repository import dataset_recipes from uni2ts.eval_util.data import get_gluonts_test_dataset from uni2ts.eval_util.plot import plot_next_multi from uni2ts.model.moirai import MoiraiForecast, MoiraiModule from uni2ts.model.moirai_moe import MoiraiMoEForecast, MoiraiMoEModule from uni2ts.model.moirai2 import Moirai2Forecast, Moirai2Module ``` -------------------------------- ### Import necessary libraries Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/lotsa_v1_weighting.ipynb Imports required libraries for dataset manipulation, numerical operations, and specific LOTSA v1 dataset builders. ```python import datasets import pyarrow.compute as pc import numpy as np from uni2ts.common.env import env from uni2ts.data.builder.lotsa_v1 import ( Buildings900KDatasetBuilder, BuildingsBenchDatasetBuilder, CloudOpsTSFDatasetBuilder, CMIP6DatasetBuilder, ERA5DatasetBuilder, GluonTSDatasetBuilder, LargeSTDatasetBuilder, LibCityDatasetBuilder, OthersLOTSADatasetBuilder, ProEnFoDatasetBuilder, SubseasonalDatasetBuilder, ) ``` -------------------------------- ### Export OpenAI API Key Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/moirai-agent/ctx_forecast/README.md Sets the OPENAI_API_KEY environment variable. Ensure you replace '...' with your actual key. ```bash export OPENAI_API_KEY="..." ``` -------------------------------- ### List Available Datasets Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast.ipynb Retrieves and sorts a list of all available datasets from the GluonTS dataset recipes. ```python # List of available datasets: sorted(dataset_recipes.keys()) ``` -------------------------------- ### Clone Uni2TS Repository Source: https://github.com/salesforceairesearch/uni2ts/blob/main/README.md Clone the Uni2TS repository from GitHub and navigate into the project directory. ```shell git clone https://github.com/SalesforceAIResearch/uni2ts.git cd uni2ts ``` -------------------------------- ### Run VisionTS on PF Datasets Source: https://github.com/salesforceairesearch/uni2ts/blob/main/project/benchmarks/README.md Execute the VisionTS model on datasets for Probabilistic Forecasting (PF) using the provided bash script. ```bash bash visionts_scripts/pf_visionts.sh ``` -------------------------------- ### Loading Dataset Progress Indicator Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/lotsa_v1_weighting.ipynb This snippet shows a progress indicator for loading a dataset. It is repeated multiple times in the source. ```text Result: Loading dataset from disk: 0%| | 0/96 [00:00> .env ``` -------------------------------- ### Generate a Single Prediction Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Fetch the next input, label, and forecast from their respective iterators to process a single prediction. ```python # Make predictions inp = next(input_it) label = next(label_it) forecast = next(forecast_it) ``` -------------------------------- ### Define Features for Multivariate Dataset Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Specifies the schema for the Hugging Face Dataset, defining the structure for multivariate time series data. Note that the target is a sequence of sequences, representing (variables, time). ```python features = Features( dict( target=Sequence( Sequence(Value("float32")), length=len(df.columns) ), # multivariate time series are saved as (var, time) start=Value("timestamp[s]"), freq=Value("string"), item_id=Value("string"), ) ) ``` -------------------------------- ### Convert DataFrame to Multivariate GluonTS Dataset and Split Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Converts a Pandas DataFrame into a multivariate GluonTS dataset using a grouper, then splits it into training and testing sets. This is suitable for forecasting multiple related time series simultaneously. ```python # Convert into GluonTS dataset ds = PandasDataset(dict(df)) # Group time series into multivariate dataset grouper = MultivariateGrouper(len(ds)) multivar_ds = grouper(ds) # Split into train/test set train, test_template = split( multivar_ds, offset=-TEST ) # assign last TEST time steps as test set # Construct rolling window evaluation test_data = test_template.generate_instances( prediction_length=PDT, # number of time steps for each prediction windows=TEST // PDT, # number of windows in rolling window evaluation distance=PDT, # number of time steps between each window - distance=PDT for non-overlapping windows ) ``` -------------------------------- ### Visualize Forecasts for Multiple Dimensions Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/moirai_forecast_pandas.ipynb Visualize the input, label, and forecast for different dimensions of the time series data. This uses the `plot_single` function to display predictions with confidence intervals. ```python # Visualize different dimensions fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(25, 10)) for i, ax in enumerate(axes.flatten()): plot_single( inp, label, forecast, context_length=200, intervals=(0.5, 0.9), dim=i, ax=ax, name="pred", show_label=True, ) ``` -------------------------------- ### Aggregate dataset lists Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/lotsa_v1_weighting.ipynb Concatenates the dataset lists from various LOTSA v1 dataset builders into a single list. This is a prerequisite for processing all available datasets. ```python dataset_list = ( Buildings900KDatasetBuilder.dataset_list + BuildingsBenchDatasetBuilder.dataset_list + CloudOpsTSFDatasetBuilder.dataset_list + CMIP6DatasetBuilder.dataset_list + ERA5DatasetBuilder.dataset_list + GluonTSDatasetBuilder.dataset_list + LargeSTDatasetBuilder.dataset_list + LibCityDatasetBuilder.dataset_list + OthersLOTSADatasetBuilder.dataset_list + ProEnFoDatasetBuilder.dataset_list + SubseasonalDatasetBuilder.dataset_list ) ``` -------------------------------- ### Define Dataset Features Source: https://github.com/salesforceairesearch/uni2ts/blob/main/example/prepare_data.ipynb Defines the schema for the Hugging Face Dataset. This ensures that data types like sequences of floats, timestamps, and strings are correctly interpreted and stored. ```python features = Features( dict( target=Sequence(Value("float32")), start=Value("timestamp[s]"), freq=Value("string"), item_id=Value("string"), ) ) ```