TawsifDipto commited on
Commit
196ceca
·
verified ·
1 Parent(s): 5ff2f52

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -24
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
- # Histogram for numeric columns
173
  for col in numeric_cols:
174
- fig, ax = plt.subplots()
175
- df[col].hist(ax=ax, bins=30)
176
- ax.set_title(f"Histogram of {col}")
177
- img_path = savefig(fig)
178
- visuals.append(cl.Image(name=f"Histogram of {col}",path=img_path))
179
- saved_images.append(img_path)
 
 
 
 
 
 
 
180
 
181
  # Correlation heatmap
182
  if len(numeric_cols) > 1:
183
- corr = df[numeric_cols].corr()
184
- fig, ax = plt.subplots(figsize=(10, 8))
185
- sns.heatmap(corr, annot=True, fmt=".2f", cmap='coolwarm', ax=ax)
186
- ax.set_title("Correlation Heatmap")
187
- img_path = savefig(fig)
188
- visuals.append(cl.Image(name="Correlation Heatmap",path=img_path))
189
- saved_images.append(img_path)
 
 
 
 
 
190
 
191
  if categorical_cols:
192
  # Bar plots for categorical columns
193
  for col in categorical_cols:
194
- fig, ax = plt.subplots()
195
- df[col].value_counts().plot(kind='bar', ax=ax)
196
- ax.set_title(f"Bar Plot of {col}")
197
- img_path = savefig(fig)
198
- visuals.append(cl.Image(name=f"Bar Plot of {col}",path=img_path))
199
- saved_images.append(img_path)
 
 
 
 
 
 
 
 
200
  except Exception as e:
201
- print(f"Error generating visuals: {e}")
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: