class_docstr: Wrapper to load datasets found in the Numenta Anomaly Benchmark
  (https://github.com/numenta/NAB).
  
  The NAB contains a range of datasets and are categorized by their domains.
class_docstr_tok: ["Wrapper","to","load","datasets","found","in","the","Numenta","Anomaly","Benchmark","(","https",":","\/\/github.com\/numenta\/NAB",")",".","The","NAB","contains","a","range","of","datasets","and","are","categorized","by","their","domains","."]
docstr: :param subset: One of the elements in subsets.
  :param rootdir: The root directory at which the dataset can be found.
docstr_tok: [":","param","subset",":","One","of","the","elements","in","subsets",".",":","param","rootdir",":","The","root","directory","at","which","the","dataset","can","be","found","."]
code: def __init__(self, subset="all", rootdir=None):
      """
      :param subset: One of the elements in subsets.
      :param rootdir: The root directory at which the dataset can be found.
      """
      super().__init__()
      assert (
          subset in self.valid_subsets
      ), f"subset should be in {self.valid_subsets}, but got {subset}"
      self.subset = subset
  
      if rootdir is None:
          fdir = os.path.dirname(os.path.abspath(__file__))
          merlion_root = os.path.abspath(
              os.path.join(fdir, "..", "..", "..")
          )
          rootdir = os.path.join(merlion_root, "data", "nab")
  
      if subset == "artificial":
          subsets = ["artificialNoAnomaly", "artificialWithAnomaly"]
      elif subset == "all":
          subsets = [
              "artificialNoAnomaly",
              "artificialWithAnomaly",
              "realAWSCloudwatch",
              "realAdExchange",
              "realKnownCause",
              "realTraffic",
              "realTweets",
          ]
      else:
          subsets = [subset]
      self.download(rootdir, subsets)
      dsetdirs = [os.path.join(rootdir, s) for s in subsets]
  
      labelfile = os.path.join(rootdir, "labels/combined_windows.json")
      with open(labelfile) as json_file:
          label_list = json.load(json_file)
  
      csvs = sum(
          [sorted(glob.glob(f"{d}/*.csv")) for d in dsetdirs], []
      )
      for i, csv in enumerate(sorted(csvs)):
          df = pd.read_csv(csv)
          df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0])
          df = df.sort_values(by="timestamp")
          if (
              len(
                  df["timestamp"][
                      df["timestamp"].diff() == datetime.timedelta(0)
                  ]
              )
              != 0
          ):
              df = df.drop_duplicates(subset="timestamp", keep="first")
              logger.warning(
                  f"Time series {csv} (index {i}) has timestamp duplicates. Kept first values."
              )
  
          all_dt = np.unique(np.diff(df["timestamp"])).astype(np.int64)
          gcd_dt = all_dt[0]
          for dt in all_dt[1:]:
              gcd_dt = np.gcd(gcd_dt, dt)
          gcd_dt = pd.to_timedelta(gcd_dt)
          labels = self.load_labels(csv, label_list, gcd_dt)
          df["anomaly"] = df["timestamp"].apply(lambda x: x in labels)
          df = df.set_index("timestamp")
  
          # First 15% of data is "probationary", i.e. model can use it to
          # warm-start without being tested. See Figure 2 of the NAB
          # paper https://arxiv.org/pdf/1510.03336.pdf
          n = len(df) * 0.15
          df["trainval"] = pd.Series(
              np.arange(len(df)) <= n, index=df.index
          )
  
          md_cols = ["anomaly", "trainval"]
          self.metadata.append(df[md_cols])
          self.time_series.append(
              df[[c for c in df.columns if c not in md_cols]]
          )
  
file_path: merlion/ts_datasets/ts_datasets/anomaly/nab.py
imports: import datetime
  import glob
  import json
  import logging
  import os
  import re
  import requests
  import numpy
  import pandas
  import tqdm
  from ts_datasets.anomaly.base import TSADBaseDataset
label_desc: Use image node_id 1 to create a new NAB object from inherited base classes: TSADBaseDataset with example: obj = NAB(subset, rootdir)