Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion invisible_cities/cities/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,37 @@ def index_tables(file_out):
table.colinstances[colname].create_index()


def dict_to_string(arg : dict,
parent_key : str= ""):
'''
A function that recusively flattens nested dictionaries and converts the values to strings.

Each nested key is combined with its parent keys and associated with its respective value
as a string.

Parameters
----------
arg : the dictionary to flatten which can contain nested dictionaries
parent_key : the string containing the prefix to use for keys in the flattened dictionary (default "").

Returns
-------
flat_dict : flattened dictionary containing no nested dictionaries and where all values are strings
'''
flat_dict = {}
for k, v in arg.items():
new_key = f"{parent_key}.{k}" if parent_key else k
if isinstance(v, dict):
flat_dict.update(dict_to_string(v, new_key))
else:
flat_dict[new_key] = str(v)
return flat_dict


def write_city_configuration( filename : str
, city_name: str
, args : dict):
args = {k: str(v) for k,v in args.items()}
args = dict_to_string(args)

with tb.open_file(filename, "a") as file:
df = (pd.Series(args)
Expand Down
11 changes: 10 additions & 1 deletion invisible_cities/cities/components_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,17 +560,26 @@ def test_write_city_configuration(config_tmpdir):
d = "two strings".split(),
e = [1,2,3],
f = np.linspace(0, 1, 5),
g = {'a' : 2, 'b' : 3, 'c' : {'alpha' : 5, 'beta' : 10}}
)
write_city_configuration(filename, city_name, args)
with tb.open_file(filename, "r") as file:
assert "config" in file.root
assert city_name in file.root.config

df = pd.read_hdf(filename, "/config/" + city_name).set_index("variable")
for var, value in args.items():

# ignoring the nested dictionary
for var, value in list(args.items())[:-1]:
assert var in df.index
assert str(value) == df.value.loc[var]

# considering just the nested dictionary
assert 'g.a' in df.index and str(args['g']['a']) == df.value.loc['g.a']
assert 'g.b' in df.index and str(args['g']['b']) == df.value.loc['g.b']
assert 'g.c.alpha' in df.index and str(args['g']['c']['alpha']) == df.value.loc['g.c.alpha']
assert 'g.c.beta' in df.index and str(args['g']['c']['beta']) == df.value.loc['g.c.beta']


def test_copy_cities_configuration(config_tmpdir):
filename1 = os.path.join(config_tmpdir, "test_copy_cities_configuration_1.h5")
Expand Down