Coverage for maze_dataset\plotting\plot_dataset.py: 14%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-23 12:49 -0700

1"""`plot_dataset_mazes` will plot several mazes using `as_pixels` 

2 

3`print_dataset_mazes` will use `as_ascii` to print several mazes 

4""" 

5 

6import matplotlib.pyplot as plt # type: ignore[import] 

7 

8from maze_dataset.dataset.maze_dataset import MazeDataset 

9 

10 

11def plot_dataset_mazes( 

12 ds: MazeDataset, 

13 count: int | None = None, 

14 figsize_mult: tuple[float, float] = (1.0, 2.0), 

15 title: bool | str = True, 

16) -> tuple: 

17 count = count or len(ds) 

18 if count == 0: 

19 print("No mazes to plot for dataset") 

20 return 

21 fig, axes = plt.subplots( 

22 1, count, figsize=(count * figsize_mult[0], figsize_mult[1]) 

23 ) 

24 if count == 1: 

25 axes = [axes] 

26 for i in range(count): 

27 axes[i].imshow(ds[i].as_pixels()) 

28 # remove ticks 

29 axes[i].set_xticks([]) 

30 axes[i].set_yticks([]) 

31 

32 # set title 

33 if title: 

34 if isinstance(title, str): 

35 fig.suptitle(title) 

36 else: 

37 kwargs: dict = { 

38 "grid_n": ds.cfg.grid_n, 

39 # "n_mazes": ds.cfg.n_mazes, 

40 **ds.cfg.maze_ctor_kwargs, 

41 } 

42 fig.suptitle( 

43 f"{ds.cfg.to_fname()}\n{ds.cfg.maze_ctor.__name__}({', '.join(f'{k}={v}' for k, v in kwargs.items())})" 

44 ) 

45 

46 # tight layout 

47 fig.tight_layout() 

48 # remove whitespace between title and subplots 

49 fig.subplots_adjust(top=1.0) 

50 

51 return fig, axes 

52 

53 

54def print_dataset_mazes(ds: MazeDataset, count: int | None = None): 

55 count = count or len(ds) 

56 if count == 0: 

57 print("No mazes to print for dataset") 

58 return 

59 for i in range(count): 

60 print(ds[i].as_ascii(), "\n\n-----\n")