[docs]@dataclass(init=False,repr=True)# For repr only.classNStepAheadHorizon(Horizon):n_step:intdef__init__(self,n_step:int)->None:super().__init__(horizon_type=HorizonOpts.N_STEP_AHEAD)ifn_step<=0:raiseValueError("N step ahead horizon must be > 0.")self.n_step=n_step
TimeIndexSequence=Sequence[T_TSIndexClass]# TODO: Unit-test.# TODO: This is currently rough, and will change.
[docs]@dataclass(init=False,repr=True)# For repr only.classTimeIndexHorizon(Horizon):time_index_sequence:TimeIndexSequence# TODO: Perhaps also allow for just T_TSIndexClass.def__init__(self,time_index_sequence:TimeIndexSequence)->None:super().__init__(horizon_type=HorizonOpts.TIME_INDEX)fortiintime_index_sequence:ifnotisinstance(ti,T_TSIndexClass_AsTuple):raiseValueError(f"Time index horizon `time_index` must be one of the following types: {T_TSIndexClass_AsTuple}")ifnotissubclass(python_type_from_np_pd_dtype(ti.dtype),T_TSIndexDtype_AsTuple):# type: ignoreraiseValueError(f"Time index horizon `time_index` dtype must be one of: {T_TSIndexDtype_AsTuple}")self.time_index_sequence=time_index_sequence
[docs]@classmethoddeffuture_horizon_from_dataset(cls,data:Dataset,forecast_n_future_steps:int,time_delta:T_TSIndexDtype=1)->"TimeIndexHorizon":targets=data.temporal_targetsiftargetsisNone:raiseValueError("Temporal targets must be set but was None")ifnotisinstance(time_delta,T_NumericDtype_AsTuple):raise_not_implemented(feature=f"{TimeIndexHorizon.__name__} from Dataset initialization when `time_delta` "f"is not one of: {T_NumericDtype_AsTuple}, was {type(time_delta)}")targets_index_dtype=python_type_from_np_pd_dtype(targets.sample_index.dtype)# type: ignoreifnotissubclass(targets_index_dtype,T_NumericDtype_AsTuple):raise_not_implemented(feature=f"{TimeIndexHorizon.__name__} from Dataset initialization when temporal targets index "f"is not one of: {T_NumericDtype_AsTuple}, was {targets_index_dtype}")ifnotisinstance(time_delta,targets_index_dtype):raiseValueError(f"`time_delta` type ({type(time_delta)}) did not match the "f"temporal targets index dtype ({targets_index_dtype})")indices=[]fortsintargets:start=list(ts.time_index)[-1]+time_deltaifTYPE_CHECKING:assertisinstance(time_delta,(int,float))assertisinstance(start,(int,float))seq=[start+time_delta*xforxinrange(forecast_n_future_steps)]indices.append(pd.Index(seq))returncls(time_index_sequence=indices)