def test_get_latex_result(self):
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        split_net = self.net
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()
        self.assertEqual(split_net.get_latex_result('b'),'1\cdot\exp(j (0.0))\cdot b_{in}(t-0.0)')

        edge_1 = Edge('a', 'b', phase=1, attenuation=0.4, delay=2)
        edge_2 = Edge('b', 'c', phase=2, attenuation=0.3, delay=1.2)
        edge_3 = Edge('c', 'a', phase=3, attenuation=0.2, delay=0)

        loop_net = Network()
        loop_net.add_node('a')
        loop_net.add_node('b')
        loop_net.add_node('c')
        loop_net.add_edge(edge_1)
        loop_net.add_edge(edge_2)
        loop_net.add_edge(edge_3)
        loop_net.add_input('a', amplitude=1)

        loop_net.evaluate(amplitude_cutoff=1e-4)
        self.assertEqual(loop_net.get_latex_result('b',precision=2),'0.4\cdot\exp(j (1))\cdot a_{in}(t-2)+0.0096\cdot\exp(j (7))\cdot a_{in}(t-5.2)+0.00023\cdot\exp(j (13))\cdot a_{in}(t-8.4)')
    def test_get_reduced_output(self):
        """ creates and evaluates a feed forward combiner """
        edge_1 = Edge('b', 'a', phase=0, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=0, attenuation=1.5, delay=2)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        amp, phase, delay = split_net.get_reduced_output('a')

        self.assertEqual(amp[0], 2)
        self.assertEqual(phase[0], 0)
        self.assertEqual(delay[0], 2)

        split_net.edges[1].phase=-np.pi/2
        split_net.edges[1].attenuation=0.5

        split_net.evaluate()
        amp, phase, delay = split_net.get_reduced_output('a')
        self.assertAlmostEqual(amp[0], 1/np.sqrt(2),places=5)
        self.assertAlmostEqual(phase[0], -np.pi/4, places=5)
        self.assertEquals(delay[0], 2)
    def test_evaluate_loop(self):
        edge_1 = Edge('a', 'b', phase=1, attenuation=0.4, delay=2)
        edge_2 = Edge('b', 'c', phase=2, attenuation=0.3, delay=1)
        edge_3 = Edge('c', 'a', phase=3, attenuation=0.2, delay=0)

        expected_result = {'a': [(1, 0.0, 0.0, '-a'), (edge_1.attenuation * edge_2.attenuation * edge_3.attenuation,
                                                       edge_1.phase + edge_2.phase + edge_3.phase,
                                                       edge_1.delay + edge_2.delay + edge_3.delay, '-a-b-c-a')],
                           'b': [(edge_1.attenuation, edge_1.phase, edge_1.delay, '-a-b'),
                                 (edge_1.attenuation * edge_2.attenuation * edge_3.attenuation * edge_1.attenuation,
                                  edge_1.phase + edge_2.phase + edge_3.phase + edge_1.phase,
                                  edge_1.delay + edge_2.delay + edge_3.delay + edge_1.delay, '-a-b-c-a-b')],
                           'c': [(edge_1.attenuation * edge_2.attenuation, edge_1.phase + edge_2.phase,
                                  edge_1.delay + edge_2.delay, '-a-b-c'),
                                 (
                                     edge_1.attenuation * edge_2.attenuation * edge_3.attenuation * edge_1.attenuation * edge_2.attenuation,
                                     edge_1.phase + edge_2.phase + edge_3.phase + edge_1.phase + edge_2.phase,
                                     edge_1.delay + edge_2.delay + edge_3.delay + edge_1.delay + edge_2.delay,
                                     '-a-b-c-a-b-c')]}

        loop_net = Network()
        loop_net.add_node('a')
        loop_net.add_node('b')
        loop_net.add_node('c')
        loop_net.add_edge(edge_1)
        loop_net.add_edge(edge_2)
        loop_net.add_edge(edge_3)
        loop_net.add_input('a', amplitude=1)

        loop_net.evaluate(amplitude_cutoff=1e-3)
        self.assertEqual(loop_net.nodes_to_output, expected_result)
    def test_evaluate_feed_forward(self):
        """ creates and evaluates a feed forward network """
        ff_net = Network()
        ff_net.add_node('a')
        ff_net.add_node('b')
        ff_net.add_node('c')

        ff_net.add_edge(Edge('a', 'b', phase=0.5, attenuation=0.8, delay=2))
        ff_net.add_edge(Edge('b', 'c', phase=-5, attenuation=1.5, delay=-1))

        ff_net.add_input('a', amplitude=1)

        ff_net.evaluate()

        self.assertEqual(ff_net.nodes_to_output, {'a': [(1, 0, 0, '-a')],
                                                  'b': [(1 * 0.8, 0 + 0.5, 2, '-a-b')],
                                                  'c': [(1 * 0.8 * 1.5, 0 + 0.5 - 5, 2 - 1, '-a-b-c')]})
    def setUp(self):
        self.tb_empty = Testbench(Network())

        ff_net = Network()
        ff_net.add_node('a')
        ff_net.add_node('b')
        ff_net.add_node('c')

        ff_net.add_edge(Edge('a', 'b', phase=0.5, attenuation=0.8, delay=2))
        ff_net.add_edge(Edge('b', 'c', phase=-5, attenuation=1.5, delay=-1))
        self.ff_net = ff_net

        self.tb_ff = Testbench(network=self.ff_net, timestep=1)

        self.x1 = np.array([0, 1, 6, 7])
        self.x2 = np.array([0, 2, 3])
        self.t1 = np.array([0, 2, 5, 7, 9])
        self.t2 = np.array([0, 2, 5, 12])
    def test_get_eval_result(self):
        """ creates and evaluates a feed forward combiner """
        edge_1 = Edge('b', 'a', phase=SymNum('phi1',default=0.5,product=False), attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=SymNum('phi2',default=0.0,product=False), attenuation=SymNum('amp2',default=1.5,product=True), delay=-1)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        self.assertEqual(split_net.get_eval_result('a'), [(0.5, 0.5, 2.0), (1.5, 0.0, -1.0)])
        self.assertEqual(split_net.get_eval_result('a',feed_dict=None, use_shared_default=True), [(0.5, 0.5, 2.0), (1.5, 0.0, -1.0)])
        self.assertEqual(split_net.get_eval_result('a',feed_dict={'phi1':0.6,'phi2':3,'amp2':6}, use_shared_default=True), [(0.5, 0.6, 2.0), (6.0, 3.0, -1.0)])
    def test_evaluate_splitting(self):
        """ creates and evaluates a feed forward split """
        edge_1 = Edge('a', 'b', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('a', 'c', phase=-0.5, attenuation=1.5, delay=-1)

        expected_nodes_to_output = {'a': [(1, 0, 0, '-a')],
                                    'b': [(edge_1.attenuation, edge_1.phase, edge_1.delay, '-a-b')],
                                    'c': [(edge_2.attenuation, edge_2.phase, edge_2.delay, '-a-c')]}

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('a', amplitude=1)
        split_net.evaluate()

        self.assertEqual(split_net.nodes_to_output, expected_nodes_to_output)
    def test_visualize(self):
        """ This test only checks that a graph is generated.

        It does not check if the graph does match the network description. This test will fail if graphviz is not setup.
        """
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()
        self.assertEqual(split_net.visualize(show_edge_labels=True,path='./visualizations/test1'), './visualizations\\test1.pdf')
        self.assertEqual(split_net.visualize(show_edge_labels=False, path='./visualizations/test2'), './visualizations\\test2.pdf')
        self.assertEqual(split_net.visualize(show_edge_labels=True,format='svg',path='./visualizations/test1'), './visualizations\\test1.svg')
        rmtree('./visualizations') # remove the directory
    def test_init_from_phase_and_attenuation_matrix(self):
        test_dev = Device(name='test')

        # splitter 1x2
        test_dev.init_from_phase_and_attenuation_matrix(np.array([[1 / np.sqrt(2), 1 / np.sqrt(2)]]), np.array([[np.pi, 0]]),
                                             delay=2.0)

        expected_net = Network()
        expected_net.add_node('device:test:i0')
        expected_net.add_node('device:test:o0')
        expected_net.add_node('device:test:o1')
        expected_net.add_edge(
            Edge(start='device:test:i0', end='device:test:o0', phase=np.pi, attenuation=1 / np.sqrt(2), delay=2.0))
        expected_net.add_edge(
            Edge(start='device:test:i0', end='device:test:o1', phase=0, attenuation=1 / np.sqrt(2), delay=2.0))

        self.assertEqual(test_dev.nodes, expected_net.nodes)
        self.assertEqual(test_dev.edges, expected_net.edges)

        # mixing 2x2
        test_dev = Device(name='test')

        # splitter 1x2
        test_dev.init_from_phase_and_attenuation_matrix(np.array([[0.5, 0.4], [1.0, 0.4]]), np.array([[0.0,0.0],[np.pi,np.pi]]), delay=1.0)

        expected_net = Network()
        expected_net.add_node('device:test:i0')
        expected_net.add_node('device:test:o0')
        expected_net.add_node('device:test:o1')
        expected_net.add_node('device:test:i1')
        expected_net.add_edge(Edge(start='device:test:i0', end='device:test:o0', phase=0.0, attenuation=0.5, delay=1.0))
        expected_net.add_edge(Edge(start='device:test:i0', end='device:test:o1', phase=0.0, attenuation=0.4, delay=1.0))
        expected_net.add_edge(
            Edge(start='device:test:i1', end='device:test:o0', phase=np.pi, attenuation=1.0, delay=1.0))
        expected_net.add_edge(
            Edge(start='device:test:i1', end='device:test:o1', phase=np.pi, attenuation=0.4, delay=1.0))
        self.assertEqual(test_dev.nodes, expected_net.nodes)
        self.assertEqual(test_dev.edges, expected_net.edges)
    def test_evaluate_loop(self):

        """ computes an output sequence for an input sequence passing through a 3 node network."""
        edge_1 = Edge('a', 'b', phase=1, attenuation=0.4, delay=2)
        edge_2 = Edge('b', 'c', phase=2, attenuation=0.3, delay=1)
        edge_3 = Edge('c', 'a', phase=3, attenuation=0.2, delay=0)

        loop_net = Network()
        loop_net.add_node('a')
        loop_net.add_node('b')
        loop_net.add_node('c')
        loop_net.add_edge(edge_1)
        loop_net.add_edge(edge_2)
        loop_net.add_edge(edge_3)
        loop_net.add_input('a', amplitude=1)

        tb = Testbench(network=loop_net)
        tb.add_input_sequence('b',x=[1,2],t=[0,5,7])

        tb.add_output_node('c')

        # evaluate the network (through the testbench)
        tb.evaluate_network(amplitude_cutoff=1e-3)

        # Calculate the output signal at the output nodes
        tb.calculate_output(n_threads=8)  # uses multithreading with at most 8 threads

        t_expected = np.arange(0,8,1)
        x_expected = np.array([0, 0.3*np.exp(1j*2),0.3*np.exp(1j*2), 0.3*np.exp(1j*2),
                               0.3*np.exp(1j*2)+0.3*0.2*0.4*0.3*np.exp(1j*8),
                               0.3*np.exp(1j*2)+0.3*0.2*0.4*0.3*np.exp(1j*8),
                               0.6 * np.exp(1j * 2) + 0.3 * 0.2 * 0.4 * 0.3 * np.exp(1j * 8),
                               0.6 * np.exp(1j * 2) + 0.3 * 0.2 * 0.4 * 0.3 * np.exp(1j * 8)]).transpose()

        self.assertEqual(np.allclose(tb.x_out, x_expected, atol=1e-3, rtol=0),True)
        self.assertEqual(np.allclose(tb.t_out, t_expected, atol=1e-3, rtol=0),True)
    Edge('l', 'k'),
    Edge('l', 'n'),
    Edge('k', 'm'),
    Edge('n', 'm'),
    Edge('n', 'p'),
    Edge('p', 'o'),
    Edge('m', 'o'),
    Edge('o', 'a'),
    Edge('m', 'c')
]

