def test_to_dict(self): sb = StatBook('test_sb') ms = MultiStat(name='test_ms') sb.add_multi_stat(ms) output_dict = sb.to_dict() self.assertEqual(sb.name, output_dict["name"]) self.assertEqual([ms.to_dict() for ms in sb.multi_stats_list()], output_dict["multi_stats"])
def test_add_raw_value_stat(self): ms = MultiStat('test_ms') func = lambda x: x ** 2 ms.add_raw_value_stat(func) self.assertIn(ms.RAW_VALUE_STAT_NAME, ms.stats) self.assertEqual(ms.stats.get(ms.RAW_VALUE_STAT_NAME).name, ms.RAW_VALUE_STAT_NAME) self.assertEqual(ms.stats.get(ms.RAW_VALUE_STAT_NAME).function, func)
def test_std(self): ms = MultiStat('test_ms') ms.add_raw_value_stat(lambda x: x) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(3) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([5, 7]) self.assertListEqual(ms.std(), [0, 0, 0, 1.])
def test_multi_stats(self): sb = StatBook('test_sb') ms1 = MultiStat(name='test_stat1') ms2 = MultiStat(name='test_stat2') sb.add_multi_stat(ms1) sb.add_multi_stat(ms2) result = sb.multi_stats_list() self.assertListEqual(result, [ms1, ms2])
def test_running_mean(self): ms = MultiStat('test_ms') ms.add_raw_value_stat(lambda x: x) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(3) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([5, 7, 0]) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([40, 0, 5, 5, 5, 5]) self.assertListEqual(ms.running_mean(), [1., 1.5, 2., 2.5, 4.])
def test_running_max(self): ms = MultiStat('test_ms') ms.add_raw_value_stat(lambda x: x) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(3) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([5, 7]) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([4, 1, 1]) self.assertListEqual(ms.running_max(), [1, 2, 3, 7, 7])
def test_maximum(self): sb = StatBook('test_sb') ms = MultiStat(name='test_stat') ms.maximum = MagicMock() ms.maximum.return_value = [3., 4., 5.] sb.add_multi_stat(ms) result = sb.maximum(ms.name) self.assertListEqual(result, [3., 4., 5.]) ms.maximum.assert_called_once()
def test_running_std(self): sb = StatBook('test_sb') ms = MultiStat(name='test_stat') ms.running_std = MagicMock() ms.running_std.return_value = [3., 4., 5.] sb.add_multi_stat(ms) result = sb.running_std(ms.name) self.assertListEqual(result, [3., 4., 5.]) ms.running_std.assert_called_once()
def test_maximum(self): ms = MultiStat('test_ms') ms.add_raw_value_stat(lambda x: x) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(3) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([5, 7, 9]) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record([0, 1, 1]) self.assertListEqual(ms.maximum(), [1., 2., 3., 9., 1.])
def test_stat_names(self): ms = MultiStat('test_ms') stat_1 = Statistic('name1') stat_2 = Statistic('name2') stat_3 = Statistic('name3') ms.add_statistic(stat_1) ms.add_statistic(stat_2) ms.add_statistic(stat_3) self.assertListEqual([stat_1.name, stat_2.name, stat_3.name], ms.stat_names())
def test_stats_list(self): ms = MultiStat('test_ms') stat_1 = Statistic('name1') stat_2 = Statistic('name2') stat_3 = Statistic('name3') ms.add_statistic(stat_1) ms.add_statistic(stat_2) ms.add_statistic(stat_3) self.assertListEqual([stat_1, stat_2, stat_3], ms.stats_list())
def test_running_std(self): ms = MultiStat('test_ms') ms.add_raw_value_stat(lambda x: x) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) self.assertListEqual(ms.running_std(), [0, 0.5]) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(4) self.assertTrue(np.allclose([0, 0.5, 1.247219128924647], ms.running_std()))
def test_to_dict(self): ms = MultiStat(name='test_ms') stat_1 = Statistic('name1') stat_1.record(6) ms.add_statistic(stat_1) output_dict = ms.to_dict() self.assertEqual(ms.name, output_dict["name"]) self.assertEqual([stat.to_dict() for stat in ms.stats_list()], output_dict["stats"])
def test_clear_all_values(self): ms = MultiStat('test_ms') stat_1 = Statistic('name1') stat_1.clear = MagicMock() stat_2 = Statistic('name2') stat_2.clear = MagicMock() stat_3 = Statistic('name3') stat_3.clear = MagicMock() ms.add_statistic(stat_1) ms.add_statistic(stat_2) ms.add_statistic(stat_3) ms.clear_all_values() stat_1.clear.assert_called_once() stat_2.clear.assert_called_once() stat_3.clear.assert_called_once()
def test_get_raw_values(self): ms = MultiStat('test_ms') result = ms.get_raw_values() self.assertIsNone(result) ms.add_raw_value_stat(lambda x: x ** 2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(1) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(2) ms.stats.get(ms.RAW_VALUE_STAT_NAME).record(3) self.assertListEqual(ms.get_raw_values(), [1., 4., 9.])
def test_clear_all_values(self): sb = StatBook('test_sb') ms1 = MultiStat(name='test_stat1') ms1.clear_all_values = MagicMock() ms2 = MultiStat(name='test_stat2') ms2.clear_all_values = MagicMock() sb.add_multi_stat(ms1) sb.add_multi_stat(ms2) sb.clear_all_values() ms1.clear_all_values.assert_called_once() ms2.clear_all_values.assert_called_once()
def test_add_statistic(self): ms = MultiStat('test_ms') stat = Statistic(name='test_stat') ms.add_statistic(stat) self.assertIn(stat.name, ms.stats) self.assertEqual(ms.stats.get(stat.name), stat)
def test_add_multistat(self): sb = StatBook('test_sb') ms = MultiStat(name='test_stat') sb.add_multi_stat(ms) self.assertIn(ms.name, sb.multi_stats) self.assertEqual(sb.multi_stats.get(ms.name), ms)