def load(fname, verbose=0): data = utils.parse_hvsrpy_output(fname) std_curve = np.log(data["upper"]) - np.log(data["curve"]) clarity = utils.sesame_clarity(data["frequency"], data["curve"], std_curve, data["std_f0"], verbose=verbose) reliability = utils.sesame_reliability(data["windowlength"], data["accepted_windows"], data["frequency"], data["curve"], std_curve, verbose=verbose) return (reliability, clarity)
def test_sesame_with_limits(self): frequency = np.array([0.3, 0.4, 0.5, 0.6, 0.9, 1, 1.1, 1.2, 1.3]) mean_curve = np.array([1, 3, 1, 1, 1, 1, 1, 3, 1]) std_curve = np.ones(9) * 0.2 f0_std = 0.07 # reliability expecteds = [3, 3] settings = [(0.3, 0.5), (0.9, 1.3)] for expected, limits in zip(expecteds, settings): with open(os.devnull, "w") as sys.stdout: returned = np.sum( utils.sesame_reliability(60, 10, frequency, mean_curve, std_curve, search_limits=limits, verbose=2)) sys.stdout = sys.__stdout__ self.assertEqual(expected, returned) # clarity expecteds = [6, 6] settings = [(0.3, 0.5), (0.9, 1.3)] for expected, limits in zip(expecteds, settings): with open(os.devnull, "w") as sys.stdout: returned = np.sum( utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, search_limits=limits, verbose=2)) sys.stdout = sys.__stdout__ self.assertEqual(expected, returned)
def test_sesame_by_condition(self): # Defaults frequency = np.array([0.7, 0.8, 0.9, 1, 1.1]) mean_curve = np.array([1, 1, 2.1, 1, 1]) std_curve = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) f0_std = 0.1 # Condition 1: there exists an f in [f0/4, f0] where A(f) < A(f0)/2 frequency = np.array([0.2, 0.4, 0.7, 1, 1.5]) potential_curves = [(1, np.array([1.0, 1.0, 1.0, 3.0, 1.0])), (0, np.array([2.9, 2.9, 2.9, 3.0, 1.0])), (0, np.array([1.6, 2.0, 2.9, 3.0, 1.0])), (1, np.array([1.4, 1.4, 2.0, 3.0, 1.0])), (0, np.array([1.5, 1.5, 2.9, 3.0, 1.0]))] for expected, mean_curve in potential_curves: returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[0] self.assertEqual(expected, returned) # Condition 2: there exists an f in [f0, 4f0] where A(f) < A(f0)/2 frequency = np.array([0.7, 1, 2.5, 3.5, 5]) potential_curves = [(1, np.array([1.0, 3.0, 1.0, 1.0, 1.0])), (0, np.array([1.0, 3.0, 2.9, 2.9, 2.9])), (0, np.array([1.0, 3.0, 2.9, 2.0, 1.6])), (1, np.array([1.0, 3.0, 2.0, 1.4, 1.4])), (0, np.array([1.0, 3.0, 2.9, 1.5, 1.5]))] for expected, mean_curve in potential_curves: returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[1] self.assertEqual(expected, returned) # Condition 3: A0>2 potential_curves = [(0, np.array([1.0, 1.0, 1.1, 1.0, 1.0])), (0, np.array([1.0, 2.0, 1.0, 1.0, 1.0])), (0, np.array([1.0, 1.0, 1.0, 1.2, 1.0])), (1, np.array([1.0, 3.0, 2.0, 1.4, 1.4])), (1, np.array([1.0, 5.0, 2.9, 1.5, 1.5])), (1, np.array([1.0, 3.0, 2.9, 7.5, 1.5]))] for expected, mean_curve in potential_curves: returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[2] self.assertEqual(expected, returned) # Condition 4: fpeak[Ahv(f) +/- sigma_a(f)] = f0 +/- 5% frequency = np.array([0.90, 0.94, 0.96, 1, 1.04, 1.06, 1.1]) mean_curves = [ np.array([1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0]), np.array([1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0]), np.array([1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0]), np.array([1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0]), np.array([1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0]), np.array([1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0]), np.array([1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0]) ] uppers = [ np.array([1.0, 1.1, 1.0, 1.0, 1.0, 1.0, 1.0]) + 0.1, np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.1, 1.0]) + 0.1, np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.1, 1.0]) + 0.1, np.array([1.0, 1.1, 1.0, 1.0, 1.0, 1.0, 1.0]) + 0.1, np.array([1.0, 1.0, 1.0, 2.1, 1.0, 1.0, 1.0]) + 0.1, np.array([1.0, 1.0, 1.1, 1.0, 1.0, 1.0, 1.0]) + 0.1, np.array([1.0, 1.0, 1.0, 1.0, 1.1, 1.0, 1.0]) + 0.1 ] rating = [0, 0, 0, 0, 1, 1, 1] for expected, upper, mean_curve in zip(rating, uppers, mean_curves): std_curve = np.log(upper) - np.log(mean_curve) curve = np.exp(np.log(mean_curve) + std_curve) self.assertArrayEqual(upper, curve) returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[3] self.assertEqual(expected, returned) # Condition 5: f0_std < epsilon(f0) frequency = np.array([0.05, 0.1, 0.35, 0.75, 1.5, 3, 5]) factors = [0.25, 0.2, 0.15, 0.1, 0.05] for peak_frequency, factor in zip(frequency[1:-1], factors): mean_curve = np.ones_like(frequency) mean_curve[peak_frequency == frequency] = 2 std_curve = 0.1 * mean_curve for adjust, expected in zip([0.9, 1.1], [1, 0]): f0_std = factor * peak_frequency * adjust returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[4] self.assertEqual(expected, returned) # Condition 6: sigma_a(f0) < theta(f0) frequency = np.array([0.05, 0.1, 0.35, 0.75, 1.5, 3, 5]) factors = [3.0, 2.5, 2.0, 1.78, 1.58] for peak_frequency, factor in zip(frequency[1:-1], factors): mean_curve = np.ones_like(frequency) mean_curve[peak_frequency == frequency] = 2 for adjust, expected in zip([0.9, 1.1], [1, 0]): sigma_a = np.ones_like(frequency) * adjust * factor upper = mean_curve * sigma_a std_curve = np.log(upper) - np.log(mean_curve) curve = np.exp(np.log(mean_curve) + std_curve) self.assertArrayAlmostEqual(upper, curve, places=6) f0_std = factor * peak_frequency * adjust returned = utils.sesame_clarity(frequency, mean_curve, std_curve, f0_std, verbose=0)[5] self.assertEqual(expected, returned)