Spaces:
Sleeping
Sleeping
Delete visualizer.py
Browse files- visualizer.py +0 -222
visualizer.py
DELETED
|
@@ -1,222 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Module de visualisation pour la séquence récursive
|
| 3 |
-
Génère différentes représentations visuelles: cercles colorés, formes géométriques, grilles de pixels
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import matplotlib.pyplot as plt
|
| 7 |
-
import matplotlib.patches as patches
|
| 8 |
-
import numpy as np
|
| 9 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 10 |
-
import io
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class SequenceVisualizer:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
# Couleurs par défaut pour chaque lettre
|
| 16 |
-
self.default_colors = {
|
| 17 |
-
'A': '#FF6B6B', # Rouge
|
| 18 |
-
'L': '#4ECDC4', # Turquoise
|
| 19 |
-
'M': '#45B7D1', # Bleu
|
| 20 |
-
'I': '#FFA07A', # Orange clair
|
| 21 |
-
'F': '#98D8C8' # Vert menthe
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# Formes géométriques selon les nombres
|
| 25 |
-
self.shapes = {
|
| 26 |
-
1: 'point',
|
| 27 |
-
2: 'segment',
|
| 28 |
-
3: 'triangle',
|
| 29 |
-
4: 'square',
|
| 30 |
-
5: 'circle'
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
def create_text_visualization(self, sequence, numbers):
|
| 34 |
-
"""Crée une visualisation texte de la séquence"""
|
| 35 |
-
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(16, 6))
|
| 36 |
-
fig.patch.set_facecolor('white')
|
| 37 |
-
|
| 38 |
-
# Visualisation des lettres
|
| 39 |
-
ax1.axis('off')
|
| 40 |
-
ax1.set_xlim(0, len(sequence))
|
| 41 |
-
ax1.set_ylim(0, 1)
|
| 42 |
-
ax1.set_title('Séquence en Lettres', fontsize=16, fontweight='bold', pad=20)
|
| 43 |
-
|
| 44 |
-
for i, letter in enumerate(sequence):
|
| 45 |
-
color = self.default_colors.get(letter, '#000000')
|
| 46 |
-
ax1.text(i + 0.5, 0.5, letter, ha='center', va='center',
|
| 47 |
-
fontsize=12, fontweight='bold', color=color,
|
| 48 |
-
bbox=dict(boxstyle='round,pad=0.5', facecolor='white',
|
| 49 |
-
edgecolor=color, linewidth=2))
|
| 50 |
-
|
| 51 |
-
# Visualisation des nombres
|
| 52 |
-
ax2.axis('off')
|
| 53 |
-
ax2.set_xlim(0, len(numbers))
|
| 54 |
-
ax2.set_ylim(0, 1)
|
| 55 |
-
ax2.set_title('Séquence en Nombres', fontsize=16, fontweight='bold', pad=20)
|
| 56 |
-
|
| 57 |
-
for i, (num, letter) in enumerate(zip(numbers, sequence)):
|
| 58 |
-
color = self.default_colors.get(letter, '#000000')
|
| 59 |
-
ax2.text(i + 0.5, 0.5, str(num), ha='center', va='center',
|
| 60 |
-
fontsize=12, fontweight='bold', color=color,
|
| 61 |
-
bbox=dict(boxstyle='round,pad=0.5', facecolor='white',
|
| 62 |
-
edgecolor=color, linewidth=2))
|
| 63 |
-
|
| 64 |
-
plt.tight_layout()
|
| 65 |
-
return fig
|
| 66 |
-
|
| 67 |
-
def create_colored_circles(self, sequence, custom_colors=None):
|
| 68 |
-
"""Crée une visualisation avec des cercles colorés"""
|
| 69 |
-
colors = custom_colors if custom_colors else self.default_colors
|
| 70 |
-
|
| 71 |
-
# Calculer la grille
|
| 72 |
-
n = len(sequence)
|
| 73 |
-
cols = min(20, n)
|
| 74 |
-
rows = (n + cols - 1) // cols
|
| 75 |
-
|
| 76 |
-
fig, ax = plt.subplots(figsize=(16, max(4, rows * 0.8)))
|
| 77 |
-
fig.patch.set_facecolor('white')
|
| 78 |
-
ax.set_xlim(-1, cols)
|
| 79 |
-
ax.set_ylim(-1, rows)
|
| 80 |
-
ax.set_aspect('equal')
|
| 81 |
-
ax.axis('off')
|
| 82 |
-
ax.set_title('Cercles Colorés', fontsize=16, fontweight='bold', pad=20)
|
| 83 |
-
|
| 84 |
-
for i, letter in enumerate(sequence):
|
| 85 |
-
row = i // cols
|
| 86 |
-
col = i % cols
|
| 87 |
-
x = col
|
| 88 |
-
y = rows - row - 1
|
| 89 |
-
|
| 90 |
-
color = colors.get(letter, '#CCCCCC')
|
| 91 |
-
circle = plt.Circle((x, y), 0.4, color=color, ec='black', linewidth=1.5)
|
| 92 |
-
ax.add_patch(circle)
|
| 93 |
-
|
| 94 |
-
# Ajouter la lettre au centre
|
| 95 |
-
ax.text(x, y, letter, ha='center', va='center',
|
| 96 |
-
fontsize=10, fontweight='bold', color='white')
|
| 97 |
-
|
| 98 |
-
plt.tight_layout()
|
| 99 |
-
return fig
|
| 100 |
-
|
| 101 |
-
def create_geometric_shapes(self, sequence, numbers, custom_colors=None):
|
| 102 |
-
"""Crée une visualisation avec des formes géométriques"""
|
| 103 |
-
colors = custom_colors if custom_colors else self.default_colors
|
| 104 |
-
|
| 105 |
-
# Calculer la grille
|
| 106 |
-
n = len(sequence)
|
| 107 |
-
cols = min(20, n)
|
| 108 |
-
rows = (n + cols - 1) // cols
|
| 109 |
-
|
| 110 |
-
fig, ax = plt.subplots(figsize=(16, max(4, rows * 0.8)))
|
| 111 |
-
fig.patch.set_facecolor('white')
|
| 112 |
-
ax.set_xlim(-1, cols)
|
| 113 |
-
ax.set_ylim(-1, rows)
|
| 114 |
-
ax.set_aspect('equal')
|
| 115 |
-
ax.axis('off')
|
| 116 |
-
ax.set_title('Formes Géométriques (1=point, 2=segment, 3=triangle, 4=carré, 5=cercle)',
|
| 117 |
-
fontsize=14, fontweight='bold', pad=20)
|
| 118 |
-
|
| 119 |
-
for i, (letter, num) in enumerate(zip(sequence, numbers)):
|
| 120 |
-
row = i // cols
|
| 121 |
-
col = i % cols
|
| 122 |
-
x = col
|
| 123 |
-
y = rows - row - 1
|
| 124 |
-
|
| 125 |
-
color = colors.get(letter, '#CCCCCC')
|
| 126 |
-
|
| 127 |
-
if num == 1: # Point
|
| 128 |
-
ax.plot(x, y, 'o', color=color, markersize=8,
|
| 129 |
-
markeredgecolor='black', markeredgewidth=1.5)
|
| 130 |
-
|
| 131 |
-
elif num == 2: # Segment
|
| 132 |
-
ax.plot([x - 0.3, x + 0.3], [y, y], color=color,
|
| 133 |
-
linewidth=4, solid_capstyle='round')
|
| 134 |
-
|
| 135 |
-
elif num == 3: # Triangle
|
| 136 |
-
triangle = patches.RegularPolygon((x, y), 3, radius=0.4,
|
| 137 |
-
color=color, ec='black', linewidth=1.5)
|
| 138 |
-
ax.add_patch(triangle)
|
| 139 |
-
|
| 140 |
-
elif num == 4: # Carré
|
| 141 |
-
square = patches.Rectangle((x - 0.35, y - 0.35), 0.7, 0.7,
|
| 142 |
-
color=color, ec='black', linewidth=1.5)
|
| 143 |
-
ax.add_patch(square)
|
| 144 |
-
|
| 145 |
-
elif num == 5: # Cercle
|
| 146 |
-
circle = plt.Circle((x, y), 0.4, color=color, ec='black', linewidth=1.5)
|
| 147 |
-
ax.add_patch(circle)
|
| 148 |
-
|
| 149 |
-
plt.tight_layout()
|
| 150 |
-
return fig
|
| 151 |
-
|
| 152 |
-
def create_pixel_grid(self, sequence, numbers, custom_colors=None, cell_size=30):
|
| 153 |
-
"""Crée une grille de pixels colorés"""
|
| 154 |
-
colors = custom_colors if custom_colors else self.default_colors
|
| 155 |
-
|
| 156 |
-
# Calculer la grille
|
| 157 |
-
n = len(sequence)
|
| 158 |
-
cols = min(20, n)
|
| 159 |
-
rows = (n + cols - 1) // cols
|
| 160 |
-
|
| 161 |
-
# Créer l'image
|
| 162 |
-
width = cols * cell_size
|
| 163 |
-
height = rows * cell_size
|
| 164 |
-
img = Image.new('RGB', (width, height), 'white')
|
| 165 |
-
draw = ImageDraw.Draw(img)
|
| 166 |
-
|
| 167 |
-
for i, letter in enumerate(sequence):
|
| 168 |
-
row = i // cols
|
| 169 |
-
col = i % cols
|
| 170 |
-
x = col * cell_size
|
| 171 |
-
y = row * cell_size
|
| 172 |
-
|
| 173 |
-
# Convertir la couleur hex en RGB
|
| 174 |
-
color_hex = colors.get(letter, '#CCCCCC')
|
| 175 |
-
color_rgb = tuple(int(color_hex.lstrip('#')[j:j+2], 16) for j in (0, 2, 4))
|
| 176 |
-
|
| 177 |
-
# Dessiner le rectangle
|
| 178 |
-
draw.rectangle([x, y, x + cell_size - 1, y + cell_size - 1],
|
| 179 |
-
fill=color_rgb, outline='black', width=1)
|
| 180 |
-
|
| 181 |
-
return img
|
| 182 |
-
|
| 183 |
-
def create_custom_image_pattern(self, sequence, numbers, custom_images=None):
|
| 184 |
-
"""Crée un motif avec des images personnalisées pour chaque nombre"""
|
| 185 |
-
if not custom_images:
|
| 186 |
-
# Utiliser des formes par défaut si pas d'images
|
| 187 |
-
return self.create_geometric_shapes(sequence, numbers)
|
| 188 |
-
|
| 189 |
-
# À implémenter: intégration d'images personnalisées
|
| 190 |
-
n = len(sequence)
|
| 191 |
-
cols = min(20, n)
|
| 192 |
-
rows = (n + cols - 1) // cols
|
| 193 |
-
cell_size = 50
|
| 194 |
-
|
| 195 |
-
width = cols * cell_size
|
| 196 |
-
height = rows * cell_size
|
| 197 |
-
img = Image.new('RGB', (width, height), 'white')
|
| 198 |
-
|
| 199 |
-
for i, num in enumerate(numbers):
|
| 200 |
-
row = i // cols
|
| 201 |
-
col = i % cols
|
| 202 |
-
x = col * cell_size
|
| 203 |
-
y = row * cell_size
|
| 204 |
-
|
| 205 |
-
if num in custom_images and custom_images[num]:
|
| 206 |
-
# Redimensionner et coller l'image personnalisée
|
| 207 |
-
custom_img = Image.open(custom_images[num])
|
| 208 |
-
custom_img = custom_img.resize((cell_size - 4, cell_size - 4))
|
| 209 |
-
img.paste(custom_img, (x + 2, y + 2))
|
| 210 |
-
|
| 211 |
-
return img
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
def fig_to_pil(fig):
|
| 215 |
-
"""Convertit une figure matplotlib en image PIL"""
|
| 216 |
-
buf = io.BytesIO()
|
| 217 |
-
fig.savefig(buf, format='png', dpi=100, bb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