Function iagraphPlot

Synopse

This function plots a non-oriented graph in the eucidean space. It also has the capability of highlighting elements in the graph.

  • img = iagraphPlot(features,AM,SG = [],CV = [],SV = [])

    • Output

      • img: Image array containing the graph plot.
    • Input

      • features: Features matrix (N x 2), the lines indexes correspond to the graph nodes;
      • AM: Adjacency Matrix of the graph, no edges are represented by 'Inf';
      • SG: Adjacency matrix of a possible sub-graph, no edges are represented by 'Inf';
      • CV: Row vector of integers containing labels tha will correspond to different colors for the nodes in the graph;
      • SV: Row vector of integers containing labels tha will correspond to different shapes for the nodes in the graph;

Description

This function plots a non-oriented graph in the bidimensional eucidean space. Sub-graphs may be highlighted setting the parameter SG. The graph edges are represented by black dashed ('-') lines and the sub-graph edges are represented by green continous lines. Nodes of the graph may alsho be highlighted setting the options CV and SV

Function Code

 1 from numpy import*
 2 from matplotlib.pyplot import*
 3 from courseIA368Q1S2012.and_lib_0 import figureToArray
 4 import matplotlib.pyplot as plt
 5 
 6 
 7 
 8 def iagraphPlot(features,AM,SG = [],CV = [],SV = []):
 9 
10    img = figure(figsize=(6, 6))
11    shapes = ['o' , '^' , '$?$' , 'H' , '_' , 'p' , '+', 's' , '*' , 'd' , 'v' , '<' , '>' , 'x' , 'D']
12    line_styles = ['','solid',  'dashed', 'dashdot', 'dotted']
13    colors = ['b','r','g','c','m','y','k','w']
14 
15    if CV == []:
16       CV = len(features[:,0])*[0]
17 
18    if SV == []:
19       SV = len(features[:,0])*[0]
20 
21 
22    if SG!=[]:
23       AM[where(SG!=float('inf'))] = float('inf')
24 
25    for i in range(len(features[:,0])):
26       plot(features[i,0],features[i,1],color=colors[CV[i]],linestyle=' ',marker=shapes[SV[i]],markersize=10)
27 
28 
29       temp = where(AM[i,:]!=float('inf'))
30       for j in temp[0]:
31          x = [features[i,0],features[j,0]]
32          y = [features[i,1],features[j,1]]
33          plot(x,y,'k--')
34 
35       if SG!=[]:
36          temp2 = where(SG[i,:]!=float('inf'))
37          for l in temp2[0]:
38             x = [features[i,0],features[l,0]]
39             y = [features[i,1],features[l,1]]
40             plot(x,y,'g',linewidth = 2)
41 
42    plt.axis([features[:,0].min()-0.15, features[:,0].max()+0.15,features[:,1].min()-0.15,features[:,1].max()+0.15])
43 
44    return img

Examples

The first example displays a graph plot with its MST highlighted. The second, third and fourth examples display the OPF Fit results for different datasets with different classes represented with different vertices colors and prototypes and non-prototypes differentiated by the edges shape.

Example 1 - Graph and subgraph

1 from scipy.spatial import distance
2 from courseIA368Q1S2012.rob_biblio2 import*
3 from numpy import*
4 from courseIA368Q1S2012.and_lib_0 import figureToArray
5 from iaOPF import*
6 import matplotlib.pyplot as plt
7 import scipy.io
 1 #Loading Saturn Dataset
 2 data = scipy.io.loadmat(find_attachment_file('courseIA368Q1S2012/rob_proj_teste_spirals/spirals.mat'))
 3 
 4 #Dataset Pre-processing
 5 data = data['spirals']
 6 features = data[:,:-1]
 7 labels = data[:,-1]
 8 features,labels,_ ,_ =   setTrainingTestSets(features,labels,0.05)
 9 
10 
11 #Generating a complete graph
12 graph = iarandGraph(len(labels), 0.3,'yes')
13 AM  = distance.squareform(distance.pdist(features, euclidian))
14 AM[graph==float('inf')] = float('inf')
15 
16 #Finding its MST
17 SG = iaprim(AM)
18 img = iagraphPlot(features,AM,SG)
19 
20 #Image Pos-Processing
21 plt.xlabel('x')
22 plt.ylabel('y')
23 plt.title('Graph and subgraph')
24 
25 adshow(figureToArray(img))
/usr/lib/python2.6/dist-packages/scipy/io/matlab/mio.py:84: FutureWarning: Using struct_as_record default value (False) This will change to True in future versions
  return MatFile5Reader(byte_stream, **kwargs)

