-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral_experiment_control.py
511 lines (432 loc) · 15.8 KB
/
general_experiment_control.py
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
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
import os, time, json
import cv2, tifffile
import numpy as np
from typing import List, Dict
from nodeology.state import State
from nodeology.node import Node, as_node
from nodeology.workflow import Workflow
from nodeology.client import R2R_Client, PPLX_Client
from langgraph.graph import END
from microscope_manager import MicroscopeManager
# Actual microscope client may be used in production
# from autoscript_tem_microscope_client import TemMicroscopeClient
# from autoscript_tem_microscope_client.enumerations import *
# from autoscript_tem_microscope_client.structures import *
# Use mock microscope client for development
from mock_tem_client import (
MockTemMicroscopeClient,
CameraType,
RunBeamTiltAutoFocusSettings,
)
# Define the state class
class TEMState(State):
"""State for TEM experiment workflow"""
# Microscope connection (store ID or reference instead of client object)
microscope_id: str # Store ID or connection reference
microscope_info: Dict[str, str]
# System status
vacuum_state: str
beam_state: bool
detector_status: Dict[str, bool]
# Optical settings
magnification: float
focus: float
stigmator_values: Dict[str, float]
# Acquisition settings
exposure_time: float
frame_size: int
detector_type: str
trial_mode: bool
direct_mode: bool
# Knowledge
recommender_knowledge: str
data_analysis_tool: str
trial_mode_detector_type: str
# Results
current_image: str # Path to the image file instead of np.ndarray
image_quality_metrics: Dict[str, float]
optimization_history: List[Dict]
validation_response: Dict
updated_parameters: Dict
data_analysis_results: Dict
# Define validation node
validate_setup = Node(
prompt_template="""Analyze microscope setup:
Vacuum State: {vacuum_state}
Beam State: {beam_state}
Detector Status: {detector_status}
Validate against required conditions:
1. Vacuum must be in READY state
2. Beam must be active and properly controlled
3. Selected detector must be operational
Output validation results as JSON:
{{
"is_valid": bool,
"issues": [str],
"recommendations": [str]
}}""",
sink="validation_response",
sink_format="json",
sink_transform=lambda x: json.loads(x),
)
parameter_recommender = Node(
prompt_template="""Recommender knowledge:
{recommender_knowledge}
Current microscope state and setup:
- Vacuum State: {vacuum_state}
- Beam State: {beam_state}
- Detector Type: {detector_type}
Recommend optimal imaging parameters for the current setup.
Consider:
1. Detector capabilities and limitations
2. Sample preservation requirements
3. Image quality requirements
Output as JSON:
{{
"parameter_values": {{
"magnification": float,
"focus": float,
"exposure_time": float,
"frame_size": int
}},
"reasoning": str
}}""",
sink="updated_parameters",
sink_format="json",
sink_transform=lambda x: json.loads(x),
)
# Function to initialize microscope
@as_node(
sink=[
"microscope_id",
"microscope_info",
"vacuum_state",
"beam_state",
"detector_status",
]
)
def initialize_microscope(ip_address: str = "localhost") -> tuple:
"""Initialize microscope connection and get basic status"""
try:
microscope = MockTemMicroscopeClient()
microscope.connect(ip_address)
# Store microscope in manager and get reference ID
microscope_id = MicroscopeManager.get_instance().set_microscope(microscope)
# Get microscope info
info = {
"name": microscope.service.system.name,
"serial": microscope.service.system.serial_number,
"version": microscope.service.system.version,
}
# Get system status
vacuum = microscope.vacuum.state
beam = not microscope.optics.is_beam_blanked
# Get detector status
detectors = {}
for cam in microscope.detectors.camera_detectors:
detector = microscope.detectors.get_camera_detector(cam)
detectors[cam] = detector.is_operational
return microscope_id, info, vacuum, beam, detectors
except Exception as e:
raise RuntimeError(f"Failed to initialize microscope: {str(e)}")
@as_node(sink=["current_image"])
def acquire_image(
microscope_id: str,
detector_type: str,
frame_size: int,
exposure_time: float,
trial_mode: bool = False,
) -> str: # Return str instead of np.ndarray
"""Acquire image and save to file"""
try:
microscope = MicroscopeManager.get_instance().get_microscope(microscope_id)
if detector_type == "empad":
image = microscope.acquisition.acquire_stem_data(
frame_size, exposure_time, trial_mode
)
elif detector_type in ["haadf"]:
image = microscope.acquisition.acquire_stem_image(
frame_size, exposure_time, trial_mode
)
else:
image = microscope.acquisition.acquire_camera_image(
detector_type, frame_size, exposure_time, trial_mode
)
# Convert to numpy array if needed
if not isinstance(image, np.ndarray):
image = np.array(image)
# Save image to file with timestamp or unique identifier
timestamp = time.strftime("%Y%m%d-%H%M%S")
tiff_image_path = os.path.join("data", "images", f"tem_image_{timestamp}.tiff")
png_image_path = os.path.join("data", "images", f"tem_image_{timestamp}.png")
# Ensure directory exists
os.makedirs(os.path.dirname(tiff_image_path), exist_ok=True)
os.makedirs(os.path.dirname(png_image_path), exist_ok=True)
# Save image (using appropriate format/library depending on your needs)
tifffile.imwrite(tiff_image_path, image)
cv2.imwrite(png_image_path, image)
return png_image_path
except Exception as e:
raise RuntimeError(f"Image acquisition failed: {str(e)}")
@as_node(sink=["focus", "stigmator_values"])
def auto_align(microscope_id: str) -> tuple:
"""Run automatic alignment procedures"""
try:
microscope = MicroscopeManager.get_instance().get_microscope(microscope_id)
# Configure auto functions
settings = RunBeamTiltAutoFocusSettings(CameraType.BM_CETA)
# Run auto focus
focus_result = microscope.auto_functions.run_beam_tilt_auto_focus(settings)
# Run stigmator correction
stig_result = microscope.auto_functions.run_objective_auto_stigmator(settings)
return focus_result.focus_value, stig_result.stigmator_values
except Exception as e:
raise RuntimeError(f"Auto-alignment failed: {str(e)}")
# The quality accessment process itself could be a complex workflow that involves multiple steps and techniques
# This is a simplified version for demonstration purposes using only vision language model
quality_assessor = Node(
prompt_template="""Analyze the TEM image quality:
Current Settings:
- Magnification: {magnification}
- Focus: {focus}
- Stigmator Values: {stigmator_values}
- Frame Size: {frame_size}
- Exposure: {exposure_time}
Evaluate image for:
1. Focus quality (check Fresnel fringes, edge sharpness)
2. Astigmatism correction
3. Brightness and contrast
4. Signal-to-noise ratio
5. Stage drift effects
6. Beam damage indicators
Output analysis as JSON:
{{
"quality_score": float (0-10),
"issues": [
{{
"type": str,
"severity": str,
"description": str
}}
],
"improvements": [
{{
"parameter": str,
"suggestion": str,
"reasoning": str
}}
]
}}""",
sink="image_quality_metrics",
sink_format="json",
image_keys=["current_image"],
sink_transform=lambda x: json.loads(x),
)
parameter_optimizer = Node(
prompt_template="""Review current imaging performance:
Quality Metrics: {image_quality_metrics}
Optimization History: {optimization_history}
Current Parameters:
- Magnification: {magnification}
- Focus: {focus}
- Stigmator Values: {stigmator_values}
- Exposure Time: {exposure_time}
- Frame Size: {frame_size}
Suggest parameter adjustments to improve image quality.
Consider:
1. Previous optimization attempts
2. Quality improvement trends
3. Physical limits of the system
Output as JSON:
{{
"parameter_values": {{
"magnification": float,
"focus": float,
"exposure_time": float,
"frame_size": int
}},
"reasoning": str,
"stop_optimization": bool
}}""",
sink="updated_parameters",
sink_format="json",
sink_transform=lambda x: json.loads(x),
)
@as_node(
sink=[
"magnification",
"focus",
"exposure_time",
"frame_size",
"trial_mode",
"direct_mode",
"detector_type",
]
)
def confirm_parameters(updated_parameters: Dict) -> tuple:
"""Display recommended parameters and get user confirmation/adjustments"""
print("\nRecommended parameters:")
for param, value in updated_parameters.get("parameter_values", {}).items():
print(f"- {param}: {value}")
if "reasoning" in updated_parameters:
print(f"\nReasoning: {updated_parameters['reasoning']}")
while True:
choice = input("\nDo you want to: [a]ccept, [m]odify, or [c]ancel? ").lower()
if choice == "a":
params = updated_parameters["parameter_values"]
print("Do you want to use trial mode? [y/n]")
trial_mode = input().lower() == "y"
print("Do you want to use direct mode? [y/n]")
direct_mode = input().lower() == "y"
return (
params.get("magnification"),
params.get("focus"),
params.get("exposure_time"),
params.get("frame_size"),
trial_mode,
direct_mode,
(
params.get("trial_mode_detector_type")
if trial_mode
else params.get("detector_type")
),
)
elif choice == "m":
modified_params = updated_parameters.copy()
params = modified_params["parameter_values"]
print("\nEnter new values (press Enter to keep current value):")
for param in ["magnification", "focus", "exposure_time", "frame_size"]:
current = params.get(param)
new_value = input(f"{param} ({current}): ").strip()
if new_value:
try:
params[param] = float(new_value)
except ValueError:
print(f"Invalid value for {param}, keeping original")
print("Do you want to use trial mode? [y/n]")
trial_mode = input().lower() == "y"
print("Do you want to use direct mode? [y/n]")
direct_mode = input().lower() == "y"
return (
params.get("magnification"),
params.get("focus"),
params.get("exposure_time"),
params.get("frame_size"),
trial_mode,
direct_mode,
(
params.get("trial_mode_detector_type")
if trial_mode
else params.get("detector_type")
),
)
elif choice == "c":
params = updated_parameters["parameter_values"]
if "stop_optimization" in params:
params["stop_optimization"] = True
print("Do you want to use trial mode? [y/n]")
trial_mode = input().lower() == "y"
print("Do you want to use direct mode? [y/n]")
direct_mode = input().lower() == "y"
return (
params.get("magnification"),
params.get("focus"),
params.get("exposure_time"),
params.get("frame_size"),
trial_mode,
direct_mode,
(
params.get("trial_mode_detector_type")
if trial_mode
else params.get("detector_type")
),
)
@as_node(sink=["data_analysis_results"])
def data_analysis(data_analysis_tool: str, data_analysis_results: Dict) -> Dict:
"""Run data analysis using specified tool"""
try:
# Use the specified data analysis tool to analyze the results
# This is a placeholder for actual data analysis logic
# Replace this with the actual implementation of the data analysis tool
return {"data_analysis_results": data_analysis_results}
except Exception as e:
raise RuntimeError(f"Data analysis failed: {str(e)}")
# Create the workflow class
class TEMWorkflow(Workflow):
"""Workflow for automated TEM imaging"""
def create_workflow(self):
# Add nodes
self.add_node("initialize", initialize_microscope)
self.add_node("validate", validate_setup)
self.add_node("recommend", parameter_recommender)
self.add_node("acquire", acquire_image)
self.add_node("align", auto_align)
self.add_node("assess", quality_assessor)
self.add_node("optimize", parameter_optimizer)
self.add_node("confirm", confirm_parameters)
self.add_node("analyze", data_analysis)
# Add edges with conditional logic
self.add_flow("initialize", "validate")
self.add_conditional_flow(
"validate",
lambda state: state["validation_response"]["is_valid"],
then="recommend",
otherwise=END,
)
self.add_flow("recommend", "confirm")
self.add_flow("confirm", "acquire")
self.add_conditional_flow(
"acquire",
lambda state: not state["direct_mode"],
then="assess",
otherwise="analyze",
)
self.add_conditional_flow(
"assess",
lambda state: state["image_quality_metrics"]["quality_score"] < 8.0,
then="align",
otherwise="optimize",
)
self.add_flow("align", "acquire")
self.add_conditional_flow(
"optimize",
lambda state: not state["updated_parameters"]["stop_optimization"],
then="confirm",
otherwise="analyze",
)
self.add_flow("analyze", END)
# Set entry point
self.set_entry("initialize")
# Compile workflow with intervention points
self.compile(checkpointer="memory")
if __name__ == "__main__":
# Initialize workflow
workflow = TEMWorkflow(
state_defs=TEMState, llm_name="gpt-4o", vlm_name="gpt-4o", debug_mode=True
)
# Optional: Export workflow to yaml template and flowchart
workflow.to_yaml("tem_workflow.yaml")
# workflow.graph.get_graph().draw_mermaid_png(output_file_path="tem_workflow.png")
# Provide expert knowledge for recommender or retrieve from knowledge base or search the internet
expert_knowledge = """Knowledge about microscope settings and parameters..."""
# # Define query
# query = "How will the parameters (magnification, focus, exposure time, frame size, detector type) affect the STEM image quality?"
# # Retrieve from knowledge base
# rag_client = R2R_Client(model_name="gpt-4o", search_strategy="hybrid", rag_strategy="hyde")
# expert_knowledge = rag_client([{"role":"user", "content":query}])
# # Retrieve from internet
# web_client = PPLX_Client(model_name="lama-3.1-sonar-large-128k-online")
# expert_knowledge = web_client([{"role":"user", "content":query}])
# Define initial state
initial_state = {
# Set initial imaging parameters
"magnification": 20000,
"focus": 0.0,
"exposure_time": 0.1,
"frame_size": 2048,
"detector_type": "empad",
"trial_mode_detector_type": "haadf",
"recommender_knowledge": expert_knowledge,
}
# Run workflow
result = workflow.run(initial_state)