sakshee05 commited on
Commit
5d6afe2
·
verified ·
1 Parent(s): eeb203f

add assertion testing

Browse files
Files changed (1) hide show
  1. code/exploratory_data_analysis.py +52 -16
code/exploratory_data_analysis.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import pandas as pd
2
  import matplotlib.pyplot as plt
3
  import seaborn as sns
@@ -5,6 +6,10 @@ import seaborn as sns
5
  # Set seaborn style
6
  sns.set(style="whitegrid")
7
 
 
 
 
 
8
  # Function to plot age distribution
9
  def plot_age_distribution(df):
10
  plt.figure(figsize=(8, 5))
@@ -12,7 +17,11 @@ def plot_age_distribution(df):
12
  plt.title('Age Distribution')
13
  plt.xlabel('Age')
14
  plt.ylabel('Count')
15
- plt.show()
 
 
 
 
16
 
17
  # Function to plot gender distribution (Donut Chart)
18
  def plot_gender_distribution(df):
@@ -22,32 +31,48 @@ def plot_gender_distribution(df):
22
  colors=sns.color_palette("pastel"))
23
  plt.title('Gender Distribution')
24
  plt.gca().set_aspect('equal')
25
- plt.show()
 
 
 
 
26
 
27
  # Function to plot nationality distribution
28
  def plot_nationality_distribution(df):
29
  plt.figure(figsize=(8, 5))
30
- sns.countplot(y=df['Nationality'], palette='coolwarm')
31
  plt.title('Nationality Distribution')
32
  plt.gca().set_aspect('equal')
33
- plt.show()
 
 
 
 
34
 
35
  # Function to plot native language distribution
36
  def plot_native_language_distribution(df):
37
  plt.figure(figsize=(8, 5))
38
- sns.countplot(y=df['Native Language'], palette='coolwarm')
39
  plt.title('Native Language Distribution')
40
  plt.gca().set_aspect('equal')
41
- plt.show()
 
 
 
 
42
 
43
  # Function to plot familiarity with English distribution
44
  def plot_familiarity_with_english(df):
45
  plt.figure(figsize=(8, 5))
46
- sns.countplot(y=df['Familiarity with English'], palette='coolwarm')
47
  plt.title('Familiarity with English')
48
  plt.xlabel('Count')
49
  plt.ylabel('Familiarity Level')
50
- plt.show()
 
 
 
 
51
 
52
  # Function to plot recording duration distribution
53
  def plot_duration_distribution(df):
@@ -56,20 +81,31 @@ def plot_duration_distribution(df):
56
  plt.title('Recording Duration Distribution')
57
  plt.xlabel('Duration (seconds)')
58
  plt.ylabel('Count')
59
- plt.show()
 
 
 
 
60
 
61
 
62
  def main():
63
  # Load the dataset
64
  df = pd.read_csv("metadata.csv")
65
 
66
- # Plot the distributions
67
- plot_age_distribution(df)
68
- plot_gender_distribution(df)
69
- plot_nationality_distribution(df)
70
- plot_native_language_distribution(df)
71
- plot_familiarity_with_english(df)
72
- plot_duration_distribution(df)
 
 
 
 
 
 
 
73
 
74
 
75
  if __name__ == "__main__":
 
1
+ import os
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
 
6
  # Set seaborn style
7
  sns.set(style="whitegrid")
8
 
9
+ # Ensure the 'assets' directory exists
10
+ if not os.path.exists('assets'):
11
+ os.makedirs('assets')
12
+
13
  # Function to plot age distribution
14
  def plot_age_distribution(df):
15
  plt.figure(figsize=(8, 5))
 
17
  plt.title('Age Distribution')
18
  plt.xlabel('Age')
19
  plt.ylabel('Count')
20
+ plot_filename = 'assets/age_distribution.png'
21
+ plt.savefig(plot_filename)
22
+ plt.show()
23
+ plt.close()
24
+ return plot_filename
25
 
26
  # Function to plot gender distribution (Donut Chart)
27
  def plot_gender_distribution(df):
 
31
  colors=sns.color_palette("pastel"))
32
  plt.title('Gender Distribution')
33
  plt.gca().set_aspect('equal')
34
+ plot_filename = 'assets/gender_distribution.png'
35
+ plt.savefig(plot_filename)
36
+ plt.show()
37
+ plt.close()
38
+ return plot_filename
39
 
40
  # Function to plot nationality distribution
41
  def plot_nationality_distribution(df):
42
  plt.figure(figsize=(8, 5))
43
+ sns.countplot(y=df['Nationality'], hue=df['Nationality'], palette='coolwarm', legend=False)
44
  plt.title('Nationality Distribution')
45
  plt.gca().set_aspect('equal')
46
+ plot_filename = 'assets/nationality_distribution.png'
47
+ plt.savefig(plot_filename)
48
+ plt.show()
49
+ plt.close()
50
+ return plot_filename
51
 
52
  # Function to plot native language distribution
53
  def plot_native_language_distribution(df):
54
  plt.figure(figsize=(8, 5))
55
+ sns.countplot(y=df['Native Language'], hue=df['Native Language'], palette='coolwarm', legend=False)
56
  plt.title('Native Language Distribution')
57
  plt.gca().set_aspect('equal')
58
+ plot_filename = 'assets/native_language_distribution.png'
59
+ plt.savefig(plot_filename)
60
+ plt.show()
61
+ plt.close()
62
+ return plot_filename
63
 
64
  # Function to plot familiarity with English distribution
65
  def plot_familiarity_with_english(df):
66
  plt.figure(figsize=(8, 5))
67
+ sns.countplot(y=df['Familiarity with English'], hue=df['Familiarity with English'], palette='coolwarm', legend=False)
68
  plt.title('Familiarity with English')
69
  plt.xlabel('Count')
70
  plt.ylabel('Familiarity Level')
71
+ plot_filename = 'assets/familiarity_with_eng.png'
72
+ plt.savefig(plot_filename)
73
+ plt.show()
74
+ plt.close()
75
+ return plot_filename
76
 
77
  # Function to plot recording duration distribution
78
  def plot_duration_distribution(df):
 
81
  plt.title('Recording Duration Distribution')
82
  plt.xlabel('Duration (seconds)')
83
  plt.ylabel('Count')
84
+ plot_filename = 'assets/recording_duration_distribution.png'
85
+ plt.savefig(plot_filename)
86
+ plt.show()
87
+ plt.close()
88
+ return plot_filename
89
 
90
 
91
  def main():
92
  # Load the dataset
93
  df = pd.read_csv("metadata.csv")
94
 
95
+ # Plot the distributions and save files
96
+ plot_files = [
97
+ plot_age_distribution(df),
98
+ plot_gender_distribution(df),
99
+ plot_nationality_distribution(df),
100
+ plot_native_language_distribution(df),
101
+ plot_familiarity_with_english(df),
102
+ plot_duration_distribution(df)
103
+ ]
104
+
105
+ # Testing
106
+ for plot_file in plot_files:
107
+ assert os.path.exists(plot_file), f"Plot {plot_file} was not saved."
108
+ print(f"Assertion passed for {plot_file}")
109
 
110
 
111
  if __name__ == "__main__":