statserver.html 331 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Anthony Larcher's avatar
Anthony Larcher committed
3
4


Anthony Larcher's avatar
Anthony Larcher committed
5
6
7
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    
Anthony Larcher's avatar
doc    
Anthony Larcher committed
9
    <title>statserver &mdash; SIDEKIT  documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
13
14
15
16
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
Anthony Larcher's avatar
doc    
Anthony Larcher committed
17
        VERSION:     '',
Anthony Larcher's avatar
Anthony Larcher committed
18
19
20
21
22
23
24
25
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
26
    <link rel="top" title="SIDEKIT  documentation" href="../index.html" />
Anthony Larcher's avatar
Anthony Larcher committed
27
28
29
30
31
32
33
34
35
36
37
38
    <link rel="up" title="Module code" href="index.html" /> 
  </head>
  <body role="document">
    <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>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
39
        <li class="nav-item nav-item-0"><a href="../index.html">SIDEKIT  documentation</a> &raquo;</li>
Anthony Larcher's avatar
Anthony Larcher committed
40
41
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
42
43
44
45
46
47
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
Anthony Larcher's avatar
doc    
Anthony Larcher committed
48
49
      <input type="text" name="q" />
      <input type="submit" value="Go" />
Anthony Larcher's avatar
Anthony Larcher committed
50
51
52
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
53
54
55
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
Anthony Larcher's avatar
Anthony Larcher committed
56
57
58
59
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
60
61
62
63
64

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
Anthony Larcher's avatar
Anthony Larcher committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
            
  <h1>Source code for statserver</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="c">#</span>
<span class="c"># This file is part of SIDEKIT.</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is a python package for speaker verification.</span>
<span class="c"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is a python package for speaker verification.</span>
<span class="c"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c">#    </span>
<span class="c"># SIDEKIT is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU LLesser General Public License as </span>
<span class="c"># published by the Free Software Foundation, either version 3 of the License, </span>
<span class="c"># or (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU Lesser General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU Lesser General Public License</span>
<span class="c"># along with SIDEKIT.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>

Anthony Larcher's avatar
Anthony Larcher committed
90

Anthony Larcher's avatar
Anthony Larcher committed
91
<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
minor    
Anthony Larcher committed
92
<span class="sd">Copyright 2014-2016 Anthony Larcher</span>
Anthony Larcher's avatar
Anthony Larcher committed
93
94
95
96

<span class="sd">:mod:`statserver` provides methods to manage zero and first statistics.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">copy</span>
Anthony Larcher's avatar
Anthony Larcher committed
97
98
<span class="kn">import</span> <span class="nn">ctypes</span>
<span class="kn">import</span> <span class="nn">h5py</span>
Anthony Larcher's avatar
Anthony Larcher committed
99
100
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">multiprocessing</span>
Anthony Larcher's avatar
Anthony Larcher committed
101
102
103
104
105
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">scipy</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">warnings</span>
Anthony Larcher's avatar
Anthony Larcher committed
106

Anthony Larcher's avatar
Anthony Larcher committed
107
108
109
<span class="kn">from</span> <span class="nn">sidekit.bosaris</span> <span class="kn">import</span> <span class="n">IdMap</span>
<span class="kn">from</span> <span class="nn">sidekit.mixture</span> <span class="kn">import</span> <span class="n">Mixture</span>
<span class="kn">from</span> <span class="nn">sidekit.features_server</span> <span class="kn">import</span> <span class="n">FeaturesServer</span>
Anthony Larcher's avatar
Anthony Larcher committed
110
<span class="kn">from</span> <span class="nn">sidekit.sidekit_wrappers</span> <span class="kn">import</span> <span class="n">process_parallel_lists</span><span class="p">,</span> <span class="n">deprecated</span><span class="p">,</span> <span class="n">check_path_existance</span>
Anthony Larcher's avatar
Anthony Larcher committed
111
<span class="kn">import</span> <span class="nn">sidekit.frontend</span>
Anthony Larcher's avatar
Anthony Larcher committed
112
<span class="kn">from</span> <span class="nn">sidekit</span> <span class="kn">import</span> <span class="n">STAT_TYPE</span>
Anthony Larcher's avatar
Anthony Larcher committed
113

Anthony Larcher's avatar
Anthony Larcher committed
114

Anthony Larcher's avatar
Anthony Larcher committed
115
116
117
<span class="n">ct</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span>
<span class="k">if</span> <span class="n">STAT_TYPE</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">:</span>
    <span class="n">ct</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_float</span>
Anthony Larcher's avatar
Anthony Larcher committed
118
119


Anthony Larcher's avatar
Anthony Larcher committed
120
121
122
123
124
125
126
127
128
<span class="n">__license__</span> <span class="o">=</span> <span class="s">&quot;LGPL&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s">&quot;Anthony Larcher&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&quot;Copyright 2014-2016 Anthony Larcher&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s">&#39;reStructuredText&#39;</span>


