Add troubleshooting section to readme file
This commit is contained in:
+41
-43
@@ -12,7 +12,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import numpy as np
|
||||
@@ -22,10 +21,10 @@ import pytest
|
||||
import timesfm
|
||||
|
||||
|
||||
def create_sample_dataframe(
|
||||
start_date: datetime, end_date: datetime, freq: str = "D"
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
def create_sample_dataframe(start_date: datetime,
|
||||
end_date: datetime,
|
||||
freq: str = "D") -> pd.DataFrame:
|
||||
"""
|
||||
Create a sample DataFrame with time series data.
|
||||
|
||||
Args:
|
||||
@@ -36,10 +35,10 @@ def create_sample_dataframe(
|
||||
Returns:
|
||||
pd.DataFrame: DataFrame with columns 'unique_id', 'ds', and 'ts'.
|
||||
"""
|
||||
date_range = pd.date_range(start=start_date, end=end_date, freq=freq)
|
||||
ts_data = np.random.randn(len(date_range))
|
||||
df = pd.DataFrame({"unique_id": "ts-1", "ds": date_range, "ts": ts_data})
|
||||
return df
|
||||
date_range = pd.date_range(start=start_date, end=end_date, freq=freq)
|
||||
ts_data = np.random.randn(len(date_range))
|
||||
df = pd.DataFrame({"unique_id": "ts-1", "ds": date_range, "ts": ts_data})
|
||||
return df
|
||||
|
||||
|
||||
@pytest.mark.parametrize("context_length", [128, 256, 512])
|
||||
@@ -50,42 +49,41 @@ def test_timesfm_forecast_on_df(
|
||||
prediction_length: int,
|
||||
freq: str,
|
||||
) -> None:
|
||||
model = timesfm.TimesFm(
|
||||
context_len=context_length,
|
||||
horizon_len=prediction_length,
|
||||
input_patch_len=32,
|
||||
output_patch_len=128,
|
||||
num_layers=20,
|
||||
model_dims=1280,
|
||||
backend="cpu",
|
||||
)
|
||||
model.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")
|
||||
model = timesfm.TimesFm(
|
||||
context_len=context_length,
|
||||
horizon_len=prediction_length,
|
||||
input_patch_len=32,
|
||||
output_patch_len=128,
|
||||
num_layers=20,
|
||||
model_dims=1280,
|
||||
backend="cpu",
|
||||
)
|
||||
model.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")
|
||||
|
||||
end_date = datetime.now()
|
||||
start_date = end_date - timedelta(days=context_length)
|
||||
input_df = create_sample_dataframe(start_date, end_date, freq)
|
||||
end_date = datetime.now()
|
||||
start_date = end_date - timedelta(days=context_length)
|
||||
input_df = create_sample_dataframe(start_date, end_date, freq)
|
||||
|
||||
forecast_df = model.forecast_on_df(
|
||||
inputs=input_df,
|
||||
freq=freq,
|
||||
value_name="ts",
|
||||
num_jobs=-1,
|
||||
)
|
||||
forecast_df = model.forecast_on_df(
|
||||
inputs=input_df,
|
||||
freq=freq,
|
||||
value_name="ts",
|
||||
num_jobs=-1,
|
||||
)
|
||||
|
||||
assert (
|
||||
len(forecast_df) == prediction_length
|
||||
), f"Expected forecast length of {prediction_length}, but got {len(forecast_df)}"
|
||||
assert (
|
||||
"timesfm" in forecast_df.columns
|
||||
), "Forecast DataFrame should contain 'timesfm' column"
|
||||
assert (
|
||||
len(forecast_df) == prediction_length
|
||||
), f"Expected forecast length of {prediction_length}, but got {len(forecast_df)}"
|
||||
assert ("timesfm" in forecast_df.columns
|
||||
), "Forecast DataFrame should contain 'timesfm' column"
|
||||
|
||||
last_input_date = input_df["ds"].max()
|
||||
first_forecast_date = forecast_df["ds"].min()
|
||||
expected_first_forecast_date = last_input_date + pd.Timedelta(1, unit=freq)
|
||||
assert (
|
||||
first_forecast_date == expected_first_forecast_date
|
||||
), f"Forecast should start from {expected_first_forecast_date}, but starts from {first_forecast_date}"
|
||||
last_input_date = input_df["ds"].max()
|
||||
first_forecast_date = forecast_df["ds"].min()
|
||||
expected_first_forecast_date = last_input_date + pd.Timedelta(1, unit=freq)
|
||||
assert (
|
||||
first_forecast_date == expected_first_forecast_date
|
||||
), f"Forecast should start from {expected_first_forecast_date}, but starts from {first_forecast_date}"
|
||||
|
||||
print(
|
||||
f"Successful forecast with context_length={context_length}, prediction_length={prediction_length}, freq={freq}"
|
||||
)
|
||||
print(
|
||||
f"Successful forecast with context_length={context_length}, prediction_length={prediction_length}, freq={freq}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user