lczerolens.board#

Board class.

class lczerolens.board.InputEncoding(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: int, Enum

Input encoding for the board tensor.

class lczerolens.board.LczeroBoard(fen: str | None = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', *, chess960: bool = False)#

Bases: Board

A class for wrapping the LczeroBoard class.

decode_move(index: int) Move#

Converts an index to a chess.Move object.

Parameters:

index (int) – The index to convert.

Returns:

The chess move.

Return type:

chess.Move

static encode_move(move: Move, us: bool) int#

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

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]

static get_piece_index(piece: str, us: bool, plane_order: str | None = None)#

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

static get_plane_order(us: bool)#

Get the plane order for the given us view.

Parameters:

us (bool) – The us_them tuple.

Returns:

The plane order.

Return type:

str

render_heatmap(heatmap: Tensor | ndarray, square: str | None = None, vmin: float | None = None, vmax: float | None = None, arrows: List[Tuple[str, str]] | None = None, normalise: str = 'none', save_to: str | None = None, cmap_name: str = 'RdYlBu_r', alpha: float = 1.0, flip_mode: str = 'board') Tuple[str | None, Any]#

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.

  • flip_mode (str, default="board") – Flip mode for black’s perspective. Use “board” to flip the board, “heatmap” to flip the heatmap.

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.

to_config_tensor(us: bool | None = None, input_encoding: InputEncoding = InputEncoding.INPUT_CLASSICAL_112_PLANE)#

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

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

  • input_encoding (InputEncoding) – The input encoding method.

Returns:

The 13x8x8 tensor.

Return type:

torch.Tensor

to_input_tensor(with_history: bool = True, input_encoding: InputEncoding = InputEncoding.INPUT_CLASSICAL_112_PLANE)#

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

Parameters:
  • with_history (bool) – Whether to include the history of the game.

  • input_encoding (InputEncoding) – The input encoding method.

Returns:

The 112x8x8 tensor.

Return type:

torch.Tensor