Small example for Tree

Currently, there is no functionality in the ghhops-server implementation to use tree output. Since I have been struggling with this for some weeks now, I tried to implement my own fix for this:

You will have to edit params.py in you ghhops-server installation, and replace the from_result function.

Now your server has the capabilities of putting out trees, which will need to be dictionaries. You can use something like this to convert i.e. a numpy array to a hops-compatbile dict that can be used with tree output:

def np_float_array_to_hops_tree(np_array: np.array, paths: list = []):
    """
    Converts a numpy array to a Hops DataTree (dict with paths as keys).
    """
    if not paths:
        paths = ["{0;" + str(x) + "}" for x in range(np_array.shape[0])]
    tree = {}
    for i, branch in enumerate(np_array):
        tree[paths[i].strip("}{")] = [float(v) for v in branch]
    return tree

@stevebaer I’m really sorry to be bugging with this, but it would be absolutely great if we could get an “official” solution for outputting datatrees through hops python servers. We plan on using a lot of hops stuff with students in the future and would really need it to avoid having them to copy-paste our workaround/hack like described above.

1 Like