From 3045d5b49f082973c6393453c0d6d1de8a8ffde4 Mon Sep 17 00:00:00 2001 From: Albert Wu <84879801+awu-labelbox@users.noreply.github.com> Date: Fri, 4 Jun 2021 13:35:09 -0700 Subject: [PATCH 1/5] Adds create model to client --- labelbox/__init__.py | 1 + labelbox/client.py | 17 +++++++++++++++++ labelbox/schema/__init__.py | 11 ++++++----- tests/integration/test_model.py | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/integration/test_model.py diff --git a/labelbox/__init__.py b/labelbox/__init__.py index 2dc09ac39..878fc1454 100644 --- a/labelbox/__init__.py +++ b/labelbox/__init__.py @@ -19,3 +19,4 @@ from labelbox.schema.role import Role, ProjectRole from labelbox.schema.invite import Invite, InviteLimit from labelbox.schema.model_run import ModelRun +from labelbox.schema.model import Model diff --git a/labelbox/client.py b/labelbox/client.py index fcbf565ee..83556068b 100644 --- a/labelbox/client.py +++ b/labelbox/client.py @@ -533,3 +533,20 @@ def get_models(self, where=None): An iterable of Models (typically a PaginatedCollection). """ return self._get_all(Model, where, filter_deleted=False) + + def create_model(self, **kwargs): + """ Creates a Model object on the server. + + Attribute values are passed as keyword arguments. + + >>> model = client.create_model(name="", ontology_id="") + + Args: + **kwargs: Keyword arguments with Model attribute values. + Returns: + A new Model object. + Raises: + InvalidAttributeError: If the Model type does not contain + any of the attribute names given in kwargs. + """ + return self._create(Model, kwargs) diff --git a/labelbox/schema/__init__.py b/labelbox/schema/__init__.py index 926ad9414..78b9637fd 100644 --- a/labelbox/schema/__init__.py +++ b/labelbox/schema/__init__.py @@ -3,16 +3,17 @@ import labelbox.schema.benchmark import labelbox.schema.data_row import labelbox.schema.dataset +import labelbox.schema.invite import labelbox.schema.label import labelbox.schema.labeling_frontend +import labelbox.schema.model +import labelbox.schema.model_run +import labelbox.schema.ontology import labelbox.schema.organization +import labelbox.schema.prediction import labelbox.schema.project import labelbox.schema.review +import labelbox.schema.role import labelbox.schema.task import labelbox.schema.user import labelbox.schema.webhook -import labelbox.schema.prediction -import labelbox.schema.ontology -import labelbox.schema.invite -import labelbox.schema.role -import labelbox.schema.model_run diff --git a/tests/integration/test_model.py b/tests/integration/test_model.py new file mode 100644 index 000000000..9a6fe2b8f --- /dev/null +++ b/tests/integration/test_model.py @@ -0,0 +1,23 @@ +from labelbox import Model + +def test_model(client, configured_project, rand_gen): + before = list(client.get_models()) + for m in before: + assert isinstance(m, Model) + + ontology = configured_project.ontology + + data = {"name": rand_gen(str), "ontology_id": ontology.uid} + model = client.create_model(**data) + assert model.name == data["name"] + assert model.ontology.id == data["ontology_id"] + + after = list(client.get_models()) + assert len(after) == len(before) + 1 + assert model in after + + model = client.get_model(model.uid) + assert model.name == data["name"] + assert model.ontology.id == data["ontology_id"] + + From 84af3768b190c4dece21b4e8c94f366018caddd0 Mon Sep 17 00:00:00 2001 From: Albert Wu <84879801+awu-labelbox@users.noreply.github.com> Date: Mon, 7 Jun 2021 09:32:31 -0700 Subject: [PATCH 2/5] Adds create model run to model entity --- labelbox/schema/model.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/labelbox/schema/model.py b/labelbox/schema/model.py index 0b30ae1d7..53f539d13 100644 --- a/labelbox/schema/model.py +++ b/labelbox/schema/model.py @@ -1,5 +1,6 @@ +from labelbox.orm import query from labelbox.orm.db_object import DbObject -from labelbox.orm.model import Field, Relationship +from labelbox.orm.model import Entity, Field, Relationship class Model(DbObject): @@ -16,3 +17,26 @@ class Model(DbObject): model_runs = Relationship.ToMany("ModelRun", False) ontology = Relationship.ToOne("Ontology", False) + + def create_model_run(self, name): + """ Creates a model run belonging to this model. + + Args: + name (string): The name for the model run. + Returns: + ModelRun, the created model run. + """ + name_param = "name" + model_id_param = "modelId" + ModelRun = Entity.ModelRun + query_str = """mutation CreateModelRunPyApi($%s: String!, $%s: ID!) { + createModelRun(data: {name: $%s, modelId: $%s}) {%s}}""" % ( + name_param, model_id_param, + name_param, model_id_param, + query.results_query_part(ModelRun) + ) + res = self.client.execute(query_str, { + name_param: name, + model_id_param: self.uid + }) + return ModelRun(self.client, res["createModelRun"]) From 6cdf6665f303d56c303aed0b22e1648b58f252de Mon Sep 17 00:00:00 2001 From: Albert Wu <84879801+awu-labelbox@users.noreply.github.com> Date: Mon, 7 Jun 2021 09:48:31 -0700 Subject: [PATCH 3/5] Use explicit args for create_model --- labelbox/client.py | 11 +++++------ tests/integration/test_model.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/labelbox/client.py b/labelbox/client.py index 83556068b..21b6b8891 100644 --- a/labelbox/client.py +++ b/labelbox/client.py @@ -534,19 +534,18 @@ def get_models(self, where=None): """ return self._get_all(Model, where, filter_deleted=False) - def create_model(self, **kwargs): + def create_model(self, name, ontology_id): """ Creates a Model object on the server. - Attribute values are passed as keyword arguments. - - >>> model = client.create_model(name="", ontology_id="") + >>> model = client.create_model(, ") Args: - **kwargs: Keyword arguments with Model attribute values. + name (string): Name of the model + ontology_id (string): ID of the related ontology Returns: A new Model object. Raises: InvalidAttributeError: If the Model type does not contain any of the attribute names given in kwargs. """ - return self._create(Model, kwargs) + return self._create(Model, {name: name, ontology_id: ontology_id}) diff --git a/tests/integration/test_model.py b/tests/integration/test_model.py index 9a6fe2b8f..2399382df 100644 --- a/tests/integration/test_model.py +++ b/tests/integration/test_model.py @@ -8,7 +8,7 @@ def test_model(client, configured_project, rand_gen): ontology = configured_project.ontology data = {"name": rand_gen(str), "ontology_id": ontology.uid} - model = client.create_model(**data) + model = client.create_model(data["name"], data["ontology_id"]) assert model.name == data["name"] assert model.ontology.id == data["ontology_id"] From 82922ac8f367ce7da9f740fcf80547b1affdba95 Mon Sep 17 00:00:00 2001 From: Albert Wu <84879801+awu-labelbox@users.noreply.github.com> Date: Mon, 7 Jun 2021 10:42:00 -0700 Subject: [PATCH 4/5] Fix broken dict --- labelbox/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/labelbox/client.py b/labelbox/client.py index 21b6b8891..8e589910c 100644 --- a/labelbox/client.py +++ b/labelbox/client.py @@ -548,4 +548,4 @@ def create_model(self, name, ontology_id): InvalidAttributeError: If the Model type does not contain any of the attribute names given in kwargs. """ - return self._create(Model, {name: name, ontology_id: ontology_id}) + return self._create(Model, {"name": name, "ontology_id": ontology_id}) From e313b657280630d0d108750e5f3f105789be63be Mon Sep 17 00:00:00 2001 From: Albert Wu <84879801+awu-labelbox@users.noreply.github.com> Date: Mon, 7 Jun 2021 10:44:44 -0700 Subject: [PATCH 5/5] nit: fixup docstring --- labelbox/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/labelbox/client.py b/labelbox/client.py index 8e589910c..3628226be 100644 --- a/labelbox/client.py +++ b/labelbox/client.py @@ -537,7 +537,7 @@ def get_models(self, where=None): def create_model(self, name, ontology_id): """ Creates a Model object on the server. - >>> model = client.create_model(, ") + >>> model = client.create_model(, ) Args: name (string): Name of the model