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
    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:
    print('number of paths to ' + node + ':', len(net.get_paths(node)))
print('final path to a added:', net.get_paths('a')[-1])
net.print_stats()

phases = np.asarray([val[1] for val in net.get_result('a')])
phases = phases % 2 * np.pi
Esempio n. 3
0
### 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)

# add output nodes to testbench (nodes at which output signal should be recorded)
tb.add_output_node('c')
tb.add_output_node('d')
Esempio n. 4
0
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)
tb.add_input_sequence(node_name='a', x=x_in_a, t=t_in)

# add output nodes to testbench (nodes at which output signal should be recorded)
tb.add_output_node('c')
tb.add_output_node('d')
# Add nodes
net.add_node(name='a')
net.add_node(name='b')
net.add_node(name='c')
net.add_node(name='d')

# Add edges
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))

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

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

# Evaluate the network
net.evaluate(amplitude_cutoff=1e-3)

# Compute output and show results
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'))
print('latex string for waves arriving at c:', net.get_latex_result('c'))

# render output in a html file
net.get_html_result(['c', 'd'],
                    precision=2,
                    path='./visualizations/feedforward.html')
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,
               timestep=0.05,
               feed_dict={
                   'v1': 0.8,
                   'v2': 0.8,
                   'v3': 0.9,
                   'v4': 3
               })
         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')

####
# Evaluate Network
####
net.evaluate(amplitude_cutoff=1e-2,
             max_endpoints=1e6,
             use_shared_default=False)

print('paths leading to a:', net.get_paths('a'))
waves = [
    tuple([w.eval() if hasattr(w, 'eval') else w for w in inner])
    for inner in net.get_result('a')
]
print('waves arriving at a:', waves, '\n')
net.print_stats()
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')

# Create a testbench
tb = Testbench(network=net, timestep=0.1)

# Add an input signal
tb.add_input_sequence(node_name='a',
                      x=np.array([1, 2, 0]),
                      t=np.array([0, 2, 7, 10]))

# register an output node
tb.add_output_node('c')

# set the feed dictionary for the symbolic numbers
tb.set_feed_dict({'amp_{bc}': 0.7, 'ph_{bc}': 3.1, 'ph_{ab}': 4.9})
Esempio n. 9
0
###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')

####
#Evaluate Network
####
net.evaluate(amplitude_cutoff=1e-1, max_endpoints=1e6)

####
#Print data
####
print('paths leading to a:', net.get_paths('a'))
print('waves arriving at a:', net.get_result('a'))
net.print_stats()