Ndarray
MultiArrayNumpyFile
dataclass
Source code in mbodied/types/ndarray.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
load()
Load the NDArray stored in the given path within the given key.
Returns:
NDArray
Source code in mbodied/types/ndarray.py
165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
NumpyArray
Bases: Generic[T]
, NDArray[Any]
Pydantic validation for shape and dtype. Specify shape with a tuple of integers, "*" or Any
for any size.
If the last dimension is a type (e.g. np.uint8), it will validate the dtype as well.
Examples:
- NumpyArray[1, 2, 3] will validate a 3D array with shape (1, 2, 3).
- NumpyArray[Any, "*", Any] will validate a 3D array with any shape.
- NumpyArray[3, 224, 224, np.uint8] will validate an array with shape (3, 224, 224) and dtype np.uint8.
Lazy loading and caching by default.
Usage:
from pydantic import BaseModel from embdata.ndarray import NumpyArray class MyModel(BaseModel): ... uint8_array: NumpyArray[np.uint8] ... must_have_exact_shape: NumpyArray[1, 2, 3] ... must_be_3d: NumpyArray["", "", ""] # NumpyArray[Any, Any, Any] also works. ... must_be_1d: NumpyArray["",] # NumpyArray[Any,] also works.
Source code in mbodied/types/ndarray.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
|
NumpyModel
Bases: BaseModel
Source code in mbodied/types/ndarray.py
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 |
|
load(output_directory, object_id, *, pre_load_modifier=None)
classmethod
Load NumpyModel instance.
Parameters
output_directory: DirectoryPath The root directory where all model instances of interest are stored object_id: String The ID of the model instance pre_load_modifier: Callable[[dict[str, Any]], dict[str, Any]] | None Optional function that modifies the loaded arrays
Returns:
NumpyModel instance
Source code in mbodied/types/ndarray.py
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 |
|
create_array_validator(shape, dtype, labels)
Creates a validator function for NumPy arrays with a specified shape and data type.
Source code in mbodied/types/ndarray.py
537 538 539 540 541 542 543 |
|
get_numpy_json_schema(_field_core_schema, _handler, shape=None, data_type=None, labels=None)
Generates a JSON schema for a NumPy array field within a Pydantic model.
This function constructs a JSON schema definition compatible with Pydantic models that are intended to validate NumPy array inputs. It supports specifying the data type and dimensions of the NumPy array, which are used to construct a schema that ensures input data matches the expected structure and type.
Parameters
_field_core_schema : core_schema.CoreSchema
The core schema component of the Pydantic model, used for building basic schema structures.
_handler : GetJsonSchemaHandler
A handler function or object responsible for converting Python types to JSON schema components.
shape : Optional[List[PositiveInt]], optional
The expected shape of the NumPy array. If specified, the schema will enforce that the input
data_type : Optional[SupportedDTypes], optional
The expected data type of the NumPy array elements. If specified, the schema will enforce
that the input array's data type is compatible with this. If None
, any data type is allowed,
by default None.
Returns:
JsonSchemaValue A dictionary representing the JSON schema for a NumPy array field within a Pydantic model. This schema includes details about the expected array dimensions and data type.
Source code in mbodied/types/ndarray.py
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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
|
model_agnostic_load(output_directory, object_id, models, not_found_error=False, **load_kwargs)
Provided an Iterable containing possible models, and the directory where they have been dumped.
Load the first instance of model that matches the provided object ID.
Parameters
output_directory: DirectoryPath The root directory where all model instances of interest are stored object_id: String The ID of the model instance models: Iterable[type[NumpyModel]] All NumpyModel instances of interest, note that they should have differing names not_found_error: bool If True, throw error when the respective model instance was not found load_kwargs Key-word arguments to pass to the load function
Returns:
NumpyModel instance if found
Source code in mbodied/types/ndarray.py
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 |
|
np_general_all_close(arr_a, arr_b, rtol=1e-05, atol=1e-08)
Data type agnostic function to define if two numpy array have elements that are close.
Parameters
arr_a: npt.NDArray arr_b: npt.NDArray rtol: float See np.allclose atol: float See np.allclose
Returns:
Bool
Source code in mbodied/types/ndarray.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
validate_multi_array_numpy_file(v)
Validation function for loading numpy array from a name mapping numpy file.
Parameters
v: MultiArrayNumpyFile MultiArrayNumpyFile to load
Returns:
NDArray from MultiArrayNumpyFile
Source code in mbodied/types/ndarray.py
100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
validate_numpy_array_file(v)
Validate file path to numpy file by loading and return the respective numpy array.
Source code in mbodied/types/ndarray.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|