Upload visualization.py with huggingface_hub
Browse files- visualization.py +88 -0
visualization.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# 他のモジュールからタイル生成機能をインポート
|
| 6 |
+
from knowledge_tile_generator import generate_sample_tile
|
| 7 |
+
|
| 8 |
+
def plot_medical_space(tiles: list):
|
| 9 |
+
"""
|
| 10 |
+
Knowledge Tileのリストを受け取り、その座標を3D空間に可視化します。
|
| 11 |
+
点の色は確実性(certainty)スコアに基づいて決定されます。
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
tiles (list): Knowledge Tileオブジェクトのリスト。
|
| 15 |
+
|
| 16 |
+
注意:
|
| 17 |
+
この関数を実行するには matplotlib が必要です。
|
| 18 |
+
pip install matplotlib
|
| 19 |
+
"""
|
| 20 |
+
if not tiles:
|
| 21 |
+
print("可視化するタイルがありません。")
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
fig = plt.figure(figsize=(12, 9))
|
| 25 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 26 |
+
|
| 27 |
+
# データを抽出
|
| 28 |
+
coords_x = [t['coordinates']['medical_space'][0] for t in tiles]
|
| 29 |
+
coords_y = [t['coordinates']['medical_space'][1] for t in tiles]
|
| 30 |
+
coords_z = [t['coordinates']['medical_space'][2] for t in tiles]
|
| 31 |
+
certainty = [t['coordinates']['meta_space'][0] for t in tiles]
|
| 32 |
+
labels = [t['metadata']['topic'] for t in tiles]
|
| 33 |
+
|
| 34 |
+
# 散布図をプロット
|
| 35 |
+
# c: 色, cmap: カラーマップ, s: サイズ, alpha: 透明度
|
| 36 |
+
scatter = ax.scatter(coords_x, coords_y, coords_z,
|
| 37 |
+
c=certainty, cmap='RdYlGn', s=100, alpha=0.7, vmin=0, vmax=100)
|
| 38 |
+
|
| 39 |
+
# 軸ラベルとタイトル
|
| 40 |
+
ax.set_xlabel('臓器系 (Organ System, X)')
|
| 41 |
+
ax.set_ylabel('病態深さ (Pathophysiology Depth, Y)')
|
| 42 |
+
ax.set_zlabel('臨床時間軸 (Clinical Timeline, Z)')
|
| 43 |
+
ax.set_title('Medical Knowledge Space Visualization')
|
| 44 |
+
|
| 45 |
+
# カラーバーを追加
|
| 46 |
+
cbar = fig.colorbar(scatter, ax=ax, shrink=0.6)
|
| 47 |
+
cbar.set_label('確実性 (Certainty)')
|
| 48 |
+
|
| 49 |
+
# 各点にラベルを付ける (数が多すぎない場合)
|
| 50 |
+
if len(tiles) <= 20:
|
| 51 |
+
for i, label in enumerate(labels):
|
| 52 |
+
ax.text(coords_x[i], coords_y[i], coords_z[i], f' {label}', size=8)
|
| 53 |
+
|
| 54 |
+
plt.show()
|
| 55 |
+
|
| 56 |
+
def generate_random_tiles(num_tiles=20):
|
| 57 |
+
"""可視化テスト用にランダムなタイルを生成します。"""
|
| 58 |
+
random_tiles = []
|
| 59 |
+
for i in range(num_tiles):
|
| 60 |
+
tile = generate_sample_tile()
|
| 61 |
+
# 座標をランダム化
|
| 62 |
+
tile['metadata']['topic'] = f"Topic {i}"
|
| 63 |
+
tile['coordinates']['medical_space'] = (
|
| 64 |
+
np.random.uniform(10, 100),
|
| 65 |
+
np.random.uniform(10, 100),
|
| 66 |
+
np.random.uniform(10, 100)
|
| 67 |
+
)
|
| 68 |
+
tile['coordinates']['meta_space'] = (
|
| 69 |
+
np.random.uniform(20, 100), # certainty
|
| 70 |
+
np.random.uniform(50, 300), # granularity
|
| 71 |
+
np.random.uniform(50, 100) # verification
|
| 72 |
+
)
|
| 73 |
+
random_tiles.append(tile)
|
| 74 |
+
return random_tiles
|
| 75 |
+
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
+
print("--- 知識空間可視化ツールのデモ ---")
|
| 78 |
+
print("matplotlib が必要です。 `pip install matplotlib`")
|
| 79 |
+
|
| 80 |
+
# 1. 単一のサンプルタイルを生成して表示
|
| 81 |
+
# print("\n1. 単一のサンプルタイルを可視化します...")
|
| 82 |
+
# single_tile = [generate_sample_tile()]
|
| 83 |
+
# plot_medical_space(single_tile)
|
| 84 |
+
|
| 85 |
+
# 2. 複数のランダムなタイルを生成して表示
|
| 86 |
+
print("\n2. 複数のランダムなタイルを生成して可視化します...")
|
| 87 |
+
random_sample_tiles = generate_random_tiles(15)
|
| 88 |
+
plot_medical_space(random_sample_tiles)
|