def test_givenABPEmbAddressParser_whenTestWithConfigWithCallbacks_thenCallbackAreUse( self): address_parser = AddressParser(model_type=self.a_bpemb_model_type, device=self.a_torch_device, verbose=self.verbose) self.training(address_parser) callback_mock = MagicMock() performance_after_test = address_parser.test( self.test_container, batch_size=self.a_batch_size, num_workers=self.a_number_of_workers, callbacks=[callback_mock], logging_path=self.a_checkpoints_saving_dir) self.assertIsNotNone(performance_after_test) callback_test_start_call = [call.on_test_begin({})] callback_mock.assert_has_calls(callback_test_start_call) callback_test_end_call = [ call.on_test_end({ "time": ANY, "test_loss": performance_after_test["test_loss"], "test_accuracy": performance_after_test["test_accuracy"] }) ] callback_mock.assert_has_calls(callback_test_end_call) callback_mock.assert_not_called()
def _test_callbacks_test(self, params, result_log): test_batch_dict = dict(zip(self.batch_metrics_names, self.batch_metrics_values), loss=ANY, time=ANY) call_list = [] call_list.append(call.on_test_begin({})) for batch in range(1, params['batch'] + 1): call_list.append(call.on_test_batch_begin(batch, {})) call_list.append( call.on_test_batch_end(batch, { 'batch': batch, 'size': ANY, **test_batch_dict })) call_list.append(call.on_test_end(result_log)) method_calls = self.mock_callback.method_calls self.assertEqual(call.set_model(self.model), method_calls[0]) # skip set_model self.assertEqual(len(method_calls), len(call_list) + 1) # for set_model self.assertEqual(method_calls[1:], call_list)
def test_givenAFasttextAddressParser_whenTestWithConfigWithCallbacks_thenCallbackAreUse( self, ): address_parser = AddressParser( model_type=self.a_fasttext_model_type, device=self.a_cpu_device, verbose=self.verbose, ) self.training(address_parser, self.training_container, self.a_number_of_workers) callback_mock = MagicMock() performance_after_test = address_parser.test( self.test_container, batch_size=self.a_batch_size, num_workers=self.a_number_of_workers, callbacks=[callback_mock], ) self.assertIsNotNone(performance_after_test) callback_test_start_call = [call.on_test_begin({})] callback_mock.assert_has_calls(callback_test_start_call) callback_test_end_call = [ call.on_test_end( { "time": ANY, "test_loss": performance_after_test["test_loss"], "test_accuracy": performance_after_test["test_accuracy"], } ) ] callback_mock.assert_has_calls(callback_test_end_call) callback_mock.assert_not_called()
def _get_callback_expected_on_calls_when_testing(self, params): test_batch_dict = {"time": ANY, "test_loss": ANY} test_batch_dict.update({ "test_" + metric_name: metric for metric_name, metric in zip(self.batch_metrics_names, self.batch_metrics_values) }) call_list = [] call_list.append(call.on_test_begin({})) for batch in range(1, params['steps'] + 1): call_list.append(call.on_test_batch_begin(batch, {})) call_list.append( call.on_test_batch_end(batch, { 'batch': batch, 'size': ANY, **test_batch_dict })) test_batch_dict.update({ "test_" + metric_name: metric for metric_name, metric in zip(self.epoch_metrics_names, self.epoch_metrics_values) }) call_list.append( call.on_test_end({ "time": ANY, "test_loss": ANY, **test_batch_dict })) return call_list
def _test_callbacks_test(self, params): test_batch_dict = {"time": ANY, "test_loss": ANY} test_batch_dict.update({ "test_" + metric_name: metric for metric_name, metric in zip(self.batch_metrics_names, self.batch_metrics_values) }) call_list = [] call_list.append(call.on_test_begin({})) for batch in range(1, params['steps'] + 1): call_list.append(call.on_test_batch_begin(batch, {})) call_list.append( call.on_test_batch_end(batch, { 'batch': batch, 'size': ANY, **test_batch_dict })) test_batch_dict.update({ "test_" + metric_name: metric for metric_name, metric in zip(self.epoch_metrics_names, self.epoch_metrics_values) }) call_list.append( call.on_test_end({ "time": ANY, "test_loss": ANY, **test_batch_dict })) method_calls = self.mock_callback.method_calls self.assertEqual(call.set_model(self.model), method_calls[0]) # skip set_model and set param call self.assertEqual(call.set_params(params), method_calls[1]) self.assertEqual(len(method_calls), len(call_list) + 2) # for set_model and set param self.assertEqual(method_calls[2:], call_list)