• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

feihoo87 / waveforms / 7087368175

04 Dec 2023 01:33PM UTC coverage: 43.134% (-0.5%) from 43.641%
7087368175

push

github

feihoo87
fix workflow

7413 of 17186 relevant lines covered (43.13%)

2.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/waveforms/visualization/__init__.py
1
import matplotlib.pyplot as plt
×
2
import numpy as np
×
3

4
from ._autoplot import autoplot
×
5

6

7
def plotLine(c0, c1, ax, **kwargs):
×
8
    t = np.linspace(0, 1, 11)
×
9
    c = (c1 - c0) * t + c0
×
10
    ax.plot(c.real, c.imag, **kwargs)
×
11

12

13
def plotCircle(c0, r, ax, **kwargs):
×
14
    t = np.linspace(0, 1, 1001) * 2 * np.pi
×
15
    s = c0 + r * np.exp(1j * t)
×
16
    ax.plot(s.real, s.imag, **kwargs)
×
17

18

19
def plotEllipse(c0, a, b, phi, ax, **kwargs):
×
20
    t = np.linspace(0, 1, 1001) * 2 * np.pi
×
21
    c = np.exp(1j * t)
×
22
    s = c0 + (c.real * a + 1j * c.imag * b) * np.exp(1j * phi)
×
23
    ax.plot(s.real, s.imag, **kwargs)
×
24

25

26
def plotDistribution(s0,
×
27
                     s1,
28
                     fig=None,
29
                     axes=None,
30
                     info=None,
31
                     hotThresh=10000,
32
                     logy=False):
33
    from waveforms.math.fit import get_threshold_info, mult_gaussian_pdf
×
34

35
    if info is None:
×
36
        info = get_threshold_info(s0, s1)
×
37
    else:
38
        info = get_threshold_info(s0, s1, info['threshold'], info['phi'])
×
39
    thr, phi = info['threshold'], info['phi']
×
40
    # visibility, p0, p1 = info['visibility']
41
    # print(
42
    #     f"thr={thr:.6f}, phi={phi:.6f}, visibility={visibility:.3f}, {p0}, {1-p1}"
43
    # )
44

45
    if axes is not None:
×
46
        ax1, ax2 = axes
×
47
    else:
48
        if fig is None:
×
49
            fig = plt.figure()
×
50
        ax1 = fig.add_subplot(121)
×
51
        ax2 = fig.add_subplot(122)
×
52

53
    if (len(s0) + len(s1)) < hotThresh:
×
54
        ax1.plot(np.real(s0), np.imag(s0), '.', alpha=0.2)
×
55
        ax1.plot(np.real(s1), np.imag(s1), '.', alpha=0.2)
×
56
    else:
57
        _, *bins = np.histogram2d(np.real(np.hstack([s0, s1])),
×
58
                                  np.imag(np.hstack([s0, s1])),
59
                                  bins=50)
60

61
        H0, *_ = np.histogram2d(np.real(s0),
×
62
                                np.imag(s0),
63
                                bins=bins,
64
                                density=True)
65
        H1, *_ = np.histogram2d(np.real(s1),
×
66
                                np.imag(s1),
67
                                bins=bins,
68
                                density=True)
69
        vlim = max(np.max(np.abs(H0)), np.max(np.abs(H1)))
×
70

71
        ax1.imshow(H1.T - H0.T,
×
72
                   alpha=(np.fmax(H0.T, H1.T) / vlim).clip(0, 1),
73
                   interpolation='nearest',
74
                   origin='lower',
75
                   cmap='coolwarm',
76
                   vmin=-vlim,
77
                   vmax=vlim,
78
                   extent=(bins[0][0], bins[0][-1], bins[1][0], bins[1][-1]))
79

80
    ax1.axis('equal')
×
81
    ax1.set_xticks([])
×
82
    ax1.set_yticks([])
×
83
    for s in ax1.spines.values():
×
84
        s.set_visible(False)
×
85

86
    # c0, c1 = info['center']
87
    # a0, b0, a1, b1 = info['std']
88
    params = info['params']
×
89
    r0, i0, r1, i1 = params[0][0], params[1][0], params[0][1], params[1][1]
×
90
    a0, b0, a1, b1 = params[0][2], params[1][2], params[0][3], params[1][3]
×
91
    c0 = (r0 + 1j * i0) * np.exp(1j * phi)
×
92
    c1 = (r1 + 1j * i1) * np.exp(1j * phi)
×
93
    phi0 = phi + params[0][6]
×
94
    phi1 = phi + params[1][6]
×
95
    plotEllipse(c0, 2 * a0, 2 * b0, phi0, ax1)
×
96
    plotEllipse(c1, 2 * a1, 2 * b1, phi1, ax1)
×
97

98
    im0, im1 = info['idle']
×
99
    lim = min(im0.min(), im1.min()), max(im0.max(), im1.max())
×
100
    t = (np.linspace(lim[0], lim[1], 3) + 1j * thr) * np.exp(-1j * phi)
