lczerolens.board#

Board class.

Classes#

InputEncoding

Input encoding for the board tensor.

LczeroBoard

A class for wrapping the LczeroBoard class.

Module Contents#

class lczerolens.board.InputEncoding[source]#

Bases: int, enum.Enum

Input encoding for the board tensor.

INPUT_CLASSICAL_112_PLANE = 0[source]#
INPUT_CLASSICAL_112_PLANE_REPEATED = 1[source]#
INPUT_CLASSICAL_112_PLANE_NO_HISTORY_REPEATED = 2[source]#
INPUT_CLASSICAL_112_PLANE_NO_HISTORY_ZEROS = 3[source]#
class lczerolens.board.LczeroBoard(fen=STARTING_FEN, *, chess960=False)[source]#

Bases: chess.Board

A class for wrapping the LczeroBoard class.

Parameters:
  • fen (Optional[str])

  • chess960 (bool)

static get_plane_order(us)[source]#

Get the plane order for the given us view.

Parameters:

us (bool) – The us_them tuple.

Returns:

The plane order.

Return type:

str

static get_piece_index(piece, us, plane_order=None)[source]#

Converts a piece to its index in the plane order.

Parameters:
  • piece (str) – The piece to convert.

  • us (bool) – The us_them tuple.

  • plane_order (Optional[str]) – The plane order.

Returns:

The index of the piece in the plane order.

Return type:

int

to_config_tensor(us=None)[source]#

Converts a LczeroBoard to a tensor based on the pieces configuration.

Parameters:

us (Optional[bool]) – The us_them tuple.

Returns:

The 13x8x8 tensor.

Return type:

torch.Tensor

to_input_tensor(*, input_encoding=InputEncoding.INPUT_CLASSICAL_112_PLANE)[source]#

Create the lc0 input tensor from the history of a game.

Parameters:

input_encoding (InputEncoding) – The input encoding method.

Returns:

The 112x8x8 tensor.

Return type:

torch.Tensor

static encode_move(move, us)[source]#

Converts a chess.Move object to an index.

Parameters:
  • move (chess.Move) – The chess move to encode.

  • us (bool) – The side to move (True for white, False for black).

Returns:

The encoded move index.

Return type:

int

decode_move(index)[source]#

Converts an index to a chess.Move object.

Parameters:

index (int) – The index to convert.

Returns:

The chess move.

Return type:

chess.Move

Gets the legal indices.

Returns:

Tensor containing indices of legal moves.

Return type:

torch.Tensor

Gets the next legal boards.

Parameters:

n_history (int, optional) – Number of previous positions to keep in the move stack, by default 7.

Returns:

Generator yielding board positions after each legal move.

Return type:

Generator[LczeroBoard, None, None]

render_heatmap(heatmap, square=None, vmin=None, vmax=None, arrows=None, normalise='none', save_to=None, cmap_name='RdYlBu_r', alpha=1.0, relative_board_view=True, heatmap_mode='relative_flip')[source]#

Render a heatmap on the board.

Parameters:
  • heatmap (torch.Tensor or numpy.ndarray) – The heatmap values to visualize on the board (64,) or (8, 8).

  • square (Optional[str], default=None) – Chess square to highlight (e.g. ‘e4’).

  • vmin (Optional[float], default=None) – Minimum value for the colormap normalization.

  • vmax (Optional[float], default=None) – Maximum value for the colormap normalization.

  • arrows (Optional[List[Tuple[str, str]]], default=None) – List of arrow tuples (from_square, to_square) to draw on board.

  • normalise (str, default="none") – Normalization method. Use “abs” for absolute value normalization.

  • save_to (Optional[str], default=None) – Path to save the visualization. If None, returns the figure.

  • cmap_name (str, default="RdYlBu_r") – Name of matplotlib colormap to use.

  • alpha (float, default=1.0) – Opacity of the heatmap overlay.

  • relative_board_view (bool, default=True) – Whether to use the relative board view.

  • heatmap_mode (str, default="relative_flip") – Use “relative_flip” if the heatmap corresponds to a relative flip of the board, “relative_rotation” if it corresponds to a relative rotation of the board, or “absolute” if it is already in the correct orientation.

Returns:

If save_to is None, returns (SVG string, matplotlib figure). If save_to is provided, saves files and returns None.

Return type:

Union[Tuple[str, matplotlib.figure.Figure], None]

Raises:

ValueError – If save_to is provided and does not end with .svg.