Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sim Library Improvements #116

Merged
merged 6 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions sim/h5_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@
import h5py
import matplotlib.pyplot as plt
import numpy as np
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the logic to the sim/produce_sim_data to use it as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 6a65d01

from datetime import datetime


class HDF5Logger:
def __init__(self, data_name: str, num_actions: int, max_timesteps: int, num_observations: int):
def __init__(self, data_name: str, num_actions: int, max_timesteps: int, num_observations: int, h5_out_dir: str = "sim/resources/"):
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())
h5_file = h5py.File(f"{self.data_name}/{idd}.h5", "w")
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")

# 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 Down
11 changes: 10 additions & 1 deletion sim/sim2sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ 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 @@ -178,7 +179,13 @@ def run_mujoco(

if log_h5:
stop_state_log = int(cfg.sim_duration / cfg.dt) / cfg.decimation
logger = HDF5Logger(embodiment, model_info["num_actions"], stop_state_log, model_info["num_observations"])
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
)

# Initialize variables for tracking upright steps and average speed
upright_steps = 0
Expand Down Expand Up @@ -312,6 +319,7 @@ 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 @@ -363,4 +371,5 @@ def parse_modelmeta(
args.keyboard_use,
args.log_h5,
args.render,
args.h5_out_dir,
)
Loading