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

View File

@ -1,3 +1,7 @@
"""
Benchmark utility functions
"""
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from copy import deepcopy 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): 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 = [] ret = []
if sintetic: 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): 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 import time
from pyFTS import yu,chen,hofts,ifts,pwfts,ismailefendi,sadaei from pyFTS import yu,chen,hofts,ifts,pwfts,ismailefendi,sadaei
from pyFTS.partitioners import Grid, Entropy, FCM 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], 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, partitions=[10], max_order=3, transformation=None, indexer=None, dump=False,
save=False, file=None, sintetic=False,nodes=None, depends=None): 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) 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): 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 import time
from pyFTS import hofts,ifts,pwfts from pyFTS import hofts,ifts,pwfts
from pyFTS.partitioners import Grid, Entropy, FCM 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], 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, partitions=[10], max_order=3, transformation=None, indexer=None, dump=False,
save=False, file=None, sintetic=False,nodes=None, depends=None): 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) cluster = dispy.JobCluster(run_point, nodes=nodes) #, depends=dependencies)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@ from pyFTS.common import FuzzySet, Membership
from pyFTS.partitioners import partitioner from pyFTS.partitioners import partitioner
def distancia(x, y): def distance(x, y):
if isinstance(x, list): if isinstance(x, list):
tmp = functools.reduce(operator.add, [(x[k] - y[k]) ** 2 for k in range(0, len(x))]) tmp = functools.reduce(operator.add, [(x[k] - y[k]) ** 2 for k in range(0, len(x))])
else: else:
@ -38,7 +38,7 @@ def c_means(k, dados, tam):
grupotmp = grupos[inst_count] grupotmp = grupos[inst_count]
for grupo in centroides: for grupo in centroides:
tmp = distancia(instancia, grupo) tmp = distance(instancia, grupo)
if tmp < dist: if tmp < dist:
dist = tmp dist = tmp
# associa a a centroide de menor distância à instância # associa a a centroide de menor distância à instância
@ -76,6 +76,7 @@ def c_means(k, dados, tam):
return centroides return centroides
class CMeansPartitioner(partitioner.Partitioner): class CMeansPartitioner(partitioner.Partitioner):
def __init__(self, data, npart, func = Membership.trimf, transformation=None): def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(CMeansPartitioner, self).__init__("CMeans", data, npart, func=func, transformation=transformation) 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): class EntropyPartitioner(partitioner.Partitioner):
"""Huarng Entropy Partitioner"""
def __init__(self, data, npart, func = Membership.trimf, transformation=None): def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(EntropyPartitioner, self).__init__("Entropy", data, npart, func=func, transformation=transformation) 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): class FCMPartitioner(partitioner.Partitioner):
"""
"""
def __init__(self, data,npart,func = Membership.trimf, transformation=None): def __init__(self, data,npart,func = Membership.trimf, transformation=None):
super(FCMPartitioner, self).__init__("FCM", data, npart, func=func, transformation=transformation) 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): class GridPartitioner(partitioner.Partitioner):
"""Even Length Grid Partitioner"""
def __init__(self, data, npart, func = Membership.trimf, transformation=None): def __init__(self, data, npart, func = Membership.trimf, transformation=None):
super(GridPartitioner, self).__init__("Grid", data, npart, func=func, transformation=transformation) 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): class HuarngPartitioner(partitioner.Partitioner):
"""Huarng Empirical Partitioner"""
def __init__(self, data,npart,func = Membership.trimf, transformation=None): def __init__(self, data,npart,func = Membership.trimf, transformation=None):
super(HuarngPartitioner, self).__init__("Huarng", data, npart, func=func, transformation=transformation) 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): class ProbabilisticWeightedFLRG(hofts.HighOrderFLRG):
"""High Order Probabilistic Weighted Fuzzy Logical Relationship Group"""
def __init__(self, order): def __init__(self, order):
super(ProbabilisticWeightedFLRG, self).__init__(order) super(ProbabilisticWeightedFLRG, self).__init__(order)
self.RHS = {} self.RHS = {}
@ -42,6 +43,7 @@ class ProbabilisticWeightedFLRG(hofts.HighOrderFLRG):
class ProbabilisticWeightedFTS(ifts.IntervalFTS): class ProbabilisticWeightedFTS(ifts.IntervalFTS):
"""High Order Probabilistic Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs): def __init__(self, name, **kwargs):
super(ProbabilisticWeightedFTS, self).__init__(order=1, name=name) super(ProbabilisticWeightedFTS, self).__init__(order=1, name=name)
self.shortname = "PWFTS " + name self.shortname = "PWFTS " + name

View File

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

View File

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

View File

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

2
yu.py
View File

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