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_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_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_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_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')
    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_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)
 def test_equal(self):
     b = Edge(start='a', end='b', phase=.5, attenuation=.6, delay=0.7)
     self.assertEqual(self.edge == b, True)
     b = Edge(start='c', end='b', phase=.5, attenuation=.6, delay=0.7)
     self.assertEqual(self.edge == b, False)
     b = Edge(start='a', end='c', phase=.5, attenuation=.6, delay=0.7)
     self.assertEqual(self.edge == b, False)
     b = Edge(start='a', end='b', phase=.51, attenuation=.6, delay=0.7)
     self.assertEqual(self.edge == b, False)
     b = Edge(start='a', end='b', phase=.5, attenuation=.61, delay=0.7)
     self.assertEqual(self.edge == b, False)
     b = Edge(start='a', end='b', phase=.5, attenuation=.6, delay=0.71)
     self.assertEqual(self.edge == b, False)
    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)
Esempio n. 14
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)
from colna.analyticnetwork import Network, Edge, SymNum, Testbench

amp1 = SymNum(name='a1', default=1.5, product=True)
amp2 = SymNum(name='a2', default=2.0, product=True)
phi1 = SymNum(name='phi1', default=2.0, product=False)
phi2 = SymNum(name='phi2', default=3.0, product=False)

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=phi1, attenuation=amp1, delay=1))
net.add_edge(Edge(start='b', end='c', phase=phi2, attenuation=amp2, delay=2))
net.add_edge(Edge(start='b', end='d', phase=3, attenuation=0.4, delay=3))

net.add_input(name='a', amplitude=1.0, phase=0)

net.visualize(path='./visualizations/symbolicfeedforward', format='svg')

net.evaluate(use_shared_default=False, feed_dict=None)

# print('paths leading to c:', net.get_paths('c'))
# print('paths leading to d:', net.get_paths('d'))

print('waves arriving at c:', net.get_result('c'))
print('waves arriving at d:', net.get_result('d'))
net.get_html_result(['c', 'd'],
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,
 def test_add_device(self):
     self.net.add_device(self.splitter)
     self.assertEqual(self.net.nodes,['pd:split_0:i0', 'pd:split_0:o0', 'pd:split_0:o1'])
     self.assertEqual(self.net.edges,[Edge('pd:split_0:i0', 'pd:split_0:o0',0,1/np.sqrt(2)),
                                      Edge('pd:split_0:i0', 'pd:split_0:o1', 0, 1 / np.sqrt(2))])
A -> B
^    v
D <- C

"""
from colna.analyticnetwork import Network, Edge, SymNum

####
# Define Network
####
nodes = ['a', 'b', 'c', 'd']
edges = [
    Edge('a',
         'b',
         phase=SymNum('ph1', default=0.4, product=False),
         attenuation=0.95,
         delay=1.0),
    Edge('b', 'c', .5, SymNum('amp1', default=0.95), 1.),
    Edge('c', 'd', .5, 0.95, SymNum('del1', default=1.2, product=False)),
    Edge('d', 'a', .5, 0.95, 1.)
]

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)

net.visualize(path='./visualizations/symbolicrecurrent')
 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 setUp(self):
     self.edge = Edge(start='a', end='b', phase=.5, attenuation=.6, delay=0.7)
Esempio n. 21
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)
"""

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')
# v    v    v    ^
# O <- M <- K <- I
# v    v    ^    ^
# A -> C -> E -> G
# v    v    ^    ^
# B -> D -> F -> H

####
# Define Network
####
nodes = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p'
]
edges = [
    Edge('a', 'b'),
    Edge('a', 'c'),
    Edge('b', 'd'),
    Edge('c', 'd'),
    Edge('c', 'e'),
    Edge('d', 'f'),
    Edge('f', 'e'),
    Edge('e', 'g'),
    Edge('f', 'h'),
    Edge('h', 'g'),
    Edge('g', 'i'),
    Edge('e', 'k'),
    Edge('i', 'j'),
    Edge('i', 'k'),
    Edge('j', 'l'),
    Edge('l', 'k'),
Esempio n. 24
0
Network topology:

A -> B
^    v
D <- C

"""
from colna.analyticnetwork import Network, Edge

###Define Network

nodes = ['a', 'b', 'c', 'd']
edges = [
    Edge(
        'a', 'b', phase=0.5, attenuation=1.1, delay=1.0
    ),  # some edges can have gain, but the overall gain of loops must be <1
    Edge('b', 'c', phase=1, attenuation=0.9, delay=2.0),
    Edge('c', 'd', phase=0.2, attenuation=0.98, delay=0.5),
    Edge('d', 'a', phase=-0.5, attenuation=0.8, delay=1.5)
]

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)
net.visualize(path='./visualizations/recurrent', format='svg')

####