-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples_api_usage.py
More file actions
319 lines (254 loc) · 10.4 KB
/
Copy pathexamples_api_usage.py
File metadata and controls
319 lines (254 loc) · 10.4 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
#!/usr/bin/env python
"""
Example: Using napari-fast4dreg programmatically via API.
This script demonstrates how to integrate Fast4DReg into your
workflows without the napari GUI.
Key Features:
- XY, Z, and 3D rotation drift correction
- Sequential rotation correction (XY → ZX → ZY) for improved accuracy
- Flexible reference channel selection (single or multiple channels)
- Progress callbacks for integration
- Efficient out-of-memory processing with dask/zarr
"""
from pathlib import Path
import numpy as np
# Import the API
from napari_fast4dreg import register_image_from_file
def example_1_basic():
"""Example 1: Basic registration with numpy array."""
print("="*60)
print("Example 1: Basic registration")
print("="*60)
# Create or load your image data (CTZYX format)
# For this example, we'll load from file
example_dir = Path(__file__).parent / "example_files"
# Option A: Load and register in one step
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX", # ImageJ format
ref_channel=1, # Use channel 1 (nuclei)
output_dir=example_dir / "api_output_example1",
correct_xy=True,
correct_z=True,
correct_rotation=True,
)
# Access results
registered = result['registered_image']
print(f"Registered image shape: {registered.shape}")
print(f"Output saved to: {result['output_path']}")
print(f"XY drift shape: {result['xy_drift'].shape}")
print(f"Z drift shape: {result['z_drift'].shape}")
print()
def example_2_with_progress():
"""Example 2: Registration with progress tracking."""
print("="*60)
print("Example 2: Registration with progress tracking")
print("="*60)
# Define progress callback
def show_progress(message):
print(f" [PROGRESS] {message}")
example_dir = Path(__file__).parent / "example_files"
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel=1,
output_dir=example_dir / "api_output_example2",
correct_xy=True,
correct_z=True,
correct_rotation=False, # Skip rotation for speed
progress_callback=show_progress,
)
print(f"\nCompleted! Registered image saved to: {result['output_path']}")
print()
def example_3_selective_corrections():
"""Example 3: Selective corrections only."""
print("="*60)
print("Example 3: Only XY drift correction")
print("="*60)
example_dir = Path(__file__).parent / "example_files"
# Only correct XY drift, skip Z and rotation
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel=1,
output_dir=example_dir / "api_output_example3",
correct_xy=True,
correct_z=False, # Skip Z
correct_rotation=False, # Skip rotation
progress_callback=lambda msg: print(f" {msg}"),
)
print(f"\nXY drift detected: {result['xy_drift'][:5]}")
print(f"Output: {result['output_path']}")
print()
def example_4_multi_channel_reference():
"""Example 4: Multi-channel reference with normalization."""
print("="*60)
print("Example 4: Multi-channel reference")
print("="*60)
example_dir = Path(__file__).parent / "example_files"
# Use multiple channels as reference
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel="0,1", # Use both channels
normalize_channels=True, # Normalize before summing
projection_type='max', # Use max projection
reference_mode='first_frame', # Compare to first frame
output_dir=example_dir / "api_output_example4",
progress_callback=lambda msg: print(f" {msg}"),
)
print("\nRegistered with multi-channel reference")
print(f"Output: {result['output_path']}")
print()
def example_5_rotation_correction():
"""Example 5: Understanding sequential rotation correction."""
print("="*60)
print("Example 5: Sequential Rotation Correction")
print("="*60)
example_dir = Path(__file__).parent / "example_files"
print("\nFast4DReg applies 3D rotation correction sequentially:")
print(" 1. Alpha (XY plane) - detected and applied first")
print(" 2. Beta (ZX plane) - detected on alpha-corrected data")
print(" 3. Gamma (ZY plane) - detected on alpha+beta-corrected data")
print("\nThis sequential approach improves accuracy when multiple")
print("rotation components are present.")
print()
# Run with rotation correction enabled
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel=1,
output_dir=example_dir / "api_output_example5",
correct_xy=True,
correct_z=True,
correct_rotation=True, # Sequential XY→ZX→ZY rotation
progress_callback=lambda msg: print(f" {msg}"),
)
# Analyze rotation components
print("\n Rotation angles detected:")
print(f" XY (alpha): mean={result['rotation_xy'].mean():.3f}°, max={result['rotation_xy'].max():.3f}°")
print(f" ZX (beta): mean={result['rotation_zx'].mean():.3f}°, max={result['rotation_zx'].max():.3f}°")
print(f" ZY (gamma): mean={result['rotation_zy'].mean():.3f}°, max={result['rotation_zy'].max():.3f}°")
print(f"\n Output: {result['output_path']}")
print()
def example_6_integration_workflow():
"""Example 6: Integration into a larger workflow."""
print("="*60)
print("Example 6: Integration into workflow")
print("="*60)
example_dir = Path(__file__).parent / "example_files"
# Step 1: Your preprocessing
print("Step 1: Preprocessing...")
# (Your preprocessing code here)
# Step 2: Registration
print("Step 2: Running Fast4DReg registration...")
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel=1,
output_dir=example_dir / "api_output_example6",
correct_xy=True,
correct_z=True,
correct_rotation=True,
keep_temp_files=False, # Clean up temp files
return_drifts=True, # Get drift data
)
registered_image = result['registered_image']
# Step 3: Your postprocessing
print("Step 3: Postprocessing...")
# (Your postprocessing code here)
# Step 4: Analysis using drift information
print("Step 4: Analyzing drift patterns...")
xy_drift = result['xy_drift']
total_drift = np.sqrt(xy_drift[:, 0]**2 + xy_drift[:, 1]**2)
print(f" Max XY drift: {total_drift.max():.2f} pixels")
print(f" Mean XY drift: {total_drift.mean():.2f} pixels")
if 'z_drift' in result:
z_drift = result['z_drift']
print(f" Max Z drift: {np.abs(z_drift).max():.2f} pixels")
print(f" Mean Z drift: {np.abs(z_drift).mean():.2f} pixels")
print()
def example_7_gpu_acceleration():
"""Example 7: GPU acceleration - automatic detection and manual control."""
print("="*60)
print("Example 7: GPU Acceleration (Automatic Detection)")
print("="*60)
from napari_fast4dreg import get_gpu_info
example_dir = Path(__file__).parent / "example_files"
print("\nGPU acceleration is AUTOMATICALLY DETECTED and enabled on import.")
print("NVIDIA GPUs are preferred over Intel GPUs.")
print("Requires: pip install pyclesperanto-prototype")
print()
# Check current GPU status
print(f"Current backend: {get_gpu_info()}")
# Check if GPU is already enabled from automatic detection
from napari_fast4dreg._fast4Dreg_functions import USE_GPU_ACCELERATION
if USE_GPU_ACCELERATION:
print("✓ GPU acceleration was automatically enabled!")
print("\nRunning registration with GPU acceleration...")
import time
start_time = time.time()
result = register_image_from_file(
example_dir / "xtitched_organoid_timelapse_ch0_cytosol_ch1_nuclei.tif",
axis_order="TZCYX",
ref_channel=1,
output_dir=example_dir / "api_output_example7_gpu",
correct_xy=True,
correct_z=True,
correct_rotation=True,
progress_callback=lambda msg: print(f" {msg}"),
)
gpu_time = time.time() - start_time
print(f"\n GPU processing time: {gpu_time:.1f}s")
print(f" GPU device: {get_gpu_info()}")
print(f" Output: {result['output_path']}")
# Show how to manually disable GPU
print("\n You can manually disable GPU if needed:")
print(" set_gpu_acceleration(False)")
print(" Typical GPU speedup: 5-10x for transformation operations")
else:
print("✗ GPU acceleration not available (CPU mode)")
print(f" Backend: {get_gpu_info()}")
print("\n To enable GPU acceleration:")
print(" 1. Install: pip install pyclesperanto-prototype")
print(" 2. Ensure OpenCL-compatible GPU is available")
print(" 3. Restart Python - GPU will be auto-detected")
print("\n Or manually enable: set_gpu_acceleration(True)")
print(" Requires OpenCL-compatible GPU (NVIDIA, AMD, or Intel)")
print()
if __name__ == "__main__":
print("\n" + "="*60)
print("napari-fast4dreg API Examples")
print("="*60 + "\n")
# Run examples
try:
example_1_basic()
except Exception as e:
print(f"Example 1 failed: {e}\n")
try:
example_2_with_progress()
except Exception as e:
print(f"Example 2 failed: {e}\n")
try:
example_3_selective_corrections()
except Exception as e:
print(f"Example 3 failed: {e}\n")
try:
example_4_multi_channel_reference()
except Exception as e:
print(f"Example 4 failed: {e}\n")
try:
example_5_rotation_correction()
except Exception as e:
print(f"Example 5 failed: {e}\n")
try:
example_6_integration_workflow()
except Exception as e:
print(f"Example 6 failed: {e}\n")
try:
example_7_gpu_acceleration()
except Exception as e:
print(f"Example 7 failed: {e}\n")
print("="*60)
print("All examples completed!")
print("="*60)