Skip to content

Commit

Permalink
added functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed May 16, 2024
1 parent 10a76b1 commit 12965c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
20 changes: 10 additions & 10 deletions xgi/readwrite/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,23 @@ def write_json(H, path):
output_file.write(datastring)


def write_json_collection(collection, dir):
def write_json_collection(collection, dir, collection_name=None):
collection_data = defaultdict(dict)
if collection_name is not None:
collection_name += "_"

for i, H in enumerate(collection):
path = f"{dir}/{i}.json"
path = f"{dir}/{collection_name}{i}.json"
collection_data["path"][i] = path
write_json(H, path)

collection_data["type"] = "collection"

datastring = json.dumps(collection_data, indent=2)

with open(f"{dir}/collection.json", "w") as output_file:
with open(
f"{dir}/{collection_name}collection_information.json", "w"
) as output_file:
output_file.write(datastring)


Expand Down Expand Up @@ -120,16 +125,11 @@ def read_json(path, nodetype=None, edgetype=None):

if data["type"] == "collection":
collection = {}
paths = data["path"]
for i, path in paths.items():
for name, path in data["path"].items():
with open(path) as file:
data = json.loads(file.read())
H = dict_to_hypergraph(data, nodetype=nodetype, edgetype=edgetype)

try:
collection[H["name"]] = H
except XGIError:
collection[i] = H
collection[name] = H
return collection

return dict_to_hypergraph(data, nodetype=nodetype, edgetype=edgetype)
30 changes: 24 additions & 6 deletions xgi/readwrite/xgi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from warnings import warn

from .. import convert
from ..convert import dict_to_hypergraph
from ..exception import XGIError
from ..utils import request_json_from_url, request_json_from_url_cached

Expand Down Expand Up @@ -66,7 +66,7 @@ def load_xgi_data(
if os.path.exists(cfp):
data = json.load(open(cfp, "r"))

return convert.dict_to_hypergraph(
return dict_to_hypergraph(
data, nodetype=nodetype, edgetype=edgetype, max_order=max_order
)
else:
Expand All @@ -77,7 +77,17 @@ def load_xgi_data(
)
data = _request_from_xgi_data(index_url, dataset, cache=cache)

return convert.dict_to_hypergraph(
if data["type"] == "collection":
collection = {}
for name, path in data["path"].items():
with open(path) as file:
data = json.loads(file.read())

H = dict_to_hypergraph(data, nodetype=nodetype, edgetype=edgetype)
collection[name] = H
return collection

return dict_to_hypergraph(
data, nodetype=nodetype, edgetype=edgetype, max_order=max_order
)

Expand All @@ -96,10 +106,18 @@ def download_xgi_data(dataset, path=""):
file to local directory.
"""
index_url = "https://raw.githubusercontent.com/xgi-org/xgi-data/main/index.json"

jsondata = _request_from_xgi_data(index_url, dataset)
jsonfile = open(os.path.join(path, dataset + ".json"), "w")
json.dump(jsondata, jsonfile)
jsonfile.close()
if jsondata["type"] == "collection":
jsonfile = open(os.path.join(path, f"{dataset}_collection_data.json"), "w")
json.dump(jsondata, jsonfile, indent=2)
jsonfile.close()

for name, url in jsondata["path"].items():
jsondata = request_json_from_url(url)
jsonfile = open(os.path.join(path, f"{dataset}_{name}.json"), "w")
json.dump(jsondata, jsonfile, indent=2)
jsonfile.close()


def _request_from_xgi_data(index_url, dataset=None, cache=True):
Expand Down

0 comments on commit 12965c0

Please sign in to comment.