Skip to content

Commit a638019

Browse files
Add functions to load LR system and reference data from disk
1 parent c76396b commit a638019

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

lrmodule/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
1+
import pickle
12
from pathlib import Path
23

34
import numpy as np
45
from lir.config.lrsystem_architectures import specific_source
56
from lir.data.models import FeatureData, LLRData
7+
from lir.datasets.feature_data_csv import FeatureDataCsvFileParser
68
from lir.lrsystems.lrsystems import LRSystem
79

810
from lrmodule import persistence
911
from lrmodule.data_types import ModelSettings
1012
from lrmodule.lrsystem import get_trained_model
1113

1214

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+
1358
def get_model(settings: ModelSettings, training_data: FeatureData, model_storage_path: Path | None) -> LRSystem:
1459
"""
1560
Obtain a model by loading it from disk, or by fitting it from training data.

validation.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ experiments:
189189
- method: lrmodule.copy_csv.copy_csv
190190
file: ${data_root}/breech_face_impression.csv
191191
new_file_name: source_data.csv
192+
- method: lrmodule.copy_csv.copy_csv
193+
file: ${data_root}/breech_face_impression.csv
194+
new_file_name: reference_data.csv
195+
columns:
196+
- hypothesis
197+
- accf
192198
- save_model
193199

194200

0 commit comments

Comments
 (0)