Directory
datamate.directory ¶
This module exports the Directory
class, an array- and metadata-friendly view
into a directory.
Instances of the base Directory class have methods to simplify reading/writing collections of arrays.
Directory ¶
Array- and metadata-friendly view into a directory.
Provides a dictionary-like interface for working with arrays and metadata stored in a directory structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Optional[Path]
|
Path at which the Directory is/should be stored.
Can be relative to current |
required |
config
|
Optional[Dict[str, Any]]
|
Configuration dictionary. When including a
|
required |
Attributes:
Name | Type | Description |
---|---|---|
path |
Path
|
Path where Directory is stored.
Type: |
config |
Config
|
Directory configuration.
Type: |
meta |
Namespace
|
Metadata stored in |
status |
Literal['running', 'done', 'stopped']
|
Build status from metadata.
Type: |
parent |
Directory
|
Parent directory.
Type: |
Valid constructors
# Auto-name relative to root_dir:
Directory()
Directory(config={"type": "MyType"})
# Name relative to root_dir or absolute:
Directory("/path/to/dir")
Directory("/path/to/dir", config={"type": "MyType"})
After instantiation, Directories act as string-keyed mutable dictionaries containing: - ArrayFiles: Single-entry HDF5 files in SWMR mode - Paths: Non-array files in other formats - Directories: Subdirectories
Array-like numeric and byte-string data written via __setitem__
, __setattr__
, or
extend
is stored as an array file.
Example
# Create directory with arrays
dir = Directory("my_data")
dir["array1"] = np.array([1,2,3])
dir.array2 = np.array([[4,5],[6,7]])
# Access data
arr1 = dir["array1"] # Returns array([1,2,3])
arr2 = dir.array2 # Returns array([[4,5],[6,7]])
Source code in datamate/directory.py
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 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 |
|
Config ¶
Bases: Protocol
Protocol defining the configuration interface for Directory classes.
This protocol defines the structure of the config
argument to the
Directory
constructor, and provides type hints for the config
attribute of Directory
instances.
Note
Subclasses should implement this protocol to define their configuration
interface, or implement __init__
with typed parameters.
Source code in datamate/directory.py
190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
__new__ ¶
__new__() -> T
__new__(path: Union[str, Path]) -> T
__new__(config: ConfigType) -> T
__new__(path: Union[str, Path], config: ConfigType) -> T
__new__(_type, *args, **kwargs)
Implementation of overloaded constructors.
Source code in datamate/directory.py
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 |
|
__init__ ¶
__init__()
Implement to compile Directory
from a configuration.
Note
Subclasses can either implement Config
to determine the interface,
types and defaults of config
, or implement __init__
with keyword
arguments. If both are implemented, the config is created from the joined
interface as long as defaults are not conflicting.
Source code in datamate/directory.py
267 268 269 270 271 272 273 274 275 276 |
|
__init_subclass__ ¶
__init_subclass__(**kwargs)
Initializes a Directory subclass.
Automatically generates documentation for the subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional keyword arguments passed to parent init_subclass |
{}
|
Source code in datamate/directory.py
278 279 280 281 282 283 284 285 286 287 |
|
__len__ ¶
__len__()
Returns the number of public files in self.path
.
Non-public files (files whose names start with “_”) are not counted.
Returns:
Type | Description |
---|---|
int
|
Number of public files in the directory |
Source code in datamate/directory.py
325 326 327 328 329 330 331 332 333 |
|
__iter__ ¶
__iter__()
Yields field names corresponding to the public files in self.path
.
Entries it understands (subdirectories and HDF5 files) are yielded without extensions. Non-public files (files whose names start with “_”) are ignored.
Yields:
Type | Description |
---|---|
str
|
Field names for each public file |
Source code in datamate/directory.py
335 336 337 338 339 340 341 342 343 344 345 346 |
|
__copy__ ¶
__copy__()
Creates a shallow copy of the directory.
Returns:
Type | Description |
---|---|
Directory
|
New |
Source code in datamate/directory.py
348 349 350 351 352 353 354 |
|
keys ¶
keys()
Returns an iterator over public file names in the directory.
Returns:
Type | Description |
---|---|
Iterator[str]
|
Iterator yielding public file names |
Source code in datamate/directory.py
359 360 361 362 363 364 365 |
|
items ¶
items()
Returns an iterator over (key, value) pairs in the directory.
Returns:
Type | Description |
---|---|
Iterator[Tuple[str, ArrayFile]]
|
Iterator yielding tuples of (filename, file content) |
Source code in datamate/directory.py
367 368 369 370 371 372 373 374 |
|
from_df
classmethod
¶
from_df(df, dtypes, *args, **kwargs)
Create a Directory from a DataFrame by splitting into column arrays.
Each column is stored as a separate HDF5 array with specified dtype. This is different from storing the DataFrame directly, which uses CSV format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Source DataFrame |
required |
dtypes
|
Dict[str, Any]
|
Dictionary mapping column names to numpy dtypes |
required |
*args
|
Additional arguments passed to |
()
|
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
T
|
|
Examples:
df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z']})
dtypes = {'a': np.int64, 'b': 'S'}
# Store columns as separate arrays
dir1 = Directory.from_df(df, dtypes)
# Results in:
# dir1/
# ├── a.h5 # array([1, 2, 3])
# └── b.h5 # array([b'x', b'y', b'z'])
# Store as single CSV
dir2 = Directory()
dir2['data'] = df
# Results in:
# dir2/
# └── data.csv
Source code in datamate/directory.py
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 |
|
update ¶
update(other, suffix='')
Updates self with items of other and appends an optional suffix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Union[Dict, Directory]
|
Dictionary or Directory to copy items from |
required |
suffix
|
str
|
Optional string to append to copied keys |
''
|
Source code in datamate/directory.py
420 421 422 423 424 425 426 427 428 429 |
|
move ¶
move(dst)
Moves directory to new location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dst
|
Union[str, Path]
|
Destination path |
required |
Returns:
Type | Description |
---|---|
Directory
|
New |
Source code in datamate/directory.py
431 432 433 434 435 436 437 438 439 440 441 |
|
rmtree ¶
rmtree(y_n=None)
Recursively deletes the directory after confirmation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_n
|
Optional[str]
|
Optional pre-supplied confirmation (‘y’ or ‘n’). If not provided, will prompt user interactively |
None
|
Source code in datamate/directory.py
443 444 445 446 447 448 449 450 451 452 |
|
__truediv__ ¶
__truediv__(other)
Implements path-like division operator for accessing entries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
str
|
Key to access |
required |
Returns:
Type | Description |
---|---|
Any
|
Same as |
Source code in datamate/directory.py
464 465 466 467 468 469 470 471 472 473 |
|
__getitem__ ¶
__getitem__(key)
Returns ArrayFile
, Path
, or Directory
corresponding to self.path/key
.
HDF5 files are returned as ArrayFile
s, other files as Path
s, and
directories and nonexistent entries as (possibly empty) Directory
s.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Name of the entry to retrieve |
required |
Returns:
Type | Description |
---|---|
Any
|
The requested entry as an appropriate type |
Note
Attribute access syntax is also supported, and occurrences of __
in
key
are transformed into .
, to support accessing encoded files as
attributes (i.e. Directory['name.ext']
is equivalent to
Directory.name__ext
).
Source code in datamate/directory.py
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 |
|
__setitem__ ¶
__setitem__(key, val)
Writes an ArrayFile
, Path
, or Directory
to self.path/key
np.ndarray
-like objects are written as ArrayFiles
, Path
-like
objects are written as Path
s, and string-keyed mappings are
written as subDirectorys.
Attribute access syntax is also supported, and occurrences of “__” in
key
are transformed into “.”, to support accessing encoded files as
attributes (i.e. Directory['name.ext'] = val
is equivalent to
Directory.name__ext = val
).
Source code in datamate/directory.py
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 |
|
__delitem__ ¶
__delitem__(key)
Deletes the entry at self.path/key
Attribute access syntax is also supported, and occurrences of “__” in
key
are transformed into “.”, to support accessing encoded files as
attributes (i.e. del Directory['name.ext']
is equivalent to
del Directory.name__ext
).
Source code in datamate/directory.py
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 |
|
__eq__ ¶
__eq__(other)
Returns True if self
and other
are equal.
Two Directories are equal if they have the same keys and the same values for each key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
object
|
Object to compare against |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the directories are equal |
Raises:
Type | Description |
---|---|
ValueError
|
If comparing |
Source code in datamate/directory.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
|
__neq__ ¶
__neq__(other)
Returns True if directories are not equal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
object
|
Object to compare against |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the directories are not equal |
Source code in datamate/directory.py
646 647 648 649 650 651 652 653 654 655 |
|
diff ¶
diff(other)
Returns a dictionary of differences between this directory and another.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Directory
|
Directory to compare against |
required |
Returns:
Type | Description |
---|---|
Dict[str, List[str]]
|
Dictionary with two keys - the name of self and other. Values are lists of |
Dict[str, List[str]]
|
strings describing differences between corresponding entries. |
Source code in datamate/directory.py
657 658 659 660 661 662 663 664 665 666 667 668 |
|
extend ¶
extend(key, val)
Extends an array, file or directory at the given key.
Extending arrays performs concatenation along the first axis, extending files performs byte-level concatenation, and extending directories extends their fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Name of the entry to extend |
required |
val
|
object
|
Value to append to the existing entry. Can be |
required |
Note
Files corresponding to self[key]
are created if they do not already exist.
Source code in datamate/directory.py
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 |
|
tree ¶
tree(
level=-1,
length_limit=None,
verbose=True,
last_modified=True,
limit_to_directories=False,
)
Prints a tree representation of the directory structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
int
|
Maximum depth to display (-1 for unlimited) |
-1
|
length_limit
|
Optional[int]
|
Maximum number of entries to show per directory |
None
|
verbose
|
bool
|
Whether to show detailed information |
True
|
last_modified
|
bool
|
Whether to show last modification times |
True
|
limit_to_directories
|
bool
|
Whether to only show directories |
False
|
Source code in datamate/directory.py
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 |
|
__manual_config ¶
__manual_config(config, status=None)
Overriding config stored in _meta.yaml.
config (Dict): update for meta.config status (str): status if config did not exist before, i.e. _overrid_config is used to store a _meta.yaml for the first time instead of build.
Source code in datamate/directory.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 |
|
check_size ¶
check_size(
warning_at=20 * 1024**3,
print_size=False,
*,
raise_on_warning=False,
)
Checks the total size of the directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
warning_at
|
int
|
Size in bytes at which to issue a warning |
20 * 1024 ** 3
|
print_size
|
bool
|
Whether to print the directory size |
False
|
raise_on_warning
|
bool
|
Whether to raise exception instead of warning |
False
|
Returns:
Type | Description |
---|---|
int
|
Total size in bytes |
Raises:
Type | Description |
---|---|
ValueError
|
if directory size exceeds warning_at and raise_on_warning is True |
Source code in datamate/directory.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 |
|
to_df ¶
to_df(dtypes=None)
Reconstruct a DataFrame from HDF5 column arrays in this directory.
Combines all equal-length, single-dimensional HDF5 datasets into DataFrame columns. Results are cached to avoid expensive recomputation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtypes
|
Optional[Dict[str, Any]]
|
Optional dictionary mapping column names to numpy dtypes |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
|
Note
This is the complement to from_df()
. While direct DataFrame assignment
stores as CSV, from_df()
splits columns into HDF5 arrays which can be
recombined using this method.
Source code in datamate/directory.py
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
|
to_dict ¶
to_dict()
Returns a DataFrame from all equal length, single-dim .h5 datasets in self.path.
Source code in datamate/directory.py
939 940 941 942 943 944 945 946 947 948 |
|
mtime ¶
mtime()
Returns the last modification time of the directory.
Returns:
Type | Description |
---|---|
datetime
|
Datetime object representing last modification time |
Source code in datamate/directory.py
950 951 952 953 954 955 956 |
|
NonExistingDirectory ¶
Bases: type
Directory metaclass to allow create non-existing Directory instances.
Source code in datamate/directory.py
120 121 122 123 124 |
|