From 5fe45514877e16deae6ce8c1e3df8505acb4ff84 Mon Sep 17 00:00:00 2001 From: John La Date: Tue, 26 Mar 2024 09:24:52 -0500 Subject: [PATCH] Print scores after every progress bar (#540) --- sdmetrics/reports/base_report.py | 21 ++++++++++----------- tests/unit/reports/test_base_report.py | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/sdmetrics/reports/base_report.py b/sdmetrics/reports/base_report.py index c934f14f..4028ee79 100644 --- a/sdmetrics/reports/base_report.py +++ b/sdmetrics/reports/base_report.py @@ -125,15 +125,8 @@ def _print_results(self, verbose): """ if verbose: sys.stdout.write( - f'\nOverall Score: {round(self._overall_score * 100, 2)}%\n\n' + f'Overall Score (Average): {round(self._overall_score * 100, 2)}%\n\n' ) - sys.stdout.write('Properties:\n') - - for property_name, property_instance in self._properties.items(): - property_score = round(property_instance._compute_average() * 100, 2) - sys.stdout.write( - f'- {property_name}: {property_score}%\n' - ) def generate(self, real_data, synthetic_data, metadata, verbose=True): """Generate report. @@ -173,15 +166,19 @@ def generate(self, real_data, synthetic_data, metadata, verbose=True): scores = [] progress_bar = None if verbose: - sys.stdout.write('Generating report ...\n') + sys.stdout.write('Generating report ...\n\n') start_time = time.time() for ind, (property_name, property_instance) in enumerate(self._properties.items()): if verbose: num_iterations = int(property_instance._get_num_iterations(metadata)) - progress_bar = tqdm.tqdm(total=num_iterations, file=sys.stdout) + progress_bar = tqdm.tqdm( + total=num_iterations, + file=sys.stdout, + bar_format='{desc}|{bar}{r_bar}|' + ) progress_bar.set_description( - f'({ind + 1}/{len(self._properties)}) Evaluating {property_name}: ' + f'({ind + 1}/{len(self._properties)}) Evaluating {property_name}' ) score = self._properties[property_name].get_score( @@ -190,6 +187,8 @@ def generate(self, real_data, synthetic_data, metadata, verbose=True): scores.append(score) if verbose: progress_bar.close() + sys.stdout.write(f'{property_name} Score: {round(score * 100, 2)}%\n\n') + sys.stdout.flush() self._overall_score = np.nanmean(scores) self.is_generated = True diff --git a/tests/unit/reports/test_base_report.py b/tests/unit/reports/test_base_report.py index 776d8250..1b821442 100644 --- a/tests/unit/reports/test_base_report.py +++ b/tests/unit/reports/test_base_report.py @@ -277,11 +277,9 @@ def test_print_results_verbose_true(self, mock_write): # Assert calls = [ - call('\nOverall Score: 50.0%\n\n'), - call('Properties:\n'), - call('- Column Shapes: 60.0%\n'), - call('- Column Pair Trends: 40.0%\n'), + call('Overall Score (Average): 50.0%\n\n') ] + assert mock_write.call_count == 1 mock_write.assert_has_calls(calls, any_order=True) @patch('sys.stdout.write') @@ -450,8 +448,9 @@ def test_generate_multi_table_details(self, version_mock, time_mock, datetime_mo } assert base_report.report_info == expected_info + @patch('sys.stdout.write') @patch('tqdm.tqdm') - def test_generate_verbose(self, mock_tqdm): + def test_generate_verbose(self, mock_tqdm, mock_write): """Test the ``generate`` method with verbose=True.""" # Setup base_report = BaseReport() @@ -493,7 +492,16 @@ def test_generate_verbose(self, mock_tqdm): base_report.generate(real_data, synthetic_data, metadata, verbose=True) # Assert - calls = [call(total=4, file=sys.stdout), call(total=6, file=sys.stdout)] + write_calls = [ + call('Property 1 Score: 100.0%\n\n'), + call('Property 2 Score: 100.0%\n\n'), + ] + mock_write.assert_has_calls(write_calls, any_order=True) + + calls = [ + call(total=4, bar_format='{desc}|{bar}{r_bar}|', file=sys.stdout), + call(total=6, bar_format='{desc}|{bar}{r_bar}|', file=sys.stdout) + ] mock_tqdm.assert_has_calls(calls, any_order=True) base_report._print_results.assert_called_once_with(True)