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

Subversion Repositories nocmodel

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

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 4 dargor
import warnings
52 2 dargor
 
53
from noc_base import *
54
 
55
def draw_noc(noc, rectangular=True, nodepos=None):
56
    """
57
    Draw a representation of a NoC
58
 
59
    Arguments:
60
    * noc: Model to draw
61
    * rectangular: If True assumes rectangular layout, with routers
62
      having coord_x and coord_y attributes. If false, expect router's
63
      positions in nodepos argument
64
    * nodepos: Optional dictionary where keys are router's indexes and values
65
      are tuples with x and y positions.
66
    """
67 3 dargor
    if not has_matplotlib:
68 4 dargor
        warnings.warn("Function not available")
69 3 dargor
        return None
70
 
71 2 dargor
    # node positions
72
    if rectangular:
73
        if nodepos == None:
74
            nodepos = {}
75
            for i in noc.router_list():
76
                nodepos[i.index] = (i.coord_x, i.coord_y)
77
    else:
78
        if nodepos == None:
79
            raise ValueError("For non-rectangular layouts this function needs argument 'nodepos'")
80
 
81
    # some parameters
82
    ip_relpos = (-0.3, 0.3) # relative to router
83
    # node labels
84
    nodelabels = {}
85
    for i in nodepos.iterkeys():
86
        nodelabels[i] = noc.node[i]["router_ref"].name
87
 
88
    # channel positions
89
    chpos = {}
90
    for i in noc.channel_list():
91
        ep = i.endpoints
92
        ep_1x = nodepos[ep[0].index][0]
93
        ep_1y = nodepos[ep[0].index][1]
94
        ep_2x = nodepos[ep[1].index][0]
95
        ep_2y = nodepos[ep[1].index][1]
96
        thepos = (ep_2x + ((ep_1x - ep_2x)/2.0), ep_2y + ((ep_1y - ep_2y)/2.0))
97
        chpos[i.index] = {"pos": thepos, "text": i.name}
98
 
99
    # start drawing
100
    nx.draw_networkx_nodes(noc, pos=nodepos, node_size=1000, node_color="blue", alpha=0.5)
101
    nx.draw_networkx_edges(noc, pos=nodepos, edge_color="black", alpha=1.0, width=3.0)
102
    nx.draw_networkx_labels(noc, pos=nodepos, labels=nodelabels, font_color="red")
103
 
104
    ax=plt.gca()
105
    # channel labels
106
    for i in chpos.itervalues():
107
        ax.text(i["pos"][0], i["pos"][1], i["text"], horizontalalignment="center", verticalalignment="top", bbox=dict(facecolor='red', alpha=0.2))
108
 
109
    # ipcore with channels and labels
110
    for i in noc.ipcore_list():
111
        thepos = nodepos[i.router_ref.index]
112
        # channel
113
        ax.arrow(thepos[0], thepos[1], ip_relpos[0], ip_relpos[1])
114
        # ip channel label
115
        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))
116
        # box with ipcore labels
117
        ax.text(thepos[0]+ip_relpos[0], thepos[1]+ip_relpos[1], i.name, horizontalalignment="center", bbox=dict(facecolor='green', alpha=0.2))
118
 
119 4 dargor
    # adjust axis TODO!
120 2 dargor
    plt.show()

powered by: WebSVN 2.1.0

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