ranklens.io¶
ranklens.io
¶
Streaming readers for runs and relevance judgements.
ClickLogSchema
dataclass
¶
Columns of a feedback log: one row per shown document.
Feedback is either a numeric reward column or an event column translated
by a FeedbackScale; exactly one of them is set. An empty event means the document
was shown and nothing happened.
Source code in src/ranklens/io/clicklog.py
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 | |
ErrorCollector
¶
Accumulates recoverable data errors; read the result with snapshot.
Memory is bounded: at most max_examples messages are kept per error type,
no matter how many rows are skipped.
Source code in src/ranklens/io/errors.py
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 | |
snapshot
¶
snapshot() -> ErrorSummary
Immutable summary of everything recorded so far.
Source code in src/ranklens/io/errors.py
32 33 34 35 36 37 38 | |
FeatureQuery
dataclass
¶
Documents of one query: rows[i] are the features of docs[i].
Source code in src/ranklens/io/features.py
42 43 44 45 46 47 48 49 | |
FeatureSchema
dataclass
¶
Id and grade columns; features empty means every other column.
Source code in src/ranklens/io/features.py
32 33 34 35 36 37 38 39 | |
ArrowStreamExportable
¶
Bases: Protocol
In-memory table exporting the Arrow C stream: pandas, polars, pyarrow, DuckDB.
Source code in src/ranklens/io/formats.py
44 45 46 47 48 | |
PairedRuns
¶
Merge-join of two runs sorted by query_id: pairs of rankings of the same query.
Written as an iterator rather than built on zip because the two runs have
different, only partly overlapping sets of queries: zip would pair unrelated
rankings as soon as one run skips a query. The merge keeps one ranking from each
side in memory (a lookahead of one), so two huge runs are still streamed.
Both sides must be sorted by query_id as strings — iter_run(check="sorted")
guarantees that. Queries present on one side only are counted, not paired.
Used as a context manager it closes both streams — handy when the loop breaks early.
Source code in src/ranklens/io/paired.py
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 | |
QrelsSchema
dataclass
¶
Names of qrels columns in csv/tsv headers and jsonl keys.
Source code in src/ranklens/io/schema.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
RunSchema
dataclass
¶
Names of run columns in csv/tsv headers and jsonl keys.
Source code in src/ranklens/io/schema.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
iter_clicklog
¶
iter_clicklog(
source: Source,
*,
schema: ClickLogSchema | None = None,
scale: FeedbackScale | None = None,
fmt: FormatName | None = None,
strict: bool = False,
errors: ErrorCollector | None = None,
) -> Iterator[Impression]
Stream a feedback log as one Impression per logged showing.
Rows of an impression must be contiguous; within it they may come in any order
and are sorted by position. With an event column, scale turns events into
rewards. Invalid rows follow the same rules as in iter_run: they raise in
strict mode and are skipped and recorded otherwise.
Source code in src/ranklens/io/clicklog.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
column_names
¶
column_names(
source: Source, fmt: FormatName | None = None
) -> list[str]
Columns of a csv/tsv header, of the first jsonl object, or of a parquet/Feather schema.
Source code in src/ranklens/io/features.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
read_feature_table
¶
read_feature_table(
source: Source,
*,
schema: FeatureSchema | None = None,
fmt: FormatName | None = None,
strict: bool = False,
errors: ErrorCollector | None = None,
) -> FeatureTable
Load a feature table grouped by query, in the order queries first appear.
Rows of a query need not be contiguous. A non-numeric feature or grade is a malformed
row: skipped and recorded, or raised in strict mode, as in the other readers.
A feature may be nan — a missing value, which boosting models handle; a grade
must be finite, and an empty grade leaves the document without a judgement.
Source code in src/ranklens/io/features.py
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 | |
detect_format
¶
detect_format(source: Source) -> FormatName
Format of a file by extension, or "arrow" for an in-memory table.
A compression suffix (.gz, .bz2, .xz) is skipped for text formats;
parquet and Feather compress internally.
Source code in src/ranklens/io/formats.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
iter_run
¶
iter_run(
source: Source,
*,
schema: RunSchema | None = None,
fmt: FormatName | None = None,
order: Literal["file", "rank", "score"] | None = None,
check: Literal["grouped", "sorted"] = "grouped",
strict: bool = False,
errors: ErrorCollector | None = None,
) -> Iterator[RankedList]
Stream a run as one RankedList per query.
source is a file (csv, tsv, jsonl, TREC, parquet, Feather) or an in-memory
table (pandas, polars, pyarrow). Only the rows of the current query are held
in memory.
order sets document positions: "file" order; ascending "rank" column
(stable for ties); or descending "score" with ties broken by descending doc_id
compared as strings — exactly as trec_eval, which ignores the file order.
By default TREC runs are ordered by score and other formats by file order.
check sets the input contract:
"grouped": rows of a query are contiguous, queries in any order. Memory grows with the number of queries (ids of finished queries are kept)."sorted": query ids ascend as strings. Constant memory; required when two runs are merged side by side.
Invalid rows raise in strict mode. Otherwise they are skipped and recorded
in errors; without a collector a SkippedRowsWarning is emitted
when the file is exhausted. Grouping and sorting violations always raise.
Source code in src/ranklens/io/readers.py
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 | |
read_qrels
¶
read_qrels(
source: Source,
*,
schema: QrelsSchema | None = None,
fmt: FormatName | None = None,
strict: bool = False,
errors: ErrorCollector | None = None,
) -> Qrels
Load relevance judgements into memory: qrels[query_id][doc_id] -> relevance.
source is any source accepted by iter_run.
Unlike runs, qrels are small and are needed for random access, so rows may come
in any order. A repeated (query_id, doc_id) pair is an error: the first
judgement is kept in non-strict mode. Error handling is the same as in
iter_run.
Source code in src/ranklens/io/readers.py
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 | |