def test_1D_single(self):
     session = None
     ary = np.array([3.0, 2.1, 1.3, 1.8, 5.7])
     sources = make_data_sources(session, "none", ary)
     assert_almost_equal(sources[0][0].get_data(), np.arange(len(ary)))
     assert_almost_equal(sources[0][1].get_data(), ary)
     return
Beispiel #2
0
 def test_CYP_p2s(self):
     wcslibout = self.wazp.wcs.p2s([[-10, 30]], 1)
     wcs_phi = wcslibout['phi']
     wcs_theta = wcslibout['theta']
     phi, theta = self.azp(-10, 30)
     utils.assert_almost_equal(np.asarray(phi), wcs_phi)
     utils.assert_almost_equal(np.asarray(theta), wcs_theta)
Beispiel #3
0
 def test_default_pars(self):
     self.cheb2.parameters = np.arange(9)
     p = np.array([1344., 1772., 400., 1860., 2448., 552., 432., 568.,
                   128.])
     z = self.cheb2(self.x, self.y)
     self.fitter(self.x, self.y, z)
     utils.assert_almost_equal(self.cheb2.parameters, p)
Beispiel #4
0
 def test_gross_pnl_calculation(self):
     tick = self.goog[0]
     self.backtest.trades.append(Trade('buy', tick))
     self.assertEqual(-100.34, self.backtest.gross)
     tick = self.goog[10]
     self.backtest.trades.append(Trade('sell', tick))
     assert_almost_equal(1.17, self.backtest.gross)
Beispiel #5
0
 def test_single_array_input(self):
     model = SummedCompositeModel([self.p1, self.p11])
     result = model(self.x)
     delta11 = self.p11(self.x)
     delta1 = self.p1(self.x)
     xx = delta1 + delta11
     utils.assert_almost_equal(xx, result)
Beispiel #6
0
 def test_labeledinput_1(self):
     # Note: No actual use of LabeledInput in this test; this just uses the
     # same name for symmetry with the old tests
     p1 = Polynomial1D(3, c0=1, c1=2, c2=3, c3=4)
     p2 = Polynomial2D(3, c0_0=1, c2_0=2, c0_1=3, c2_1=4)
     m = p2 | p1
     assert_almost_equal(p1(p2(self.x, self.y)), m(self.x, self.y))
Beispiel #7
0
 def test_multiple_input(self):
     rot = models.Rotation2D(angle=-60)
     model = SerialCompositeModel([rot, rot])
     xx, yy = model(self.x, self.y)
     x1, y1 = model.inverse(xx, yy)
     utils.assert_almost_equal(x1, self.x)
     utils.assert_almost_equal(y1, self.y)
Beispiel #8
0
def test_chunk_unchunk_grad2():
  n_time = 101
  n_batch = 3
  n_dim = 5
  numpy.random.seed(1234)
  _x = numpy.random.randn(n_time, n_batch, n_dim).astype(f32)
  _Dx2 = numpy.random.randn(n_time, n_batch, n_dim).astype(f32)
  _index = numpy.ones((n_time, n_batch), dtype="int8")
  x = T.as_tensor(_x)
  Dx2 = T.as_tensor(_Dx2)
  index = T.as_tensor(_index)
  chunk_size = 11
  chunk_step = 7

  out, oindex = chunk(x, index=index, chunk_size=chunk_size, chunk_step=chunk_step)
  chunk_op = NativeOp.Chunking().make_op()
  assert type(out.owner.op) is type(chunk_op)

  x2, index2, factors = unchunk(out, index=oindex, chunk_size=chunk_size, chunk_step=chunk_step, n_time=x.shape[0], n_batch=x.shape[1])
  unchunk_op = NativeOp.UnChunking().make_op()
  assert type(x2.owner.op) is type(unchunk_op)

  Dout, _, _, _, _, _ = unchunk_op.grad(x2.owner.inputs, (Dx2, None, None))
  Dx, _, _, _, _ = chunk_op.grad(out.owner.inputs, (Dout, None))
  _Dx = Dx.eval()
  assert_almost_equal(_Dx, _Dx2)
Beispiel #9
0
def test_Updater_add_check_numerics_ops():
  class _Layer(DummyLayer):
    def _get_loss_value(self):
      return tf.log(self.x)

  from TFNetwork import TFNetwork, ExternData
  from Config import Config

  with make_scope() as session:
    config = Config()
    config.set("debug_add_check_numerics_ops", True)
    network = TFNetwork(extern_data=ExternData(), train_flag=True)
    network.add_layer(name="output", layer_class=_Layer, initial_value=1.0)
    network.initialize_params(session=session)

    updater = Updater(config=config, network=network)
    updater.set_learning_rate(1.0, session=session)
    updater.set_trainable_vars(network.get_trainable_params())
    updater.init_optimizer_vars(session=session)
    # Should succeed.
    session.run(updater.get_optim_op())
    # One gradient descent step from ln(x), x = 1.0: gradient is 1.0 / x, thus x - 1.0 = 0.0.
    assert_almost_equal(session.run(network.get_default_output_layer().output.placeholder), 0.0)

    try:
      # Now, should fail.
      session.run(updater.get_optim_op())
    except tf.errors.InvalidArgumentError as exc:
      print("Expected exception: %r" % exc)
    else:
      assert False, "should have raised an exception"
Beispiel #10
0
def test_Stats():
  rnd = numpy.random.RandomState(42)
  m = rnd.uniform(-2., 10., (1000, 3))
  mean_ref = numpy.mean(m, axis=0)
  var_ref = numpy.var(m, axis=0)
  std_dev_ref = numpy.std(m, axis=0)
  print("ref mean/var/stddev:", mean_ref, var_ref, std_dev_ref)
  assert_almost_equal(numpy.sqrt(var_ref), std_dev_ref)
  stats = Stats()
  t = 0
  while t < len(m):
    s = int(rnd.uniform(10, 100))
    m_sub = m[t:t + s]
    print("sub seq from t=%i, len=%i" % (t, len(m_sub)))
    stats.collect(m_sub)
    t += s
  mean = stats.get_mean()
  std_dev = stats.get_std_dev()
  print("mean/stddev:", mean, std_dev)
  assert_almost_equal(mean, mean_ref)
  assert_almost_equal(std_dev, std_dev_ref)
  m -= mean[None, :]
  m /= std_dev[None, :]
  stats2 = Stats()
  t = 0
  while t < len(m):
    s = int(rnd.uniform(10, 100))
    m_sub = m[t:t + s]
    stats2.collect(m_sub)
    t += s
  mean0 = stats2.get_mean()
  stddev1 = stats2.get_std_dev()
  print("normalized mean/stddev:", mean0, stddev1)
  assert_almost_equal(mean0, 0.)
  assert_almost_equal(stddev1, 1.)
Beispiel #11
0
 def test_derived(self):
     """ Derived parameters should have expected values """
     domain = Domain(2, 32, 200.0, 193.2)
     self.assertEqual(domain.total_samples, 64)
     self.assertEqual(domain.window_t, 400.0)
     assert_almost_equal(domain.centre_omega, 1213.911401347, 9)
     assert_almost_equal(domain.centre_lambda, 1551.720797, 6)
Beispiel #12
0
 def test_labeledinput(self):
     ado = LabeledInput([self.x, self.y], ['x', 'y'])
     scomptr = SCompositeModel([self.p2, self.p1], [['x', 'y'], ['z']], [['z'], ['z']])
     sresult = scomptr(ado)
     z = self.p2(self.x, self.y)
     z1 = self.p1(z)
     utils.assert_almost_equal(z1, sresult.z)