Anthony Larcher's avatar
doc    
Anthony Larcher committed
129
<span class="k">def</span> <span class="nf">compute_llk</span><span class="p">(</span><span class="n">stat</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">U</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
130
    <span class="c"># Compute Likelihood</span>
Anthony Larcher's avatar
Anthony Larcher committed
131
    <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span>
Anthony Larcher's avatar
Anthony Larcher committed
132
133
    <span class="n">centered_data</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">stat1</span> <span class="o">-</span> <span class="n">stat</span><span class="o">.</span><span class="n">get_mean_stat1</span><span class="p">()</span>
    
Anthony Larcher's avatar
Anthony Larcher committed
134
135
    <span class="k">if</span> <span class="n">sigma</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">sigma_tot</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">V</span><span class="p">,</span> <span class="n">V</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">+</span> <span class="n">sigma</span>
Anthony Larcher's avatar
Anthony Larcher committed
136
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
137
        <span class="n">sigma_tot</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">V</span><span class="p">,</span> <span class="n">V</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">+</span> <span class="n">numpy</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">sigma</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
138
    <span class="k">if</span> <span class="n">U</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
139
        <span class="n">sigma_tot</span> <span class="o">+=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">U</span><span class="p">,</span> <span class="n">U</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
140
    
Anthony Larcher's avatar
Anthony Larcher committed
141
142
143
144
145
146
    <span class="n">E</span><span class="p">,</span> <span class="n">junk</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">eigh</span><span class="p">(</span><span class="n">sigma_tot</span><span class="p">)</span>
    <span class="n">log_det</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">E</span><span class="p">))</span>

    <span class="k">return</span> <span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="n">d</span> <span class="o">*</span> <span class="n">numpy</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">numpy</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">+</span> <span class="n">n</span> <span class="o">*</span> <span class="n">log_det</span> <span class="o">+</span>
                    <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">centered_data</span><span class="p">,</span>
                                                  <span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">sigma_tot</span><span class="p">))</span> <span class="o">*</span> <span class="n">centered_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))))</span>
Anthony Larcher's avatar
Anthony Larcher committed
147
148
149
150
151
152
153


<span class="k">def</span> <span class="nf">sum_log_probabilities</span><span class="p">(</span><span class="n">lp</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Sum log probabilities in a secure manner to avoid extreme values</span>

<span class="sd">    :param lp: ndarray of log-probabilities to sum</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
154
155
156
157
    <span class="n">pp_max</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">lp</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">log_lk</span> <span class="o">=</span> <span class="n">pp_max</span> \
        <span class="o">+</span> <span class="n">numpy</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">exp</span><span class="p">((</span><span class="n">lp</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span> <span class="o">-</span> <span class="n">pp_max</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">()),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
    <span class="n">ind</span> <span class="o">=</span> <span class="o">~</span><span class="n">numpy</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="n">pp_max</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
158
    <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="n">ind</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
159
160
161
        <span class="n">log_lk</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">=</span> <span class="n">pp_max</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span>
    <span class="n">pp</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">exp</span><span class="p">((</span><span class="n">lp</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span> <span class="o">-</span> <span class="n">log_lk</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">())</span>
    <span class="k">return</span> <span class="n">pp</span><span class="p">,</span> <span class="n">log_lk</span>
Anthony Larcher's avatar
Anthony Larcher committed
162
163


Anthony Larcher's avatar
Anthony Larcher committed
164
<span class="nd">@process_parallel_lists</span>
Anthony Larcher's avatar
Anthony Larcher committed
165
166
167
168
169
170
171
172
173
174
175
<span class="k">def</span> <span class="nf">fa_model_loop</span><span class="p">(</span><span class="n">batch_start</span><span class="p">,</span>
                  <span class="n">mini_batch_indices</span><span class="p">,</span>
                  <span class="n">r</span><span class="p">,</span>
                  <span class="n">phi_white</span><span class="p">,</span>
                  <span class="n">phi</span><span class="p">,</span>
                  <span class="n">sigma</span><span class="p">,</span>
                  <span class="n">stat0</span><span class="p">,</span>
                  <span class="n">stat1</span><span class="p">,</span>
                  <span class="n">e_h</span><span class="p">,</span>
                  <span class="n">e_hh</span><span class="p">,</span>
                  <span class="n">num_thread</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
176
    <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
177
178
179
<span class="sd">    :param batch_start: index to start at in the list</span>
<span class="sd">    :param mini_batch_indices: indices of the elements in the list (should start at zero)</span>
<span class="sd">    :param r: rank of the matrix</span>
Anthony Larcher's avatar
Anthony Larcher committed
180
181
182
<span class="sd">    :param phi_white: whitened version of the factor matrix</span>
<span class="sd">    :param phi: non-whitened version of the factor matrix</span>
<span class="sd">    :param sigma: covariance matrix</span>
Anthony Larcher's avatar
Anthony Larcher committed
183
184
<span class="sd">    :param stat0: matrix of zero order statistics</span>
<span class="sd">    :param stat1: matrix of first order statistics</span>
Anthony Larcher's avatar
Anthony Larcher committed
185
186
187
<span class="sd">    :param e_h: accumulator</span>
<span class="sd">    :param e_hh: accumulator</span>
<span class="sd">    :param num_thread: number of parallel process to run</span>
Anthony Larcher's avatar
Anthony Larcher committed
188
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
189
190
    <span class="k">if</span> <span class="n">sigma</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">A</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">sigma</span><span class="p">,</span> <span class="n">phi</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
191

Anthony Larcher's avatar
Anthony Larcher committed
192
193
    <span class="n">tmp</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">phi</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">phi</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
194
    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">mini_batch_indices</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
195
196
        <span class="k">if</span> <span class="n">sigma</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">inv_lambda</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">phi_white</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="n">stat0</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="n">batch_start</span><span class="p">,</span> <span class="p">:])</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">phi_white</span><span class="p">))</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
