fimama.configuration
Configuration classes and extensions to Pydantic BaseModel.
1""" 2Configuration classes and extensions to Pydantic BaseModel. 3""" 4 5import logging 6 7from pydantic import BaseModel, ConfigDict, model_validator 8 9_logger = logging.getLogger('patkit.base_model_extensions') 10 11 12class FimamaModel(BaseModel): 13 """ 14 The extended Pydantic BaseModel for fimama. 15 16 This BaseModel which accepts empty strings for any field as None. 17 18 This BaseModel which can be updated with new data. The update will trigger 19 validation again. 20 21 Additionally, trying to parse undefined fields will raise an exception. 22 """ 23 24 model_config = ConfigDict(extra="forbid") 25 26 @model_validator(mode="before") 27 @classmethod 28 def empty_str_to_none(cls, input_string: str) -> str | None: 29 """ 30 Validate empty strings to None, but non-empties to themselves. 31 32 Parameters 33 ---------- 34 input_string : str 35 String to be validated. 36 37 Returns 38 ------- 39 Optional[str] 40 None for empty string, otherwise the string itself. 41 """ 42 if input_string == '': 43 input_string = None 44 return input_string 45 46 def update(self, data: dict) -> 'FimamaModel': 47 """ 48 Update the BaseModel with the contents of data and validate. 49 50 The update does not happen in place but rather a new updated object is 51 returned and updating triggers validation. 52 53 Parameters 54 ---------- 55 data : dict 56 Only valid key, value pairs are accepted. 57 58 Returns 59 ------- 60 UpdatableBaseModel 61 The updated BaseModel. 62 """ 63 update = self.model_dump() 64 update.update(data) 65 new_dict = self.model_validate( 66 update).model_dump(exclude_defaults=True) 67 for key, value in new_dict.items(): 68 _logger.debug( 69 "Updating value of '%s' from '%s' to '%s'.", 70 str(key), str(getattr(self, key, None)), str(value)) 71 setattr(self, key, value) 72 return self 73 74 75class PerlinParameters(FimamaModel): 76 """ 77 Parameters for generating a heightmap with Perlin noise. 78 79 Apart from `scale` these are parameters of the `noise.pnoise2` function. 80 81 Parameters 82 ---------- 83 scale : float 84 Factor to zoom into the noise field by. Small values will produce a 85 very densely noisy map, by default 100.0. 86 octaves: int 87 The number of passes for generating fractional Brownian motion 88 (fBm) noise, by default 6. 89 persistence: float 90 The amplitude of each successive octave relative to the one below 91 it. Note the amplitude of the first pass is always 1.0. Defaults to 92 0.5 (each higher octave's amplitude is halved). 93 lacunarity: float 94 The frequency of each successive octave relative to the one below 95 it, similar to persistence. Defaults to 2.0. 96 base: int 97 Fixed offset for the input coordinates. Useful for generating 98 different noise textures with the same repeat interval, by default 99 42. 100 """ 101 scale: float = 100.0 102 octaves: int = 6 103 persistence: float = 0.5 104 lacunarity: float = 2.0 105 base: int = 42 106 107 108class VoronoiConfiguration(FimamaModel): 109 """ 110 Configuration for plotting elements of the Voronoi grid. 111 112 Note that these parameters do not affect drawing the Voronoi cell polygons. 113 Their plotting is controlled by configuration for drawing the heightmap. 114 115 Parameters 116 ---------- 117 plot_voronoi_grid: bool 118 Should any part of the grid be plotted, if set to False, none of 119 the elements get plotted regardless of the parameter values, by 120 default False. 121 show_ridges: bool 122 Should the perimeter of the polygons - the Voronoi ridges - be 123 plotted, by default True. 124 show_vertices: bool 125 Should the vertices, or intersections of the ridges, be plotted, by 126 default False. 127 show_points: bool 128 Should the points that the Voronoi grid is based on be plotted, by 129 default False. 130 """ 131 plot_voronoi_grid: bool = False 132 show_ridges: bool = True 133 show_vertices: bool = False 134 show_points: bool = False 135 136 137class MapConfiguration(FimamaModel): 138 """ 139 Map generation configuration. 140 141 Parameters 142 ---------- 143 height: int 144 Height of the map in cells, by default 125. 145 width: int 146 Width of the map in cells, by default 200. 147 generator: str 148 Which generator to use for the heightmap, by default "perlin". 149 colormap_name: str 150 Name of the colormap to use for displaying the heightmap, by 151 default "dark-atlas". 152 perlin_parameters: `PerlinParameters` | None 153 Parameters for generating a heightmap with Perlin noise, by default 154 None. 155 voronoi_configuration: `VoronoiConfiguration` | None 156 Parameters for plotting the Voronoi grid (not the voronoi polygons, 157 but rather associated edges and points), by default None. 158 """ 159 height: int = 125 160 width: int = 200 161 generator: str = "perlin" 162 colormap_name: str = "dark-atlas" 163 perlin_parameters: PerlinParameters | None = None 164 voronoi_configuration: VoronoiConfiguration | None = None
13class FimamaModel(BaseModel): 14 """ 15 The extended Pydantic BaseModel for fimama. 16 17 This BaseModel which accepts empty strings for any field as None. 18 19 This BaseModel which can be updated with new data. The update will trigger 20 validation again. 21 22 Additionally, trying to parse undefined fields will raise an exception. 23 """ 24 25 model_config = ConfigDict(extra="forbid") 26 27 @model_validator(mode="before") 28 @classmethod 29 def empty_str_to_none(cls, input_string: str) -> str | None: 30 """ 31 Validate empty strings to None, but non-empties to themselves. 32 33 Parameters 34 ---------- 35 input_string : str 36 String to be validated. 37 38 Returns 39 ------- 40 Optional[str] 41 None for empty string, otherwise the string itself. 42 """ 43 if input_string == '': 44 input_string = None 45 return input_string 46 47 def update(self, data: dict) -> 'FimamaModel': 48 """ 49 Update the BaseModel with the contents of data and validate. 50 51 The update does not happen in place but rather a new updated object is 52 returned and updating triggers validation. 53 54 Parameters 55 ---------- 56 data : dict 57 Only valid key, value pairs are accepted. 58 59 Returns 60 ------- 61 UpdatableBaseModel 62 The updated BaseModel. 63 """ 64 update = self.model_dump() 65 update.update(data) 66 new_dict = self.model_validate( 67 update).model_dump(exclude_defaults=True) 68 for key, value in new_dict.items(): 69 _logger.debug( 70 "Updating value of '%s' from '%s' to '%s'.", 71 str(key), str(getattr(self, key, None)), str(value)) 72 setattr(self, key, value) 73 return self
The extended Pydantic BaseModel for fimama.
This BaseModel which accepts empty strings for any field as None.
This BaseModel which can be updated with new data. The update will trigger validation again.
Additionally, trying to parse undefined fields will raise an exception.
27 @model_validator(mode="before") 28 @classmethod 29 def empty_str_to_none(cls, input_string: str) -> str | None: 30 """ 31 Validate empty strings to None, but non-empties to themselves. 32 33 Parameters 34 ---------- 35 input_string : str 36 String to be validated. 37 38 Returns 39 ------- 40 Optional[str] 41 None for empty string, otherwise the string itself. 42 """ 43 if input_string == '': 44 input_string = None 45 return input_string
Validate empty strings to None, but non-empties to themselves.
Parameters
- input_string (str): String to be validated.
Returns
- Optional[str]: None for empty string, otherwise the string itself.
47 def update(self, data: dict) -> 'FimamaModel': 48 """ 49 Update the BaseModel with the contents of data and validate. 50 51 The update does not happen in place but rather a new updated object is 52 returned and updating triggers validation. 53 54 Parameters 55 ---------- 56 data : dict 57 Only valid key, value pairs are accepted. 58 59 Returns 60 ------- 61 UpdatableBaseModel 62 The updated BaseModel. 63 """ 64 update = self.model_dump() 65 update.update(data) 66 new_dict = self.model_validate( 67 update).model_dump(exclude_defaults=True) 68 for key, value in new_dict.items(): 69 _logger.debug( 70 "Updating value of '%s' from '%s' to '%s'.", 71 str(key), str(getattr(self, key, None)), str(value)) 72 setattr(self, key, value) 73 return self
Update the BaseModel with the contents of data and validate.
The update does not happen in place but rather a new updated object is returned and updating triggers validation.
Parameters
- data (dict): Only valid key, value pairs are accepted.
Returns
- UpdatableBaseModel: The updated BaseModel.
76class PerlinParameters(FimamaModel): 77 """ 78 Parameters for generating a heightmap with Perlin noise. 79 80 Apart from `scale` these are parameters of the `noise.pnoise2` function. 81 82 Parameters 83 ---------- 84 scale : float 85 Factor to zoom into the noise field by. Small values will produce a 86 very densely noisy map, by default 100.0. 87 octaves: int 88 The number of passes for generating fractional Brownian motion 89 (fBm) noise, by default 6. 90 persistence: float 91 The amplitude of each successive octave relative to the one below 92 it. Note the amplitude of the first pass is always 1.0. Defaults to 93 0.5 (each higher octave's amplitude is halved). 94 lacunarity: float 95 The frequency of each successive octave relative to the one below 96 it, similar to persistence. Defaults to 2.0. 97 base: int 98 Fixed offset for the input coordinates. Useful for generating 99 different noise textures with the same repeat interval, by default 100 42. 101 """ 102 scale: float = 100.0 103 octaves: int = 6 104 persistence: float = 0.5 105 lacunarity: float = 2.0 106 base: int = 42
Parameters for generating a heightmap with Perlin noise.
Apart from scale these are parameters of the noise.pnoise2 function.
Parameters
- scale (float): Factor to zoom into the noise field by. Small values will produce a very densely noisy map, by default 100.0.
- octaves (int): The number of passes for generating fractional Brownian motion (fBm) noise, by default 6.
- persistence (float): The amplitude of each successive octave relative to the one below it. Note the amplitude of the first pass is always 1.0. Defaults to 0.5 (each higher octave's amplitude is halved).
- lacunarity (float): The frequency of each successive octave relative to the one below it, similar to persistence. Defaults to 2.0.
- base (int): Fixed offset for the input coordinates. Useful for generating different noise textures with the same repeat interval, by default 42.
Inherited Members
109class VoronoiConfiguration(FimamaModel): 110 """ 111 Configuration for plotting elements of the Voronoi grid. 112 113 Note that these parameters do not affect drawing the Voronoi cell polygons. 114 Their plotting is controlled by configuration for drawing the heightmap. 115 116 Parameters 117 ---------- 118 plot_voronoi_grid: bool 119 Should any part of the grid be plotted, if set to False, none of 120 the elements get plotted regardless of the parameter values, by 121 default False. 122 show_ridges: bool 123 Should the perimeter of the polygons - the Voronoi ridges - be 124 plotted, by default True. 125 show_vertices: bool 126 Should the vertices, or intersections of the ridges, be plotted, by 127 default False. 128 show_points: bool 129 Should the points that the Voronoi grid is based on be plotted, by 130 default False. 131 """ 132 plot_voronoi_grid: bool = False 133 show_ridges: bool = True 134 show_vertices: bool = False 135 show_points: bool = False
Configuration for plotting elements of the Voronoi grid.
Note that these parameters do not affect drawing the Voronoi cell polygons. Their plotting is controlled by configuration for drawing the heightmap.
Parameters
- plot_voronoi_grid (bool): Should any part of the grid be plotted, if set to False, none of the elements get plotted regardless of the parameter values, by default False.
- show_ridges (bool): Should the perimeter of the polygons - the Voronoi ridges - be plotted, by default True.
- show_vertices (bool): Should the vertices, or intersections of the ridges, be plotted, by default False.
- show_points (bool): Should the points that the Voronoi grid is based on be plotted, by default False.
Inherited Members
138class MapConfiguration(FimamaModel): 139 """ 140 Map generation configuration. 141 142 Parameters 143 ---------- 144 height: int 145 Height of the map in cells, by default 125. 146 width: int 147 Width of the map in cells, by default 200. 148 generator: str 149 Which generator to use for the heightmap, by default "perlin". 150 colormap_name: str 151 Name of the colormap to use for displaying the heightmap, by 152 default "dark-atlas". 153 perlin_parameters: `PerlinParameters` | None 154 Parameters for generating a heightmap with Perlin noise, by default 155 None. 156 voronoi_configuration: `VoronoiConfiguration` | None 157 Parameters for plotting the Voronoi grid (not the voronoi polygons, 158 but rather associated edges and points), by default None. 159 """ 160 height: int = 125 161 width: int = 200 162 generator: str = "perlin" 163 colormap_name: str = "dark-atlas" 164 perlin_parameters: PerlinParameters | None = None 165 voronoi_configuration: VoronoiConfiguration | None = None
Map generation configuration.
Parameters
- height (int): Height of the map in cells, by default 125.
- width (int): Width of the map in cells, by default 200.
- generator (str): Which generator to use for the heightmap, by default "perlin".
- colormap_name (str): Name of the colormap to use for displaying the heightmap, by default "dark-atlas".
- perlin_parameters (
PerlinParameters| None): Parameters for generating a heightmap with Perlin noise, by default None. - voronoi_configuration (
VoronoiConfiguration| None): Parameters for plotting the Voronoi grid (not the voronoi polygons, but rather associated edges and points), by default None.