Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -163,46 +163,67 @@ def generate_visuals(df):
|
|
| 163 |
"""Generate visualizations for the DataFrame."""
|
| 164 |
visuals = []
|
| 165 |
saved_images = []
|
| 166 |
-
|
| 167 |
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
| 168 |
categorical_cols = [col for col in df.select_dtypes('object') if 1 < df[col].nunique() < 30]
|
| 169 |
-
|
| 170 |
try:
|
| 171 |
if numeric_cols:
|
| 172 |
-
#
|
| 173 |
for col in numeric_cols:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
# Correlation heatmap
|
| 182 |
if len(numeric_cols) > 1:
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
if categorical_cols:
|
| 192 |
# Bar plots for categorical columns
|
| 193 |
for col in categorical_cols:
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
except Exception as e:
|
| 201 |
-
print(f"
|
| 202 |
plt.close('all')
|
| 203 |
-
|
| 204 |
return visuals, saved_images
|
| 205 |
|
|
|
|
| 206 |
async def cleanup_images(saved_images):
|
| 207 |
"""Clean up temporary image files."""
|
| 208 |
for img_path in saved_images:
|
|
|
|
| 163 |
"""Generate visualizations for the DataFrame."""
|
| 164 |
visuals = []
|
| 165 |
saved_images = []
|
| 166 |
+
|
| 167 |
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
| 168 |
categorical_cols = [col for col in df.select_dtypes('object') if 1 < df[col].nunique() < 30]
|
| 169 |
+
|
| 170 |
try:
|
| 171 |
if numeric_cols:
|
| 172 |
+
# Histograms for numeric columns
|
| 173 |
for col in numeric_cols:
|
| 174 |
+
try:
|
| 175 |
+
fig, ax = plt.subplots()
|
| 176 |
+
df[col].dropna().hist(ax=ax, bins=30)
|
| 177 |
+
ax.set_title(f"Histogram of {col}")
|
| 178 |
+
ax.set_xlabel(col)
|
| 179 |
+
ax.set_ylabel("Frequency")
|
| 180 |
+
img_path = savefig(fig)
|
| 181 |
+
visuals.append(cl.Image(name=f"Histogram of {col}", path=img_path))
|
| 182 |
+
saved_images.append(img_path)
|
| 183 |
+
plt.close(fig)
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"Error generating histogram for {col}: {e}")
|
| 186 |
+
plt.close()
|
| 187 |
|
| 188 |
# Correlation heatmap
|
| 189 |
if len(numeric_cols) > 1:
|
| 190 |
+
try:
|
| 191 |
+
corr = df[numeric_cols].corr().round(2)
|
| 192 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
| 193 |
+
sns.heatmap(corr, annot=True, fmt=".2f", cmap='coolwarm', ax=ax)
|
| 194 |
+
ax.set_title("Correlation Heatmap")
|
| 195 |
+
img_path = savefig(fig)
|
| 196 |
+
visuals.append(cl.Image(name="Correlation Heatmap", path=img_path))
|
| 197 |
+
saved_images.append(img_path)
|
| 198 |
+
plt.close(fig)
|
| 199 |
+
except Exception as e:
|
| 200 |
+
print(f"Error generating correlation heatmap: {e}")
|
| 201 |
+
plt.close()
|
| 202 |
|
| 203 |
if categorical_cols:
|
| 204 |
# Bar plots for categorical columns
|
| 205 |
for col in categorical_cols:
|
| 206 |
+
try:
|
| 207 |
+
fig, ax = plt.subplots()
|
| 208 |
+
df[col].fillna("Missing").value_counts().head(20).plot(kind='bar', ax=ax)
|
| 209 |
+
ax.set_title(f"Bar Plot of {col} (Top 20 Categories)")
|
| 210 |
+
ax.set_xlabel(col)
|
| 211 |
+
ax.set_ylabel("Count")
|
| 212 |
+
img_path = savefig(fig)
|
| 213 |
+
visuals.append(cl.Image(name=f"Bar Plot of {col}", path=img_path))
|
| 214 |
+
saved_images.append(img_path)
|
| 215 |
+
plt.close(fig)
|
| 216 |
+
except Exception as e:
|
| 217 |
+
print(f"Error generating bar plot for {col}: {e}")
|
| 218 |
+
plt.close()
|
| 219 |
+
|
| 220 |
except Exception as e:
|
| 221 |
+
print(f"Unexpected error generating visuals: {e}")
|
| 222 |
plt.close('all')
|
| 223 |
+
|
| 224 |
return visuals, saved_images
|
| 225 |
|
| 226 |
+
|
| 227 |
async def cleanup_images(saved_images):
|
| 228 |
"""Clean up temporary image files."""
|
| 229 |
for img_path in saved_images:
|