grid
Module documentation for grid.
API Reference
average_grids(grids, mask='nan')
Compute the average of multiple grids.
:param grids: The input grids to average. :type grids: list of ndarrays :param mask: The mask type to use for averaging. If 'nan', only non-NaN values are considered. Default is 'nan'. :type mask: str :return: The average grid. :rtype: ndarray :raises NotImplementedError: If the mask option is not implemented.
Example
grid1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) grid2 = np.array([[2, 4, 6], [8, 10, 12], [14, 16, 18]]) averaged_grid = average_grids([grid1, grid2], mask='nan') print(averaged_grid)
Source code in mrdna/arbdmodel/submodule/grid.py
9 10 11 12 13 14 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 |
|
convolve_kernel_truncate(array, kernel)
Convolve an array with a kernel, truncating the output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array
|
ndarray
|
The input array. |
required |
kernel
|
ndarray
|
The kernel to convolve with the array. |
required |
Returns:
Name | Type | Description |
---|---|---|
ndarray |
The truncated convolution result. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the dimensions of the array and kernel do not match. |
AssertionError
|
If any dimension of the array is smaller than the corresponding dimension of the kernel. |
Notes
- This function assumes that the kernel has odd dimensions along each dimension.
- If any dimension of the kernel has an even number of elements, a warning message is printed to stderr indicating that the output may be shifted.
Example
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) kernel = np.array([[0.5, 0.5], [0.5, 0.5]]) result = convolve_kernel_truncate(array, kernel) print(result)
Source code in mrdna/arbdmodel/submodule/grid.py
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 |
|
create_bounding_grid(*grids)
Construct a grid bigger than all the (GridDataFormats) grids provided in the arguments; note that the inputs must be orthonormal and have exactly overlapping voxels
Source code in mrdna/arbdmodel/submodule/grid.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
|
fill_nans(grid, neighborhood=1, max_iterations=np.inf, mask=None)
Fill NaN values in a grid using neighborhood averaging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid
|
ndarray
|
The input grid containing NaN values. |
required |
neighborhood
|
int
|
The size of the neighborhood to use for averaging. Default is 1. |
1
|
max_iterations
|
int
|
The maximum number of iterations to perform. Default is infinity. |
inf
|
mask
|
ndarray
|
A mask specifying which values to consider for filling NaNs. If None, all non-NaN values are used. Default is None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
ndarray |
The grid with NaN values filled using neighborhood averaging. |
Notes
- This function requires the 'skimage' package, specifically the 'find_boundaries' function from 'skimage.segmentation'.
Example
grid = np.array([[1, 2, np.nan], [4, np.nan, 6], [np.nan, 8, 9]]) filled_grid = fill_nans(grid, neighborhood=2, max_iterations=10) print(filled_grid)
Source code in mrdna/arbdmodel/submodule/grid.py
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 |
|
gaussian_kernel(voxels=5, sig=1.0, ndim=3)
creates gaussian kernel with side length l
and a sigma of sig
Source code in mrdna/arbdmodel/submodule/grid.py
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 |
|
isotropic_kernel(function, delta, shape=None, cutoff=None, normalize=False)
Generate an isotropic kernel function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
callable
|
A callable representing the kernel function. It should accept a scalar or an array-like object and return a scalar or an array-like object of the same shape. |
required |
delta
|
list or tuple
|
A list or tuple specifying the spacing between grid points in each dimension. |
required |
shape
|
list or tuple
|
A list or tuple specifying the shape of the kernel. If None, it is determined based on the cutoff parameter. |
None
|
cutoff
|
scalar
|
A scalar specifying the maximum distance from the center to consider for the kernel. If provided, it determines the shape of the kernel based on the delta values. |
None
|
normalize
|
bool
|
A boolean indicating whether to normalize the kernel. If True, the kernel values are divided by the sum of all kernel values, ensuring that the kernel sums to 1. |
False
|
Returns:
Name | Type | Description |
---|---|---|
ndarray |
An ndarray representing the isotropic kernel. |
Raises:
Type | Description |
---|---|
ValueError
|
If the cutoff parameter is None, as it is required to determine the shape of the kernel. |
Example
import numpy as np def gaussian(x): ... return np.exp(-0.5 * x**2) delta = [0.1, 0.1, 0.1] kernel = isotropic_kernel(gaussian, delta, cutoff=1.0, normalize=True) print(kernel)
Source code in mrdna/arbdmodel/submodule/grid.py
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 |
|
neighborhood_average(grid, neighborhood=1, fill_value='mirror')
Compute the neighborhood average of a grid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid
|
ndarray
|
The input grid. |
required |
neighborhood
|
int or list
|
The size of the neighborhood to use for averaging. If an integer is provided, the same neighborhood size is used for all dimensions. If a list is provided, it should specify the neighborhood size for each dimension separately. Default is 1. |
1
|
fill_value
|
str or ndarray
|
The fill value to use for padding the grid. If 'mirror', values are mirrored along each dimension. If 'nearest', values are filled with the nearest neighbor. If 'periodic', values are filled with periodic boundary conditions. If a number is provided, it is used directly as the fill value. Default is 'mirror'. |
'mirror'
|
Returns:
Name | Type | Description |
---|---|---|
ndarray |
The grid with the neighborhood average computed. |
Notes
- Currently, only the 'mirror' fill value option is implemented.
- This function requires the 'average_grids' function from an external source.
Example
grid = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) averaged_grid = neighborhood_average(grid, neighborhood=2, fill_value='mirror') print(averaged_grid)
Source code in mrdna/arbdmodel/submodule/grid.py
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 |
|