-
Notifications
You must be signed in to change notification settings - Fork 12
/
PulseSequencePlotter.py
267 lines (219 loc) · 9.79 KB
/
PulseSequencePlotter.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
'''
Created on Jan 8, 2012
Large refactor to notebook Bokeh plotting 5 Jan, 2015
@author: cryan@bbn.com
A simple interactive tool plotting pulse sequences for visual inspection
Copyright 2012,2013,2014,2015 Raytheon BBN Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import os.path
import json
from importlib import import_module
import numpy as np
from . import config
from . import drivers
import pkgutil
def all_zero_seqs(seqs):
return all([np.allclose([_[1] for _ in seq], 0) for seq in seqs])
def build_awg_translator_map():
translators_map = {}
translators = [_[1] for _ in pkgutil.walk_packages(drivers.__path__)]
for translator in translators:
module = import_module('QGL.drivers.' + translator)
ext = module.get_seq_file_extension()
if ext in translators_map:
translators_map[ext].append(module)
else:
translators_map[ext] = [module]
return translators_map
# static translator map
translators = build_awg_translator_map()
def resolve_translator(filename, translators):
ext = os.path.splitext(filename)[1]
if ext not in translators:
raise NameError("No translator found to open the given file %s",
filename)
if len(translators[ext]) == 1:
return translators[ext][0]
for t in translators[ext]:
if t.is_compatible_file(filename):
return t
raise NameError("No translator found to open the given file %s", filename)
def plot_pulse_files(metafile, time=True, backend='bqplot'):
'''
plot_pulse_files(metafile)
Helper function to plot a list of AWG files. A jupyter slider widget allows choice of sequence number.
'''
#If we only go one filename turn it into a list
with open(metafile, 'r') as FID:
meta_info = json.load(FID)
fileNames = []
for el in meta_info["instruments"].values():
# Accomodate seq_file per instrument and per channel
if isinstance(el, str):
fileNames.append(el)
elif isinstance(el, dict):
for file in el.values():
fileNames.append(file)
line_names, num_seqs, data_dicts = extract_waveforms(fileNames, time=time)
localname = os.path.split(fileNames[0])[1]
sequencename = localname.split('-')[0]
if backend=='matplotlib':
import matplotlib.pyplot as plt
from ipywidgets import interact, IntSlider
def update_plot(seq_ind):
for line_name in line_names:
dat = data_dicts[f"{line_name}_{seq_ind}"]
plt.plot(dat['x'], dat['y'], label=line_name, linewidth=1.0)
interact(update_plot, seq_ind=IntSlider(min=1,max=num_seqs,step=1,value=1,description="Sequence Number"))
elif backend=='bqplot':
from bqplot import DateScale, LinearScale, Axis, Lines, Figure, Tooltip
from bqplot.toolbar import Toolbar
from bqplot.colorschemes import CATEGORY10, CATEGORY20
from ipywidgets import interact, IntSlider, VBox
sx = LinearScale()
sy = LinearScale(min=-1.0, max=2*len(line_names)-1.0)
if time:
ax = Axis(label='Time (ns)', scale=sx)
else:
ax = Axis(label="Samples", scale=sx)
ay = Axis(label='Amplitude', scale=sy, orientation='vertical')
colors = CATEGORY10 if len(line_names)<10 else CATEGORY20
lines = []
tt = Tooltip(fields=['name'], labels=['Channel'])
x_mult = 1.0e9 if time else 1
for i, line_name in enumerate(line_names):
dat = data_dicts[f"{line_name}_1"]
lines.append(Lines(labels=[line_name], x=x_mult*dat['x'], y=dat['y'], scales={'x': sx, 'y': sy},
tooltip=tt, animate=False, colors=[colors[i]]))
slider = IntSlider(min=1, max=num_seqs, step=1, description='Segment', value=1)
def segment_changed(change):
for line, line_name in zip(lines, line_names):
dat = data_dicts[f"{line_name}_{slider.value}"]
line.x = x_mult*dat['x']
line.y = dat['y']
slider.observe(segment_changed, 'value')
fig = Figure(marks=lines, axes=[ax, ay], title='Waveform Plotter',animation_duration=50)
toolb = Toolbar(figure=fig)
return VBox([slider, fig, toolb])
def extract_waveforms(fileNames, nameDecorator='', time=False):
line_names = []
data_dicts = {}
num_seqs = 0
for fileName in sorted(fileNames):
# Assume a naming convention path/to/file/SequenceName-AWGName.h5
AWGName = "-".join((os.path.split(os.path.splitext(fileName)[0])[1]).split('-')[1:])
# Strip any _ suffix
# if '_' in AWGName:
# AWGName = AWGName[:AWGName.index('_')]
translator = resolve_translator(fileName, translators)
wfs = translator.read_sequence_file(fileName)
sample_time = 1.0/translator.SAMPLING_RATE if time else 1
for (k, seqs) in sorted(wfs.items()):
if all_zero_seqs(seqs):
continue
num_seqs = max(num_seqs, len(seqs))
line_names.append((AWGName + nameDecorator + '_' + k).replace("-","_"))
k_ = line_names[-1].replace("-", "_")
for ct, seq in enumerate(seqs):
data_dicts[k_ + "_{:d}".format(ct + 1)] = {}
# Convert from time amplitude pairs to x,y lines with points at start and beginnning to prevent interpolation
data_dicts[k_ + "_{:d}".format(ct + 1)]["x"] = sample_time * np.tile(
np.cumsum([0] + [_[0] for _ in seq]),
(2, 1)).flatten(order="F")[1:-1]
data_dicts[k_ + "_{:d}".format(ct + 1)]["y"] = np.tile(
[_[1] for _ in seq],
(2, 1)).flatten(order="F") + 2 * (len(line_names) - 1)
return line_names, num_seqs, data_dicts
# def plot_pulse_files_compare(metafile1, metafile2, time=False):
# '''
# plot_pulse_files_compare(fileNames1, fileNames2)
# Helper function to plot a list of AWG files. A JS slider allows choice of sequence number.
# '''
# #If we only go one filename turn it into a list
# fileNames1 = []
# fileNames2 = []
# with open(metafile1, 'r') as FID:
# meta_info1 = json.load(FID)
# for el in meta_info1["instruments"].values():
# # Accomodate seq_file per instrument and per channel
# if isinstance(el, str):
# fileNames1.append(el)
# elif isinstance(el, dict):
# for file in el.values():
# fileNames1.append(file)
# with open(metafile2, 'r') as FID:
# meta_info2 = json.load(FID)
# for el in meta_info2["instruments"].values():
# # Accomodate seq_file per instrument and per channel
# if isinstance(el, str):
# fileNames2.append(el)
# elif isinstance(el, dict):
# for file in el.values():
# fileNames2.append(file)
# line_names1, num_seqs1, data_dicts1 = extract_waveforms(fileNames1, 'A', time=time)
# line_names2, num_seqs2, data_dicts2 = extract_waveforms(fileNames2, 'B', time=time)
# num_seqs = max(num_seqs1, num_seqs2)
# data_dicts1.update(data_dicts2)
# line_names = line_names1 + line_names2
# localname = os.path.split(fileNames1[0])[1]
# sequencename = localname.split('-')[0]
# data = {line_name: ColumnDataSource(data=dat) for line_name,dat in data_dicts1.items()}
# plot = Figure(title=sequencename, plot_width=1000)
# plot.background_fill_color = config.plotBackground
# if config.gridColor:
# plot.xgrid.grid_line_color = config.gridColor
# plot.ygrid.grid_line_color = config.gridColor
# # Colobrewer2 qualitative Set1 (http://colorbrewer2.org)
# colours = [
# "#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33",
# "#a65628", "#f781bf", "#999999"
# ]
# js_sources = data
# for ct, k in enumerate(line_names):
# k_ = k.replace("-", "_")
# line = plot.line(data_dicts1[k + "_1"]["x"],
# data_dicts1[k + "_1"]["y"],
# color=colours[ct % len(colours)],
# line_width=2,
# legend=k.replace("_", "-"))
# js_sources[k_] = line.data_source
# code_template = Template("""
# var seq_num = cb_obj.getv('value');
# // console.log(seq_num);
# var all_data = {
# {% for trace in trace_names %}
# '{{trace}}': {{trace}}.getv('data'),
# {% endfor %}
# };
# {% for line in line_names %}
# {{line}}['data'] = {'x': (all_data['{{line}}_'.concat(seq_num.toString())])['x'],
# 'y': (all_data['{{line}}_'.concat(seq_num.toString())])['y']};
# {% endfor %}
# """)
# rendered = code_template.render(
# line_names=line_names,
# trace_names=list(data.keys())[:-len(line_names)]
# )
# callback = CustomJS(
# args=dict(**js_sources),
# code=rendered)
# if num_seqs > 1:
# slider = Slider(start=1,
# end=num_seqs,
# value=1,
# step=1,
# title="Sequence",
# callback=callback)
# layout = column(slider, plot)
# show(layout)
# else:
# show(plot)