197
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
198
            <span class="n">inv_lambda</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">stat0</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="n">batch_start</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">A</span> <span class="o">+</span> <span class="n">numpy</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="n">A</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
Anthony Larcher's avatar
Anthony Larcher committed
199

Anthony Larcher's avatar
Anthony Larcher committed
200
201
202
        <span class="n">Aux</span> <span class="o">=</span> <span class="n">phi_white</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">stat1</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="n">batch_start</span><span class="p">,</span> <span class="p">:])</span>
        <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">Aux</span><span class="p">,</span> <span class="n">inv_lambda</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">e_h</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span>
        <span class="n">e_hh</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">inv_lambda</span> <span class="o">+</span> <span class="n">numpy</span><span class="o">.</span><span class="n">outer</span><span class="p">(</span><span class="n">e_h</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="n">e_h</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="n">tmp</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
203

Anthony Larcher's avatar
Anthony Larcher committed
204

Anthony Larcher's avatar
Anthony Larcher committed
205
<span class="nd">@process_parallel_lists</span>
Anthony Larcher's avatar
Anthony Larcher committed
206
<span class="k">def</span> <span class="nf">fa_distribution_loop</span><span class="p">(</span><span class="n">distrib_indices</span><span class="p">,</span> <span class="n">_A</span><span class="p">,</span> <span class="n">stat0</span><span class="p">,</span> <span class="n">batch_start</span><span class="p">,</span> <span class="n">batch_stop</span><span class="p">,</span> <span class="n">e_hh</span><span class="p">,</span> <span class="n">num_thread</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
207
    <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
208
209
210
211
212
<span class="sd">    :param distrib_indices: indices of the distributions to iterate on</span>
<span class="sd">    :param _A: accumulator</span>
<span class="sd">    :param stat0: matrix of zero order statistics</span>
<span class="sd">    :param batch_start: index of the first session to process</span>
<span class="sd">    :param batch_stop: index of the last session to process</span>
Anthony Larcher's avatar
Anthony Larcher committed
213
214
<span class="sd">    :param e_hh: accumulator</span>
<span class="sd">    :param num_thread: number of parallel process to run</span>
Anthony Larcher's avatar
Anthony Larcher committed
215
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
216
    <span class="n">tmp</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">e_hh</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">e_hh</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
217
    <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">distrib_indices</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
218
219
220
221
        <span class="n">_A</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s">&#39;ijk,i-&gt;jk&#39;</span><span class="p">,</span> <span class="n">e_hh</span><span class="p">,</span> <span class="n">stat0</span><span class="p">[</span><span class="n">batch_start</span><span class="p">:</span><span class="n">batch_stop</span><span class="p">,</span> <span class="n">c</span><span class="p">],</span> <span class="n">out</span><span class="o">=</span><span class="n">tmp</span><span class="p">)</span>
        <span class="c"># The line abov is equivalent to the two lines below:</span>
        <span class="c"># tmp = (E_hh.T * stat0[batch_start:batch_stop, c]).T</span>
        <span class="c"># _A[c] += numpy.sum(tmp, axis=0)</span>
Anthony Larcher's avatar
Anthony Larcher committed
222
223


Anthony Larcher's avatar
Anthony Larcher committed
224
225
226
<span class="k">def</span> <span class="nf">load_existing_statistics_hdf5</span><span class="p">(</span><span class="n">statserver</span><span class="p">,</span> <span class="n">statserver_file_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Load required statistics into the StatServer by reading from a file</span>
<span class="sd">        in hdf5 format.</span>
Anthony Larcher's avatar
Anthony Larcher committed
227

Anthony Larcher's avatar
Anthony Larcher committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<span class="sd">    :param statserver: sidekit.StatServer to fill</span>
<span class="sd">    :param statserver_file_name: name of the file to read from</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</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">statserver_file_name</span><span class="p">),</span> <span class="s">&quot;statserver_file_name does not exist&quot;</span>

    <span class="c"># Load the StatServer</span>
    <span class="n">ss</span> <span class="o">=</span> <span class="n">StatServer</span><span class="p">(</span><span class="n">statserver_file_name</span><span class="p">)</span>

    <span class="c"># Check dimension consistency with current Stat_Server</span>
    <span class="n">ok</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">if</span> <span class="n">statserver</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">ok</span> <span class="o">&amp;=</span> <span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">statserver</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">ok</span> <span class="o">&amp;=</span> <span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">statserver</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">statserver</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">statserver</span><span class="o">.</span> <span class="n">modelset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
        <span class="n">statserver</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">statserver</span><span class="o">.</span> <span class="n">modelset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">ok</span><span class="p">:</span>
        <span class="c"># For each segment, load statistics if they exist</span>
        <span class="c"># Get the lists of existing segments</span>
        <span class="n">seg_idx</span> <span class="o">=</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="nb">range</span><span class="p">(</span><span class="n">statserver</span><span class="o">.</span> <span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">if</span> <span class="n">statserver</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">in</span> <span class="n">ss</span><span class="o">.</span><span class="n">segset</span><span class="p">]</span>
        <span class="n">stat_idx</span> <span class="o">=</span> <span class="p">[</span><span class="n">numpy</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">segset</span> <span class="o">==</span> <span class="n">seg</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">for</span> <span class="n">seg</span> <span class="ow">in</span> <span class="n">statserver</span><span class="o">.</span><span class="n">segset</span> <span class="k">if</span> <span class="n">seg</span> <span class="ow">in</span> <span class="n">ss</span><span class="o">.</span><span class="n">segset</span><span class="p">]</span>

        <span class="c"># Copy statistics</span>
        <span class="n">statserver</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">seg_idx</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">stat_idx</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">statserver</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">seg_idx</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">stat_idx</span><span class="p">,</span> <span class="p">:]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Mismatched statistic dimensions&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
256

Anthony Larcher's avatar
Anthony Larcher committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

<div class="viewcode-block" id="StatServer"><a class="viewcode-back" href="../statserver.html#statserver.StatServer">[docs]</a><span class="k">class</span> <span class="nc">StatServer</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;A class for statistic storage and processing</span>

<span class="sd">    :attr modelset: list of model IDs for each session as an array of strings</span>
<span class="sd">    :attr segset: the list of session IDs as an array of strings</span>
<span class="sd">    :attr start: index of the first frame of the segment</span>
<span class="sd">    :attr stop: index of the last frame of the segment</span>
<span class="sd">    :attr stat0: a ndarray of float64. Each line contains 0-order statistics </span>
<span class="sd">        from the corresponding session</span>
<span class="sd">    :attr stat1: a ndarray of float64. Each line contains 1-order statistics </span>
<span class="sd">        from the corresponding session</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
272
    <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">statserver_file_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">ubm</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
273
274
275
        <span class="sd">&quot;&quot;&quot;Initialize an empty StatServer or load a StatServer from an existing</span>
<span class="sd">        file.</span>

Anthony Larcher's avatar
Anthony Larcher committed
276
<span class="sd">        :param statserver_file_name: name of the file to read from. If filename</span>
Anthony Larcher's avatar
Anthony Larcher committed
277
278
279
280
<span class="sd">                is an empty string, the StatServer is initialized empty. </span>
<span class="sd">                If filename is an IdMap object, the StatServer is initialized </span>
<span class="sd">                to match the structure of the IdMap.</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
281
282
283
284
285
286
287
288
        <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&quot;|O&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&quot;|O&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&quot;|O&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&quot;|O&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">statserver_file_name</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
289
290
            <span class="k">pass</span>
        <span class="c"># initialize</span>
Anthony Larcher's avatar
Anthony Larcher committed
291
292
293
294
295
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">statserver_file_name</span><span class="p">,</span> <span class="n">IdMap</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">statserver_file_name</span><span class="o">.</span><span class="n">leftids</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">statserver_file_name</span><span class="o">.</span><span class="n">rightids</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">statserver_file_name</span><span class="o">.</span><span class="n">start</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">statserver_file_name</span><span class="o">.</span><span class="n">stop</span>
Anthony Larcher's avatar
Anthony Larcher committed
296
297
298

            <span class="k">if</span> <span class="n">ubm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>            
                <span class="c"># Initialize stat0 and stat1 given the size of the UBM</span>
Anthony Larcher's avatar
Anthony Larcher committed
299
300
                <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span> <span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">distrib_nb</span><span class="p">()),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span> <span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">sv_size</span><span class="p">()),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
301
302
303
            
                <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&#39;ignore&#39;</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span> 
Anthony Larcher's avatar
Anthony Larcher committed
304
305
                    <span class="n">tmp_stat0</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Array</span><span class="p">(</span><span class="n">ct</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ctypeslib</span><span class="o">.</span><span class="n">as_array</span><span class="p">(</span><span class="n">tmp_stat0</span><span class="o">.</span><span class="n">get_obj</span><span class="p">())</span>
Anthony Larcher's avatar
Anthony Larcher committed
306
307
                    <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">distrib_nb</span><span class="p">())</span>
            
Anthony Larcher's avatar
Anthony Larcher committed
308
309
                    <span class="n">tmp_stat1</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Array</span><span class="p">(</span><span class="n">ct</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ctypeslib</span><span class="o">.</span><span class="n">as_array</span><span class="p">(</span><span class="n">tmp_stat1</span><span class="o">.</span><span class="n">get_obj</span><span class="p">())</span>
Anthony Larcher's avatar
Anthony Larcher committed
310
311
                    <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">sv_size</span><span class="p">())</span>

Anthony Larcher's avatar
Anthony Larcher committed
312
        <span class="c"># initialize by reading an existing StatServer</span>
Anthony Larcher's avatar
Anthony Larcher committed
313
314
315
316
317
318
319
320
        <span class="k">else</span><span class="p">:</span>
            <span class="n">tmp</span> <span class="o">=</span> <span class="n">StatServer</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">statserver_file_name</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">modelset</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">segset</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">start</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">stop</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">stat0</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">stat1</span>
Anthony Larcher's avatar
Anthony Larcher committed
321

Anthony Larcher's avatar
Anthony Larcher committed
322
323
324
325
326
327
328
329
330
331
332
    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">ch</span> <span class="o">=</span> <span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">30</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;modelset: &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;segset: &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;seg start:&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;seg stop:&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;stat0:&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;stat1:&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">30</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="k">return</span> <span class="n">ch</span>

Anthony Larcher's avatar
Anthony Larcher committed
333
334
335
336
337
338
339
340
341
342
343
344
<div class="viewcode-block" id="StatServer.validate"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.validate">[docs]</a>    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">warn</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Validate the structure and content of the StatServer. </span>
<span class="sd">        Check consistency between the different attributes of </span>
<span class="sd">        the StatServer:</span>
<span class="sd">        - dimension of the modelset</span>
<span class="sd">        - dimension of the segset</span>
<span class="sd">        - length of the modelset and segset</span>
<span class="sd">        - consistency of stat0 and stat1</span>
<span class="sd">        </span>
<span class="sd">        :param warn: bollean optional, if True, display possible warning</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ok</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span> \
Anthony Larcher's avatar
Anthony Larcher committed
345
            <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> \
Anthony Larcher's avatar
Anthony Larcher committed
346
            <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> \
Anthony Larcher's avatar
Anthony Larcher committed
347
            <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
Anthony Larcher's avatar
Anthony Larcher committed
348

Anthony Larcher's avatar
Anthony Larcher committed
349
        <span class="k">if</span> <span class="n">warn</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">)</span><span class="o">.</span><span class="n">shape</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
350
                <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&#39;Duplicated segments in StatServer&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
351
352
        <span class="k">return</span> <span class="n">ok</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<div class="viewcode-block" id="StatServer.merge"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.merge">[docs]</a>    <span class="k">def</span> <span class="nf">merge</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Merge a variable number of StatServers into one.</span>
<span class="sd">        If a pair segmentID is duplicated, keep ony one</span>
<span class="sd">        of them and raises a WARNING</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">line_number</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">ss</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
            <span class="k">assert</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">ss</span><span class="p">,</span> <span class="n">sidekit</span><span class="o">.</span><span class="n">StatServer</span><span class="p">)</span> <span class="ow">and</span> <span class="n">ss</span><span class="o">.</span><span class="n">validate</span><span class="p">()),</span> <span class="s">&quot;Arguments must be proper StatServers&quot;</span>
            
            <span class="c"># Check consistency of StatServers (dimension of the stat0 and stat1)</span>
            <span class="k">if</span> <span class="n">idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">dim_stat0</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">dim_stat1</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>            
            <span class="k">else</span><span class="p">:</span>
                <span class="k">assert</span><span class="p">(</span><span class="n">dim_stat0</span> <span class="o">==</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">and</span> 
                       <span class="n">dim_stat1</span> <span class="o">==</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="s">&quot;Stat dimensions are not consistent&quot;</span>
    
            <span class="n">line_number</span> <span class="o">+=</span> <span class="n">ss</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    
        <span class="c"># Get a list of unique modelID-segmentID    </span>
Anthony Larcher's avatar
Anthony Larcher committed
374
        <span class="n">id_list</span> <span class="o">=</span> <span class="p">[]</span>
Anthony Larcher's avatar
Anthony Larcher committed
375
        <span class="k">for</span> <span class="n">ss</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
376
377
378
            <span class="n">id_list</span> <span class="o">+=</span> <span class="nb">list</span><span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">segset</span><span class="p">)</span>
        <span class="n">id_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">id_list</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">line_number</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
379
380
381
382
            <span class="k">print</span><span class="p">(</span><span class="s">&quot;WARNING: duplicated segmentID in input StatServers&quot;</span><span class="p">)</span>
        
        <span class="c"># Initialize the new StatServer with unique set of segmentID</span>
        <span class="n">new_stat_server</span> <span class="o">=</span> <span class="n">sidekit</span><span class="o">.</span><span class="n">StatServer</span><span class="p">()</span>
Anthony Larcher's avatar
Anthony Larcher committed
383
384
385
386
387
388
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&#39;object&#39;</span><span class="p">)</span>
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">id_set</span><span class="p">))</span>
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">),</span> <span class="s">&#39;object&#39;</span><span class="p">)</span>
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s">&#39;object&#39;</span><span class="p">)</span>
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">),</span> <span class="n">dim_stat0</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
        <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">id_set</span><span class="p">),</span> <span class="n">dim_stat1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
389
390
391
        
        <span class="k">for</span> <span class="n">ss</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">segment</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">segset</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
392
                <span class="n">new_idx</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">argwhere</span><span class="p">(</span><span class="n">new_stat_server</span><span class="o">.</span><span class="n">segset</span> <span class="o">==</span> <span class="n">segment</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
393
394
395
396
397
398
399
                <span class="n">new_stat_server</span><span class="o">.</span><span class="n">modelset</span><span class="p">[</span><span class="n">new_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">modelset</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
                <span class="n">new_stat_server</span><span class="o">.</span><span class="n">start</span><span class="p">[</span><span class="n">new_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">start</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
                <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">new_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
                <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">new_idx</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">idx</span><span class="p">,</span> <span class="p">:]</span>
                <span class="n">new_stat_server</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">new_idx</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">idx</span><span class="p">,</span> <span class="p">:]</span>
                
        <span class="k">assert</span><span class="p">(</span><span class="n">new_stat_server</span><span class="o">.</span><span class="n">validate</span><span class="p">()),</span> <span class="s">&quot;Problem in StatServer Merging&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
400
401
        <span class="k">return</span> <span class="n">new_stat_server</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
402
403
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="StatServer.read"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.read">[docs]</a>    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="n">statserver_file_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
404
405
        <span class="sd">&quot;&quot;&quot;Read StatServer in hdf5 format</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
406
<span class="sd">        :param statserver_file_name: name of the file to read from</span>
407
<span class="sd">        :param prefix: prefixe of the dataset to read from in HDF5 file</span>
Anthony Larcher's avatar
Anthony Larcher committed
408
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
409
410
411
412
        <span class="k">with</span> <span class="n">h5py</span><span class="o">.</span><span class="n">File</span><span class="p">(</span><span class="n">statserver_file_name</span><span class="p">,</span> <span class="s">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">statserver</span> <span class="o">=</span> <span class="n">StatServer</span><span class="p">()</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;modelset&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;segset&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
Anthony Larcher's avatar
Anthony Larcher committed
413
414
415

            <span class="c"># if running python 3, need a conversion to unicode</span>
            <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
416
417
                <span class="n">statserver</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="n">statserver</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;U&#39;</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
                <span class="n">statserver</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="n">statserver</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;U&#39;</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
418

Anthony Larcher's avatar
Anthony Larcher committed
419
420
421
422
423
424
            <span class="n">tmpstart</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;start&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
            <span class="n">tmpstop</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stop&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;start&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s">&#39;|O&#39;</span><span class="p">)</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stop&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s">&#39;|O&#39;</span><span class="p">)</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">start</span><span class="p">[</span><span class="n">tmpstart</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmpstart</span><span class="p">[</span><span class="n">tmpstart</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">tmpstop</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmpstop</span><span class="p">[</span><span class="n">tmpstop</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
425

Anthony Larcher's avatar
Anthony Larcher committed
426
427
            <span class="n">statserver</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stat0&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
            <span class="n">statserver</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stat1&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
428

Anthony Larcher's avatar
Anthony Larcher committed
429
            <span class="k">assert</span> <span class="n">statserver</span><span class="o">.</span><span class="n">validate</span><span class="p">(),</span> <span class="s">&quot;Error: wrong StatServer format&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
430
431
            <span class="k">return</span> <span class="n">statserver</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
432
    <span class="nd">@check_path_existance</span>
Anthony Larcher's avatar
Anthony Larcher committed
433
    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output_file_name</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
434
435
        <span class="sd">&quot;&quot;&quot;Write the StatServer to disk in hdf5 format.</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
436
437
<span class="sd">        :param output_file_name: name of the file to write in.</span>
<span class="sd">        :param prefix:</span>
Anthony Larcher's avatar
Anthony Larcher committed
438
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
439
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">validate</span><span class="p">(),</span> <span class="s">&quot;Error: wrong StatServer format&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
440
        <span class="k">with</span> <span class="n">h5py</span><span class="o">.</span><span class="n">File</span><span class="p">(</span><span class="n">output_file_name</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
441

Anthony Larcher's avatar
Anthony Larcher committed
442
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;modelset&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;S&#39;</span><span class="p">),</span>
Anthony Larcher's avatar
Anthony Larcher committed
443
444
445
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
446
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;segset&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;S&#39;</span><span class="p">),</span>
Anthony Larcher's avatar
Anthony Larcher committed
447
448
449
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
450
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stat0&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
451
452
453
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
454
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stat1&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
455
456
457
458
459
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

            <span class="n">start</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
460
            <span class="n">start</span><span class="p">[</span><span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;float&#39;</span><span class="p">))]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
461
462
463
            <span class="n">start</span> <span class="o">=</span> <span class="n">start</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int8&#39;</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

            <span class="n">stop</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
464
            <span class="n">stop</span><span class="p">[</span><span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;float&#39;</span><span class="p">))]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
465
466
            <span class="n">stop</span> <span class="o">=</span> <span class="n">stop</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int8&#39;</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
467
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;start&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">start</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
468
469
470
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
471
            <span class="n">f</span><span class="o">.</span><span class="n">create_dataset</span><span class="p">(</span><span class="n">prefix</span><span class="o">+</span><span class="s">&quot;stop&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">stop</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
472
473
474
                             <span class="n">maxshape</span><span class="o">=</span><span class="p">(</span><span class="bp">None</span><span class="p">,),</span>
                             <span class="n">compression</span><span class="o">=</span><span class="s">&quot;gzip&quot;</span><span class="p">,</span>
                             <span class="n">fletcher32</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
475

Anthony Larcher's avatar
Anthony Larcher committed
476
<div class="viewcode-block" id="StatServer.get_model_stat0"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_stat0">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_stat0</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_id</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
477
478
        <span class="sd">&quot;&quot;&quot;Return zero-order statistics of a given model</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
479
<span class="sd">        :param mod_id: ID of the model which stat0 will be returned</span>
Anthony Larcher's avatar
Anthony Larcher committed
480
481
482
<span class="sd">          </span>
<span class="sd">        :return: a matrix of zero-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
483
        <span class="n">S</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span> <span class="n">modelset</span> <span class="o">==</span> <span class="n">mod_id</span><span class="p">,</span> <span class="p">:]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
484
485
        <span class="k">return</span> <span class="n">S</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
486
<div class="viewcode-block" id="StatServer.get_model_stat1"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_stat1">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_stat1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_id</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
487
488
        <span class="sd">&quot;&quot;&quot;Return first-order statistics of a given model</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
489
<span class="sd">        :param mod_id: string, ID of the model which stat1 will be returned</span>
Anthony Larcher's avatar
Anthony Larcher committed
490
491
492
<span class="sd">          </span>
<span class="sd">        :return: a matrix of first-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
493
494
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">mod_id</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
495
<div class="viewcode-block" id="StatServer.get_model_stat0_by_index"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_stat0_by_index">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_stat0_by_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_idx</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
496
497
        <span class="sd">&quot;&quot;&quot;Return zero-order statistics of model number modIDX</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
498
<span class="sd">        :param mod_idx: integer, index of the unique model which stat0 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
499
500
501
502
<span class="sd">            returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of zero-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
503
504
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="p">)[</span><span class="n">mod_idx</span><span class="p">]),</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
505
<div class="viewcode-block" id="StatServer.get_model_stat1_by_index"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_stat1_by_index">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_stat1_by_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_idx</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
506
507
        <span class="sd">&quot;&quot;&quot;Return first-order statistics of model number modIDX</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
508
<span class="sd">        :param mod_idx: integer, index of the unique model which stat1 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
509
510
511
512
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of first-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
513
        <span class="n">selectSeg</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="p">)[</span><span class="n">mod_idx</span><span class="p">])</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
514
515
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">selectSeg</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
516
<div class="viewcode-block" id="StatServer.get_segment_stat0"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_segment_stat0">[docs]</a>    <span class="k">def</span> <span class="nf">get_segment_stat0</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seg_id</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
517
518
        <span class="sd">&quot;&quot;&quot;Return zero-order statistics of segment which ID is segID</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
519
<span class="sd">        :param seg_id: string, ID of the segment which stat0 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
520
521
522
523
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of zero-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
524
525
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">==</span> <span class="n">seg_id</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
526
<div class="viewcode-block" id="StatServer.get_segment_stat1"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_segment_stat1">[docs]</a>    <span class="k">def</span> <span class="nf">get_segment_stat1</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seg_id</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
527
528
        <span class="sd">&quot;&quot;&quot;Return first-order statistics of segment which ID is segID</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
529
<span class="sd">        :param seg_id: string, ID of the segment which stat1 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
530
531
532
533
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of first-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
534
535
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">==</span> <span class="n">seg_id</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
536
<div class="viewcode-block" id="StatServer.get_segment_stat0_by_index"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_segment_stat0_by_index">[docs]</a>    <span class="k">def</span> <span class="nf">get_segment_stat0_by_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seg_idx</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
537
538
        <span class="sd">&quot;&quot;&quot;Return zero-order statistics of segment number segIDX</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
539
<span class="sd">        :param seg_idx: integer, index of the unique segment which stat0 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
540
541
542
543
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of zero-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
544
545
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">seg_idx</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
546
<div class="viewcode-block" id="StatServer.get_segment_stat1_by_index"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_segment_stat1_by_index">[docs]</a>    <span class="k">def</span> <span class="nf">get_segment_stat1_by_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seg_idx</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
547
548
        <span class="sd">&quot;&quot;&quot;Return first-order statistics of segment number segIDX</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
549
<span class="sd">        :param seg_idx: integer, index of the unique segment which stat1 will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
550
551
552
553
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a matrix of first-order statistics as a ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
554
555
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">seg_idx</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
556
<div class="viewcode-block" id="StatServer.get_model_segments"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_segments">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_segments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_id</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
557
558
        <span class="sd">&quot;&quot;&quot;Return the list of segments belonging to model modID</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
559
<span class="sd">        :param mod_id: string, ID of the model which belonging segments will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
560
561
562
563
<span class="sd">              returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a list of segments belonging to the model</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
564
565
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">mod_id</span><span class="p">]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
566
<div class="viewcode-block" id="StatServer.get_model_segments_by_index"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.get_model_segments_by_index">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_segments_by_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mod_idx</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
567
568
        <span class="sd">&quot;&quot;&quot;Return the list of segments belonging to model number modIDX</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
569
<span class="sd">        :param mod_idx: index of the model which list of segments will be</span>
Anthony Larcher's avatar
Anthony Larcher committed
570
571
572
573
<span class="sd">            returned</span>
<span class="sd">        </span>
<span class="sd">        :return: a list of segments belonging to the model</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
574
        <span class="n">select_seg</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="p">)[</span><span class="n">mod_idx</span><span class="p">])</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
575
576
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">select_seg</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
577
<div class="viewcode-block" id="StatServer.align_segments"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.align_segments">[docs]</a>    <span class="k">def</span> <span class="nf">align_segments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">segment_list</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
578
579
580
581
        <span class="sd">&quot;&quot;&quot;Align segments of the current StatServer to match a list of segment </span>
<span class="sd">            provided as input parameter. The size of the StatServer might be </span>
<span class="sd">            reduced to match the input list of segments.</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
582
<span class="sd">        :param segment_list: ndarray of strings, list of segments to match</span>
Anthony Larcher's avatar
Anthony Larcher committed
583
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
584
        <span class="n">indx</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">numpy</span><span class="o">.</span><span class="n">argwhere</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">==</span> <span class="n">v</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">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">segment_list</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
585
586
587
588
589
        <span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">indx</span><span class="p">,</span> <span class="p">:]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
590
591
        <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">indx</span><span class="p">,</span> <span class="p">:]</span>
        </div>
Anthony Larcher's avatar
Anthony Larcher committed
592
<div class="viewcode-block" id="StatServer.align_models"><a class="viewcode-back" href="../statserver.html#statserver.StatServer.align_models">[docs]</a>    <span class="k">def</span> <span class="nf">align_models</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_list</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
593
594
595
596
        <span class="sd">&quot;&quot;&quot;Align models of the current StatServer to match a list of models </span>
<span class="sd">            provided as input parameter. The size of the StatServer might be </span>
<span class="sd">            reduced to match the input list of models.</span>
<span class="sd">        </span>
Anthony Larcher's avatar
Anthony Larcher committed
597
<span class="sd">        :param model_list: ndarray of strings, list of models to match</span>
Anthony Larcher's avatar
Anthony Larcher committed
598
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
599
        <span class="n">indx</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">numpy</span><span class="o">.</span><span class="n">argwhere</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">==</span> <span class="n">v</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">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">model_list</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
600
601
602
603
604
        <span class="bp">self</span><span class="o">.</span><span class="n">segset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelset</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">indx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="p">[</span><span class="n">indx</span><span class="p">,</span> <span class="p">:]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
605
606
        <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="p">[</span><span class="n">indx</span><span class="p">,</span> <span class="p">:]</span>
</div>
Anthony Larcher's avatar
Anthony Larcher committed
607
    <span class="nd">@process_parallel_lists</span>
Anthony Larcher's avatar
Anthony Larcher committed
608
    <span class="k">def</span> <span class="nf">accumulate_stat</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ubm</span><span class="p">,</span> <span class="n">feature_server</span><span class="p">,</span> <span class="n">seg_indices</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">channel_extension</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;_b&quot;</span><span class="p">),</span> <span class="n">num_thread</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
609
610
611
612
613
        <span class="sd">&quot;&quot;&quot;Compute statistics for a list of sessions which indices </span>
<span class="sd">            are given in segIndices.</span>
<span class="sd">        </span>
<span class="sd">        :param ubm: a Mixture object used to compute the statistics</span>
<span class="sd">        :param feature_server: featureServer object</span>
Anthony Larcher's avatar
Anthony Larcher committed
614
<span class="sd">        :param seg_indices: list of indices of segments to process</span>
Anthony Larcher's avatar
Anthony Larcher committed
615
<span class="sd">              if segIndices is an empty list, process all segments.</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
616
617
<span class="sd">        :param channel_extension: tuple of strings, extension of first and second channel for stereo files, default</span>
<span class="sd">        is (&quot;&quot;, &quot;_b&quot;)</span>
Anthony Larcher's avatar
Anthony Larcher committed
618
<span class="sd">        :param num_thread: number of parallel process to run</span>
Anthony Larcher's avatar
Anthony Larcher committed
619
620
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ubm</span><span class="p">,</span> <span class="n">Mixture</span><span class="p">),</span> <span class="s">&#39;First parameter has to be a Mixture&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
621
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">feature_server</span><span class="p">,</span> <span class="n">FeaturesServer</span><span class="p">),</span> <span class="s">&#39;Second parameter has to be a FeaturesServer&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
622
623
624
625
626
627

        <span class="k">if</span> <span class="p">(</span><span class="n">seg_indices</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">)</span> \
                <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> \
                <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</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">stat0</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">distrib_nb</span><span class="p">()),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stat1</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ubm</span><span class="o">.</span><span class="n">sv_size</span><span class="p">()),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">STAT_TYPE</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
628
            <span class="n">seg_indices</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
629
630
        <span class="n">feature_server</span><span class="o">.</span><span class="n">keep_all_features</span> <span class="o">=</span> <span class="bp">True</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
631
        <span class="k">for</span> <span class="n">count</span><span class="p">,</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">seg_indices</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
632
633
634
635
636
637
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&#39;Compute statistics for {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">idx</span><span class="p">]))</span>

            <span class="n">show</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">segset</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>

            <span class="c"># If using a FeaturesExtractor, get the channel number by checking the extension of the show</span>
            <span class="n">channel</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
Anthony Larcher committed
638
            <span class="k">if</span> <span class="n">feature_server</span><span class="o">.</span><span class="n">features_extractor</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">show</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">channel_extension</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
Anthony Larcher's avatar
Anthony Larcher committed
639
                <span class="n">channel</span> <span class="o">=</span> <span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
640
641
            <span class="n">show</span> <span class="o">=</span> <span class="n">show</span><span class="p">[:</span><span class="n">show</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="n">channel_extension</span><span class="p">[</span><span class="n">channel</span><span class="p">])]</span>

Anthony Larcher's avatar
Anthony Larcher committed
642
643
            <span class="n">cep</span><span class="p">,</span> <span class="n">vad</span> <span class="o">=</span> <span class="n">feature_server</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="n">channel</span><span class="p">)</span>
            <span class="n">stop</span> <span class="o">=</span> <span class="n">vad</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="n">vad</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
Anthony Larcher's avatar