-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving logging to different file without habitat dependencies, adding…
… episode logging support for gibson
- Loading branch information
1 parent
6bdb7b5
commit 82ac534
Showing
3 changed files
with
79 additions
and
29 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
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,44 @@ | ||
import json | ||
import os | ||
import time | ||
from typing import Dict, Union | ||
|
||
|
||
def log_episode(episode_id: Union[str, int], scene_id: str, data: Dict) -> None: | ||
log_dir = os.environ["ZSOS_LOG_DIR"] | ||
try: | ||
os.makedirs(log_dir, exist_ok=True) | ||
except Exception: | ||
pass | ||
base = f"{episode_id}_{scene_id}.json" | ||
filename = os.path.join(log_dir, base) | ||
|
||
# Skip if the filename already exists AND it isn't empty | ||
if not (os.path.exists(filename) and os.path.getsize(filename) > 0): | ||
print(f"Logging episode {int(episode_id):04d} to {filename}") | ||
with open(filename, "w") as f: | ||
json.dump( | ||
{"episode_id": episode_id, "scene_id": scene_id, **data}, f, indent=4 | ||
) | ||
|
||
|
||
def is_evaluated(episode_id: Union[str, int], scene_id: str) -> bool: | ||
log_dir = os.environ["ZSOS_LOG_DIR"] | ||
base = f"{episode_id}_{scene_id}.json" | ||
filename = os.path.join(log_dir, base) | ||
|
||
# Return false if the directory doesn't exist | ||
if not os.path.exists(log_dir): | ||
return False | ||
|
||
# Delete any empty files that are older than 5 minutes | ||
for f in os.listdir(log_dir): | ||
try: | ||
if os.path.getsize(os.path.join(log_dir, f)) == 0 and ( | ||
time.time() - os.path.getmtime(os.path.join(log_dir, f)) > 300 | ||
): | ||
os.remove(os.path.join(log_dir, f)) | ||
except Exception: | ||
pass | ||
|
||
return os.path.exists(filename) |