- Issue #3 - Code documentation with PEP 257 compliance

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-05-05 15:33:27 -03:00
parent 9da53a4845
commit 3365fa72f1
18 changed files with 128 additions and 3 deletions

View File

@ -1,6 +1,8 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
"""Residual Analysis methods"""
import numpy as np
import pandas as pd
import matplotlib as plt
@ -11,15 +13,28 @@ from scipy import stats
def residuals(targets, forecasts, order=1):
"""First order residuals"""
return np.array(targets[order:]) - np.array(forecasts[:-1])
def ChiSquared(q,h):
"""
Chi-Squared value
:param q:
:param h:
:return:
"""
p = stats.chi2.sf(q, h)
return p
def compareResiduals(data, models):
"""
Compare residual's statistics of several models
:param data:
:param models:
:return:
"""
ret = "Model & Order & Mean & STD & Box-Pierce & Box-Ljung & P-value \\\\ \n"
for mfts in models:
forecasts = mfts.forecast(data)
@ -40,7 +55,15 @@ def compareResiduals(data, models):
def plotResiduals(targets, models, tam=[8, 8], save=False, file=None):
"""
Plot residuals and statistics
:param targets:
:param models:
:param tam:
:param save:
:param file:
:return:
"""
fig, axes = plt.subplots(nrows=len(models), ncols=3, figsize=tam)
c = 0
for mfts in models:

View File

@ -1,3 +1,7 @@
"""
Benchmark utility functions
"""
import numpy as np
import pandas as pd
from copy import deepcopy
@ -5,6 +9,19 @@ from pyFTS.common import Util
def save_dataframe_point(experiments, file, objs, rmse, save, sintetic, smape, times, u):
"""
Create a dataframe to store the benchmark results
:param experiments: dictionary with the execution results
:param file:
:param objs:
:param rmse:
:param save:
:param sintetic:
:param smape:
:param times:
:param u:
:return:
"""
ret = []
if sintetic:

View File

