|
|
|
|
|
""" |
|
|
Visualizzazione 3D dello spazio Feature Typology |
|
|
(mean_consistency, max_affinity, logit_influence) |
|
|
""" |
|
|
|
|
|
import json |
|
|
import numpy as np |
|
|
import matplotlib.pyplot as plt |
|
|
from mpl_toolkits.mplot3d import Axes3D |
|
|
|
|
|
def create_3d_visualization(): |
|
|
"""Crea visualizzazione 3D dello spazio feature typology""" |
|
|
|
|
|
print("=" * 60) |
|
|
print("VISUALIZZAZIONE 3D FEATURE TYPOLOGY SPACE") |
|
|
print("=" * 60) |
|
|
|
|
|
|
|
|
print("\nπ Caricamento dati...") |
|
|
try: |
|
|
with open("output/feature_typology.json", 'r', encoding='utf-8') as f: |
|
|
typology = json.load(f) |
|
|
print(f" β
Caricati {len(typology)} feature") |
|
|
except FileNotFoundError: |
|
|
print(" β File feature_typology.json non trovato!") |
|
|
print(" Esegui prima: python anthropological_basic.py") |
|
|
return |
|
|
|
|
|
|
|
|
coords = np.array([v['coordinates'] for v in typology.values()]) |
|
|
types = [v['type'] for v in typology.values()] |
|
|
|
|
|
|
|
|
type_counts = {} |
|
|
for t in types: |
|
|
type_counts[t] = type_counts.get(t, 0) + 1 |
|
|
|
|
|
print("\nπ Distribuzione per tipo:") |
|
|
for t, count in sorted(type_counts.items()): |
|
|
pct = count / len(types) * 100 |
|
|
print(f" {t.capitalize()}: {count} ({pct:.1f}%)") |
|
|
|
|
|
|
|
|
color_map = { |
|
|
'generalist': '#1f77b4', |
|
|
'specialist': '#ff7f0e', |
|
|
'computational': '#2ca02c', |
|
|
'hybrid': '#d62728' |
|
|
} |
|
|
colors = [color_map.get(t, '#888888') for t in types] |
|
|
|
|
|
|
|
|
print("\nπ¨ Creazione visualizzazione 3D...") |
|
|
fig = plt.figure(figsize=(14, 10)) |
|
|
ax = fig.add_subplot(111, projection='3d') |
|
|
|
|
|
|
|
|
scatter = ax.scatter(coords[:, 0], coords[:, 1], coords[:, 2], |
|
|
c=colors, alpha=0.6, s=20, edgecolors='w', linewidth=0.5) |
|
|
|
|
|
|
|
|
ax.set_xlabel('Mean Consistency\n(Generalizability)', fontsize=11, labelpad=10) |
|
|
ax.set_ylabel('Max Affinity\n(Specialization)', fontsize=11, labelpad=10) |
|
|
ax.set_zlabel('Logit Influence\n(Causal Importance)', fontsize=11, labelpad=10) |
|
|
ax.set_title('Feature Typology Space\n3D Behavioral Coordinates', |
|
|
fontsize=14, fontweight='bold', pad=20) |
|
|
|
|
|
|
|
|
ax.grid(True, alpha=0.3) |
|
|
|
|
|
|
|
|
from matplotlib.patches import Patch |
|
|
legend_elements = [] |
|
|
for t in sorted(color_map.keys()): |
|
|
if t in type_counts: |
|
|
count = type_counts[t] |
|
|
pct = count / len(types) * 100 |
|
|
label = f"{t.capitalize()} ({count}, {pct:.1f}%)" |
|
|
legend_elements.append(Patch(facecolor=color_map[t], label=label)) |
|
|
|
|
|
ax.legend(handles=legend_elements, loc='upper left', |
|
|
frameon=True, framealpha=0.9, fontsize=10) |
|
|
|
|
|
|
|
|
ax.set_xlim([0, 1]) |
|
|
ax.set_ylim([0, 1]) |
|
|
|
|
|
|
|
|
|
|
|
ax.view_init(elev=20, azim=45) |
|
|
|
|
|
plt.tight_layout() |
|
|
|
|
|
|
|
|
output_path = 'output/feature_typology_3d.png' |
|
|
plt.savefig(output_path, dpi=150, bbox_inches='tight') |
|
|
print(f" β
Salvata: {output_path}") |
|
|
|
|
|
|
|
|
try: |
|
|
output_path_svg = 'output/feature_typology_3d.svg' |
|
|
plt.savefig(output_path_svg, format='svg', bbox_inches='tight') |
|
|
print(f" β
Salvata versione SVG: {output_path_svg}") |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
print("\nπ Centroidi per tipo (mean coordinates):") |
|
|
for t in sorted(color_map.keys()): |
|
|
if t in type_counts: |
|
|
type_coords = coords[[i for i, typ in enumerate(types) if typ == t]] |
|
|
if len(type_coords) > 0: |
|
|
centroid = type_coords.mean(axis=0) |
|
|
print(f" {t.capitalize()}:") |
|
|
print(f" Mean Consistency: {centroid[0]:.3f}") |
|
|
print(f" Max Affinity: {centroid[1]:.3f}") |
|
|
print(f" Logit Influence: {centroid[2]:.3f}") |
|
|
|
|
|
print("\nβ
Visualizzazione 3D completata!") |
|
|
|
|
|
return fig |
|
|
|
|
|
def create_2d_projections(): |
|
|
"""Crea proiezioni 2D degli assi principali""" |
|
|
|
|
|
print("\nπ Creazione proiezioni 2D...") |
|
|
|
|
|
with open("output/feature_typology.json", 'r', encoding='utf-8') as f: |
|
|
typology = json.load(f) |
|
|
|
|
|
coords = np.array([v['coordinates'] for v in typology.values()]) |
|
|
types = [v['type'] for v in typology.values()] |
|
|
|
|
|
color_map = { |
|
|
'generalist': '#1f77b4', |
|
|
'specialist': '#ff7f0e', |
|
|
'computational': '#2ca02c', |
|
|
'hybrid': '#d62728' |
|
|
} |
|
|
colors = [color_map.get(t, '#888888') for t in types] |
|
|
|
|
|
|
|
|
fig, axes = plt.subplots(1, 3, figsize=(18, 5)) |
|
|
|
|
|
|
|
|
axes[0].scatter(coords[:, 0], coords[:, 1], c=colors, alpha=0.6, s=30, edgecolors='w', linewidth=0.5) |
|
|
axes[0].set_xlabel('Mean Consistency (Generalizability)', fontsize=10) |
|
|
axes[0].set_ylabel('Max Affinity (Specialization)', fontsize=10) |
|
|
axes[0].set_title('Consistency vs Affinity', fontsize=11, fontweight='bold') |
|
|
axes[0].grid(True, alpha=0.3) |
|
|
axes[0].set_xlim([0, 1]) |
|
|
axes[0].set_ylim([0, 1]) |
|
|
|
|
|
|
|
|
axes[1].scatter(coords[:, 0], coords[:, 2], c=colors, alpha=0.6, s=30, edgecolors='w', linewidth=0.5) |
|
|
axes[1].set_xlabel('Mean Consistency (Generalizability)', fontsize=10) |
|
|
axes[1].set_ylabel('Logit Influence (Causal Importance)', fontsize=10) |
|
|
axes[1].set_title('Consistency vs Influence', fontsize=11, fontweight='bold') |
|
|
axes[1].grid(True, alpha=0.3) |
|
|
axes[1].set_xlim([0, 1]) |
|
|
|
|
|
|
|
|
axes[2].scatter(coords[:, 1], coords[:, 2], c=colors, alpha=0.6, s=30, edgecolors='w', linewidth=0.5) |
|
|
axes[2].set_xlabel('Max Affinity (Specialization)', fontsize=10) |
|
|
axes[2].set_ylabel('Logit Influence (Causal Importance)', fontsize=10) |
|
|
axes[2].set_title('Affinity vs Influence', fontsize=11, fontweight='bold') |
|
|
axes[2].grid(True, alpha=0.3) |
|
|
axes[2].set_xlim([0, 1]) |
|
|
|
|
|
|
|
|
from matplotlib.patches import Patch |
|
|
legend_elements = [Patch(facecolor=color_map[t], label=t.capitalize()) |
|
|
for t in sorted(color_map.keys())] |
|
|
fig.legend(handles=legend_elements, loc='upper center', ncol=4, |
|
|
frameon=True, framealpha=0.9, fontsize=10, bbox_to_anchor=(0.5, 0.98)) |
|
|
|
|
|
plt.tight_layout(rect=[0, 0, 1, 0.96]) |
|
|
|
|
|
output_path = 'output/feature_typology_2d_projections.png' |
|
|
plt.savefig(output_path, dpi=150, bbox_inches='tight') |
|
|
print(f" β
Salvata: {output_path}") |
|
|
|
|
|
return fig |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("π¨ Avvio visualizzazione Feature Typology Space...") |
|
|
|
|
|
try: |
|
|
|
|
|
fig_3d = create_3d_visualization() |
|
|
|
|
|
|
|
|
fig_2d = create_2d_projections() |
|
|
|
|
|
print("\nπ COMPLETATO!") |
|
|
print(" Visualizzazioni salvate in output/") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"\nβ Errore: {e}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
|
|
|
|