Skip to content

Commit

Permalink
🔊 Update verbosity option
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed May 12, 2024
1 parent 622b7ee commit d58a971
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
6 changes: 6 additions & 0 deletions craftground/craftground/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def make(
track_native_memory=False,
ld_preload=None,
native_debug: bool = False,
verbose_python=False,
verbose_jvm=False,
verbose_gradle=False,
**kwargs,
) -> CraftGroundEnvironment:
env = InitialEnvironment(**kwargs)
Expand All @@ -30,4 +33,7 @@ def make(
track_native_memory=track_native_memory,
ld_preload=ld_preload,
native_debug=native_debug,
verbose_python=verbose_python,
verbose_jvm=verbose_jvm,
verbose_gradle=verbose_gradle,
)
44 changes: 31 additions & 13 deletions craftground/craftground/craftground.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
send_action_and_commands,
send_exit,
)
from .print_with_time import print_with_time, enable_print_with_time
from .print_with_time import print_with_time
from .proto import observation_space_pb2, initial_environment_pb2
from .screen_encoding_modes import ScreenEncodingMode

Expand All @@ -45,6 +45,10 @@ def __init__(
track_native_memory: bool = False,
ld_preload: Optional[str] = None,
native_debug: bool = False,
verbose_python: bool = False,
verbose_gradle: bool = False,
verbose_jvm: bool = False,
profile: bool = False,
):
self.action_space = ActionSpace(6)
entity_info_space = gym.spaces.Dict(
Expand Down Expand Up @@ -265,8 +269,11 @@ def __init__(
self.last_action = None
self.render_action = render_action
self.verbose = verbose
if verbose:
enable_print_with_time()
self.verbose_python = verbose_python
self.verbose_gradle = verbose_gradle
self.verbose_jvm = verbose_jvm
self.profile = profile

self.render_alternating_eyes = render_alternating_eyes
self.render_alternating_eyes_counter = 0
self.port = port
Expand All @@ -280,7 +287,7 @@ def __init__(
else:
self.env_path = env_path
self.csv_logger = CsvLogger(
"py_log.csv", enabled=verbose and False, profile=False
"py_log.csv", enabled=verbose and False, profile=profile
)

def reset(
Expand Down Expand Up @@ -317,13 +324,16 @@ def reset(
send_fastreset2(self.sock, extra_commands)
self.csv_logger.profile_end("fast_reset")
self.csv_logger.log("Sent fast reset")
print_with_time("Sent fast reset")
print_with_time("Reading response...")
if self.verbose_python:
print_with_time("Sent fast reset")
if self.verbose_python:
print_with_time("Reading response...")
self.csv_logger.log("Reading response...")
self.csv_logger.profile_start("read_response")
siz, res = self.read_one_observation()
self.csv_logger.profile_end("read_response")
print_with_time(f"Got response with size {siz}")
if self.verbose_python:
print_with_time(f"Got response with size {siz}")
self.csv_logger.log(f"Got response with size {siz}")
self.csv_logger.profile_start("convert_observation")
rgb_1, img_1, frame_1 = self.convert_observation(res.image)
Expand Down Expand Up @@ -405,7 +415,7 @@ def start_server(
)
my_env = os.environ.copy()
my_env["PORT"] = str(port)
my_env["VERBOSE"] = str(int(self.verbose))
my_env["VERBOSE"] = str(int(self.verbose_jvm))
if track_native_memory:
my_env["CRAFTGROUND_JVM_NATIVE_TRACKING"] = "detail"
if self.native_debug:
Expand All @@ -420,15 +430,16 @@ def start_server(
cmd,
cwd=self.env_path,
shell=True,
stdout=subprocess.DEVNULL if not self.verbose else None,
stdout=subprocess.DEVNULL if not self.verbose_gradle else None,
env=my_env,
)
sock: socket.socket = wait_for_server(port)
self.sock = sock
self.send_initial_env()
self.buffered_socket = BufferedSocket(self.sock)
# self.json_socket.send_json_as_base64(self.initial_env.to_dict())
print_with_time(f"Sent initial environment")
if self.verbose_python:
print_with_time(f"Sent initial environment")
self.csv_logger.log(f"Sent initial environment")

def read_one_observation(self) -> (int, ObsType):
Expand Down Expand Up @@ -506,16 +517,23 @@ def step(self, action: ActType) -> Tuple[ObsType, float, bool, bool, dict]:
# send the action
self.last_action = action
self.csv_logger.profile_start("send_action_and_commands")
send_action_and_commands(self.sock, action, commands=self.queued_commands)
send_action_and_commands(
self.sock,
action,
commands=self.queued_commands,
verbose=self.verbose_python,
)
self.csv_logger.profile_end("send_action_and_commands")
self.queued_commands.clear()
# read the response
print_with_time("Sent action and reading response...")
if self.verbose_python:
print_with_time("Sent action and reading response...")
self.csv_logger.log("Sent action and reading response...")
self.csv_logger.profile_start("read_response")
siz, res = self.read_one_observation()
self.csv_logger.profile_end("read_response")
print_with_time("Read observation...")
if self.verbose_python:
print_with_time("Read observation...")
self.csv_logger.log("Read observation...")
self.csv_logger.profile_start("convert_observation")
rgb_1, img_1, frame_1 = self.convert_observation(res.image)
Expand Down
11 changes: 8 additions & 3 deletions craftground/craftground/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ def send_commands(sock: socket.socket, commands: List[str]):


def send_action_and_commands(
sock: socket.socket, action_array: List[int], commands: List[str]
sock: socket.socket,
action_array: List[int],
commands: List[str],
verbose: bool = False,
):
print_with_time("Sending action and commands")
if verbose:
print_with_time("Sending action and commands")
action_space = action_space_pb2.ActionSpaceMessage()
action_space.action.extend(action_array)
action_space.commands.extend(commands)
v = action_space.SerializeToString()
sock.send(struct.pack("<I", len(v)))
sock.sendall(v)
print_with_time("Sent actions and commands")
if verbose:
print_with_time("Sent actions and commands")


def send_fastreset2(sock: socket.socket, extra_commands: List[str] = None):
Expand Down
9 changes: 0 additions & 9 deletions craftground/craftground/print_with_time.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
from datetime import datetime

do_print_with_time = False


def enable_print_with_time():
global do_print_with_time
do_print_with_time = True


def print_with_time(*args, **kwargs):
if not do_print_with_time:
return
time_str = datetime.now().strftime("%H:%M:%S.%f")
print(f"[{time_str}]", *args, **kwargs)

0 comments on commit d58a971

Please sign in to comment.