Skip to content

ユーザーフィードバックに基づく label2id の修正#1

Open
soutaito wants to merge 3 commits intomainfrom
feature/label2id-fixes
Open

ユーザーフィードバックに基づく label2id の修正#1
soutaito wants to merge 3 commits intomainfrom
feature/label2id-fixes

Conversation

@soutaito
Copy link
Copy Markdown

@soutaito soutaito commented Mar 17, 2026

概要

ユーザーフィードバックに基づき、label2id 機能およびその他の機能修正を実施しました。

変更内容(第2弾)

1. taxonomy 必須チェックの追加

  • dataset.yamllabel_resolver.taxonomytrue のデータセットで、taxonomy が未指定の場合に ValueError を raise するように修正

2. PubDictionaries API に use_ngram_similarity=true を追加

  • PubDictionaries の仕様変更に対応

変更内容(第3弾)

3. config_list_targets で逆向きリンクを表示

  • 順向き(src-dst)だけでなく逆向き(dst-src)のリンクも表示するように修正

4. execute_queryformat パラメータ追加

  • format="dict"(デフォルト、後方互換)と format="dataframe"(pandas DataFrame 出力)を選択可能に

5. label_converter.convertlabel_types マッピング対応

  • PubDictionaries 使用時に、dataset.yaml の label_type 値(短縮名、例: "label", "exact_synonym")を指定可能に
  • 内部で対応する dictionary 名(例: "togoid_chebi_label")に自動変換
  • 直接 dictionary 名を渡した場合も後方互換で動作

テスト方法

全テストを実行

cd /path/to/togoid-lib-python

# 第2弾テスト
python3 test_additional_feedback.py

個別テスト

テスト1: taxonomy 必須チェック

from togoid import LabelConverter

converter = LabelConverter()

# エラーが発生することを確認
try:
    result = converter.convert(
        labels=['AR', 'OCT4'],
        dataset='ncbigene'
        # taxonomy 未指定
    )
    print('FAILED: Should have raised an error')
except ValueError as e:
    print('SUCCESS: Error raised as expected')
    print(f'Error message: {e}')

テスト2: PubDictionaries API

from togoid import LabelConverter

converter = LabelConverter()
result = converter.convert(
    labels=['ATP', 'water'],
    dataset='chebi'
)
print(f'Number of results: {len(result)}')
print(result[:5])

テスト3: config_list_targets 逆向きリンク

from togoid import TogoIDConverter

converter = TogoIDConverter()

# tair は逆向きリンクのみ存在
targets = converter.config_list_targets('tair')
print(targets)
# ['ncbigene', 'rnacentral']

テスト4: execute_query に format パラメータ

from togoid import AnnotationsConverter

ann = AnnotationsConverter()

# dict 形式(デフォルト)
result_dict = ann.execute_query('ncbigene', ['672', '7157'], ['label'])
print(type(result_dict))
# <class 'dict'>
print(result_dict)
# {'672': {'label': 'BRCA1'}, '7157': {'label': 'TP53'}}

# dataframe 形式
result_df = ann.execute_query('ncbigene', ['672', '7157'], ['label'], format='dataframe')
print(type(result_df))
# <class 'pandas.core.frame.DataFrame'>
print(result_df)
#      id  label
# 0   672  BRCA1
# 1  7157   TP53

テスト5: label_types の短縮名マッピング

from togoid import LabelConverter

converter = LabelConverter()

# label_type の短縮名で辞書をフィルタリング
# "label" は内部で "togoid_chebi_label" に変換される
result = converter.convert(
    labels=['ATP'],
    dataset='chebi',
    label_types=['label']
)
print(f'Number of results: {len(result)}')
print(result)

# 直接 dictionary 名を渡しても動作(後方互換)
result2 = converter.convert(
    labels=['ATP'],
    dataset='chebi',
    label_types=['togoid_chebi_label']
)
print(f'Number of results: {len(result2)}')

R版の対応

R版も同様の修正を実施しました:

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com


第4弾フィードバック対応 (追加コミット 4af8872)

修正点

P-A: PubDictionaries 出力を Web UI と同じ形式に

  • match_typedataset.yamllabel 値("Name" / "Exact synonym" / "Broad synonym" 等)で表示
  • dictionary 列を削除
  • preferred: true の辞書から find_terms API で正規ラベルを取得して列追加(列名は preferred dict の label、例: Name
  • find_terms のクエリパラメータを idsidentifiers に修正(PubDictionaries の正しい仕様)

P-B: AnnotationsConverter.execute_query(format='dataframe') の改善

  • 取得不能 ID を NA 行で出力 DataFrame に含める(list field は空配列 []、scalar field は None
  • 入力 ID 順を維持して出力

P-C: TogoIDConverter.convert(annotate=...) のカラム名修正

  • "ncbigene label""ncbigene.label" (R版と統一、ドット区切り)

テスト方法 (第4弾)

python3 test_round4.py   # 全項目を一括検証

個別の検証コマンド

P-A: PubDictionaries Web UI 形式

from togoid import LabelConverter
lc = LabelConverter()
df = lc.convert(labels=["Lung Cancer"], dataset="mondo", format="dataframe")
print(df)
#          input     match_type            Name     score identifier
# 0  Lung Cancer           Name     lung cancer  0.941176    0008903
# 1  Lung Cancer  Exact synonym     lung cancer  0.941176    0008903
# 2  Lung Cancer  Broad synonym  lung carcinoma  0.941176    0005138

P-B: execute_query 欠損 ID

from togoid.annotations import AnnotationsConverter
ann = AnnotationsConverter()
df = ann.execute_query(
    dataset_name="ncbigene",
    ids=["1", "8", "9"],
    fields=["label", "gene_synonym"],
    format="dataframe",
)
print(df)
#   id label               gene_synonym
# 0  1  A1BG  [GAB, HYST2477, A1B, ABG]
# 1  8   NaN                         []
# 2  9  NAT1  [NATI, MNAT, NAT-1, AAC1]

P-C: annotation カラム名

from togoid import TogoIDConverter
conv = TogoIDConverter()
result = conv.convert(
    ids=["1", "9"],
    route=["ncbigene", "ensembl_gene"],
    annotate=[("ncbigene", "label")],
    format="dataframe",
)
print(list(result.columns))
# ['ncbigene', 'ncbigene.label', 'ensembl_gene']

関連 PR

soutaito and others added 2 commits March 17, 2026 15:58
- taxonomy 必須チェックを追加:taxonomy が必須のデータセットで
  未指定時にエラーを出すように修正
- PubDictionaries API 呼び出しに use_ngram_similarity=true パラメータを追加

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- config_list_targets: 逆向きリンクの表示対応
- execute_query: format="dataframe"パラメータ追加(pandas DataFrame出力対応)
- label_converter: label_typesの短縮名マッピング対応(PubDictionaries)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- LabelConverter.convert_pubdictionaries 出力を Web UI 形式に変更
  - match_type 列を dataset.yaml の label 値 (Name/Exact synonym 等) に
  - dictionary 列を削除
  - 正規ラベル列を find_terms で取得して追加 (列名は preferred dict の label)
  - find_terms のパラメータを 'identifiers' に修正
- AnnotationsConverter.execute_query (format='dataframe') の改善
  - 取得失敗 ID を NA 行 (list field は空配列) で出力に含める
  - 入力 ID 順を維持
- TogoIDConverter.convert (annotate) のカラム名をドット区切りに
  - "ncbigene label" → "ncbigene.label"

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant