def test_infeasible(self):
        np.random.seed(1)
        leadfield = np.random.random((5, 10, 3))
        np.random.seed(None)
        targets = [0, 1]
        target_direction = [[1, 0, 0], [1, 0, 0]]

        target_mean = 1e4
        max_total_current = 0.2
        max_el_current = 0.1

        tes_problem = optimization_methods.TESLinearConstrained(
            leadfield, max_total_current, max_el_current
        )
        tes_problem.add_linear_constraint(targets, target_direction, target_mean)

        x = tes_problem.solve()

        x_sp = optimize_comp(
            np.squeeze(tes_problem.l),
            np.ones((1, 6)),
            max_el_current,
            max_total_current
        )

        assert np.linalg.norm(x, 1) <= 2 * max_total_current
        assert np.all(np.abs(x) <= max_el_current)
        assert np.isclose(np.sum(x), 0)
        assert np.allclose(tes_problem.l.dot(x), tes_problem.l.dot(x_sp),
                           rtol=1e-4, atol=1e-5)
    def test_feasible(self, target_mean):
        np.random.seed(1)
        leadfield = np.random.random((5, 10, 3))
        np.random.seed(None)
        targets = [0, 1]
        target_direction = [[1, 0, 0], [1, 0, 0]]

        max_total_current = 0.2
        max_el_current = 0.1

        tes_problem = optimization_methods.TESLinearConstrained(
            leadfield, max_total_current, max_el_current
        )
        tes_problem.add_linear_constraint(targets, target_direction, target_mean)

        x = tes_problem.solve()

        x_sp = optimize_focality(
            tes_problem.l, tes_problem.Q,
            target_mean, max_el_current,
            max_total_current
        )

        assert np.all(np.abs(x) <= max_el_current)
        assert np.all(np.linalg.norm(x) <= 2*max_total_current)
        assert np.isclose(np.sum(x), 0)
        assert np.isclose(tes_problem.l.dot(x), tes_problem.target_means)
        assert np.allclose(
            x.dot(tes_problem.Q.dot(x)),
            x_sp.dot(tes_problem.Q.dot(x_sp)),
            rtol=1e-4, atol=1e-5
        )
 def test_calc_many_targets(self):
     A = np.random.random((2, 5, 3))
     volumes = np.array([1, 2, 2, 2, 2])
     tes_problem = optimization_methods.TESLinearConstrained(
         A, weights=volumes
     )
     tes_problem.add_linear_constraint([0], [1, 0, 0], 0.2)
     tes_problem.add_linear_constraint([1], [1, 0, 0], 0.4)
     currents = [-2, 1, 1]
     field_x = A[..., 0].T.dot(currents[1:])
     assert np.allclose(field_x[[0, 1]], tes_problem.l.dot(currents))
     assert np.allclose([0.2, 0.4], tes_problem.target_means)
 def test_calc_different_directions(self):
     A = np.random.random((2, 5, 3))
     targets = [0, 1]
     target_direction = np.array([[1, 0, 0], [0, 1, 0]])
     volumes = np.array([1, 2, 2, 2, 2])
     tes_problem = optimization_methods.TESLinearConstrained(
         A, weights=volumes
     )
     tes_problem.add_linear_constraint(targets, target_direction, 0.2)
     currents = [1, 1]
     field_x = A[..., 0].T.dot(currents)
     field_y = A[..., 1].T.dot(currents)
     avg = (field_x[0]*1 + field_y[1]*2)/3
     assert np.allclose(avg, tes_problem.l.dot([-2, 1, 1]))
 def test_calc_l(self):
     A = np.random.random((2, 5, 3))
     targets = [0, 1]
     target_direction = [[1, 0, 0], [1, 0, 0]]
     volumes = np.array([1, 2, 2, 2, 2])
     tes_problem = optimization_methods.TESLinearConstrained(
         A, weights=volumes
     )
     tes_problem.add_linear_constraint(targets, target_direction, 0.2)
     currents = [-2, 1, 1]
     field_x = A[..., 0].T.dot(currents[1:])
     avg = np.average(field_x[[0, 1]], weights=[1, 2])
     assert np.allclose(avg, tes_problem.l.dot(currents))
     assert np.allclose(0.2, tes_problem.target_means)
    def test_multi_target_feasible(self):
        np.random.seed(1)
        leadfield = np.random.random((5, 10, 3))
        np.random.seed(None)

        max_total_current = 0.2
        max_el_current = 0.1

        tes_problem = optimization_methods.TESLinearConstrained(
            leadfield, max_total_current, max_el_current
        )
        tes_problem.add_linear_constraint([0], [1, 0, 0], 1e-3)
        tes_problem.add_linear_constraint([1], [1, 0, 0], 2e-3)

        x = tes_problem.solve()

        assert np.all(np.abs(x) <= max_el_current)
        assert np.all(np.linalg.norm(x) <= 2*max_total_current)
        assert np.isclose(np.sum(x), 0)
        assert np.allclose(tes_problem.l.dot(x), tes_problem.target_means)