Beispiel #13
0
    def test_ss_agrawal(self):
        """ Test splitstep Agrawal method. """
        self.parameters["method"] = "SS_AGRAWAL"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 4)
Beispiel #14
0
def test_Pix2Sky(code):
    """Check astropy model eval against wcslib eval"""

    wcs_map = os.path.join(os.pardir, os.pardir, "wcs", "tests", "maps",
                           "1904-66_{0}.hdr".format(code))
    test_file = get_pkg_data_filename(wcs_map)
    header = fits.Header.fromfile(test_file, endcard=False, padding=False)

    params = []
    for i in range(3):
        key = 'PV2_{0}'.format(i + 1)
        if key in header:
            params.append(header[key])

    w = wcs.WCS(header)
    w.wcs.crval = [0., 0.]
    w.wcs.crpix = [0, 0]
    w.wcs.cdelt = [1, 1]
    wcslibout = w.wcs.p2s([PIX_COORDINATES], 1)
    wcs_phi = wcslibout['phi']
    wcs_theta = wcslibout['theta']
    model = getattr(projections, 'Pix2Sky_' + code)
    tanprj = model(*params)
    phi, theta = tanprj(*PIX_COORDINATES)
    utils.assert_almost_equal(np.asarray(phi), wcs_phi)
    utils.assert_almost_equal(np.asarray(theta), wcs_theta)
Beispiel #15
0
def test_update():
    data = ex4()
    X = add_bias(data['x'])
    theta = array([[.01, 0.01, 0.01]])
    y = vstack([data['y'], 1 - data['y']]).T

    assert_almost_equal(update(X, y, theta, rho=.05, l=1.), [[-0.5585, 0.0146, 0.0150]], decimal=3)
Beispiel #16
0
def test_p_bin():
    X = array([[1, 0, 1], [0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 1], [1, 0, 0]])
    theta = array([[1, 2, 3]])

    assert_almost_equal(prob(X, theta), [[0.9820, 0.01799], [0.9526, 0.04743], [0.9820, 0.01799],
                                         [0.7311, 0.2689], [0.9975, 0.002473], [0.7311, 0.2689]],
                        decimal=3)
Beispiel #17
0
 def test_change_par(self):
     """
     Test that a change to one parameter as a set propagates to param_sets.
     """
     self.gmodel.amplitude = [1, 10]
     utils.assert_almost_equal(self.gmodel.param_sets, np.array([[1.0, 10], [3.5, 5.2], [0.4, 0.7]]))
     np.all(self.gmodel.parameters == [1.0, 10.0, 3.5, 5.2, 0.4, 0.7])
Beispiel #18
0
def test_p_mult():
    X = array([[1, 0, 1], [0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 1], [1, 0, 0]])
    theta = array([[1, 0, 3],
                   [0, 2, 1],
                   [2, 3, 1]])
    assert_almost_equal(prob(X, theta), [[0.696, 0.0347, 0.256, 0.0128],
                                         [0.757, 0.1025, 0.1025, 0.0377],
                                         [0.696, 0.0347, 0.256, 0.0128],
                                         [0.225, 0.0826, 0.610, 0.0826],
                                         [0.114, 0.0419, 0.842, 0.00209],
                                         [0.225, 0.0826, 0.610, 0.0826]],
                        decimal=3)

    data = ex4()
    X = add_bias(data['x'])
    theta = array([[.01, 0.01, 0.01]])

    assert_almost_equal(prob(X, theta)[:, 0], [0.7790, 0.7747, 0.8030, 0.7875, 0.7604, 0.7712, 0.7586, 0.7649,
                                               0.7982, 0.7850, 0.7613, 0.7721, 0.7841, 0.7455, 0.7799, 0.7649,
                                               0.7721, 0.7941, 0.7613, 0.7521, 0.7512, 0.7558, 0.7398, 0.7604,
                                               0.7359, 0.7211, 0.7446, 0.7841, 0.7150, 0.7436, 0.7799, 0.7408,
                                               0.7512, 0.7658, 0.7577, 0.7738, 0.7221, 0.7558, 0.7586, 0.7455,
                                               0.7120, 0.7130, 0.7016, 0.6846, 0.7221, 0.7369, 0.7058, 0.7079,
                                               0.6559, 0.7211, 0.7231, 0.6964, 0.7037, 0.7465, 0.7408, 0.7530,
                                               0.7301, 0.6835, 0.7301, 0.7058, 0.7604, 0.7613, 0.7340, 0.6974,
                                               0.7271, 0.7110, 0.7658, 0.7291, 0.7568, 0.7465, 0.7359, 0.7503,
                                               0.6932, 0.7417, 0.6963, 0.7037, 0.7389, 0.7191, 0.7099, 0.7359],
                        decimal=3)
Beispiel #19
0
def test_add_check_numerics_ops():
  with make_scope() as session:
    x = tf.constant(3.0, name="x")
    y = tf.log(x * 3, name="y")
    assert isinstance(y, tf.Tensor)
    assert_almost_equal(session.run(y), numpy.log(9.))
    check = add_check_numerics_ops([y])
    session.run(check)
    z1 = tf.log(x - 3, name="z1")
    assert_equal(str(session.run(z1)), "-inf")
    z2 = tf.log(x - 4, name="z2")
    assert_equal(str(session.run(z2)), "nan")
    check1 = add_check_numerics_ops([z1])
    try:
      session.run(check1)
    except tf.errors.InvalidArgumentError as exc:
      print("Expected exception: %r" % exc)
    else:
      assert False, "should have raised an exception"
    check2 = add_check_numerics_ops([z2])
    try:
      session.run(check2)
    except tf.errors.InvalidArgumentError as exc:
      print("Expected exception: %r" % exc)
    else:
      assert False, "should have raised an exception"
Beispiel #20
0
def test_iris_benchmark():
    data = iris()
    x = add_bias(data['x'])
    y = binarize(data['y'])

    train_split = [12, 39, 23, 5, 3, 29, 49, 47, 21, 30, 34, 48, 20, 45, 31, 27, 17, 22,
                   41, 6, 40, 38, 42, 19, 26, 15, 35, 10, 46, 25, 0, 32, 1, 16, 4, 13,
                   24, 33, 43, 18, 81, 65, 62, 50, 93, 92, 53, 58, 87, 55, 70, 72, 83,
                   56, 52, 73, 78, 64, 68, 59, 74, 89, 67, 51, 66, 98, 90, 69, 95, 63,
                   82, 54, 86, 85, 96, 97, 79, 71, 94, 80, 142, 147, 125, 145, 119, 101,
                   141, 105, 129, 138, 122, 120, 139, 124, 134, 111, 148, 117, 132, 133,
                   104, 130, 128, 115, 127, 131, 136, 112, 107, 143, 149, 106, 109, 108,
                   102, 100, 126, 103, 146, 113]

    test_split = [2, 7, 8, 9, 11, 14, 28, 36, 37, 44, 57, 60, 61, 75, 76, 77, 84, 88,
                  91, 99, 110, 114, 116, 118, 121, 123, 135, 137, 140, 144]

    xTrain = x[train_split, :]
    yTrain = y[train_split, :]
    xTest = x[test_split, :]
    yTest = y[test_split, :]

    model = OVRClassifier(LogisticModel(rho=1.)).train(xTrain, yTrain, verbose=False)
    pred = binarize(model.predict(xTest))
    assert_almost_equal(accuracy_score(yTest, pred), 0.96667, decimal=3)
