Tom commited on
Commit
da69da5
·
1 Parent(s): cab20ec
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -122,6 +122,7 @@ def process_extra_curricular(df):
122
 
123
  # Analyze the impact of extra-curricular activities
124
  def analyze_extra_curricular_impact(df):
 
125
  for activity in ['Cricket', 'Debating', 'Football', 'Art Club', 'Drama Club', 'Rugby']:
126
  if activity in df.columns:
127
  data = df.copy()
@@ -133,11 +134,15 @@ def analyze_extra_curricular_impact(df):
133
  plt.xlabel('Participation Status', fontsize=12)
134
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
135
  plt.tight_layout()
136
- plt.savefig(f'{activity}_impact.png')
 
137
  plt.close()
 
 
138
 
139
  # Analyze teacher performance
140
  def analyze_teacher_performance(df):
 
141
  teacher_year_levels = {
142
  'year_11_english_teacher': 'Year 11',
143
  'year_11_maths_teacher': 'Year 11',
@@ -155,8 +160,11 @@ def analyze_teacher_performance(df):
155
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
156
  plt.xticks(rotation=45)
157
  plt.tight_layout()
158
- plt.savefig(f'teacher_performance_{col}.png')
 
159
  plt.close()
 
 
160
 
161
  # Analyze the impact of primary languages
162
  def analyze_language_impact(df):
@@ -170,8 +178,10 @@ def analyze_language_impact(df):
170
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
171
  plt.xticks(rotation=45)
172
  plt.tight_layout()
173
- plt.savefig('language_impact.png')
 
174
  plt.close()
 
175
 
176
  # Perform clustering using KMeans and PCA
177
  def perform_clustering(df):
@@ -192,8 +202,10 @@ def perform_clustering(df):
192
  plt.ylabel('Principal Component 2', fontsize=12)
193
  plt.legend(title='Cluster')
194
  plt.tight_layout()
195
- plt.savefig('clustering_plot.png')
 
196
  plt.close()
 
197
 
198
  # Plot correlation with credit achievement rate
199
  def plot_correlation_with_credit_achievement(df):
@@ -207,14 +219,17 @@ def plot_correlation_with_credit_achievement(df):
207
  plt.title('Correlation with Credit Achievement Rate', fontsize=16)
208
  plt.xticks(rotation=45, ha='right', fontsize=10)
209
  plt.tight_layout()
210
- plt.savefig('correlation_heatmap.png')
 
211
  plt.close()
 
212
 
213
  # Plot top features vs credit achievement rate
214
  def plot_top_features_vs_credit(df):
215
  numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
216
  corr_matrix = df[numeric_cols].corr()
217
  top_corr_features = corr_matrix['credit_achievement_rate'].abs().sort_values(ascending=False).index[1:6]
 
218
  for feature in top_corr_features:
219
  if pd.api.types.is_numeric_dtype(df[feature]):
220
  df_sorted = df[[feature, 'credit_achievement_rate']].sort_values(by=feature)
@@ -224,33 +239,35 @@ def plot_top_features_vs_credit(df):
224
  plt.xlabel(feature.replace('_', ' ').title(), fontsize=12)
225
  plt.ylabel('Credit Achievement Rate', fontsize=12)
226
  plt.tight_layout()
227
- plt.savefig(f'{feature}_vs_credit.png')
 
228
  plt.close()
 
 
229
 
230
  # Comprehensive analysis function
231
  def perform_comprehensive_analysis(df):
232
  df = clean_data(df)
233
  report = identify_at_risk_students(df)
234
- df = process_extra_curricular(df)
235
- analyze_extra_curricular_impact(df)
236
- analyze_teacher_performance(df)
237
- analyze_language_impact(df)
238
- perform_clustering(df)
239
- plot_correlation_with_credit_achievement(df)
240
- plot_top_features_vs_credit(df)
241
- return report
242
 
243
  # Gradio interface
244
  def process_csv(file):
245
  df = pd.read_csv(file.name)
246
- report = perform_comprehensive_analysis(df)
247
- return report, 'teacher_performance_year_11_english_teacher.png', 'correlation_heatmap.png', 'clustering_plot.png'
248
 
249
  # Gradio Interface setup
250
  interface = gr.Interface(
251
  fn=process_csv,
252
  inputs="file",
253
- outputs=["text", "image", "image", "image"],
254
  title="Student Data Analysis",
255
  description="Upload a CSV file to perform a comprehensive analysis.",
256
  )
 
122
 
123
  # Analyze the impact of extra-curricular activities
124
  def analyze_extra_curricular_impact(df):
125
+ images = []
126
  for activity in ['Cricket', 'Debating', 'Football', 'Art Club', 'Drama Club', 'Rugby']:
127
  if activity in df.columns:
128
  data = df.copy()
 
134
  plt.xlabel('Participation Status', fontsize=12)
135
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
136
  plt.tight_layout()
137
+ img_path = f'{activity}_impact.png'
138
+ plt.savefig(img_path)
139
  plt.close()
140
+ images.append(img_path)
141
+ return images
142
 
143
  # Analyze teacher performance
144
  def analyze_teacher_performance(df):
145
+ images = []
146
  teacher_year_levels = {
147
  'year_11_english_teacher': 'Year 11',
148
  'year_11_maths_teacher': 'Year 11',
 
160
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
161
  plt.xticks(rotation=45)
162
  plt.tight_layout()
163
+ img_path = f'teacher_performance_{col}.png'
164
+ plt.savefig(img_path)
165
  plt.close()
166
+ images.append(img_path)
167
+ return images
168
 
169
  # Analyze the impact of primary languages
170
  def analyze_language_impact(df):
 
178
  plt.ylabel('Average Credit Achievement Rate', fontsize=12)
179
  plt.xticks(rotation=45)
180
  plt.tight_layout()
181
+ img_path = 'language_impact.png'
182
+ plt.savefig(img_path)
183
  plt.close()
184
+ return img_path
185
 
186
  # Perform clustering using KMeans and PCA
187
  def perform_clustering(df):
 
202
  plt.ylabel('Principal Component 2', fontsize=12)
203
  plt.legend(title='Cluster')
204
  plt.tight_layout()
205
+ img_path = 'clustering_plot.png'
206
+ plt.savefig(img_path)
207
  plt.close()
208
+ return img_path
209
 
210
  # Plot correlation with credit achievement rate
211
  def plot_correlation_with_credit_achievement(df):
 
219
  plt.title('Correlation with Credit Achievement Rate', fontsize=16)
220
  plt.xticks(rotation=45, ha='right', fontsize=10)
221
  plt.tight_layout()
222
+ img_path = 'correlation_heatmap.png'
223
+ plt.savefig(img_path)
224
  plt.close()
225
+ return img_path
226
 
227
  # Plot top features vs credit achievement rate
228
  def plot_top_features_vs_credit(df):
229
  numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
230
  corr_matrix = df[numeric_cols].corr()
231
  top_corr_features = corr_matrix['credit_achievement_rate'].abs().sort_values(ascending=False).index[1:6]
232
+ images = []
233
  for feature in top_corr_features:
234
  if pd.api.types.is_numeric_dtype(df[feature]):
235
  df_sorted = df[[feature, 'credit_achievement_rate']].sort_values(by=feature)
 
239
  plt.xlabel(feature.replace('_', ' ').title(), fontsize=12)
240
  plt.ylabel('Credit Achievement Rate', fontsize=12)
241
  plt.tight_layout()
242
+ img_path = f'{feature}_vs_credit.png'
243
+ plt.savefig(img_path)
244
  plt.close()
245
+ images.append(img_path)
246
+ return images
247
 
248
  # Comprehensive analysis function
249
  def perform_comprehensive_analysis(df):
250
  df = clean_data(df)
251
  report = identify_at_risk_students(df)
252
+ extra_curricular_imgs = analyze_extra_curricular_impact(df)
253
+ teacher_performance_imgs = analyze_teacher_performance(df)
254
+ language_impact_img = analyze_language_impact(df)
255
+ clustering_img = perform_clustering(df)
256
+ correlation_img = plot_correlation_with_credit_achievement(df)
257
+ top_features_imgs = plot_top_features_vs_credit(df)
258
+ return report, extra_curricular_imgs + teacher_performance_imgs + [language_impact_img, clustering_img, correlation_img] + top_features_imgs
 
259
 
260
  # Gradio interface
261
  def process_csv(file):
262
  df = pd.read_csv(file.name)
263
+ report, images = perform_comprehensive_analysis(df)
264
+ return [report] + images
265
 
266
  # Gradio Interface setup
267
  interface = gr.Interface(
268
  fn=process_csv,
269
  inputs="file",
270
+ outputs=["text"] + ["image"] * 20, # Assuming we have up to 20 image outputs
271
  title="Student Data Analysis",
272
  description="Upload a CSV file to perform a comprehensive analysis.",
273
  )