def get_training_run_details(self, model_id, run_index): model = self._client.get_model(model_id) options = model.training_runs[run_index].training_options details = {} if model.training_runs[run_index].results: details['actual_iterations'] = len( model.training_runs[run_index].results) for field in options.ListFields(): field_name = field[0].name value = field[1] if field_name == 'data_split_method': details[field_name] = Model.DataSplitMethod(value).name elif field_name == 'distance_type': details[field_name] = Model.DistanceType(value).name elif field_name == 'learn_rate_strategy': details[field_name] = Model.LearnRateStrategy(value).name elif field_name == 'loss_type': details[field_name] = Model.LossType(value).name elif field_name == 'optimization_strategy': details[field_name] = Model.OptimizationStrategy(value).name elif field_name == 'kmeans_initialization_method': details[ field_name] = Model.KmeansEnums.KmeansInitializationMethod( value).name else: details[field_name] = format_preview_value(value) return {'details': details}
def testGetModelDetailsEmptyFields(self): client = Mock() model = Mock(project='project_id', dataset_id='dataset_id', model_id='model_id', description=None, labels={}, created=datetime.datetime(2020, 7, 14, 13, 23, 45, 67, tzinfo=None), expires=datetime.datetime(2021, 7, 14, 13, 23, 45, 67, tzinfo=None), location='US', modified=datetime.datetime(2020, 7, 15, 15, 11, 23, 32, tzinfo=None), model_type=0, label_columns=[], feature_columns=[], training_runs=[Mock(start_time=Mock(seconds=1234567))]) client.get_model = Mock(return_value=model) bigquery = BigQueryService(client) expected = { 'details': { 'id': 'project_id.dataset_id.model_id', 'name': 'model_id', 'description': None, 'labels': None, 'date_created': '2020-07-14T13:23:45.000067', 'expires': '2021-07-14T13:23:45.000067', 'location': 'US', 'last_modified': '2020-07-15T15:11:23.000032', 'model_type': Model.ModelType(0).name, 'schema_labels': [], 'feature_columns': [], 'training_runs': ['1970-01-15T06:56:07+00:00'] } } result = bigquery.get_model_details('some_model_id') self.assertEqual(expected, result)
def testGetModelDetailsEmptyFields(self): client = Mock() model = Mock(project='project_id', dataset_id='dataset_id', model_id='model_id', description=None, labels={}, created=datetime.datetime(2020, 7, 14, 13, 23, 45, 67, tzinfo=None), expires=datetime.datetime(2021, 7, 14, 13, 23, 45, 67, tzinfo=None), location='US', modified=datetime.datetime(2020, 7, 15, 15, 11, 23, 32, tzinfo=None), model_type=0, label_columns=[], feature_columns=[]) client.get_model = Mock(return_value=model) expected = { 'details': { 'id': 'project_id.dataset_id.model_id', 'name': 'model_id', 'description': None, 'labels': None, 'date_created': '2020-07-14T13:23:45.000067', 'expires': '2021-07-14T13:23:45.000067', 'location': 'US', 'last_modified': '2020-07-15T15:11:23.000032', 'model_type': Model.ModelType(0).name, 'schema_labels': [], 'feature_columns': [] } } result = get_model_details(client, 'some_model_id') self.assertEqual(expected, result)
def get_model_details(self, model_id): '''Returns basic metadata for a model, as well as an array of training run dates.''' model = self._client.get_model(model_id) return { 'details': { 'id': "{}.{}.{}".format(model.project, model.dataset_id, model.model_id), 'name': model.model_id, 'description': model.description, 'labels': [ "{}: {}".format(label, value) for label, value in model.labels.items() ] if model.labels else None, 'date_created': format_date(model.created), 'last_modified': format_date(model.modified), 'expires': format_date(model.expires) if model.expires else None, 'location': model.location, 'model_type': Model.ModelType(model.model_type).name, 'schema_labels': [{ 'name': label_col.name, 'type': SqlTypeNames[StandardSqlDataTypes( label_col.type.type_kind).name].name } for label_col in model.label_columns], 'feature_columns': [{ 'name': feat_col.name, 'type': SqlTypeNames[StandardSqlDataTypes( feat_col.type.type_kind).name].name } for feat_col in model.feature_columns], 'training_runs': [ format_date( datetime.datetime.fromtimestamp( run.start_time.seconds, datetime.timezone.utc)) for run in model.training_runs ] } }
def get_model_details(client, model_id): model = client.get_model(model_id) return { 'details': { 'id': "{}.{}.{}".format(model.project, model.dataset_id, model.model_id), 'name': model.model_id, 'description': model.description, 'labels': [ "{}: {}".format(label, value) for label, value in model.labels.items() ] if model.labels else None, 'date_created': format_date(model.created), 'last_modified': format_date(model.modified), 'expires': format_date(model.expires) if model.expires else None, 'location': model.location, 'model_type': Model.ModelType(model.model_type).name, 'schema_labels': [{ 'name': label_col.name, 'type': SqlTypeNames[StandardSqlDataTypes( label_col.type.type_kind).name].name } for label_col in model.label_columns], 'feature_columns': [{ 'name': feat_col.name, 'type': SqlTypeNames[StandardSqlDataTypes( feat_col.type.type_kind).name].name } for feat_col in model.feature_columns] } }
def testGetTrainingRunDetails(self): client = Mock() options = [['data_split_column', 'column_name_1'], ['data_split_eval_fraction', 0.3], ['data_split_method', 1], ['distance_type', 1], ['early_stop', BoolValue(value=False)], ['initial_learn_rate', 0.1], ['input_label_columns', ['column1', 'column2']], ['kmeans_initialization_column', 'column_name_2'], ['kmeans_initialization_method', 1], ['l1_regularization', DoubleValue(value=0.5)], ['l2_regularization', DoubleValue(value=0.6)], ['label_class_weights', [0.2, 0.3]], ['learn_rate', 0.7], ['learn_rate_strategy', 1], ['loss_type', 1], ['max_iterations', 20], ['min_relative_progress', DoubleValue(value=0.4)], ['model_uri', 'model.uri.string'], ['num_clusters', 7], ['optimization_strategy', 1], ['warm_start', BoolValue(value=True)]] mocked_options = [] for option in options: option_mock = Mock() option_mock.name = option[0] mocked_options.append([option_mock, option[1]]) training_options = Mock() training_options.ListFields = Mock(return_value=mocked_options) training_run = Mock(training_options=training_options, results=['result', 'result', 'result']) model = Mock(training_runs=[training_run]) client.get_model = Mock(return_value=model) bigquery = BigQueryService(client) expected = { 'details': { 'actual_iterations': 3, 'data_split_column': 'column_name_1', 'data_split_eval_fraction': 0.3, 'data_split_method': Model.DataSplitMethod(1).name, 'distance_type': Model.DistanceType(1).name, 'early_stop': 'False', 'initial_learn_rate': 0.1, 'input_label_columns': "['column1', 'column2']", 'kmeans_initialization_column': 'column_name_2', 'kmeans_initialization_method': Model.KmeansEnums.KmeansInitializationMethod(1).name, 'l1_regularization': 0.5, 'l2_regularization': 0.6, 'label_class_weights': '[0.2, 0.3]', 'learn_rate': 0.7, 'learn_rate_strategy': Model.LearnRateStrategy(1).name, 'loss_type': Model.LossType(1).name, 'max_iterations': 20, 'min_relative_progress': 0.4, 'model_uri': 'model.uri.string', 'num_clusters': 7, 'optimization_strategy': Model.OptimizationStrategy(1).name, 'warm_start': 'True' } } result = bigquery.get_training_run_details('some_model_id', 0) self.assertEqual(expected, result)
def testGetModelDetailsFull(self): client = Mock() label_col_0 = Mock() label_col_0.name = 'schema_label_0' label_col_0.type = Mock(type_kind=7) label_col_1 = Mock() label_col_1.name = 'schema_label_1' label_col_1.type = Mock(type_kind=9) feature_col_0 = Mock() feature_col_0.name = 'feature_col_0' feature_col_0.type = Mock(type_kind=8) model = Mock(project='project_id', dataset_id='dataset_id', model_id='model_id', description='description of model', labels={ 'label_0': 'value_0', 'label_1': 'value_1' }, created=datetime.datetime(2020, 7, 14, 13, 23, 45, 67, tzinfo=None), expires=datetime.datetime(2021, 7, 14, 13, 23, 45, 67, tzinfo=None), location='US', modified=datetime.datetime(2020, 7, 15, 15, 11, 23, 32, tzinfo=None), model_type=0, label_columns=[label_col_0, label_col_1], feature_columns=[feature_col_0], training_runs=[ Mock(start_time=Mock(seconds=1234567)), Mock(start_time=Mock(seconds=1234789)) ]) client.get_model = Mock(return_value=model) bigquery = BigQueryService(client) expected = { 'details': { 'id': 'project_id.dataset_id.model_id', 'name': 'model_id', 'description': 'description of model', 'labels': ['label_0: value_0', 'label_1: value_1'], 'date_created': '2020-07-14T13:23:45.000067', 'last_modified': '2020-07-15T15:11:23.000032', 'expires': '2021-07-14T13:23:45.000067', 'location': 'US', 'model_type': Model.ModelType(0).name, 'schema_labels': [{ 'name': 'schema_label_0', 'type': SqlTypeNames[StandardSqlDataTypes(7).name].name }, { 'name': 'schema_label_1', 'type': SqlTypeNames[StandardSqlDataTypes(9).name].name }], 'feature_columns': [{ 'name': 'feature_col_0', 'type': SqlTypeNames[StandardSqlDataTypes(8).name].name }], 'training_runs': ['1970-01-15T06:56:07+00:00', '1970-01-15T06:59:49+00:00'] } } result = bigquery.get_model_details('some_model_id') self.assertEqual(expected, result)