×
101
    ax1.plot(t.imag, t.real, 'k--')
×
102

103
    ax1.plot(np.real(c0), np.imag(c0), 'o', color='C3')
×
104
    ax1.plot(np.real(c1), np.imag(c1), 'o', color='C4')
×
105

106
    re0, re1 = info['signal']
×
107
    x, a, b, c = info['cdf']
×
108

109
    xrange = (min(re0.min(), re1.min()), max(re0.max(), re1.max()))
×
110

111
    n0, bins0, *_ = ax2.hist(re0, bins=80, range=xrange, alpha=0.5)
×
112
    n1, bins1, *_ = ax2.hist(re1, bins=80, range=xrange, alpha=0.5)
×
113

114
    x_range = np.linspace(x.min(), x.max(), 1001)
×
115
    *_, cov0, cov1 = info['std']
×
116
    ax2.plot(
×
117
        x_range,
118
        np.sum(n0) * (bins0[1] - bins0[0]) *
119
        mult_gaussian_pdf(x_range, [r0, r1], [
120
            np.sqrt(cov0[0, 0]), np.sqrt(cov1[0, 0])
121
        ], [params[0][4], 1 - params[0][4]]))
122
    ax2.plot(
×
123
        x_range,
124
        np.sum(n1) * (bins1[1] - bins1[0]) *
125
        mult_gaussian_pdf(x_range, [r0, r1], [
126
            np.sqrt(cov0[0, 0]), np.sqrt(cov1[0, 0])
127
        ], [params[0][5], 1 - params[0][5]]))
128
    ax2.set_ylabel('Count')
×
129
    ax2.set_xlabel('Projection Axes')
×
130
    if logy:
×
131
        ax2.set_yscale('log')
×
132
        ax2.set_ylim(0.1, max(np.sum(n0), np.sum(n1)))
×
133

134
    ax3 = ax2.twinx()
×
135
    ax3.plot(x, a, '--', lw=1, color='C0')
×
136
    ax3.plot(x, b, '--', lw=1, color='C1')
×
137
    ax3.plot(x, c, 'k--', alpha=0.5, lw=1)
×
138
    ax3.set_ylim(0, 1.1)
×
139
    ax3.vlines(thr, 0, 1.1, 'k', alpha=0.5)
×
140
    ax3.set_ylabel('Integral Probability')
×
141

142
    return info
×
143

144

145
ALLXYSeq = [('I', 'I'), ('X', 'X'), ('Y', 'Y'), ('X', 'Y'), ('Y', 'X'),
×
146
            ('X/2', 'I'), ('Y/2', 'I'), ('X/2', 'Y/2'), ('Y/2', 'X/2'),
147
            ('X/2', 'Y'), ('Y/2', 'X'), ('X', 'Y/2'), ('Y', 'X/2'),
148
            ('X/2', 'X'), ('X', 'X/2'), ('Y/2', 'Y'), ('Y', 'Y/2'), ('X', 'I'),
149
            ('Y', 'I'), ('X/2', 'X/2'), ('Y/2', 'Y/2')]
150

151

152
def plotALLXY(data, ax=None):
×
153
    assert len(data) % len(ALLXYSeq) == 0
×
154

155
    if ax is None:
×
156
        ax = plt.gca()
×
157

158
    ax.plot(np.array(data), 'o-')
×
159
    repeat = len(data) // len(ALLXYSeq)
×
160
    ax.set_xticks(np.arange(len(ALLXYSeq)) * repeat + 0.5 * (repeat - 1))
×
161
    ax.set_xticklabels([','.join(seq) for seq in ALLXYSeq], rotation=60)
×
162
    ax.grid(which='major')
×
163

164

165
def plot_mat(rho, title='$\\chi$', cmap='coolwarm'):
×
166
    lim = np.abs(rho).max()
×
167
    N = rho.shape[0]
×
168

169
    fig = plt.figure(figsize=(6, 4))
×
170
    fig.suptitle(title)
×
171

172
    ax1 = plt.subplot(121)
×
173
    cax1 = ax1.imshow(rho.real, vmin=-lim, vmax=lim, cmap=cmap)
×
174
    ax1.set_title('Re')
×
175
    ax1.set_xticks(np.arange(N))
×
176
    ax1.set_yticks(np.arange(N))
×
177

178
    ax2 = plt.subplot(122)
×
179
    cax2 = ax2.imshow(rho.imag, vmin=-lim, vmax=lim, cmap=cmap)
×
180
    ax2.set_title('Im')
×
181
    ax2.set_xticks(np.arange(N))
×
182
    ax2.set_yticks(np.arange(N))
×
183

184
    plt.subplots_adjust(bottom=0.2, right=0.9, top=0.95)
×
185

186
    cbar_ax = fig.add_axes([0.15, 0.15, 0.7, 0.05])
×
187
    cb = fig.colorbar(cax1, cax=cbar_ax, orientation='horizontal')
×
188
    plt.show()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc