linkml-store 0.1.12__py3-none-any.whl → 0.1.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of linkml-store might be problematic. Click here for more details.

Files changed (31) hide show
  1. linkml_store/api/client.py +37 -8
  2. linkml_store/api/collection.py +81 -9
  3. linkml_store/api/config.py +28 -1
  4. linkml_store/api/database.py +26 -3
  5. linkml_store/api/stores/mongodb/mongodb_collection.py +4 -0
  6. linkml_store/api/stores/neo4j/__init__.py +0 -0
  7. linkml_store/api/stores/neo4j/neo4j_collection.py +429 -0
  8. linkml_store/api/stores/neo4j/neo4j_database.py +154 -0
  9. linkml_store/cli.py +140 -13
  10. linkml_store/graphs/__init__.py +0 -0
  11. linkml_store/graphs/graph_map.py +24 -0
  12. linkml_store/inference/__init__.py +13 -0
  13. linkml_store/inference/implementations/__init__.py +0 -0
  14. linkml_store/inference/implementations/rag_inference_engine.py +145 -0
  15. linkml_store/inference/implementations/rule_based_inference_engine.py +158 -0
  16. linkml_store/inference/implementations/sklearn_inference_engine.py +290 -0
  17. linkml_store/inference/inference_config.py +62 -0
  18. linkml_store/inference/inference_engine.py +173 -0
  19. linkml_store/inference/inference_engine_registry.py +74 -0
  20. linkml_store/utils/format_utils.py +21 -90
  21. linkml_store/utils/llm_utils.py +95 -0
  22. linkml_store/utils/neo4j_utils.py +42 -0
  23. linkml_store/utils/object_utils.py +3 -1
  24. linkml_store/utils/pandas_utils.py +55 -2
  25. linkml_store/utils/sklearn_utils.py +193 -0
  26. linkml_store/utils/stats_utils.py +53 -0
  27. {linkml_store-0.1.12.dist-info → linkml_store-0.1.14.dist-info}/METADATA +30 -3
  28. {linkml_store-0.1.12.dist-info → linkml_store-0.1.14.dist-info}/RECORD +31 -14
  29. {linkml_store-0.1.12.dist-info → linkml_store-0.1.14.dist-info}/LICENSE +0 -0
  30. {linkml_store-0.1.12.dist-info → linkml_store-0.1.14.dist-info}/WHEEL +0 -0
  31. {linkml_store-0.1.12.dist-info → linkml_store-0.1.14.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,290 @@
1
+ import logging
2
+ from dataclasses import dataclass, field
3
+ from pathlib import Path
4
+ from typing import Any, ClassVar, Dict, List, Optional, TextIO, Type, Union
5
+
6
+ import numpy as np
7
+ import pandas as pd
8
+ from sklearn.model_selection import cross_val_score
9
+ from sklearn.preprocessing import LabelEncoder, MultiLabelBinarizer, OneHotEncoder
10
+ from sklearn.tree import DecisionTreeClassifier
11
+
12
+ from linkml_store.api.collection import OBJECT
13
+ from linkml_store.inference.implementations.rule_based_inference_engine import RuleBasedInferenceEngine
14
+ from linkml_store.inference.inference_config import Inference, InferenceConfig
15
+ from linkml_store.inference.inference_engine import InferenceEngine, ModelSerialization
16
+ from linkml_store.utils.sklearn_utils import tree_to_nested_expression, visualize_decision_tree
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ @dataclass
22
+ class SklearnInferenceEngine(InferenceEngine):
23
+ config: InferenceConfig
24
+ classifier: Any = None
25
+ encoders: Dict[str, Any] = field(default_factory=dict)
26
+ transformed_features: List[str] = field(default_factory=list)
27
+ transformed_targets: List[str] = field(default_factory=list)
28
+ skip_features: List[str] = field(default_factory=list)
29
+ categorical_encoder_class: Optional[Type[Union[OneHotEncoder, MultiLabelBinarizer]]] = None
30
+ maximum_proportion_distinct_features: float = 0.2
31
+ confidence: float = 0.0
32
+
33
+ strict: bool = False
34
+
35
+ PERSIST_COLS: ClassVar = [
36
+ "config",
37
+ "classifier",
38
+ "encoders",
39
+ "transformed_features",
40
+ "transformed_targets",
41
+ "skip_features",
42
+ "confidence",
43
+ ]
44
+
45
+ def _get_encoder(self, v: Union[List[Any], Any]) -> Any:
46
+ if isinstance(v, list):
47
+ if all(isinstance(x, list) for x in v):
48
+ return MultiLabelBinarizer()
49
+ elif all(isinstance(x, str) for x in v):
50
+ return OneHotEncoder(sparse_output=False, handle_unknown="ignore")
51
+ elif all(isinstance(x, (int, float)) for x in v):
52
+ return None
53
+ else:
54
+ raise ValueError("Mixed data types in the list are not supported")
55
+ else:
56
+ if hasattr(v, "dtype"):
57
+ if v.dtype == "object" or v.dtype.name == "category":
58
+ if isinstance(v.iloc[0], list):
59
+ return MultiLabelBinarizer()
60
+ elif self.categorical_encoder_class:
61
+ return self.categorical_encoder_class(handle_unknown="ignore")
62
+ else:
63
+ return OneHotEncoder(sparse_output=False, handle_unknown="ignore")
64
+ elif v.dtype.kind in "biufc":
65
+ return None
66
+ raise ValueError("Unable to determine appropriate encoder for the input data")
67
+
68
+ def _is_complex_column(self, column: pd.Series) -> bool:
69
+ """Check if the column contains complex data types like lists or dicts."""
70
+ # MV_TYPE = (list, dict)
71
+ MV_TYPE = (list,)
72
+ return (column.dtype == "object" or column.dtype == "category") and any(
73
+ isinstance(x, MV_TYPE) for x in column.dropna()
74
+ )
75
+
76
+ def _get_unique_values(self, column: pd.Series) -> set:
77
+ """Get unique values from a column, handling list-type data."""
78
+ if self._is_complex_column(column):
79
+ # For columns with lists, flatten the lists and get unique values
80
+ return set(
81
+ item for sublist in column.dropna() for item in (sublist if isinstance(sublist, list) else [sublist])
82
+ )
83
+ else:
84
+ return set(column.unique())
85
+
86
+ def initialize_model(self, **kwargs):
87
+ logger.info(f"Initializing model with config: {self.config}")
88
+ df = self.training_data.as_dataframe(flattened=True)
89
+ logger.info(f"Training data shape: {df.shape}")
90
+ target_cols = self.config.target_attributes
91
+ feature_cols = self.config.feature_attributes
92
+ if len(target_cols) != 1:
93
+ raise ValueError("Only one target column is supported")
94
+ if not feature_cols:
95
+ feature_cols = df.columns.difference(target_cols).tolist()
96
+ self.config.feature_attributes = feature_cols
97
+ target_col = target_cols[0]
98
+ logger.info(f"Feature columns: {feature_cols}")
99
+ X = df[feature_cols].copy()
100
+ logger.info(f"Target column: {target_col}")
101
+ y = df[target_col].copy()
102
+
103
+ # find list of features to skip (categorical with > N categories)
104
+ skip_features = []
105
+ for col in X.columns:
106
+ unique_values = self._get_unique_values(X[col])
107
+ if len(unique_values) > self.maximum_proportion_distinct_features * len(X[col]):
108
+ skip_features.append(col)
109
+ if False and (X[col].dtype == "object" or X[col].dtype.name == "category"):
110
+ if len(X[col].unique()) > self.maximum_proportion_distinct_features * len(X[col]):
111
+ skip_features.append(col)
112
+ self.skip_features = skip_features
113
+ X = X.drop(skip_features, axis=1)
114
+ logger.info(f"Skipping features: {skip_features}")
115
+
116
+ # Encode features
117
+ encoded_features = []
118
+ for col in X.columns:
119
+ logger.info(f"Checking whether to encode: {col}")
120
+ col_encoder = self._get_encoder(X[col])
121
+ if col_encoder:
122
+ self.encoders[col] = col_encoder
123
+ if isinstance(col_encoder, OneHotEncoder):
124
+ encoded = col_encoder.fit_transform(X[[col]])
125
+ feature_names = col_encoder.get_feature_names_out([col])
126
+ encoded_df = pd.DataFrame(encoded, columns=feature_names, index=X.index)
127
+ X = pd.concat([X.drop(col, axis=1), encoded_df], axis=1)
128
+ encoded_features.extend(feature_names)
129
+ elif isinstance(col_encoder, MultiLabelBinarizer):
130
+ encoded = col_encoder.fit_transform(X[col])
131
+ feature_names = [f"{col}_{c}" for c in col_encoder.classes_]
132
+ encoded_df = pd.DataFrame(encoded, columns=feature_names, index=X.index)
133
+ X = pd.concat([X.drop(col, axis=1), encoded_df], axis=1)
134
+ encoded_features.extend(feature_names)
135
+ else:
136
+ X[col] = col_encoder.fit_transform(X[col])
137
+ encoded_features.append(col)
138
+ else:
139
+ encoded_features.append(col)
140
+
141
+ self.transformed_features = encoded_features
142
+ logger.info(f"Encoded features: {self.transformed_features}")
143
+ logger.info(f"Number of features after encoding: {len(self.transformed_features)}")
144
+
145
+ # Encode target
146
+ # y_encoder = LabelEncoder()
147
+ y_encoder = self._get_encoder(y)
148
+ if isinstance(y_encoder, OneHotEncoder):
149
+ y_encoder = LabelEncoder()
150
+ # self.encoders[target_col] = y_encoder
151
+ if y_encoder:
152
+ self.encoders[target_col] = y_encoder
153
+ y = y_encoder.fit_transform(y.values.ravel()) # Convert to 1D numpy array
154
+ self.transformed_targets = y_encoder.classes_
155
+
156
+ logger.info(f"Fitting model with features: {X.columns}")
157
+ clf = DecisionTreeClassifier(random_state=42)
158
+ clf.fit(X, y)
159
+ self.classifier = clf
160
+ logger.info("Model fit complete")
161
+ cv_scores = cross_val_score(self.classifier, X, y, cv=5)
162
+ self.confidence = cv_scores.mean()
163
+ logger.info(f"Cross-validation scores: {cv_scores}")
164
+
165
+ def derive(self, object: OBJECT) -> Optional[Inference]:
166
+ object = self._normalize(object)
167
+ new_X = pd.DataFrame([object])
168
+
169
+ # Apply encodings
170
+ encoded_features = {}
171
+ for col in self.config.feature_attributes:
172
+ if col in self.skip_features:
173
+ continue
174
+ if col in self.encoders:
175
+ encoder = self.encoders[col]
176
+ if isinstance(encoder, OneHotEncoder):
177
+ encoded = encoder.transform(new_X[[col]])
178
+ feature_names = encoder.get_feature_names_out([col])
179
+ for i, name in enumerate(feature_names):
180
+ encoded_features[name] = encoded[0, i]
181
+ elif isinstance(encoder, MultiLabelBinarizer):
182
+ encoded = encoder.transform(new_X[col])
183
+ feature_names = [f"{col}_{c}" for c in encoder.classes_]
184
+ for i, name in enumerate(feature_names):
185
+ encoded_features[name] = encoded[0, i]
186
+ else: # LabelEncoder or similar
187
+ encoded_features[col] = encoder.transform(new_X[col].astype(str))[0]
188
+ else:
189
+ encoded_features[col] = new_X[col].iloc[0]
190
+
191
+ # Ensure all expected features are present and in the correct order
192
+ final_features = []
193
+ for feature in self.transformed_features:
194
+ if feature in encoded_features:
195
+ final_features.append(encoded_features[feature])
196
+ else:
197
+ final_features.append(0) # or some other default value
198
+
199
+ # Create the final input array
200
+ new_X_array = np.array(final_features).reshape(1, -1)
201
+
202
+ logger.info(f"Input features: {self.transformed_features}")
203
+ logger.info(f"Number of input features: {len(self.transformed_features)}")
204
+
205
+ predictions = self.classifier.predict(new_X_array)
206
+ target_attribute = self.config.target_attributes[0]
207
+ y_encoder = self.encoders.get(target_attribute)
208
+
209
+ if y_encoder:
210
+ v = y_encoder.inverse_transform(predictions)
211
+ else:
212
+ v = predictions
213
+
214
+ predicted_object = {target_attribute: v[0]}
215
+ logger.info(f"Predicted object: {predicted_object}")
216
+ return Inference(predicted_object=predicted_object, confidence=self.confidence)
217
+
218
+ def _normalize(self, object: OBJECT) -> OBJECT:
219
+ return {k: object.get(k, None) for k in self.config.feature_attributes}
220
+
221
+ def export_model(
222
+ self, output: Optional[Union[str, Path, TextIO]], model_serialization: ModelSerialization = None, **kwargs
223
+ ):
224
+ def as_file():
225
+ if isinstance(output, (str, Path)):
226
+ return open(output, "w")
227
+ return output
228
+
229
+ if model_serialization is None:
230
+ if isinstance(output, (str, Path)):
231
+ model_serialization = ModelSerialization.from_filepath(output)
232
+ if model_serialization is None:
233
+ model_serialization = ModelSerialization.JOBLIB
234
+
235
+ if model_serialization == ModelSerialization.LINKML_EXPRESSION:
236
+ expr = tree_to_nested_expression(
237
+ self.classifier,
238
+ self.transformed_features,
239
+ self.encoders.keys(),
240
+ feature_encoders=self.encoders,
241
+ target_encoder=self.encoders.get(self.config.target_attributes[0]),
242
+ )
243
+ as_file().write(expr)
244
+ elif model_serialization == ModelSerialization.JOBLIB:
245
+ self.save_model(output)
246
+ elif model_serialization == ModelSerialization.RULE_BASED:
247
+ rbie = RuleBasedInferenceEngine(config=self.config)
248
+ rbie.import_model_from(self)
249
+ rbie.save_model(output)
250
+ elif model_serialization == ModelSerialization.PNG:
251
+ visualize_decision_tree(self.classifier, self.transformed_features, self.transformed_targets, output)
252
+ else:
253
+ raise ValueError(f"Unsupported model serialization: {model_serialization}")
254
+
255
+ def save_model(self, output: Union[str, Path]) -> None:
256
+ """
257
+ Save the trained model and related data to a file.
258
+
259
+ :param output: Path to save the model
260
+ """
261
+ import joblib
262
+
263
+ if self.classifier is None:
264
+ raise ValueError("Model has not been trained. Call initialize_model() first.")
265
+
266
+ # Use self.PERSIST_COLS
267
+ model_data = {k: getattr(self, k) for k in self.PERSIST_COLS}
268
+
269
+ joblib.dump(model_data, output)
270
+
271
+ @classmethod
272
+ def load_model(cls, file_path: Union[str, Path]) -> "SklearnInferenceEngine":
273
+ """
274
+ Load a trained model and related data from a file.
275
+
276
+ :param file_path: Path to the saved model
277
+ :return: SklearnInferenceEngine instance with loaded model
278
+ """
279
+ import joblib
280
+
281
+ model_data = joblib.load(file_path)
282
+
283
+ engine = cls(config=model_data["config"])
284
+ for k, v in model_data.items():
285
+ if k == "config":
286
+ continue
287
+ setattr(engine, k, v)
288
+
289
+ logger.info(f"Model loaded from {file_path}")
290
+ return engine
@@ -0,0 +1,62 @@
1
+ import logging
2
+ from typing import List, Optional, Tuple
3
+
4
+ from pydantic import BaseModel, ConfigDict, Field
5
+
6
+ from linkml_store.api.collection import OBJECT
7
+ from linkml_store.utils.format_utils import Format, load_objects
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ class LLMConfig(BaseModel, extra="forbid"):
13
+ """
14
+ Configuration for the LLM indexer.
15
+ """
16
+
17
+ model_config = ConfigDict(protected_namespaces=())
18
+
19
+ model_name: str = "gpt-4o-mini"
20
+ token_limit: Optional[int] = None
21
+ number_of_few_shot_examples: Optional[int] = None
22
+ role: str = "Domain Expert"
23
+ cached_embeddings_database: Optional[str] = None
24
+ cached_embeddings_collection: Optional[str] = None
25
+ text_template: Optional[str] = None
26
+ text_template_syntax: Optional[str] = None
27
+
28
+
29
+ class InferenceConfig(BaseModel, extra="forbid"):
30
+ """
31
+ Configuration for inference engines.
32
+ """
33
+
34
+ target_attributes: Optional[List[str]] = None
35
+ feature_attributes: Optional[List[str]] = None
36
+ train_test_split: Optional[Tuple[float, float]] = None
37
+ llm_config: Optional[LLMConfig] = None
38
+
39
+ @classmethod
40
+ def from_file(cls, file_path: str, format: Optional[Format] = None) -> "InferenceConfig":
41
+ """
42
+ Load an inference config from a file.
43
+
44
+ :param file_path: Path to the file.
45
+ :param format: Format of the file (YAML is recommended).
46
+ :return: InferenceConfig
47
+ """
48
+ if format and format.is_xsv():
49
+ logger.warning("XSV format is not recommended for inference config files")
50
+ objs = load_objects(file_path, format=format)
51
+ if len(objs) != 1:
52
+ raise ValueError(f"Expected 1 object, got {len(objs)}")
53
+ return cls(**objs[0])
54
+
55
+
56
+ class Inference(BaseModel, extra="forbid"):
57
+ """
58
+ Result of an inference derivation.
59
+ """
60
+
61
+ predicted_object: OBJECT = Field(..., description="The predicted object.")
62
+ confidence: Optional[float] = Field(default=None, description="The confidence of the prediction.", le=1.0, ge=0.0)
@@ -0,0 +1,173 @@
1
+ import logging
2
+ from abc import ABC
3
+ from dataclasses import dataclass
4
+ from enum import Enum
5
+ from pathlib import Path
6
+ from typing import Optional, TextIO, Tuple, Union
7
+
8
+ import pandas as pd
9
+ from pydantic import BaseModel, ConfigDict, Field
10
+
11
+ from linkml_store.api.collection import OBJECT, Collection
12
+ from linkml_store.inference.inference_config import Inference, InferenceConfig
13
+ from linkml_store.utils.pandas_utils import nested_objects_to_dataframe
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ class ModelSerialization(str, Enum):
19
+ """
20
+ Enum for model serialization types.
21
+ """
22
+
23
+ PICKLE = "pickle"
24
+ ONNX = "onnx"
25
+ PMML = "pmml"
26
+ PFA = "pfa"
27
+ JOBLIB = "joblib"
28
+ PNG = "png"
29
+ LINKML_EXPRESSION = "linkml_expression"
30
+ RULE_BASED = "rulebased"
31
+
32
+ @classmethod
33
+ def from_filepath(cls, file_path: str) -> Optional["ModelSerialization"]:
34
+ """
35
+ Get the serialization type from the file path.
36
+
37
+ >>> ModelSerialization.from_filepath("model.onnx")
38
+ <ModelSerialization.ONNX: 'onnx'>
39
+ >>> ModelSerialization.from_filepath("model.pkl")
40
+ <ModelSerialization.PICKLE: 'pickle'>
41
+ >>> assert ModelSerialization.from_filepath("poor_file_name") is None
42
+
43
+ :param file_path:
44
+ :return:
45
+ """
46
+ toks = file_path.split(".")
47
+ suffix = toks[-1]
48
+ if len(toks) > 2:
49
+ if suffix == "yaml" and toks[-2] == "rulebased":
50
+ return cls.RULE_BASED
51
+ # Generate mapping dynamically
52
+ extension_mapping = {v.lower(): v for v in cls}
53
+ # Add special cases
54
+ extension_mapping["pkl"] = cls.PICKLE
55
+ extension_mapping["py"] = cls.LINKML_EXPRESSION
56
+ return extension_mapping.get(suffix, None)
57
+
58
+
59
+ class CollectionSlice(BaseModel):
60
+ model_config = ConfigDict(arbitrary_types_allowed=True)
61
+
62
+ collection: Optional[Collection] = None
63
+ dataframe: Optional[pd.DataFrame] = None
64
+ slice: Tuple[Optional[int], Optional[int]] = Field(default=(None, None))
65
+
66
+ def as_dataframe(self, flattened=False) -> pd.DataFrame:
67
+ """
68
+ Return the slice of the collection as a dataframe.
69
+
70
+ :return:
71
+ """
72
+ if self.dataframe is not None:
73
+ df = self.dataframe
74
+ return df.iloc[self.slice[0] : self.slice[1]]
75
+ elif self.collection is not None:
76
+ rs = self.collection.find({}, offset=self.slice[0], limit=self.slice[1] - self.slice[0])
77
+ if flattened:
78
+ return nested_objects_to_dataframe(rs.rows)
79
+ else:
80
+ return rs.rows_dataframe
81
+ else:
82
+ raise ValueError("No dataframe or collection provided")
83
+
84
+
85
+ @dataclass
86
+ class InferenceEngine(ABC):
87
+ """
88
+ Base class for all inference engine.
89
+
90
+ An InferenceEngine is capable of deriving inferences from input objects and a collection.
91
+ """
92
+
93
+ predictor_type: Optional[str] = None
94
+ config: Optional[InferenceConfig] = None
95
+
96
+ training_data: Optional[CollectionSlice] = None
97
+ testing_data: Optional[CollectionSlice] = None
98
+
99
+ def load_and_split_data(self, collection: Collection, split: Optional[Tuple[float, float]] = None):
100
+ """
101
+ Load the data and split it into training and testing sets.
102
+
103
+ :param collection:
104
+ :param split:
105
+ :return:
106
+ """
107
+ split = split or self.config.train_test_split
108
+ if not split:
109
+ split = (0.7, 0.3)
110
+ logger.info(f"Loading and splitting data from collection {collection.alias}")
111
+ size = collection.size()
112
+ self.training_data = CollectionSlice(collection=collection, slice=(0, int(size * split[0])))
113
+ self.testing_data = CollectionSlice(collection=collection, slice=(int(size * split[0]), size))
114
+
115
+ def initialize_model(self, **kwargs):
116
+ """
117
+ Initialize the model.
118
+
119
+ :param kwargs:
120
+ :return:
121
+ """
122
+ raise NotImplementedError("Initialize model method must be implemented by subclass")
123
+
124
+ def export_model(
125
+ self, output: Optional[Union[str, Path, TextIO]], model_serialization: ModelSerialization = None, **kwargs
126
+ ):
127
+ """
128
+ Export the model to the given output.
129
+
130
+ :param model_serialization:
131
+ :param output:
132
+ :param kwargs:
133
+ :return:
134
+ """
135
+ raise NotImplementedError("Export model method must be implemented by subclass")
136
+
137
+ def import_model_from(self, inference_engine: "InferenceEngine", **kwargs):
138
+ """
139
+ Import the model from the given inference engine.
140
+
141
+ :param inference_engine:
142
+ :param kwargs:
143
+ :return:
144
+ """
145
+ raise NotImplementedError("Import model method must be implemented by subclass")
146
+
147
+ def save_model(self, output: Union[str, Path]) -> None:
148
+ """
149
+ Save the model to the given output.
150
+
151
+ :param output:
152
+ :return:
153
+ """
154
+ raise NotImplementedError("Save model method must be implemented by subclass")
155
+
156
+ @classmethod
157
+ def load_model(cls, file_path: Union[str, Path]) -> "InferenceEngine":
158
+ """
159
+ Load the model from the given file path.
160
+
161
+ :param file_path:
162
+ :return:
163
+ """
164
+ raise NotImplementedError("Load model method must be implemented by subclass")
165
+
166
+ def derive(self, object: OBJECT) -> Optional[Inference]:
167
+ """
168
+ Derive the prediction for the given object.
169
+
170
+ :param object:
171
+ :return:
172
+ """
173
+ raise NotImplementedError("Predict method must be implemented by subclass")
@@ -0,0 +1,74 @@
1
+ import importlib
2
+ import inspect
3
+ import os
4
+ from typing import Dict, Type
5
+
6
+ from linkml_store.inference.inference_config import InferenceConfig
7
+ from linkml_store.inference.inference_engine import InferenceEngine
8
+ from linkml_store.utils.object_utils import object_path_update
9
+
10
+
11
+ class InferenceEngineRegistry:
12
+ def __init__(self):
13
+ self.engines: Dict[str, Type[InferenceEngine]] = {}
14
+
15
+ def register(self, name: str, engine_class: Type[InferenceEngine]):
16
+ self.engines[name] = engine_class
17
+
18
+ def get_engine_class(self, name: str) -> Type[InferenceEngine]:
19
+ if name not in self.engines:
20
+ raise ValueError(f"Unknown inference engine type: {name}" f"Known engines: {list(self.engines.keys())}")
21
+ return self.engines[name]
22
+
23
+ def create_engine(self, engine_type: str, config: InferenceConfig = None, **kwargs) -> InferenceEngine:
24
+ kwargs = {k: v for k, v in kwargs.items() if v is not None}
25
+ if ":" in engine_type:
26
+ engine_type, conf_args = engine_type.split(":", 1)
27
+ if config is None:
28
+ config = InferenceConfig()
29
+ for arg in conf_args.split(","):
30
+ k, v = arg.split("=")
31
+ config = object_path_update(config, k, v)
32
+
33
+ engine_class = self.get_engine_class(engine_type)
34
+ kwargs["predictor_type"] = engine_type
35
+ return engine_class(config=config, **kwargs)
36
+
37
+ @classmethod
38
+ def load_engines(cls, package_path: str):
39
+ registry = cls()
40
+ package_dir = os.path.dirname(importlib.import_module(package_path).__file__)
41
+ for filename in os.listdir(package_dir):
42
+ if filename.endswith(".py") and not filename.startswith("__"):
43
+ module_name = f"{package_path}.{filename[:-3]}"
44
+ try:
45
+ module = importlib.import_module(module_name)
46
+ for name, obj in inspect.getmembers(module):
47
+ if inspect.isclass(obj) and issubclass(obj, InferenceEngine) and obj != InferenceEngine:
48
+ engine_name = name.lower().replace("inferenceengine", "")
49
+ registry.register(engine_name, obj)
50
+ except ImportError as e:
51
+ print(f"Error importing {module_name}: {e}")
52
+ return registry
53
+
54
+
55
+ # Initialize the registry
56
+ registry = InferenceEngineRegistry.load_engines("linkml_store.inference.implementations")
57
+
58
+
59
+ # Function to get an inference engine (can be used as before)
60
+ def get_inference_engine(engine_type: str, config: InferenceConfig = None, **kwargs) -> InferenceEngine:
61
+ """
62
+ Get an inference engine.
63
+
64
+ >>> from linkml_store.inference import get_inference_engine
65
+ >>> ie = get_inference_engine('sklearn')
66
+ >>> type(ie)
67
+ <class 'linkml_store.inference.implementations.sklearn_inference_engine.SklearnInferenceEngine'>
68
+
69
+ :param engine_type:
70
+ :param config:
71
+ :param kwargs:
72
+ :return:
73
+ """
74
+ return registry.create_engine(engine_type, config, **kwargs)