123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Features — Image Features Extraction 1.0.0 documentation</title>
- <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../',
- VERSION: '1.0.0',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true,
- SOURCELINK_SUFFIX: '.txt'
- };
- </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>
- <link rel="index" title="Index" href="../genindex.html" />
- <link rel="search" title="Search" href="../search.html" />
-
- <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
-
-
- <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
- </head>
- <body>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <h1>Source code for Features</h1><div class="highlight"><pre>
- <span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
- <span class="kn">import</span> <span class="nn">os.path</span>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">MyException</span>
- <div class="viewcode-block" id="Features"><a class="viewcode-back" href="../code.html#Features.Features">[docs]</a><span class="k">class</span> <span class="nc">Features</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> contain the dataframe</span>
- <span class="sd"> """</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="n">data_frame</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__data_frame</span> <span class="o">=</span> <span class="n">data_frame</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span> <span class="o">=</span> <span class="s1">''</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span><span class="o">=</span><span class="kc">None</span>
- <div class="viewcode-block" id="Features.set_class_name"><a class="viewcode-back" href="../code.html#Features.Features.set_class_name">[docs]</a> <span class="k">def</span> <span class="nf">set_class_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">class_name</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span> <span class="o">=</span> <span class="n">class_name</span></div>
- <div class="viewcode-block" id="Features.set_class_value"><a class="viewcode-back" href="../code.html#Features.Features.set_class_value">[docs]</a> <span class="k">def</span> <span class="nf">set_class_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">class_value</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span> <span class="o">=</span> <span class="n">class_value</span></div>
- <div class="viewcode-block" id="Features.get_class_name"><a class="viewcode-back" href="../code.html#Features.Features.get_class_name">[docs]</a> <span class="k">def</span> <span class="nf">get_class_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span></div>
- <div class="viewcode-block" id="Features.get_class_value"><a class="viewcode-back" href="../code.html#Features.Features.get_class_value">[docs]</a> <span class="k">def</span> <span class="nf">get_class_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span></div>
- <div class="viewcode-block" id="Features.merge"><a class="viewcode-back" href="../code.html#Features.Features.merge">[docs]</a> <span class="k">def</span> <span class="nf">merge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Features_Obj</span><span class="p">,</span> <span class="n">how_in</span><span class="o">=</span><span class="s1">'inner'</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Merges in the current Features object a second Features object (ex. obtained from the Voronoi Object)</span>
- <span class="sd"> :param Features_Obj: External Features Object</span>
- <span class="sd"> :type Features_Obj: Features (image_features_extraction package)</span>
- <span class="sd"> :param how_in: 'inner', 'outer'</span>
- <span class="sd"> :type how_in: string</span>
- <span class="sd"> >>> import image_features_extraction.Images as fe</span>
- <span class="sd"> >>> IMGS = fe.Images('../images/CA/1')</span>
- <span class="sd"> >>> vor = IMG.Voronoi()</span>
- <span class="sd"> >>> features1 = IMG.features(['area','perimeter','centroid','bbox', 'eccentricity'])</span>
- <span class="sd"> >>> features2 = vor.features(['area','perimeter','centroid','bbox', 'eccentricity'])</span>
- <span class="sd"> >>> features3 = features1.merge(features2, how_in='inner')</span>
- <span class="sd"> >>> features3.get_dataframe().head()</span>
- <span class="sd"> """</span>
- <span class="n">df1</span> <span class="o">=</span> <span class="n">Features_Obj</span><span class="o">.</span><span class="n">get_dataframe</span><span class="p">();</span>
- <span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__data_frame</span><span class="p">,</span> <span class="n">df1</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s1">'id'</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="n">how_in</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">Features</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span></div>
- <div class="viewcode-block" id="Features.save"><a class="viewcode-back" href="../code.html#Features.Features.save">[docs]</a> <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">storage_name</span><span class="p">,</span> <span class="n">type_storage</span><span class="o">=</span><span class="s1">'file'</span><span class="p">,</span> <span class="n">do_append</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> save the current dataframe into the type of storage given in input.</span>
- <span class="sd"> :param storage_name: storage name, (e.g., file name if ' storage type is 'file')</span>
- <span class="sd"> :type storage_name: string</span>
- <span class="sd"> :param type_storage: type of storage (default is 'file') ('db', 'json' will be future implementations)</span>
- <span class="sd"> :type type_storage: string</span>
- <span class="sd"> :param do_append: if True it appends to existing storage. If False it creates a new storage</span>
- <span class="sd"> :type do_append: boolean</span>
- <span class="sd"> if 'do_append=True': This version of the method does not check whether the new data are consistent with presisitng data into</span>
- <span class="sd"> the existing storage. It just tries to append the date and might fail or not depending on the type of storage</span>
- <span class="sd"> </span>
- <span class="sd"> :returns: 1 if sucessuful otherwise 0</span>
- <span class="sd"> :rtype: int</span>
- <span class="sd"> :example:</span>
- <span class="sd"> >>> import image_features_extraction as fe</span>
- <span class="sd"> >>> imgs = fe.Images(folder_name)</span>
- <span class="sd"> >>> img = imgs.item(1)</span>
- <span class="sd"> >>> reg = img.Regions().item(1)</span>
- <span class="sd"> >>> features = regs.get_features(['label', 'area','perimeter', 'centroid'], class_value=1)</span>
- <span class="sd"> >>> features.save('my_file_name')</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">type_storage</span> <span class="o">==</span> <span class="s1">'file'</span><span class="p">:</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__save_file</span><span class="p">(</span><span class="n">storage_name</span><span class="p">,</span> <span class="n">do_append</span><span class="p">)</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">"error: storage type no specified or not found"</span><span class="p">)</span>
- <span class="k">return</span> <span class="mi">0</span>
- <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"one or more input labels might be wrong:</span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
- <span class="k">return</span> <span class="mi">0</span></div>
- <span class="k">def</span> <span class="nf">__save_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">do_append</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> load the data from the local directory with the name indicated</span>
- <span class="sd"> """</span>
- <span class="k">if</span> <span class="n">do_append</span><span class="o">==</span><span class="kc">True</span><span class="p">:</span>
- <span class="n">add_header</span> <span class="o">=</span> <span class="kc">False</span>
- <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span> <span class="o">==</span> <span class="kc">False</span><span class="p">:</span>
- <span class="n">add_header</span> <span class="o">=</span> <span class="kc">True</span>
- <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="c1"># it appends or creates a new file</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__data_frame</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="n">add_header</span><span class="p">)</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="c1"># it creates a new file</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__data_frame</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
- <span class="k">return</span> <span class="mi">1</span>
- <div class="viewcode-block" id="Features.get_dataframe"><a class="viewcode-back" href="../code.html#Features.Features.get_dataframe">[docs]</a> <span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_class</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> creates a panda data frame contining the feature values</span>
- <span class="sd"> :param include_class: if True if includes the class name and value as the last column</span>
- <span class="sd"> :type include_class: Boolean</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="n">df</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__data_frame</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
- <span class="k">if</span> <span class="n">include_class</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span> <span class="o">==</span> <span class="s1">''</span><span class="p">:</span>
- <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">"error: class name not set"</span><span class="p">)</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
- <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">"error: class value not set"</span><span class="p">)</span>
- <span class="n">df</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span><span class="p">]</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span>
- <span class="k">return</span> <span class="n">df</span>
- <span class="k">except</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
- <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
- <span class="k">return</span> <span class="kc">None</span></div></div>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
- <div class="sphinxsidebarwrapper">
- <h1 class="logo"><a href="../index.html">Image Features Extraction</a></h1>
- <h3>Navigation</h3>
- <ul>
- <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
- </ul>
- <div class="relations">
- <h3>Related Topics</h3>
- <ul>
- <li><a href="../index.html">Documentation overview</a><ul>
- <li><a href="index.html">Module code</a><ul>
- </ul></li>
- </ul></li>
- </ul>
- </div>
- <div id="searchbox" style="display: none" role="search">
- <h3>Quick search</h3>
- <form class="search" action="../search.html" method="get">
- <div><input type="text" name="q" /></div>
- <div><input type="submit" value="Go" /></div>
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="footer">
- ©2017, Remigio Picone.
-
- |
- Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
- & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
-
- </div>
-
-
- </body>
- </html>
|