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

[Hex] Modify visualizer.py to handle TypeError when displaying the initial state of hex. #1261

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

KazukiOhta
Copy link
Contributor

@KazukiOhta KazukiOhta commented Oct 23, 2024

Issue

We cannot visualize the initial state of hex.
It raises TypeError: 'int' object is not subscriptable.

Reason

This is caused by the following code in line 396 of visualizer.py.

self.config["BOARD_WIDTH"] = four_dig(
    _state._size[0] * 1.5  # type:ignore
)

The current implementation above tries to read _state._size, which is int object.
Also, python raises TypeError, therefore it would not be caught by the exception handler except IndexError:.

Proposal

I propose to convert _state._size into int via one dimensional jnp.array.
I think this is stabler because both int and jaxlib.xla_extension.ArrayImpl with one element can be converted to int.

Reproduction of the error

If we run the following code, we get TypeError: 'int' object is not subscriptable.

import jax
import pgx

print(pgx.__version__)   # -> 2.4.0
env = pgx.make("hex")
state = env.init(jax.random.key(42))
print(type(state._size)) # -> <class 'int'>
print(state._size)       # -> 11
display(state)

Or, this is the screenshot if you prefer.
screenshot 2024-10-23 16 29 28

Modify visualizer for hex environment.
@KazukiOhta KazukiOhta changed the title Update visualizer.py [Hex] Modify visualizer.py to handle TypeError when displaying the initial state of hex Oct 23, 2024
@KazukiOhta KazukiOhta changed the title [Hex] Modify visualizer.py to handle TypeError when displaying the initial state of hex [Hex] Modify visualizer.py to handle TypeError when displaying the initial state of hex. Oct 23, 2024
Via one dimensional array.
@sotetsuk
Copy link
Owner

Thank you again for your PR! 🙏
I reproduced the error and confirmed that this fix works perfect in my laptop 👍

w/o vmap

image

w/ vmap

image

@sotetsuk sotetsuk merged commit dc4cc17 into sotetsuk:main Oct 23, 2024
2 checks passed
@sotetsuk
Copy link
Owner

@KazukiOhta v2.4.1 is released. It should be available from PyPI 👍

@KazukiOhta
Copy link
Contributor Author

Thank you again for your quick response!

@KazukiOhta KazukiOhta deleted the patch-2 branch October 23, 2024 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants