Skip to content

xpdeep

Xpdeep.

Modules:

Name Description
client_factory

The client factory module is responsible for creating a client instance.

dataset

Manage data upload to be processed by xpdeep.

explain

Define the explain package, used to compute explanations.

filtering

Filtering module.

initialize

Initialize the API.

metric

Metrics to use for training or explanations.

model

The model package provides tools for working with Xpdeep models.

project

Define a project.

trainer

Define the trainer package, used to train the XpdeepModel.

utils

Utils package.

Classes:

Name Description
Project

A project is used to organize your artifacts.

Functions:

Name Description
init

Initialize the Xpdeep API with an API key.

get_project

Get the current project.

set_project

Set the current project.

__all__ = ('Project', 'get_project', 'init', 'set_project') #

Project(model: ProjectModel) #

A project is used to organize your artifacts.

Methods:

Name Description
create_or_get

Create project with the given name and description. Load project if the given name already exists.

load_all

Load all projects.

update

Update the current project.

__enter__

Set Project.CURRENT to this project.

__exit__

Restore previous Project.CURRENT.

list_datasets

List all datasets in this project.

list_trained_models

List all trained models in this project.

list_explanations

List all explanations in this project.

delete

Delete this project.

Attributes:

Name Type Description
CURRENT ContextVar[Project]
model
Source code in src/xpdeep/project.py
def __init__(self, model: ProjectModel) -> None:
    self.model = model

CURRENT: ContextVar[Project] = ContextVar('CURRENT_PROJECT') #

model = model #

create_or_get(*, name: str, description: str = '') -> Project #

Create project with the given name and description. Load project if the given name already exists.

Parameters:

Name Type Description Default
name #
str

Name of the project to create. If a project within this name already exists, it will be loaded

required
description #
str

Description of the new created project. Ignored in case of project loading.

""
Source code in src/xpdeep/project.py
@staticmethod
@initialized_client_verification
def create_or_get(*, name: str, description: str = "") -> "Project":
    """Create project with the given name and description. Load project if the given name already exists.

    Parameters
    ----------
    name : str
        Name of the project to create. If a project within this name already exists, it will be loaded
    description : str, default ""
        Description of the new created project. Ignored in case of project loading.
    """
    try:
        project_model = next(project for project in Project.load_all() if project.model.name == name).model
    except StopIteration:
        project_model = cast(
            "ProjectModel",
            create_project.sync(
                body=ProjectCreateRequestBody(
                    name=name,
                    description=description,
                ),
                client=ClientFactory.CURRENT.get()(),
            ),
        )

    return Project(project_model)

load_all() -> list[Project] #

Load all projects.

Source code in src/xpdeep/project.py
@classmethod
@initialized_client_verification
def load_all(cls) -> list["Project"]:
    """Load all projects."""
    project_models = cast("list[ProjectModel]", get_all_projects.sync(client=ClientFactory.CURRENT.get()()))
    return [cls(project_model) for project_model in project_models]

update(*, name: str | None = None, description: str | None = None) -> None #

Update the current project.

Parameters:

Name Type Description Default
name #
str | None

New project's name.

None
description #
str | None

New project's description.

None
Source code in src/xpdeep/project.py
@initialized_client_verification
def update(self, *, name: str | None = None, description: str | None = None) -> None:
    """Update the current project.

    Parameters
    ----------
    name : str | None, default None
        New project's name.
    description : str | None, default None
        New project's description.
    """
    if name is not None or description is not None:
        self.model = cast(
            "ProjectModel",
            update_project.sync(
                self.model.id,
                client=ClientFactory.CURRENT.get()(),
                body=ProjectUpdateRequestBody(name=name, description=description),
            ),
        )

__enter__() -> Self #

Set Project.CURRENT to this project.

Source code in src/xpdeep/project.py
def __enter__(self) -> Self:
    """Set ``Project.CURRENT`` to this project."""
    self.token = Project.CURRENT.set(self)

    return self

__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> None #

Restore previous Project.CURRENT.

Source code in src/xpdeep/project.py
def __exit__(
    self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> None:
    """Restore previous Project.CURRENT."""
    Project.CURRENT.reset(self.token)

list_datasets() -> list[DatasetModel] #

List all datasets in this project.

Source code in src/xpdeep/project.py
@initialized_client_verification
def list_datasets(self) -> list[DatasetModel]:
    """List all datasets in this project."""
    client_factory = ClientFactory.CURRENT.get()
    with client_factory() as client:
        return cast("list[DatasetModel]", get_many_datasets.sync(self.model.id, client=client))

list_trained_models() -> list[TrainedModelModel] #

List all trained models in this project.

Source code in src/xpdeep/project.py
@initialized_client_verification
def list_trained_models(self) -> list[TrainedModelModel]:
    """List all trained models in this project."""
    client_factory = ClientFactory.CURRENT.get()
    with client_factory() as client:
        return cast("list[TrainedModelModel]", get_many_trained_models.sync(self.model.id, client=client))

list_explanations() -> list[ExplanationModel] #

List all explanations in this project.

Source code in src/xpdeep/project.py
@initialized_client_verification
def list_explanations(self) -> list[ExplanationModel]:
    """List all explanations in this project."""
    client_factory = ClientFactory.CURRENT.get()
    with client_factory() as client:
        return cast("list[ExplanationModel]", get_many_explanations.sync(self.model.id, client=client))

delete() -> None #

Delete this project.

Also deletes all artifacts in this project recursively. WARNING: not recoverable

Source code in src/xpdeep/project.py
@initialized_client_verification
def delete(self) -> None:
    """Delete this project.

    Also deletes all artifacts in this project recursively. WARNING: not recoverable
    """
    client_factory = ClientFactory.CURRENT.get()
    with client_factory() as client:
        delete_project.sync(self.model.id, client=client)
    del self.model

init(api_key: str, api_url: str = 'https://xpdeep.com/api') -> None #

Initialize the Xpdeep API with an API key.

To generate an API key, please refer to the documentation.

https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key

Source code in src/xpdeep/initialize.py
def init(
    api_key: str,
    api_url: str = "https://xpdeep.com/api",
) -> None:
    """Initialize the Xpdeep API with an API key.

    To generate an API key, please refer to the documentation.

    https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key
    """
    ClientFactory.CURRENT.set(ClientFactory(api_key, api_url))
    with ClientFactory.CURRENT.get()() as client:
        try:
            response = check_auth.sync(client=client)
        except UnexpectedStatus as exc:
            if exc.status_code == 401:  # noqa: PLR2004
                msg = (
                    "Please check your API key:\n"
                    "https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key"
                )
                raise ApiError(msg) from exc
            if exc.status_code == 404:  # noqa: PLR2004
                msg = "Please check your API URL. Does it end with '/api'?"
                raise ApiError(msg) from exc
            raise
        if response is not None:
            logger.debug("authenticated: user_id=%s, tenant_id=%s", response.id, response.tenant_id)

get_project() -> Project | None #

Get the current project.

Source code in src/xpdeep/project.py
def get_project() -> Project | None:
    """Get the current project."""
    try:
        return Project.CURRENT.get()
    except LookupError:
        return None

set_project(project: Project) -> None #

Set the current project.

Source code in src/xpdeep/project.py
def set_project(project: Project) -> None:
    """Set the current project."""
    Project.CURRENT.set(project)