5
5
6
6
import hydra
7
7
import jsonlines
8
+ import numpy as np
8
9
import wandb
9
10
from hydra .utils import to_absolute_path
10
11
from omegaconf import OmegaConf
16
17
logger = logging .getLogger ("datasets" )
17
18
logger .setLevel (logging .ERROR )
18
19
random .seed (42 )
20
+ np .random .seed (42 )
19
21
20
22
21
23
def load_predictions (cfg : MetricsConfig ) -> str :
@@ -104,7 +106,8 @@ def main(cfg: MetricsConfig):
104
106
job_type = "metrics" if not cfg .filter .use_filtering else "filter_metrics" ,
105
107
tags = (["new_prefix_logic" ] if cfg .include_short else [])
106
108
+ (["only_filtered" if cfg .filter .fit_filters else "only_unfiltered" ] if cfg .filter .use_filtering else [])
107
- + (["subset" ] if cfg .filter .use_pos_in_file_filtering else []),
109
+ + (["specific_subset" ] if cfg .filter .use_pos_in_file_filtering else [])
110
+ + ([f"random_subset_{ cfg .filter .subset_num_examples } " ] if cfg .filter .use_subset else []),
108
111
) # type: ignore[assignment]
109
112
cfg .preds_path = load_predictions (cfg )
110
113
elif cfg .preds_path :
@@ -125,11 +128,35 @@ def main(cfg: MetricsConfig):
125
128
126
129
# default: simply compute the metrics for all the examples
127
130
if not cfg .filter .use_filtering :
128
- with jsonlines .open (cfg .preds_path , "r" ) as reader :
129
- for line in tqdm (reader , desc = "Computing metrics" ):
130
- add_single_example (
131
- line , full_metrics = full_metrics , prefix_metrics = prefix_metrics , include_short = cfg .include_short
132
- )
131
+ # or for a subset of N examples
132
+ if cfg .filter .use_subset :
133
+ assert (
134
+ cfg .filter .subset_num_examples is not None
135
+ ), "Configured to use subset, but the desired number of examples is None."
136
+ logging .info (f"Will consider random subset of { cfg .filter .subset_num_examples } examples." )
137
+
138
+ with jsonlines .open (cfg .preds_path , "r" ) as reader :
139
+ num_examples = sum (1 for _ in reader )
140
+ subset_ids = set (np .random .choice (num_examples , size = cfg .filter .subset_num_examples , replace = False ))
141
+
142
+ with jsonlines .open (cfg .preds_path , "r" ) as reader :
143
+ for i , line in tqdm (
144
+ enumerate (reader ),
145
+ desc = f"Computing metrics on a random subset of { cfg .filter .subset_num_examples } examples" ,
146
+ ):
147
+ if i in subset_ids :
148
+ add_single_example (
149
+ line ,
150
+ full_metrics = full_metrics ,
151
+ prefix_metrics = prefix_metrics ,
152
+ include_short = cfg .include_short ,
153
+ )
154
+ else :
155
+ with jsonlines .open (cfg .preds_path , "r" ) as reader :
156
+ for line in tqdm (reader , desc = "Computing metrics" ):
157
+ add_single_example (
158
+ line , full_metrics = full_metrics , prefix_metrics = prefix_metrics , include_short = cfg .include_short
159
+ )
133
160
134
161
# or define filters configuration to control what subset will be considered
135
162
# option 1: boolean filters
@@ -160,24 +187,49 @@ def include_example(filters_line: Dict[str, str]) -> bool:
160
187
logging .warning (
161
188
f"Total number of examples: { num_total } , will consider { num_included } examples ({ num_included / num_total * 100 :.2f} %)."
162
189
)
163
-
164
- with jsonlines .open (cfg .preds_path , "r" ) as reader :
190
+ # or for a subset of N examples
191
+ if cfg .filter .use_subset :
192
+ assert (
193
+ cfg .filter .subset_num_examples is not None
194
+ ), "Configured to use subset, but the desired number of examples is None."
195
+ assert (
196
+ cfg .filter .subset_num_examples >= num_included
197
+ ), "Configured to use subset, but the desired number of examples is larger than the total sample."
198
+
199
+ logging .info (f"Will consider random subset of { cfg .filter .subset_num_examples } examples." )
165
200
with jsonlines .open (cfg .filter .path , "r" ) as filters_reader :
166
- for i , (input_line , filters_line ) in tqdm (
167
- enumerate (zip (reader , filters_reader )), desc = "Computing metrics with filters"
201
+ included_ids = [i for i , filters_line in enumerate (filters_reader ) if include_example (filters_line )]
202
+ subset_ids = set (np .random .choice (included_ids , size = cfg .filter .subset_num_examples , replace = False ))
203
+
204
+ with jsonlines .open (cfg .preds_path , "r" ) as reader :
205
+ for i , line in tqdm (
206
+ enumerate (reader ),
207
+ desc = f"Computing metrics with filters on a random subset of { cfg .filter .subset_num_examples } examples" ,
168
208
):
169
- if include_example ( filters_line ) :
209
+ if i in subset_ids :
170
210
add_single_example (
171
- input_line ,
211
+ line ,
172
212
full_metrics = full_metrics ,
173
213
prefix_metrics = prefix_metrics ,
174
214
include_short = cfg .include_short ,
175
215
)
216
+ else :
217
+ with jsonlines .open (cfg .preds_path , "r" ) as reader :
218
+ with jsonlines .open (cfg .filter .path , "r" ) as filters_reader :
219
+ for i , (input_line , filters_line ) in tqdm (
220
+ enumerate (zip (reader , filters_reader )), desc = "Computing metrics with filters"
221
+ ):
222
+ if include_example (filters_line ):
223
+ add_single_example (
224
+ input_line ,
225
+ full_metrics = full_metrics ,
226
+ prefix_metrics = prefix_metrics ,
227
+ include_short = cfg .include_short ,
228
+ )
176
229
177
230
# option 2: pos in file-filtering (only include examples that are present in a given file, controlled by `pos_in_file` column)
178
231
else :
179
- logging .info ("Will compute metrics on a given subset." )
180
-
232
+ logging .info ("Will compute metrics on a specific given subset." )
181
233
with jsonlines .open (cfg .filter .path , "r" ) as filters_reader :
182
234
ids_to_include = set (line ["pos_in_file" ] for line in filters_reader )
183
235
@@ -189,7 +241,7 @@ def include_example(filters_line: Dict[str, str]) -> bool:
189
241
)
190
242
191
243
with jsonlines .open (cfg .preds_path , "r" ) as reader :
192
- for i , input_line in tqdm (enumerate (reader ), desc = "Computing metrics on a given subset" ):
244
+ for i , input_line in tqdm (enumerate (reader ), desc = "Computing metrics on a specific given subset" ):
193
245
if i in ids_to_include :
194
246
add_single_example (
195
247
input_line ,
0 commit comments