Numeric
numeric
¶
Functions¶
calc_communities(starts, ends, edge_weights, louvain_resolution=1.0, out_dir=None, transform_to_epsg_4978=None)
¶
Build a networkx graph from adjacency information. Each node represents a detection while the edges represent the quality of the matches between detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starts
|
ndarray
|
(N, 3) array of ray start points |
required |
ends
|
ndarray
|
(N, 3) array of ray end points |
required |
edge_weights
|
List[Tuple[int, int, Dict[str, float]]]
|
List of edges defining the graph connectivity. Each edge is (start_idx, end_idx, weight_dict) where weight_dict contains the edge weight information |
required |
louvain_resolution
|
float
|
Resolution hyperparameter for the Louvain community detection algorithm. Higher values lead to more communities |
1.0
|
out_dir
|
PATH_TYPE
|
Directory to save the output NPZ file containing community information. If None, results are returned as a dictionary |
None
|
transform_to_epsg_4978
|
ndarray
|
4x4 transformation matrix to convert points from local coordinates to EPSG:4978 (Earth-centered Earth-fixed) |
None
|
Returns:
Type | Description |
---|---|
Union[Path, Dict[str, ndarray]]
|
Union[Path, Dict[str, np.ndarray]]: If out_dir is provided, returns the path to the NPZ file containing the community information. Otherwise returns a dictionary with keys: - 'ray_IDs': (N,) array mapping each ray to its community ID - 'community_points': (M, 3) array of 3D points representing each community - 'community_points_latlon': (M, 3) array of lat/lon points (only if transform_to_epsg_4978 is provided) |
Source code in geograypher/utils/numeric.py
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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
|
calc_graph_weights(starts, ends, ray_IDs, similarity_threshold, out_dir=None, min_dist=1e-06, step=5000, transform=None)
¶
This function processes sets of ray segments to build a graph where edges represent ray intersections. The weight of each edge is inversely proportional to the intersection distance between rays. For memory efficiency with large numbers of segments, the computation is done in chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starts
|
ndarray
|
(N, 3) array of ray start points |
required |
ends
|
ndarray
|
(N, 3) array of ray end points |
required |
ray_IDs
|
ndarray
|
(N,) array of integers identifying which image each ray comes from |
required |
similarity_threshold
|
float
|
Maximum intersection distance to consider when creating graph edges. Greater distances will be dropped. |
required |
out_dir
|
PATH_TYPE
|
Directory to save the output JSON file containing edge information. If None, no file is saved and a list of edge weights is returned instead. |
None
|
min_dist
|
float
|
Minimum intersection distance to allow, used to avoid division by zero when calculating weights. Defaults to 1e-6 |
1e-06
|
step
|
int
|
Number of rays to process in each chunk to manage memory usage. Defaults to 5000 |
5000
|
transform
|
callable
|
Function to apply to distances before inversion for weight calculation. For example, if you want graph weights to be distance^3, use lambda x: x**3. Defaults to None |
None
|
Returns:
Type | Description |
---|---|
Union[Path, List[Tuple]]
|
Union[Path, List[Tuple]]: If out_dir is provided, returns the path to the JSON |
Union[Path, List[Tuple]]
|
file containing the edge information. Otherwise returns a list of tuples, each |
Union[Path, List[Tuple]]
|
containing (start_idx, end_idx, weight_dict) where weight_dict contains the |
Union[Path, List[Tuple]]
|
edge weight information |
Source code in geograypher/utils/numeric.py
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 |
|
chunk_slices(N, step)
¶
Yield slices for (step, step) chunks of the upper triangular area of an (N, N) square matrix.
For example, if N=5 and step=2, the slices would grab: 1 1 2 2 3 1 1 2 2 3 - - 4 4 5 - - 4 4 5 - - - - 6 And in the (1, 4, 6) cases, is_diag would be True
Yields:
Type | Description |
---|---|
slice
|
Each yielded value is (islice, jslice, is_diag), where: |
slice
|
|
bool
|
|
Tuple[slice, slice, bool]
|
|
Source code in geograypher/utils/numeric.py
compute_3D_triangle_area_vectorized(corners, return_z_proj_area=True)
¶
summary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corners
|
ndarray
|
(n_faces, n) |
required |
return_z_proj_area
|
bool
|
description. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
_type_ |
description |
Source code in geograypher/utils/numeric.py
compute_approximate_ray_intersections(a0, a1, b0, b1, clamp=False)
¶
Compute closest points and distances between N line segments a0->a1 and b0->b1. Returns (N, N, 3), (N, N, 3), (N, N). If clamp is True, then respect the line segment ends. If clamp is False, then use the infinite rays.
Based on https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a0
|
ndarray
|
Start points of the first segments (N, 3). |
required |
a1
|
ndarray
|
End points of the first segments (N, 3). |
required |
b0
|
ndarray
|
Start points of the second segments (N, 3). |
required |
b1
|
ndarray
|
End points of the second segments (N, 3). |
required |
clamp
|
bool
|
If True, the closest points are clamped to the segment endpoints. If False, the closest points may be anywhere along the infinite rays. |
False
|
Returns:
Name | Type | Description |
---|---|---|
pA |
ndarray
|
Closest point on the A segments (axis 0) compared to each of the B segments (axis 1) (N, N, 3). For example, the closest point between A[5] and B[2] at pA[5, 2] |
pB |
ndarray
|
Closest point on the B segments (axis 1) compared to each of the A segments (axis 1) (N, N, 3) |
dist |
ndarray
|
The minimum distance between the A (axis 0) and B (axis 1) segments. |
Source code in geograypher/utils/numeric.py
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 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 |
|
create_ramped_weighting(rectangle_shape, ramp_dist_frac)
¶
Create a ramped weighting that is higher toward the center with a max value of 1 at a fraction from the edge
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rectangle_shape
|
Tuple[int, int]
|
Size of rectangle to create a mask for |
required |
ramp_dist_frac
|
float
|
Portions at least this far from an edge will have full weight |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array representing the weighting from 0-1 |
Source code in geograypher/utils/numeric.py
fair_mode_non_nan(values)
¶
Compute the most common value per row in an array of integers and nans. This behaves similarly to scipy.stats.mode(values, axis=1, nan_policy="omit") except that for values with equal counts, one is chosen randomly, rather than taking the lower value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
ndarray
|
(n, m) The input values (float-typed), consisting of integers and nans |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (n,) the most common value per row |
Source code in geograypher/utils/numeric.py
format_graph_edges(islice, jslice, dist, ray_IDs)
¶
This function generates edge definitions for a graph where nodes represent rays and edges represent valid intersections between rays. It applies three filtering criteria: 1. Only uses edges where (i, j) is finite (not NaN) 2. Only uses edges where i < j (keeps it upper triangular) 3. Only uses edges between rays from different images (different ray_IDs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
islice
|
slice
|
Slice indicating the block of rows being processed in the chunked computation |
required |
jslice
|
slice
|
Slice indicating the block of columns being processed in the chunked computation |
required |
dist
|
ndarray
|
Distance matrix containing distances between rays (chunked) This is the [islice, jslice] section of a larger distance matrix. |
required |
ray_IDs
|
ndarray
|
Array of identifiers indicating which image each ray comes from (not chunked) |
required |
Returns:
Type | Description |
---|---|
List[Tuple[int, int, Dict[str, float]]]
|
List[Tuple[int, int, Dict[str, float]]]: List of edge definitions, where each edge |
List[Tuple[int, int, Dict[str, float]]]
|
is a tuple of |
List[Tuple[int, int, Dict[str, float]]]
|
|
List[Tuple[int, int, Dict[str, float]]]
|
|
List[Tuple[int, int, Dict[str, float]]]
|
|
Source code in geograypher/utils/numeric.py
intersection_average(starts, ends)
¶
Given arrays of line segment start and end points, compute the average of the closest intersection points between all pairs of segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starts
|
ndarray
|
(N, 3) array of segment start points |
required |
ends
|
ndarray
|
(N, 3) array of segment end points |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (3,) array, the average intersection point |