<|start_filename|>docs/_build/html/_modules/pylabel/analyze.html<|end_filename|>
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>pylabel.analyze &mdash; PyLabel 0.1.30 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> PyLabel
            <img src="https://raw.githubusercontent.com/pylabel-project/datasets_models/main/arrow_logo.png" class="logo" alt="Logo"/>
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Documentation :</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../pylabel.html">pylabel package</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#submodules">Submodules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.analyze">pylabel.analyze module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.dataset">pylabel.dataset module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.exporter">pylabel.exporter module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.importer">pylabel.importer module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.labeler">pylabel.labeler module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.shared">pylabel.shared module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.splitter">pylabel.splitter module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel.visualize">pylabel.visualize module</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pylabel.html#module-pylabel">Module contents</a></li>
</ul>
</li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">PyLabel</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>pylabel.analyze</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for pylabel.analyze</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;The analyze module analyzes the contents of the dataset and provides summary statistics </span>
<span class="sd">such as the count of images and classes. &quot;&quot;&quot;</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">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="Analyze"><a class="viewcode-back" href="../../pylabel.html#pylabel.analyze.Analyze">[docs]</a><span class="k">class</span> <span class="nc">Analyze</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span> <span class="o">=</span> <span class="n">dataset</span>
        <span class="c1"># self.split_counts = ds.df[&quot;split&quot;].value_counts(dropna=False)</span>
        <span class="c1"># self.split_pct = ds.df[&quot;split&quot;].value_counts(normalize=True, dropna=False)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">classes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns list of all cat names in the dataset sorted by cat_id value.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List</span>

<span class="sd">        Example:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.classes</span>
<span class="sd">            [&quot;Squirrel&quot;, &quot;Nut&quot;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filtered_df</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="s2">&quot;cat_id&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">notnull</span><span class="p">()]</span>
        <span class="n">categories</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">filtered_df</span><span class="o">.</span><span class="n">cat_name</span><span class="p">,</span> <span class="n">filtered_df</span><span class="o">.</span><span class="n">cat_id</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;int&quot;</span><span class="p">)))</span>
        <span class="n">categories</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">categories</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">categories</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">class_ids</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns a sorted list of all cat ids in the dataset.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List</span>

<span class="sd">        Example:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.class_ids</span>
<span class="sd">            [0,1]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filtered_df</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="s2">&quot;cat_id&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">notnull</span><span class="p">()]</span>
        <span class="n">cat_ids</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">filtered_df</span><span class="o">.</span><span class="n">cat_id</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;int&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">unique</span><span class="p">())</span>
        <span class="n">cat_ids</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">cat_ids</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">class_counts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Counts the numbers of instances of each class in the dataset. Uses the Pandas value_counts</span>
<span class="sd">        method and returns a Pandas Series.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Pandas Series</span>
<span class="sd">        Example:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.class_counts</span>
<span class="sd">            squirrel  50</span>
<span class="sd">            nut       100</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value_counts</span><span class="p">(</span><span class="n">dropna</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">num_classes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Counts the unique number of classes in the dataset.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Int</span>
<span class="sd">        Example:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.num_classes</span>
<span class="sd">            2</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cat_names</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">cat_name</span><span class="o">.</span><span class="n">unique</span><span class="p">())</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">cat_names</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">])</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">num_images</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Counts the number of images in the dataset.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Int</span>
<span class="sd">        Example:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.num_images</span>
<span class="sd">            100</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="s2">&quot;img_filename&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">nunique</span><span class="p">()</span>

<div class="viewcode-block" id="Analyze.ShowClassSplits"><a class="viewcode-back" href="../../pylabel.html#pylabel.analyze.Analyze.ShowClassSplits">[docs]</a>    <span class="k">def</span> <span class="nf">ShowClassSplits</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Show the distribution of classes across train, val, and</span>
<span class="sd">        test splits of the dataset.</span>

<span class="sd">        Args:</span>
<span class="sd">            normalize (bool): Defaults to True.</span>
<span class="sd">                If True, then will return the relative frequencies of the classes between 0 and 1.</span>
<span class="sd">                If False, then will return the absolute counts of each class.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Pandas Dataframe</span>

<span class="sd">        Examples:</span>
<span class="sd">            &gt;&gt;&gt; dataset.analyze.ShowClassSplits(normalize=True)</span>
<span class="sd">            cat_name  all  train  test  dev</span>
<span class="sd">            squirrel  .66  .64    .65   .63</span>
<span class="sd">            nut       .34  .34    .35   .37</span>

<span class="sd">            &gt;&gt;&gt; dataset.analyze.ShowClassSplits(normalize=False)</span>
<span class="sd">            cat_name  all  train  test  dev</span>
<span class="sd">            squirrel  66   64     65    63</span>
<span class="sd">            nut       34   34     35    37</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ds</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span>

        <span class="k">def</span> <span class="nf">move_column_inplace</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">col</span><span class="p">,</span> <span class="n">pos</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Assists to rearrange columns to a desired order.</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">col</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">col</span><span class="p">)</span>
            <span class="n">df</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span>

        <span class="n">df_value_counts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
            <span class="n">ds</span><span class="o">.</span><span class="n">df</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value_counts</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="n">normalize</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">]</span>
        <span class="p">)</span>

        <span class="n">df_value_counts</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;cat_name&quot;</span>
        <span class="n">df_value_counts</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;all&quot;</span><span class="p">]</span>

        <span class="n">split_df</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s2">&quot;split&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">split_df</span><span class="o">.</span><span class="n">ngroups</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">df_value_counts</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">split_df</span><span class="p">:</span>
            <span class="n">group_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
            <span class="n">df_split_value_counts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
                <span class="n">group_df</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value_counts</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="n">normalize</span><span class="p">),</span>
                <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">],</span>
            <span class="p">)</span>
            <span class="n">df_split_value_counts</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;cat_name&quot;</span>
            <span class="n">df_split_value_counts</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="n">df_value_counts</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="n">df_value_counts</span><span class="p">,</span> <span class="n">df_split_value_counts</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;cat_name&quot;</span><span class="p">]</span>
            <span class="p">)</span>

        <span class="c1"># Move &#39;train&#39; to the left of the table since that is the usual convention.</span>
        <span class="k">if</span> <span class="s2">&quot;train&quot;</span> <span class="ow">in</span> <span class="n">df_value_counts</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="n">move_column_inplace</span><span class="p">(</span><span class="n">df_value_counts</span><span class="p">,</span> <span class="s2">&quot;train&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">df_value_counts</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, PyLabel Project.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>