openmodels 0.2.1__tar.gz → 0.2.2__tar.gz
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.
- {openmodels-0.2.1 → openmodels-0.2.2}/PKG-INFO +2 -2
- {openmodels-0.2.1 → openmodels-0.2.2}/README.md +1 -1
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/serializers/sklearn/sklearn_serializer.py +80 -21
- {openmodels-0.2.1 → openmodels-0.2.2}/pyproject.toml +1 -1
- {openmodels-0.2.1 → openmodels-0.2.2}/LICENSE +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/__init__.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/converters/__init__.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/converters/json_converter.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/converters/msgpack_converter.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/converters/pickle_converter.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/converters/yaml_converter.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/core.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/exceptions.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/format_registry.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/protocols.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/serializers/__init__.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/serializers/base.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/serializers/sklearn/__init__.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/serializers/sklearn/_custom_estimator.py +0 -0
- {openmodels-0.2.1 → openmodels-0.2.2}/openmodels/test_helpers.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: openmodels
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Export scikit-learn model files to JSON for sharing or deploying predictive models with peace of mind.
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -33,7 +33,7 @@ Description-Content-Type: text/markdown
|
|
|
33
33
|
|
|
34
34
|
# OpenModels
|
|
35
35
|
|
|
36
|
-
[](https://pypi.org/project/openmodels)
|
|
37
37
|
[](https://opensource.org/licenses/MIT)
|
|
38
38
|
[](https://pypi.org/project/openmodels/)
|
|
39
39
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# OpenModels
|
|
2
2
|
|
|
3
|
-
[](https://pypi.org/project/openmodels)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[](https://pypi.org/project/openmodels/)
|
|
6
6
|
|
|
@@ -8,6 +8,7 @@ converted to and from dictionary representations.
|
|
|
8
8
|
from typing import Any, Callable, Dict, List, Set, Tuple, Type, Optional, Union
|
|
9
9
|
from importlib.metadata import version as _package_version, PackageNotFoundError
|
|
10
10
|
from datetime import datetime, timezone
|
|
11
|
+
import platform
|
|
11
12
|
import sys
|
|
12
13
|
import numpy as np
|
|
13
14
|
import scipy # type: ignore
|
|
@@ -90,12 +91,19 @@ ALL_ESTIMATORS["_ConstantPredictor"] = _ConstantPredictor
|
|
|
90
91
|
TESTED_VERSIONS = ["1.6.1", "1.7.2", "1.8.0", "1.9.1"]
|
|
91
92
|
|
|
92
93
|
# Version of openmodels's own wire format (the shape of the serialized dict), independent of
|
|
93
|
-
# scikit-learn's version (
|
|
94
|
-
# (
|
|
94
|
+
# scikit-learn's version (domain_version) and of the writing tool's release version
|
|
95
|
+
# (producer_version, informational only). Bump this only when the structure or the meaning of
|
|
96
|
+
# its fields changes.
|
|
95
97
|
# v2: producer_version/producer_name/domain/openmodels_format_version/openmodels_version moved
|
|
96
98
|
# from flat top-level keys into a single nested "metadata" dict, present once at the true root
|
|
97
99
|
# (not duplicated on every nested/composite sub-estimator as in v1).
|
|
98
|
-
|
|
100
|
+
# v3: producer_name/producer_version follow ONNX and name the tool that wrote the file
|
|
101
|
+
# ("openmodels", or e.g. an R exporter), not the outermost model's package + scikit-learn's
|
|
102
|
+
# version; scikit-learn's version moved to the new domain_version field, and "producers" was
|
|
103
|
+
# renamed "packages" (always including the domain package). Readers fall back to
|
|
104
|
+
# producer_version as the scikit-learn version, and to "producers", for v1/v2 files only.
|
|
105
|
+
# openmodels_version was dropped: producer_version holds the same value.
|
|
106
|
+
OPENMODELS_FORMAT_VERSION = 3
|
|
99
107
|
|
|
100
108
|
|
|
101
109
|
def _openmodels_version() -> str:
|
|
@@ -338,11 +346,13 @@ class SklearnSerializer(
|
|
|
338
346
|
|
|
339
347
|
Notes
|
|
340
348
|
-----
|
|
341
|
-
- Issues a warning if the stored version does not match the current
|
|
342
|
-
|
|
343
|
-
-
|
|
349
|
+
- Issues a warning if the stored version string does not exactly match the current
|
|
350
|
+
version (patch-level differences included).
|
|
351
|
+
- Mentions the versions openmodels has been tested under (TESTED_VERSIONS).
|
|
352
|
+
- Does nothing if no version is stored (for backward compatibility), or if it is
|
|
353
|
+
"unknown" (a placeholder a non-openmodels writer may use).
|
|
344
354
|
"""
|
|
345
|
-
if not stored_version:
|
|
355
|
+
if not stored_version or stored_version == "unknown":
|
|
346
356
|
return # No version info available
|
|
347
357
|
|
|
348
358
|
current_version = sklearn.__version__
|
|
@@ -355,6 +365,38 @@ class SklearnSerializer(
|
|
|
355
365
|
UserWarning,
|
|
356
366
|
)
|
|
357
367
|
|
|
368
|
+
def _check_package_versions(self, packages: Optional[Dict[str, str]]) -> None:
|
|
369
|
+
"""
|
|
370
|
+
Check the stored version of every non-scikit-learn package whose estimator classes
|
|
371
|
+
the file needs (the "packages" metadata field) against the installed one.
|
|
372
|
+
|
|
373
|
+
Parameters
|
|
374
|
+
----------
|
|
375
|
+
packages : dict
|
|
376
|
+
``{package_name: version}`` recorded during serialization.
|
|
377
|
+
|
|
378
|
+
Notes
|
|
379
|
+
-----
|
|
380
|
+
- Issues a warning per package whose stored version doesn't exactly match the
|
|
381
|
+
installed one. scikit-learn itself is skipped - `_check_version` covers it.
|
|
382
|
+
- Skips entries whose stored or installed version is "unknown" (nothing to compare).
|
|
383
|
+
- Does nothing if no packages are stored (files predating this field).
|
|
384
|
+
"""
|
|
385
|
+
if not packages:
|
|
386
|
+
return
|
|
387
|
+
|
|
388
|
+
for name, stored_version in packages.items():
|
|
389
|
+
if name == "sklearn" or stored_version == "unknown":
|
|
390
|
+
continue
|
|
391
|
+
current_version = self._resolve_package_version(name)
|
|
392
|
+
if current_version != "unknown" and stored_version != current_version:
|
|
393
|
+
warnings.warn(
|
|
394
|
+
f"Version mismatch detected for package '{name}':\n"
|
|
395
|
+
f"- Model serialized with {name} {stored_version}\n"
|
|
396
|
+
f"- Current environment: {name} {current_version}",
|
|
397
|
+
UserWarning,
|
|
398
|
+
)
|
|
399
|
+
|
|
358
400
|
def _check_format_version(self, stored_version: Optional[int]) -> None:
|
|
359
401
|
"""
|
|
360
402
|
Check compatibility between the stored openmodels wire-format version and the version
|
|
@@ -370,7 +412,7 @@ class SklearnSerializer(
|
|
|
370
412
|
- Issues a warning if the stored version is newer than what this installation
|
|
371
413
|
understands (the file may use a structure introduced after this release).
|
|
372
414
|
- Does nothing if no version is stored - true for every file written before this field
|
|
373
|
-
existed, which
|
|
415
|
+
existed, which all have the version-1 (flat, unversioned) shape.
|
|
374
416
|
"""
|
|
375
417
|
if stored_version is None:
|
|
376
418
|
return # No format version info available - pre-versioning file.
|
|
@@ -1152,7 +1194,7 @@ class SklearnSerializer(
|
|
|
1152
1194
|
except PackageNotFoundError:
|
|
1153
1195
|
return "unknown"
|
|
1154
1196
|
|
|
1155
|
-
def
|
|
1197
|
+
def _collect_package_names(self, serialized: Any, names: Set[str]) -> None:
|
|
1156
1198
|
"""
|
|
1157
1199
|
Recursively walk an already-serialized estimator dict (params/attributes, however
|
|
1158
1200
|
deeply nested) and collect the top-level package name of every estimator class found
|
|
@@ -1167,10 +1209,10 @@ class SklearnSerializer(
|
|
|
1167
1209
|
if cls is not None:
|
|
1168
1210
|
names.add(cls.__module__.split(".")[0])
|
|
1169
1211
|
for value in serialized.values():
|
|
1170
|
-
self.
|
|
1212
|
+
self._collect_package_names(value, names)
|
|
1171
1213
|
elif isinstance(serialized, (list, tuple)):
|
|
1172
1214
|
for item in serialized:
|
|
1173
|
-
self.
|
|
1215
|
+
self._collect_package_names(item, names)
|
|
1174
1216
|
|
|
1175
1217
|
def serialize(self, model: BaseEstimator) -> Dict[str, Any]:
|
|
1176
1218
|
"""
|
|
@@ -1206,21 +1248,24 @@ class SklearnSerializer(
|
|
|
1206
1248
|
"""
|
|
1207
1249
|
serialized_estimator = self._serialize_core(model)
|
|
1208
1250
|
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1251
|
+
# The domain package is always a dependency, even when no class in the tree is its own.
|
|
1252
|
+
package_names: Set[str] = {"sklearn"}
|
|
1253
|
+
self._collect_package_names(serialized_estimator, package_names)
|
|
1254
|
+
packages = {
|
|
1255
|
+
name: self._resolve_package_version(name) for name in sorted(package_names)
|
|
1213
1256
|
}
|
|
1214
1257
|
|
|
1215
1258
|
metadata = {
|
|
1216
|
-
|
|
1217
|
-
"producer_name":
|
|
1218
|
-
"
|
|
1259
|
+
# ONNX-style: the tool that wrote this file, not the model's own package.
|
|
1260
|
+
"producer_name": "openmodels",
|
|
1261
|
+
"producer_version": _openmodels_version(),
|
|
1219
1262
|
"domain": "sklearn",
|
|
1263
|
+
"domain_version": sklearn.__version__,
|
|
1264
|
+
"packages": packages,
|
|
1220
1265
|
"openmodels_format_version": OPENMODELS_FORMAT_VERSION,
|
|
1221
|
-
"openmodels_version": _openmodels_version(),
|
|
1222
1266
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
1223
1267
|
"dependency_versions": {
|
|
1268
|
+
"python": platform.python_version(),
|
|
1224
1269
|
"numpy": np.__version__,
|
|
1225
1270
|
"scipy": scipy.__version__,
|
|
1226
1271
|
},
|
|
@@ -1258,8 +1303,22 @@ class SklearnSerializer(
|
|
|
1258
1303
|
# Version control check. `data` itself is the fallback for pre-v2 files, which have
|
|
1259
1304
|
# no nested "metadata" key and carried these fields flat at the top level instead.
|
|
1260
1305
|
metadata = data.get("metadata", data)
|
|
1261
|
-
|
|
1262
|
-
|
|
1306
|
+
format_version = metadata.get("openmodels_format_version")
|
|
1307
|
+
if "domain_version" in metadata:
|
|
1308
|
+
sklearn_version = metadata["domain_version"]
|
|
1309
|
+
elif format_version is None or format_version <= 2:
|
|
1310
|
+
# v1/v2 files have no domain_version; their producer_version always held the
|
|
1311
|
+
# scikit-learn version. From v3 on it's the writing tool's version, so it must
|
|
1312
|
+
# never be compared against scikit-learn.
|
|
1313
|
+
sklearn_version = metadata.get("producer_version")
|
|
1314
|
+
else:
|
|
1315
|
+
sklearn_version = None
|
|
1316
|
+
self._check_version(sklearn_version)
|
|
1317
|
+
# v2 files called this map "producers".
|
|
1318
|
+
self._check_package_versions(
|
|
1319
|
+
metadata.get("packages", metadata.get("producers"))
|
|
1320
|
+
)
|
|
1321
|
+
self._check_format_version(format_version)
|
|
1263
1322
|
|
|
1264
1323
|
return self._deserialize_core(data)
|
|
1265
1324
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "openmodels"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.2"
|
|
4
4
|
description = "Export scikit-learn model files to JSON for sharing or deploying predictive models with peace of mind."
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Alejandro Gutierrez", email = "a.gutierrez@g-npd.com" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|