|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +import models.base as base_model |
| 8 | +import models.baseline as baseline_model |
| 9 | + |
| 10 | + |
| 11 | +class ParametersBase(ABC): |
| 12 | + """ |
| 13 | + An abstract class to represent the parameters of a model. |
| 14 | + """ |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + def create_Pipeline(self, index) -> base_model.Pipeline: |
| 18 | + """ |
| 19 | + Creates a pipeline with the given index. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + index : pt.Index |
| 24 | + The PyTerrier index. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + base_model.Pipeline |
| 29 | + The pipeline. |
| 30 | + """ |
| 31 | + ... |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + @abstractmethod |
| 35 | + def from_tuple(tup: Tuple) -> ParametersBase: |
| 36 | + """ |
| 37 | + Creates a ParametersBase object from a tuple. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + tup : Tuple |
| 42 | + The tuple. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + ParametersBase |
| 47 | + The ParametersBase object. |
| 48 | + """ |
| 49 | + ... |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class BaselineParameters(ParametersBase): |
| 54 | + """ |
| 55 | + A class to represent the parameters of the baseline retrieval method. |
| 56 | +
|
| 57 | + Attributes |
| 58 | + ---------- |
| 59 | + bm25_docs : int |
| 60 | + The number of documents to retrieve with BM25. |
| 61 | + mono_t5_docs : int |
| 62 | + The number of documents to rerank with MonoT5. |
| 63 | + duo_t5_docs : int |
| 64 | + The number of documents to rerank with DuoT5. |
| 65 | + """ |
| 66 | + |
| 67 | + bm_25_docs: int |
| 68 | + mono_t5_docs: int |
| 69 | + duo_t5_docs: int |
| 70 | + |
| 71 | + def create_Pipeline(self, index) -> base_model.Pipeline: |
| 72 | + """ |
| 73 | + Creates the baseline pipeline with the given index. |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + index : pt.Index |
| 78 | + The PyTerrier index. |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + base_model.Pipeline (baseline_model.Baseline) |
| 83 | + The baseline pipeline. |
| 84 | + """ |
| 85 | + return baseline_model.Baseline( |
| 86 | + index, |
| 87 | + bm25_docs=self.bm_25_docs, |
| 88 | + mono_t5_docs=self.mono_t5_docs, |
| 89 | + duo_t5_docs=self.duo_t5_docs, |
| 90 | + ) |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def from_tuple(tup: Tuple) -> ParametersBase: |
| 94 | + """ |
| 95 | + Creates a BaselineParameters object from a tuple. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + tup : Tuple[int, int, int] |
| 100 | + The tuple (bm25_docs, mono_t5_docs, duo_t5_docs) |
| 101 | +
|
| 102 | + Returns |
| 103 | + ------- |
| 104 | + ParametersBase (BaselineParameters) |
| 105 | + The ParametersBase object. |
| 106 | +
|
| 107 | + Raises |
| 108 | + ------ |
| 109 | + AssertionError |
| 110 | + If the tuple does not have 3 elements or if any of the elements is not a positive integer. |
| 111 | + """ |
| 112 | + assert len(tup) == 3, "The tuple must have 3 elements." |
| 113 | + for i in tup: |
| 114 | + assert isinstance(i, int), "All parameters must be integers." |
| 115 | + assert i > 0, "All parameters must be positive integers." |
| 116 | + return BaselineParameters(tup[0], tup[1], tup[2]) |
0 commit comments