Beispiel #21
0
    def test_rk4ip(self):
        """ Test Runge-kutta in the interaction picture. """
        self.parameters["method"] = "RK4IP"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 5)
Beispiel #22
0
def test_Sky2Pix(code):
    """Check astropy model eval against wcslib eval"""

    wcs_map = os.path.join(os.pardir, os.pardir, "wcs", "tests", "maps",
                           "1904-66_{0}.hdr".format(code))
    test_file = get_pkg_data_filename(wcs_map)
    header = fits.Header.fromfile(test_file, endcard=False, padding=False)

    params = []
    for i in range(3):
        key = 'PV2_{0}'.format(i + 1)
        if key in header:
            params.append(header[key])

    w = wcs.WCS(header)
    w.wcs.crval = [0., 0.]
    w.wcs.crpix = [0, 0]
    w.wcs.cdelt = [1, 1]
    wcslibout = w.wcs.p2s([PIX_COORDINATES], 1)
    wcs_pix = w.wcs.s2p(wcslibout['world'], 1)['pixcrd']
    model = getattr(projections, 'Sky2Pix_' + code)
    tinv = model(*params)
    x, y = tinv(wcslibout['phi'], wcslibout['theta'])
    utils.assert_almost_equal(np.asarray(x), wcs_pix[:, 0])
    utils.assert_almost_equal(np.asarray(y), wcs_pix[:, 1])
Beispiel #23
0
    def test_ss_sym_midpoint(self):
        """ Test symmetric splitstep with midpoint method. """
        self.parameters["method"] = "SS_SYM_MIDPOINT"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 2)
Beispiel #24
0
    def test_ss_symmetric(self):
        """ Test symmetric splitstep method. """
        self.parameters["method"] = "SS_SYMMETRIC"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 0)
Beispiel #25
0
    def test_ss_reduced(self):
        """ Test reduced splitstep method. """
        self.parameters["method"] = "SS_REDUCED"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 3)
Beispiel #26
0
    def test_ss_simple(self):
        """ Test simple splitstep method. """
        self.parameters["method"] = "SS_SIMPLE"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 3)
Beispiel #27
0
 def test_single_array_input(self):
     pcomptr = PCompositeModel([self.p1, self.p11])
     presult = pcomptr(self.x)
     delta11 = self.p11(self.x)
     delta1 = self.p1(self.x)
     xx = self.x + delta1 + delta11
     utils.assert_almost_equal(xx, presult)
Beispiel #28
0
 def test_AZP_s2p(self):
     wcslibout = self.wazp.wcs.p2s([[-10, 30]],1)
     wcs_pix = self.wazp.wcs.s2p(wcslibout['world'], 1)['pixcrd']
     azpinv = projections.Sky2Pix_AZP(mu=2, gamma=30)
     x, y = azpinv(wcslibout['phi'], wcslibout['theta'])
     utils.assert_almost_equal(np.asarray(x), wcs_pix[:,0])
     utils.assert_almost_equal(np.asarray(y), wcs_pix[:,1])
Beispiel #29
0
def test_a_bin():
    X = array([[1, 0, 1], [0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 1], [1, 0, 0]])
    theta = array([[1, 2, 3]])

    assert_almost_equal(act(X, theta), [[54.598, 1.], [20.0855, 1.], [54.598, 1.],
                                        [2.7183, 1.], [403.429, 1.], [2.71828, 1.]],
                        decimal=3)
Beispiel #30
0
    def test_ss_sym_rk4(self):
        """ Test symmetric splitstep with Runge-Kutta method. """
        self.parameters["method"] = "SS_SYM_RK4"
        stepper = Stepper(**self.parameters)
        A_out = stepper(self.A_in)

        assert_almost_equal(max(np.abs(A_out) ** 2), self.P_analytical, 3)
Beispiel #31
0
    def test_labledinput_2(self):
        rot = Rotation2D(angle=23.4)
        offx = Shift(-2)
        offy = Shift(1.2)
        m = rot | (offx & Identity(1)) | (Identity(1) & offy)

        x, y = rot(self.x, self.y)
        x = offx(x)
        y = offy(y)

        assert_almost_equal(x, m(self.x, self.y)[0])
        assert_almost_equal(y, m(self.x, self.y)[1])

        a = np.deg2rad(23.4)
        # For kicks
        matrix = [[np.cos(a), -np.sin(a)],
                  [np.sin(a), np.cos(a)]]
        x, y = AffineTransformation2D(matrix, [-2, 1.2])(self.x, self.y)
        assert_almost_equal(x, m(self.x, self.y)[0])
        assert_almost_equal(y, m(self.x, self.y)[1])
def test_max_and_argmax_sparse():
    n_time = 3
    n_batch = 2
    n_dim = 5
    s0 = np.array([[0, 0], [0, 1], [1, 1], [1, 2], [1, 2], [2, 2], [2, 2]],
                  dtype=f32)
    s1 = np.array([[1, 2], [2, 3], [1, 1], [2, 0], [4, 1], [3, 3], [4, 4]],
                  dtype=f32)
    w = np.array([[1, 2], [2, 1], [1, 2], [3, 4], [5, 6], [7, 8], [9, 13]],
                 dtype=f32)
    m = np.array([[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 0]],
                 dtype=f32)
    print("W:\n%r" %
          NativeOp.sparse_to_dense(s0, s1, w, m, n_time, n_dim).eval())
    init_out_max = T.zeros((n_time, n_batch), dtype=f32)
    init_out_arg = T.zeros((n_time, n_batch), dtype=f32)
    max1, arg1 = NativeOp.max_and_argmax_sparse(s0, s1, w, m, init_out_max,
                                                init_out_arg)
    W = NativeOp.sparse_to_dense(s0, s1, w, m, n_time, n_dim)
    assert W.ndim == 3
    max2, arg2 = T.max_and_argmax(W, axis=2)
    arg0 = np.array([[2, 2], [4, 1], [4, 3]])
    max0 = np.array([[2, 2], [5, 2], [9, 8]])
    arg1 = arg1.eval()
    arg2 = arg2.eval()
    max1 = max1.eval()
    max2 = max2.eval()
    print("arg0:\n%r" % arg0)
    print("arg1:\n%r" % arg1)
    print("arg2:\n%r" % arg2)
    print("max0:\n%r" % max0)
    print("max1:\n%r" % max1)
    print("max2:\n%r" % max2)
    assert_almost_equal(arg0, arg1)
    assert_almost_equal(arg0, arg2)
    assert_almost_equal(max0, max1)
    assert_almost_equal(max0, max2)
