features_server.html 101 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1

Anthony Larcher's avatar
Anthony Larcher committed
2
3
<!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
4

Anthony Larcher's avatar
Anthony Larcher committed
5
6
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
Anthony Larcher's avatar
Anthony Larcher committed
7
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
doc    
Anthony Larcher committed
9
    <title>features_server &#8212; SIDEKIT  documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
Anthony Larcher's avatar
Anthony Larcher committed
13
14
15
    <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
16
    <link rel="index" title="Index" href="../genindex.html" />
Anthony Larcher's avatar
Anthony Larcher committed
17
    <link rel="search" title="Search" href="../search.html" /> 
Anthony Larcher's avatar
Anthony Larcher committed
18
  </head><body>
Anthony Larcher's avatar
Anthony Larcher committed
19
20
21
22
23
24
25
26
27
    <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
28
29
        <li class="nav-item nav-item-0"><a href="../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
30
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
31
32
33
34
35
    </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>
Anthony Larcher's avatar
Anthony Larcher committed
36
    <div class="searchformwrapper">
Anthony Larcher's avatar
Anthony Larcher committed
37
    <form class="search" action="../search.html" method="get">
Anthony Larcher's avatar
Anthony Larcher committed
38
39
      <input type="text" name="q" />
      <input type="submit" value="Go" />
Anthony Larcher's avatar
Anthony Larcher committed
40
41
42
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
Anthony Larcher's avatar
Anthony Larcher committed
43
    </div>
Anthony Larcher's avatar
Anthony Larcher committed
44
45
46
47
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
48
49
50
51
52

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

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
79
<span class="sd">Copyright 2014-2018 Sylvain Meignier and Anthony Larcher</span>
Anthony Larcher's avatar
Anthony Larcher committed
80
81
82
83

<span class="sd">    :mod:`features_server` provides methods to manage features</span>

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
84
<span class="kn">import</span> <span class="nn">multiprocessing</span>
Anthony Larcher's avatar
Anthony Larcher committed
85
<span class="kn">import</span> <span class="nn">numpy</span>
Anthony Larcher's avatar
Anthony Larcher committed
86
<span class="kn">import</span> <span class="nn">logging</span>
Anthony Larcher's avatar
Anthony Larcher committed
87
88
<span class="kn">import</span> <span class="nn">h5py</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
89
<span class="kn">from</span> <span class="nn">sidekit.frontend.features</span> <span class="k">import</span> <span class="n">pca_dct</span><span class="p">,</span> <span class="n">shifted_delta_cepstral</span><span class="p">,</span> <span class="n">compute_delta</span><span class="p">,</span> <span class="n">framing</span><span class="p">,</span> <span class="n">dct_basis</span>
Anthony Larcher's avatar
Anthony Larcher committed
90
<span class="kn">from</span> <span class="nn">sidekit.frontend.io</span> <span class="k">import</span> <span class="n">read_hdf5_segment</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
91
92
93
<span class="kn">from</span> <span class="nn">sidekit.frontend.vad</span> <span class="k">import</span> <span class="n">label_fusion</span>
<span class="kn">from</span> <span class="nn">sidekit.frontend.normfeat</span> <span class="k">import</span> <span class="n">cms</span><span class="p">,</span> <span class="n">cmvn</span><span class="p">,</span> <span class="n">stg</span><span class="p">,</span> <span class="n">cep_sliding_norm</span><span class="p">,</span> <span class="n">rasta_filt</span>
<span class="kn">from</span> <span class="nn">sidekit.sv_utils</span> <span class="k">import</span> <span class="n">parse_mask</span>
Anthony Larcher's avatar
Anthony Larcher committed
94
95


Anthony Larcher's avatar
doc    
Anthony Larcher committed
96
97
<span class="n">__license__</span> <span class="o">=</span> <span class="s2">&quot;LGPL&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher &amp; Sylvain Meignier&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
98
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2014-2018 Anthony Larcher&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
99
100
101
102
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s1">&#39;reStructuredText&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
103
104


Anthony Larcher's avatar
Anthony Larcher committed
105
<div class="viewcode-block" id="FeaturesServer"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer">[docs]</a><span class="k">class</span> <span class="nc">FeaturesServer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
106
    <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
