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
|
"""Benchmark the _convolution_coefficient function."""
import argparse
import datetime
import pickle
import socket
import timeit
import numpy as np
import matplotlib.pyplot as plt
import qmix
from qmix.qtcurrent import calculate_phase_factor_coeff
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--save', action="store_true", help="save speed run?")
parser.add_argument('-t', '--tone', type=int, help="tone to run")
parser.add_argument('-o', '--overwrite', action="store_true", help="overwrite stored value")
args = parser.parse_args()
# Setup ----------------------------------------------------------------------
resp = qmix.respfn.RespFnPolynomial(50, verbose=False)
num_b = (15, 5, 5, 5)
print("\n\tRUNNING SPEED TEST:\n")
# 1 tone ---------------------------------------------------------------------
if args.tone is None or args.tone == 1:
cct = qmix.circuit.EmbeddingCircuit(1, 2)
cct.freq[1] = 0.3
vj = cct.initialize_vj()
vj[1, 1, :] = 0.3
vj[1, 2, :] = 0.03
def one_tone():
calculate_phase_factor_coeff(vj, cct.freq, 1, 2, num_b)
t_1tone = min(timeit.Timer(one_tone).repeat(1000, 1))
print("1 tone:\t\t{:.2f} ms".format(t_1tone*1000))
ccc1 = calculate_phase_factor_coeff(vj, cct.freq, 1, 2, num_b)
if args.overwrite:
print(" -> Save ccc1")
with open('data/coeff1.data', 'wb') as f:
pickle.dump(ccc1, f)
else:
print(" -> Load ccc1")
with open('data/coeff1.data', 'rb') as f:
ccc1_test = pickle.load(f)
np.testing.assert_allclose(ccc1, ccc1_test, atol=1e-7)
# plt.plot(ccc1)
# plt.plot(ccc1_test)
# plt.show()
# 2 tone ---------------------------------------------------------------------
if args.tone is None or args.tone == 2:
cct = qmix.circuit.EmbeddingCircuit(2, 2)
cct.freq[1] = 0.30
cct.freq[2] = 0.33
vj = cct.initialize_vj()
vj[1, 1, :] = 0.3
vj[2, 1, :] = 0.1
vj[1, 2, :] = 0.03
vj[2, 2, :] = 0.01
def two_tone():
calculate_phase_factor_coeff(vj, cct.freq, 2, 2, num_b)
t_2tone = min(timeit.Timer(two_tone).repeat(600, 1))
print("2 tones:\t{:.2f} ms".format(t_2tone*1000))
ccc2 = calculate_phase_factor_coeff(vj, cct.freq, 2, 2, num_b)
if args.overwrite:
print(" -> Save ccc2")
with open('data/coeff2.data', 'wb') as f:
pickle.dump(ccc2, f)
else:
print(" -> Load ccc2")
with open('data/coeff2.data', 'rb') as f:
ccc2_test = pickle.load(f)
np.testing.assert_allclose(ccc2, ccc2_test, atol=1e-7)
# 3 tone ---------------------------------------------------------------------
if args.tone is None or args.tone == 3:
cct = qmix.circuit.EmbeddingCircuit(3, 2)
cct.freq[1] = 0.30
cct.freq[2] = 0.33
cct.freq[3] = 0.27
vj = cct.initialize_vj()
vj[1, 1, :] = 0.3
vj[2, 1, :] = 0.1
vj[3, 1, :] = 0.1
vj[1, 2, :] = 0.03
vj[2, 2, :] = 0.01
vj[3, 2, :] = 0.01
def three_tone():
calculate_phase_factor_coeff(vj, cct.freq, 3, 2, num_b)
t_3tone = min(timeit.Timer(three_tone).repeat(400, 1))
print("3 tones:\t{:.2f} ms".format(t_3tone*1000))
ccc3 = calculate_phase_factor_coeff(vj, cct.freq, 3, 2, num_b)
if args.overwrite:
print(" -> Save ccc3")
with open('data/coeff3.data', 'wb') as f:
pickle.dump(ccc3, f)
else:
print(" -> Load ccc3")
with open('data/coeff3.data', 'rb') as f:
ccc3_test = pickle.load(f)
np.testing.assert_allclose(ccc3, ccc3_test, atol=1e-7)
# 4 tone ---------------------------------------------------------------------
if args.tone is None or args.tone == 4:
cct = qmix.circuit.EmbeddingCircuit(4, 2)
cct.freq[1] = 0.30
cct.freq[2] = 0.33
cct.freq[3] = 0.27
cct.freq[4] = 0.03
vj = cct.initialize_vj()
vj[1, 1, :] = 0.3
vj[2, 1, :] = 0.1
vj[3, 1, :] = 0.1
vj[4, 1, :] = 0.0
vj[1, 2, :] = 0.03
vj[2, 2, :] = 0.01
vj[3, 2, :] = 0.01
vj[4, 2, :] = 0.00
def four_tone():
calculate_phase_factor_coeff(vj, cct.freq, 4, 2, num_b)
t_4tone = min(timeit.Timer(four_tone).repeat(200, 1))
print("4 tones:\t{:.2f} ms".format(t_4tone*1000))
ccc4 = calculate_phase_factor_coeff(vj, cct.freq, 4, 2, num_b)
if args.overwrite:
print(" -> Save ccc4")
with open('data/coeff4.data', 'wb') as f:
pickle.dump(ccc4, f)
else:
print(" -> Load ccc4")
with open('data/coeff4.data', 'rb') as f:
ccc4_test = pickle.load(f)
np.testing.assert_allclose(ccc4, ccc4_test, atol=1e-7)
print("")
# WRITE TO FILE --------------------------------------------------------------
if args.tone is None:
if args.save:
with open('results/coeff.txt', 'a') as f:
now = datetime.datetime.now()
machine = socket.gethostname()
msg = "{}\t{:.6f}\t{:.6f}\t{:.6f}\t{:.6f}\t{}\n"
msg = msg.format(now, t_1tone, t_2tone, t_3tone, t_4tone, machine)
f.write(msg)
|