Beispiel #33
0
def test_multiclass_merge_estimators():
    coef1 = np.array([[1., 2, 3], [4, 5, 6], [7., 8, 9]])
    coef2 = np.array([[10., 11, 12]])
    coef3 = np.array([[13., 14, 15], [16, 17, 18], [19, 20, 21]])

    intercept1 = np.array([[1.], [2], [3]])
    intercept2 = np.array([[4.]])
    intercept3 = np.array([[5.], [6], [7]])

    classes1 = np.array([0, 1, 2])
    classes2 = np.array([1, 2])
    classes3 = np.array([1, 2, 3])

    def f(coef, intercept, classes):
        s = SGDClassifier()
        s.coef_ = coef
        s.intercept_ = intercept
        s.classes_ = classes
        return s

    ests = [
        f(coef1, intercept1, classes1),
        f(coef2, intercept2, classes2),
        f(coef3, intercept3, classes3)
    ]

    coef = np.zeros((4, 3), dtype='f8')
    coef[classes1] += coef1
    coef[[classes2[1]]] += coef2
    coef[classes3] += coef3
    coef /= 3

    intercept = np.zeros((4, 1), dtype='f8')
    intercept[classes1] += intercept1
    intercept[[classes2[1]]] += intercept2
    intercept[classes3] += intercept3
    intercept /= 3

    res = merge_estimators(ests)
    tm.assert_almost_equal(res.coef_, coef)
    tm.assert_almost_equal(res.intercept_, intercept)
    tm.assert_almost_equal(res.classes_, np.arange(4))
Beispiel #34
0
 def test_recorder_a(self):
     """
     test some statistical operations
     """
     p_1 = {(1, ) : 0.1,
            (2, ) : 0.2,
            (3, ) : 0.3,
            (4, ) : 0.4,}
     
     rec = cmepy.recorder.create((('foo',),))
     
     rec.write(1.0, p_1)
     
     ev = rec['foo'].expected_value
     assert_almost_equal(ev, numpy.array([[3.0]]))
     
     var = rec['foo'].variance
     assert_almost_equal(var, numpy.array([1.0]))
     std_dev = rec['foo'].standard_deviation
     assert_almost_equal(std_dev, numpy.array([1.0]))
Beispiel #35
0
def test_strided_slice_grad_and_back_grad():
    shape = (3, 2, 2)
    from tensorflow.python.ops.gen_array_ops import strided_slice_grad
    x = tf.reshape(tf.range(0, numpy.prod(shape), dtype=tf.float32), shape)
    x_ = x[0::1]
    y = strided_slice_grad(shape=tf.convert_to_tensor(shape),
                           begin=tf.constant([0]),
                           end=tf.constant([0], dtype=tf.int32),
                           end_mask=1,
                           strides=tf.constant([1]),
                           dy=x_)
    y.set_shape(x.get_shape())
    vx, vx_, vy = session.run([x, x_, y])
    print("vx, vx_, vy:", vx, vx_, vy)
    assert_almost_equal(vx, vx_)
    assert_almost_equal(vx, vy)
    dy = tf.reshape(
        tf.range(0, numpy.prod(shape), dtype=tf.float32) * 0.1 - 5.135, shape)
    dx, = tf.gradients(ys=[y], xs=[x], grad_ys=[dy])
    vdx, vdy = session.run([dx, dy])
    print("vdx, vdy:", vdx, vdy)
    assert_almost_equal(vdx, vdy)
Beispiel #36
0
 def test_recorder_b(self):
     """
     test out some more statistical operations
     """
     p_1 = {(1, ) : 0.1,
            (2, ) : 0.2,
            (3, ) : 0.3,
            (4, ) : 0.4,}
     
     # map states via pairwise aggregation
     even = lambda *x : x[0]/2
     odd = lambda *x : x[0]/2 + 1
     
     rec = cmepy.recorder.create((('even', 'odd'), (even, odd)))
     
     rec.write(1.0, p_1)
     
     even_ev = rec['even'].expected_value
     assert_almost_equal(even_ev, numpy.array([[1.3]]))
     odd_ev = rec['odd'].expected_value
     assert_almost_equal(odd_ev, numpy.array([[2.3]]))
     
     cov = rec[('even', 'odd')].covariance
     assert_almost_equal(cov, numpy.array([0.41]))
Beispiel #37
0
 def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True):
     """
     Alternative naming for assertArrayAlmostEqual.
     """
     return nptu.assert_almost_equal(actual, desired, decimal, err_msg, verbose)
Beispiel #38
0
 def test_single_array_input(self):
     scomptr = SCompositeModel([self.p1, self.p11])
     sresult = scomptr(self.x)
     xx = self.p11(self.p1(self.x))
     utils.assert_almost_equal(xx, sresult)
Beispiel #39
0
 def test_single_array_input(self):
     model = SerialCompositeModel([self.p1, self.p11])
     result = model(self.x)
     xx = self.p11(self.p1(self.x))
     utils.assert_almost_equal(xx, result)
Beispiel #40
0
 def test_poly2D_cheb2D(self):
     model = self.fitter(self.cheb2, self.x, self.y, self.z)
     z1 = model(self.x, self.y)
     assert_almost_equal(self.z, z1)
Beispiel #41
0
 def test_CYP_s2p(self):
     wcslibout = self.wazp.wcs.p2s([[-10, 30]], 1)
     wcs_pix = self.wazp.wcs.s2p(wcslibout['world'], 1)['pixcrd']
     x, y = self.azp.inverse(wcslibout['phi'], wcslibout['theta'])
     utils.assert_almost_equal(np.asarray(x), wcs_pix[:, 0])
     utils.assert_almost_equal(np.asarray(y), wcs_pix[:, 1])
Beispiel #42
0
 def test_repeate(self):
     # make sure repeated runs of updatewcs do not change the WCS.
     updatewcs.updatewcs(self.acs_file)
     w1 = HSTWCS(self.acs_file, ext=('SCI', 1))
     w4 = HSTWCS(self.acs_file, ext=('SCI', 2))
     w1r = HSTWCS(self.ref_file, ext=('SCI', 1))
     w4r = HSTWCS(self.ref_file, ext=('SCI', 2))
     utils.assert_almost_equal(w1.wcs.crval, w1r.wcs.crval)
     utils.assert_almost_equal(w1.wcs.crpix, w1r.wcs.crpix)
     utils.assert_almost_equal(w1.wcs.cd, w1r.wcs.cd)
     assert ((np.array(w1.wcs.ctype) == np.array(w1r.wcs.ctype)).all())
     utils.assert_almost_equal(w1.sip.a, w1r.sip.a)
     utils.assert_almost_equal(w1.sip.b, w1r.sip.b)
     utils.assert_almost_equal(w4.wcs.crval, w4r.wcs.crval)
     utils.assert_almost_equal(w4.wcs.crpix, w4r.wcs.crpix)
     utils.assert_almost_equal(w4.wcs.cd, w4r.wcs.cd)
     assert ((np.array(self.w4.wcs.ctype) == np.array(w4r.wcs.ctype)).all())
     utils.assert_almost_equal(w4.sip.a, w4r.sip.a)
     utils.assert_almost_equal(w4.sip.b, w4r.sip.b)
