Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored into a python package #457

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __pycache__/
*.so

# Distribution / packaging
_version.py
.Python
build/
develop-eggs/
Expand Down
47 changes: 47 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[build-system]
requires = ["setuptools", "setuptools_scm[toml]>7.0"]
build-backend = "setuptools.build_meta"

[project]
name = "timeserieslib"
description = "A library for time series analysis"
readme = "README.md"
requires-python = ">=3.8"
authors = [

]
keywords = ["regression", "python", "sklearn"]
license = {text = "BSD3 License"} #??? unclear
classifiers = [
"Programming Language :: Python :: 3",
]
dependencies = [
"einops>=0.4.0",
"matplotlib>=3.7.0",
"numpy>=1.23.5",
"pandas>=1.5.3",
"patool>=1.12",
"reformer-pytorch>=1.4.4",
"scikit-learn>=1.2.2",
"scipy>=1.10.1",
"sktime>=0.16.1",
"sympy>=1.11.1",
"torch>=1.7.1",
"tqdm>=4.64.1,"
]
dynamic = ["version"]

[tool.setuptools]
# packages = ["timeserieslib"]
include-package-data = true

[tool.setuptools.packages.find]
where = ["src"]
include = ["vfdt*"]
namespaces = true

[tool.setuptools_scm]
write_to = "src/timeserieslib/_version.py"

[tool.setuptools.dynamic]
version = {attr = "timeserieslib._version.__version__"}
12 changes: 6 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import argparse
import os
import torch
from exp.exp_long_term_forecasting import Exp_Long_Term_Forecast
from exp.exp_imputation import Exp_Imputation
from exp.exp_short_term_forecasting import Exp_Short_Term_Forecast
from exp.exp_anomaly_detection import Exp_Anomaly_Detection
from exp.exp_classification import Exp_Classification
from utils.print_args import print_args
from timeserieslib.exp.exp_long_term_forecasting import Exp_Long_Term_Forecast
from timeserieslib.exp.exp_imputation import Exp_Imputation
from timeserieslib.exp.exp_short_term_forecasting import Exp_Short_Term_Forecast
from timeserieslib.exp.exp_anomaly_detection import Exp_Anomaly_Detection
from timeserieslib.exp.exp_classification import Exp_Classification
from timeserieslib.utils.print_args import print_args
import random
import numpy as np

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/timeserieslib/data_provider/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from data_provider.data_loader import Dataset_ETT_hour, Dataset_ETT_minute, Dataset_Custom, Dataset_M4, PSMSegLoader, \
from timeserieslib.data_provider.data_loader import Dataset_ETT_hour, Dataset_ETT_minute, Dataset_Custom, Dataset_M4, PSMSegLoader, \
MSLSegLoader, SMAPSegLoader, SMDSegLoader, SWATSegLoader, UEAloader
from data_provider.uea import collate_fn
from timeserieslib.data_provider.uea import collate_fn
from torch.utils.data import DataLoader

data_dict = {
Expand Down Expand Up @@ -31,7 +31,7 @@ def data_provider(args, flag):
if args.task_name == 'anomaly_detection':
drop_last = False
data_set = Data(
args = args,
args=args,
root_path=args.root_path,
win_size=args.seq_len,
flag=flag,
Expand All @@ -47,7 +47,7 @@ def data_provider(args, flag):
elif args.task_name == 'classification':
drop_last = False
data_set = Data(
args = args,
args=args,
root_path=args.root_path,
flag=flag,
)
Expand All @@ -65,7 +65,7 @@ def data_provider(args, flag):
if args.data == 'm4':
drop_last = False
data_set = Data(
args = args,
args=args,
root_path=args.root_path,
data_path=args.data_path,
flag=flag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import StandardScaler
from utils.timefeatures import time_features
from data_provider.m4 import M4Dataset, M4Meta
from data_provider.uea import subsample, interpolate_missing, Normalizer
from timeserieslib.utils.timefeatures import time_features
from timeserieslib.data_provider.m4 import M4Dataset, M4Meta
from timeserieslib.data_provider.uea import subsample, interpolate_missing, Normalizer
from sktime.datasets import load_from_tsfile_to_dataframe
import warnings
from utils.augmentation import run_augmentation_single
from timeserieslib.utils.augmentation import run_augmentation_single

warnings.filterwarnings('ignore')

Expand All @@ -23,7 +23,7 @@ def __init__(self, args, root_path, flag='train', size=None,
# size [seq_len, label_len, pred_len]
self.args = args
# info
if size == None:
if size is None:
self.seq_len = 24 * 4 * 4
self.label_len = 24 * 4
self.pred_len = 24 * 4
Expand Down Expand Up @@ -79,14 +79,14 @@ def __read_data__(self):
data_stamp = df_stamp.drop(['date'], 1).values
elif self.timeenc == 1:
data_stamp = time_features(pd.to_datetime(df_stamp['date'].values), freq=self.freq)
data_stamp = data_stamp.transpose(1, 0)
data_stamp = data_stamp.transpose(1, 0)

self.data_x = data[border1:border2]
self.data_y = data[border1:border2]

if self.set_type == 0 and self.args.augmentation_ratio > 0:
self.data_x, self.data_y, augmentation_tags = run_augmentation_single(self.data_x, self.data_y, self.args)

self.data_stamp = data_stamp

def __getitem__(self, index):
Expand Down Expand Up @@ -116,7 +116,7 @@ def __init__(self, args, root_path, flag='train', size=None,
# size [seq_len, label_len, pred_len]
self.args = args
# info
if size == None:
if size is None:
self.seq_len = 24 * 4 * 4
self.label_len = 24 * 4
self.pred_len = 24 * 4
Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self, args, root_path, flag='train', size=None,
# size [seq_len, label_len, pred_len]
self.args = args
# info
if size == None:
if size is None:
self.seq_len = 24 * 4 * 4
self.label_len = 24 * 4
self.pred_len = 24 * 4
Expand Down Expand Up @@ -359,7 +359,7 @@ def __getitem__(self, index):
insample[-len(insample_window):, 0] = insample_window
insample_mask[-len(insample_window):, 0] = 1.0
outsample_window = sampled_timeseries[
cut_point - self.label_len:min(len(sampled_timeseries), cut_point + self.pred_len)]
cut_point - self.label_len:min(len(sampled_timeseries), cut_point + self.pred_len)]
outsample[:len(outsample_window), 0] = outsample_window
outsample_mask[:len(outsample_window), 0] = 1.0
return insample, outsample, insample_mask, outsample_mask
Expand Down Expand Up @@ -676,7 +676,7 @@ def load_all(self, root_path, file_list=None, flag=None):
data_paths = list(filter(lambda x: re.search(flag, x), data_paths))
input_paths = [p for p in data_paths if os.path.isfile(p) and p.endswith('.ts')]
if len(input_paths) == 0:
pattern='*.ts'
pattern = '*.ts'
raise Exception("No .ts files found using pattern: '{}'".format(pattern))

all_df, labels_df = self.load_single(input_paths[0]) # a single file contains dataset
Expand All @@ -685,7 +685,7 @@ def load_all(self, root_path, file_list=None, flag=None):

def load_single(self, filepath):
df, labels = load_from_tsfile_to_dataframe(filepath, return_separate_X_and_y=True,
replace_missing_vals_with='NaN')
replace_missing_vals_with='NaN')
labels = pd.Series(labels, dtype="category")
self.class_names = labels.cat.categories
labels_df = pd.DataFrame(labels.cat.codes,
Expand Down Expand Up @@ -742,7 +742,7 @@ def __getitem__(self, ind):
batch_x = batch_x.reshape((1 * seq_len, num_columns))

return self.instance_norm(torch.from_numpy(batch_x)), \
torch.from_numpy(labels)
torch.from_numpy(labels)

def __len__(self):
return len(self.all_IDs)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from data_provider.data_factory import data_provider
from exp.exp_basic import Exp_Basic
from utils.tools import EarlyStopping, adjust_learning_rate, adjustment
import numpy as np
import warnings
import time
import os
from torch import optim
import torch.nn as nn
import torch
from timeserieslib.data_provider.data_factory import data_provider
from timeserieslib.exp.exp_basic import Exp_Basic
from timeserieslib.utils.tools import EarlyStopping, adjust_learning_rate, adjustment
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import accuracy_score
import torch.multiprocessing

torch.multiprocessing.set_sharing_strategy('file_system')
import torch
import torch.nn as nn
from torch import optim
import os
import time
import warnings
import numpy as np

warnings.filterwarnings('ignore')

Expand Down
13 changes: 10 additions & 3 deletions exp/exp_basic.py → src/timeserieslib/exp/exp_basic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
import torch
from models import Autoformer, Transformer, TimesNet, Nonstationary_Transformer, DLinear, FEDformer, \
from timeserieslib.models import Autoformer, Transformer, TimesNet, Nonstationary_Transformer, DLinear, FEDformer, \
Informer, LightTS, Reformer, ETSformer, Pyraformer, PatchTST, MICN, Crossformer, FiLM, iTransformer, \
Koopa, TiDE, FreTS, TimeMixer, TSMixer, SegRNN, MambaSimple, Mamba, TemporalFusionTransformer
Koopa, TiDE, FreTS, TimeMixer, TSMixer, SegRNN, MambaSimple, TemporalFusionTransformer
try:
from timeserieslib.models import Mamba # Mamba does not work without cuda
except ModuleNotFoundError:
Mamba = None


class Exp_Basic(object):
Expand All @@ -29,12 +33,15 @@ def __init__(self, args):
'TiDE': TiDE,
'FreTS': FreTS,
'MambaSimple': MambaSimple,
'Mamba': Mamba,
'TimeMixer': TimeMixer,
'TSMixer': TSMixer,
'SegRNN': SegRNN,
'TemporalFusionTransformer': TemporalFusionTransformer
}
if Mamba is not None:
self.model_dict['Mamba'] = Mamba
elif self.args.model == 'Mamba':
raise ModuleNotFoundError('Mamba is not installed')
self.device = self._acquire_device()
self.model = self._build_model().to(self.device)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from data_provider.data_factory import data_provider
from exp.exp_basic import Exp_Basic
from utils.tools import EarlyStopping, adjust_learning_rate, cal_accuracy
from timeserieslib.data_provider.data_factory import data_provider
from timeserieslib.exp.exp_basic import Exp_Basic
from timeserieslib.utils.tools import EarlyStopping, adjust_learning_rate, cal_accuracy
import torch
import torch.nn as nn
from torch import optim
Expand Down Expand Up @@ -183,8 +183,8 @@ def test(self, setting, test=0):
os.makedirs(folder_path)

print('accuracy:{}'.format(accuracy))
file_name='result_classification.txt'
f = open(os.path.join(folder_path,file_name), 'a')
file_name = 'result_classification.txt'
f = open(os.path.join(folder_path, file_name), 'a')
f.write(setting + " \n")
f.write('accuracy:{}'.format(accuracy))
f.write('\n')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from data_provider.data_factory import data_provider
from exp.exp_basic import Exp_Basic
from utils.tools import EarlyStopping, adjust_learning_rate, visual
from utils.metrics import metric
from timeserieslib.data_provider.data_factory import data_provider
from timeserieslib.exp.exp_basic import Exp_Basic
from timeserieslib.utils.tools import EarlyStopping, adjust_learning_rate, visual
from timeserieslib.utils.metrics import metric
import torch
import torch.nn as nn
from torch import optim
Expand Down Expand Up @@ -186,7 +186,7 @@ def test(self, setting, test=0):
f_dim = -1 if self.args.features == 'MS' else 0
outputs = outputs[:, :, f_dim:]

# add support for MS
# add support for MS
batch_x = batch_x[:, :, f_dim:]
mask = mask[:, :, f_dim:]

Expand All @@ -200,7 +200,7 @@ def test(self, setting, test=0):
if i % 20 == 0:
filled = true[0, :, -1].copy()
filled = filled * mask[0, :, -1].detach().cpu().numpy() + \
pred[0, :, -1] * (1 - mask[0, :, -1].detach().cpu().numpy())
pred[0, :, -1] * (1 - mask[0, :, -1].detach().cpu().numpy())
visual(true[0, :, -1], filled, os.path.join(folder_path, str(i) + '.pdf'))

preds = np.concatenate(preds, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from data_provider.data_factory import data_provider
from exp.exp_basic import Exp_Basic
from utils.tools import EarlyStopping, adjust_learning_rate, visual
from utils.metrics import metric
from timeserieslib.data_provider.data_factory import data_provider
from timeserieslib.exp.exp_basic import Exp_Basic
from timeserieslib.utils.tools import EarlyStopping, adjust_learning_rate, visual
from timeserieslib.utils.metrics import metric
import torch
import torch.nn as nn
from torch import optim
import os
import time
import warnings
import numpy as np
from utils.dtw_metric import dtw,accelerated_dtw
from utils.augmentation import run_augmentation,run_augmentation_single
from timeserieslib.utils.dtw_metric import dtw, accelerated_dtw
from timeserieslib.utils.augmentation import run_augmentation, run_augmentation_single

warnings.filterwarnings('ignore')

Expand Down Expand Up @@ -223,7 +223,7 @@ def test(self, setting, test=0):
shape = outputs.shape
outputs = test_data.inverse_transform(outputs.squeeze(0)).reshape(shape)
batch_y = test_data.inverse_transform(batch_y.squeeze(0)).reshape(shape)

outputs = outputs[:, :, f_dim:]
batch_y = batch_y[:, :, f_dim:]

Expand Down Expand Up @@ -252,22 +252,21 @@ def test(self, setting, test=0):
folder_path = './results/' + setting + '/'
if not os.path.exists(folder_path):
os.makedirs(folder_path)

# dtw calculation
if self.args.use_dtw:
dtw_list = []
manhattan_distance = lambda x, y: np.abs(x - y)
def manhattan_distance(x, y): return np.abs(x - y)
for i in range(preds.shape[0]):
x = preds[i].reshape(-1,1)
y = trues[i].reshape(-1,1)
x = preds[i].reshape(-1, 1)
y = trues[i].reshape(-1, 1)
if i % 100 == 0:
print("calculating dtw iter:", i)
d, _, _, _ = accelerated_dtw(x, y, dist=manhattan_distance)
dtw_list.append(d)
dtw = np.array(dtw_list).mean()
else:
dtw = -999


mae, mse, rmse, mape, mspe = metric(preds, trues)
print('mse:{}, mae:{}, dtw:{}'.format(mse, mae, dtw))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from data_provider.data_factory import data_provider
from data_provider.m4 import M4Meta
from exp.exp_basic import Exp_Basic
from utils.tools import EarlyStopping, adjust_learning_rate, visual
from utils.losses import mape_loss, mase_loss, smape_loss
from utils.m4_summary import M4Summary
from timeserieslib.data_provider.data_factory import data_provider
from timeserieslib.data_provider.m4 import M4Meta
from timeserieslib.exp.exp_basic import Exp_Basic
from timeserieslib.utils.tools import EarlyStopping, adjust_learning_rate, visual
from timeserieslib.utils.losses import mape_loss, mase_loss, smape_loss
from timeserieslib.utils.m4_summary import M4Summary
import torch
import torch.nn as nn
from torch import optim
Expand Down
File renamed without changes.
File renamed without changes.
Loading