Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Tty rendering for NLE environment #147

Merged
merged 9 commits into from
Feb 19, 2021
Merged

Tty rendering for NLE environment #147

merged 9 commits into from
Feb 19, 2021

Conversation

rockt
Copy link
Contributor

@rockt rockt commented Feb 12, 2021

No description provided.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 12, 2021
@rockt rockt requested a review from cdmatters February 12, 2021 15:06
Copy link
Contributor

@cdmatters cdmatters left a comment

Choose a reason for hiding this comment

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

Good and clear, some general comments on string addition, and api. Perhaps we could make terminal render mode a sepate 'mode' instead of simply deleting the old code, for users? I will see if we can get the ansi mapping from nethack object etc...

nle/env/base.py Outdated
line.append(COLORS[color] + chr(char))
row_index_str = COLORS[1] + str(i % 10)
if print_guides:
rendering += row_index_str + "".join(line) + row_index_str + "\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

in general this string additions can get very slow because in Python string are immutable, and + is a binary operator.
so "some long" + "strings" + "can be" + "expensive" + "to make"

is evaluated as:

("some long" + "strings")  # all copied to "some long strings"
("some long string"+ "can be")  # copied to "some long strings can be"
("some long strings can be"  + "expensive")  ... # etc O(N^2)

Thats why the "Pythonic" thing is to collect the strings in a list and use .join method as you have above, which means the allocation is only done once (O(N))

Copy link
Contributor

Choose a reason for hiding this comment

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

TBF you have done the line.join for for the rows, so N in this case is only like 24 rather than 80, but its still an order of magnitude.

nle/env/base.py Outdated
print("")
return
obs = self.last_observation
return self.tty_render(obs, print_guides=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

can I suggest either

Suggested change
return self.tty_render(obs, print_guides=False)
self.tty_render(obs, print_guides=False)
return

or

Suggested change
return self.tty_render(obs, print_guides=False)
image = self.tty_render(obs, print_guides=False)
print(image)
return

The current API suggests render doesnt return anything, and so by writing it as either of these examples, we can clearly see that the printing must happen in the tty_render function, and not elsewhere in the code (ie perhaps with the returned value).

in fact I would be tempted to go for the second example, remove the print function from the tty_render method, and rename it as "tty_to_string" or "get_tty_string". That way it can be programatically reused, possibly for logging by downstream users.

@@ -131,6 +131,27 @@
)


# TODO: can probably be imported from somewhere?
COLORS = [
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should work with all ansi compatible terminals, but Im not entirely sure. It could probably be clearer which colour is which as well... But also this might not be a priority.

@rockt
Copy link
Contributor Author

rockt commented Feb 18, 2021

Thanks @condnsdmatters — PTAL

Copy link
Contributor

@cdmatters cdmatters left a comment

Choose a reason for hiding this comment

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

Some nit pick suggestions, but looks good.

rockt and others added 4 commits February 19, 2021 11:49
Co-authored-by: Eric Hammy <6815729+condnsdmatters@users.noreply.github.com>
Co-authored-by: Eric Hammy <6815729+condnsdmatters@users.noreply.github.com>
Co-authored-by: Eric Hammy <6815729+condnsdmatters@users.noreply.github.com>
Co-authored-by: Eric Hammy <6815729+condnsdmatters@users.noreply.github.com>
@rockt rockt merged commit cffbf5d into master Feb 19, 2021
@heiner
Copy link
Contributor

heiner commented Jun 4, 2021

Hey, would be interested in the history of this change. I think we should probably merge the "rendering" modes here, but I would first like to understand why we added this mode here?

@heiner heiner deleted the rockt/dev branch July 7, 2021 10:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants