From ac1857d882526157bbc0ddfcf7bf7a36d0e6d46a Mon Sep 17 00:00:00 2001
From: ferrari <maxence.ferrari@gmail.com>
Date: Fri, 6 May 2022 11:50:33 +0200
Subject: [PATCH] Add verbose option

---
 gsrp_smart_util.py    | 12 ++++++++++--
 gsrp_tdoa_hyperres.py |  8 +++++---
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/gsrp_smart_util.py b/gsrp_smart_util.py
index a13d51b..585e5d4 100644
--- a/gsrp_smart_util.py
+++ b/gsrp_smart_util.py
@@ -1,4 +1,5 @@
 import numpy as np
+from tqdm import tqdm
 from math import ceil
 
 
@@ -135,7 +136,11 @@ def constrained_argmax(mem, cc, tij_ind, curr_tij, used_tij, t_max, n_ind):
     return mem_tij[:, np.argmax(mem_val)]
 
 
-def smart_gsrp(cc, n_ind, n_tot, t_max, tree, program, clean_list):
+def _get_mem_size(memory):
+    return sum(len(o[0].T) for o in memory.values())
+
+
+def smart_gsrp(cc, n_ind, n_tot, t_max, tree, program, clean_list, verbose=False):
     memory = dict()
     val = cc[:, 0].prod()
     tij = np.zeros(n_tot, int)
@@ -166,7 +171,10 @@ def smart_gsrp(cc, n_ind, n_tot, t_max, tree, program, clean_list):
             for k in range(j):
                 memory[(i, k)] = mask_lim(memory[(i, k)], tij_min, tij_max, t_max)
 
-        # print('tdoa:', tij, 'val:', val, 'mem size:', (lambda x: f'{x} ({100 * x / (n_ind//(i+1)) / (2 * t_max + 1) ** (i+1)}%)')(sum(len(o[0].T) for o in memory.values())))
+        if verbose:
+            mem_size = _get_mem_size(memory)
+            tqdm.write(f'TDOA: {tij}, val: {val}, mem size: {mem_size} items,'
+                       f' {100 * mem_size / (n_ind // (i + 1)) / (2 * t_max + 1) ** (i + 1)}%')
 
         # Mem clean up
         for p in clean_list[i]:
diff --git a/gsrp_tdoa_hyperres.py b/gsrp_tdoa_hyperres.py
index 5c90b2b..ce507ab 100755
--- a/gsrp_tdoa_hyperres.py
+++ b/gsrp_tdoa_hyperres.py
@@ -44,7 +44,7 @@ def slicer(down, up, ndim, n):
     return slices[index].reshape(ndim, -1).T
 
 
-def corr(data, pos, w_size, max_tdoa, decimate=1, mode='prepare', hyper=True):
+def corr(data, pos, w_size, max_tdoa, decimate=1, mode='prepare', hyper=True, verbose=False):
     num_channels = data.shape[1]
     num_channel_pairs = num_channels * (num_channels - 1) // 2
 
@@ -119,7 +119,7 @@ def corr(data, pos, w_size, max_tdoa, decimate=1, mode='prepare', hyper=True):
             tdoas[i, :2], tdoas[i, 2:] = c_corr.c_corr_all(cc, cc_size//2, num_channels - 1)
         elif mode == 'smart':
             tdoas[i, :2], tdoas[i, 2:] = smart_gsrp(cc, num_channels - 1, num_channel_pairs, cc_size // 2,
-                                           tree, program, clean_list)
+                                           tree, program, clean_list, verbose=verbose)
         else:
             raise ValueError(f'Unknown mode {mode}')
         tdoas[i, 1] += maxs
@@ -184,7 +184,8 @@ def main(args):
 
     print("Computing TDOAs...")
     results = corr(sound, pos, int(sr * args.frame_size), max_tdoa=int(np.ceil(sr * args.max_tdoa)),
-                   decimate=args.decimate if not args.temporal else 1, mode=args.mode, hyper=not args.no_hyperres)
+                   decimate=args.decimate if not args.temporal else 1, mode=args.mode, hyper=not args.no_hyperres,
+                   verbose=args.verbose)
     if args.no_hyperres:
         result1 = results
     else:
@@ -273,6 +274,7 @@ if __name__ == "__main__":
                              f'{BColors.BOLD}smart{BColors.ENDC} gradually increase the search space dimension, '
                              f'reducing the number of tdoa to evaluate.\n'
                              f'{BColors.BOLD}auto{BColors.ENDC} automatically try to pick the right method.')
+    group4.add_argument('-v', '--verbose', action='store_true', help='Activate verbose for smart mode')
 
     args = parser.parse_args()
     try:
-- 
GitLab