Field¶
Field¶
Bases: Module
A container that describes the chromatic light field at a 2D plane.
Field objects track various attributes of a complex-valued field (in
addition to the field itself for each wavelength): the spacing of the
samples along the field, the wavelengths in the spectrum, and the density
of the wavelengths. This information can be used, for example, to calculate
the intensity of a field at a plane, appropriately weighted by the spectrum.
Field objects also provide various grids for convenience, as well
as allow elementwise operations with any broadcastable values, including
scalars, arrays, or other Field objects. These operations include: +,
- (including negation), *, /, +=, -=, *=, /=.
The shape of a Field object is determined by the type of Field it
is. In the simplest case, we have ScalarFields which are monochromatic
and scalar descriptions of the electric field. In this case, the complex
tensor describing the field is a 2D array of shape (... height width)
where the ... means that potentially zero or more batch dimensions are
allowed (by default there are no batch dimensions --- but they can be
useful to describe e.g. a field at multiple depth planes or at different
time points). For fields that have multiple wavelengths (i.e. a spectrum),
ChromaticScalarFields are used which have the 3D shape (... height width
wavelengths).
Any Chromatix functions that produce multiple depths (e.g. propagation
to multiple z values) will automatically create a batch dimension. If
more dimensions are required, we encourage the use of jax.vmap,
jax.pmap, or a combination of the two. We intend for these zero-or-more
batch dimensions to be a compromise between not having too many dimensions
when they are not required, and also not having to litter a program with
jax.vmap transformations for common simulations in 3D.
Due to this shape, in order to ensure that attributes of Field
objects broadcast appropriately, attributes which could be 1D arrays are
ensured to have extra singleton dimensions. In order to make the creation
of Field objects more convenient, we provide the class methods
Field.build, Field.empty, and Field.zeros (detailed below),
which accepts scalar or 1D array arguments for the various attributes
(e.g. if a single wavelength is desired, a scalar value can be used, but if
multiple wavelengths are desired, a 1D array can be used for the value of
spectrum). These methods appropriately reshapes the attributes provided
to the correct shapes.
Attributes:
| Name | Type | Description |
|---|---|---|
u |
AbstractVar[Array]
|
The complex-valued field of shape at least |
dx |
AbstractVar[Array]
|
The spacing (pixel size) of the samples of the |
origin |
AbstractVar[Array]
|
The origin of the field's coordinate grid in units of distance.
By default, fields are initialized with an origin of |
spectrum |
AbstractVar[Spectrum]
|
The
|
Source code in src/chromatix/core/field.py
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 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
amplitude
property
¶
batch_dims
property
¶
broadcasted_wavelength
property
¶
The wavelength(s) of the field's spectrum, reshaped for appropriate broadcasting to the appropriate dimension if there are multiple wavelengths.
central_dx
abstractmethod
property
¶
central_extent
property
¶
The extent (lengths in height and width per wavelength) of the field in units of distance.
central_rectangular_dx
abstractmethod
property
¶
central_wavelength
property
¶
The central wavelength of the spectrum (defined as the first wavelength provided to the spectrum because you could construct the complex field with multiple wavelengths in any order).
conj
property
¶
df
property
¶
The frequency spacing of the samples in the frequency space of u.
Defined as an array of same shape as dx specifying the spacing in
the y and x directions respectively (can be the same for y and x for the
common case of square pixels).
dims
instance-attribute
¶
dx
instance-attribute
¶
extent
property
¶
The extent (lengths in height and width per wavelength) of the field
in units of distance. Defined as an array of the same shape as dx
specifying the extent in the y and x dimensions respectively.
f_grid
abstractmethod
property
¶
The frequency grid for each spatial dimension as an array. The 2 entries
along the last dimension represent the y and x grids, respectively. This
grid assumes that the center of the Field is the origin and that
the elements are sampling from the center, not the corner.
Warning
Previous versions of Chromatix used this k_grid property for what
is now f_grid, i.e. the old k_grid used to not be multiplied
by 2 * jnp.pi. For the frequency grid (what used to be called
k_grid), use f_grid. For angular frequency, use k_grid.
grid
abstractmethod
property
¶
The grid for each spatial dimension as an array. The 2 entries along
the last dimension represent the y and x grids, respectively. This grid
assumes that the center of the Field is the origin and that the
elements are sampling from the center, not the corner.
intensity
abstractmethod
property
¶
Intensity of the complex field.
k
property
¶
k_grid
property
¶
The angular frequency grid for each spatial dimension as an array.
This is the same as f_grid but multiplied by 2 * jnp.pi.
The 2 entries along the last dimension represent the y and x grids,
respectively. This grid assumes that the center of the Field is
the origin and that the elements are sampling from the center, not
the corner.
Warning
Previous versions of Chromatix used this k_grid property for what
is now f_grid, i.e. the old k_grid used to not be multiplied
by 2 * jnp.pi. For the frequency grid (what used to be called
k_grid), use f_grid. For angular frequency, use k_grid.
ndim
property
¶
Number of dimensions (the rank) of the complex field.
num_batch_dims
property
¶
origin
instance-attribute
¶
phase
property
¶
power
abstractmethod
property
¶
Power of the complex field.
shape
property
¶
Shape of the complex field.
spatial_dims
property
¶
Axis indices representing the height and width of the complex field.
spatial_limits
property
¶
Return the spatial limits of the field as a (2 2)-shaped array of the
format [[y_min, y_max], [x_min, x_max]].
spatial_shape
property
¶
The height and width of the complex field.
spectrum
instance-attribute
¶
surface_area
property
¶
u
instance-attribute
¶
wavelength
property
¶
The wavelength(s) of the field's spectrum as a float or 1D array.
__add__(other)
¶
Source code in src/chromatix/core/field.py
__floordiv__(other)
¶
Source code in src/chromatix/core/field.py
__matmul__(other)
¶
__mod__(other)
¶
Source code in src/chromatix/core/field.py
__mul__(other)
¶
Source code in src/chromatix/core/field.py
__pow__(other)
¶
Source code in src/chromatix/core/field.py
__radd__(other)
¶
__rfloordiv__(other)
¶
__rmatmul__(other)
¶
__rmod__(other)
¶
__rmul__(other)
¶
__rpow__(other)
¶
Source code in src/chromatix/core/field.py
__rsub__(other)
¶
__rtruediv__(other)
¶
__sub__(other)
¶
Source code in src/chromatix/core/field.py
__truediv__(other)
¶
Source code in src/chromatix/core/field.py
build(u, dx, spectrum)
classmethod
¶
Source code in src/chromatix/core/field.py
empty(shape, dx, spectrum, scalar=True)
classmethod
¶
Source code in src/chromatix/core/field.py
empty_like(field, shape=None, dx=None, origin=None, spectrum=None)
classmethod
¶
Copy over attributes of field to a new Field object, with the
option of changing some attributes.
Note that this function creates a field u with a new empty field.
Source code in src/chromatix/core/field.py
expand_dims()
¶
replace(**kwargs)
¶
squeeze()
¶
unsqueeze()
¶
zeros(shape, dx, spectrum, scalar=True)
classmethod
¶
Source code in src/chromatix/core/field.py
ScalarField¶
Bases: Field, Monochromatic, Scalar
Source code in src/chromatix/core/field.py
central_dx
property
¶
central_rectangular_dx
property
¶
dims = IntEnum('dims', [('y', -2), ('x', -1)])
class-attribute
¶
dx = rearrange(_broadcast_dx_to_grid(dx, spectrum.size), '1 d -> d', d=2)
instance-attribute
¶
f_grid
property
¶
grid
property
¶
intensity
property
¶
origin = rearrange(_broadcast_dx_to_grid(origin, spectrum.size), '1 d -> d', d=2)
instance-attribute
¶
power
property
¶
spectrum = spectrum
instance-attribute
¶
u = jnp.asarray(u, dtype=(jnp.complex64))
instance-attribute
¶
__init__(u, dx, origin, spectrum)
¶
Source code in src/chromatix/core/field.py
ChromaticScalarField¶
Bases: Field, Chromatic, Scalar
Source code in src/chromatix/core/field.py
central_dx
property
¶
central_rectangular_dx
property
¶
dims = IntEnum('dims', [('y', -3), ('x', -2), ('wv', -1)])
class-attribute
¶
dx = _broadcast_dx_to_grid(dx, spectrum.size)
instance-attribute
¶
f_grid
property
¶
grid
property
¶
intensity
property
¶
origin = _broadcast_dx_to_grid(origin, spectrum.size)
instance-attribute
¶
power
property
¶
spectrum = spectrum
instance-attribute
¶
u = jnp.asarray(u, dtype=(jnp.complex64))
instance-attribute
¶
__init__(u, dx, origin, spectrum)
¶
Source code in src/chromatix/core/field.py
VectorField¶
Bases: Field, Monochromatic, Vector
Source code in src/chromatix/core/field.py
central_dx
property
¶
central_rectangular_dx
property
¶
dims = IntEnum('dims', [('y', -3), ('x', -2), ('p', -1)])
class-attribute
¶
dx = _broadcast_dx_to_grid(dx, spectrum.size)
instance-attribute
¶
f_grid
property
¶
grid
property
¶
intensity
property
¶
origin = _broadcast_dx_to_grid(origin, spectrum.size)
instance-attribute
¶
power
property
¶
spectrum = spectrum
instance-attribute
¶
u = jnp.asarray(u, dtype=(jnp.complex64))
instance-attribute
¶
__init__(u, dx, origin, spectrum)
¶
Source code in src/chromatix/core/field.py
ChromaticVectorField¶
Bases: Field, Chromatic, Vector
Source code in src/chromatix/core/field.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 | |
central_dx
property
¶
central_rectangular_dx
property
¶
dims = IntEnum('dims', [('y', -4), ('x', -3), ('wv', -2), ('p', -1)])
class-attribute
¶
dx = rearrange(_broadcast_dx_to_grid(dx, spectrum.size), 'wv d -> wv 1 d', d=2)
instance-attribute
¶
f_grid
property
¶
grid
property
¶
intensity
property
¶
origin = rearrange(_broadcast_dx_to_grid(origin, spectrum.size), 'wv d -> wv 1 d', d=2)
instance-attribute
¶
power
property
¶
spectrum = spectrum
instance-attribute
¶
u = jnp.asarray(u, dtype=(jnp.complex64))
instance-attribute
¶
__init__(u, dx, origin, spectrum)
¶
Source code in src/chromatix/core/field.py
Helpers¶
Pad the field with zeros by the desired amount.
Args:
field: The field to pad.
pad_width: The number of pixels to pad the field with.
cval: The value to pad the field with (defauls is zero).
Source code in src/chromatix/core/field.py
Crop the field by removing pixels from the edges.
Args:
field: The field to crop.
crop_width: The number of pixels to remove from the edges.
Source code in src/chromatix/core/field.py
Shift the sampling grid by an arbitrary amount in y and x directions.
Args:
shift_yx: The shift in y and x directions. Should be an array of
shape [2,] in the format [y, x].
Source code in src/chromatix/core/field.py
Shift field by an integer number of pixels in one or two dimensions,
while keeping the sampling grid centered at the origin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Field
|
The field to shift. |
required |
shift_by
|
int | tuple[int, int]
|
The number of pixels to shift the field by. |
required |
Source code in src/chromatix/core/field.py
Converts the field to a spherical basis. This is useful for high NA lenses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
VectorField | ChromaticVectorField
|
The incoming |
required |
n
|
float
|
Refractive index of the lens. |
required |
NA
|
float
|
NA of the lens. |
required |
f
|
float
|
Focal length of the lens. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The Field.u in spherical coordinates. |
Array
|
Warning Caution, does NOT return a full Field object. |