code: def add_duration_feature(
      ts: TimeSeries,
      log_scale: Optional[float] = None,
      normalized: bool = True,
  ) -> TimeSeries:
      freq = pd.infer_freq(ts.time_index)
      if freq is None:
          age = np.arange(len(ts), dtype=np.float)
      else:
          age = ts.time_index - ts.time_index[0]
          if freq == "H":
              age = age.seconds.values // 3600
          elif freq == "D":
              age = age.days.values
          else:
              raise NotImplementedError
      if log_scale is not None:
          age = np.log1p(age) / np.log(log_scale)
      if normalized:
          age = age / np.max(age) - 0.5
      ts.add_features(
          "age",
          age.reshape(-1, 1),
          data_type="numerical",
          feature_type="revealed",
      )
      return ts
  
imports: from typing import Optional
  from copy import copy, deepcopy
  import numpy
  import pandas
  from .timeseries import TimeSeries
label_desc: Use image node_id 2 for calling a global function with example usage: add_duration_feature(ts, log_scale, normalized) and returns: ts