From dd54133468674c568849237f0f9faa5068f084b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Thu, 6 Jul 2017 15:28:08 -0300 Subject: [PATCH] Bugfixes and improvements on ProbabilityDistribution --- benchmarks/benchmarks.py | 19 +++++++++++++++++++ ensemble/multiseasonal.py | 6 ++++-- models/seasonal/SeasonalIndexer.py | 6 ++++++ probabilistic/ProbabilityDistribution.py | 4 +++- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 5c78989..18e3414 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -826,6 +826,25 @@ def plot_density_rectange(ax, cmap, density, fig, resolution, time_from, time_to cb.set_label('Density') +def plot_probabilitydistribution_density(ax, cmap, probabilitydist, fig, time_from): + from matplotlib.patches import Rectangle + from matplotlib.collections import PatchCollection + from matplotlib.colorbar import ColorbarPatch + patches = [] + colors = [] + for ct, dt in enumerate(probabilitydist): + for y in dt.bins: + s = Rectangle((time_from+ct, y), 1, dt.resolution, fill=True, lw = 0) + patches.append(s) + colors.append(dt.distribution[y]*5) + pc = PatchCollection(patches=patches, match_original=True) + pc.set_clim([0, 1]) + pc.set_cmap(cmap) + pc.set_array(np.array(colors)) + ax.add_collection(pc) + cb = fig.colorbar(pc, ax=ax) + cb.set_label('Density') + def plotCompared(original, forecasts, labels, title): fig = plt.figure(figsize=[13, 6]) diff --git a/ensemble/multiseasonal.py b/ensemble/multiseasonal.py index 154d43d..abccc6e 100644 --- a/ensemble/multiseasonal.py +++ b/ensemble/multiseasonal.py @@ -82,8 +82,10 @@ class SeasonalEnsembleFTS(ensemble.EnsembleFTS): tmp = np.ravel(tmp).tolist() - dist = ProbabilityDistribution.ProbabilityDistribution("KDE", uod=[self.original_min, self.original_max], data=tmp, - **kwargs) + name = str(self.indexer.get_index(data.ix[k])) + + dist = ProbabilityDistribution.ProbabilityDistribution("KDE", uod=[self.original_min, self.original_max], + data=tmp, name=name, **kwargs) ret.append(dist) diff --git a/models/seasonal/SeasonalIndexer.py b/models/seasonal/SeasonalIndexer.py index 2a8d304..1672c0a 100644 --- a/models/seasonal/SeasonalIndexer.py +++ b/models/seasonal/SeasonalIndexer.py @@ -26,6 +26,9 @@ class SeasonalIndexer(object): def get_data(self, data): pass + def get_index(self, data): + pass + class LinearSeasonalIndexer(SeasonalIndexer): def __init__(self,seasons,units,ignore=None, **kwargs): @@ -196,5 +199,8 @@ class DateTimeSeasonalIndexer(SeasonalIndexer): def get_data(self, data): return data[self.data_fields].tolist() + def get_index(self, data): + return data[self.date_field] + def set_data(self, data, value): raise Exception("Operation not available!") \ No newline at end of file diff --git a/probabilistic/ProbabilityDistribution.py b/probabilistic/ProbabilityDistribution.py index 46eda71..e9fbd88 100644 --- a/probabilistic/ProbabilityDistribution.py +++ b/probabilistic/ProbabilityDistribution.py @@ -25,6 +25,7 @@ class ProbabilityDistribution(object): if self.bins is None: self.bins = np.linspace(int(self.uod[0]), int(self.uod[1]), int(self.nbins)).tolist() + self.resolution = (self.uod[1] - self.uod[0])/self.nbins self.labels = [str(k) for k in self.bins] self.index = SortedCollection.SortedCollection(iterable=sorted(self.bins)) @@ -127,7 +128,8 @@ class ProbabilityDistribution(object): axis.plot(self.data, yp, c="red") - axis.plot(self.bins, ys, c=color, label=self.name) + axis.plot(self.bins, ys, c=color) + axis.set_title(self.name) axis.set_xlabel('Universe of Discourse') axis.set_ylabel('Probability')