renderer.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import annotations
  2. from abc import ABC, abstractmethod
  3. from typing import TYPE_CHECKING, Any
  4. import numpy as np
  5. if TYPE_CHECKING:
  6. import io
  7. from numpy.typing import ArrayLike
  8. from contourpy._contourpy import CoordinateArray, FillReturn, FillType, LineReturn, LineType
  9. class Renderer(ABC):
  10. """Abstract base class for renderers, defining the interface that they must implement."""
  11. def _grid_as_2d(self, x: ArrayLike, y: ArrayLike) -> tuple[CoordinateArray, CoordinateArray]:
  12. x = np.asarray(x)
  13. y = np.asarray(y)
  14. if x.ndim == 1:
  15. x, y = np.meshgrid(x, y)
  16. return x, y
  17. x = np.asarray(x)
  18. y = np.asarray(y)
  19. if x.ndim == 1:
  20. x, y = np.meshgrid(x, y)
  21. return x, y
  22. @abstractmethod
  23. def filled(
  24. self,
  25. filled: FillReturn,
  26. fill_type: FillType | str,
  27. ax: Any = 0,
  28. color: str = "C0",
  29. alpha: float = 0.7,
  30. ) -> None:
  31. pass
  32. @abstractmethod
  33. def grid(
  34. self,
  35. x: ArrayLike,
  36. y: ArrayLike,
  37. ax: Any = 0,
  38. color: str = "black",
  39. alpha: float = 0.1,
  40. point_color: str | None = None,
  41. quad_as_tri_alpha: float = 0,
  42. ) -> None:
  43. pass
  44. @abstractmethod
  45. def lines(
  46. self,
  47. lines: LineReturn,
  48. line_type: LineType | str,
  49. ax: Any = 0,
  50. color: str = "C0",
  51. alpha: float = 1.0,
  52. linewidth: float = 1,
  53. ) -> None:
  54. pass
  55. @abstractmethod
  56. def mask(
  57. self,
  58. x: ArrayLike,
  59. y: ArrayLike,
  60. z: ArrayLike | np.ma.MaskedArray[Any, Any],
  61. ax: Any = 0,
  62. color: str = "black",
  63. ) -> None:
  64. pass
  65. @abstractmethod
  66. def save(self, filename: str, transparent: bool = False) -> None:
  67. pass
  68. @abstractmethod
  69. def save_to_buffer(self) -> io.BytesIO:
  70. pass
  71. @abstractmethod
  72. def show(self) -> None:
  73. pass
  74. @abstractmethod
  75. def title(self, title: str, ax: Any = 0, color: str | None = None) -> None:
  76. pass
  77. @abstractmethod
  78. def z_values(
  79. self,
  80. x: ArrayLike,
  81. y: ArrayLike,
  82. z: ArrayLike,
  83. ax: Any = 0,
  84. color: str = "green",
  85. fmt: str = ".1f",
  86. quad_as_tri: bool = False,
  87. ) -> None:
  88. pass