common_nwbfile.py
Nwbfile
¶
Bases: SpyglassMixin, Manual
Source code in src/spyglass/common/common_nwbfile.py
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 | |
insert_from_relative_file_name(nwb_file_name)
classmethod
¶
Insert a new session from an existing NWB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The relative path to the NWB file. |
required |
Source code in src/spyglass/common/common_nwbfile.py
get_abs_path(nwb_file_name, new_file=False, **kwargs)
classmethod
¶
Return absolute path for a stored raw NWB file given file name.
The SPYGLASS_BASE_DIR must be set, either as an environment or part of dj.config['custom']. See spyglass.settings.load_config
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of an NWB file that has been inserted into the Nwbfile() table. May be file substring. May include % wildcard(s). |
required |
new_file
|
bool
|
Adding a new file to Nwbfile table. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
nwb_file_abspath |
str
|
The absolute path for the given file name. |
Source code in src/spyglass/common/common_nwbfile.py
add_to_lock(nwb_file_name)
staticmethod
¶
Add the specified NWB file to the list of locked items.
The NWB_LOCK_FILE environment variable must be set to the path of the lock file, listing locked NWB files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of an NWB file in the Nwbfile table. |
required |
Source code in src/spyglass/common/common_nwbfile.py
cleanup(delete_files=False)
staticmethod
¶
Remove the filepath entries for NWB files that are not in use.
This does not delete the files themselves unless delete_files=True is specified. Run this after deleting the Nwbfile() entries themselves.
Source code in src/spyglass/common/common_nwbfile.py
AnalysisRegistry
¶
Bases: Manual
Central registry tracking all custom AnalysisNwbfile tables.
This table maintains a record of all team-specific AnalysisNwbfile tables to enable coordinated cleanup, export operations, and cross-table queries. Tables are auto-registered when declared via SpyglassAnalysis mixin.
Key Methods: get_class(prefix) - Get AnalysisNwbfile class for a specific team prefix get_all_classes() - Get all registered AnalysisNwbfile class objects get_tracked_files() - Get all files tracked across all custom tables clear_cache() - Clear the class cache (useful for testing)
Usage: from spyglass.common import AnalysisRegistry
# View all registered tables
AnalysisRegistry().fetch()
# Get a specific team's table
MyTeamAnalysis = AnalysisRegistry().get_class("myteam")
# Get an instance
my_team_analysis = MyTeamAnalysis()
# Use enhanced helper methods
my_team_analysis.get_prefix() # 'myteam'
Source code in src/spyglass/common/common_nwbfile.py
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 | |
insert1(key, **kwargs)
¶
Auto-add created_by if not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str or dict
|
The full_table_name as a string or a dict with the key 'full_table_name'. |
required |
kwargs
|
additional arguments to pass to insert1.
|
|
{}
|
Source code in src/spyglass/common/common_nwbfile.py
get_class(key)
¶
Return the class object for the given full_table_name, uninitialized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str or dict
|
The prefix or full_table_name as a string or a dict with the key 'full_table_name'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
class_obj |
type or None
|
The class object for the given full_table_name, or None. |
Source code in src/spyglass/common/common_nwbfile.py
all_classes
property
¶
Return all registered analysis table class objects, initialized.
Returns:
| Name | Type | Description |
|---|---|---|
class_objs |
list of dj.FreeTable
|
A list of all registered analysis table class objects. |
clear_cache()
classmethod
¶
Clear the class cache.
Useful for testing or when custom tables are modified. After clearing, the next call to get_class() will recreate the class objects.
Examples:
Source code in src/spyglass/common/common_nwbfile.py
get_externals(store='analysis')
¶
Return external table objects for all registered analysis schemas.
External tables are used to manage externally stored files (e.g., on S3).
Each custom AnalysisNwbfile schema has a corresponding external table
named {prefix}_nwbfile.~external_analysis.
Used for updating externals after file surgeries or migrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
str
|
The external store name to use. Default is "analysis". This should match a configured store in dj.config['stores']. |
'analysis'
|
Returns:
| Name | Type | Description |
|---|---|---|
externals |
list of dj.external.ExternalTable
|
A list of ExternalTable objects for all registered schemas. |
Example
from spyglass.common import AnalysisRegistry registry = AnalysisRegistry() externals = registry.get_externals() for ext in externals: ... print(ext.database)
Source code in src/spyglass/common/common_nwbfile.py
block_new_inserts(dry_run=False)
¶
Block new inserts into all registered analysis tables.
Creates BEFORE INSERT triggers on all registered custom analysis tables to prevent data modifications during maintenance operations. Refuses to adopt an existing trigger because it may belong to another cleanup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run
|
bool
|
If True, log blocking without making changes. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If blocker inspection or trigger creation fails, or if any blocker already exists. A partial creation failure may leave some tables blocked because triggers do not carry per-run ownership. |
Source code in src/spyglass/common/common_nwbfile.py
unblock_new_inserts()
¶
Unblock new inserts into all registered analysis tables.
Removes BEFORE INSERT triggers from all registered custom analysis tables, re-enabling normal insert operations.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If any trigger removal fails. |
Source code in src/spyglass/common/common_nwbfile.py
AnalysisNwbfile
¶
Bases: SpyglassAnalysis, Manual
Source code in src/spyglass/common/common_nwbfile.py
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 1002 1003 1004 1005 1006 1007 1008 1009 | |
cleanup(dry_run=False, *, max_delete_fraction=0.9, max_delete_to_tracked_ratio=10.0, min_file_age_hours=24.0)
¶
Clean up common and all custom AnalysisNwbfile tables.
Removes orphaned analysis files across both common and custom tables. A file is considered orphaned if it has no downstream foreign key references. This method coordinates cleanup across all registered custom AnalysisNwbfile tables to prevent premature deletion.
Process: 1. Discover custom tables and snapshot filesystem/tracking state. 2. Validate the complete untracked-file deletion plan. 3. Delete eligible filesystem candidates from that snapshot. 4. For each custom analysis table: a. Delete orphaned entries (no downstream references) b. Clean up unused external file entries c. Remove valid entries from common orphan list 5. Delete remaining common orphans. 6. Clean up common external entries without deleting their files.
Example: from spyglass.common import AnalysisNwbfile
# Run cleanup across all tables
AnalysisNwbfile().cleanup(dry_run=False)
Note: This is a destructive operation. Ensure you have backups before running cleanup on production databases. File deletions cannot be undone.
See Also: docs/src/ForDevelopers/Management.md for detailed cleanup guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run
|
bool
|
If True, perform a non-destructive dry run: log and report all cleanup actions without deleting database entries or files. If False, apply the cleanup changes, including deleting orphaned entries and associated files. |
False
|
max_delete_fraction
|
float
|
Maximum fraction of eligible analysis NWB files that may be deleted by filesystem cleanup. The eligible set is the planned deletions plus scanned files recognized as tracked; age-deferred files are excluded. Set high by default (0.9) so it catches a catastrophically misconfigured analysis directory rather than routine large cleanups. Defaults to 0.9. |
0.9
|
max_delete_to_tracked_ratio
|
float
|
Maximum ratio of filesystem cleanup deletions to tracked analysis
files found in the scan. At the default |
10.0
|
min_file_age_hours
|
float
|
Untracked files newer than this are deferred to the next cleanup rather than deleted, protecting work that exists on disk but is not yet registered -- notably a file written to another volume and symlinked in before its row is inserted. Defaults to 24.0. The target modification time is the age basis. Pass 0 only for intentional immediate cleanup. |
24.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a numeric safety limit is non-finite or outside its bounds. |
RuntimeError
|
If insert blocking or unblocking fails, a destructive plan is refused, or registry/database cleanup fails. |
Source code in src/spyglass/common/common_nwbfile.py
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 | |
check_all_files(resolve_tables=False, verbose=False)
¶
Check files across all analysis tables for issues.
Iterates through common and all custom AnalysisNwbfile tables, checking file existence and readability. Populates AnalysisFileIssues with any problems found. This monitoring operation does not delete files, but it does write issue rows and can be run independently of cleanup at different frequencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolve_tables
|
bool
|
After all issues are collected, populate the table field for each issue by querying downstream child tables. More efficient than per-table resolution since children are fetched once per analysis table across all newly inserted issues. Default False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
results |
dict
|
Dictionary mapping table names to issue counts |
Example
from spyglass.common import AnalysisNwbfile results = AnalysisNwbfile().check_all_files(resolve_tables=True) print(f"Total issues: {sum(results.values())}")
See Also
AnalysisFileIssues : Table that stores detected issues AnalysisFileIssues.resolve_table_refs : Populate table field on demand