|
| 1 | +import pickle |
1 | 2 | from pathlib import Path |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | from lir.config.lrsystem_architectures import specific_source |
5 | 6 | from lir.data.models import FeatureData, LLRData |
| 7 | +from lir.datasets.feature_data_csv import FeatureDataCsvFileParser |
6 | 8 | from lir.lrsystems.lrsystems import LRSystem |
7 | 9 |
|
8 | 10 | from lrmodule import persistence |
9 | 11 | from lrmodule.data_types import ModelSettings |
10 | 12 | from lrmodule.lrsystem import get_trained_model |
11 | 13 |
|
12 | 14 |
|
| 15 | +def get_lr_system(lr_system_folder: Path, file_name: str = "model.pkl") -> LRSystem: |
| 16 | + """ |
| 17 | + Load a trained LR system from disk from a given folder. |
| 18 | +
|
| 19 | + It is expected that the folder contains a file named "model.pkl" (or another name specified by file_name), |
| 20 | + which is a pickled LRSystem object. The function loads this object and returns it. |
| 21 | +
|
| 22 | + The system is returned as an instance of the LRSystem class. This class provides an apply method, which can be used |
| 23 | + to calculate LLRs for given features. These features should be contained in a FeatureData object from lir. |
| 24 | +
|
| 25 | + Example usage: |
| 26 | + ``` |
| 27 | + from lir.data.models import FeatureData |
| 28 | +
|
| 29 | + lr_system = get_lr_system(Path("path/to/lr_system_folder/")) |
| 30 | +
|
| 31 | + # Create three instances of features, each with two feature values. |
| 32 | + features = np.array([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]) |
| 33 | + feature_data = FeatureData(features=features) |
| 34 | + llr_data = lr_system.apply(feature_data) |
| 35 | + ``` |
| 36 | + """ |
| 37 | + with (lr_system_folder / file_name).open("rb") as f: |
| 38 | + return pickle.load(f) # noqa: S301 |
| 39 | + |
| 40 | + |
| 41 | +def get_reference_data(lr_system_folder: Path, file_name: str = "reference_data.csv") -> FeatureData: |
| 42 | + """Load reference data from disk. |
| 43 | +
|
| 44 | + It is expected that the folder contains a file named "reference_data.csv" (or another name specified by file_name), |
| 45 | + which is a CSV file containing the reference data. The function loads this data and returns it as a FeatureData |
| 46 | + object. |
| 47 | +
|
| 48 | + If the data has `n` features, the CSV file should have `n+1` columns. One of the columns should be named |
| 49 | + "hypothesis", and contain the labels for the data. The other columns should contain the feature values. |
| 50 | +
|
| 51 | + The data is returned as an instance of the FeatureData class from lir. This class has a features and a labels |
| 52 | + attribute, which can be used to access the feature values and the labels, respectively. |
| 53 | + """ |
| 54 | + reference_data_file = lr_system_folder / file_name |
| 55 | + return FeatureDataCsvFileParser(file=reference_data_file, label_column="hypothesis").get_instances() |
| 56 | + |
| 57 | + |
13 | 58 | def get_model(settings: ModelSettings, training_data: FeatureData, model_storage_path: Path | None) -> LRSystem: |
14 | 59 | """ |
15 | 60 | Obtain a model by loading it from disk, or by fitting it from training data. |
|
0 commit comments