Skip to content

Commit

Permalink
temporarily storing hif to from
Browse files Browse the repository at this point in the history
  • Loading branch information
brendapraggastis committed Nov 7, 2024
1 parent 84f01cb commit 54aa7a8
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions hypernetx/read_write.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
# Copyright © 2018 Battelle Memorial Institute
# All rights reserved.

import pickle
import json
import fastjsonschema
import requests
from .exception import HyperNetXError

schema_url = "https://raw.githubusercontent.com/pszufe/HIF_validators/main/schemas/hif_schema_v0.1.0.json"
resp = requests.get(url)
schema = json.loads(resp.text)
validator = fastjsonschema.compile(schema)

def to_pickle(obj, filename):
"""Writes object to a pickle file"""
with open(f"{filename}", "wb") as f:
pickle.dump(obj, f)

def from_hif(hif):
assert validator(hif)
incidences = pd.DataFrame(hif["incidences"])
misc_cell_properties_col = 'attrs' if 'attrs' in incidences.columns else None

def load_from_pickle(filepath):
"""Returns object from file"""
with open(filepath, "rb") as f:
temp = pickle.load(f)
return temp
if "edges" in hif and len(hif["edges"])>0:
edges = pd.DataFrame(hif['edges']).rename(columns={'edge':'id'})
misc_edge_properties_col = 'attrs' if 'attrs' in edges.columns else None
else:
edges = None
misc_edge_properties_col = None

if "nodes" in hif and len(hif["nodes"])>0:
nodes = pd.DataFrame(hif['nodes']).rename(columns={'node':'id'})
misc_node_properties_col = 'attrs' if 'attrs' in nodes.columns else None
else:
nodes = None
misc_node_properties_col = None

h = hnx.Hypergraph(incidences, misc_cell_properties_col=misc_cell_properties_col,
node_properties=nodes, misc_node_properties_col=misc_node_properties_col,
edge_properties=edges, misc_edge_properties_col=misc_edge_properties_col,
)
## store additional data for reuse later
h.hif_data = {k:hif[k] for k in set(hif.keys()).difference(['edges','nodes','incidences'])}
return h


def to_hif(hg):
edgj = hg.edges.to_dataframe.rename(columns={"misc_properties":"attrs"})
edid = edgj.index._name or "index"
nodj = hg.nodes.to_dataframe.rename(columns={"misc_properties":"attrs"})
ndid = nodj.index._name or "index"
edgj = edgj.reset_index().rename(columns={edid: "edge"}).to_dict(orient="records")
nodj = nodj.reset_index().rename(columns={ndid: "node"}).to_dict(orient="records")
incj = (
hg.incidences.to_dataframe.reset_index()
.rename(columns={"nodes": "node", "edges": "edge","misc_properties":"attrs"})
.to_dict(orient="records")
)
hif = {"edges": edgj, "nodes": nodj, "incidences": incj}
hif.update(getattr(hg,'hif_data',{}))
assert validator(hif)
return hif

0 comments on commit 54aa7a8

Please sign in to comment.