-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_confusion_bias.py
215 lines (182 loc) · 7.67 KB
/
main_confusion_bias.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Get statistics needed to make confusion bias tables
"""
import json
import logging
import os
from dataclasses import dataclass, field
from tabulate import tabulate
from transformers import HfArgumentParser
from svc_demographic_bias_assessment import (
create_dataframe_for_statistical_description,
)
logger = logging.getLogger(__name__)
@dataclass
class ScriptArguments:
"""
Arguments needed to run this script.
"""
set_: str = field(
metadata={"help": "Either train, dev or test."},
)
metadata_filepath: str = field(
metadata={
"help": "Filepath pointing towards the metadata of the corresponding set."
},
)
save_tables: bool = field(
metadata={"help": "Whether to save the generated tables."},
)
save_data_directory: str = field(
metadata={"help": "Directory where all created data will be stored."},
)
def __post_init__(self):
assert self.set_ in ["train", "dev", "test"], ValueError(
f"set_ should be either train, dev or test; got {self.set_}"
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
dataclasses_list = [
ScriptArguments,
]
parser = HfArgumentParser(dataclasses_list)
args = parser.parse_args_into_dataclasses()
datapath_args = args[0]
os.makedirs(datapath_args.save_data_directory, exist_ok=True)
logger.info("Running main_confusion_bias.py script with following arguments:")
logger.info(vars(datapath_args))
logger.info(f"Loading metadata of set {datapath_args.set_}")
with open(datapath_args.metadata_filepath, "r", encoding="utf-8") as file:
metadata_json = json.load(file)
logger.info("Creating dataset with shape: ")
df = create_dataframe_for_statistical_description(metadata_json)
df = df.reset_index()
logger.info(df.shape)
############################################ DIALECTAL REGION ############################################
logger.info(
"Compute for each dialectal region, the number of speakers, samples and age and gender repartition to detect "
"possible empty buckets"
)
logger.info("Number of audio samples & speakers per dialectal region:")
df1 = df.groupby("dialectal_region").agg(
{"audio_id": "count", "user_id": "nunique"}
)
logger.info(tabulate(df1, headers="keys", tablefmt="psql"))
logger.info("Distribution of age group per dialectal region:")
df2 = df.groupby(["dialectal_region", "age_group"])["audio_id"].count().to_frame()
df2["percentage"] = (
df2.groupby(["dialectal_region"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df2, headers="keys", tablefmt="psql"))
logger.info("Distribution of gender per dialectal region:")
df3 = df.groupby(["dialectal_region", "gender"])["audio_id"].count().to_frame()
df3["percentage"] = (
df3.groupby(["dialectal_region"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df3, headers="keys", tablefmt="psql"))
############################################ AGE ############################################
logger.info(
"Compute for each age group, the number of speakers, samples and dialectal region and gender repartition to detect "
"possible empty buckets"
)
logger.info("Number of audio samples & speakers per age group:")
df1 = df.groupby("age_group").agg({"audio_id": "count", "user_id": "nunique"})
logger.info(tabulate(df1, headers="keys", tablefmt="psql"))
logger.info("Distribution of dialectal region per age group:")
df2 = df.groupby(["age_group", "dialectal_region"])["audio_id"].count().to_frame()
df2["percentage"] = (
df2.groupby(["age_group"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df2, headers="keys", tablefmt="psql"))
logger.info("Distribution of gender per age group:")
df3 = df.groupby(["age_group", "gender"])["audio_id"].count().to_frame()
df3["percentage"] = (
df3.groupby(["age_group"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df3, headers="keys", tablefmt="psql"))
############################################ GENDER ############################################
logger.info(
"Compute for each gender, the number of speakers, samples and dialectal region and age group repartition to detect "
"possible empty buckets"
)
logger.info("Number of audio samples & speakers per gender:")
df1 = df.groupby("gender").agg({"audio_id": "count", "user_id": "nunique"})
logger.info(tabulate(df1, headers="keys", tablefmt="psql"))
logger.info("Distribution of dialectal region per gender:")
df2 = df.groupby(["gender", "dialectal_region"])["audio_id"].count().to_frame()
df2["percentage"] = (
df2.groupby(["gender"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df2, headers="keys", tablefmt="psql"))
logger.info("Distribution of gender per age group:")
df3 = df.groupby(["gender", "age_group"])["audio_id"].count().to_frame()
df3["percentage"] = (
df3.groupby(["gender"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df3, headers="keys", tablefmt="psql"))
############################################ ETHNICITY ############################################
if datapath_args.set_ == "test":
logger.info("Focus on the particular case of the partial 'ethnicity' label")
where = ~df["ethnicity"].isna()
df_eth = df[where]
logger.info(df_eth.shape)
logger.info(f"Number of unique speakers: {df_eth['user_id'].nunique()}")
logger.info(
"Compute for each dialectal region, the number of speakers, samples and age and gender repartition to "
"detect possible empty buckets"
)
logger.info("Number of audio samples & speakers per ethnicity:")
df1 = df_eth.groupby("ethnicity").agg(
{"audio_id": "count", "user_id": "nunique"}
)
logger.info(tabulate(df1, headers="keys", tablefmt="psql"))
logger.info("Distribution of age group per ethnicity:")
df2 = df_eth.groupby(["ethnicity", "age_group"])["audio_id"].count().to_frame()
df2["percentage"] = (
df2.groupby(["ethnicity"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df2, headers="keys", tablefmt="psql"))
logger.info("Distribution of gender per ethnicity:")
df3 = df_eth.groupby(["ethnicity", "gender"])["audio_id"].count().to_frame()
df3["percentage"] = (
df3.groupby(["ethnicity"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df3, headers="keys", tablefmt="psql"))
logger.info("Distribution of dialectal region per ethnicity:")
df4 = (
df_eth.groupby(["ethnicity", "dialectal_region"])["audio_id"]
.count()
.to_frame()
)
df4["percentage"] = (
df4.groupby(["ethnicity"])["audio_id"]
.transform(lambda x: (x / x.sum()) * 100)
.round()
.to_frame()
)
logger.info(tabulate(df4, headers="keys", tablefmt="psql"))
logger.info("Done!")