Utility Functions¶
fdtdx.utils.Logger
¶
Logger for managing experiment outputs and visualization.
Handles experiment logging, metrics tracking, and visualization of simulation results. Creates a working directory structure, initializes logging, and provides methods for saving figures, metrics, and device parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiment_name
|
str
|
Name of the experiment |
required |
name
|
str | None
|
Optional specific name for the working directory. If None, uses timestamp. |
None
|
Source code in src/fdtdx/utils/logger.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
params_dir: Path
property
¶
Directory for storing parameter files.
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
Directory for parameter file outputs |
stl_dir: Path
property
¶
Directory for storing STL files.
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
Directory for STL file outputs |
log_detectors(iter_idx, objects, detector_states, exclude=[])
¶
Log detector states and generate visualization plots.
Creates plots for each detector's state and saves them to the detector's output directory. Handles both figure outputs and other detector-specific file formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iter_idx
|
int
|
Current iteration index |
required |
objects
|
ObjectContainer
|
Container with simulation objects |
required |
detector_states
|
dict[str, DetectorState]
|
Dictionary mapping detector names to their states |
required |
exclude
|
list[str]
|
List of detector names to exclude from logging |
[]
|
Source code in src/fdtdx/utils/logger.py
log_params(iter_idx, params, objects, export_figure=False, export_stl=False, export_air_stl=False)
¶
Log parameter states and export device visualizations.
Saves device parameters and optionally exports visualizations as figures or STL files. Tracks changes in device voxels between iterations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iter_idx
|
int
|
Current iteration index |
required |
params
|
ParameterContainer
|
Container with device parameters |
required |
objects
|
ObjectContainer
|
Container with simulation objects |
required |
export_figure
|
bool
|
Whether to export index matrix figures |
False
|
export_stl
|
bool
|
Whether to export device geometry as STL |
False
|
export_air_stl
|
bool
|
Whether to export air regions as STL |
False
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of voxels that changed since last iteration |
Source code in src/fdtdx/utils/logger.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
savefig(directory, filename, fig, dpi=300)
¶
Save a matplotlib figure to file.
Creates a figures subdirectory if needed and saves the figure with specified settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
Path
|
Base directory to save in |
required |
filename
|
str
|
Name for the figure file |
required |
fig
|
Figure
|
Matplotlib figure to save |
required |
dpi
|
int
|
Resolution in dots per inch |
300
|
Source code in src/fdtdx/utils/logger.py
write(stats, do_print=True)
¶
Write statistics to CSV file and optionally print them.
Records metrics in a CSV file and optionally displays them in a formatted table. Automatically initializes CSV headers on first write.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stats
|
dict
|
Dictionary of statistics to record |
required |
do_print
|
bool
|
Whether to print stats to console |
True
|
Source code in src/fdtdx/utils/logger.py
A logger which can automatically record important metrics during optimization. For a detailed guide on the usage, see the example scripts in the github repository.
fdtdx.utils.plot_setup.plot_setup(config, objects, exclude_object_list=[], filename=None, axs=None, plot_legend=True, exclude_xy_plane_object_list=[], exclude_yz_plane_object_list=[], exclude_xz_plane_object_list=[])
¶
Creates a visualization of the simulation setup showing objects in XY, XZ and YZ planes.
Generates three subplots showing cross-sections of the simulation volume and the objects within it. Objects are drawn as colored rectangles with optional legends. The visualization helps verify the correct positioning and sizing of objects in the simulation setup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
SimulationConfig
|
Configuration object containing simulation parameters like resolution |
required |
objects
|
ObjectContainer
|
Container holding all simulation objects to be plotted |
required |
exclude_object_list
|
list[SimulationObject]
|
List of objects to exclude from all plots |
[]
|
filename
|
str | Path | None
|
If provided, saves the plot to this file instead of displaying |
None
|
axs
|
Sequence[Any] | None
|
Optional matplotlib axes to plot on. If None, creates new figure |
None
|
plot_legend
|
bool
|
Whether to add a legend showing object names/types |
True
|
exclude_xy_plane_object_list
|
list[SimulationObject]
|
Objects to exclude from XY plane plot |
[]
|
exclude_yz_plane_object_list
|
list[SimulationObject]
|
Objects to exclude from YZ plane plot |
[]
|
exclude_xz_plane_object_list
|
list[SimulationObject]
|
Objects to exclude from XZ plane plot |
[]
|
Returns:
Type | Description |
---|---|
Figure
|
matplotlib.figure.Figure: The generated figure object |
Note
The plots show object positions in micrometers, converting from simulation units. PML objects are automatically excluded from their respective boundary planes.
Source code in src/fdtdx/utils/plot_setup.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
Plots an image of the simulation scene using matplotlib. This is very helpful for veryfying the correct positions of all objects in the simulation scene.