コード例 #1
0
def pywr_json_to_d3_json(model, attributes=False):
    """
    Converts a JSON file or a JSON-derived dict into structure that d3js can use.

    Parameters
    ----------
    model : dict or str
        str inputs should be a path to a json file containing the model.
    """

    if isinstance(model, str):
        with open(model) as d:
            model = json.load(d)

    nodes = [node["name"] for node in model["nodes"]]

    edges = []
    for edge in model["edges"]:
        sourceindex = nodes.index(edge[0])
        targetindex = nodes.index(edge[1])
        edges.append({'source': sourceindex, 'target': targetindex})

    nodes = []
    node_classes = create_node_class_trees()

    for node in model["nodes"]:
        json_node = {
            'name': node["name"],
            'clss': node_classes[node["type"].lower()]
        }
        try:
            json_node['position'] = node['position']['schematic']
        except KeyError:
            pass

        if attributes:
            json_node["attributes"] = []
            for name, val in node.items():

                if name == "type":
                    continue

                attr_val = val

                if isinstance(val, dict):
                    try:
                        attr_val = get_parameter_from_registry(
                            val["type"]).__name__
                    except KeyError:
                        pass
                elif val in model["parameters"].keys():
                    param = model["parameters"][val]
                    attr_type = get_parameter_from_registry(
                        param["type"]).__name__
                    attr_val = attr_val + " - " + attr_type

                attr_dict = {"attribute": name, "value": attr_val}
                json_node["attributes"].append(attr_dict)

        nodes.append(json_node)

    graph = {"nodes": nodes, "links": edges}

    return graph
コード例 #2
0
ファイル: __init__.py プロジェクト: tomjanus/pywr
def pywr_json_to_d3_json(model, attributes=False):
    """
    Converts a JSON file or a JSON-derived dict into structure that d3js can use.

    Parameters
    ----------
    model : dict or str
        str inputs should be a path to a json file containing the model.
    """

    if isinstance(model, str):
        with open(model) as d:
            model = json.load(d)

    nodes = []
    node_classes = create_node_class_trees()
    for node in model["nodes"]:

        if node["type"].lower() in [
            "annualvirtualstorage",
            "virtualstorage",
            "aggregatednode",
            "aggregatedstorage",
            "seasonalvirtualstorage",
        ]:
            # Do not add virtual nodes to the graph
            continue

        json_node = {"name": node["name"], "clss": node_classes[node["type"].lower()]}
        try:
            json_node["position"] = node["position"]["schematic"]
        except KeyError:
            pass

        if attributes:
            json_node["attributes"] = []
            for name, val in node.items():

                if name == "type":
                    continue

                attr_val = val

                if isinstance(val, dict):
                    try:
                        attr_val = get_parameter_from_registry(val["type"]).__name__
                    except KeyError:
                        pass
                elif val in model["parameters"].keys():
                    param = model["parameters"][val]
                    attr_type = get_parameter_from_registry(param["type"]).__name__
                    attr_val = attr_val + " - " + attr_type
                else:
                    attr_val = str(attr_val)

                attr_dict = {"attribute": name, "value": attr_val}
                json_node["attributes"].append(attr_dict)

        nodes.append(json_node)

    nodes_names = [node["name"] for node in nodes]

    edges = []
    for edge in model["edges"]:
        sourceindex = nodes_names.index(edge[0])
        targetindex = nodes_names.index(edge[1])
        edges.append({"source": sourceindex, "target": targetindex})

    graph = {"nodes": nodes, "links": edges}

    return graph