Skip to content

Commit

Permalink
Revert "add h5 ouput path as cmd arg w sensible defaults, and add tim…
Browse files Browse the repository at this point in the history
…estamp to the h5 output filename"

This reverts commit 2c48d0b.
  • Loading branch information
alik-git committed Nov 20, 2024
1 parent 2c48d0b commit f3a93f4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
14 changes: 3 additions & 11 deletions sim/h5_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@
import h5py
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime


class HDF5Logger:
def __init__(self, data_name: str, num_actions: int, max_timesteps: int, num_observations: int, h5_out_dir: str = "sim/resources/"):
def __init__(self, data_name: str, num_actions: int, max_timesteps: int, num_observations: int):
self.data_name = data_name
self.num_actions = num_actions
self.max_timesteps = max_timesteps
self.num_observations = num_observations
self.max_threshold = 1e3 # Adjust this threshold as needed
self.h5_out_dir = h5_out_dir
self.h5_file, self.h5_dict = self._create_h5_file()
self.current_timestep = 0

def _create_h5_file(self):
# Create a unique file ID
idd = str(uuid.uuid4())
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

curr_h5_out_dir = f"{self.h5_out_dir}/{self.data_name}/h5_out/"
os.makedirs(curr_h5_out_dir, exist_ok=True)

h5_file_path = f"{curr_h5_out_dir}/{timestamp}__{idd}.h5"
print(f"Saving HDF5 data to {h5_file_path}")
h5_file = h5py.File(h5_file_path, "w")
h5_file = h5py.File(f"{self.data_name}/{idd}.h5", "w")

# Create datasets for logging actions and observations
dset_actions = h5_file.create_dataset("prev_actions", (self.max_timesteps, self.num_actions), dtype=np.float32)
Expand All @@ -56,6 +47,7 @@ def _create_h5_file(self):
"buffer": dset_buffer,
}
return h5_file, h5_dict

def log_data(self, data: Dict[str, np.ndarray]):
if self.current_timestep >= self.max_timesteps:
print(f"Warning: Exceeded maximum timesteps ({self.max_timesteps})")
Expand Down
11 changes: 1 addition & 10 deletions sim/sim2sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def run_mujoco(
keyboard_use: bool = False,
log_h5: bool = False,
render: bool = True,
h5_out_dir: str = "sim/resources",
) -> None:
"""
Run the Mujoco simulation using the provided policy and configuration.
Expand Down Expand Up @@ -179,13 +178,7 @@ def run_mujoco(

if log_h5:
stop_state_log = int(cfg.sim_duration / cfg.dt) / cfg.decimation
logger = HDF5Logger(
data_name=embodiment,
num_actions=model_info["num_actions"],
max_timesteps=stop_state_log,
num_observations=model_info["num_observations"],
h5_out_dir=h5_out_dir
)
logger = HDF5Logger(embodiment, model_info["num_actions"], stop_state_log, model_info["num_observations"])

# Initialize variables for tracking upright steps and average speed
upright_steps = 0
Expand Down Expand Up @@ -319,7 +312,6 @@ def parse_modelmeta(
parser.add_argument("--load_model", type=str, required=True, help="Path to run to load from.")
parser.add_argument("--keyboard_use", action="store_true", help="keyboard_use")
parser.add_argument("--log_h5", action="store_true", help="log_h5")
parser.add_argument("--h5_out_dir", type=str, default="sim/resources", help="Directory to save HDF5 files")
parser.add_argument("--no_render", action="store_false", dest="render", help="Disable rendering")
parser.set_defaults(render=True)
args = parser.parse_args()
Expand Down Expand Up @@ -371,5 +363,4 @@ def parse_modelmeta(
args.keyboard_use,
args.log_h5,
args.render,
args.h5_out_dir,
)

0 comments on commit f3a93f4

Please sign in to comment.