OpenCores
URL https://opencores.org/ocsvn/nocmodel/nocmodel/trunk

Subversion Repositories nocmodel

[/] [nocmodel/] [trunk/] [nocmodel/] [noc_guilib.py] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dargor
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
 
4
#
5
# Support for graphical representation of NoC objects
6
#
7
# Author:  Oscar Diaz
8
# Version: 0.1
9
# Date:    03-03-2011
10
 
11
#
12
# This code is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU Lesser General Public
14
# License as published by the Free Software Foundation; either
15
# version 2.1 of the License, or (at your option) any later version.
16
#
17
# This code is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
# Lesser General Public License for more details.
21
#
22
# You should have received a copy of the GNU Lesser General Public
23
# License along with this library; if not, write to the
24
# Free Software  Foundation, Inc., 59 Temple Place, Suite 330,
25
# Boston, MA  02111-1307  USA
26
#
27
 
28
#
29
# Changelog:
30
#
31
# 03-03-2011 : (OD) initial release
32
#
33
 
34
"""
35
================
36
NoCmodel Graphic utilities
37
================
38
 
39
This module declares functions to draw graphical representations of
40
NoCmodel objects.
41
 
42
"""
43 3 dargor
try:
44
    import matplotlib.pyplot as plt
45
    has_matplotlib = True
46
except:
47
    print("Matplotlib package not found. Drawing functions will not work.")
48
    has_matplotlib = False
49 2 dargor
 
50
import networkx as nx
51
 
52
from noc_base import *
53
 
54
def draw_noc(noc, rectangular=True, nodepos=None):
55
    """
56
    Draw a representation of a NoC
57
 
58
    Arguments:
59
    * noc: Model to draw
60
    * rectangular: If True assumes rectangular layout, with routers
61
      having coord_x and coord_y attributes. If false, expect router's
62
      positions in nodepos argument
63
    * nodepos: Optional dictionary where keys are router's indexes and values
64
      are tuples with x and y positions.
65
    """
66 3 dargor
    if not has_matplotlib:
67
        print("Function not available")
68
        return None
69
 
70 2 dargor
    # node positions
71
    if rectangular:
72
        if nodepos == None:
73
            nodepos = {}
74
            for i in noc.router_list():
75
                nodepos[i.index] = (i.coord_x, i.coord_y)
76
    else:
77
        if nodepos == None:
78
            raise ValueError("For non-rectangular layouts this function needs argument 'nodepos'")
79
 
80
    # some parameters
81
    ip_relpos = (-0.3, 0.3) # relative to router
82
    # node labels
83
    nodelabels = {}
84
    for i in nodepos.iterkeys():
85
        nodelabels[i] = noc.node[i]["router_ref"].name
86
 
87
    # channel positions
88
    chpos = {}
89
    for i in noc.channel_list():
90
        ep = i.endpoints
91
        ep_1x = nodepos[ep[0].index][0]
92
        ep_1y = nodepos[ep[0].index][1]
93
        ep_2x = nodepos[ep[1].index][0]
94
        ep_2y = nodepos[ep[1].index][1]
95
        thepos = (ep_2x + ((ep_1x - ep_2x)/2.0), ep_2y + ((ep_1y - ep_2y)/2.0))
96
        chpos[i.index] = {"pos": thepos, "text": i.name}
97
 
98
    # start drawing
99
    nx.draw_networkx_nodes(noc, pos=nodepos, node_size=1000, node_color="blue", alpha=0.5)
100
    nx.draw_networkx_edges(noc, pos=nodepos, edge_color="black", alpha=1.0, width=3.0)
101
    nx.draw_networkx_labels(noc, pos=nodepos, labels=nodelabels, font_color="red")
102
 
103
    ax=plt.gca()
104
    # channel labels
105
    for i in chpos.itervalues():
106
        ax.text(i["pos"][0], i["pos"][1], i["text"], horizontalalignment="center", verticalalignment="top", bbox=dict(facecolor='red', alpha=0.2))
107
 
108
    # ipcore with channels and labels
109
    for i in noc.ipcore_list():
110
        thepos = nodepos[i.router_ref.index]
111
        # channel
112
        ax.arrow(thepos[0], thepos[1], ip_relpos[0], ip_relpos[1])
113
        # ip channel label
114
        ax.text(thepos[0]+(ip_relpos[0]/2), thepos[1]+(ip_relpos[1]/2), i.channel_ref.name, horizontalalignment="center", bbox=dict(facecolor='red', alpha=0.2))
115
        # box with ipcore labels
116
        ax.text(thepos[0]+ip_relpos[0], thepos[1]+ip_relpos[1], i.name, horizontalalignment="center", bbox=dict(facecolor='green', alpha=0.2))
117
 
118
    plt.show()

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.