Unverified Commit b4559f60 authored by liuzhe-lz's avatar liuzhe-lz Committed by GitHub
Browse files

Fix issue 4810 (#4866)

parent efd8c7c8
......@@ -171,7 +171,7 @@ class TpeTuner(Tuner):
def generate_parameters(self, parameter_id, **kwargs):
if self.liar and self._running_params:
# give a fake loss for each concurrently running paramater set
history = {key: records.copy() for key, records in self._history.items()} # copy history
history = defaultdict(list, {key: records.copy() for key, records in self._history.items()}) # copy history
lie = self.liar.lie()
for param in self._running_params.values():
for key, value in param.items():
......
......@@ -392,7 +392,8 @@ def _dump(*, obj: Any, fp: Optional[Any], use_trace: bool, pickle_size_limit: in
return json_tricks.dumps(obj, obj_encoders=encoders, **json_tricks_kwargs)
def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comments: bool = True, **json_tricks_kwargs) -> Any:
def load(string: Optional[str] = None, *, fp: Optional[Any] = None,
preserve_order: bool = False, ignore_comments: bool = True, **json_tricks_kwargs) -> Any:
"""
Load the string or from file, and convert it to a complex data structure.
At least one of string or fp has to be not none.
......@@ -403,6 +404,10 @@ def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comme
JSON string to parse. Can be set to none if fp is used.
fp : str
File path to load JSON from. Can be set to none if string is used.
preserve_order : bool
`json_tricks parameter <https://json-tricks.readthedocs.io/en/latest/#order>`_
to use ``OrderedDict`` instead of ``dict``.
The order is in fact always preserved even when this is False.
ignore_comments : bool
Remove comments (starting with ``#`` or ``//``). Default is true.
......@@ -427,6 +432,8 @@ def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comme
_json_tricks_any_object_decode
]
# there was an issue that the user code does not accept ordered dict, and 3.7+ dict has guaranteed order
json_tricks_kwargs['preserve_order'] = preserve_order
# to bypass a deprecation warning in json-tricks
json_tricks_kwargs['ignore_comments'] = ignore_comments
......
from collections import OrderedDict
import math
import os
import pickle
......@@ -24,6 +25,18 @@ if True: # prevent auto formatting
from imported._test_serializer_py38 import test_positional_only
def test_ordered_json():
items = [
('a', 1),
('c', 3),
('b', 2),
]
orig = OrderedDict(items)
json = nni.dump(orig)
loaded = nni.load(json)
assert list(loaded.items()) == items
@nni.trace
class SimpleClass:
def __init__(self, a, b=1):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment