|
5 | 5 | import keras |
6 | 6 | import numpy as np |
7 | 7 | import sys |
| 8 | +from warnings import warn |
8 | 9 |
|
9 | 10 | # this import needs to be exactly like this to work with monkey patching |
10 | 11 | from keras.saving import deserialize_keras_object |
|
19 | 20 |
|
20 | 21 |
|
21 | 22 | def serialize_value_or_type(config, name, obj): |
22 | | - """Serialize an object that can be either a value or a type |
23 | | - and add it to a copy of the supplied dictionary. |
24 | | -
|
25 | | - Parameters |
26 | | - ---------- |
27 | | - config : dict |
28 | | - Dictionary to add the serialized object to. This function does not |
29 | | - modify the dictionary in place, but returns a modified copy. |
30 | | - name : str |
31 | | - Name of the obj that should be stored. Required for later deserialization. |
32 | | - obj : object or type |
33 | | - The object to serialize. If `obj` is of type `type`, we use |
34 | | - `keras.saving.get_registered_name` to obtain the registered type name. |
35 | | - If it is not a type, we try to serialize it as a Keras object. |
36 | | -
|
37 | | - Returns |
38 | | - ------- |
39 | | - updated_config : dict |
40 | | - Updated dictionary with a new key `"_bayesflow_<name>_type"` or |
41 | | - `"_bayesflow_<name>_val"`. The prefix is used to avoid name collisions, |
42 | | - the suffix indicates how the stored value has to be deserialized. |
43 | | -
|
44 | | - Notes |
45 | | - ----- |
46 | | - We allow strings or `type` parameters at several places to instantiate objects |
47 | | - of a given type (e.g., `subnet` in `CouplingFlow`). As `type` objects cannot |
48 | | - be serialized, we have to distinguish the two cases for serialization and |
49 | | - deserialization. This function is a helper function to standardize and |
50 | | - simplify this. |
51 | | - """ |
52 | | - updated_config = config.copy() |
53 | | - if isinstance(obj, type): |
54 | | - updated_config[f"{PREFIX}{name}_type"] = keras.saving.get_registered_name(obj) |
55 | | - else: |
56 | | - updated_config[f"{PREFIX}{name}_val"] = keras.saving.serialize_keras_object(obj) |
57 | | - return updated_config |
| 23 | + """This function is deprecated.""" |
| 24 | + warn( |
| 25 | + "This method is deprecated. It was replaced by bayesflow.utils.serialization.serialize.", |
| 26 | + DeprecationWarning, |
| 27 | + stacklevel=2, |
| 28 | + ) |
58 | 29 |
|
59 | 30 |
|
60 | 31 | def deserialize_value_or_type(config, name): |
61 | | - """Deserialize an object that can be either a value or a type and add |
62 | | - it to the supplied dictionary. |
63 | | -
|
64 | | - Parameters |
65 | | - ---------- |
66 | | - config : dict |
67 | | - Dictionary containing the object to deserialize. If a type was |
68 | | - serialized, it should contain the key `"_bayesflow_<name>_type"`. |
69 | | - If an object was serialized, it should contain the key |
70 | | - `"_bayesflow_<name>_val"`. In a copy of this dictionary, |
71 | | - the item will be replaced with the key `name`. |
72 | | - name : str |
73 | | - Name of the object to deserialize. |
74 | | -
|
75 | | - Returns |
76 | | - ------- |
77 | | - updated_config : dict |
78 | | - Updated dictionary with a new key `name`, with a value that is either |
79 | | - a type or an object. |
80 | | -
|
81 | | - See Also |
82 | | - -------- |
83 | | - serialize_value_or_type |
84 | | - """ |
85 | | - updated_config = config.copy() |
86 | | - if f"{PREFIX}{name}_type" in config: |
87 | | - updated_config[name] = keras.saving.get_registered_object(config[f"{PREFIX}{name}_type"]) |
88 | | - del updated_config[f"{PREFIX}{name}_type"] |
89 | | - elif f"{PREFIX}{name}_val" in config: |
90 | | - updated_config[name] = keras.saving.deserialize_keras_object(config[f"{PREFIX}{name}_val"]) |
91 | | - del updated_config[f"{PREFIX}{name}_val"] |
92 | | - return updated_config |
| 32 | + """This function is deprecated.""" |
| 33 | + warn( |
| 34 | + "This method is deprecated. It was replaced by bayesflow.utils.serialization.deserialize.", |
| 35 | + DeprecationWarning, |
| 36 | + stacklevel=2, |
| 37 | + ) |
93 | 38 |
|
94 | 39 |
|
95 | 40 | def deserialize(config: dict, custom_objects=None, safe_mode=True, **kwargs): |
|
0 commit comments