analysis_builder.py
AnalysisFileBuilder: Safe analysis file creation with lifecycle enforcement.
This module provides AnalysisFileBuilder, a context manager that enforces the CREATE → POPULATE → REGISTER lifecycle for analysis NWB files, preventing common user errors like forgetting registration or modifying registered files.
AnalysisFileBuilder
¶
Context manager for safe analysis file creation.
Enforces CREATE → POPULATE → REGISTER lifecycle with automatic state
tracking and fail-fast error messages.
The builder prevents common errors:
- Forgetting to call add() (auto-registration on __exit__)
- Calling helpers after registration (state checks with clear errors)
- Opening files in write mode after registration (prevented)
- Losing track of file state (explicit state machine)
- Creating orphaned files on exception (logged for cleanup)
Parameters
Parameters
analysis_table : AnalysisMixin instance
The table to create/register file in (master or custom)
nwb_file_name : str
Parent NWB file name
Attributes
Attributes
analysis_file_name : str
Name of created analysis file (set after __enter__)
nwb_file_name : str
Parent NWB file name
Examples
Examples
Basic usage with helper methods:
>>> 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 helper calls:
>>> with AnalysisNwbfile().build(nwb_file_name) as builder:
... builder.add_nwb_object(position_data, "position")
... builder.add_nwb_object(velocity_data, "velocity")
... file = builder.analysis_file_name
Direct NWB I/O for complex operations:
>>> with AnalysisNwbfile().build(nwb_file_name) as builder:
... io, nwbf = builder.open_nwb
... nwbf.add_unit(spike_times=times, id=unit_id) ... file = builder.analysis_file_name
See Also
See Also
AnalysisMixin.build : Factory method that creates this builder
Source code in src/spyglass/utils/mixins/analysis_builder.py
16 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 | |
__init__(analysis_table, nwb_file_name)
¶
Initialize builder with table and parent file name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_table
|
AnalysisMixin instance
|
The table to create/register file in |
required |
nwb_file_name
|
str
|
Parent NWB file name |
required |
Source code in src/spyglass/utils/mixins/analysis_builder.py
__enter__()
¶
Create analysis file (CREATE phase).
Returns:
| Name | Type | Description |
|---|---|---|
self |
AnalysisFileBuilder
|
Builder instance with analysis_file_name set |
Raises:
| Type | Description |
|---|---|
Exception
|
Any exception from underlying file creation |
Source code in src/spyglass/utils/mixins/analysis_builder.py
__exit__(exc_type, exc_val, exc_tb)
¶
Register file automatically if no exception (REGISTER phase).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
type or None
|
Exception type if exception occurred |
required |
exc_val
|
Exception or None
|
Exception instance if exception occurred |
required |
exc_tb
|
traceback or None
|
Exception traceback if exception occurred |
required |
Returns:
| Type | Description |
|---|---|
False
|
Never suppresses exceptions |
Source code in src/spyglass/utils/mixins/analysis_builder.py
open_nwb
property
¶
Tuple of (io, nwb) for currently open NWB file, or opens it if not already open.
register()
¶
Register the analysis file in the database.
This is called automatically by exit. Can also be called manually if needed (idempotent - does nothing if already registered).
Raises:
| Type | Description |
|---|---|
ValueError
|
If file is not yet created |
Source code in src/spyglass/utils/mixins/analysis_builder.py
close_and_write()
¶
Close open NWB file and write changes to disk.
This is used internally to ensure that any open NWB file is properly closed and written before registration. It can also be called manually if needed (e.g. to finalize changes before registration). If no NWB file is currently open, this method does nothing.
Source code in src/spyglass/utils/mixins/analysis_builder.py
add_nwb_object(nwb_object, table_name=None)
¶
Add NWB object to analysis file (POPULATE phase).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwb_object
|
pynwb object, DataFrame, or ndarray
|
Object to add to file scratch space |
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 |
|---|---|---|
object_id |
str
|
NWB object ID for retrieval |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called before create or after registration |
Source code in src/spyglass/utils/mixins/analysis_builder.py
add_units(units, units_valid_times, units_sort_interval, metrics=None)
¶
Add spike sorting units to analysis file (POPULATE phase).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
units
|
dict
|
Unit ID → spike times mapping |
required |
units_valid_times
|
dict
|
Unit ID → valid time intervals mapping |
required |
units_sort_interval
|
dict
|
Unit ID → sorting intervals mapping |
required |
metrics
|
dict
|
Unit ID → metrics mapping |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
units_object_id |
str
|
Object ID for units table |
waveforms_object_id |
str
|
Object ID for waveforms |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called before create or after registration |
Source code in src/spyglass/utils/mixins/analysis_builder.py
add_units_metrics(metrics)
¶
Add unit metrics to analysis file (POPULATE phase).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
dict
|
Metrics dictionary |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object_id |
str
|
NWB object ID |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called before create or after registration |
Source code in src/spyglass/utils/mixins/analysis_builder.py
add_units_waveforms(waveform_extractor)
¶
Add unit waveforms to analysis file (POPULATE phase).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
waveform_extractor
|
WaveformExtractor
|
Waveform extractor object |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object_id |
str
|
NWB object ID |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called before create or after registration |
Source code in src/spyglass/utils/mixins/analysis_builder.py
get_path()
¶
Get absolute filesystem path to analysis file.
Returns:
| Name | Type | Description |
|---|---|---|
path |
str
|
Absolute path to analysis file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If file not yet created |