-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
148 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from copy import deepcopy | ||
|
||
def _iteratively_replace_none(to_write, replace_from, replace_with): | ||
""" Limitation: can only replace None with a string, or a string with None. """ | ||
# First two checks needed because comparison to triqs many-body operator fails | ||
if (isinstance(to_write, str) or to_write is None) and to_write == replace_from: | ||
return replace_with | ||
|
||
if isinstance(to_write, dict): | ||
for key, value in to_write.items(): | ||
to_write[key] = _iteratively_replace_none(value, replace_from, replace_with) | ||
elif isinstance(to_write, list): | ||
for i, value in enumerate(to_write): | ||
to_write[i] = _iteratively_replace_none(value, replace_from, replace_with) | ||
|
||
return to_write | ||
|
||
def prep_params_for_h5(dict_to_write): | ||
return _iteratively_replace_none(deepcopy(dict_to_write), None, 'none') | ||
|
||
# Not sure if the reverse route is actually needed | ||
def prep_params_from_h5(dict_to_read): | ||
return _iteratively_replace_none(deepcopy(dict_to_read), 'none', None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from solid_dmft.io_tools import dict_to_h5 | ||
|
||
def test_prep_params_for_h5(): | ||
inp = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']} | ||
expected = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']} | ||
|
||
assert dict_to_h5.prep_params_for_h5(inp) == expected | ||
|
||
def test_prep_params_from_h5(): | ||
inp = {'a': 'none', 'b': {'c': 'none', 'd': 'e'}, 'f': ['none', 'g']} | ||
expected = {'a': None, 'b': {'c': None, 'd': 'e'}, 'f': [None, 'g']} | ||
|
||
assert dict_to_h5.prep_params_from_h5(inp) == expected |