annofabapi.util package#

Submodules#

annofabapi.util.annotation_specs module#

class annofabapi.util.annotation_specs.AnnotationSpecsAccessor(annotation_specs: dict[str, Any])[source]#

Bases: object

アノテーション仕様の情報にアクセスするためのクラス。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報

get_attribute(*, attribute_id: str | None = None, attribute_name: str | None = None, label: LabelDefinition | None = None) AttributeDefinition[source]#

属性情報を取得します。

Parameters:
  • attribute_id – 属性ID

  • attribute_name – 属性名(英語)

  • label – Noneでなければ、指定したラベルに紐づく属性情報を取得します。

Raises:

ValueError – ‘attribute_id’か’attribute_name’の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。

get_label(*, label_id: str | None = None, label_name: str | None = None) LabelDefinition[source]#

ラベル情報を取得します。

Parameters:
  • label_id – ラベルID

  • label_name – ラベル名(英語)

Raises:

ValueError – ‘label_id’か’label_name’の指定方法が間違っている。または引数に合致するラベル情報が見つからない。または複数見つかった。

class annofabapi.util.annotation_specs.AttributeChoice[source]#

Bases: TypedDict

属性の選択肢です。

choice_id: str#
name: InternationalizationMessage#
class annofabapi.util.annotation_specs.AttributeDefinition[source]#

Bases: TypedDict

アノテーション仕様上の属性定義です。

additional_data_definition_id: str#
choices: list[AttributeChoice] | None#
name: InternationalizationMessage#
type: AdditionalDataDefinitionType#
class annofabapi.util.annotation_specs.InternationalizationMessage[source]#

Bases: TypedDict

多言語メッセージです。

messages: list[InternationalizationMessageItem]#
class annofabapi.util.annotation_specs.InternationalizationMessageItem[source]#

Bases: TypedDict

多言語メッセージの1要素です。

lang: str#
message: str#
class annofabapi.util.annotation_specs.LabelDefinition[source]#

Bases: TypedDict

アノテーション仕様上のラベル定義です。

additional_data_definitions: list[str]#
label_id: str#
label_name: InternationalizationMessage#
class annofabapi.util.annotation_specs.LabelNameHolder[source]#

Bases: TypedDict

ラベル名を持つ情報です。

label_name: InternationalizationMessage#
class annofabapi.util.annotation_specs.NameHolder[source]#

Bases: TypedDict

多言語化された名前を持つ情報です。

name: InternationalizationMessage#
annofabapi.util.annotation_specs.STR_LANG#

対応している lang の文字列

alias of Literal[‘en-US’, ‘ja-JP’, ‘vi-VN’]

annofabapi.util.annotation_specs.get_attribute(additionals: list[AttributeDefinition], *, attribute_id: str | None = None, attribute_name: str | None = None, label: LabelDefinition | None = None) AttributeDefinition[source]#

属性情報を取得します。

Parameters:
  • attribute_id – 属性ID

  • attribute_name – 属性名(英語)

  • label – Noneでなければ、指定したラベルに紐づく属性情報を取得します。

Raises:

ValueError – ‘attribute_id’か’attribute_name’の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。

annofabapi.util.annotation_specs.get_attribute_name_en(attribute: Mapping[str, Any]) str[source]#

属性情報から英語名を取得します。

Parameters:

attribute – 属性情報。キー name が存在している必要があります。

Returns:

属性の英語名。

Raises:

ValueError – 英語メッセージが見つからない場合

annofabapi.util.annotation_specs.get_choice(choices: list[AttributeChoice], *, choice_id: str | None = None, choice_name: str | None = None) AttributeChoice[source]#

選択肢情報を取得します。

Parameters:
  • choice_id – 選択肢ID

  • choice_name – 選択肢名(英語)

Raises:

ValueError – ‘choice_id’か’choice_name’の指定方法が間違っている。または引数に合致する選択肢情報が見つからない。または複数見つかった。

annofabapi.util.annotation_specs.get_choice_name_en(choice: Mapping[str, Any]) str[source]#

選択肢情報から英語名を取得します。

Parameters:

choice – 選択肢情報。キー name が存在している必要があります。

Returns:

選択肢の英語名。

Raises:

ValueError – 英語メッセージが見つからない場合

annofabapi.util.annotation_specs.get_english_message(internationalization_message: Mapping[str, Any]) str[source]#

InternationalizationMessage`クラスの値から、英語メッセージを取得します。 英語メッセージが見つからない場合は ``ValueError` をスローします。

Notes

英語メッセージは必ず存在するはずなので、英語メッセージが見つからない場合は ValueError をスローするようにしました。

Parameters:

internationalization_message – 多言語化されたメッセージ。キー messages が存在している必要があります。

Returns:

指定した言語に対応するメッセージ。

Raises:

ValueError – 英語メッセージが見つからない場合

annofabapi.util.annotation_specs.get_label(labels: list[LabelDefinition], *, label_id: str | None = None, label_name: str | None = None) LabelDefinition[source]#

ラベル情報を取得します。

Parameters:
  • label_id – ラベルID

  • label_name – ラベル名(英語)

Raises:

ValueError – ‘label_id’か’label_name’の指定方法が間違っている。または引数に合致するラベル情報が見つからない。または複数見つかった。

annofabapi.util.annotation_specs.get_label_name_en(label: Mapping[str, Any]) str[source]#

ラベル情報から英語名を取得します。

Parameters:

label – ラベル情報。キー label_name が存在している必要があります。

Returns:

ラベルの英語名。

Raises:

ValueError – 英語メッセージが見つからない場合

annofabapi.util.annotation_specs.get_message_with_lang(internationalization_message: InternationalizationMessage, lang: Lang | Literal['en-US', 'ja-JP', 'vi-VN']) str | None[source]#

InternationalizationMessage`クラスの値から、指定した ``lang` に対応するメッセージを取得します。

Parameters:
  • internationalization_message – 多言語化されたメッセージ。キー messages が存在している必要があります。

  • lang – 取得したいメッセージに対応する言語コード。

Returns:

指定した言語に対応するメッセージ。見つからない場合はNoneを返します。

annofabapi.util.attribute_restrictions module#

属性の制約に関するモジュール。

以下のサンプルコードのように属性名で制約情報を出力できます。

Example

>>> import annofabapi
>>> from annofabapi.util.attribute_restrictions import AttributeFactory
>>> service = annofabapi.build()
>>> annotation_specs, _ = service.api.get_annotation_specs("prj1", query_params={"v": "3"})
>>> fac = AttributeFactory(annotation_specs)
>>> premise_restriction = fac.checkbox(attribute_name="occluded").checked()
>>> conclusion_restriction = fac.string_textbox(attribute_name="note").is_not_empty()
>>> restriction = premise_restriction.imply(conclusion_restriction)
>>> restriction.to_dict()
{
    "additional_data_definition_id": "9b05648d-1e16-4ea2-ab79-48907f5eed00",
    "condition": {
        "_type": "Imply",
        "premise": {
            "additional_data_definition_id": "2517f635-2269-4142-8ef4-16312b4cc9f7",
            "condition": {"_type": "Equals", "value": "true"},
        },
        "condition": {"_type": "NotEquals", "value": ""},
    },
}

Bases: Attribute, EmptyCheckMixin

アノテーションリンク属性

has_label(label_ids: Collection[str] | None = None, label_names: Collection[str] | None = None) Restriction[source]#

リンク先のアノテーションが、引数`label_ids`または`label_names`に一致するラベルであるという制約

class annofabapi.util.attribute_restrictions.Attribute(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: ABC

disabled() Restriction[source]#

属性値を入力できないという制約

enabled() Restriction[source]#

属性値を入力できるという制約

class annofabapi.util.attribute_restrictions.AttributeFactory(annotation_specs: dict[str, Any])[source]#

Bases: object

属性を生成するためのFactoryクラス。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報です。

checkbox(*, attribute_id: str | None = None, attribute_name: str | None = None) Checkbox[source]#
create(attribute_type: AdditionalDataDefinitionType, *, attribute_id: str | None = None, attribute_name: str | None = None) Attribute[source]#

属性の種類に応じて対応する高水準属性オブジェクトを生成します。

Parameters:
  • attribute_type – 属性の種類です。

  • attribute_id – 属性IDです。

  • attribute_name – 属性名(英語)です。

Returns:

対応する高水準属性オブジェクトです。

integer_textbox(*, attribute_id: str | None = None, attribute_name: str | None = None) IntegerTextbox[source]#
selection(*, attribute_id: str | None = None, attribute_name: str | None = None) Selection[source]#
string_textbox(*, attribute_id: str | None = None, attribute_name: str | None = None) StringTextbox[source]#
tracking_id(*, attribute_id: str | None = None, attribute_name: str | None = None) TrackingId[source]#
class annofabapi.util.attribute_restrictions.AttributeRestrictionCatalogItem(*, attribute_name: str, attribute_type: AdditionalDataDefinitionType, allowed_ast_types: list[RestrictionAstType], choice_names: list[str] | None = None, label_names: list[str] | None = None)[source]#

Bases: BaseModel

LLMへ渡す属性制約カタログの1要素を表すモデル。

Parameters:
  • attribute_name – 属性名です。

  • attribute_type – 属性種類です。

  • allowed_ast_types – その属性で利用できるAST種別の一覧です。

  • choice_names – 選択系属性で利用できる選択肢名の一覧です。

  • label_names – リンク属性で利用できるラベル名の一覧です。

allowed_ast_types: list[RestrictionAstType]#
attribute_name: str#
attribute_type: AdditionalDataDefinitionType#
choice_names: list[str] | None#
label_names: list[str] | None#
model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'frozen': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

serialize_allowed_ast_types(allowed_ast_types: list[RestrictionAstType]) list[str][source]#
serialize_attribute_type(attribute_type: AdditionalDataDefinitionType) str[source]#
class annofabapi.util.attribute_restrictions.CanInput(attribute_id: str, enable: bool)[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.Checkbox(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: Attribute

チェックボックスの属性

checked() Restriction[source]#

チェックされているという制約

unchecked() Restriction[source]#

チェックされていないという制約

class annofabapi.util.attribute_restrictions.EmptyCheckMixin[source]#

Bases: object

属性が空かどうかを判定するメソッドを提供するMix-inクラス

attribute_id: str#
is_empty() Restriction[source]#

属性値が空であるという制約

is_not_empty() Restriction[source]#

属性値が空でないという制約

class annofabapi.util.attribute_restrictions.Equals(attribute_id: str, value: str)[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.HasLabel(attribute_id: str, label_ids: Collection[str])[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.Imply(premise_restriction: Restriction, conclusion_restriction: Restriction)[source]#

Bases: Restriction

「AならB」という制約を表すクラス。

Parameters:
  • premise_restriction – 前提となる制約です。

  • conclusion_restriction – 最終的に満たしたい制約です。

imply(conclusion_restriction: Restriction) Restriction[source]#
class annofabapi.util.attribute_restrictions.IntegerTextbox(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: Attribute, EmptyCheckMixin

整数用のテキストボックスの属性

equals(value: int) Restriction[source]#

引数`value`に渡された整数に一致するという制約

not_equals(value: int) Restriction[source]#

引数`value`に渡された整数に一致しないという制約

class annofabapi.util.attribute_restrictions.Matches(attribute_id: str, value: str)[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.NotEquals(attribute_id: str, value: str)[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.NotMatches(attribute_id: str, value: str)[source]#

Bases: Restriction

class annofabapi.util.attribute_restrictions.Restriction(attribute_id: str)[source]#

Bases: ABC

属性の制約を表すクラス。

classmethod from_ast(ast: RestrictionAst, annotation_specs: dict[str, Any]) Restriction[source]#

意味ベースのASTからRestrictionオブジェクトを復元します。

Parameters:
  • ast – 復元元の RestrictionAst です。

  • annotation_specs – アノテーション仕様(v3)の情報です。

Returns:

復元した Restriction オブジェクトです。

classmethod from_dict(obj: dict[str, Any]) Restriction[source]#

dictからRestrictionオブジェクトを復元します。

Parameters:

objrestrictions の1要素を表す辞書です。

Returns:

復元した Restriction オブジェクトです。

imply(conclusion_restriction: Restriction) Restriction[source]#
to_ast(annotation_specs: dict[str, Any]) RestrictionAst[source]#

Restrictionオブジェクトを、LLMやCLIで扱いやすい意味ベースのASTへ変換します。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報です。

Returns:

名前ベースで表現された RestrictionAst です。

to_dict() dict[str, Any][source]#

アノテーション仕様の`restrictions`に格納できるdictを出力します。

Returns:

restrictions に格納できる辞書形式の制約情報です。

to_human_readable(annotation_specs: dict[str, Any]) str[source]#

Restrictionオブジェクトを、人にとって読みやすい文字列表現へ変換します。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報です。

Returns:

CLIなどで表示しやすい文字列表現です。

class annofabapi.util.attribute_restrictions.RestrictionAst(*, type: RestrictionAstType, attribute_name: str | None = None, value: str | int | None = None, choice_name: str | None = None, enable: bool | None = None, label_names: list[str] | None = None, premise: RestrictionAst | None = None, conclusion: RestrictionAst | None = None)[source]#

Bases: BaseModel

LLMやCLI向けの意味ベースな属性制約ASTを表すクラス。

type に応じて必要なフィールドが変わります。例えば checked では attribute_name を使い、imply では premiseconclusion を使います。

attribute_name: str | None#
choice_name: str | None#
conclusion: RestrictionAst | None#
enable: bool | None#
label_names: list[str] | None#
model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'frozen': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

premise: RestrictionAst | None#
to_human_readable() str[source]#

ASTを人間向けの読みやすい文字列へ変換します。

Returns:

CLIなどで表示しやすい文字列表現です。

Raises:

ValueError – 未知のAST種別が指定された場合

to_restriction(annotation_specs: dict[str, Any]) Restriction[source]#

ASTをRestrictionオブジェクトへコンパイルします。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報です。

Returns:

コンパイル後の Restriction オブジェクトです。

type: RestrictionAstType#
validate_restriction_ast() RestrictionAst[source]#

RestrictionAst の構造がAST種別に整合しているか検証します。

Raises:

ValueError – AST種別に対して必須フィールドが不足している場合、または型が不正な場合

value: str | int | None#
class annofabapi.util.attribute_restrictions.RestrictionAstType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: str, Enum

属性制約ASTの種別です。

CAN_INPUT = 'can_input'#

属性が編集可能かどうかを表すAST種別です。

CHECKED = 'checked'#

チェックボックス属性がチェックされていることを表すAST種別です。

EQUALS_INTEGER = 'equals_integer'#

整数属性が指定した整数値と一致することを表すAST種別です。

EQUALS_STRING = 'equals_string'#

文字列属性またはtracking属性が指定文字列と一致することを表すAST種別です。

HAS_CHOICE = 'has_choice'#

選択属性で指定した選択肢が選ばれていることを表すAST種別です。

HAS_LABEL = 'has_label'#

リンク属性が指定したラベル群のいずれかを指すことを表すAST種別です。

IMPLY = 'imply'#

前提を満たす場合に結論を要求する含意制約を表すAST種別です。

IS_EMPTY = 'is_empty'#

属性値が空であることを表すAST種別です。

IS_NOT_EMPTY = 'is_not_empty'#

属性値が空でないことを表すAST種別です。必須制約に相当します。

MATCHES_STRING = 'matches_string'#

文字列属性が指定した正規表現に一致することを表すAST種別です。

NOT_EQUALS_INTEGER = 'not_equals_integer'#

整数属性が指定した整数値と一致しないことを表すAST種別です。

NOT_EQUALS_STRING = 'not_equals_string'#

文字列属性またはtracking属性が指定文字列と一致しないことを表すAST種別です。

NOT_HAS_CHOICE = 'not_has_choice'#

選択属性で指定した選択肢が選ばれていないことを表すAST種別です。

NOT_MATCHES_STRING = 'not_matches_string'#

文字列属性が指定した正規表現に一致しないことを表すAST種別です。

UNCHECKED = 'unchecked'#

チェックボックス属性がチェックされていないことを表すAST種別です。

property description: str#

AST種別の説明文を返します。

class annofabapi.util.attribute_restrictions.Selection(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: Attribute, EmptyCheckMixin

排他選択の属性(ドロップダウンまたラジオボタン)

has_choice(*, choice_id: str | None = None, choice_name: str | None = None) Restriction[source]#

引数`choice_id`または`choice_name`に一致する選択肢が選択されているという制約

not_has_choice(*, choice_id: str | None = None, choice_name: str | None = None) Restriction[source]#

引数`choice_id`または`choice_name`に一致する選択肢が選択されていないという制約

class annofabapi.util.attribute_restrictions.StringTextbox(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: Attribute, EmptyCheckMixin

文字列用のテキストボックス(自由記述)の属性

equals(value: str) Restriction[source]#

引数`value`に渡された文字列に一致するという制約

matches(value: str) Restriction[source]#

引数`value`に渡された正規表現に一致するという制約

not_equals(value: str) Restriction[source]#

引数`value`に渡された文字列に一致しないという制約

not_matches(value: str) Restriction[source]#

引数`value`に渡された正規表現に一致しないという制約

class annofabapi.util.attribute_restrictions.TrackingId(accessor: AnnotationSpecsAccessor, *, attribute_id: str | None = None, attribute_name: str | None = None)[source]#

Bases: Attribute, EmptyCheckMixin

トラッキングID属性

equals(value: str) Restriction[source]#

引数`value`に渡された文字列に一致するという制約

not_equals(value: str) Restriction[source]#

引数`value`に渡された文字列に一致しないという制約

annofabapi.util.attribute_restrictions.get_attribute_restriction_catalog(annotation_specs: dict[str, Any]) list[AttributeRestrictionCatalogItem][source]#

属性制約ASTを組み立てるための属性カタログを返します。

Parameters:

annotation_specs – アノテーション仕様(v3)の情報です。

Returns:

LLMへのプロンプトや入力候補生成に使いやすい属性カタログです。

annofabapi.util.page module#

Annofabの画面に関するユーティリティ関数を定義します。

annofabapi.util.page.create_3dpc_editor_url(project_id: str, task_id: str, *, input_data_id: str | None = None, annotation_id: str | None = None, base_url: str = 'https://d2rljy8mjgrfyd.cloudfront.net/3d-editor-latest/index.html') str[source]#

3次元エディタ画面のURLを生成します。

Parameters:
  • project_id – プロジェクトID

  • task_id – タスクID

  • input_data_id – 入力データID

  • annotation_id – アノテーションID。指定した場合は、input_data_idも指定する必要があります。

  • base_url – 3次元エディタ画面のベースとなるURL。この部分はプラグインにより変更可能なので、引数として受け取るようにしました。

Returns:

3次元エディタ画面のURL (例: https://d2rljy8mjgrfyd.cloudfront.net/3d-editor-latest/index.html?p=p1&t=t1#/i1/a1

annofabapi.util.page.create_image_editor_url(project_id: str, task_id: str, *, input_data_id: str | None = None, annotation_id: str | None = None) str[source]#

画像エディタ画面のURLを生成します。

Parameters:
  • project_id – プロジェクトID

  • task_id – タスクID

  • input_data_id – 入力データID

  • annotation_id – アノテーションID。指定した場合は、input_data_idも指定する必要があります。

Returns:

画像エディタ画面のURL (例: https://annofab.com/projects/p1/tasks/t1/editor?#i1/a1

annofabapi.util.page.create_video_editor_url(project_id: str, task_id: str, *, annotation_id: str | None = None, seek_seconds: float | None = None) str[source]#

動画エディタ画面のURLを生成します。

Parameters:
  • project_id – プロジェクトID

  • task_id – タスクID

  • annotation_id – アノテーションID

  • seek_seconds – 動画の再生位置(単位は秒)

Returns:

動画エディタ画面のURL (例: https://annofab.com/projects/p1/tasks/t1/timeline?#a1/3.3

annofabapi.util.task_history module#

annofabapi.util.task_history.find_rejected_task_history_indices(task_history_list: list[dict[str, Any]]) list[int][source]#

差し戻されたタスク履歴のインデックス番号(0始まり)を返します。

Parameters:

task_history_listget_task_histories APIのレスポンス

Returns:

差し戻されたタスク履歴のインデックス番号のリスト

annofabapi.util.task_history.get_task_creation_datetime(task: dict[str, Any], task_history_list: list[dict[str, Any]]) str[source]#

タスクが作成された日時を取得します。

Parameters:
  • task – タスク情報。 タスクが作成された直後は task_history_list に有効な日時が格納されていないので、 operation_updated_datetime をタスク作成日時とします。

  • task_history_listget_task_histories APIのレスポンス

Returns:

タスクの作成日時

Notes

2020-12-08以前に作成されたタスクでは、タスクの作成日時を取得できません。 2020-12-08以前に作成されたタスクでは、先頭のタスク履歴は「タスク作成」ではなく、「教師付け作業」の履歴だからです。 https://annofab.com/docs/releases/2020.html#v01020

Raises:

ValueError – 2020-12-08以前に作成されたタスクの情報を指定した場合

annofabapi.util.type_util module#

annofabapi.util.type_util.assert_noreturn(x: NoReturn) NoReturn[source]#

python 3.11 以降に追加されたassert_neverの代わり

Module contents#