Skip to content

Commit bd8831c

Browse files
committed
add get_unused_ontologies and get_unused_feature_schemas
1 parent c728290 commit bd8831c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

labelbox/client.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,62 @@ def insert_feature_schema_into_ontology(self, feature_schema_id: str,
10311031
"Failed to insert the feature schema into the ontology, message: "
10321032
+ str(response.json()['message']))
10331033

1034+
def get_unused_ontologies(self, after: str = None) -> List[str]:
1035+
"""
1036+
Returns a list of unused ontology ids
1037+
Args:
1038+
after (str): The cursor to use for pagination
1039+
Returns:
1040+
A list of unused ontology ids
1041+
Example:
1042+
To get the first page of unused ontology ids (100 at a time)
1043+
>>> client.get_unused_ontologies()
1044+
To get the next page of unused ontology ids
1045+
>>> client.get_unused_ontologies("cleabc1my012ioqvu5anyaabc")
1046+
"""
1047+
1048+
endpoint = self.rest_endpoint + "/ontologies/unused"
1049+
response = requests.get(
1050+
endpoint,
1051+
headers=self.headers,
1052+
json={"after": after},
1053+
)
1054+
1055+
if response.status_code == requests.codes.ok:
1056+
return response.json()
1057+
else:
1058+
raise labelbox.exceptions.LabelboxError(
1059+
"Failed to get unused ontologies, message: " +
1060+
str(response.json()['message']))
1061+
1062+
def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1063+
"""
1064+
Returns a list of unused feature schema ids
1065+
Args:
1066+
after (str): The cursor to use for pagination
1067+
Returns:
1068+
A list of unused feature schema ids
1069+
Example:
1070+
To get the first page of unused feature schema ids (100 at a time)
1071+
>>> client.get_unused_feature_schemas()
1072+
To get the next page of unused feature schema ids
1073+
>>> client.get_unused_feature_schemas("cleabc1my012ioqvu5anyaabc")
1074+
"""
1075+
1076+
endpoint = self.rest_endpoint + "/feature-schemas/unused"
1077+
response = requests.get(
1078+
endpoint,
1079+
headers=self.headers,
1080+
json={"after": after},
1081+
)
1082+
1083+
if response.status_code == requests.codes.ok:
1084+
return response.json()
1085+
else:
1086+
raise labelbox.exceptions.LabelboxError(
1087+
"Failed to get unused feature schemas, message: " +
1088+
str(response.json()['message']))
1089+
10341090
def create_ontology(self, name, normalized, media_type=None) -> Ontology:
10351091
"""
10361092
Creates an ontology from normalized data

tests/integration/test_feature_schema.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,18 @@ def test_updates_a_feature_schema(client):
112112
tool_to_update.asdict())
113113

114114
assert updated_feature_schema.normalized['name'] == "new name"
115+
116+
117+
def test_does_not_include_used_feature_schema(client):
118+
tool = client.upsert_feature_schema(point.asdict())
119+
feature_schema_id = tool.normalized['featureSchemaId']
120+
ontology = client.create_ontology_from_feature_schemas(
121+
name='ontology name',
122+
feature_schema_ids=[feature_schema_id],
123+
media_type=MediaType.Image)
124+
unused_feature_schemas = client.get_unused_feature_schemas()
125+
126+
assert feature_schema_id not in unused_feature_schemas
127+
128+
client.delete_unused_ontology(ontology.uid)
129+
client.delete_unused_feature_schema(feature_schema_id)

tests/integration/test_ontology.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ def test_moves_already_added_feature_schema_in_ontology(client):
108108
client.delete_unused_ontology(ontology.uid)
109109

110110

111+
def test_does_not_include_used_ontologies(client):
112+
tool = client.upsert_feature_schema(point.asdict())
113+
feature_schema_id = tool.normalized['featureSchemaId']
114+
ontology_with_project = client.create_ontology_from_feature_schemas(
115+
name='ontology name',
116+
feature_schema_ids=[feature_schema_id],
117+
media_type=MediaType.Image)
118+
project = client.create_project(name="test project",
119+
media_type=MediaType.Image)
120+
project.setup_editor(ontology_with_project)
121+
unused_ontologies = client.get_unused_ontologies()
122+
123+
assert ontology_with_project.uid not in unused_ontologies
124+
125+
project.delete()
126+
client.delete_unused_ontology(ontology_with_project.uid)
127+
client.delete_unused_feature_schema(feature_schema_id)
128+
129+
111130
def _get_attr_stringify_json(obj, attr):
112131
value = getattr(obj, attr.name)
113132
if attr.field_type.name.lower() == "json":

0 commit comments

Comments
 (0)