Beispiel #43
0
def coordinates():
    easy_algo_db = get_easy_algo_db()
    localization_template = easy_algo_db.create_instance(
        FAMILY_LOC_TEMPLATES, template)

    localization_template._init_metrics()
    center = localization_template.center
    offset = localization_template.offset
    pose = dtu.geo.SE2_from_translation_angle([center[0], center[1] - offset],
                                              np.deg2rad(0))
    location = localization_template.coords_from_pose(pose)
    assert_almost_equal(location['phi'], 0)
    assert_almost_equal(location['d'], 0)

    DX = 0.01
    pose = dtu.geo.SE2_from_translation_angle(
        [center[0], center[1] - offset + DX], np.deg2rad(0))
    location = localization_template.coords_from_pose(pose)
    assert_almost_equal(location['phi'], 0)
    assert_almost_equal(location['d'], DX)

    pose = dtu.geo.SE2_from_translation_angle(
        [center[0], center[1] - offset + DX], np.deg2rad(10))
    location = localization_template.coords_from_pose(pose)
    assert_almost_equal(location['phi'], np.deg2rad(10))
    assert_almost_equal(location['d'], DX)

    pose = dtu.geo.SE2_from_translation_angle([center[0] + offset, center[1]],
                                              np.deg2rad(90))
    location = localization_template.coords_from_pose(pose)
    assert_almost_equal(location['phi'], np.deg2rad(0))
    assert_almost_equal(location['d'], 0)

    a = 0
    pose = dtu.geo.SE2_from_translation_angle(
        [center[0] + offset * np.cos(a), center[1] + offset * np.sin(a)],
        np.deg2rad(45))
    location = localization_template.coords_from_pose(pose)

    assert_almost_equal(location['phi'], np.deg2rad(-45))
    assert_almost_equal(location['d'], 0)

    a = np.deg2rad(-40)
    pose = dtu.geo.SE2_from_translation_angle(
        [center[0] + offset * np.cos(a), center[1] + offset * np.sin(a)],
        np.deg2rad(50))
    location = localization_template.coords_from_pose(pose)
    #     print('offset: %s' % offset)
    #     print('pose: %s' % SE2.friendly(pose))
    #     print('location: %s' % phi_d_friendly(location))
    #
    assert_almost_equal(location['phi'], np.deg2rad(0))
    assert_almost_equal(location['d'], 0)

    a = np.deg2rad(-40)
    D = 0.01
    pose = dtu.geo.SE2_from_translation_angle([
        center[0] + (offset + D) * np.cos(a), center[1] +
        (offset + D) * np.sin(a)
    ], np.deg2rad(50))
    location = localization_template.coords_from_pose(pose)
    #     print('offset: %s' % offset)
    #     print('pose: %s' % SE2.friendly(pose))
    #     print('location: %s' % phi_d_friendly(location))
    #
    assert_almost_equal(location['phi'], np.deg2rad(0))
    assert_almost_equal(location['d'], -D)

    pose = dtu.geo.SE2_from_translation_angle([+0.1095, 0], np.deg2rad(0))
    check_coords(localization_template, pose)

    pose = dtu.geo.SE2_from_translation_angle([+0.1095, 0], np.deg2rad(15))
    check_coords(localization_template, pose)
    #
    pose = dtu.geo.SE2_from_translation_angle([0.15, -0.1], np.deg2rad(5))
    check_coords(localization_template, pose)
Beispiel #44
0
def compare_faster():
    variables = collections.OrderedDict()
    variables['alpha'] = dict(min=-180,
                              max=180,
                              description="angle",
                              resolution=5,
                              units='deg',
                              units_display='deg')
    variables['r'] = dict(min=3,
                          max=5,
                          description="distance",
                          resolution=0.1,
                          units='m',
                          units_display='cm')
    # this will fail if precision is float32
    gh = GridHelper(variables, precision='float64')
    val_fast = gh.create_new()
    val_fast.fill(0)
    val_slow = gh.create_new()
    val_slow.fill(0)

    od = dtu.get_output_dir_for_test()

    F = 1

    alpha0 = 7
    # r0 = 4
    r0 = 4.1
    w0 = 1.
    value = dict(alpha=alpha0, r=r0)
    gh.add_vote(val_slow, value, w0, F)

    assert_equal(np.sum(val_slow > 0), 9)

    values = np.zeros((2, 1))
    values[0, 0] = alpha0
    values[1, 0] = r0
    weights = np.zeros(1)
    weights[0] = w0
    gh.add_vote_faster(val_fast, values, weights, F)

    assert_equal(np.sum(val_fast > 0), 9)

    d = grid_helper_plot(gh, val_slow)
    fn = os.path.join(od, 'compare_faster_slow.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    d = grid_helper_plot(gh, val_fast)
    fn = os.path.join(od, 'compare_faster_fast.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    D = val_fast - val_slow
    diff = np.max(np.abs(D))
    print('diff: %r' % diff)
    if diff > 1e-8:
        print(dtu.indent(array_as_string_sign(val_fast), 'val_fast '))
        print(dtu.indent(array_as_string_sign(val_slow), 'val_slow '))
        print(dtu.indent(array_as_string_sign(D), 'Diff '))
        print('non zero val_fast: %s' % val_fast[val_fast > 0])
        print('non zero val_slow: %s' % val_slow[val_slow > 0])

    assert_almost_equal(val_fast, val_slow)
Beispiel #45
0
def get_map_straight_lane(
    tile_size: float,
    width_yellow: float,
    width_white: float,
    tile_spacing: float,
    gap_len: float,
    dash_len: float,
    width_red: float,
) -> SegmentsMap:
    # from inner yellow to inner white
    constants = {}
    constants["tile_size"] = tile_size
    constants["width_yellow"] = width_yellow
    constants["width_white"] = width_white
    constants["dash_len"] = dash_len
    constants["gap_len"] = gap_len
    constants["tile_spacing"] = gap_len

    lane_width = L = (tile_size - 2 * width_white - width_yellow) / 2
    constants["lane_width"] = lane_width

    y1 = +width_yellow / 2 + L + width_white
    y2 = +width_yellow / 2 + L
    y3 = +width_yellow / 2
    y4 = -width_yellow / 2
    y5 = -width_yellow / 2 - L
    y6 = -width_yellow / 2 - L - width_white

    assert_almost_equal(width_white + L + width_yellow + L + width_white,
                        tile_size)

    extra = (tile_spacing - tile_size) / 2

    FRAME = FRAME_TILE

    S = -tile_size / 2 - extra
    D = tile_size / 2 + extra

    points: Dict[PointName, SegMapPoint] = {}
    segments: List[SegMapSegment] = []
    faces: List[SegMapFace] = []

    points["p1"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y1, 0]))
    points["q1"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y1, 0]))
    points["p2"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y2, 0]))
    points["q2"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y2, 0]))
    points["p3"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y3, 0]))
    points["q3"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y3, 0]))
    points["p4"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y4, 0]))
    points["q4"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y4, 0]))
    points["p5"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y5, 0]))
    points["q5"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y5, 0]))
    points["p6"] = SegMapPoint(id_frame=FRAME, coords=np.array([S, y6, 0]))
    points["q6"] = SegMapPoint(id_frame=FRAME, coords=np.array([D, y6, 0]))

    add_tile(points, faces, segments, tile_size, tile_spacing)

    def add_dash(x, length):
        s = len(points)
        pre = f"dash{s}_"
        points[pre + "t0"] = SegMapPoint(id_frame=FRAME,
                                         coords=np.array([x, y3, 0]))
        points[pre + "t1"] = SegMapPoint(id_frame=FRAME,
                                         coords=np.array([x, y4, 0]))
        points[pre + "t2"] = SegMapPoint(id_frame=FRAME,
                                         coords=np.array([x + length, y4, 0]))
        points[pre + "t3"] = SegMapPoint(id_frame=FRAME,
                                         coords=np.array([x + length, y3, 0]))
        faces.append(
            SegMapFace(
                color=YELLOW,
                points=[
                    PointName(pre + "t0"),
                    PointName(pre + "t1"),
                    PointName(pre + "t2"),
                    PointName(pre + "t3"),
                ],
            ))

    ngaps: int = int(tile_size / (gap_len + dash_len)) + 1
    if width_red is not None:
        do_not_go_over = tile_size / 2
    else:
        do_not_go_over = tile_size / 2 + extra

    for i in range(int(ngaps)):
        x = i * (gap_len + dash_len) - tile_size / 2
        x1 = min(x + dash_len, do_not_go_over)
        length = x1 - x
        if length > 0:
            add_dash(x, length)

    segments.append(
        SegMapSegment(color=Segment.WHITE,
                      points=[PointName("q1"),
                              PointName("p1")]))
    segments.append(
        SegMapSegment(color=Segment.WHITE,
                      points=[PointName("p2"),
                              PointName("q2")]))
    segments.append(
        SegMapSegment(color=Segment.YELLOW,
                      points=[PointName("q3"),
                              PointName("p3")]))
    segments.append(
        SegMapSegment(color=Segment.YELLOW,
                      points=[PointName("p4"),
                              PointName("q4")]))
    segments.append(
        SegMapSegment(color=Segment.WHITE,
                      points=[PointName("q5"),
                              PointName("p5")]))
    segments.append(
        SegMapSegment(color=Segment.WHITE,
                      points=[PointName("p6"),
                              PointName("q6")]))

    faces.append(
        SegMapFace(color=WHITE,
                   points=[
                       PointName("p1"),
                       PointName("q1"),
                       PointName("q2"),
                       PointName("p2")
                   ]))
    faces.append(
        SegMapFace(color=WHITE,
                   points=[
                       PointName("p5"),
                       PointName("q5"),
                       PointName("q6"),
                       PointName("p6")
                   ]))

    if width_red is not None:
        x1 = tile_size / 2 - width_red
        y2 = -width_yellow / 2
        y1 = -tile_size / 2 + width_white
        x2 = tile_size / 2

        _add_rect(
            points,
            faces,
            segments,
            x1,
            y1,
            x2,
            y2,
            FRAME,
            RED,
            use_sides_for_loc=[Segment.RED, None, Segment.RED, None],
        )
        constants["width_red"] = width_red

    return SegmentsMap(points=points,
                       segments=segments,
                       faces=faces,
                       constants=constants)
Beispiel #46
0
def test_fast_bw_uniform():
  print("Make op...")
  from NativeOp import FastBaumWelchOp
  op = FastBaumWelchOp().make_op()  # (am_scores, edges, weights, start_end_states, float_idx, state_buffer)
  print("Op:", op)
  n_batch = 3
  seq_len = 7
  n_classes = 5
  from Fsa import FastBwFsaShared
  fsa = FastBwFsaShared()
  for i in range(n_classes):
    fsa.add_edge(i, i + 1, emission_idx=i)  # fwd
    fsa.add_edge(i + 1, i + 1, emission_idx=i)  # loop
  assert n_classes <= seq_len
  fast_bw_fsa = fsa.get_fast_bw_fsa(n_batch=n_batch)
  print("edges:")
  print(fast_bw_fsa.edges)
  edges = fast_bw_fsa.edges.view("float32")
  edges_placeholder = T.fmatrix(name="edges")
  weights = fast_bw_fsa.weights
  weights_placeholder = T.fvector(name="weights")
  print("start_end_states:")
  print(fast_bw_fsa.start_end_states)
  start_end_states = fast_bw_fsa.start_end_states.view("float32")
  start_end_states_placeholder = T.fmatrix(name="start_end_states")
  am_scores = numpy.ones((seq_len, n_batch, n_classes), dtype="float32") * numpy.float32(1.0 / n_classes)
  am_scores = -numpy.log(am_scores)  # in -log space
  am_scores_placeholder = T.ftensor3(name="am_scores")
  float_idx = numpy.ones((seq_len, n_batch), dtype="float32")
  float_idx_placeholder = T.fmatrix(name="float_idx")
  last_state_idx = numpy.max(fast_bw_fsa.start_end_states[1])  # see get_automata_for_batch
  state_buffer = numpy.zeros((2, last_state_idx + 1), dtype="float32")
  state_buffer_placeholder = T.fmatrix(name="state_buffer")
  print("Construct call...")
  fwdbwd, obs_scores = op(
    am_scores_placeholder, edges_placeholder, weights_placeholder, start_end_states_placeholder, float_idx_placeholder, state_buffer_placeholder)
  f = theano.function(inputs=[am_scores_placeholder, edges_placeholder, weights_placeholder, start_end_states_placeholder, float_idx_placeholder, state_buffer_placeholder], outputs=[fwdbwd, obs_scores])
  print("Done.")
  print("Eval:")
  fwdbwd, score = f(am_scores, edges, weights, start_end_states, float_idx, state_buffer)
  print("score:")
  print(repr(score))
  assert_equal(score.shape, (seq_len, n_batch))
  bw = numpy.exp(-fwdbwd)
  print("Baum-Welch soft alignment:")
  print(repr(bw))
  assert_equal(bw.shape, (seq_len, n_batch, n_classes))
  from numpy import array, float32
  if seq_len == n_classes:
    print("Extra check identity...")
    for i in range(n_batch):
      assert_almost_equal(numpy.identity(n_classes), bw[:, i])
  if seq_len == 7 and n_classes == 5:
    print("Extra check ref_align (7,5)...")
    assert_allclose(score, 8.55801582, rtol=1e-5)  # should be the same everywhere
    ref_align = \
      array([[[1., 0., 0., 0., 0.]],
             [[0.33333316, 0.66666663, 0., 0., 0.]],
             [[0.06666669, 0.53333354, 0.40000018, 0., 0.]],
             [[0., 0.20000014, 0.60000014, 0.19999999, 0.]],
             [[0., 0., 0.39999962, 0.53333312, 0.06666663]],
             [[0., 0., 0., 0.66666633, 0.33333316]],
             [[0., 0., 0., 0., 0.99999982]]], dtype=float32)
    assert_equal(ref_align.shape, (seq_len, 1, n_classes))
    ref_align = numpy.tile(ref_align, (1, n_batch, 1))
    assert_equal(ref_align.shape, bw.shape)
    # print("Reference alignment:")
    # print(repr(ref_align))
    print("mean square diff:", numpy.mean(numpy.square(ref_align - bw)))
    print("max square diff:", numpy.max(numpy.square(ref_align - bw)))
    assert_allclose(ref_align, bw, rtol=1e-5)
  print("Done.")
Beispiel #47
0
    c0 = np.array([d[1], -d[0], 0])

    if False:
        z = np.array([0, 0, 1])
        c = np.cross(d, z)
        assert_almost_equal(np.linalg.norm(c), 1.0)
        assert_almost_equal(np.linalg.norm(c0), 1.0)
        assert_almost_equal(c0, c[:2])

    return c0


# TODO: move away
assert_almost_equal(
    np.array([0, -1, 0]),
    get_normal_outward_for_segment(np.array([0, 0]), np.array([2, 0])))


def add_prefix(sm, prefix):
    points = {}
    faces = []
    segments = []
    for k, v in sm.points.items():
        points[prefix + k] = v
    for face in sm.faces:
        points2 = tuple(prefix + _ for _ in face.points)
        faces.append(face._replace(points=points2))
    for segment in sm.segments:
        points2 = tuple(prefix + _ for _ in segment.points)
        segments.append(segment._replace(points=points2))
Beispiel #48
0
def compare_faster():
    variables = {}
    variables["alpha"] = dict(min=-180,
                              max=180,
                              description="angle",
                              resolution=5,
                              units="deg",
                              units_display="deg")
    variables["r"] = dict(min=3,
                          max=5,
                          description="distance",
                          resolution=0.1,
                          units="m",
                          units_display="cm")
    # this will fail if precision is float32
    gh = GridHelper(variables, precision="float64")
    val_fast = gh.create_new()
    val_fast.fill(0)
    val_slow = gh.create_new()
    val_slow.fill(0)

    od = dtu.get_output_dir_for_test()

    F = 1

    alpha0 = 7
    # r0 = 4
    r0 = 4.1
    w0 = 1.0
    value = dict(alpha=alpha0, r=r0)
    gh.add_vote(val_slow, value, w0, F)

    assert_equal(np.sum(val_slow > 0), 9)

    values = np.zeros((2, 1))
    values[0, 0] = alpha0
    values[1, 0] = r0
    weights = np.zeros(1)
    weights[0] = w0
    gh.add_vote_faster(val_fast, values, weights, F)

    assert_equal(np.sum(val_fast > 0), 9)

    d = grid_helper_plot(gh, val_slow)
    fn = os.path.join(od, "compare_faster_slow.jpg")
    dtu.write_data_to_file(d.get_png(), fn)

    d = grid_helper_plot(gh, val_fast)
    fn = os.path.join(od, "compare_faster_fast.jpg")
    dtu.write_data_to_file(d.get_png(), fn)

    D = val_fast - val_slow
    diff = np.max(np.abs(D))
    print(f"diff: {diff!r}")
    if diff > 1e-8:
        print(dtu.indent(array_as_string_sign(val_fast), "val_fast "))
        print(dtu.indent(array_as_string_sign(val_slow), "val_slow "))
        print(dtu.indent(array_as_string_sign(D), "Diff "))
        print(f"non zero val_fast: {val_fast[val_fast > 0]}")
        print(f"non zero val_slow: {val_slow[val_slow > 0]}")

    assert_almost_equal(val_fast, val_slow)
def test_outcome_dependent_data():
    np.random.seed(10)
    m = 1000
    max_terms = 100
    y = np.random.normal(size=m)
    w = np.random.normal(size=m) ** 2
    weight = SingleWeightDependentData.alloc(w, m, max_terms, 1e-16)
    data = SingleOutcomeDependentData.alloc(y, weight, m, max_terms)

    # Test updating
    B = np.empty(shape=(m, max_terms))
    for k in range(max_terms):
        b = np.random.normal(size=m)
        B[:, k] = b
        code = weight.update_from_array(b)
        if k >= 99:
            1 + 1
        data.update()
        assert_equal(code, 0)
        assert_almost_equal(
            np.dot(weight.Q_t[:k + 1, :], np.transpose(weight.Q_t[:k + 1, :])),
            np.eye(k + 1))
    assert_equal(weight.update_from_array(b), -1)
#     data.update(1e-16)

    # Test downdating
    q = np.array(weight.Q_t).copy()
    theta = np.array(data.theta[:max_terms]).copy()
    weight.downdate()
    data.downdate()
    weight.update_from_array(b)
    data.update()
    assert_almost_equal(q, np.array(weight.Q_t))
    assert_almost_equal(theta, np.array(data.theta[:max_terms]))
    assert_almost_equal(
        np.array(data.theta[:max_terms]), np.dot(weight.Q_t, w * y))
    wB = B * w[:, None]
    Q, _ = qr(wB, pivoting=False, mode='economic')
    assert_almost_equal(np.abs(np.dot(weight.Q_t, Q)), np.eye(max_terms))

    # Test that reweighting works
    assert_equal(data.k, max_terms)
    w2 = np.random.normal(size=m) ** 2
    weight.reweight(w2, B, max_terms)
    data.synchronize()
    assert_equal(data.k, max_terms)
    w2B = B * w2[:, None]
    Q2, _ = qr(w2B, pivoting=False, mode='economic')
    assert_almost_equal(np.abs(np.dot(weight.Q_t, Q2)), np.eye(max_terms))
    assert_almost_equal(
        np.array(data.theta[:max_terms]), np.dot(weight.Q_t, w2 * y))
Beispiel #50
0
def test_fast_bw_uniform():
    print("Make op...")
    op = make_fast_baum_welch_op(compiler_opts=dict(
        verbose=True))  # will be cached, used inside :func:`fast_baum_welch`
    # args: (am_scores, edges, weights, start_end_states, float_idx, state_buffer)
    print("Op:", op)
    n_batch = 3
    seq_len = 7
    n_classes = 5
    from Fsa import FastBwFsaShared
    fsa = FastBwFsaShared()
    for i in range(n_classes):
        fsa.add_edge(i, i + 1, emission_idx=i)  # fwd
        fsa.add_edge(i + 1, i + 1, emission_idx=i)  # loop
    assert n_classes <= seq_len
    fast_bw_fsa = fsa.get_fast_bw_fsa(n_batch=n_batch)
    edges = tf.constant(fast_bw_fsa.edges, dtype=tf.int32)
    weights = tf.constant(fast_bw_fsa.weights, dtype=tf.float32)
    start_end_states = tf.constant(fast_bw_fsa.start_end_states,
                                   dtype=tf.int32)
    am_scores = numpy.ones((seq_len, n_batch, n_classes),
                           dtype="float32") * numpy.float32(1.0 / n_classes)
    am_scores = -numpy.log(am_scores)  # in -log space
    am_scores = tf.constant(am_scores, dtype=tf.float32)
    float_idx = tf.ones((seq_len, n_batch), dtype=tf.float32)
    # from TFUtil import sequence_mask_time_major
    # float_idx = tf.cast(sequence_mask_time_major(tf.convert_to_tensor(list(range(seq_len - n_batch + 1, seq_len + 1)))), dtype=tf.float32)
    print("Construct call...")
    fwdbwd, obs_scores = fast_baum_welch(am_scores=am_scores,
                                         float_idx=float_idx,
                                         edges=edges,
                                         weights=weights,
                                         start_end_states=start_end_states)
    print("Done.")
    print("Eval:")
    fwdbwd, score = session.run([fwdbwd, obs_scores])
    print("score:")
    print(repr(score))
    assert_equal(score.shape, (seq_len, n_batch))
    bw = numpy.exp(-fwdbwd)
    print("Baum-Welch soft alignment:")
    print(repr(bw))
    assert_equal(bw.shape, (seq_len, n_batch, n_classes))
    from numpy import array, float32
    if seq_len == n_classes:
        print("Extra check identity...")
        for i in range(n_batch):
            assert_almost_equal(numpy.identity(n_classes), bw[:, i])
    if seq_len == 7 and n_classes == 5:
        print("Extra check ref_align (7,5)...")
        assert_allclose(score, 8.55801582,
                        rtol=1e-5)  # should be the same everywhere
        ref_align = \
          array([[[1., 0., 0., 0., 0.]],
                 [[0.33333316, 0.66666663, 0., 0., 0.]],
                 [[0.06666669, 0.53333354, 0.40000018, 0., 0.]],
                 [[0., 0.20000014, 0.60000014, 0.19999999, 0.]],
                 [[0., 0., 0.39999962, 0.53333312, 0.06666663]],
                 [[0., 0., 0., 0.66666633, 0.33333316]],
                 [[0., 0., 0., 0., 0.99999982]]], dtype=float32)
        assert_equal(ref_align.shape, (seq_len, 1, n_classes))
        ref_align = numpy.tile(ref_align, (1, n_batch, 1))
        assert_equal(ref_align.shape, bw.shape)
        # print("Reference alignment:")
        # print(repr(ref_align))
        print("mean square diff:", numpy.mean(numpy.square(ref_align - bw)))
        print("max square diff:", numpy.max(numpy.square(ref_align - bw)))
        assert_allclose(ref_align, bw, rtol=1e-5)
    print("Done.")
Beispiel #51
0
 def test_object_pars(self):
     l2 = TestParModel(coeff=[[1, 2], [3, 4]], e=(2, 3), param_dim=2)
     utils.assert_almost_equal(l2.parameters,
                               [1.0, 2.0, 3.0, 4.0, 2.0, 3.0])
Beispiel #52
0
def get_map_curve(tile_size, tile_spacing, width_yellow, width_white, gap_len,
                  dash_len, direction):
    constants = {}
    constants["tile_size"] = tile_size
    constants["width_yellow"] = width_yellow
    constants["width_white"] = width_white
    constants["dash_len"] = dash_len
    constants["gap_len"] = gap_len
    constants["tile_spacing"] = gap_len

    lane_width = L = (tile_size - 2 * width_white - width_yellow) / 2
    constants["lane_width"] = lane_width

    assert_almost_equal(width_white + L + width_yellow + L + width_white,
                        tile_size)

    extra = (tile_spacing - tile_size) / 2

    id_frame = FRAME_TILE

    points = {}
    segments = []
    faces = []

    add_tile(points, faces, segments, tile_size, tile_spacing)

    radius = tile_size - width_white / 2
    width = width_white

    if direction == "right":
        center = [-tile_size / 2, -tile_size / 2]
        alpha1 = -np.deg2rad(3)
        alpha2 = np.pi / 2 + np.deg2rad(3)
    else:
        center = [-tile_size / 2, +tile_size / 2]
        alpha1 = -np.pi / 2 - np.deg2rad(3)
        alpha2 = +np.deg2rad(3)

    colors = [WHITE]
    lengths = [width_white * 2]
    detect_color = Segment.WHITE

    add_curved(
        points,
        faces,
        segments,
        id_frame,
        center,
        radius,
        alpha1,
        alpha2,
        width,
        colors,
        lengths,
        detect_color,
    )

    radius = tile_size / 2

    width = width_yellow
    colors = [YELLOW, None]
    lengths = [dash_len, gap_len]
    detect_color = Segment.YELLOW
    add_curved(
        points,
        faces,
        segments,
        id_frame,
        center,
        radius,
        alpha1,
        alpha2,
        width,
        colors,
        lengths,
        detect_color,
    )

    if direction == "right":
        angle = 0
    else:
        angle = 3 * np.pi / 2
    add_corner(points, faces, segments, tile_size, extra, width_white,
               id_frame, angle)

    return SegmentsMap(points=points,
                       segments=segments,
                       faces=faces,
                       constants=constants)
Beispiel #53
0
 def test_single_array_input(self):
     p1 = Polynomial1D(3, c0=1, c1=2, c2=3, c3=4)
     p2 = Polynomial1D(3, c0=2, c1=3, c2=4, c3=5)
     m = p1 | p2
     assert_almost_equal(p2(p1(self.x)), m(self.x))
Beispiel #54
0
def test_firing_rate():
    assert_equal(firing_rate([1, 2, 3]), 1)
    assert_equal(firing_rate([1, 1.2, 1.4, 1.6, 1.8]), 5)
    assert_almost_equal(firing_rate([1.1, 1.2, 1.3, 1.4, 1.5]), 10)
Beispiel #55
0
 def test_change_parameters(self):
     self.gmodel.parameters = [13, 10, 9, 5.2, 0.4, 0.7]
     utils.assert_almost_equal(self.gmodel.amplitude.value, [13., 10.])
     utils.assert_almost_equal(self.gmodel.mean.value, [9., 5.2])
Beispiel #56
0
def test_get_features_feature_metric():
    # "voltage traces" that are constant at -70*mV, -60mV, -50mV, -40mV for
    # 50ms each.
    voltage_target = np.ones(
        (2, 200)) * np.repeat([-70, -60, -50, -40], 50) * mV
    dt = 1 * ms
    # The results for the first and last "parameter set" are too high/low, the
    # middle one is perfect
    voltage_model = np.ones(
        (3, 2, 200)) * np.repeat([-70, -60, -50, -40], 50) * mV
    voltage_model[0, 0, :] += 2.5 * mV
    voltage_model[0, 1, :] += 5 * mV
    voltage_model[2, 0, :] -= 2.5 * mV
    voltage_model[2, 1, :] -= 5 * mV

    inp_times = [[99 * ms, 150 * ms], [49 * ms, 150 * ms]]

    # Default comparison: absolute difference
    # Check that FeatureMetric rejects the normalization argument
    with pytest.raises(ValueError):
        feature_metric = FeatureMetric(inp_times, ['voltage_base'],
                                       normalization=2)
    feature_metric = FeatureMetric(inp_times, ['voltage_base'])
    results = feature_metric.get_features(voltage_model, voltage_target, dt=dt)
    assert len(results) == 3
    assert all(isinstance(r, dict) for r in results)
    assert all(r.keys() == {'voltage_base'} for r in results)
    assert_almost_equal(results[0]['voltage_base'],
                        np.array([2.5 * mV, 5 * mV]))
    assert_almost_equal(results[1]['voltage_base'], [0, 0])
    assert_almost_equal(results[2]['voltage_base'],
                        np.array([2.5 * mV, 5 * mV]))

    # Custom comparison: squared difference
    feature_metric = FeatureMetric(inp_times, ['voltage_base'],
                                   combine=lambda x, y: (x - y)**2)
    results = feature_metric.get_features(voltage_model, voltage_target, dt=dt)
    assert len(results) == 3
    assert all(isinstance(r, dict) for r in results)
    assert all(r.keys() == {'voltage_base'} for r in results)
    assert_almost_equal(results[0]['voltage_base'],
                        np.array([(2.5 * mV)**2, (5 * mV)**2]))
    assert_almost_equal(results[1]['voltage_base'], [0, 0])
    assert_almost_equal(results[2]['voltage_base'],
                        np.array([(2.5 * mV)**2, (5 * mV)**2]))
Beispiel #57
0
def test_get_initializer_const_formula():
  shape = (2, 3)
  initializer = get_initializer("log(1.0 / 4.0)")
  v = initializer(shape)
  assert_almost_equal(session.run(v), numpy.zeros(shape) + numpy.log(1.0 / 4.0))
Beispiel #58
0
def test_get_initializer_constant():
  shape = (2, 3)
  initializer = get_initializer("constant")
  v = initializer(shape)
  assert_almost_equal(session.run(v), numpy.zeros(shape))
Beispiel #59
0
 def test_poly2D_cheb2D(self):
     self.fitter(self.x, self.y, self.z)
     z1 = self.cheb2(self.x, self.y)
     utils.assert_almost_equal(self.z, z1)
Beispiel #60
0
def test_get_initializer_zero():
  shape = (2, 3)
  initializer = get_initializer(0.0)
  v = initializer(shape)
  assert_almost_equal(session.run(v), numpy.zeros(shape))