Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
478 lines (370 loc) · 12.8 KB
/
Copy pathutils.py
File metadata and controls
478 lines (370 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
"""
Utilities Module
================
Helper functions for:
- Data I/O (CSV, HDF5, etc.)
- Coordinate transformations
- Statistics and analysis
- File format conversions
"""
import numpy as np
import pandas as pd
from pathlib import Path
def load_localizations_csv(filepath):
"""Load localizations from CSV file.
Parameters
----------
filepath : str or Path
Path to CSV file
Returns
-------
localizations : dict
Dictionary with localization data
"""
df = pd.read_csv(filepath)
localizations = {}
# Common column mappings
column_map = {
'x': ['x', 'x [nm]', 'x_nm', 'X'],
'y': ['y', 'y [nm]', 'y_nm', 'Y'],
'z': ['z', 'z [nm]', 'z_nm', 'Z'],
'frame': ['frame', 'Frame', 'frame_number'],
'intensity': ['intensity', 'Intensity', 'photons', 'N'],
'background': ['background', 'Background', 'bg', 'offset'],
'sigma_x': ['sigma_x', 'sigma', 'sigma [nm]', 'PSF_sigma'],
'sigma_y': ['sigma_y', 'sigma', 'sigma [nm]', 'PSF_sigma'],
'uncertainty': ['uncertainty', 'Uncertainty', 'precision', 'sigma_xy']
}
for key, possible_cols in column_map.items():
for col in possible_cols:
if col in df.columns:
localizations[key] = df[col].values
break
return localizations
def save_localizations_csv(localizations, filepath, include_metadata=True):
"""Save localizations to CSV file.
Columns keep the pipeline's conventions: 'x [nm]', 'y [nm]' with a
pixel-centre origin (centre of pixel (0, 0) at 0 nm), 0-based frames,
sigma in pixels. For thunderSTORM's corner-origin format use
thunderstorm_format.save_thunderstorm_csv.
Parameters
----------
localizations : dict
Dictionary with localization data
filepath : str or Path
Output filepath
include_metadata : bool
Include additional metadata columns
"""
# Convert to DataFrame
data = {}
# Core columns
if 'x' in localizations:
data['x [nm]'] = localizations['x']
if 'y' in localizations:
data['y [nm]'] = localizations['y']
if 'z' in localizations:
data['z [nm]'] = localizations['z']
if 'frame' in localizations:
data['frame'] = localizations['frame']
# Optional columns
if include_metadata:
for key in ['intensity', 'background', 'sigma_x', 'sigma_y',
'uncertainty', 'chi_squared']:
if key in localizations:
data[key] = localizations[key]
df = pd.DataFrame(data)
df.to_csv(filepath, index=False)
def load_image_stack(filepath):
"""Load image stack from various formats.
Parameters
----------
filepath : str or Path
Path to image file
Returns
-------
images : ndarray
Image stack (n_frames, height, width)
metadata : dict
Image metadata
"""
filepath = Path(filepath)
suffix = filepath.suffix.lower()
if suffix == '.tif' or suffix == '.tiff':
from tifffile import imread
images = imread(str(filepath))
# Ensure 3D
if images.ndim == 2:
images = images[np.newaxis, ...]
metadata = {'filename': str(filepath)}
elif suffix == '.npy':
images = np.load(str(filepath))
metadata = {'filename': str(filepath)}
else:
raise ValueError(f"Unsupported file format: {suffix}")
return images, metadata
def save_image_stack(images, filepath):
"""Save image stack to file.
Parameters
----------
images : ndarray
Image stack (n_frames, height, width)
filepath : str or Path
Output filepath
"""
filepath = Path(filepath)
suffix = filepath.suffix.lower()
if suffix == '.tif' or suffix == '.tiff':
from tifffile import imwrite
imwrite(str(filepath), images.astype(np.float32))
elif suffix == '.npy':
np.save(str(filepath), images)
else:
raise ValueError(f"Unsupported file format: {suffix}")
def compute_nearest_neighbor_distances(localizations):
"""Compute nearest neighbor distances.
Parameters
----------
localizations : dict
Localization data with 'x', 'y'
Returns
-------
distances : ndarray
Nearest neighbor distance for each localization
"""
from scipy.spatial import cKDTree
positions = np.column_stack([localizations['x'], localizations['y']])
tree = cKDTree(positions)
# Query for 2 nearest (self + nearest)
distances, indices = tree.query(positions, k=2)
# Return distance to nearest neighbor (not self)
return distances[:, 1]
def compute_ripley_k(localizations, radii, area=None):
"""Compute Ripley's K function for spatial statistics.
Parameters
----------
localizations : dict
Localization data with 'x', 'y'
radii : ndarray
Radii to evaluate
area : float, optional
Total area (computed from data if not provided)
Returns
-------
K : ndarray
Ripley's K values
L : ndarray
Linearized K (L = sqrt(K/pi) - r)
"""
from scipy.spatial import cKDTree
positions = np.column_stack([localizations['x'], localizations['y']])
n = len(positions)
if area is None:
x_range = localizations['x'].max() - localizations['x'].min()
y_range = localizations['y'].max() - localizations['y'].min()
area = x_range * y_range
tree = cKDTree(positions)
K = np.zeros(len(radii))
for i, r in enumerate(radii):
# Count pairs within distance r
pairs = tree.query_pairs(r)
n_pairs = len(pairs)
# Ripley's K
K[i] = (area * n_pairs) / (n * (n - 1))
# Linearized L function
L = np.sqrt(K / np.pi) - radii
return K, L
def convert_coordinates(localizations, pixel_size_from, pixel_size_to=None,
origin=(0, 0)):
"""Convert coordinate system.
Parameters
----------
localizations : dict
Localization data
pixel_size_from : float
Original pixel size (nm)
pixel_size_to : float, optional
Target pixel size (nm)
origin : tuple
Origin offset (x, y) in nm
Returns
-------
converted : dict
Converted localizations
"""
converted = localizations.copy()
# Translate
converted['x'] = localizations['x'] - origin[0]
converted['y'] = localizations['y'] - origin[1]
# Scale
if pixel_size_to is not None and pixel_size_to != pixel_size_from:
scale = pixel_size_from / pixel_size_to
converted['x'] *= scale
converted['y'] *= scale
if 'sigma_x' in converted:
converted['sigma_x'] *= scale
if 'sigma_y' in converted:
converted['sigma_y'] *= scale
if 'uncertainty' in converted:
converted['uncertainty'] *= scale
return converted
def compute_localization_density(localizations, pixel_size=100):
"""Compute spatial density map.
Parameters
----------
localizations : dict
Localization data
pixel_size : float
Pixel size for density map (nm)
Returns
-------
density_map : ndarray
2D density map
extent : tuple
(x_min, x_max, y_min, y_max) in nm
"""
x = localizations['x']
y = localizations['y']
# Determine bounds
x_min, x_max = x.min(), x.max()
y_min, y_max = y.min(), y.max()
# Create bins
x_bins = np.arange(x_min, x_max + pixel_size, pixel_size)
y_bins = np.arange(y_min, y_max + pixel_size, pixel_size)
# Compute 2D histogram
density_map, _, _ = np.histogram2d(y, x, bins=[y_bins, x_bins])
extent = (x_min, x_max, y_min, y_max)
return density_map, extent
def filter_by_roi(localizations, roi):
"""Filter localizations within region of interest.
Parameters
----------
localizations : dict
Localization data
roi : tuple
(x_min, x_max, y_min, y_max) in nm
Returns
-------
filtered : dict
Filtered localizations
"""
x_min, x_max, y_min, y_max = roi
mask = ((localizations['x'] >= x_min) & (localizations['x'] <= x_max) &
(localizations['y'] >= y_min) & (localizations['y'] <= y_max))
filtered = {}
for key, value in localizations.items():
if isinstance(value, np.ndarray) and len(value) == len(mask):
filtered[key] = value[mask]
else:
filtered[key] = value
return filtered
def compute_statistics(localizations):
"""Compute summary statistics.
Parameters
----------
localizations : dict
Localization data
Returns
-------
stats : dict
Summary statistics
"""
stats = {
'n_localizations': len(localizations['x']),
}
if 'frame' in localizations:
stats['n_frames'] = len(np.unique(localizations['frame']))
stats['mean_localizations_per_frame'] = stats['n_localizations'] / stats['n_frames']
if 'intensity' in localizations:
stats['mean_intensity'] = np.mean(localizations['intensity'])
stats['median_intensity'] = np.median(localizations['intensity'])
stats['std_intensity'] = np.std(localizations['intensity'])
if 'sigma_x' in localizations:
stats['mean_sigma_x'] = np.mean(localizations['sigma_x'])
stats['median_sigma_x'] = np.median(localizations['sigma_x'])
if 'uncertainty' in localizations:
stats['mean_uncertainty'] = np.mean(localizations['uncertainty'])
stats['median_uncertainty'] = np.median(localizations['uncertainty'])
# Spatial extent
stats['x_min'] = np.min(localizations['x'])
stats['x_max'] = np.max(localizations['x'])
stats['y_min'] = np.min(localizations['y'])
stats['y_max'] = np.max(localizations['y'])
stats['x_range'] = stats['x_max'] - stats['x_min']
stats['y_range'] = stats['y_max'] - stats['y_min']
# Nearest neighbor
nn_dist = compute_nearest_neighbor_distances(localizations)
stats['mean_nn_distance'] = np.mean(nn_dist)
stats['median_nn_distance'] = np.median(nn_dist)
return stats
def merge_localization_datasets(datasets):
"""Merge multiple localization datasets.
Parameters
----------
datasets : list of dict
List of localization dictionaries
Returns
-------
merged : dict
Merged dataset
"""
merged = {}
# Get all keys
all_keys = set()
for dataset in datasets:
all_keys.update(dataset.keys())
# Merge each key
for key in all_keys:
arrays = [d[key] for d in datasets if key in d]
if arrays and isinstance(arrays[0], np.ndarray):
merged[key] = np.concatenate(arrays)
else:
merged[key] = arrays[0] if arrays else None
return merged
class LocalizationTable:
"""Class for managing localization data with pandas-like interface."""
def __init__(self, localizations=None):
self.data = localizations if localizations is not None else {}
def __len__(self):
if 'x' in self.data:
return len(self.data['x'])
return 0
def filter(self, condition):
"""Filter localizations by condition.
Parameters
----------
condition : callable or ndarray
Boolean function or array
Returns
-------
filtered : LocalizationTable
Filtered table
"""
if callable(condition):
mask = condition(self.data)
else:
mask = condition
filtered_data = {}
for key, value in self.data.items():
if isinstance(value, np.ndarray) and len(value) == len(mask):
filtered_data[key] = value[mask]
else:
filtered_data[key] = value
return LocalizationTable(filtered_data)
def to_dataframe(self):
"""Convert to pandas DataFrame."""
return pd.DataFrame(self.data)
def save(self, filepath, format='csv'):
"""Save to file."""
if format == 'csv':
save_localizations_csv(self.data, filepath)
else:
raise ValueError(f"Unsupported format: {format}")
@classmethod
def load(cls, filepath, format='csv'):
"""Load from file."""
if format == 'csv':
data = load_localizations_csv(filepath)
else:
raise ValueError(f"Unsupported format: {format}")
return cls(data)