analysis.py
AnalysisMixin
¶
Bases: BaseMixin
Provides analysis file management for AnalysisNwbfile tables.
This mixin provides core functionality for both common and custom AnalysisNwbfile tables including file creation, NWB object management, cleanup/orphan detection, and export integration (copy-to-common).
Key Methods: build() - RECOMMENDED: Create builder for safe file creation (context manager) create() - Legacy: Create new analysis file with unique suffix add() - Legacy: Register analysis file in table get_abs_path() - Get absolute path for a single analysis file get_abs_paths() - Batch fetch absolute file paths get_prefix() - Get database prefix (e.g., 'myteam') cleanup() - Remove orphaned files across all custom tables _copy_to_common() - Copy entries to common table during export
The build() method returns an AnalysisFileBuilder context manager that enforces the CREATE → POPULATE → REGISTER lifecycle, preventing common errors like forgetting registration or modifying registered files.
This mixin is used by SpyglassAnalysis for custom tables and directly inherited by the common AnalysisNwbfile table.
Source code in src/spyglass/utils/mixins/analysis.py
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 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 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 | |
get_prefix()
¶
Get the database prefix for this analysis table.
Returns the first part of the schema name before the underscore. For example, 'myteam_nwbfile' returns 'myteam'.
Returns:
| Type | Description |
|---|---|
str
|
Database prefix (e.g., 'common', 'myteam', username) |
Examples:
>>> from spyglass.common.custom_nwbfile import AnalysisNwbfile
>>> AnalysisNwbfile().get_prefix()
'yourusername'
Source code in src/spyglass/utils/mixins/analysis.py
create(nwb_file_name, recompute_file_name=None, alternate_dir=None, restrict_permission=False)
¶
Open the NWB file, create copy, write to disk and return new name.
Note that this does NOT add the file to the schema; that needs to be done after data are written to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of an NWB file to be copied. |
required |
recompute_file_name
|
str
|
The name of the file to be regenerated. Defaults to None. |
None
|
alternate_dir
|
(Union[str, Path], Optional)
|
An alternate directory to store the file. Defaults to analysis_dir. |
None
|
restrict_permission
|
bool
|
Default False, no permission restriction (666). If True, restrict write permissions to owner only. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
analysis_file_name |
str
|
The name of the new NWB file. |
Source code in src/spyglass/utils/mixins/analysis.py
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 | |
__get_new_file_name(nwb_file_name)
classmethod
¶
Generate a new unique file name based on the original NWB file name.
Adds a random string of 10 uppercase letters and digits to the base name of the original NWB file. Ensures that the new file name is not already in this table and that the file does not already exist in the analysis directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of the original NWB file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
analysis_file_name |
str
|
The name of the new NWB file. |
Source code in src/spyglass/utils/mixins/analysis.py
__get_analysis_file_dir(fname)
classmethod
¶
Strip off final underscore and remaining chars, return the result.
__get_file_parent(fname, relative=True)
classmethod
¶
Get the parent dir name for an analysis NWB file.
__get_analysis_path(fname, relative=False)
classmethod
¶
Get the path for an analysis NWB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
relative
|
bool
|
If true, return the path relative to analysis_dir. Defaults False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
path |
str
|
The path for the analysis NWB file. |
Source code in src/spyglass/utils/mixins/analysis.py
copy(nwb_file_name)
classmethod
¶
Make a copy of an analysis NWB file.
Note that this does NOT add the file to the schema; that needs to be done after data are written to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of the analysis NWB file to be copied. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
analysis_file_name |
str
|
The name of the new NWB file. |
Source code in src/spyglass/utils/mixins/analysis.py
add(nwb_file_name, analysis_file_name)
¶
Add the specified file to table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
The name of the parent NWB file. |
required |
analysis_file_name
|
str
|
The name of the analysis NWB file that was created. |
required |
Source code in src/spyglass/utils/mixins/analysis.py
build(nwb_file_name)
¶
Create a builder for safe analysis file creation.
Returns context manager that handles CREATE → POPULATE → REGISTER lifecycle with automatic state tracking and error prevention.
This is the recommended way to create analysis files. The builder: - Automatically registers files on successful exit - Prevents modification of registered files (state checks) - Logs failed files for cleanup on exceptions - Provides clear error messages for invalid operations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_file_name
|
str
|
Parent NWB file name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
builder |
AnalysisFileBuilder
|
Context manager for file lifecycle |
Examples:
Basic usage:
>>> with AnalysisNwbfile().build(nwb_file_name) as builder:
... builder.add_nwb_object(my_data, "results")
... file = builder.analysis_file_name
# File automatically registered on exit!
Multiple operations:
>>> with AnalysisNwbfile().build(nwb_file_name) as builder:
... builder.add_nwb_object(position, "position")
... builder.add_nwb_object(velocity, "velocity")
... file = builder.analysis_file_name
Direct I/O for complex cases:
>>> with AnalysisNwbfile().build(nwb_file_name) as builder:
... io, nwbf = builder.open_nwb
... nwbf.add_unit(spike_times=times, id=unit_id)
See Also
AnalysisFileBuilder : Full API documentation create : Legacy method for file creation add : Legacy method for registration
Source code in src/spyglass/utils/mixins/analysis.py
get_abs_path(analysis_nwb_file_name, from_schema=False)
classmethod
¶
Return the absolute path for an analysis NWB file given the name.
This method uses a multi-step fallback strategy to locate files: 1. If from_schema=True: Query external table (skips checksum/existence) 2. Check database table entry (validates checksum if file exists) 3. Check base analysis directory (for legacy flat structure) 4. Generate new subdirectory path (modern structure)
The spyglass config from settings.py must be set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_nwb_file_name
|
str
|
The name of the NWB file in this table. |
required |
from_schema
|
bool
|
If true, get the file path from the schema externals table, skipping checksum and file existence checks. Used internally during fetch operations. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
analysis_nwb_file_abspath |
str
|
The absolute path for the given file name. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
Raised by DataJoint when file exists in database but not on disk during checksum validation (caught internally). |
Examples:
>>> from spyglass.common import AnalysisNwbfile
>>> path = AnalysisNwbfile.get_abs_path("file_ABC123.nwb")
>>> # Returns: '/data/analysis/file_ABC/file_ABC123.nwb'
>>> # Skip checksum for faster lookups (internal use)
>>> AnalysisNwbfile.get_abs_path("file_ABC123.nwb", from_schema=True)
Source code in src/spyglass/utils/mixins/analysis.py
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 | |
get_abs_paths(**kwargs)
¶
Fetch absolute paths for all analysis files in this restriction.
Convenience method for batch path operations. Fetches all file names and resolves them to absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Additional arguments passed to fetch() |
{}
|
Returns:
| Type | Description |
|---|---|
list of str
|
Absolute paths to analysis files |
Examples:
>>> from spyglass.common import AnalysisNwbfile
>>> paths = (AnalysisNwbfile & restriction).get_abs_paths()
>>> # ['/data/analysis/file1.nwb', '/data/analysis/file2.nwb']
Source code in src/spyglass/utils/mixins/analysis.py
add_nwb_object(analysis_file_name, nwb_object, table_name=None)
¶
Add an NWB object to the analysis file and return the NWB object ID
Adds object to the scratch space of the NWB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
nwb_object
|
NWBDataInterface
|
The NWB object created by PyNWB. |
required |
table_name
|
str
|
The name of the pynwb object made from a passed dataframe or array. Defaults to "pandas_table" or "numpy_array" for dataframes and arrays respectively. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
nwb_object_id |
str
|
The NWB object ID of the added object. |
Source code in src/spyglass/utils/mixins/analysis.py
get_hash(analysis_file_name, path=None, from_schema=False, precision_lookup=None, return_hasher=False, stored_hash=None, resolve=False)
¶
Return the hash of the file contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
path
|
(Path, Optional)
|
If provided, hash this path directly instead of resolving via
|
None
|
from_schema
|
(bool, Optional)
|
If true, get the file path from the schema externals table, skipping checksum and file existence checks. Defaults to False. |
False
|
precision_lookup
|
(dict, Optional)
|
A dictionary of object names and rounding precisions, dictating the level of precision to which the data should be rounded before hashing. Defaults to None, no rounding. |
None
|
return_hasher
|
Optional[bool]
|
If true, return the hasher object instead of the hash. Defaults to False. |
False
|
stored_hash
|
(str, Optional)
|
If provided, compare the computed hash against this value and warn on mismatch. Intended for silent in-place regeneration (path B). |
None
|
resolve
|
(bool, Optional)
|
If true, update the external table entry after hashing so DataJoint's file checksum stays in sync. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
hash |
[str, NwbfileHasher]
|
The hash of the file contents or the hasher object itself. |
Source code in src/spyglass/utils/mixins/analysis.py
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 | |
add_units(analysis_file_name, units, units_valid_times, units_sort_interval, metrics=None, units_waveforms=None, labels=None)
¶
Add units to analysis NWB file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
units
|
dict
|
keys are unit ids, values are spike times |
required |
units_valid_times
|
dict
|
Dictionary of units and valid times with unit ids as keys. |
required |
units_sort_interval
|
dict
|
Dictionary of units and sort_interval with unit ids as keys. |
required |
units_waveforms
|
dict
|
Dictionary of unit waveforms with unit ids as keys. |
None
|
metrics
|
dict
|
Cluster metrics. |
None
|
labels
|
dict
|
Curation labels for clusters |
None
|
Returns:
| Type | Description |
|---|---|
units_object_id, waveforms_object_id : str, str
|
The NWB object id of the Units object and the object id of the waveforms object ('' if None) |
Source code in src/spyglass/utils/mixins/analysis.py
add_units_waveforms(analysis_file_name, waveform_extractor, metrics=None, labels=None)
¶
Add units to analysis NWB file along with the waveforms
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
waveform_extractor
|
si.WaveformExtractor object
|
|
required |
metrics
|
dict
|
Cluster metrics. |
None
|
labels
|
dict
|
Curation labels for clusters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
units_object_id |
str
|
The NWB object id of the Units object |
Source code in src/spyglass/utils/mixins/analysis.py
add_units_metrics(analysis_file_name, metrics)
¶
Add units to analysis NWB file along with the waveforms
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
metrics
|
dict
|
Cluster metrics. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
units_object_id |
str
|
The NWB object id of the Units object |
Source code in src/spyglass/utils/mixins/analysis.py
get_electrode_indices(analysis_file_name, electrode_ids)
classmethod
¶
Returns indices of the specified electrode_ids for an analysis file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_file_name
|
str
|
The name of the analysis NWB file. |
required |
electrode_ids
|
numpy array or list
|
Array or list of electrode IDs. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
electrode_indices |
numpy array
|
Array of indices in the electrodes table for the given electrode IDs. |
Source code in src/spyglass/utils/mixins/analysis.py
cleanup_external(dry_run=False, delete_external_files=False)
¶
Remove the filepath entries for NWB files that are not in use.
Because an unused file in the common may be in use in a custom table, we never want to delete external files. Instead, the common handles orphan detection and deletion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run
|
bool
|
If true, only return the unused files without deleting them. Defaults to False. |
False
|
delete_external_files
|
bool
|
If true, delete the external files from disk. Defaults to False. |
False
|