diff --git a/python/triqs_dft_tools/block_structure.py b/python/triqs_dft_tools/block_structure.py index fb9e3786..5bfb1ca9 100644 --- a/python/triqs_dft_tools/block_structure.py +++ b/python/triqs_dft_tools/block_structure.py @@ -1157,5 +1157,45 @@ class BlockStructure(object): s += str(self.transformation) return s +def gf_struct_flatten(gf_struct): + ''' + flattens gf_struct objecti + + input gf_struct can looks like this: + + [('up', [0, 1, 2]), ('down', [0, 1, 2])] + + and will be returned as + + [('up', 3), ('down', 3)] + + Same for dict but replacing the values. This is for compatibility with the upcoming triqs releases. + + Parameters + ---------- + gf_struct: list of tuple or dict representing the Gf structure + __Returns:__ + gf_struct_flat: flattens the values of the dict or the tuple representing the Gf indices by replacing them with the len of the list of indices + + ''' + + if isinstance(gf_struct, list): + # create a copy of the original list + gf_struct_flat = gf_struct.copy() + for idx, block in enumerate(gf_struct_flat): + # exchange list of indices with length of list + gf_struct_flat[idx] = (block[0], len(block[1])) + elif isinstance(gf_struct, dict): + # create a copy of the original dict + gf_struct_flat = dict(gf_struct) + for key, value in gf_struct_flat.items(): + # exchange list of indices with length of list + gf_struct_flat[key] = len(value) + else: + raise Exception('gf_struct input needs to be list or dict') + + + return gf_struct_flat + from h5.formats import register_class register_class(BlockStructure)