Evaluate
Qrels
Bases: object
Qrels
, or query relevance judgments, stores the ground truth for conducting evaluations.
The preferred way for creating a Qrels
instance is converting Python dictionary as follows:
qrels_dict = {
"q_1": {
"d_1": 1,
"d_2": 2,
},
"q_2": {
"d_3": 2,
"d_2": 1,
"d_5": 3,
},
}
qrels = Qrels(qrels_dict, name="MSMARCO")
qrels = Qrels() # Creates an empty Qrels with no name
Source code in ranx/data_structures/qrels.py
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
add(q_id, doc_ids, scores)
Add a query and its relevant documents with the associated relevance score judgment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_id |
str
|
Query ID |
required |
doc_ids |
List[str]
|
List of Document IDs |
required |
scores |
List[int]
|
List of relevance score judgments |
required |
Source code in ranx/data_structures/qrels.py
109 110 111 112 113 114 115 116 117 |
|
add_multi(q_ids, doc_ids, scores)
Add multiple queries at once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_ids |
List[str]
|
List of Query IDs |
required |
doc_ids |
List[List[str]]
|
List of list of Document IDs |
required |
scores |
List[List[int]]
|
List of list of relevance score judgments |
required |
Source code in ranx/data_structures/qrels.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
add_score(q_id, doc_id, score)
Add a (doc_id, score) pair to a query (or, change its value if it already exists).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_id |
str
|
Query ID |
required |
doc_id |
str
|
Document ID |
required |
score |
int
|
Relevance score judgment |
required |
Source code in ranx/data_structures/qrels.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
from_df(df, q_id_col='q_id', doc_id_col='doc_id', score_col='score')
staticmethod
Convert a Pandas DataFrame to ranx.Qrels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Qrels as Pandas DataFrame. |
required |
q_id_col |
str
|
Query IDs column. Defaults to "q_id". |
'q_id'
|
doc_id_col |
str
|
Document IDs column. Defaults to "doc_id". |
'doc_id'
|
score_col |
str
|
Relevance score judgments column. Defaults to "score". |
'score'
|
Returns:
Name | Type | Description |
---|---|---|
Qrels |
ranx.Qrels |
Source code in ranx/data_structures/qrels.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
from_dict(d)
staticmethod
Convert a Python dictionary in form of {q_id: {doc_id: score}} to ranx.Qrels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
Dict[str, Dict[str, int]]
|
Qrels as Python dictionary |
required |
Returns:
Name | Type | Description |
---|---|---|
Qrels |
ranx.Qrels |
Source code in ranx/data_structures/qrels.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
from_file(path, kind=None)
staticmethod
Parse a qrels file into ranx.Qrels. Supported formats are JSON, TREC qrels, and gzipped TREC qrels. Correct import behavior is inferred from the file extension: ".json" -> "json", ".trec" -> "trec", ".txt" -> "trec", ".gz" -> "gzipped trec". Use the "kind" argument to override this behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
File path. |
required |
kind |
str
|
Kind of file to load, must be either "json" or "trec". |
None
|
Returns:
Name | Type | Description |
---|---|---|
Qrels |
ranx.Qrels |
Source code in ranx/data_structures/qrels.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
from_ir_datasets(dataset_id)
staticmethod
Convert ir-datasets
qrels into ranx.Qrels. It automatically downloads data if missing.
Args:
dataset_id (str): ID of the detaset in ir-datasets
. ir-datasets
catalog is available here: https://ir-datasets.com/index.html.
Returns:
Qrels: ranx.Qrels
Source code in ranx/data_structures/qrels.py
341 342 343 344 345 346 347 348 349 350 351 |
|
from_parquet(path, q_id_col='q_id', doc_id_col='doc_id', score_col='score', pd_kwargs=None)
staticmethod
Convert a Parquet file to ranx.Qrels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
File path. |
required |
q_id_col |
str
|
Query IDs column. Defaults to "q_id". |
'q_id'
|
doc_id_col |
str
|
Document IDs column. Defaults to "doc_id". |
'doc_id'
|
score_col |
str
|
Relevance score judgments column. Defaults to "score". |
'score'
|
pd_kwargs |
Dict[str, Any]
|
Additional arguments to pass to |
None
|
Returns:
Name | Type | Description |
---|---|---|
Qrels |
ranx.Qrels |
Source code in ranx/data_structures/qrels.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
get_doc_ids_and_scores()
Returns doc ids and relevance judgments.
Source code in ranx/data_structures/qrels.py
147 148 149 |
|
get_query_ids()
Returns query ids.
Source code in ranx/data_structures/qrels.py
143 144 145 |
|
keys()
Returns query ids. Used internally.
Source code in ranx/data_structures/qrels.py
89 90 91 |
|
save(path='qrels.json', kind=None)
Write qrels
to path
as JSON file, TREC qrels format, or Parquet file. File type is automatically inferred form the filename extension: ".json" -> "json", ".trec" -> "trec", ".txt" -> "trec", ".parq" -> "parquet", ".parquet" -> "parquet". Use the "kind" argument to override this behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Saving path. Defaults to "qrels.json". |
'qrels.json'
|
kind |
str
|
Kind of file to save, must be either "json" or "trec". If None, it will be automatically inferred from the filename extension. |
None
|
Source code in ranx/data_structures/qrels.py
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 216 217 218 |
|
set_relevance_level(rel_lvl=1)
Sets relevance level.
Source code in ranx/data_structures/qrels.py
139 140 141 |
|
sort()
Sort. Used internally.
Source code in ranx/data_structures/qrels.py
152 153 154 155 156 |
|
to_dataframe()
Convert Qrels to Pandas DataFrame with the following columns: q_id
, doc_id
, and score
.
Returns:
Type | Description |
---|---|
DataFrame
|
pandas.DataFrame: Qrels as Pandas DataFrame. |
Source code in ranx/data_structures/qrels.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
to_dict()
Convert Qrels to Python dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, int]]
|
Dict[str, Dict[str, int]]: Qrels as Python dictionary |
Source code in ranx/data_structures/qrels.py
164 165 166 167 168 169 170 171 172 173 |
|
to_typed_list()
Convert Qrels to Numba Typed List. Used internally.
Source code in ranx/data_structures/qrels.py
158 159 160 161 162 |
|
Run
Bases: object
Run
stores the relevance scores estimated by the model under evaluation.<r>
The preferred way for creating a Run
instance is converting a Python dictionary as follows:
run_dict = {
"q_1": {
"d_1": 1.5,
"d_2": 2.6,
},
"q_2": {
"d_3": 2.8,
"d_2": 1.2,
"d_5": 3.1,
},
}
run = Run(run_dict, name="bm25")
run = Run() # Creates an empty Run with no name
Source code in ranx/data_structures/run.py
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
add(q_id, doc_ids, scores)
Add a query and its relevant documents with the associated relevance score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_id |
str
|
Query ID |
required |
doc_ids |
List[str]
|
List of Document IDs |
required |
scores |
List[int]
|
List of relevance scores |
required |
Source code in ranx/data_structures/run.py
97 98 99 100 101 102 103 104 105 |
|
add_multi(q_ids, doc_ids, scores)
Add multiple queries at once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_ids |
List[str]
|
List of Query IDs |
required |
doc_ids |
List[List[str]]
|
List of list of Document IDs |
required |
scores |
List[List[int]]
|
List of list of relevance scores |
required |
Source code in ranx/data_structures/run.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
add_score(q_id, doc_id, score)
Add a (doc_id, score) pair to a query (or, change its value if it already exists).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_id |
str
|
Query ID |
required |
doc_id |
str
|
Document ID |
required |
score |
int
|
Relevance score |
required |
Source code in ranx/data_structures/run.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
from_df(df, q_id_col='q_id', doc_id_col='doc_id', score_col='score', name=None)
staticmethod
Convert a Pandas DataFrame to ranx.Run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Run as Pandas DataFrame |
required |
q_id_col |
str
|
Query IDs column. Defaults to "q_id". |
'q_id'
|
doc_id_col |
str
|
Document IDs column. Defaults to "doc_id". |
'doc_id'
|
score_col |
str
|
Relevance scores column. Defaults to "score". |
'score'
|
name |
str
|
Run name. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Run |
ranx.Run |
Source code in ranx/data_structures/run.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
from_dict(d, name=None)
staticmethod
Convert a Python dictionary in form of {q_id: {doc_id: score}} to ranx.Run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
Dict[str, Dict[str, int]]
|
Run as Python dictionary |
required |
name |
str
|
Run name. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Run |
ranx.Run |
Source code in ranx/data_structures/run.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
from_file(path, kind=None, name=None)
staticmethod
Parse a run file into ranx.Run. Supported formats are JSON, TREC run, gzipped TREC run, and LZ4. Correct import behavior is inferred from the file extension: ".json" -> "json", ".trec" -> "trec", ".txt" -> "trec", ".gz" -> "gzipped trec", ".lz4" -> "lz4". Use the "kind" argument to override this behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
File path. |
required |
kind |
str
|
Kind of file to load, must be either "json" or "trec". |
None
|
name |
str
|
Run name. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Run |
ranx.Run |
Source code in ranx/data_structures/run.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
from_parquet(path, q_id_col='q_id', doc_id_col='doc_id', score_col='score', pd_kwargs=None, name=None)
staticmethod
Convert a Parquet file to ranx.Run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
File path. |
required |
q_id_col |
str
|
Query IDs column. Defaults to "q_id". |
'q_id'
|
doc_id_col |
str
|
Document IDs column. Defaults to "doc_id". |
'doc_id'
|
score_col |
str
|
Relevance scores column. Defaults to "score". |
'score'
|
pd_kwargs |
Dict[str, Any]
|
Additional arguments to pass to |
None
|
name |
str
|
Run name. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Run |
ranx.Run |
Source code in ranx/data_structures/run.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
from_ranxhub(id)
staticmethod
Download and load a ranx.Run from ranxhub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Run ID. |
required |
Returns:
Name | Type | Description |
---|---|---|
Run |
ranx.Run |
Source code in ranx/data_structures/run.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
get_doc_ids_and_scores()
Returns doc ids and relevance scores.
Source code in ranx/data_structures/run.py
131 132 133 |
|
get_query_ids()
Returns query ids.
Source code in ranx/data_structures/run.py
127 128 129 |
|
keys()
Returns query ids. Used internally.
Source code in ranx/data_structures/run.py
77 78 79 |
|
make_comparable(qrels)
Adds empty results for queries missing from the run and removes those not appearing in qrels.
Source code in ranx/data_structures/run.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
save(path='run.json', kind=None)
Write run
to path
as JSON file, TREC run, LZ4 file, or Parquet file. File type is automatically inferred form the filename extension: ".json" -> "json", ".trec" -> "trec", ".txt" -> "trec", and ".lz4" -> "lz4", ".parq" -> "parquet", ".parquet" -> "parquet". Use the "kind" argument to override this behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Saving path. Defaults to "run.json". |
'run.json'
|
kind |
str
|
Kind of file to save, must be either "json", "trec", or "ranxhub". If None, it will be automatically inferred from the filename extension. |
None
|
Source code in ranx/data_structures/run.py
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 216 217 218 219 220 221 222 |
|
sort()
Sort. Used internally.
Source code in ranx/data_structures/run.py
136 137 138 139 140 |
|
to_dataframe()
Convert Run to Pandas DataFrame with the following columns: q_id
, doc_id
, and score
.
Returns:
Type | Description |
---|---|
DataFrame
|
pandas.DataFrame: Run as Pandas DataFrame. |
Source code in ranx/data_structures/run.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
to_dict()
Convert Run to Python dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, int]]: Run as Python dictionary |
Source code in ranx/data_structures/run.py
164 165 166 167 168 169 170 171 172 173 |
|
to_typed_list()
Convert Run to Numba Typed List. Used internally.
Source code in ranx/data_structures/run.py
158 159 160 161 162 |
|
evaluate(qrels, run, metrics, return_mean=True, return_std=False, threads=0, save_results_in_run=True, make_comparable=False)
Compute the performance scores for the provided qrels
and run
for all the specified metrics.
Usage examples:
from ranx import evaluate
Compute score for a single metric
evaluate(qrels, run, "ndcg@5")
0.7861
Compute scores for multiple metrics at once
evaluate(qrels, run, ["map@5", "mrr"])
{"map@5": 0.6416, "mrr": 0.75}
Computed metric scores are saved in the Run object
run.mean_scores
{"ndcg@5": 0.7861, "map@5": 0.6416, "mrr": 0.75}
Access scores for each query
dict(run.scores)
{ ... "ndcg@5": {"q_1": 0.9430, "q_2": 0.6292}, ... "map@5": {"q_1": 0.8333, "q_2": 0.4500}, ... "mrr": {"q_1": 1.0000, "q_2": 0.5000}, ... } Args: qrels (Union[ Qrels, Dict[str, Dict[str, Number]], nb.typed.typedlist.List, np.ndarray, ]): Qrels. run (Union[ Run, Dict[str, Dict[str, Number]], nb.typed.typedlist.List, np.ndarray, ]): Run. metrics (Union[List[str], str]): Metrics or list of metric to compute. return_mean (bool, optional): Whether to return the metric scores averaged over the query set or the scores for individual queries. Defaults to True. threads (int, optional): Number of threads to use, zero means all the available threads. Defaults to 0. save_results_in_run (bool, optional): Save metric scores for each query in the input
run
. Defaults to True. make_comparable (bool, optional): Adds empty results for queries missing from the run and removes those not appearing in qrels. Defaults to False.
Returns:
Type | Description |
---|---|
Union[Dict[str, float], float]
|
Union[Dict[str, float], float]: Results. |
Source code in ranx/meta/evaluate.py
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 |
|
python_dict_to_typed_list(x, sort=True)
Converts a nested Python Dictionary to Numba Typed List to be used with ranx's metrics with no effort.
Note: Doc IDs will be hashed.
Source code in ranx/utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|