zhang qiao
Upload folder using huggingface_hub
8cf4695
raw
history blame
2.89 kB
import pandas as pd
from statsmodels.tsa.tsatools import freq_to_period
from statsmodels.tsa.stattools import acf, pacf
from ..idsc import IDSC
import logging
from .visualiser import Visualiser
class Analyser():
def __init__(self) -> None:
logging.debug('Analyser init')
self.idsc = IDSC()
self.viz = Visualiser()
def fit(self, data):
self.data = data
def profiling(self):
# --------------------------------- #
# Profiling data with IDSC profiler #
# --------------------------------- #
self.quantity_change_point = []
self.quantity_predictability = []
self.characteristic = None
self.profile = self.idsc.profiling(
self.data[['y']].rename(columns={'y': 'target'}).dropna().to_json())
# Parse profile result
self.intermittent_change_points_idx = self.profile['change_point_res']['inter_order_cpi']
self.quantity_change_points_idx = self.profile['change_point_res']['order_quantity_cpi']
# Set the change points to datetime index value
self.intermittent_change_points = self.data.iloc[self.intermittent_change_points_idx].index
self.quantity_change_points = self.data.iloc[self.quantity_change_points_idx].index
self.characteristic = self.profile['classification_res']['time_series_class']['overall_characteristic']
self.intermittent_predictability = [
round(r['predictability'], 3)
for r in
self.profile['predictability_res']['predictability_result']['inter_order']
]
self.quantity_predictability = [
round(r['predictability'], 3)
for r in
self.profile['predictability_res']['predictability_result']['order_quantity']
]
# ----- End of [Profiling data with IDSC profiler] ----- #
def analyse(self, data: pd.DataFrame):
'''
data: pd.DataFrame, required
Data must contains datetime index, and the index must be proper datetime index with freq value
data should complete, no missing value and contain at least "y" columns as the time-series data itself
'''
logging.debug('Analysing ...')
self.data = data
self.freq: str = pd.infer_freq(data.index)
self.period = freq_to_period(self.freq)
self.acf = pd.DataFrame(
[acf(data[col]) for col in data.columns],
index=data.columns).T
self.pacf = pd.DataFrame(
[pacf(data[col]) for col in data.columns],
index=data.columns).T
self.viz.fit(data)
return {
'freq': self.freq,
'period': self.period,
'change_points': self.quantity_change_point,
'predictability': self.quantity_predictability,
'characteristic': self.characteristic
}