@ -23,6 +23,17 @@ from pyFTS.benchmarks import benchmarks, parallel_benchmarks, Util as bUtil
def run_point(mfts, partitioner, train_data, test_data, window_key=None, transformation=None, indexer=None):
"""
Point forecast benchmark function to be executed on cluster nodes
:param mfts: FTS model
:param partitioner: Universe of Discourse partitioner
:param train_data: data used to train the model
:param test_data: ata used to test the model
:param window_key: id of the sliding window
:param transformation: data transformation
:param indexer: seasonal indexer
:return: a dictionary with the benchmark results
"""
import time
from pyFTS import yu,chen,hofts,ifts,pwfts,ismailefendi,sadaei
from pyFTS.partitioners import Grid, Entropy, FCM
@ -59,6 +70,25 @@ def run_point(mfts, partitioner, train_data, test_data, window_key=None, transfo
def point_sliding_window(data, windowsize, train=0.8, models=None, partitioners=[Grid.GridPartitioner],
partitions=[10], max_order=3, transformation=None, indexer=None, dump=False,
save=False, file=None, sintetic=False,nodes=None, depends=None):
"""
Distributed sliding window benchmarks for FTS point forecasters
:param data:
:param windowsize: size of sliding window
:param train: percentual of sliding window data used to train the models
:param models: FTS point forecasters
:param partitioners: Universe of Discourse partitioner
:param partitions: the max number of partitions on the Universe of Discourse
:param max_order: the max order of the models (for high order models)
:param transformation: data transformation
:param indexer: seasonal indexer
:param dump:
:param save: save results
:param file: file path to save the results
:param sintetic: if true only the average and standard deviation of the results
:param nodes: list of cluster nodes to distribute tasks
:param depends: list of module dependencies
:return: DataFrame with the results
"""
cluster = dispy.JobCluster(run_point, nodes=nodes) #, depends=dependencies)
@ -143,6 +173,17 @@ def point_sliding_window(data, windowsize, train=0.8, models=None, partitioners=
def run_interval(mfts, partitioner, train_data, test_data, transformation=None, indexer=None):
"""
Interval forecast benchmark function to be executed on cluster nodes
:param mfts: FTS model
:param partitioner: Universe of Discourse partitioner
:param train_data: data used to train the model
:param test_data: ata used to test the model
:param window_key: id of the sliding window
:param transformation: data transformation
:param indexer: seasonal indexer
:return: a dictionary with the benchmark results
"""
import time
from pyFTS import hofts,ifts,pwfts
from pyFTS.partitioners import Grid, Entropy, FCM
@ -178,6 +219,25 @@ def run_interval(mfts, partitioner, train_data, test_data, transformation=None,
def interval_sliding_window(data, windowsize, train=0.8, models=None, partitioners=[Grid.GridPartitioner],
partitions=[10], max_order=3, transformation=None, indexer=None, dump=False,
save=False, file=None, sintetic=False,nodes=None, depends=None):
"""
Distributed sliding window benchmarks for FTS interval forecasters
:param data:
:param windowsize: size of sliding window
:param train: percentual of sliding window data used to train the models
:param models: FTS point forecasters
:param partitioners: Universe of Discourse partitioner
:param partitions: the max number of partitions on the Universe of Discourse
:param max_order: the max order of the models (for high order models)
:param transformation: data transformation
:param indexer: seasonal indexer
:param dump:
:param save: save results
:param file: file path to save the results
:param sintetic: if true only the average and standard deviation of the results
:param nodes: list of cluster nodes to distribute tasks
:param depends: list of module dependencies
:return: DataFrame with the results
"""
cluster = dispy.JobCluster(run_point, nodes=nodes) #, depends=dependencies)

View File

@ -4,6 +4,7 @@ from pyFTS import fts
class ConventionalFLRG(object):
"""First Order Conventional Fuzzy Logical Relationship Group"""
def __init__(self, LHS):
self.LHS = LHS
self.RHS = set()
@ -25,6 +26,7 @@ class ConventionalFLRG(object):
class ConventionalFTS(fts.FTS):
"""Conventional Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(ConventionalFTS, self).__init__(1, "CFTS " + name)
self.name = "Conventional FTS"

View File

@ -4,6 +4,7 @@ from pyFTS import fts, yu
class TrendWeightedFLRG(yu.WeightedFTS):
"""First Order Trend Weighted Fuzzy Logical Relationship Group"""
def __init__(self, LHS, **kwargs):
super(TrendWeightedFTS, self).__init__(LHS)
@ -31,6 +32,7 @@ class TrendWeightedFLRG(yu.WeightedFTS):
class TrendWeightedFTS(yu.WeightedFTS):
"""First Order Trend Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(TrendWeightedFTS, self).__init__(1, "TWFTS " + name)
self.name = "Trend Weighted FTS"

View File

@ -4,6 +4,7 @@ from pyFTS import fts
class HighOrderFLRG(object):
"""Conventional High Order Fuzzy Logical Relationship Group"""
def __init__(self, order):
self.LHS = []
self.RHS = {}
@ -39,6 +40,7 @@ class HighOrderFLRG(object):
class HighOrderFTS(fts.FTS):
"""Conventional High Order Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(HighOrderFTS, self).__init__(1, "HOFTS" + name)
self.name = "High Order FTS"

View File

@ -7,6 +7,7 @@ from pyFTS import hofts, fts, tree
class IntervalFTS(hofts.HighOrderFTS):
"""High Order Interval Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(IntervalFTS, self).__init__(order=1, name="IFTS " + name)
self.shortname = "IFTS " + name

View File

@ -4,6 +4,7 @@ from pyFTS import fts
class ImprovedWeightedFLRG(object):
"""First Order Improved Weighted Fuzzy Logical Relationship Group"""
def __init__(self, LHS):
self.LHS = LHS
self.RHS = {}
@ -33,6 +34,7 @@ class ImprovedWeightedFLRG(object):
class ImprovedWeightedFTS(fts.FTS):
"""First Order Improved Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(ImprovedWeightedFTS, self).__init__(1, "IWFTS " + name)
self.name = "Improved Weighted FTS"

View File

@ -6,7 +6,7 @@ from pyFTS.common import FuzzySet, Membership
from pyFTS.partitioners import partitioner
def distancia(x, y):
def distance(x, y):
if isinstance(x, list):
tmp = functools.reduce(operator.add, [(x[k] - y[k]) ** 2 for k in range(0, len(x))])
else:
@ -38,7 +38,7 @@ def c_means(k, dados, tam):
grupotmp = grupos[inst_count]
for grupo in centroides:
tmp = distancia(instancia, grupo)
tmp = distance(instancia, grupo)
if tmp < dist:
dist = tmp
# associa a a centroide de menor distância à instância
@ -76,6 +76,7 @@ def c_means(k, dados, tam):
return centroides
class CMeansPartitioner(partitioner.Partitioner):
def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(CMeansPartitioner, self).__init__("CMeans", data, npart, func=func, transformation=transformation)

View File

@ -78,6 +78,7 @@ def bestSplit(data, npart):
class EntropyPartitioner(partitioner.Partitioner):
"""Huarng Entropy Partitioner"""
def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(EntropyPartitioner, self).__init__("Entropy", data, npart, func=func, transformation=transformation)

View File

@ -101,6 +101,9 @@ def fuzzy_cmeans(k, dados, tam, m, deltadist=0.001):
class FCMPartitioner(partitioner.Partitioner):
"""
"""
def __init__(self, data,npart,func = Membership.trimf, transformation=None):
super(FCMPartitioner, self).__init__("FCM", data, npart, func=func, transformation=transformation)

View File

@ -7,6 +7,7 @@ from pyFTS.partitioners import partitioner
class GridPartitioner(partitioner.Partitioner):
"""Even Length Grid Partitioner"""
def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(GridPartitioner, self).__init__("Grid", data, npart, func=func, transformation=transformation)

View File

@ -11,6 +11,7 @@ from pyFTS.partitioners import partitioner
class HuarngPartitioner(partitioner.Partitioner):
"""Huarng Empirical Partitioner"""
def __init__(self, data,npart,func = Membership.trimf, transformation=None):
super(HuarngPartitioner, self).__init__("Huarng", data, npart, func=func, transformation=transformation)

View File

@ -10,6 +10,7 @@ from pyFTS import hofts, ifts, tree
class ProbabilisticWeightedFLRG(hofts.HighOrderFLRG):
"""High Order Probabilistic Weighted Fuzzy Logical Relationship Group"""
def __init__(self, order):
super(ProbabilisticWeightedFLRG, self).__init__(order)
self.RHS = {}
@ -42,6 +43,7 @@ class ProbabilisticWeightedFLRG(hofts.HighOrderFLRG):
class ProbabilisticWeightedFTS(ifts.IntervalFTS):
"""High Order Probabilistic Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(ProbabilisticWeightedFTS, self).__init__(order=1, name=name)
self.shortname = "PWFTS " + name

View File

@ -4,6 +4,7 @@ from pyFTS import fts
class ExponentialyWeightedFLRG(object):
"""First Order Exponentialy Weighted Fuzzy Logical Relationship Group"""
def __init__(self, LHS, c):
self.LHS = LHS
self.RHS = []
@ -37,6 +38,7 @@ class ExponentialyWeightedFLRG(object):
class ExponentialyWeightedFTS(fts.FTS):
"""First Order Exponentialy Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(ExponentialyWeightedFTS, self).__init__(1, "EWFTS")
self.name = "Exponentialy Weighted FTS"

View File

@ -4,6 +4,7 @@ from pyFTS import fts
class SeasonalFLRG(FLR.FLR):
"""First Order Seasonal Fuzzy Logical Relationship Group"""
def __init__(self, seasonality):
super(SeasonalFLRG, self).__init__(None,None)
self.LHS = seasonality
@ -26,6 +27,7 @@ class SeasonalFLRG(FLR.FLR):
class SeasonalFTS(fts.FTS):
"""First Order Seasonal Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(SeasonalFTS, self).__init__(1, "SFTS")
self.name = "Seasonal FTS"

View File

@ -38,6 +38,7 @@ class FLRGTreeNode:
class FLRGTree:
"""Represents a FLRG set with a tree structure"""
def __init__(self):
self.root = FLRGTreeNode(None)

2
yu.py
View File

@ -4,6 +4,7 @@ from pyFTS import fts
class WeightedFLRG(object):
"""First Order Weighted Fuzzy Logical Relationship Group"""
def __init__(self, LHS, **kwargs):
self.LHS = LHS
self.RHS = []
@ -31,6 +32,7 @@ class WeightedFLRG(object):
class WeightedFTS(fts.FTS):
"""First Order Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(WeightedFTS, self).__init__(1, "WFTS " + name)
self.name = "Weighted FTS"