net = Network()
for node in nodes:
    net.add_node(node)
for edge in edges:
    net.add_edge(edge)
net.add_input('a', amplitude=1.0)
for edge in net.edges:
    edge.attenuation = 0.75
    edge.phase = np.random.uniform(0, 2 * np.pi)

net.visualize(path='./visualizations/mediumexample')
####
# Evaluate Network
####
net.evaluate(amplitude_cutoff=1e-3, max_endpoints=1e6)

####
# Print and plot
####
for node in net.nodes:
Example #12
0
A testbench is used to inject time varying signals at node A and B.
"""
from colna.analyticnetwork import Network, Edge, Testbench
import numpy as np
import matplotlib.pyplot as plt

### Create the Network and add the nodes

net = Network()

net.add_node(name='a')
net.add_node(name='b')
net.add_node(name='c')
net.add_node(name='d')

net.add_edge(Edge(start='a', end='b', phase=1, attenuation=0.8, delay=1))
net.add_edge(Edge(start='b', end='c', phase=2, attenuation=0.7, delay=2))
net.add_edge(Edge(start='c', end='d', phase=3, attenuation=0.8, delay=1))
net.add_edge(Edge(start='d', end='a', phase=-1, attenuation=0.9, delay=0.5))

net.visualize(path='./visualizations/recurrent_with_testbench')

### Create a testbench
tb = Testbench(network=net,
               timestep=0.1)  # Timestep should be factor of all delays

x_in_a = np.sin(np.linspace(
    0, 15, 500)) + 1.5  # create the input signal (Dimensino N)
t_in = np.linspace(0, 10,
                   num=501)  # create the input time vector (Dimension N+1)
tb.add_input_sequence(node_name='a', x=x_in_a, t=t_in)
Example #13
0
A testbench is used to inject time varying signals at node A and B.
"""

from colna.analyticnetwork import Network, Edge, Testbench
import numpy as np
import matplotlib.pyplot as plt

### Create the Network and add the nodes
net = Network()

net.add_node(name='a')
net.add_node(name='b')
net.add_node(name='c')
net.add_node(name='d')

net.add_edge(Edge(start='a', end='b', phase=1, attenuation=0.8, delay=1))
net.add_edge(Edge(start='b', end='c', phase=2, attenuation=0.6, delay=2))
net.add_edge(Edge(start='b', end='d', phase=3, attenuation=0.4, delay=3))

# Visualize the network
net.visualize(path='./visualizations/feedforward_with_testbench', format='svg')

# Create a testbench
tb = Testbench(network=net,
               timestep=0.1)  # Timestep should be factor of all delays

# add an input signal to the testbench
x_in_a = np.sin(np.linspace(
    0, 15, 500)) + 1.5  # create the input signal (Dimensino N)
t_in = np.linspace(0, 10,
                   num=501)  # create the input time vector (Dimension N+1)
matplotlib.rc('axes', linewidth=2)
matplotlib.rc('lines', linewidth=2)
matplotlib.rc('font', **font)

net = Network()

amp1 = SymNum(name='v_1', product=True)
amp2 = SymNum(name='v_2', product=True)
amp3 = SymNum(name='v_3', product=True)
phi3 = SymNum(name='v_4', product=False)

