Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/openfecli/commands/gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import click
import pandas as pd
from gufe.utils import magic_open

from openfecli import OFECommandPlugin
from openfecli.clicktypes import HyphenAwareChoice
Expand Down Expand Up @@ -178,7 +179,10 @@ def format_df_with_precision(

def is_results_json(fpath: os.PathLike | str) -> bool:
"""Sanity check that file is a result json before we try to deserialize"""
return "estimate" in open(fpath, "r").read(20)

with open(fpath, "rb") as raw:
with magic_open(raw) as f:
return "estimate" in f.read(20)


def load_json(fpath: os.PathLike | str) -> dict:
Expand All @@ -201,7 +205,9 @@ def load_json(fpath: os.PathLike | str) -> dict:

from gufe.tokenization import JSON_HANDLER

return json.load(open(fpath, "r"), cls=JSON_HANDLER.decoder)
with open(fpath, "rb") as raw:
with magic_open(raw) as f:
return json.load(f, cls=JSON_HANDLER.decoder)


def _get_names(result: dict) -> tuple[str, str]:
Expand Down Expand Up @@ -609,17 +615,13 @@ def _collect_result_jsons(results: List[os.PathLike | str]) -> List[pathlib.Path
def collect_jsons(results: List[os.PathLike]):
all_jsons = []
for p in results:
if str(p).endswith("json"):
if ".json" in str(p):
all_jsons.append(p)
elif p.is_dir():
all_jsons.extend(glob.glob(f"{p}/**/*json", recursive=True))
all_jsons.extend(glob.glob(f"{p}/**/*.json*", recursive=True))

return all_jsons

def is_results_json(fpath: os.PathLike | str) -> bool:
"""Sanity check that file is a result json before we try to deserialize"""
return "estimate" in open(fpath, "r").read(20)

results = sorted(results) # ensures reproducible output order regardless of input order

# 1) find all possible jsons
Expand Down Expand Up @@ -774,7 +776,7 @@ def gather(
RESULTS is the path(s) to JSON files or directories of JSON files containing RBFE protocol results as generated by ``openfe quickrun``.

All directories will be walked recursively and any valid JSON results files will be gathered.
Files must end in .json to be collected, and invalid files will be ignored.
Files must contain `".json"` to be collected, and invalid files will be ignored.

The results reported depends on ``--report`` flag:

Expand Down
7 changes: 0 additions & 7 deletions src/openfecli/tests/commands/test_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,6 @@ def septop_result_dir(tmp_path_factory) -> pathlib.Path:
ZENODO_SEPTOP_DATA.fetch("septop_results.zip", processor=pooch.Unzip())
result_dir = pathlib.Path(POOCH_CACHE) / "septop_results.zip.unzip/septop_results/"

for gz_file in result_dir.rglob("*.json.gz"):
json_file = gz_file.with_suffix("") # removes .gz, leaving .json
with gzip.open(gz_file, "rb") as f_in:
with open(json_file, "wb") as f_out:
f_out.write(f_in.read())
gz_file.unlink() # remove the .gz file

return result_dir


Expand Down
Loading