Example 2 - OPF Fit Cone-Torus

 1 #Loading Cone-Torus Dataset
 2 data = scipy.io.loadmat(find_attachment_file('courseIA368Q1S2012/rob_proj_teste_cone_torus/cone_torus.mat'))
 3 
 4 #Dataset Pre-processing
 5 data = data['cone_torus']
 6 features = data[:,:-1]
 7 labels = data[:,-1]
 8 labels[labels==2]=0
 9 features,labels,_ ,_ =   setTrainingTestSets(features,labels,0.5)
10 
11 
12 #Generating a complete graph
13 AM  = distance.squareform(distance.pdist(features, euclidian))
14 AM[AM==0] = float('inf')
15 
16 #Finding its MST
17 SG = iaprim(AM)
18 
19 #Finding its prototypes
20 prototypes = iamstseeds(SG, labels)
21 
22 #Setting the shapes vector and the colors vector
23 SV = zeros((1,len(labels)))
24 SV[0,prototypes] = 1
25 SV = SV[0].astype(int)
26 CV = labels.astype(int)
27 img = iagraphPlot(features,SG,[],CV,SV)
28 
29 #Image Pos-Processing
30 plt.xlabel('x')
31 plt.ylabel('y')
32 plt.title('OPF Fit Results')
33 
34 adshow(figureToArray(img))

Example 3 - OPF Fit Lines

 1 #Loading Lines Dataset
 2 data = scipy.io.loadmat(find_attachment_file('courseIA368Q1S2012/rob_proj_teste_lines/lines.mat'))
 3 
 4 #Dataset Pre-processing
 5 data = data['lines']
 6 features = data[:,:-1]
 7 labels = data[:,-1]
 8 labels[labels==2]=0
 9 features,labels,_ ,_ =   setTrainingTestSets(features,labels,0.5)
10 
11 
12 #Generating a complete graph
13 AM  = distance.squareform(distance.pdist(features, euclidian))
14 AM[AM==0] = float('inf')
15 
16 #Finding its MST
17 SG = iaprim(AM)
18 
19 #Finding its prototypes
20 prototypes = iamstseeds(SG, labels)
21 
22 #Setting the shapes vector and the colors vector
23 SV = zeros((1,len(labels)))
24 SV[0,prototypes] = 1
25 SV = SV[0].astype(int)
26 CV = labels.astype(int)
27 img = iagraphPlot(features,SG,[],CV,SV)
28 
29 #Image Pos-Processing
30 plt.xlabel('x')
31 plt.ylabel('y')
32 plt.title('OPF Fit Results')
33 
34 adshow(figureToArray(img))

Example 4 - OPF Fit Spirals

 1 #Loading Lines Dataset
 2 data = scipy.io.loadmat(find_attachment_file('courseIA368Q1S2012/rob_proj_teste_spirals/spirals.mat'))
 3 
 4 #Dataset Pre-processing
 5 data = data['spirals']
 6 features = data[:,:-1]
 7 labels = data[:,-1]
 8 labels[labels==2]=0
 9 features,labels,_ ,_ =   setTrainingTestSets(features,labels,0.5)
10 
11 
12 #Generating a complete graph
13 AM  = distance.squareform(distance.pdist(features, euclidian))
14 AM[AM==0] = float('inf')
15 
16 #Finding its MST
17 SG = iaprim(AM)
18 
19 #Finding its prototypes
20 prototypes = iamstseeds(SG, labels)
21 
22 #Setting the shapes vector and the colors vector
23 SV = zeros((1,len(labels)))
24 SV[0,prototypes] = 1
25 SV = SV[0].astype(int)
26 CV = labels.astype(int)
27 img = iagraphPlot(features,SG,[],CV,SV)
28 
29 #Image Pos-Processing
30 plt.xlabel('x')
31 plt.ylabel('y')
32 plt.title('OPF Fit Results')
33 
34 adshow(figureToArray(img))

See also