107
108
109
<span class="sd">    Management of features. FeaturesServer instances load datasets from a HDF5 files</span>
<span class="sd">    (that can be read from disk or produced by a FeaturesExtractor object)</span>
<span class="sd">    Datasets read from one or many files are concatenated and processed</span>
Anthony Larcher's avatar
Anthony Larcher committed
110
111
<span class="sd">    &quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
112
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
                 <span class="n">features_extractor</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">feature_filename_structure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">dataset_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">mask</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">feat_norm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">global_cmvn</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">dct_pca</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">dct_pca_config</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">sdc</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">sdc_config</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">double_delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">delta_filter</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">traps_dct_nb</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">rasta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">keep_all_features</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize a FeaturesServer for two cases:</span>
<span class="sd">        1. each call to load will load datasets from a single file. This mode requires to provide a dataset_list</span>
<span class="sd">        (lists of datasets to load from each file.</span>
<span class="sd">        2. each call to load will load datasets from several files (possibly several datasets from each file)</span>
<span class="sd">        and concatenate them. In this mode, you should provide a FeaturesServer for each source, thus, datasets</span>
<span class="sd">        read from each source can be post-processed independently before being concatenated with others. The dataset</span>
<span class="sd">        resulting from the concatenation from all sources is then post-processed.</span>

<span class="sd">        :param features_extractor: a FeaturesExtractor if required to extract features from audio file</span>
<span class="sd">        if None, data are loaded from an existing HDF5 file</span>
<span class="sd">        :param feature_filename_structure: structure of the filename to use to load HDF5 files</span>
<span class="sd">        :param sources: tuple of sources to load features different files (optional: for the case where datasets</span>
<span class="sd">        are loaded from several files and concatenated.</span>
<span class="sd">        :param dataset_list: string of the form &#39;[&quot;cep&quot;, &quot;fb&quot;, vad&quot;, energy&quot;, &quot;bnf&quot;]&#39; (only when loading datasets</span>
<span class="sd">        from a single file) list of datasets to load.</span>
<span class="sd">        :param mask: string of the form &#39;[1-3,10,15-20]&#39; mask to apply on the concatenated dataset</span>
<span class="sd">        to select specific components. In this example, coefficients 1,2,3,10,15,16,17,18,19,20 are kept</span>
<span class="sd">        In this example,</span>
<span class="sd">        :param feat_norm: tpye of normalization to apply as post-processing</span>
<span class="sd">        :param global_cmvn: boolean, if True, use a global mean and std when normalizing the frames</span>
<span class="sd">        :param dct_pca: if True, add temporal context by using a PCA-DCT approach</span>
<span class="sd">        :param dct_pca_config: configuration of the PCA-DCT, default is (12, 12, none)</span>
<span class="sd">        :param sdc: if True, compute shifted delta cepstra coefficients</span>
<span class="sd">        :param sdc_config: configuration to compute sdc coefficients, default is (1,3,7)</span>
<span class="sd">        :param delta: if True, append the first order derivative</span>
<span class="sd">        :param double_delta: if True, append the second order derivative</span>
<span class="sd">        :param delta_filter: coefficients of the filter used to compute delta coefficients</span>
<span class="sd">        :param context: add a left and right context, default is (0,0)</span>
<span class="sd">        :param traps_dct_nb: number of DCT coefficients to keep when computing TRAP coefficients</span>
<span class="sd">        :param rasta: if True, perform RASTA filtering</span>
<span class="sd">        :param keep_all_features: boolean, if True, keep all features, if False, keep frames according to the vad labels</span>
<span class="sd">        :return:</span>
Anthony Larcher's avatar
Anthony Larcher committed
164
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
165
166
        <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
167
        <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="p">()</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
168
169
170
171
172
173
174
175
176
        <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Post processing options</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span> <span class="o">=</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span> <span class="o">=</span> <span class="kc">False</span>
Anthony Larcher's avatar
Anthony Larcher committed
177
        <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
178
179
        <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span> <span class="o">=</span> <span class="kc">False</span>
Anthony Larcher's avatar
Anthony Larcher committed
180
181
182
        <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</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="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-.</span><span class="mi">25</span><span class="p">,</span> <span class="o">-.</span><span class="mi">5</span><span class="p">,</span> <span class="o">-.</span><span class="mi">25</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">context</span> <span class="o">=</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="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
183
184
        <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span> <span class="o">=</span> <span class="kc">True</span>
Anthony Larcher's avatar
Anthony Larcher committed
185

Anthony Larcher's avatar
doc    
Anthony Larcher committed
186
        <span class="k">if</span> <span class="n">features_extractor</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
187
            <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="o">=</span> <span class="n">features_extractor</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
188
        <span class="k">if</span> <span class="n">feature_filename_structure</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
189
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">feature_filename_structure</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
190
        <span class="k">if</span> <span class="n">sources</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
191
            <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="n">sources</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
192
        <span class="k">if</span> <span class="n">dataset_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
193
            <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="o">=</span> <span class="n">dataset_list</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
194
        <span class="k">if</span> <span class="n">mask</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
195
            <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="n">parse_mask</span><span class="p">(</span><span class="n">mask</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
196
        <span class="k">if</span> <span class="n">feat_norm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
197
            <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">=</span> <span class="n">feat_norm</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
198
        <span class="k">if</span> <span class="n">global_cmvn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
199
            <span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span> <span class="o">=</span> <span class="n">global_cmvn</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
200
        <span class="k">if</span> <span class="n">dct_pca</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
201
            <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span> <span class="o">=</span> <span class="n">dct_pca</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
202
        <span class="k">if</span> <span class="n">dct_pca_config</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
203
            <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span> <span class="o">=</span> <span class="n">dct_pca_config</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
204
        <span class="k">if</span> <span class="n">sdc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
205
            <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span> <span class="o">=</span> <span class="n">sdc</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
206
        <span class="k">if</span> <span class="n">sdc_config</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
207
            <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span> <span class="o">=</span> <span class="n">sdc_config</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
208
        <span class="k">if</span> <span class="n">delta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
209
            <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">delta</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
210
        <span class="k">if</span> <span class="n">double_delta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
211
            <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span> <span class="o">=</span> <span class="n">double_delta</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
212
        <span class="k">if</span> <span class="n">delta_filter</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
213
            <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span> <span class="o">=</span> <span class="n">delta_filter</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
214
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
215
            <span class="bp">self</span><span class="o">.</span><span class="n">context</span> <span class="o">=</span> <span class="n">context</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
216
        <span class="k">if</span> <span class="n">traps_dct_nb</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
217
            <span class="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span> <span class="o">=</span> <span class="n">traps_dct_nb</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
218
        <span class="k">if</span> <span class="n">rasta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
219
            <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span> <span class="o">=</span> <span class="n">rasta</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
220
        <span class="k">if</span> <span class="n">keep_all_features</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
221
            <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span> <span class="o">=</span> <span class="n">keep_all_features</span>
Anthony Larcher's avatar
Anthony Larcher committed
222

Anthony Larcher's avatar
doc    
Anthony Larcher committed
223
224
225
226
        <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">=</span> <span class="s1">&#39;empty&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">=</span> <span class="s1">&#39;empty&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
227
228
229
230

    <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="sd">&quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
231
<span class="sd">        :return: a string to display the object</span>
Anthony Larcher's avatar
Anthony Larcher committed
232
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
233
234
235
236
237
238
239
240
241
        <span class="n">ch</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> show: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n\n</span><span class="s1">&#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">show</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> input_feature_filename: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n\n</span><span class="s1">&#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">input_feature_filename</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> feature_filename_structure: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">feature_filename_structure</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">  </span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">  </span><span class="se">\n\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> Post processing options: </span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> mask: </span><span class="si">{}</span><span class="s1">  </span><span class="se">\n</span><span class="s1">&#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">mask</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> feat_norm: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">feat_norm</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> dct_pca: </span><span class="si">{}</span><span class="s1">, dct_pca_config: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">dct_pca</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
242
                                                               <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
243
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> sdc: </span><span class="si">{}</span><span class="s1">, sdc_config: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">sdc</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
244
                                                       <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
245
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> delta: </span><span class="si">{}</span><span class="s1">, double_delta: </span><span class="si">{}</span><span class="s1">, delta_filter: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">delta</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
246
247
                                                                             <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">,</span>
                                                                             <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
248
249
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> rasta: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">rasta</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> keep_all_features: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#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">keep_all_features</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
250

Anthony Larcher's avatar
Anthony Larcher committed
251
252
        <span class="k">return</span> <span class="n">ch</span>

Anthony Larcher's avatar
Anthony Larcher committed
253
<div class="viewcode-block" id="FeaturesServer.post_processing"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.post_processing">[docs]</a>    <span class="k">def</span> <span class="nf">post_processing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_std</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
254
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
255
256
257
258
259
260
261
262
263
<span class="sd">        After cepstral coefficients, filter banks or bottleneck parameters are computed or read from file</span>
<span class="sd">        post processing is applied.</span>

<span class="sd">        :param feat: the matrix of acoustic parameters to post-process</span>
<span class="sd">        :param label: the VAD labels for the acoustic parameters</span>
<span class="sd">        :param global_mean: vector or mean to use for normalization</span>
<span class="sd">        :param global_std: vector of standard deviation to use for normalization</span>

<span class="sd">        :return: the matrix of acoustic parameters ingand their VAD labels after post-process</span>
Anthony Larcher's avatar
Anthony Larcher committed
264
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
265
266
        <span class="c1"># Apply a mask on the features</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
267
268
            <span class="n">feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mask</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
269
        <span class="c1"># Perform RASTA filtering if required</span>
Anthony Larcher's avatar
Anthony Larcher committed
270
271
272
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">:</span>
            <span class="n">feat</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rasta</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
273
        <span class="c1"># Add temporal context</span>
Anthony Larcher's avatar
Anthony Larcher committed
274
275
276
277
278
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delta_and_2delta</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="n">pca_dct</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</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">dct_pca_config</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
279
            <span class="n">feat</span> <span class="o">=</span> <span class="n">shifted_delta_cepstral</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">p</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">k</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
280

Anthony Larcher's avatar
doc    
Anthony Larcher committed
281
282
        <span class="c1"># Smooth the labels and fuse the channels if more than one.</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Smooth the labels and fuse the channels if more than one&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
283
        <span class="n">label</span> <span class="o">=</span> <span class="n">label_fusion</span><span class="p">(</span><span class="n">label</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
284
        
Anthony Larcher's avatar
doc    
Anthony Larcher committed
285
286
287
        <span class="c1"># Normalize the data</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
288
289
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_normalize</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
290

Anthony Larcher's avatar
doc    
Anthony Larcher committed
291
        <span class="c1"># if not self.keep_all_features, only selected features and labels are kept</span>
Anthony Larcher's avatar
Anthony Larcher committed
292
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
293
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no keep all&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
294
295
            <span class="n">feat</span> <span class="o">=</span> <span class="n">feat</span><span class="p">[</span><span class="n">label</span><span class="p">]</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="n">label</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
296

Anthony Larcher's avatar
doc    
Anthony Larcher committed
297
298
        <span class="k">return</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
299
300
301
302
    <span class="k">def</span> <span class="nf">_mask</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Keep only the MFCC index present in the filter list</span>
<span class="sd">        :param cep: acoustic parameters to filter</span>
Anthony Larcher's avatar
Anthony Larcher committed
303

Anthony Larcher's avatar
Anthony Larcher committed
304
<span class="sd">        :return: return the list of MFCC given by filter list</span>
Anthony Larcher's avatar
Anthony Larcher committed
305
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
306
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
307
308
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;filter list is empty&#39;</span><span class="p">)</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;applied mask&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
309
        <span class="k">return</span> <span class="n">cep</span><span class="p">[:,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
310

Anthony Larcher's avatar
doc    
Anthony Larcher committed
311
    <span class="k">def</span> <span class="nf">_normalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">cep</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_std</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
312
313
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalize acoustic parameters in place</span>
Anthony Larcher's avatar
Anthony Larcher committed
314

Anthony Larcher's avatar
Anthony Larcher committed
315
316
317
318
319
<span class="sd">        :param label: vad labels to use for normalization</span>
<span class="sd">        :param cep: acoustic parameters to normalize</span>
<span class="sd">        :param global_mean: mean vector to use if provided</span>
<span class="sd">        :param global_std: standard deviation vector to use if provided</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
320
321
322
        <span class="c1"># Perform feature normalization on the entire session.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
323
            <span class="k">pass</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
324
325
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cms&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;cms norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
326
            <span class="n">cms</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
327
328
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cmvn&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;cmvn norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
329
            <span class="n">cmvn</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
330
331
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;stg&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;stg norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
332
            <span class="n">stg</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
333
334
335
336
337
338
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cmvn_sliding&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;sliding cmvn norm&#39;</span><span class="p">)</span>
            <span class="n">cep_sliding_norm</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">win</span><span class="o">=</span><span class="mi">301</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cms_sliding&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;sliding cms norm&#39;</span><span class="p">)</span>
            <span class="n">cep_sliding_norm</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">win</span><span class="o">=</span><span class="mi">301</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
339
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
340
            <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Wrong feature normalisation type&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
341

Anthony Larcher's avatar
Anthony Larcher committed
342
    <span class="k">def</span> <span class="nf">_delta_and_2delta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
343
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
344
345
346
347
<span class="sd">        Add deltas and double deltas.</span>
<span class="sd">        :param cep: a matrix of cepstral cefficients</span>

<span class="sd">        :return: the cepstral coefficient stacked with deltas and double deltas</span>
Anthony Larcher's avatar
Anthony Larcher committed
348
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
349
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
350
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;add delta&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
351
352
353
            <span class="n">delta</span> <span class="o">=</span> <span class="n">compute_delta</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">filt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
            <span class="n">cep</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">cep</span><span class="p">,</span> <span class="n">delta</span><span class="p">))</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
354
                <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;add delta delta&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
355
356
357
                <span class="n">double_delta</span> <span class="o">=</span> <span class="n">compute_delta</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="n">filt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
                <span class="n">cep</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">cep</span><span class="p">,</span> <span class="n">double_delta</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">cep</span>
Anthony Larcher's avatar
Anthony Larcher committed
358
359
360
361
362
363
364
365

    <span class="k">def</span> <span class="nf">_rasta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Performs RASTA filtering if required.</span>
<span class="sd">        The two first frames are copied from the third to keep</span>
<span class="sd">        the length consistent</span>
<span class="sd">        !!! if vad is None: label[] is empty</span>

Anthony Larcher's avatar
Anthony Larcher committed
366
367
<span class="sd">        :param cep: the acoustic features to filter</span>
<span class="sd">        :param label: the VAD label</span>
Anthony Larcher's avatar
Anthony Larcher committed
368
369
370
<span class="sd">        :return:</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
371
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;perform RASTA </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
372
373
374
375
376
            <span class="n">cep</span> <span class="o">=</span> <span class="n">rasta_filt</span><span class="p">(</span><span class="n">cep</span><span class="p">)</span>
            <span class="n">cep</span><span class="p">[:</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">cep</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span>
            <span class="n">label</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">cep</span><span class="p">,</span> <span class="n">label</span>

Anthony Larcher's avatar
Anthony Larcher committed
377
<div class="viewcode-block" id="FeaturesServer.get_context"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.get_context">[docs]</a>    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
378
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
379
<span class="sd">        Add a left and right context to each frame.</span>
Anthony Larcher's avatar
Anthony Larcher committed
380
<span class="sd">        First and last frames are duplicated to provide context at the begining and at the end</span>
Anthony Larcher's avatar
Anthony Larcher committed
381

Anthony Larcher's avatar
Anthony Larcher committed
382
383
384
385
<span class="sd">        :param feat: sequence of feature frames (one fame per line)</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>
<span class="sd">        :param label: vad label if available</span>
Anthony Larcher's avatar
Anthony Larcher committed
386

Anthony Larcher's avatar
Anthony Larcher committed
387
<span class="sd">        :return: a sequence of frames with their left and right context</span>
Anthony Larcher's avatar
Anthony Larcher committed
388
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
389
        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
390
            <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
391
        <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
392
393
394
395
396
            <span class="n">stop</span> <span class="o">=</span> <span class="n">feat</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">context_feat</span> <span class="o">=</span> <span class="n">framing</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span>
                      <span class="p">((</span><span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">stop</span> <span class="o">-</span> <span class="n">feat</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">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)),</span>
                       <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)),</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
397
                      <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span><span class="p">)[</span><span class="n">start</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
398
            <span class="n">stop</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">:],</span> <span class="n">win_size</span><span class="o">=</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
399
400
        <span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">))</span> <span class="o">*</span> <span class="n">feat</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
doc    
Anthony Larcher committed
401
        <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
402
            <span class="n">context_label</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
403
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
404
            <span class="n">context_label</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
405

Anthony Larcher's avatar
doc    
Anthony Larcher committed
406
407
        <span class="k">return</span> <span class="n">context_feat</span><span class="p">,</span> <span class="n">context_label</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
408
<div class="viewcode-block" id="FeaturesServer.get_traps"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.get_traps">[docs]</a>    <span class="k">def</span> <span class="nf">get_traps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
409
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
410
411
412
413
414
415
416
417
418
<span class="sd">        Compute TRAP parameters. The input frames are concatenated to add their left and right context,</span>
<span class="sd">        a Hamming window is applied and a DCT reduces the dimensionality of the resulting vector.</span>

<span class="sd">        :param feat: input acoustic parameters to process</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>
<span class="sd">        :param label: vad label if available</span>

<span class="sd">        :return: a sequence of TRAP parameters</span>
Anthony Larcher's avatar
Anthony Larcher committed
419
420
<span class="sd">        &quot;&quot;&quot;</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
421
        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
422
            <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
423
        <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
424
425
426
427
428
429
            <span class="n">stop</span> <span class="o">=</span> <span class="n">feat</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">context_feat</span> <span class="o">=</span> <span class="n">framing</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span>
                      <span class="n">feat</span><span class="p">,</span> 
                      <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span> <span class="o">-</span> <span class="n">feat</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">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)),</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
430
                      <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
431
432
                      <span class="p">)[</span><span class="n">start</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span>
                        <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span><span class="n">stop</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">:],</span>
Anthony Larcher's avatar
Anthony Larcher committed
433
434
            <span class="n">win_size</span><span class="o">=</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
        <span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
435
436
        <span class="n">hamming_dct</span> <span class="o">=</span> <span class="p">(</span><span class="n">dct_basis</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span><span class="p">,</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span>
                       <span class="n">numpy</span><span class="o">.</span><span class="n">hamming</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">T</span>
Anthony Larcher's avatar
Anthony Larcher committed
437

Anthony Larcher's avatar
doc    
Anthony Larcher committed
438
        <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
439
440
            <span class="n">context_label</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
441
            <span class="n">context_label</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
442

Anthony Larcher's avatar
Anthony Larcher committed
443
444
445
        <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span>
            <span class="n">context_feat</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">hamming_dct</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">hamming_dct</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
446
447
        <span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">context_feat</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="mi">1</span><span class="p">),</span> <span class="n">context_label</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
448
<div class="viewcode-block" id="FeaturesServer.load"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.load">[docs]</a>    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</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="mi">0</span><span class="p">,</span> <span class="n">input_feature_filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
449
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
450
<span class="sd">        Depending of the setting of the FeaturesServer, can either:</span>
Anthony Larcher's avatar
Anthony Larcher committed
451

Anthony Larcher's avatar
Anthony Larcher committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<span class="sd">        1. Get the datasets from a single HDF5 file</span>
<span class="sd">            The HDF5 file is loaded from disk or processed on the fly</span>
<span class="sd">            via the FeaturesExtractor of the current FeaturesServer</span>

<span class="sd">        2. Load datasets from multiple input HDF5 files. The datasets are post-processed separately, then concatenated</span>
<span class="sd">            and post-process</span>

<span class="sd">        :param show: ID of the show to load (should be the same for each HDF5 file to read from)</span>
<span class="sd">        :param channel: audio channel index in case the parameters are extracted from an audio file</span>
<span class="sd">        :param input_feature_filename: name of the input feature file in case it is independent from the ID of the show</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
467
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
468

Anthony Larcher's avatar
doc    
Anthony Larcher committed
469
470
471
        <span class="c1"># In case the name of the input file does not include the ID of the show</span>
        <span class="c1"># (i.e., feature_filename_structure does not include {})</span>
        <span class="c1"># self.audio_filename_structure is updated to use the input_feature_filename</span>
Anthony Larcher's avatar
Anthony Larcher committed
472
473
474
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">==</span> <span class="n">show</span> \
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">==</span> <span class="n">input_feature_filename</span>\
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">==</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>  \
Anthony Larcher's avatar
doc    
Anthony Larcher committed
475
476
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;return previous load&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
477
478
479
480
481
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">=</span> <span class="n">show</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">=</span> <span class="n">input_feature_filename</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">=</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
482
        
Anthony Larcher's avatar
doc    
Anthony Larcher committed
483
484
        <span class="n">feature_filename</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">input_feature_filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
485
486
487
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">input_feature_filename</span>
            <span class="n">feature_filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">show</span><span class="p">)</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
488
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
489
490
491
492
493
            <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_features</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">input_feature_filename</span><span class="o">=</span><span class="n">feature_filename</span><span class="p">,</span>
                                                   <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span>
                                                   <span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
494
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
495
            <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Extract tandem features from multiple sources&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
496
497
498
499
            <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_tandem_features</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">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span>
                                                          <span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
500
501
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
502
<div class="viewcode-block" id="FeaturesServer.get_features"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.get_features">[docs]</a>    <span class="k">def</span> <span class="nf">get_features</span><span class="p">(</span><span class="bp">self</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="mi">0</span><span class="p">,</span> <span class="n">input_feature_filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
503
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
504
505
506
507
508
509
510
511
512
513
514
515
<span class="sd">        Get the datasets from a single HDF5 file</span>
<span class="sd">        The HDF5 file is loaded from disk or processed on the fly</span>
<span class="sd">        via the FeaturesExtractor of the current FeaturesServer</span>

<span class="sd">        :param show: ID of the show</span>
<span class="sd">        :param channel: index of the channel to read</span>
<span class="sd">        :param input_feature_filename: name of the input file in case it does not include the ID of the show</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
516
517
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
518
519
520
<span class="sd">        Si le nom du fichier d&#39;entrée est totalement indépendant du show</span>
<span class="sd">        -&gt; si feature_filename_structure ne contient pas &quot;{}&quot;</span>
<span class="sd">        on peut mettre à jour: self.audio_filename_structure pour entrer directement le nom du fichier de feature</span>
Anthony Larcher's avatar
Anthony Larcher committed
521
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
522
        <span class="k">if</span> <span class="n">input_feature_filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
523
524
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">input_feature_filename</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
525
526
527
        <span class="c1"># If no extractor for this source, open hdf5 file and return handler</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">h5f</span> <span class="o">=</span> <span class="n">h5py</span><span class="o">.</span><span class="n">File</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">show</span><span class="p">),</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
528

Anthony Larcher's avatar
doc    
Anthony Larcher committed
529
        <span class="c1"># If an extractor is provided for this source, extract features and return an hdf5 handler</span>
Anthony Larcher's avatar
Anthony Larcher committed
530
531
532
        <span class="k">else</span><span class="p">:</span>
            <span class="n">h5f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="p">,</span> <span class="n">input_audio_filename</span><span class="o">=</span><span class="n">input_feature_filename</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
533
534
535
536
537
538
        <span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">,</span> <span class="n">global_cmvn</span> <span class="o">=</span> <span class="n">read_hdf5_segment</span><span class="p">(</span><span class="n">h5f</span><span class="p">,</span>
                                                                 <span class="n">show</span><span class="p">,</span>
                                                                 <span class="n">dataset_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">,</span>
                                                                 <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span>
                                                                 <span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">,</span>
                                                                 <span class="n">global_cmvn</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
539
        <span class="c1"># Post-process the features and return the features and vad label</span>
Anthony Larcher's avatar
Anthony Larcher committed
540
541
542
543
        <span class="k">if</span> <span class="n">global_cmvn</span><span class="p">:</span>
            <span class="n">feat</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">feat</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
544
545
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="n">feat</span><span class="p">[:,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
546

Anthony Larcher's avatar
doc    
Anthony Larcher committed
547
548
        <span class="k">return</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
549
<div class="viewcode-block" id="FeaturesServer.get_tandem_features"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.get_tandem_features">[docs]</a>    <span class="k">def</span> <span class="nf">get_tandem_features</span><span class="p">(</span><span class="bp">self</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="mi">0</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
550
551
552
553
554
555
556
557
558
559
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Read acoustic parameters from multiple HDF5 files (from disk or extracted by FeaturesExtractor objects).</span>

<span class="sd">        :param show: Id of the show</span>
<span class="sd">        :param channel: index of the channel</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
560
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
561
        <span class="c1"># Each source has its own sources (including subserver) that provides features and label</span>
Anthony Larcher's avatar
Anthony Larcher committed
562
563
        <span class="n">features</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">features_server</span><span class="p">,</span> <span class="n">get_vad</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
564
            <span class="c1"># Get features from this source</span>
Anthony Larcher's avatar
Anthony Larcher committed
565
566
567
568
            <span class="n">feat</span><span class="p">,</span> <span class="n">lbl</span> <span class="o">=</span> <span class="n">features_server</span><span class="o">.</span><span class="n">get_features</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">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">get_vad</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="n">lbl</span>
            <span class="n">features</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
569

Anthony Larcher's avatar
Anthony Larcher committed
570
571
        <span class="n">features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">hstack</span><span class="p">(</span><span class="n">features</span><span class="p">)</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
572
573
574
        <span class="c1"># If the VAD is not required, return all labels at True</span>
        <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">feat</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">dtype</span><span class="o">=</span><span class="s1">&#39;bool&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
575

Anthony Larcher's avatar
doc    
Anthony Larcher committed
576
577
578
        <span class="c1"># Apply the final post-processing on the concatenated features</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
579
<div class="viewcode-block" id="FeaturesServer.mean_std"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.mean_std">[docs]</a>    <span class="k">def</span> <span class="nf">mean_std</span><span class="p">(</span><span class="bp">self</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="mi">0</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
580
581
582
583
584
585
586
587
588
589
590
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the mean and standard deviation vectors for a segment of acoustic features</span>

<span class="sd">        :param show: the ID of the show</span>
<span class="sd">        :param channel: the index of the channel</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: the number of frames, the mean of the frames and their standard deviation</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">feat</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</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">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
        <span class="k">return</span> <span class="n">feat</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">feat</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</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">feat</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="FeaturesServer.stack_features"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.stack_features">[docs]</a>    <span class="k">def</span> <span class="nf">stack_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                       <span class="n">show_list</span><span class="p">,</span>
                       <span class="n">channel_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                       <span class="n">feature_filename_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                       <span class="n">label_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                       <span class="n">start_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                       <span class="n">stop_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Load acoustic features from a list of fils and return them stacked in a 2D-array</span>
<span class="sd">        one line per frame.</span>

<span class="sd">        :param show_list:</span>
<span class="sd">        :param channel_list:</span>
<span class="sd">        :param label_list:</span>
<span class="sd">        :param start_list:</span>
<span class="sd">        :param stop_list:</span>
<span class="sd">        :return:</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">channel_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">channel_list</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">show_list</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">feature_filename_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">feature_filename_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">label_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">label_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">start_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">start_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">stop_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stop_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>

        <span class="n">features_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">load_arg</span>  <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">show_list</span><span class="p">,</span> <span class="n">channel_list</span><span class="p">,</span> <span class="n">feature_filename_list</span><span class="p">,</span> <span class="n">label_list</span><span class="p">,</span> <span class="n">start_list</span><span class="p">,</span> <span class="n">stop_list</span><span class="p">)):</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s2">&quot;load file </span><span class="si">{}</span><span class="s2"> / </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">show_list</span><span class="p">)))</span> 
            <span class="n">features_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="o">*</span><span class="n">load_arg</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">(</span><span class="n">features_list</span><span class="p">)</span></div>


    <span class="k">def</span> <span class="nf">_stack_features_worker</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                               <span class="n">input_queue</span><span class="p">,</span>
                               <span class="n">output_queue</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Load a list of feature files into a Queue object</span>
