Indexing Module#
The indexing module provides efficient functions for working with numpy arrays, particularly for indexing and extracting values based on various conditions.
Functions#
get_indices(arr, mask_val)
#
Get the array indices for the non-zero cells.
This function returns the row and column indices of cells in a 2D array that match a specific value or all non-zero values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
2D array with values you need to get the indexes of the cells that are filled with these values. |
required |
mask_val
|
Union[int, float]
|
If you need to locate only a certain value, and not all values in the array. If None or falsy, will return indices of all non-zero values. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
A tuple of two numpy arrays: - first array is the row indices - second array is the column indices |
Raises:
Type | Description |
---|---|
ValueError
|
If the input array is not 2D. |
Examples:
- Import numpy
- Create a sample array
- Get indices of all non-zero values
- Get indices of a specific value
Source code in src/hpc/indexing.py
get_indices2(arr, mask=None)
#
Get indices of array cells after filtering values based on mask values.
This function returns the indices of array cells that don't match the values in the mask. If mask is None, returns indices of all cells in the array. This function is particularly useful for filtering out specific values (like NoData values) from an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
2D numpy array to get indices from. |
required |
mask
|
Optional[List[Union[int, float, number]]]
|
List of values to exclude from the result. - If None, returns indices of all cells. - If list with one value, returns indices of cells not equal to that value. - If list with two values, returns indices of cells not equal to either value. |
None
|
Returns:
Type | Description |
---|---|
List[Tuple[int, int]]
|
List of tuples (row, col) representing the indices of cells that pass the filter. |
Raises:
Type | Description |
---|---|
ValueError
|
If mask contains more than two values. |
ValueError
|
If the input array is not 2D. |
Examples:
- Import numpy
- Create a sample array
- Get all indices
- Filter out cells with value 5
- Filter out cells with values 1 and 9
- Works with NaN values
Source code in src/hpc/indexing.py
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 |
|
get_pixels(arr, mask=None, mask_val=None)
#
Get pixels from a raster (with optional mask).
This function extracts pixel values from an array based on a mask. It can work with both 2D and 3D arrays and can extract pixels based on specific mask values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
Array of raster data in the form [bands][y][x] or [y][x]. |
required |
mask
|
ndarray
|
Array (2D) of values to mask data (from rasterizing a vector). If None, returns the original array. |
None
|
mask_val
|
Union[int, float, None]
|
Value of the data pixels in the mask to extract. If None or falsy, will extract all non-zero values. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Array of non-masked data. - For 2D input: 1D array of values - For 3D input: 2D array with bands as rows and filtered pixels as columns |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask dimensions don't match the array dimensions. |
Examples:
- Import numpy
- 2D array example
- 3D array example
- With specific mask value
Source code in src/hpc/indexing.py
get_pixels2(arr, mask=None)
#
Get pixels from a raster using the get_indices2 function for filtering.
This function extracts pixel values from an array based on indices that don't match the mask values. It works with both 2D and 3D arrays and is particularly useful for filtering out specific values (like NoData values) from an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
Array of raster data in the form [y][x] for 2D arrays or [bands][y][x] for 3D arrays. |
required |
mask
|
Optional[List[Union[int, float, number]]]
|
List of values to exclude from the result. - If None, returns all pixels. - If list with values, returns pixels not matching those values. See get_indices2 for more details on mask behavior. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Array of filtered pixel values. - For 2D input: 1D array of values - For 3D input: 2D array with bands as rows and filtered pixels as columns |
Raises:
Type | Description |
---|---|
ValueError
|
If mask contains more than two values. |
ValueError
|
If the input array dimensions are not supported (must be 2D or 3D). |
Examples:
- Import numpy
- 2D array example
- Get all pixels
- Filter out pixels with value 5
- 3D array example
- Filter out pixels with value 5 and 50
Source code in src/hpc/indexing.py
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 |
|
locate_values(values, grid_x, grid_y)
#
Locate coordinates in a grid by finding the closest grid points.
This function takes a set of (x,y) coordinates and finds the closest matching indices in the provided grid_x and grid_y arrays. It's particularly useful for mapping point data to grid cells in spatial analysis and interpolation tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
ndarray
|
required | |
grid_x
|
ndarray
|
Array of x-coordinates (west to east) to search within. These are typically the x-coordinates of a grid. |
required |
grid_y
|
ndarray
|
Array of y-coordinates (north to south) to search within. These are typically the y-coordinates of a grid. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Raises:
Type | Description |
---|---|
ValueError
|
If values array doesn't have shape (n, 2). |
ValueError
|
If grid_x or grid_y are empty arrays. |
Examples:
- Import numpy
- Create sample coordinates to locate
- Create grid coordinates
- Find the closest grid indices
- Verify the first coordinate [10, 20] maps to grid_x[1]=10, grid_y[1]=20
- Verify the second coordinate [30, 40] maps to grid_x[3]=30, grid_y[2]=40
- Verify the third coordinate [50, 60] maps to grid_x[5]=50, grid_y[3]=60
Source code in src/hpc/indexing.py
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 |
|