-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_pulse_sequence.py
More file actions
170 lines (134 loc) · 5.64 KB
/
example_pulse_sequence.py
File metadata and controls
170 lines (134 loc) · 5.64 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
#!/usr/bin/env python3
"""
Example script demonstrating how to use the TopSpin API wrapper to open pulse sequence files.
This script shows:
1. How to initialize the TopSpin API
2. How to open an experiment
3. How to open a pulse sequence file
Usage:
python example_pulse_sequence.py
Note: This script should be run from within TopSpin or with TopSpin properly configured.
For running from outside TopSpin, you may need to set up the environment properly.
"""
from pathlib import Path
from topspin_api import TopSpinAPI
def example_open_pulse_sequence():
"""Example: Open a pulse sequence file directly."""
print("\n" + "="*70)
print("Example 1: Open a pulse sequence file directly")
print("="*70)
# Initialize TopSpin API
print("Initializing TopSpin API...")
api = TopSpinAPI(topspin_path="D:/topspin")
# Get TopSpin version info
try:
print(f"TopSpin version: {api.get_version()}")
print(f"TopSpin installation: {api.get_installation_directory()}")
except Exception as e:
print(f"Note: Could not connect to running TopSpin instance.")
print(f"Error: {e}")
print("\nIMPORTANT: TopSpin must be RUNNING to use the Python API.")
print("Please start TopSpin first, then run this script.")
return
# Check current dataset
dataset_info = api.get_current_dataset_info()
if dataset_info:
print(f"Current dataset: {dataset_info['path']}")
else:
print("No dataset currently open. Opening example dataset...")
# Open an example experiment
example_exp = "D:/topspin/examdata/Cyclosporine/1"
if Path(example_exp).exists():
api.open_experiment(example_exp)
print(f"Opened example experiment: {example_exp}")
# Example pulse sequence path
# Modify this to your actual pulse sequence file path
pulse_sequence_path = "D:/topspin/examdata/Cyclosporine/1/pulseprogram"
if Path(pulse_sequence_path).exists():
print(f"\nOpening pulse sequence: {pulse_sequence_path}")
success = api.open_pulse_sequence(pulse_sequence_path)
if success:
print("✓ Pulse sequence opened successfully!")
else:
print("✗ Failed to open pulse sequence.")
else:
print(f"Pulse sequence file not found: {pulse_sequence_path}")
print("Please provide a valid pulse sequence file path.")
def example_open_pulse_sequence_in_experiment():
"""Example: Open pulse sequence for an experiment."""
print("\n" + "="*70)
print("Example 2: Open pulse sequence for an experiment")
print("="*70)
# Initialize TopSpin API
print("Initializing TopSpin API...")
api = TopSpinAPI(topspin_path="D:/topspin")
try:
print(f"TopSpin version: {api.get_version()}")
except Exception as e:
print(f"Note: Could not connect to running TopSpin instance.")
print(f"Error: {e}")
print("\nIMPORTANT: TopSpin must be RUNNING to use the Python API.")
print("Please start TopSpin first, then run this script.")
return
# Example experiment path
# Note: Modify this to your actual experiment path
experiment_path = "D:/topspin/examdata/Cyclosporine/1"
if Path(experiment_path).exists():
print(f"Opening pulse sequence for experiment: {experiment_path}")
print("Note: Actual pulse program files may be precompiled (pulseprogram.precomp)")
print("Use TopSpin's edpul command to open the pulse sequence editor")
success = api.execute_topspin_command("edpul")
if success:
print("✓ Pulse sequence editor opened!")
else:
print("✗ Failed to open pulse sequence.")
else:
print(f"Experiment not found: {experiment_path}")
print("Please provide a valid experiment path.")
def example_with_arguments():
"""Example: Open pulse sequence with command line arguments."""
import sys
print("\n" + "="*70)
print("Example 3: Open pulse sequence from command line argument")
print("="*70)
if len(sys.argv) < 2:
print("Usage: python example_pulse_sequence.py <pulse_sequence_path>")
print("\nExamples:")
print(" python example_pulse_sequence.py D:/topspin/examdata/Cyclosporine/1/pulseprogram")
print(" python example_pulse_sequence.py ./mypulseprogram")
return
pulse_sequence_path = sys.argv[1]
# Initialize TopSpin API
api = TopSpinAPI(topspin_path="D:/topspin")
print(f"Opening pulse sequence: {pulse_sequence_path}")
success = api.open_pulse_sequence(pulse_sequence_path)
if success:
print("✓ Pulse sequence opened successfully!")
else:
print("✗ Failed to open pulse sequence.")
def example_custom_topspin_path():
"""Example: Using a custom TopSpin installation path."""
print("\n" + "="*70)
print("Example 4: Using custom TopSpin installation path")
print("="*70)
# You can specify a different TopSpin installation path
custom_topspin_path = "D:/topspin" # Change if needed
api = TopSpinAPI(topspin_path=custom_topspin_path)
print(f"Using TopSpin at: {api.get_installation_directory()}")
print(f"TopSpin version: {api.get_version()}")
if __name__ == "__main__":
print("\n" + "="*70)
print("TopSpin Pulse Sequence API Examples")
print("="*70)
# Check if command line argument is provided
import sys
if len(sys.argv) > 1:
example_with_arguments()
else:
# Run all examples
example_open_pulse_sequence()
example_open_pulse_sequence_in_experiment()
example_custom_topspin_path()
print("\n" + "="*70)
print("Examples completed!")
print("="*70)