<span class="sd">        </span>
<span class="sd">        :param input: a Queue object</span>
<span class="sd">        :param output: a list of Queue objects to fill</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">next_task</span> <span class="o">=</span> <span class="n">input_queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">next_task</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="c1"># Poison pill means shutdown</span>
                <span class="n">output_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
                <span class="n">input_queue</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>
                <span class="k">break</span>
            
            <span class="n">output_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="o">*</span><span class="n">next_task</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
            
            <span class="n">input_queue</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>

    <span class="c1">#@profile</span>
<div class="viewcode-block" id="FeaturesServer.stack_features_parallel"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.stack_features_parallel">[docs]</a>    <span class="k">def</span> <span class="nf">stack_features_parallel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>  <span class="c1"># fileList, numThread=1):</span>
                                <span class="n">show_list</span><span class="p">,</span>
                                <span class="n">channel_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">feature_filename_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">label_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">start_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">stop_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">num_thread</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Load a list of feature files and stack them in a unique ndarray. </span>
<span class="sd">        The list of files to load is splited in sublists processed in parallel</span>
<span class="sd">        </span>
<span class="sd">        :param fileList: a list of files to load</span>
<span class="sd">        :param numThread: numbe of thead (optional, default is 1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">channel_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">channel_list</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">show_list</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">feature_filename_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">feature_filename_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">label_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">label_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">start_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">start_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">stop_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stop_list</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">show_list</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;|O&#39;</span><span class="p">)</span>


        <span class="c1">#queue_in = Queue.Queue(maxsize=len(fileList)+numThread)</span>
        <span class="n">queue_in</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">JoinableQueue</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">show_list</span><span class="p">)</span><span class="o">+</span><span class="n">num_thread</span><span class="p">)</span>
        <span class="n">queue_out</span> <span class="o">=</span> <span class="p">[]</span>
        
        <span class="c1"># Start worker processes</span>
        <span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</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">num_thread</span><span class="p">):</span>
            <span class="n">queue_out</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">multiprocessing</span><span class="o">.</span><span class="n">Queue</span><span class="p">())</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_stack_features_worker</span><span class="p">,</span> 
                                        <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">queue_in</span><span class="p">,</span> <span class="n">queue_out</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
            <span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
            <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        
        <span class="c1"># Submit tasks</span>
        <span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">show_list</span><span class="p">,</span> <span class="n">channel_list</span><span class="p">,</span> <span class="n">feature_filename_list</span><span class="p">,</span> <span class="n">label_list</span><span class="p">,</span> <span class="n">start_list</span><span class="p">,</span> <span class="n">stop_list</span><span class="p">):</span>
            <span class="n">queu