-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tojson writer and from_dict and from_json readers. (#57)
* Added to/from json and dict interfaces along with tests. * fix spelling * No longer testing thicket compatibility * black reformatting Co-authored-by: Stephanie Brink <[email protected]>
- Loading branch information
1 parent
c82e153
commit aaead67
Showing
5 changed files
with
142 additions
and
0 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
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,63 @@ | ||
# Copyright 2017-2022 Lawrence Livermore National Security, LLC and other | ||
# Hatchet Project Developers. See the top-level LICENSE file for details. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import json | ||
|
||
import pandas as pd | ||
|
||
import hatchet.graphframe | ||
from hatchet.node import Node | ||
from hatchet.graph import Graph | ||
from hatchet.frame import Frame | ||
|
||
|
||
class JsonReader: | ||
"""Create a GraphFrame from a json string of the following format. | ||
Return: | ||
(GraphFrame): graphframe containing data from dictionaries | ||
""" | ||
|
||
def __init__(self, json_spec): | ||
"""Read from a json string specification of a graphframe | ||
json (string): Json specification of a graphframe. | ||
""" | ||
self.spec_dict = json.loads(json_spec) | ||
|
||
def read(self): | ||
roots = [] | ||
for graph_spec in self.spec_dict["graph"]: | ||
# turn frames into nodes | ||
for nid, value in graph_spec.items(): | ||
graph_spec[nid]["data"] = Node(Frame(value["data"]), hnid=int(nid)) | ||
|
||
# connect nodes | ||
for nid, value in graph_spec.items(): | ||
for child in value["children"]: | ||
child = str(child) | ||
value["data"].add_child(graph_spec[child]["data"]) | ||
graph_spec[child]["data"].add_parent(value["data"]) | ||
|
||
for nid, value in graph_spec.items(): | ||
if len(value["data"].parents) == 0: | ||
roots.append(value["data"]) | ||
|
||
grph = Graph(roots) | ||
|
||
# make the dataframes | ||
dataframe = pd.DataFrame(self.spec_dict["dataframe"]) | ||
for graph_spec in self.spec_dict["graph"]: | ||
dataframe["node"] = dataframe["node"].map( | ||
lambda n: graph_spec[str(n)]["data"] if (str(n) in graph_spec) else n | ||
) | ||
dataframe.set_index(self.spec_dict["dataframe_indices"], inplace=True) | ||
|
||
return hatchet.graphframe.GraphFrame( | ||
grph, | ||
dataframe, | ||
self.spec_dict["exclusive_metrics"], | ||
self.spec_dict["inclusive_metrics"], | ||
) |
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 @@ | ||
{"graph":[{"0":{"data":{"name":"foo","type":"function"},"children":[1,4,12]},"1":{"data":{"name":"bar","type":"None"},"children":[2,3]},"2":{"data":{"name":"baz","type":"function"},"children":[]},"3":{"data":{"name":"grault","type":"None"},"children":[]},"4":{"data":{"name":"qux","type":"function"},"children":[5]},"5":{"data":{"name":"quux","type":"None"},"children":[6]},"6":{"data":{"name":"corge","type":"function"},"children":[7,11,10]},"7":{"data":{"name":"bar","type":"None"},"children":[8,9]},"8":{"data":{"name":"baz","type":"function"},"children":[]},"9":{"data":{"name":"grault","type":"None"},"children":[]},"10":{"data":{"name":"garply","type":"function"},"children":[]},"11":{"data":{"name":"grault","type":"None"},"children":[]},"12":{"data":{"name":"waldo","type":"function"},"children":[13,19]},"13":{"data":{"name":"fred","type":"function"},"children":[14,15]},"14":{"data":{"name":"plugh","type":"function"},"children":[]},"15":{"data":{"name":"xyzzy","type":"function"},"children":[16]},"16":{"data":{"name":"thud","type":"function"},"children":[17,18]},"17":{"data":{"name":"baz","type":"function"},"children":[]},"18":{"data":{"name":"garply","type":"function"},"children":[]},"19":{"data":{"name":"garply","type":"function"},"children":[]}},{"20":{"data":{"name":"waldo","type":"function"},"children":[21]},"21":{"data":{"name":"bar","type":"None"},"children":[22,23]},"22":{"data":{"name":"baz","type":"function"},"children":[]},"23":{"data":{"name":"grault","type":"None"},"children":[]}}],"dataframe_indices":["node"],"dataframe":[{"node":0,"name":"foo","time (inc)":135,"time":0},{"node":1,"name":"bar","time (inc)":20,"time":5},{"node":2,"name":"baz","time (inc)":5,"time":5},{"node":3,"name":"grault","time (inc)":10,"time":10},{"node":4,"name":"qux","time (inc)":60,"time":0},{"node":5,"name":"quux","time (inc)":60,"time":5},{"node":6,"name":"corge","time (inc)":55,"time":10},{"node":7,"name":"bar","time (inc)":20,"time":5},{"node":8,"name":"baz","time (inc)":5,"time":5},{"node":9,"name":"grault","time (inc)":10,"time":10},{"node":10,"name":"garply","time (inc)":15,"time":15},{"node":11,"name":"grault","time (inc)":10,"time":10},{"node":12,"name":"waldo","time (inc)":55,"time":0},{"node":13,"name":"fred","time (inc)":40,"time":5},{"node":14,"name":"plugh","time (inc)":5,"time":5},{"node":15,"name":"xyzzy","time (inc)":30,"time":5},{"node":16,"name":"thud","time (inc)":25,"time":5},{"node":17,"name":"baz","time (inc)":5,"time":5},{"node":18,"name":"garply","time (inc)":15,"time":15},{"node":19,"name":"garply","time (inc)":15,"time":15},{"node":20,"name":"waldo","time (inc)":30,"time":10},{"node":21,"name":"bar","time (inc)":20,"time":5},{"node":22,"name":"baz","time (inc)":5,"time":5},{"node":23,"name":"grault","time (inc)":10,"time":10}],"inclusive_metrics":["time (inc)"],"exclusive_metrics":["time"]} |
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,29 @@ | ||
# Copyright 2017-2022 Lawrence Livermore National Security, LLC and other | ||
# Hatchet Project Developers. See the top-level LICENSE file for details. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from hatchet import GraphFrame | ||
|
||
|
||
def test_read_json(json_graphframe_specification): | ||
jgs = "" | ||
with open(json_graphframe_specification, "r") as f: | ||
jgs = f.read() | ||
gf = GraphFrame.from_json(jgs) | ||
|
||
assert len(gf.dataframe) == 24 | ||
assert len(gf.graph) == 24 | ||
assert gf.graph.roots[0].frame["name"] == "foo" | ||
|
||
|
||
def test_write_json(json_graphframe_specification): | ||
jgs = "" | ||
with open(json_graphframe_specification, "r") as f: | ||
jgs = f.read() | ||
gf = GraphFrame.from_json(jgs) | ||
json_out = gf.to_json() | ||
|
||
assert "".join(sorted("".join(sorted(jgs.split())))) == "".join( | ||
sorted("".join(json_out.split())) | ||
) |