2019-04-22 10:01:58 -03:00

301 lines
47 KiB
HTML

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-55120145-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<title>pyFTS.models.seasonal.partitioner &#8212; pyFTS 1.4 documentation</title>
<link rel="stylesheet" href="../../../../_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
<script type="text/javascript" src="../../../../_static/documentation_options.js"></script>
<script type="text/javascript" src="../../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script type="text/javascript" src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.4 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../../../index.html">
<img class="logo" src="../../../../_static/logo_heading2.png" alt="Logo"/>
</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.seasonal.partitioner</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Membership</span><span class="p">,</span> <span class="n">FuzzySet</span> <span class="k">as</span> <span class="n">FS</span>
<span class="kn">from</span> <span class="nn">pyFTS.common.Composite</span> <span class="k">import</span> <span class="n">FuzzySet</span> <span class="k">as</span> <span class="n">Composite</span>
<span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">Grid</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.seasonal.common</span> <span class="k">import</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">strip_datepart</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pylab</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="k">import</span> <span class="n">KDTree</span>
<div class="viewcode-block" id="TimeGridPartitioner"><a class="viewcode-back" href="../../../../pyFTS.models.seasonal.html#pyFTS.models.seasonal.partitioner.TimeGridPartitioner">[docs]</a><span class="k">class</span> <span class="nc">TimeGridPartitioner</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">Partitioner</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Even Length DateTime Grid Partitioner&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Even Length Grid Partitioner</span>
<span class="sd"> :param seasonality: Time granularity, from pyFTS.models.seasonal.common.DateTime</span>
<span class="sd"> :param data: Training data of which the universe of discourse will be extracted. The universe of discourse is the open interval between the minimum and maximum values of the training data.</span>
<span class="sd"> :param npart: The number of universe of discourse partitions, i.e., the number of fuzzy sets that will be created</span>
<span class="sd"> :param func: Fuzzy membership function (pyFTS.common.Membership)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="nb">super</span><span class="p">(</span><span class="n">TimeGridPartitioner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;TimeGrid&quot;</span><span class="p">,</span> <span class="n">preprocess</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;seasonality&#39;</span><span class="p">,</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">day_of_year</span><span class="p">)</span>
<span class="sd">&#39;&#39;&#39;Seasonality, a pyFTS.models.seasonal.common.DateTime object&#39;&#39;&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mask&#39;</span><span class="p">,</span> <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
<span class="sd">&#39;&#39;&#39;A string with datetime formating mask&#39;&#39;&#39;</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">year</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="p">[</span><span class="n">strip_datepart</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">tmp</span>
<span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span><span class="s1">&#39;seasonal&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">setnames</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">setnames</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span> <span class="o">=</span> <span class="n">FS</span><span class="o">.</span><span class="n">set_ordered</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s1">&#39;seasonal&#39;</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">extractor</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">strip_datepart</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">)</span>
<div class="viewcode-block" id="TimeGridPartitioner.build"><a class="viewcode-back" href="../../../../pyFTS.models.seasonal.html#pyFTS.models.seasonal.partitioner.TimeGridPartitioner.build">[docs]</a> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">sets</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;variable&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">variable</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="p">}</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">year</span><span class="p">:</span>
<span class="n">dlen</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">)</span>
<span class="n">partlen</span> <span class="o">=</span> <span class="n">dlen</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">day_of_week</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">pl2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">minute</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">pl2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">60</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">hour</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">pl2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="o">==</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">month</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">pl2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span> <span class="ow">in</span> <span class="p">(</span><span class="n">DateTime</span><span class="o">.</span><span class="n">half</span><span class="p">,</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">third</span><span class="p">,</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">quarter</span><span class="p">,</span> <span class="n">DateTime</span><span class="o">.</span><span class="n">sixth</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">pl2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">partlen</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span>
<span class="n">pl2</span> <span class="o">=</span> <span class="n">partlen</span> <span class="o">/</span> <span class="mi">2</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="n">partlen</span><span class="p">):</span>
<span class="n">set_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_name</span><span class="p">(</span><span class="n">count</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">membership_function</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">:</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">Composite</span><span class="p">(</span><span class="n">set_name</span><span class="p">,</span> <span class="n">superset</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="n">pl2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">pl2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">partlen</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">centroid</span> <span class="o">=</span> <span class="n">c</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="k">elif</span> <span class="n">c</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">max</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">Composite</span><span class="p">(</span><span class="n">set_name</span><span class="p">,</span> <span class="n">superset</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="o">-</span><span class="n">pl2</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span>
<span class="n">pl2</span><span class="p">],</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">partlen</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">centroid</span> <span class="o">=</span> <span class="n">c</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">partlen</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">membership_function</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span><span class="p">:</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span><span class="p">,</span> <span class="p">[</span><span class="n">c</span><span class="p">,</span> <span class="n">partlen</span> <span class="o">/</span> <span class="mi">3</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">membership_function</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span><span class="p">:</span>
<span class="n">q</span> <span class="o">=</span> <span class="n">partlen</span> <span class="o">/</span> <span class="mi">4</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">Composite</span><span class="p">(</span><span class="n">set_name</span><span class="p">,</span> <span class="n">superset</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
<span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="n">pl2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="mf">0.0000001</span><span class="p">],</span> <span class="mi">0</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append_set</span><span class="p">(</span><span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span><span class="p">,</span>
<span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">c</span> <span class="o">-</span> <span class="n">q</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">q</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">partlen</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">centroid</span> <span class="o">=</span> <span class="n">c</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">sets</span><span class="p">[</span><span class="n">set_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="p">,</span> <span class="n">set_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span><span class="p">,</span>
<span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="n">partlen</span><span class="p">,</span> <span class="n">c</span> <span class="o">-</span> <span class="n">q</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">q</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">partlen</span><span class="p">],</span> <span class="n">c</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">return</span> <span class="n">sets</span></div>
<div class="viewcode-block" id="TimeGridPartitioner.build_index"><a class="viewcode-back" href="../../../../pyFTS.models.seasonal.html#pyFTS.models.seasonal.partitioner.TimeGridPartitioner.build_index">[docs]</a> <span class="k">def</span> <span class="nf">build_index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">points</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
<span class="n">points</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">])</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="n">points</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">fset</span><span class="o">.</span><span class="n">lower</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">upper</span><span class="p">])</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
<span class="n">points</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="n">fset</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">])</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">setrecursionlimit</span><span class="p">(</span><span class="mi">100000</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span> <span class="o">=</span> <span class="n">KDTree</span><span class="p">(</span><span class="n">points</span><span class="p">)</span>
<span class="n">sys</span><span class="o">.</span><span class="n">setrecursionlimit</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span></div>
<div class="viewcode-block" id="TimeGridPartitioner.search"><a class="viewcode-back" href="../../../../pyFTS.models.seasonal.html#pyFTS.models.seasonal.partitioner.TimeGridPartitioner.search">[docs]</a> <span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Perform a search for the nearest fuzzy sets of the point &#39;data&#39;. This function were designed to work with several</span>
<span class="sd"> overlapped fuzzy sets.</span>
<span class="sd"> :param data: the value to search for the nearest fuzzy sets</span>
<span class="sd"> :param type: the return type: &#39;index&#39; for the fuzzy set indexes or &#39;name&#39; for fuzzy set names.</span>
<span class="sd"> :param results: the number of nearest fuzzy sets to return</span>
<span class="sd"> :return: a list with the nearest fuzzy sets</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="nb">type</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span><span class="s1">&#39;index&#39;</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;results&#39;</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">build_index</span><span class="p">()</span>
<span class="n">_</span><span class="p">,</span> <span class="n">ix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span><span class="o">.</span><span class="n">query</span><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">data</span><span class="p">],</span> <span class="n">results</span><span class="p">)</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">ix</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="k">if</span> <span class="mi">0</span> <span class="ow">in</span> <span class="n">ix</span><span class="p">:</span>
<span class="n">ix</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span><span class="o">-</span><span class="mi">1</span> <span class="ow">in</span> <span class="n">ix</span><span class="p">:</span>
<span class="n">ix</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">ix</span><span class="p">)]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">ix</span><span class="p">)</span></div>
<div class="viewcode-block" id="TimeGridPartitioner.plot"><a class="viewcode-back" href="../../../../pyFTS.models.seasonal.html#pyFTS.models.seasonal.partitioner.TimeGridPartitioner.plot">[docs]</a> <span class="k">def</span> <span class="nf">plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ax</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot the</span>
<span class="sd"> :param ax:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">season</span><span class="o">.</span><span class="n">value</span><span class="p">])</span>
<span class="n">ticks</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s1">&#39;composite&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">ss</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">sets</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">plot_set</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">ss</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">plot_set</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="n">ticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">s</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">centroid</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">xaxis</span><span class="o">.</span><span class="n">set_ticklabels</span><span class="p">(</span><span class="n">ticks</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">xaxis</span><span class="o">.</span><span class="n">set_ticks</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></div></div>
</pre></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.4 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2018, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.2.
</div>
</body>
</html>