Features.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Features &#8212; Image Features Extraction 1.0.0 documentation</title>
  7. <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '1.0.0',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true,
  16. SOURCELINK_SUFFIX: '.txt'
  17. };
  18. </script>
  19. <script type="text/javascript" src="../_static/jquery.js"></script>
  20. <script type="text/javascript" src="../_static/underscore.js"></script>
  21. <script type="text/javascript" src="../_static/doctools.js"></script>
  22. <link rel="index" title="Index" href="../genindex.html" />
  23. <link rel="search" title="Search" href="../search.html" />
  24. <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  25. <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
  26. </head>
  27. <body>
  28. <div class="document">
  29. <div class="documentwrapper">
  30. <div class="bodywrapper">
  31. <div class="body" role="main">
  32. <h1>Source code for Features</h1><div class="highlight"><pre>
  33. <span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
  34. <span class="kn">import</span> <span class="nn">os.path</span>
  35. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">MyException</span>
  36. <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>
  37. <span class="sd">&quot;&quot;&quot;</span>
  38. <span class="sd"> contain the dataframe</span>
  39. <span class="sd"> &quot;&quot;&quot;</span>
  40. <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>
  41. <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>
  42. <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
  43. <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span><span class="o">=</span><span class="kc">None</span>
  44. <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>
  45. <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>
  46. <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>
  47. <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>
  48. <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>
  49. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_name</span></div>
  50. <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>
  51. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class_value</span></div>
  52. <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">&#39;inner&#39;</span><span class="p">):</span>
  53. <span class="sd">&quot;&quot;&quot;</span>
  54. <span class="sd"> Merges in the current Features object a second Features object (ex. obtained from the Voronoi Object)</span>
  55. <span class="sd"> :param Features_Obj: External Features Object</span>
  56. <span class="sd"> :type Features_Obj: Features (image_features_extraction package)</span>
  57. <span class="sd"> :param how_in: &#39;inner&#39;, &#39;outer&#39;</span>
  58. <span class="sd"> :type how_in: string</span>
  59. <span class="sd"> &gt;&gt;&gt; import image_features_extraction.Images as fe</span>
  60. <span class="sd"> &gt;&gt;&gt; IMGS = fe.Images(&#39;../images/CA/1&#39;)</span>
  61. <span class="sd"> &gt;&gt;&gt; vor = IMG.Voronoi()</span>
  62. <span class="sd"> &gt;&gt;&gt; features1 = IMG.features([&#39;area&#39;,&#39;perimeter&#39;,&#39;centroid&#39;,&#39;bbox&#39;, &#39;eccentricity&#39;])</span>
  63. <span class="sd"> &gt;&gt;&gt; features2 = vor.features([&#39;area&#39;,&#39;perimeter&#39;,&#39;centroid&#39;,&#39;bbox&#39;, &#39;eccentricity&#39;])</span>
  64. <span class="sd"> &gt;&gt;&gt; features3 = features1.merge(features2, how_in=&#39;inner&#39;)</span>
  65. <span class="sd"> &gt;&gt;&gt; features3.get_dataframe().head()</span>
  66. <span class="sd"> &quot;&quot;&quot;</span>
  67. <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>
  68. <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">&#39;id&#39;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="n">how_in</span><span class="p">)</span>
  69. <span class="k">return</span> <span class="n">Features</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span></div>
  70. <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">&#39;file&#39;</span><span class="p">,</span> <span class="n">do_append</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
  71. <span class="sd">&quot;&quot;&quot;</span>
  72. <span class="sd"> save the current dataframe into the type of storage given in input.</span>
  73. <span class="sd"> :param storage_name: storage name, (e.g., file name if &#39; storage type is &#39;file&#39;)</span>
  74. <span class="sd"> :type storage_name: string</span>
  75. <span class="sd"> :param type_storage: type of storage (default is &#39;file&#39;) (&#39;db&#39;, &#39;json&#39; will be future implementations)</span>
  76. <span class="sd"> :type type_storage: string</span>
  77. <span class="sd"> :param do_append: if True it appends to existing storage. If False it creates a new storage</span>
  78. <span class="sd"> :type do_append: boolean</span>
  79. <span class="sd"> if &#39;do_append=True&#39;: This version of the method does not check whether the new data are consistent with presisitng data into</span>
  80. <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>
  81. <span class="sd"> </span>
  82. <span class="sd"> :returns: 1 if sucessuful otherwise 0</span>
  83. <span class="sd"> :rtype: int</span>
  84. <span class="sd"> :example:</span>
  85. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  86. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  87. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  88. <span class="sd"> &gt;&gt;&gt; reg = img.Regions().item(1)</span>
  89. <span class="sd"> &gt;&gt;&gt; features = regs.get_features([&#39;label&#39;, &#39;area&#39;,&#39;perimeter&#39;, &#39;centroid&#39;], class_value=1)</span>
  90. <span class="sd"> &gt;&gt;&gt; features.save(&#39;my_file_name&#39;)</span>
  91. <span class="sd"> &quot;&quot;&quot;</span>
  92. <span class="k">try</span><span class="p">:</span>
  93. <span class="k">if</span> <span class="n">type_storage</span> <span class="o">==</span> <span class="s1">&#39;file&#39;</span><span class="p">:</span>
  94. <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>
  95. <span class="k">else</span><span class="p">:</span>
  96. <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">&quot;error: storage type no specified or not found&quot;</span><span class="p">)</span>
  97. <span class="k">return</span> <span class="mi">0</span>
  98. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  99. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;one or more input labels might be wrong:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
  100. <span class="k">return</span> <span class="mi">0</span></div>
  101. <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>
  102. <span class="sd">&quot;&quot;&quot;</span>
  103. <span class="sd"> load the data from the local directory with the name indicated</span>
  104. <span class="sd"> &quot;&quot;&quot;</span>
  105. <span class="k">if</span> <span class="n">do_append</span><span class="o">==</span><span class="kc">True</span><span class="p">:</span>
  106. <span class="n">add_header</span> <span class="o">=</span> <span class="kc">False</span>
  107. <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>
  108. <span class="n">add_header</span> <span class="o">=</span> <span class="kc">True</span>
  109. <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">&#39;a&#39;</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>
  110. <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>
  111. <span class="k">else</span><span class="p">:</span>
  112. <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">&#39;w&#39;</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>
  113. <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>
  114. <span class="k">return</span> <span class="mi">1</span>
  115. <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>
  116. <span class="sd">&quot;&quot;&quot;</span>
  117. <span class="sd"> creates a panda data frame contining the feature values</span>
  118. <span class="sd"> :param include_class: if True if includes the class name and value as the last column</span>
  119. <span class="sd"> :type include_class: Boolean</span>
  120. <span class="sd"> &quot;&quot;&quot;</span>
  121. <span class="k">try</span><span class="p">:</span>
  122. <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>
  123. <span class="k">if</span> <span class="n">include_class</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
  124. <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">&#39;&#39;</span><span class="p">:</span>
  125. <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">&quot;error: class name not set&quot;</span><span class="p">)</span>
  126. <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>
  127. <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">&quot;error: class value not set&quot;</span><span class="p">)</span>
  128. <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>
  129. <span class="k">return</span> <span class="n">df</span>
  130. <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>
  131. <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>
  132. <span class="k">return</span> <span class="kc">None</span></div></div>
  133. </pre></div>
  134. </div>
  135. </div>
  136. </div>
  137. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  138. <div class="sphinxsidebarwrapper">
  139. <h1 class="logo"><a href="../index.html">Image Features Extraction</a></h1>
  140. <h3>Navigation</h3>
  141. <ul>
  142. <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
  143. <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
  144. </ul>
  145. <div class="relations">
  146. <h3>Related Topics</h3>
  147. <ul>
  148. <li><a href="../index.html">Documentation overview</a><ul>
  149. <li><a href="index.html">Module code</a><ul>
  150. </ul></li>
  151. </ul></li>
  152. </ul>
  153. </div>
  154. <div id="searchbox" style="display: none" role="search">
  155. <h3>Quick search</h3>
  156. <form class="search" action="../search.html" method="get">
  157. <div><input type="text" name="q" /></div>
  158. <div><input type="submit" value="Go" /></div>
  159. <input type="hidden" name="check_keywords" value="yes" />
  160. <input type="hidden" name="area" value="default" />
  161. </form>
  162. </div>
  163. <script type="text/javascript">$('#searchbox').show(0);</script>
  164. </div>
  165. </div>
  166. <div class="clearer"></div>
  167. </div>
  168. <div class="footer">
  169. &copy;2017, Remigio Picone.
  170. |
  171. Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
  172. &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
  173. </div>
  174. </body>
  175. </html>