utils
lacuna.utils
¶
Utility functions for data preprocessing and connectome preparation.
This module provides tools for working with neuroimaging datasets and preparing data for lesion network mapping analyses.
Key Components
GSP1000 Utilities: - create_connectome_batches: Convert GSP1000 data to optimized HDF5 batches - validate_connectome_batches: Verify integrity of batch files
Logging Utilities: - ConsoleLogger: Consistent console logger for user-facing messages - log_section, log_info, log_success, log_warning, log_error, log_progress: Convenience functions
Suggestion Utilities: - suggest_similar: Find similar strings for error message suggestions - format_suggestions: Format suggestions for error messages
ConsoleLogger
¶
Consistent console logger for user-facing messages.
Uses the standard Python logging module for output, ensuring consistent formatting with timestamps and module names across all Lacuna modules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If True, print messages. If False, silent mode (no output). |
True
|
width
|
int
|
Width for section headers |
70
|
indent
|
str
|
Indentation string for nested messages |
" "
|
name
|
str
|
Logger name for the Python logging module |
"lacuna"
|
Examples:
>>> logger = ConsoleLogger(verbose=True)
>>> logger.section("PROCESSING DATA")
2026-01-15 10:00:00 - lacuna - INFO - ============================================
2026-01-15 10:00:00 - lacuna - INFO - PROCESSING DATA
2026-01-15 10:00:00 - lacuna - INFO - ============================================
>>> logger.info("Loading connectome...")
2026-01-15 10:00:00 - lacuna - INFO - Loading connectome...
>>> logger.success("Analysis complete", details={"subjects": 10, "time": 42.3})
2026-01-15 10:00:00 - lacuna - INFO - Analysis complete
2026-01-15 10:00:00 - lacuna - INFO - subjects: 10
2026-01-15 10:00:00 - lacuna - INFO - time: 42.3
Source code in src/lacuna/utils/logging.py
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 | |
__init__(verbose=False, width=70, indent=' ', name='lacuna.analysis')
¶
Initialize console logger.
Source code in src/lacuna/utils/logging.py
blank_line()
¶
debug(message, indent_level=0)
¶
Print a debug message (only shown with -vv verbosity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Debug message |
required |
indent_level
|
int
|
Indentation level (0, 1, 2, ...) |
0
|
Examples:
Source code in src/lacuna/utils/logging.py
error(message, indent_level=0)
¶
Print an error message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message |
required |
indent_level
|
int
|
Indentation level |
0
|
Examples:
Source code in src/lacuna/utils/logging.py
info(message, indent_level=0)
¶
Print an informational message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Information message |
required |
indent_level
|
int
|
Indentation level (0, 1, 2, ...) |
0
|
Examples:
Source code in src/lacuna/utils/logging.py
progress(message, current=None, total=None, percent=None, indent_level=0)
¶
Print a progress update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Progress message |
required |
current
|
int
|
Current item number |
None
|
total
|
int
|
Total items |
None
|
percent
|
float
|
Completion percentage (0-100) |
None
|
indent_level
|
int
|
Indentation level |
0
|
Examples:
>>> logger.progress("Processing batch", current=3, total=10)
>>> logger.progress("Loading data", percent=65.5)
Source code in src/lacuna/utils/logging.py
result_summary(title, metrics, indent_level=0)
¶
Print a formatted summary of results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Summary title |
required |
metrics
|
dict
|
Dictionary of metric name: value pairs |
required |
indent_level
|
int
|
Indentation level |
0
|
Examples:
>>> logger.result_summary("Analysis Results", {
... "Mean correlation": 0.4523,
... "Std correlation": 0.1234,
... "Range": "[-0.45, 0.89]"
... })
Source code in src/lacuna/utils/logging.py
section(title)
¶
Print a major section header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Section title |
required |
Examples:
Source code in src/lacuna/utils/logging.py
subsection(title)
¶
Print a minor subsection header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Subsection title |
required |
Examples:
Source code in src/lacuna/utils/logging.py
success(message, details=None, indent_level=0)
¶
Print a success message with optional details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Success message |
required |
details
|
dict
|
Dictionary of key-value pairs to display |
None
|
indent_level
|
int
|
Indentation level |
0
|
Examples:
Source code in src/lacuna/utils/logging.py
warning(message, indent_level=0)
¶
Print a warning message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Warning message |
required |
indent_level
|
int
|
Indentation level |
0
|
Examples:
Source code in src/lacuna/utils/logging.py
create_connectome_batches(gsp_dir, mask_path, output_dir, subjects_per_batch=50, pattern='sub-*/func/*bld001_rest_*_finalmask.nii.gz', verbose=False)
¶
Create HDF5 batch files from GSP1000 functional data.
Scans a directory of functional NIfTI files, extracts time-series from within a brain mask, and saves the data into multiple smaller HDF5 batch files optimized for memory-efficient lesion network mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gsp_dir
|
str or Path
|
Directory containing GSP1000 subject functional data. Expected structure: sub-/func/bld001_rest_*_finalmask.nii.gz |
required |
mask_path
|
str or Path
|
Path to brain mask NIfTI file (e.g., MNI152_T1_2mm_Brain_Mask.nii.gz). Defines which voxels to extract. |
required |
output_dir
|
str or Path
|
Directory where HDF5 batch files will be saved. |
required |
subjects_per_batch
|
int
|
Number of subjects to include in each batch file. Larger batches = fewer files but more memory per batch. |
50
|
pattern
|
str
|
Glob pattern to find functional files within gsp_dir. Default matches standard GSP1000 structure. |
'sub-*/func/*bld001_rest_*_finalmask.nii.gz'
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Type | Description |
|---|---|
list of Path
|
Paths to created HDF5 batch files, sorted by name. |
Notes
Each HDF5 batch file contains: - 'timeseries': (n_subjects, n_timepoints, n_voxels) float32 array - 'mask_indices': (3, n_voxels) array of mask coordinates - 'mask_affine': (4, 4) affine transformation matrix - Attributes: n_subjects, n_timepoints, n_voxels, mask_shape
The batch files are designed for sequential loading during analysis, minimizing memory footprint while maintaining processing speed.
Examples:
>>> from lacuna.utils.gsp1000 import create_connectome_batches
>>> batch_files = create_connectome_batches(
... gsp_dir="/data/GSP1000",
... mask_path="/data/MNI152_T1_2mm_Brain_Mask.nii.gz",
... output_dir="/data/connectomes/gsp1000_batches",
... subjects_per_batch=100
... )
>>> print(f"Created {len(batch_files)} batch files")
Source code in src/lacuna/utils/gsp1000.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 | |
format_suggestions(suggestions)
¶
Format a list of suggestions for inclusion in an error message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suggestions
|
list[str]
|
List of suggested strings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string for error message, or empty string if no suggestions. |
Examples:
Source code in src/lacuna/utils/suggestions.py
log_error(message, verbose=False)
¶
log_info(message, verbose=False)
¶
log_progress(message, current=None, total=None, verbose=False)
¶
Print a progress message.
Source code in src/lacuna/utils/logging.py
log_section(title, width=70, verbose=False)
¶
log_success(message, details=None, verbose=False)
¶
log_warning(message, verbose=False)
¶
suggest_similar(query, candidates, max_suggestions=3, min_similarity=0.4)
¶
Find candidates most similar to the query string.
Uses difflib.SequenceMatcher for similarity scoring. Results are sorted by similarity (most similar first) and filtered by minimum threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The string to find matches for (e.g., user's typo). |
required |
candidates
|
list[str]
|
Available options to suggest from. |
required |
max_suggestions
|
int
|
Maximum number of suggestions to return. |
3
|
min_similarity
|
float
|
Minimum similarity ratio (0.0 to 1.0) to include a suggestion. Higher values require closer matches. |
0.4
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Up to |
Examples:
Source code in src/lacuna/utils/suggestions.py
validate_connectome_batches(batch_dir, verbose=False)
¶
Validate integrity of HDF5 connectome batch files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_dir
|
str or Path
|
Directory containing HDF5 batch files. |
required |
verbose
|
bool
|
Print validation results. |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
Validation summary with keys: n_batches, total_subjects, n_timepoints, n_voxels, mask_shape, consistent, errors |
Source code in src/lacuna/utils/gsp1000.py
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 | |