From 42def95dc4a426f96904d260457544aa21282216 Mon Sep 17 00:00:00 2001
From: "valentin.emiya" <valentin.emiya@lif.univ-mrs.fr>
Date: Thu, 4 Jun 2020 12:34:24 +0200
Subject: [PATCH] nb and test create_subregions

---
 python/doc/conf.py                            |  3 ++
 python/doc/tutorials.rst                      |  3 +-
 python/tffpy/create_subregions.py             | 32 +++++++++++-------
 .../experiments/tests/test_exp_solve_tff.py   |  2 ++
 python/tffpy/tests/test_create_subregions.py  | 33 +++++++++++++++++++
 python/tffpy/tests/test_tf_fading.py          |  2 +-
 6 files changed, 61 insertions(+), 14 deletions(-)
 create mode 100644 python/tffpy/tests/test_create_subregions.py

diff --git a/python/doc/conf.py b/python/doc/conf.py
index 91b6cad..41f0359 100755
--- a/python/doc/conf.py
+++ b/python/doc/conf.py
@@ -281,6 +281,9 @@ intersphinx_mapping = {
 # Allow errors in notebook
 nbsphinx_allow_errors = True
 
+# Timeout in notebook
+nbsphinx_timeout = 120
+
 # Do not show class members
 numpydoc_show_class_members = False
 
diff --git a/python/doc/tutorials.rst b/python/doc/tutorials.rst
index 619e55b..655e76b 100755
--- a/python/doc/tutorials.rst
+++ b/python/doc/tutorials.rst
@@ -4,5 +4,6 @@ Tutorials and demonstrations
 .. toctree::
     :maxdepth: 1
 
-    _notebooks/baseline_interpolation_solver.ipynb
     _notebooks/mask_energy_estimation.ipynb
+    _notebooks/create_subregions.ipynb
+    _notebooks/baseline_interpolation_solver.ipynb
diff --git a/python/tffpy/create_subregions.py b/python/tffpy/create_subregions.py
index 6265fa6..e9747bd 100644
--- a/python/tffpy/create_subregions.py
+++ b/python/tffpy/create_subregions.py
@@ -4,6 +4,8 @@
 .. moduleauthor:: Valentin Emiya
 """
 # TODO check if eigs(, 1) can be replaced by Halko to run faster
+from pathlib import Path
+import warnings
 import numpy as np
 import matplotlib.pyplot as plt
 from scipy.ndimage import label
@@ -50,6 +52,9 @@ def create_subregions(mask_bool, dgt_params, signal_params, tol,
                              dgt_params=dgt_params, signal_params=signal_params)
 
     if fig_dir is not None:
+        fig_dir = Path(fig_dir)
+        fig_dir.mkdir(parents=True, exist_ok=True)
+
         plt.figure()
         plot_mask(mask=mask_labeled, hop=dgt_params['hop'],
                   n_bins=dgt_params['n_bins'], fs=signal_params['fs'])
@@ -59,12 +64,11 @@ def create_subregions(mask_bool, dgt_params, signal_params, tol,
 
         # from matplotlib.colors import LogNorm
         plt.figure()
-        plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
-        # ax=plt.gca()
-        # im = ax.matshow(pq_norms+pq_norms.T,
-        #                 norm=LogNorm(vmin=1e-10, vmax=1))
-        plt.ylabel('p')
-        plt.xlabel('q')
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
+        plt.ylabel('Sub-region index')
+        plt.xlabel('Sub-region index')
         plt.colorbar()
         plt.set_cmap('viridis')
         plt.title('Initial norms of Gabor multiplier composition')
@@ -102,9 +106,11 @@ def create_subregions(mask_bool, dgt_params, signal_params, tol,
                         .format(n_labels_max-n_labels))
 
             plt.figure()
-            plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
-            plt.ylabel('p-1')
-            plt.xlabel('q-1')
+            with warnings.catch_warnings():
+                warnings.simplefilter("ignore")
+                plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
+            plt.ylabel('Sub-region index')
+            plt.xlabel('Sub-region index')
             plt.colorbar()
             plt.set_cmap('viridis')
             plt.title('norms of Gabor multiplier composition')
@@ -120,9 +126,11 @@ def create_subregions(mask_bool, dgt_params, signal_params, tol,
         plt.savefig(fig_dir / 'final_subregions.pdf')
 
         plt.figure()
-        plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
-        plt.ylabel('p-1')
-        plt.xlabel('q-1')
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            plt.imshow(np.log10(pq_norms+pq_norms.T), origin='lower')
+        plt.ylabel('Sub-region index')
+        plt.xlabel('Sub-region index')
         plt.colorbar()
         plt.set_cmap('viridis')
         plt.title('Final norms of Gabor multiplier composition')
diff --git a/python/tffpy/experiments/tests/test_exp_solve_tff.py b/python/tffpy/experiments/tests/test_exp_solve_tff.py
index 3cb3a95..36ad655 100644
--- a/python/tffpy/experiments/tests/test_exp_solve_tff.py
+++ b/python/tffpy/experiments/tests/test_exp_solve_tff.py
@@ -7,6 +7,8 @@
 import unittest
 
 import matplotlib.pyplot as plt
+import matplotlib as mpl
+mpl.rcParams['figure.max_open_warning'] = 40
 
 from tffpy.experiments.exp_solve_tff import \
     SolveTffExperiment, create_and_run_light_experiment
diff --git a/python/tffpy/tests/test_create_subregions.py b/python/tffpy/tests/test_create_subregions.py
new file mode 100644
index 0000000..0038cc7
--- /dev/null
+++ b/python/tffpy/tests/test_create_subregions.py
@@ -0,0 +1,33 @@
+import unittest
+from tffpy.datasets import get_mix
+from tffpy.create_subregions import create_subregions
+
+
+class TestCreateSubregions(unittest.TestCase):
+    def test_create_subregions(self):
+        fig_dir = 'fig_create_subregions'
+        x_mix, dgt_params, signal_params, mask, x_loc, x_wb = \
+            get_mix(loc_source='bird',
+                    wideband_src='car',
+                    crop=4096,
+                    win_dur=256 / 8000,
+                    win_type='gauss',
+                    hop_ratio=1 / 4,
+                    n_bins_ratio=4,
+                    n_iter_closing=3,
+                    n_iter_opening=3,
+                    closing_first=True,
+                    delta_mix_db=0,
+                    delta_loc_db=20,
+                    wb_to_loc_ratio_db=16,
+                    or_mask=True,
+                    fig_dir=None)
+        tol = 1e-9
+        mask_with_subregions, norms = create_subregions(
+            mask_bool=mask, dgt_params=dgt_params, signal_params=signal_params,
+            tol=tol, fig_dir=fig_dir, return_norms=True)
+
+        tol = 1e-5
+        mask_with_subregions = create_subregions(
+            mask_bool=mask, dgt_params=dgt_params, signal_params=signal_params,
+            tol=tol, fig_dir=None, return_norms=False)
diff --git a/python/tffpy/tests/test_tf_fading.py b/python/tffpy/tests/test_tf_fading.py
index 4fdd3dd..c4af787 100644
--- a/python/tffpy/tests/test_tf_fading.py
+++ b/python/tffpy/tests/test_tf_fading.py
@@ -26,7 +26,7 @@ class TestEstimateEnergyInMask(unittest.TestCase):
                     or_mask=True,
                     fig_dir=fig_dir)
         plt.close('all')
-        
+
         estimated_energy = estimate_energy_in_mask(
             x_mix=x_mix, mask=mask, dgt_params=dgt_params,
             signal_params=signal_params, fig_dir=fig_dir, prefix=None)
-- 
GitLab