Ontology

Ontology

Access the Ontology related data, and manipulate the Ontology. Instantiate this class using encord.user_client.EncordUserClient.get_ontology().

class encord.ontology.Ontology(querier, config, instance)

property ontology_hash: str

Gets the ontology hash (for example, the Ontology ID).

Return Type:
str


property title: str

Gets the title of the Ontology.

Return Type:

str


property description: str

Get the description of the Ontology.

Return Type:

str


property created_at: datetime.datetime

Gets the Ontology creation time.

Return Type:

datatime


property last_edited_at: datetime.datetime

Gets the time the ontology was last edited.

Return Type:

datatime


property structure: encord.objects.ontology_structure.OntologyStructure

Gets the structure of the Ontology.

Return Type:

[OntologyStructure]


refetch_data

The Ontology class will only fetch its properties once. Use this function if you suspect the state of those properties to be dirty.

refetch_data()

Return type:

None

 def refetch_data(self) -> None:
        """
        The Ontology class will only fetch its properties once. Use this function if you suspect the state of those
        properties to be dirty.
        """
        self._ontology_instance = self._get_ontology()

save

Sync local state to the server. This method is used when updates are made to structure, title or description fields.

save()

Return type:

None

    def save(self) -> None:
        """
        Sync local state to the server, if updates are made to structure, title or description fields
        """
        if self._ontology_instance:
            payload = dict(**self._ontology_instance)
            payload["editor"] = self._ontology_instance.structure.to_dict()  # we're using internal/legacy name here
            payload.pop("structure", None)
            self._querier.basic_put(OrmOntology, self._config.resource_id, payload)

list_groups

List all user groups in a specified Ontology.

list_groups()

Return type:

[OntologyGroup]


add_group

Add a user group (or a list of user groups) to an Ontology.

add_group(group_hash, user_role)

Parameters:

  • group_hash: A group hash, or a list of group hashes to be added to the Ontology.
  • user_role: Specify the Ontology user role to assign to the user group. Ontology user roles are either Admin, or User. See OntologyUserRole for more information.

Return type:

None

ontology_hash = convert_to_uuid(self.ontology_hash)
    if isinstance(group_hash, UUID):
        group_hash = [group_hash]
    payload = AddOntologyGroupsPayload(group_hash_list=group_hash, user_role=user_role)
    self.api_client.post(
        f"ontologies/{ontology_hash}/groups",
        params=None,
        payload=payload,
        result_type=None,
        )


remove_group

Remove one, or multiple groups from a specified Ontology.

remove_group(group_hash)

Parameters:

  • group_hash: A group hash, or a list of group hashes to be added to the Ontology.

Return type:

None


ontology_hash = convert_to_uuid(self.ontology_hash)
    if isinstance(group_hash, UUID):
        group_hash = [group_hash]
    params = RemoveGroupsParams(group_hash_list=group_hash)
    self.api_client.delete(f"ontologies/{ontology_hash}/groups", params=params, result_type=None)

Source


import datetime

from encord.configs import SshConfig
from encord.http.querier import Querier
from encord.objects.ontology_structure import OntologyStructure
from encord.orm.ontology import Ontology as OrmOntology


class Ontology:
    """
    Access ontology related data and manipulate the ontology. Instantiate this class via
    :meth:`encord.user_client.EncordUserClient.get_ontology()`
    """

    def __init__(self, querier: Querier, config: SshConfig, instance: OrmOntology):
        self._querier = querier
        self._config = config
        self._ontology_instance = instance

    @property
    def ontology_hash(self) -> str:
        """
        Get the ontology hash (i.e. the Ontology ID).
        """
        return self._ontology_instance.ontology_hash

    @property
    def title(self) -> str:
        """
        Get the title of the ontology.
        """
        return self._ontology_instance.title

    @title.setter
    def title(self, value: str) -> None:
        self._ontology_instance.title = value

    @property
    def description(self) -> str:
        """
        Get the description of the ontology.
        """
        return self._ontology_instance.description

    @description.setter
    def description(self, value: str) -> None:
        self._ontology_instance.description = value

    @property
    def created_at(self) -> datetime.datetime:
        """
        Get the time the ontology was created at.
        """
        return self._ontology_instance.created_at

    @property
    def last_edited_at(self) -> datetime.datetime:
        """
        Get the time the ontology was last edited at.
        """
        return self._ontology_instance.last_edited_at

    @property
    def structure(self) -> OntologyStructure:
        """
        Get the structure of the ontology.
        """
        return self._ontology_instance.structure

def refetch_data(self) -> None:
        """
        The Ontology class will only fetch its properties once. Use this function if you suspect the state of those
        properties to be dirty.
        """
        self._ontology_instance = self._get_ontology()

def save(self) -> None:
        """
        Sync local state to the server, if updates are made to structure, title or description fields
        """
        if self._ontology_instance:
            payload = dict(**self._ontology_instance)
            payload["editor"] = self._ontology_instance.structure.to_dict()  # we're using internal/legacy name here
            payload.pop("structure", None)
            self._querier.basic_put(OrmOntology, self._config.resource_id, payload)

def _get_ontology(self):
        return self._querier.basic_getter(OrmOntology, self._config.resource_id)