Array-Related Utils
Contains a suite of utility functions for operations on 2D numpy arrays, particularly arrays representing atom pairs. These operations include finding shared pairs, determining non-matching indices, and various set-like operations including intersection, difference, symmetric difference, union, and subset/superset/equality checks. It also provides functions to check for uniqueness and strict subset/superset relations.
Functions:
Name | Description |
---|---|
find_shared_pairs |
Finds shared pairs between two arrays. |
non_matching_indices |
Finds non-matching indices between two arrays. |
intersection |
Computes the intersection of two arrays. |
difference |
Computes the difference of two arrays. |
symmetric_difference |
Computes the symmetric difference of two arrays. |
union |
Computes the union of two arrays. |
isdisjoint |
Checks if two arrays are disjoint. |
issubset |
Checks if the first array is a subset of the second array. |
issuperset |
Checks if the first array is a superset of the second array. |
isequal |
Checks if two arrays are equal. |
isunique |
Checks if an array has unique rows. |
is_strict_subset |
Checks if the first array is a strict subset of the second array. |
is_strict_superset |
Checks if the first array is a strict superset of the second array. |
Each function operates on numpy arrays (with a shape of (n,2) for most functions, representing pairs of atom indices) and returns either a new array resulting from the operation or a boolean value representing the relationship between arrays.
Notes
This module is intended for use with arrays of atom pair indices. However, most of these functions would be applicable to other data as long as the input is 2D numpy arrays.
Functions like intersection
, difference
, union
etc. perform set operations considering each row of the input
arrays as an element of the set. This makes these functions particularly useful for operations on collections of
atom pairs, where each pair is represented by a row in the array.
sorting_indices ¶
sorting_indices(arr)
Sort pairs in an array such that the smaller index is in the first column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
NDArray[int32]
|
The array to sort. |
required |
Returns:
Type | Description |
---|---|
NDArray[int32]
|
NDArray[np.int32]: The sorted array. |
Source code in lahuta/utils/array_utils.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
sort_pairs ¶
sort_pairs(arr)
Sort pairs in an array such that the smaller index is in the first column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
NDArray[int32]
|
The array to sort. |
required |
Returns:
Type | Description |
---|---|
NDArray[int32]
|
NDArray[np.int32]: The sorted array. |
Source code in lahuta/utils/array_utils.py
84 85 86 87 88 89 90 91 92 93 94 95 |
|
find_shared_pairs ¶
find_shared_pairs(arr1, arr2)
Find shared elements between two 2D numpy arrays.
This function takes two 2D arrays where each row represents a pair of atom indices and returns a 1D boolean
array representing whether each pair in arr1
also appears in arr2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[int32]
|
A 2D array of shape (n_pairs1, 2) where each row represents a pair of atom indices. |
required |
arr2 |
NDArray[int32]
|
A 2D array of shape (n_pairs2, 2) where each row represents a pair of atom indices. |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
NDArray[np.bool_]: A 1D boolean array of shape (n_pairs1,) where each element represents |
NDArray[bool_]
|
whether the corresponding pair in |
Example
arr1 = np.array([[1, 2], [3, 4], [5, 6]])
arr2 = np.array([[3, 4], [7, 8], [1, 2]])
find_shared_pairs(arr1, arr2)
array([ True, True, False])
Source code in lahuta/utils/array_utils.py
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 |
|
non_matching_indices ¶
non_matching_indices(arr1, arr2)
Find the indices of non-matching elements between two 2D numpy arrays.
This function takes two 2D arrays where each row represents a pair of atom indices and returns a 1D boolean
array representing whether each pair in arr1
does not appear in arr2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[int32]
|
A 2D array of shape (n_pairs1, 2) where each row represents a pair of atom indices. |
required |
arr2 |
NDArray[int32]
|
A 2D array of shape (n_pairs2, 2) where each row represents a pair of atom indices. |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
NDArray[np.bool_]: A 1D boolean array of shape (n_pairs1,) where each element |
NDArray[bool_]
|
represents whether the corresponding |
Example
arr1 = np.array([[1, 2], [3, 4], [5, 6]])
arr2 = np.array([[3, 4], [7, 8], [1, 2]])
non_matching_indices(arr1, arr2)
array([False, False, True])
Source code in lahuta/utils/array_utils.py
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 |
|
asvoid ¶
asvoid(arr)
Convert a 2D numpy array into a 1D array of type np.void.
The function views each row in the input array as a single item of type np.void, which effectively converts the 2D array into a 1D array of binary representations of the rows. This can be useful for performing operations that are typically only possible with 1D arrays, such as membership tests.
Based on
- https://stackoverflow.com/questions/16216078/test-for-membership-in-a-2d-numpy-array
- http://stackoverflow.com/a/16973510/190597
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
NDArray[_DType]
|
A 2D numpy array of shape (n, 2), where each row is a pair of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
arr_void |
NDArray[void]
|
A 1D numpy array of dtype np.void, where each element is the binary representation of a pair of atom indices from the input array. |
Example
arr = np.array([[1, 2], [3, 4]])
print(asvoid(arr))
[(1, 2), (3, 4)]
Source code in lahuta/utils/array_utils.py
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 |
|
intersection ¶
intersection(arr1, arr2, assume_unique=False)
Calculate the intersection of two 2D arrays and return a boolean array that indicates the membership
of each pair of atom indices in arr1
in arr2
.
The function first converts arr1
and arr2
into 1D arrays of type np.void using the asvoid
function,
then uses np.in1d
to test the membership of each element in the resulting 1D array from arr1
in that
from arr2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[_DType]
|
A 2D numpy array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
NDArray[_DType]
|
A 2D numpy array of shape (m, 2) where each row is a pair of atom indices. |
required |
assume_unique |
bool
|
If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
mask |
NDArray[bool_]
|
A 1D boolean numpy array of length n, where each element indicates whether the corresponding
pair of atom indices in |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [5, 6]])
print(intersection(arr1, arr2))
[True False]
Source code in lahuta/utils/array_utils.py
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 |
|
difference ¶
difference(arr1, arr2, assume_unique=False)
Calculate the set difference between two 2D arrays, represented as a boolean array.
The function converts the input arrays into 1D arrays of type np.void, then uses np.in1d
to test
the membership of each element in arr1
in arr2
. The resulting boolean array is then inverted to
represent the elements in arr1
that are not in arr2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[_DType]
|
A 2D numpy array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
NDArray[_DType]
|
A 2D numpy array of shape (m, 2) where each row is a pair of atom indices. |
required |
assume_unique |
bool
|
If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
mask |
NDArray[bool_]
|
A 1D boolean numpy array of length n, where each element indicates whether the corresponding
pair of atom indices in |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [5, 6]])
print(difference(arr1, arr2))
[False True]
Source code in lahuta/utils/array_utils.py
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 |
|
symmetric_difference ¶
symmetric_difference(arr1, arr2, assume_unique=False)
Calculate the symmetric difference of two arrays.
This function returns the elements that are in arr1
but not in arr2
and vice versa.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[_DType]
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
NDArray[_DType]
|
An array of shape (m, 2) where each row is a pair of atom indices. |
required |
assume_unique |
bool
|
If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
mask_a |
NDArray[bool_]
|
A boolean array that can be used to index |
mask_b |
NDArray[bool_]
|
A boolean array that can be used to index |
Source code in lahuta/utils/array_utils.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
union ¶
union(arr1, arr2)
Calculate the union of two arrays and return the unique pairs along with their indices.
The function finds unique pairs from the union of arr1
and arr2
, and also returns the
indices in the concatenated array which correspond to these unique pairs. The indices can
be used to select corresponding elements in another array that has the same order as arr1
and arr2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[_DType]
|
A 2D numpy array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
NDArray[_DType]
|
A 2D numpy array of shape (m, 2) where each row is a pair of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
union_arr |
NDArray[_DType]
|
A 2D numpy array of shape (k, 2) where each row is a unique pair of atom indices
that are in |
indices |
NDArray[int32]
|
A 1D numpy array of the indices in the concatenated array which correspond to the unique pairs. |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [5, 6]])
pairs, indices = union(arr1, arr2)
print(pairs)
print(indices)
[[1, 2], [3, 4], [5, 6]]
[0 1 3]
Source code in lahuta/utils/array_utils.py
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 |
|
union_masks ¶
union_masks(arr1, arr2, assume_unique=False)
Calculate the union of two arrays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
NDArray[_DType]
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
NDArray[_DType]
|
An array of shape (m, 2) where each row is a pair of atom indices. |
required |
assume_unique |
bool
|
If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
mask_a |
NDArray[bool_]
|
A boolean array that can be used to index |
mask_b |
NDArray[bool_]
|
A boolean array that can be used to index |
Source code in lahuta/utils/array_utils.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
isdisjoint ¶
isdisjoint(arr1, arr2)
Determine if two arrays have a null intersection.
This function checks if the two input arrays do not share any common elements. Both arrays are assumed to be 2D with each row being a pair of atom indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
A 2D numpy array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
ndarray
|
A 2D numpy array of shape (m, 2) where each row is a pair of atom indices. |
required |
Returns:
Type | Description |
---|---|
bool
|
np.bool_: True if the two arrays have no common elements (i.e., disjoint), False otherwise. |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
isdisjoint(arr1, arr2)
True
Source code in lahuta/utils/array_utils.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
issubset ¶
issubset(arr1, arr2)
Determine if the first array is a subset of the second array.
This function checks if all elements of the first array are found within the second array. Both arrays are assumed to be 2D with each row being a pair of atom indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
A 2D numpy array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
ndarray
|
A 2D numpy array of shape (m, 2) where each row is a pair of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if all elements of the first array are in the second array (i.e., arr1 is a subset of arr2), False otherwise. |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [3, 4], [5, 6]])
issubset(arr1, arr2)
True
Source code in lahuta/utils/array_utils.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
issuperset ¶
issuperset(arr1, arr2)
Check if arr1
is a superset of arr2
.
This function takes two 2D numpy arrays, arr1 and arr2, each with shape (n, 2), and checks if every pair in arr2 is present in arr1. Each row in the arrays represents a pair of atom indices. The function returns True if arr1 is a superset of arr2, and False otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
A 2D numpy array of shape (n, 2) representing pairs of atom indices. |
required |
arr2 |
ndarray
|
A 2D numpy array of shape (n, 2) representing pairs of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
Example
issuperset(np.array([[1, 2], [2, 3]]), np.array([[1, 2]]))
True
issuperset(np.array([[1, 2], [2, 3]]), np.array([[1, 2], [3, 4]]))
False
Source code in lahuta/utils/array_utils.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
isequal ¶
isequal(arr1, arr2)
Check if two 2D arrays, arr1
and arr2
, are equal.
This function verifies the equality of two 2D numpy arrays, arr1
and arr2
.
Each array has a shape (n, 2), where each row represents a pair of atom indices.
The arrays are considered equal if they have the same shape and each pair from
arr1
is found in arr2
, and vice versa.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
A 2D numpy array of shape (n, 2) representing pairs of atom indices. |
required |
arr2 |
ndarray
|
A 2D numpy array of shape (n, 2) representing pairs of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
Example
isequal(np.array([[1, 2], [2, 3]]), np.array([[1, 2], [2, 3]]))
True
isequal(np.array([[1, 2], [2, 3]]), np.array([[1, 2], [3, 4]]))
False
Source code in lahuta/utils/array_utils.py
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 |
|
isunique ¶
isunique(arr)
Check if a 2D array, arr
, contains no duplicate entries.
The function checks if a 2D numpy array, arr
, of shape (n, 2), has any duplicate rows.
Each row represents a pair of atom indices.
The function returns True if arr
contains no duplicate pairs, and False otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
ndarray
|
A 2D numpy array of shape (n, 2) representing pairs of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
Example
isunique(np.array([[1, 2], [2, 3]]))
True
isunique(np.array([[1, 2], [2, 3], [1, 2]]))
False
Source code in lahuta/utils/array_utils.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
|
is_strict_subset ¶
is_strict_subset(arr1, arr2)
Check if the first array is a strict subset of the second array.
The function determines whether every element of the first array is in the second array and the two arrays are not identical. The arrays should be of shape (n, 2), where each row is a pair of atom indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
ndarray
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the first array is a strict subset of the second array, False otherwise. |
Example
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [3, 4], [5, 6]])
is_strict_subset(arr1, arr2)
True
Source code in lahuta/utils/array_utils.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
|
is_strict_superset ¶
is_strict_superset(arr1, arr2)
Check if the first array is a strict superset of the second array.
The function determines whether every element of the second array is in the first array and the two arrays are not identical. The arrays should be of shape (n, 2), where each row is a pair of atom indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
ndarray
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
arr2 |
ndarray
|
An array of shape (n, 2) where each row is a pair of atom indices. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the first array is a strict superset of the second array, False otherwise. |
Example
arr1 = np.array([[1, 2], [3, 4], [5, 6]])
arr2 = np.array([[1, 2], [3, 4]])
is_strict_superset(arr1, arr2)
True
Source code in lahuta/utils/array_utils.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
|