|
| 1 | +import pickle |
| 2 | + |
| 3 | +from c3.c3objs import Quantity |
| 4 | +from c3.libraries.envelopes import envelopes |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +ts = np.linspace(0, 10e-9, 100) |
| 9 | + |
| 10 | +with open("test/envelopes.pickle", "rb") as filename: |
| 11 | + test_data = pickle.load(filename) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.unit |
| 15 | +def test_pwc_shape(): |
| 16 | + params = { |
| 17 | + "t_bin_start": Quantity(1e-10), |
| 18 | + "t_bin_end": Quantity(9.9e-9), |
| 19 | + "t_final": Quantity(10e-9), |
| 20 | + "inphase": Quantity([0, 0.1, 0.3, 0.5, 0.1, 1.1, 0.4, 0.1]), |
| 21 | + } |
| 22 | + np.testing.assert_allclose( |
| 23 | + actual=envelopes["pwc_shape"](t=ts, params=params), |
| 24 | + desired=test_data["pwc_shape"], |
| 25 | + ) |
| 26 | + |
| 27 | + np.testing.assert_allclose( |
| 28 | + actual=envelopes["pwc_symmetric"](t=ts, params=params), |
| 29 | + desired=test_data["pwc_symmetric"], |
| 30 | + ) |
| 31 | + |
| 32 | + np.testing.assert_allclose( |
| 33 | + actual=envelopes["pwc_shape_plateau"](t=ts, params=params), |
| 34 | + desired=test_data["pwc_shape_plateau1"], |
| 35 | + ) |
| 36 | + |
| 37 | + params["width"] = Quantity(5e-9) |
| 38 | + np.testing.assert_allclose( |
| 39 | + actual=envelopes["pwc_shape_plateau"](t=ts, params=params), |
| 40 | + desired=test_data["pwc_shape_plateau2"], |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.unit |
| 45 | +def test_delta_pulse(): |
| 46 | + params = { |
| 47 | + "t_sig": Quantity( |
| 48 | + [ |
| 49 | + 0.5e-9, |
| 50 | + ] |
| 51 | + ), |
| 52 | + "t_final": Quantity(10e-9), |
| 53 | + } |
| 54 | + np.testing.assert_allclose( |
| 55 | + actual=envelopes["delta_pulse"](t=ts, params=params), |
| 56 | + desired=test_data["delta_pulse"], |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.unit |
| 61 | +def test_fourier(): |
| 62 | + params = { |
| 63 | + "amps": Quantity([0.5, 0.2]), |
| 64 | + "freqs": Quantity([1e6, 1e10]), |
| 65 | + "phases": Quantity([0, 1]), |
| 66 | + "t_final": Quantity(10e-9), |
| 67 | + } |
| 68 | + |
| 69 | + np.testing.assert_allclose( |
| 70 | + actual=envelopes["fourier_sin"](t=ts, params=params), |
| 71 | + desired=test_data["fourier_sin"], |
| 72 | + ) |
| 73 | + |
| 74 | + np.testing.assert_allclose( |
| 75 | + actual=envelopes["fourier_cos"](t=ts, params=params), |
| 76 | + desired=test_data["fourier_cos"], |
| 77 | + ) |
| 78 | + |
| 79 | + params = { |
| 80 | + "width": Quantity(9e-9), |
| 81 | + "fourier_coeffs": Quantity([1, 0.5, 0.2]), |
| 82 | + "offset": Quantity(0.1), |
| 83 | + "amp": Quantity(0.5), |
| 84 | + "t_final": Quantity(10e-9), |
| 85 | + } |
| 86 | + |
| 87 | + np.testing.assert_allclose( |
| 88 | + actual=envelopes["slepian_fourier"](t=ts, params=params), |
| 89 | + desired=test_data["slepian_fourier"], |
| 90 | + ) |
| 91 | + |
| 92 | + params["risefall"] = Quantity(4e-9) |
| 93 | + np.testing.assert_allclose( |
| 94 | + actual=envelopes["slepian_fourier"](t=ts, params=params), |
| 95 | + desired=test_data["slepian_fourier_risefall"], |
| 96 | + ) |
| 97 | + |
| 98 | + params["sin_coeffs"] = Quantity([0.3]) |
| 99 | + np.testing.assert_allclose( |
| 100 | + actual=envelopes["slepian_fourier"](t=ts, params=params), |
| 101 | + desired=test_data["slepian_fourier_sin"], |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.unit |
| 106 | +def test_flattop(): |
| 107 | + params = { |
| 108 | + "risefall": Quantity(2e-9), |
| 109 | + "t_final": Quantity(10e-9), |
| 110 | + } |
| 111 | + |
| 112 | + np.testing.assert_allclose( |
| 113 | + actual=envelopes["trapezoid"](t=ts, params=params), |
| 114 | + desired=test_data["trapezoid"], |
| 115 | + ) |
| 116 | + |
| 117 | + np.testing.assert_allclose( |
| 118 | + actual=envelopes["flattop_risefall"](t=ts, params=params), |
| 119 | + desired=test_data["flattop_risefall"], |
| 120 | + ) |
| 121 | + |
| 122 | + np.testing.assert_allclose( |
| 123 | + actual=envelopes["flattop_risefall_1ns"](t=ts, params=params), |
| 124 | + desired=test_data["flattop_risefall_1ns"], |
| 125 | + ) |
| 126 | + |
| 127 | + params = { |
| 128 | + "ramp": Quantity(2e-9), |
| 129 | + "t_up": Quantity(1e-9), |
| 130 | + "t_down": Quantity(10e-9), |
| 131 | + "t_final": Quantity(10e-9), |
| 132 | + } |
| 133 | + |
| 134 | + np.testing.assert_allclose( |
| 135 | + actual=envelopes["flattop_variant"](t=ts, params=params), |
| 136 | + desired=test_data["flattop_variant"], |
| 137 | + ) |
| 138 | + |
| 139 | + params = { |
| 140 | + "risefall": Quantity(2e-9), |
| 141 | + "t_up": Quantity(1e-9), |
| 142 | + "t_down": Quantity(10e-9), |
| 143 | + "t_final": Quantity(10e-9), |
| 144 | + } |
| 145 | + |
| 146 | + np.testing.assert_allclose( |
| 147 | + actual=envelopes["flattop"](t=ts, params=params), desired=test_data["flattop"] |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.unit |
| 152 | +def test_flattop_cut(): |
| 153 | + params = { |
| 154 | + "risefall": Quantity(2e-9), |
| 155 | + "t_up": Quantity(1e-9), |
| 156 | + "t_down": Quantity(10e-9), |
| 157 | + "t_final": Quantity(10e-9), |
| 158 | + } |
| 159 | + |
| 160 | + np.testing.assert_allclose( |
| 161 | + actual=envelopes["flattop_cut"](t=ts, params=params), |
| 162 | + desired=test_data["flattop_cut"], |
| 163 | + ) |
| 164 | + |
| 165 | + params = { |
| 166 | + "risefall": Quantity(2e-9), |
| 167 | + "width": Quantity(9e-9), |
| 168 | + "t_final": Quantity(10e-9), |
| 169 | + } |
| 170 | + |
| 171 | + np.testing.assert_allclose( |
| 172 | + actual=envelopes["flattop_cut_center"](t=ts, params=params), |
| 173 | + desired=test_data["flattop_cut_center"], |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.unit |
| 178 | +def test_gaussian(): |
| 179 | + params = { |
| 180 | + "t_final": Quantity(10e-9), |
| 181 | + "sigma": Quantity(5e-9), |
| 182 | + } |
| 183 | + |
| 184 | + np.testing.assert_allclose( |
| 185 | + actual=envelopes["gaussian_sigma"](t=ts, params=params), |
| 186 | + desired=test_data["gaussian_sigma"], |
| 187 | + ) |
| 188 | + |
| 189 | + np.testing.assert_allclose( |
| 190 | + actual=envelopes["gaussian"](t=ts, params=params), desired=test_data["gaussian"] |
| 191 | + ) |
| 192 | + |
| 193 | + np.testing.assert_allclose( |
| 194 | + actual=envelopes["gaussian_nonorm"](t=ts, params=params), |
| 195 | + desired=test_data["gaussian_nonorm"], |
| 196 | + ) |
| 197 | + |
| 198 | + np.testing.assert_allclose( |
| 199 | + actual=envelopes["gaussian_der_nonorm"](t=ts, params=params), |
| 200 | + desired=test_data["gaussian_der_nonorm"], |
| 201 | + ) |
| 202 | + |
| 203 | + np.testing.assert_allclose( |
| 204 | + actual=envelopes["gaussian_der"](t=ts, params=params), |
| 205 | + desired=test_data["gaussian_der"], |
| 206 | + ) |
| 207 | + |
| 208 | + np.testing.assert_allclose( |
| 209 | + actual=envelopes["drag_sigma"](t=ts, params=params), |
| 210 | + desired=test_data["drag_sigma"], |
| 211 | + ) |
| 212 | + |
| 213 | + np.testing.assert_allclose( |
| 214 | + actual=envelopes["drag_der"](t=ts, params=params), desired=test_data["drag_der"] |
| 215 | + ) |
| 216 | + |
| 217 | + np.testing.assert_allclose( |
| 218 | + actual=envelopes["drag"](t=ts, params=params), desired=test_data["drag"] |
| 219 | + ) |
| 220 | + |
| 221 | + |
| 222 | +@pytest.mark.unit |
| 223 | +def test_cosine(): |
| 224 | + params = { |
| 225 | + "t_final": Quantity(10e-9), |
| 226 | + } |
| 227 | + |
| 228 | + np.testing.assert_allclose( |
| 229 | + actual=envelopes["cosine"](t=ts, params=params), desired=test_data["cosine"] |
| 230 | + ) |
| 231 | + |
| 232 | + params = { |
| 233 | + "t_final": Quantity(10e-9), |
| 234 | + "t_rise": Quantity(2e-9), |
| 235 | + } |
| 236 | + |
| 237 | + np.testing.assert_allclose( |
| 238 | + actual=envelopes["cosine_flattop"](t=ts, params=params), |
| 239 | + desired=test_data["cosine_flattop"], |
| 240 | + ) |
| 241 | + |
| 242 | + |
| 243 | +@pytest.mark.unit |
| 244 | +def test_nodrive(): |
| 245 | + params = {} |
| 246 | + np.testing.assert_allclose( |
| 247 | + actual=envelopes["no_drive"](t=ts, params=params), desired=test_data["no_drive"] |
| 248 | + ) |
| 249 | + |
| 250 | + np.testing.assert_allclose( |
| 251 | + actual=envelopes["rect"](t=ts, params=params), desired=test_data["rect"] |
| 252 | + ) |
| 253 | + |
| 254 | + |
| 255 | +# test_pwc_envelope() |
| 256 | +test_pwc_shape() |
| 257 | +test_delta_pulse() |
| 258 | +test_fourier() |
| 259 | +test_flattop() |
| 260 | +test_flattop_cut() |
| 261 | +test_cosine() |
| 262 | +test_gaussian() |
| 263 | +test_nodrive() |
| 264 | + |
| 265 | +with open("test/envelopes.pickle", "wb") as filename: |
| 266 | + pickle.dump(test_data, filename) |
0 commit comments