net.add_node(name='a')
net.add_node(name='b')
net.add_node(name='c')

net.add_edge(Edge(start='a', end='b', phase=2, attenuation=amp1, delay=1))
net.add_edge(Edge(start='b', end='c', phase=1, attenuation=amp2, delay=2))
net.add_edge(
    Edge(start='c', end='a', phase=phi3, attenuation=0.5 * amp3, delay=3))
net.add_input('a')
net.add_input('b')

net.evaluate(amplitude_cutoff=0.001)
net.visualize(path='./visualizations/docdemo', format='png')
net.visualize(path='./visualizations/docdemo', format='svg')

print(net.get_result('b'))
print(net.get_latex_result('b', linebreak_limit=1))
net.get_html_result(['c', 'b'], path='./visualizations/docdemo_latex.html')
### Create a testbench with a feed dictionary
tb = Testbench(network=net,
class TestNetwork(unittest.TestCase):

    def setUp(self):
        self.net = Network()
        self.net.add_node('a')
        self.net.add_node('b')

    def test_add_node(self):
        # check that nodes where added correctly
        self.assertEqual(self.net.nodes, ['a', 'b'])

        # add a node
        self.net.add_node('d')
        self.assertEqual(self.net.nodes, ['a', 'b', 'd'])

    def test_add_node_duplication(self):
        """ Tests if node duplication is prohibited"""
        with self.assertRaises(ValueError):
            self.net.add_node('a')

        with self.assertRaises(ValueError):
            self.net.add_node('b')

    def test_add_edge(self):
        """ Tests if edges can be added to the network"""
        test_edge = Edge('a', 'b')
        self.net.add_edge(test_edge)
        self.assertEqual(self.net.edges[0], test_edge)

    def test_add_edge_Errors(self):
        """ Tests edge errors"""
        test_edge = Edge('a', 'c')
        with self.assertRaises(ValueError):
            self.net.add_edge(test_edge)

        test_edge = Edge('d', 'a')
        with self.assertRaises(ValueError):
            self.net.add_edge(test_edge)

        test_edge = Edge('d', 'c')
        with self.assertRaises(ValueError):
            self.net.add_edge(test_edge)

    def test_add_input(self):
        # add inputs to node a
        self.net.add_input('a', amplitude=0.2, phase=0.1, delay=0.1)
        self.assertEqual(self.net.inputs, [(0.2, 0.1, 0.1, 'a')])

        # add input to node b
        self.net.add_input('b', amplitude=0.5, phase=0.2, delay=0.0)
        self.assertEqual(self.net.inputs, [(0.2, 0.1, 0.1, 'a'), (0.5, 0.2, 0.0, 'b')])

        # add second input to node a
        self.net.add_input('a', amplitude=1.8, phase=-0.1, delay=-0.1)
        self.assertEqual(self.net.inputs, [(0.2, 0.1, 0.1, 'a'), (0.5, 0.2, 0.0, 'b'), (1.8, -0.1, -0.1, 'a')])

    def test_add_input_errors(self):
        # add input at non-existing node
        with self.assertRaises(ValueError):
            self.net.add_input('d', amplitude=1, phase=0.1, delay=2)

    def test_evaluate_feed_forward(self):
        """ creates and evaluates a feed forward network """
        ff_net = Network()
        ff_net.add_node('a')
        ff_net.add_node('b')
        ff_net.add_node('c')

        ff_net.add_edge(Edge('a', 'b', phase=0.5, attenuation=0.8, delay=2))
        ff_net.add_edge(Edge('b', 'c', phase=-5, attenuation=1.5, delay=-1))

        ff_net.add_input('a', amplitude=1)

        ff_net.evaluate()

        self.assertEqual(ff_net.nodes_to_output, {'a': [(1, 0, 0, '-a')],
                                                  'b': [(1 * 0.8, 0 + 0.5, 2, '-a-b')],
                                                  'c': [(1 * 0.8 * 1.5, 0 + 0.5 - 5, 2 - 1, '-a-b-c')]})

    def test_evaluate_loop(self):
        edge_1 = Edge('a', 'b', phase=1, attenuation=0.4, delay=2)
        edge_2 = Edge('b', 'c', phase=2, attenuation=0.3, delay=1)
        edge_3 = Edge('c', 'a', phase=3, attenuation=0.2, delay=0)

        expected_result = {'a': [(1, 0.0, 0.0, '-a'), (edge_1.attenuation * edge_2.attenuation * edge_3.attenuation,
                                                       edge_1.phase + edge_2.phase + edge_3.phase,
                                                       edge_1.delay + edge_2.delay + edge_3.delay, '-a-b-c-a')],
                           'b': [(edge_1.attenuation, edge_1.phase, edge_1.delay, '-a-b'),
                                 (edge_1.attenuation * edge_2.attenuation * edge_3.attenuation * edge_1.attenuation,
                                  edge_1.phase + edge_2.phase + edge_3.phase + edge_1.phase,
                                  edge_1.delay + edge_2.delay + edge_3.delay + edge_1.delay, '-a-b-c-a-b')],
                           'c': [(edge_1.attenuation * edge_2.attenuation, edge_1.phase + edge_2.phase,
                                  edge_1.delay + edge_2.delay, '-a-b-c'),
                                 (
                                     edge_1.attenuation * edge_2.attenuation * edge_3.attenuation * edge_1.attenuation * edge_2.attenuation,
                                     edge_1.phase + edge_2.phase + edge_3.phase + edge_1.phase + edge_2.phase,
                                     edge_1.delay + edge_2.delay + edge_3.delay + edge_1.delay + edge_2.delay,
                                     '-a-b-c-a-b-c')]}

        loop_net = Network()
        loop_net.add_node('a')
        loop_net.add_node('b')
        loop_net.add_node('c')
        loop_net.add_edge(edge_1)
        loop_net.add_edge(edge_2)
        loop_net.add_edge(edge_3)
        loop_net.add_input('a', amplitude=1)

        loop_net.evaluate(amplitude_cutoff=1e-3)
        self.assertEqual(loop_net.nodes_to_output, expected_result)

    def test_evaluate_loop_edge_order_independance(self):
        """
        Checks that the order in which we add edge to a network does not matter
        """
        pass

    def test_evaluate_splitting(self):
        """ creates and evaluates a feed forward split """
        edge_1 = Edge('a', 'b', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('a', 'c', phase=-0.5, attenuation=1.5, delay=-1)

        expected_nodes_to_output = {'a': [(1, 0, 0, '-a')],
                                    'b': [(edge_1.attenuation, edge_1.phase, edge_1.delay, '-a-b')],
                                    'c': [(edge_2.attenuation, edge_2.phase, edge_2.delay, '-a-c')]}

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('a', amplitude=1)
        split_net.evaluate()

        self.assertEqual(split_net.nodes_to_output, expected_nodes_to_output)

    def test_evaluate_combining(self):
        """ creates and evaluates a feed forward combiner """
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        expected_nodes_to_output = {'a': [(edge_1.attenuation, edge_1.phase, edge_1.delay, '-b-a'),
                                          (edge_2.attenuation, edge_2.phase, edge_2.delay, '-c-a')],
                                    'b': [(1, 0, 0, '-b')],
                                    'c': [(1, 0, 0, '-c')]}

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        self.assertEqual(split_net.nodes_to_output, expected_nodes_to_output)

    def test_get_reduced_output(self):
        """ creates and evaluates a feed forward combiner """
        edge_1 = Edge('b', 'a', phase=0, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=0, attenuation=1.5, delay=2)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        amp, phase, delay = split_net.get_reduced_output('a')

        self.assertEqual(amp[0], 2)
        self.assertEqual(phase[0], 0)
        self.assertEqual(delay[0], 2)

        split_net.edges[1].phase=-np.pi/2
        split_net.edges[1].attenuation=0.5

        split_net.evaluate()
        amp, phase, delay = split_net.get_reduced_output('a')
        self.assertAlmostEqual(amp[0], 1/np.sqrt(2),places=5)
        self.assertAlmostEqual(phase[0], -np.pi/4, places=5)
        self.assertEquals(delay[0], 2)

    def test_get_eval_result(self):
        """ creates and evaluates a feed forward combiner """
        edge_1 = Edge('b', 'a', phase=SymNum('phi1',default=0.5,product=False), attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=SymNum('phi2',default=0.0,product=False), attenuation=SymNum('amp2',default=1.5,product=True), delay=-1)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        self.assertEqual(split_net.get_eval_result('a'), [(0.5, 0.5, 2.0), (1.5, 0.0, -1.0)])
        self.assertEqual(split_net.get_eval_result('a',feed_dict=None, use_shared_default=True), [(0.5, 0.5, 2.0), (1.5, 0.0, -1.0)])
        self.assertEqual(split_net.get_eval_result('a',feed_dict={'phi1':0.6,'phi2':3,'amp2':6}, use_shared_default=True), [(0.5, 0.6, 2.0), (6.0, 3.0, -1.0)])


    def test_visualize(self):
        """ This test only checks that a graph is generated.

        It does not check if the graph does match the network description. This test will fail if graphviz is not setup.
        """
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        split_net = Network()
        split_net.add_node('a')
        split_net.add_node('b')
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()
        self.assertEqual(split_net.visualize(show_edge_labels=True,path='./visualizations/test1'), './visualizations\\test1.pdf')
        self.assertEqual(split_net.visualize(show_edge_labels=False, path='./visualizations/test2'), './visualizations\\test2.pdf')
        self.assertEqual(split_net.visualize(show_edge_labels=True,format='svg',path='./visualizations/test1'), './visualizations\\test1.svg')
        rmtree('./visualizations') # remove the directory

    def test_get_latex_result(self):
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        split_net = self.net
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()
        self.assertEqual(split_net.get_latex_result('b'),'1\cdot\exp(j (0.0))\cdot b_{in}(t-0.0)')

        edge_1 = Edge('a', 'b', phase=1, attenuation=0.4, delay=2)
        edge_2 = Edge('b', 'c', phase=2, attenuation=0.3, delay=1.2)
        edge_3 = Edge('c', 'a', phase=3, attenuation=0.2, delay=0)

        loop_net = Network()
        loop_net.add_node('a')
        loop_net.add_node('b')
        loop_net.add_node('c')
        loop_net.add_edge(edge_1)
        loop_net.add_edge(edge_2)
        loop_net.add_edge(edge_3)
        loop_net.add_input('a', amplitude=1)

        loop_net.evaluate(amplitude_cutoff=1e-4)
        self.assertEqual(loop_net.get_latex_result('b',precision=2),'0.4\cdot\exp(j (1))\cdot a_{in}(t-2)+0.0096\cdot\exp(j (7))\cdot a_{in}(t-5.2)+0.00023\cdot\exp(j (13))\cdot a_{in}(t-8.4)')

    def test_get_html_result(self):
        """ This checks that the html file is generated. It does not verify the correctness of the output."""
        edge_1 = Edge('b', 'a', phase=0.5, attenuation=0.5, delay=2)
        edge_2 = Edge('c', 'a', phase=-0.5, attenuation=1.5, delay=-1)

        split_net = self.net
        split_net.add_node('c')

        split_net.add_edge(edge_1)
        split_net.add_edge(edge_2)
        split_net.add_input('b', amplitude=1)
        split_net.add_input('c', amplitude=1)
        split_net.evaluate()

        path='./html_test/out.html'
        split_net.get_html_result(['a','b'], path=path)
        self.assertEqual(os.path.exists(path),True)
        split_net.get_html_result('a', path=path)
        self.assertEqual(os.path.exists(path),True)

        rmtree('./html_test')
"""

from colna.analyticnetwork import Network, Edge, Testbench, SymNum
import numpy as np
import matplotlib.pyplot as plt

# Define Network
net = Network()

# Add three nodes
net.add_node(name='a')
net.add_node(name='b')
net.add_node(name='c')

# Add three edges (with mixed symbolic and numeric values)
net.add_edge(Edge(start='c', end='a', phase=0.5, attenuation=0.6, delay=0.1))
net.add_edge(
    Edge(start='a',
         end='b',
         phase=SymNum('ph_{ab}', default=5, product=False),
         attenuation=0.95,
         delay=0.2))
net.add_edge(
    Edge(start='b',
         end='c',
         phase=SymNum('ph_{bc}', default=3, product=False),
         attenuation=SymNum('amp_{bc}', default=0.8, product=True),
         delay=0.1))

# Visualize the network (if graphviz is installed)
net.visualize(path='./visualizations/quickstart2', format='svg')