From a95b806a73f577d431475cd2c33d210bc55bc803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Fri, 31 Mar 2017 20:34:12 -0300 Subject: [PATCH] - Adding gaussmf and trapmf support on partitioners - Parallel util for partitioners --- benchmarks/parallel_benchmarks.py | 9 ++++++++ common/Membership.py | 14 ++++++------- partitioners/Entropy.py | 13 ++++++++++-- partitioners/Grid.py | 4 ++++ partitioners/Util.py | 35 ++++++++++++++++++++++++++++--- partitioners/parallel_util.py | 32 ++++++++++++++++++++++++++++ partitioners/partitioner.py | 1 + tests/general.py | 22 +++++++++++-------- 8 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 benchmarks/parallel_benchmarks.py create mode 100644 partitioners/parallel_util.py diff --git a/benchmarks/parallel_benchmarks.py b/benchmarks/parallel_benchmarks.py new file mode 100644 index 0000000..07a1337 --- /dev/null +++ b/benchmarks/parallel_benchmarks.py @@ -0,0 +1,9 @@ +from copy import deepcopy +from joblib import Parallel, delayed +import multiprocessing + + + + + + diff --git a/common/Membership.py b/common/Membership.py index 6827909..ad01263 100644 --- a/common/Membership.py +++ b/common/Membership.py @@ -5,24 +5,24 @@ from pyFTS import * def trimf(x, parameters): xx = round(x, 3) - if (xx < parameters[0]): + if xx < parameters[0]: return 0 - elif (xx >= parameters[0] and xx < parameters[1]): + elif parameters[0] <= xx < parameters[1]: return (x - parameters[0]) / (parameters[1] - parameters[0]) - elif (xx >= parameters[1] and xx <= parameters[2]): + elif parameters[1] <= xx <= parameters[2]: return (parameters[2] - xx) / (parameters[2] - parameters[1]) else: return 0 def trapmf(x, parameters): - if (x < parameters[0]): + if x < parameters[0]: return 0 - elif (x >= parameters[0] and x < parameters[1]): + elif parameters[0] <= x < parameters[1]: return (x - parameters[0]) / (parameters[1] - parameters[0]) - elif (x >= parameters[1] and x <= parameters[2]): + elif parameters[1] <= x <= parameters[2]: return 1 - elif (x >= parameters[2] and x <= parameters[3]): + elif parameters[2] <= x <= parameters[3]: return (parameters[3] - x) / (parameters[3] - parameters[2]) else: return 0 diff --git a/partitioners/Entropy.py b/partitioners/Entropy.py index 3c6d52f..c4a0313 100644 --- a/partitioners/Entropy.py +++ b/partitioners/Entropy.py @@ -76,6 +76,7 @@ def bestSplit(data, npart): else: return [threshold] + class EntropyPartitioner(partitioner.Partitioner): def __init__(self, data, npart, func = Membership.trimf, transformation=None): super(EntropyPartitioner, self).__init__("Entropy", data, npart, func=func, transformation=transformation) @@ -89,7 +90,15 @@ class EntropyPartitioner(partitioner.Partitioner): partitions = list(set(partitions)) partitions.sort() for c in np.arange(1, len(partitions) - 1): - sets.append(FuzzySet.FuzzySet(self.prefix + str(c), Membership.trimf, - [partitions[c - 1], partitions[c], partitions[c + 1]],partitions[c])) + if self.membership_function == Membership.trimf: + sets.append(FuzzySet.FuzzySet(self.prefix + str(c), Membership.trimf, + [partitions[c - 1], partitions[c], partitions[c + 1]],partitions[c])) + elif self.membership_function == Membership.trapmf: + b1 = (partitions[c] - partitions[c - 1])/2 + b2 = (partitions[c + 1] - partitions[c]) / 2 + sets.append(FuzzySet.FuzzySet(self.prefix + str(c), Membership.trapmf, + [partitions[c - 1], partitions[c] - b1, + partitions[c] - b2, partitions[c + 1]], + partitions[c])) return sets diff --git a/partitioners/Grid.py b/partitioners/Grid.py index 1effa7d..de8159a 100644 --- a/partitioners/Grid.py +++ b/partitioners/Grid.py @@ -24,6 +24,10 @@ class GridPartitioner(partitioner.Partitioner): elif self.membership_function == Membership.gaussmf: sets.append( FuzzySet.FuzzySet(self.prefix + str(count), Membership.gaussmf, [c, partlen / 3], c)) + elif self.membership_function == Membership.trapmf: + q = partlen / 2 + sets.append( + FuzzySet.FuzzySet(self.prefix + str(count), Membership.trapmf, [c - partlen, c - q, c + q, c + partlen], c)) count += 1 diff --git a/partitioners/Util.py b/partitioners/Util.py index bff9ce4..73fa5e2 100644 --- a/partitioners/Util.py +++ b/partitioners/Util.py @@ -5,9 +5,10 @@ import matplotlib.colors as pltcolors import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from pyFTS.common import Membership, Util +from pyFTS.partitioners import Grid,Huarng,FCM,Entropy -def plotSets(data, sets, titles, tam=[12, 10], save=False, file=None): +def plot_sets(data, sets, titles, tam=[12, 10], save=False, file=None): num = len(sets) #fig = plt.figure(figsize=tam) maxx = max(data) @@ -23,12 +24,40 @@ def plotSets(data, sets, titles, tam=[12, 10], save=False, file=None): ax.set_xlim([minx, maxx]) for s in sets[k]: if s.mf == Membership.trimf: - ax.plot([s.parameters[0],s.parameters[1],s.parameters[2]],[0,1,0]) + ax.plot(s.parameters,[0,1,0]) elif s.mf == Membership.gaussmf: tmpx = [ kk for kk in np.arange(s.lower, s.upper)] tmpy = [s.membership(kk) for kk in np.arange(s.lower, s.upper)] ax.plot(tmpx, tmpy) + elif s.mf == Membership.gaussmf: + ax.plot(s.parameters, [0, 1, 1, 0]) plt.tight_layout() - Util.showAndSaveImage(fig, file, save) \ No newline at end of file + Util.showAndSaveImage(fig, file, save) + + +def plot_partitioners(data, objs, tam=[12, 10], save=False, file=None): + sets = [k.sets for k in objs] + titles = [k.name for k in objs] + plot_sets(data,sets,titles,tam,save,file) + + +def explore_partitioners(data, npart, methods=None, mf=None, tam=[12, 10], save=False, file=None): + all_methods = [Grid.GridPartitioner, Entropy.EntropyPartitioner, FCM.FCMPartitioner, Huarng.HuarngPartitioner] + mfs = [Membership.trimf, Membership.gaussmf, Membership.trapmf] + + if methods is None: + methods = all_methods + + if mf is None: + mf = mfs + + objs = [] + + for p in methods: + for m in mf: + obj = p(data, npart,m) + objs.append(obj) + + plot_partitioners(data, objs, tam, save, file) diff --git a/partitioners/parallel_util.py b/partitioners/parallel_util.py new file mode 100644 index 0000000..f683dd4 --- /dev/null +++ b/partitioners/parallel_util.py @@ -0,0 +1,32 @@ +from copy import deepcopy +from joblib import Parallel, delayed +import multiprocessing +import numpy as np + +from pyFTS.common import Membership, Util +from pyFTS.partitioners import Grid,Huarng,FCM,Entropy +from pyFTS.partitioners import Util + + +def explore_partitioners(data, npart, methods=None, mf=None, tam=[12, 10], save=False, file=None): + all_methods = [Grid.GridPartitioner, Entropy.EntropyPartitioner, FCM.FCMPartitioner] + mfs = [Membership.trimf, Membership.gaussmf, Membership.trapmf] + + if methods is None: + methods = all_methods + + if mf is None: + mf = mfs + + num_cores = multiprocessing.cpu_count() + + objs = [] + for method in methods: + print(str(method)) + tmp = Parallel(n_jobs=num_cores)(delayed(method)(deepcopy(data), npart, m) for m in mf) + objs.append(tmp) + + objs = np.ravel(objs).tolist() + + Util.plot_partitioners(data, objs, tam, save, file) + diff --git a/partitioners/partitioner.py b/partitioners/partitioner.py index b887be2..abdfa0e 100644 --- a/partitioners/partitioner.py +++ b/partitioners/partitioner.py @@ -1,6 +1,7 @@ from pyFTS.common import FuzzySet, Membership import numpy as np + class Partitioner(object): def __init__(self,name,data,npart,func = Membership.trimf, names=None, prefix="A", transformation=None): self.name = name diff --git a/tests/general.py b/tests/general.py index ae56450..be2740c 100644 --- a/tests/general.py +++ b/tests/general.py @@ -23,13 +23,17 @@ from numpy import random #gauss_teste = random.normal(0,1.0,400) -os.chdir("/home/petronio/dados/Dropbox/Doutorado/Disciplinas/AdvancedFuzzyTimeSeriesModels/") +os.chdir("/home/petronio/dados/Dropbox/Doutorado/Codigos/") -#taiexpd = pd.read_csv("DataSets/TAIEX.csv", sep=",") -#taiex = np.array(taiexpd["avg"][:5000]) +taiexpd = pd.read_csv("DataSets/TAIEX.csv", sep=",") +taiex = np.array(taiexpd["avg"][:5000]) -nasdaqpd = pd.read_csv("DataSets/NASDAQ_IXIC.csv", sep=",") -nasdaq = np.array(nasdaqpd["avg"][:5000]) +from pyFTS.partitioners import parallel_util + +parallel_util.explore_partitioners(taiex,20) + +#nasdaqpd = pd.read_csv("DataSets/NASDAQ_IXIC.csv", sep=",") +#nasdaq = np.array(nasdaqpd["avg"][:5000]) #taiex = pd.read_csv("DataSets/TAIEX.csv", sep=",") #taiex_treino = np.array(taiex["avg"][2500:3900]) @@ -49,10 +53,10 @@ diff = Transformations.Differential(1) # gauss,2000,train=0.8, dump=True, save=True, file="experiments/arima_gauss.csv") -bchmk.interval_sliding_window(nasdaq,2000,train=0.8, #transformation=diff, #models=[pwfts.ProbabilisticWeightedFTS], # # - partitioners=[Grid.GridPartitioner], #Entropy.EntropyPartitioner], # FCM.FCMPartitioner, ], - partitions= np.arange(10,200,step=5), # - dump=True, save=True, file="experiments/nasdaq_interval.csv") +#bchmk.interval_sliding_window(nasdaq,2000,train=0.8, #transformation=diff, #models=[pwfts.ProbabilisticWeightedFTS], # # +# partitioners=[Grid.GridPartitioner], #Entropy.EntropyPartitioner], # FCM.FCMPartitioner, ], +# partitions= np.arange(10,200,step=5), # +# dump=True, save=True, file="experiments/nasdaq_interval.csv") #3bchmk.ahead_sliding_window(taiex,2000,train=0.8, steps=20, resolution=250, #transformation=diff, #models=[pwfts.ProbabilisticWeightedFTS], # # # partitioners=[Grid.GridPartitioner], #Entropy.EntropyPartitioner], # FCM.FCMPartitioner, ],