grid
Bases: geometric_space
A class representing a 3D grid in a geometric space, inheriting from the geometric_space
class.
The grid is defined by its height, width, depth, and an optional number of universes. It provides methods for generating coordinates, converting between grid coordinates and indices, and constructing matrices based on grid packing strategies.
Attributes:
Name | Type | Description |
---|---|---|
h |
int
|
The height of the grid. |
w |
int
|
The width of the grid. |
d |
int
|
The depth of the grid. |
universe_num |
int
|
The number of universes in the grid. |
center |
coordinate_3d
|
The central coordinate of the grid. |
name |
str
|
The name of the grid. |
coordinates |
dict
|
A dictionary containing all grid coordinates mapped to their presence status. |
Methods:
Name | Description |
---|---|
generate_coordinates |
Generates all coordinates in the grid relative to the center. |
to_attribute_index |
Converts a grid coordinate to a unique attribute index. |
to_grid_coordinate |
Converts an attribute index back to a grid coordinate and universe ID. |
get_patch_num |
Calculates the number of patches based on the center distances and universes. |
get_h |
Returns the height of the grid. |
get_w |
Returns the width of the grid. |
get_d |
Returns the depth of the grid. |
get_grid_size |
Returns the total size of the grid. |
get_grid_shape |
Returns the dimensions of the grid (height, width, depth). |
get_h_after_packing |
Returns the height after applying a packing strategy. |
get_w_after_packing |
Returns the width after applying a packing strategy. |
get_d_after_packing |
Returns the depth after applying a packing strategy. |
get_grid_shape_after_packing |
Returns the grid dimensions after packing. |
packing |
Packs the grid with patches using the specified center distances. |
to_aggregation_matrix |
Creates an aggregation matrix from packed patches. |
to_padding_matrix |
Creates a padding matrix from packed patches. |
to_matrix |
Constructs a matrix representation of the grid based on the specified parameters. |
Source code in tinybig/koala/geometry/grid.py
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 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 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 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 |
|
__init__(h, w, d=1, universe_num=1, center=coordinate_3d(0, 0, 0), name='cuboid_geometry', *args, **kwargs)
Initializes the grid with dimensions, universes, and a central coordinate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
int
|
The height of the grid. |
required |
w
|
int
|
The width of the grid. |
required |
d
|
int
|
The depth of the grid. Defaults to 1. |
1
|
universe_num
|
int
|
The number of universes in the grid. Defaults to 1. |
1
|
center
|
coordinate_3d
|
The central coordinate of the grid. Defaults to (0, 0, 0). |
coordinate_3d(0, 0, 0)
|
name
|
str
|
The name of the grid. Defaults to 'cuboid_geometry'. |
'cuboid_geometry'
|
*args
|
Additional arguments for the parent class. |
()
|
|
**kwargs
|
Additional arguments for the parent class. |
()
|
Raises:
Type | Description |
---|---|
ValueError
|
If any of |
Source code in tinybig/koala/geometry/grid.py
generate_coordinates()
Generates all grid coordinates relative to the center.
Returns:
Type | Description |
---|---|
dict
|
A dictionary of coordinates where each key is a |
Source code in tinybig/koala/geometry/grid.py
get_d()
get_d_after_packing(cd_d)
Returns the depth of the grid after packing with a specified center distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cd_d
|
int
|
Center distance along the depth. |
required |
Returns:
Type | Description |
---|---|
int
|
The packed depth. |
Raises:
Type | Description |
---|---|
ValueError
|
If the center distance is 0. |
Source code in tinybig/koala/geometry/grid.py
get_grid_shape()
Returns the dimensions of the grid.
Returns:
Type | Description |
---|---|
tuple
|
A tuple (height, width, depth) representing the grid shape. |
get_grid_shape_after_packing(cd_h, cd_w, cd_d)
Returns the grid dimensions after applying a packing strategy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cd_h
|
int
|
Center distance along the height. |
required |
cd_w
|
int
|
Center distance along the width. |
required |
cd_d
|
int
|
Center distance along the depth. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple (packed_height, packed_width, packed_depth) representing the grid shape after packing. |
Source code in tinybig/koala/geometry/grid.py
get_grid_size(across_universe=False)
Returns the total size of the grid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
across_universe
|
bool
|
If True, includes all universes in the size calculation. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
int
|
The total size of the grid. |
Source code in tinybig/koala/geometry/grid.py
get_h()
get_h_after_packing(cd_h)
Returns the height of the grid after packing with a specified center distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cd_h
|
int
|
Center distance along the height. |
required |
Returns:
Type | Description |
---|---|
int
|
The packed height. |
Raises:
Type | Description |
---|---|
ValueError
|
If the center distance is 0. |
Source code in tinybig/koala/geometry/grid.py
get_patch_num(cd_h, cd_w, cd_d, across_universe=False)
Calculates the number of patches in the grid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cd_h
|
int
|
Center distance along the height. |
required |
cd_w
|
int
|
Center distance along the width. |
required |
cd_d
|
int
|
Center distance along the depth. |
required |
across_universe
|
bool
|
If True, includes all universes. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
int
|
The number of patches. |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the center distances is 0. |
Source code in tinybig/koala/geometry/grid.py
get_w()
get_w_after_packing(cd_w)
Returns the width of the grid after packing with a specified center distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cd_w
|
int
|
Center distance along the width. |
required |
Returns:
Type | Description |
---|---|
int
|
The packed width. |
Raises:
Type | Description |
---|---|
ValueError
|
If the center distance is 0. |
Source code in tinybig/koala/geometry/grid.py
packing(patch, cd_h, cd_w, cd_d)
Packs the grid with patches using the specified center distances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch
|
cuboid | cylinder | sphere
|
The patch type to use for packing. |
required |
cd_h
|
int
|
Center distance along the height. |
required |
cd_w
|
int
|
Center distance along the width. |
required |
cd_d
|
int
|
Center distance along the depth. |
required |
Returns:
Type | Description |
---|---|
dict
|
A dictionary where keys are patch center coordinates and values are relative coordinates in the patch. |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the center distances is 0 or if the patch center is not at (0, 0, 0). |
Source code in tinybig/koala/geometry/grid.py
to_aggregation_matrix(packed_patch, n, across_universe=False, device='cpu', *args, **kwargs)
Creates an aggregation matrix from packed patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
packed_patch
|
dict
|
A dictionary of packed patches with their relative coordinates. |
required |
n
|
int
|
The total size of the grid. |
required |
across_universe
|
bool
|
If True, includes all universes in the aggregation. Defaults to False. |
False
|
device
|
str
|
The device to perform computation on (e.g., 'cpu' or 'cuda'). Defaults to 'cpu'. |
'cpu'
|
*args
|
Additional arguments for customization. |
()
|
|
**kwargs
|
Additional arguments for customization. |
()
|
Returns:
Type | Description |
---|---|
Tensor
|
A sparse COO tensor representing the aggregation matrix. |
Raises:
Type | Description |
---|---|
AssertionError
|
If row, column, and data lengths do not match. |
Source code in tinybig/koala/geometry/grid.py
to_attribute_index(coord, universe_id=0)
Converts a grid coordinate to a unique attribute index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coord
|
coordinate_3d
|
The grid coordinate to convert. |
required |
universe_id
|
int
|
The universe ID for the coordinate. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
int or None
|
The unique attribute index or |
Raises:
Type | Description |
---|---|
AssertionError
|
If the grid coordinates are not initialized or the input coordinate is None. |
Source code in tinybig/koala/geometry/grid.py
to_grid_coordinate(idx)
Converts an attribute index to a grid coordinate and universe ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
The attribute index to convert. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the |
Source code in tinybig/koala/geometry/grid.py
to_matrix(patch, packing_strategy=None, cd_h=None, cd_w=None, cd_d=None, interdependence_matrix_mode='padding', normalization=False, normalization_mode='row_column', across_universe=False, device='cpu', *args, **kwargs)
Constructs a matrix representation of the grid based on the specified parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch
|
Union[cuboid, cylinder, sphere]
|
The patch type to use for packing. |
required |
packing_strategy
|
str
|
The packing strategy to use. Defaults to None. |
None
|
cd_h
|
int
|
Center distance along the height. Defaults to None. |
None
|
cd_w
|
int
|
Center distance along the width. Defaults to None. |
None
|
cd_d
|
int
|
Center distance along the depth. Defaults to None. |
None
|
interdependence_matrix_mode
|
str
|
The mode for the matrix (e.g., 'padding' or 'aggregation'). Defaults to 'padding'. |
'padding'
|
normalization
|
bool
|
Whether to normalize the resulting matrix. Defaults to False. |
False
|
normalization_mode
|
str
|
The normalization mode (e.g., 'row', 'column', 'row_column'). Defaults to 'row_column'. |
'row_column'
|
across_universe
|
bool
|
If True, includes all universes in the matrix. Defaults to False. |
False
|
device
|
str
|
The device to perform computation on (e.g., 'cpu' or 'cuda'). Defaults to 'cpu'. |
'cpu'
|
*args
|
Additional arguments for customization. |
()
|
|
**kwargs
|
Additional arguments for customization. |
()
|
Returns:
Type | Description |
---|---|
Tensor
|
A matrix representation of the grid. |
Raises:
Type | Description |
---|---|
ValueError
|
If patch center distances are not positive or if an unknown mode is provided. |
Source code in tinybig/koala/geometry/grid.py
to_padding_matrix(packed_patch, n, across_universe=False, device='cpu', *args, **kwargs)
Creates a padding matrix from packed patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
packed_patch
|
dict
|
A dictionary of packed patches with their relative coordinates. |
required |
n
|
int
|
The total size of the grid. |
required |
across_universe
|
bool
|
If True, includes all universes in the padding. Defaults to False. |
False
|
device
|
str
|
The device to perform computation on (e.g., 'cpu' or 'cuda'). Defaults to 'cpu'. |
'cpu'
|
*args
|
Additional arguments for customization. |
()
|
|
**kwargs
|
Additional arguments for customization. |
()
|
Returns:
Type | Description |
---|---|
Tensor
|
A sparse COO tensor representing the padding matrix. |
Raises:
Type | Description |
---|---|
AssertionError
|
If row, column, and data lengths do not match. |