In [8]:
from scipy.io import loadmat
import os,sys,glob

#Setup directories to be used
TCNAME = 'Synthetic'
MAINFOLDER = '/data/ml2/rahul/fw-inference/'
N = 10
EXACTDIR   = MAINFOLDER+'/Synthetic/mat_out'
name = ''
folder  = {}
colormap = {}
markers   = {}
plotname = '$\mathcal{M} \mathrm{(no\;correction)}$'
folder[plotname] = MAINFOLDER +  TCNAME+  '/resultsSpanningfw_m_ilpMARG/TRW'
colormap[plotname] = 'g'
markers[plotname] = 'v'

plotname = '$\mathcal{M}$'
folder[plotname] = MAINFOLDER +  TCNAME+ '/resultsSpanningfw_mfw_m_ilpMARG/TRW'
colormap[plotname] = 'b'
markers[plotname] = '>'

plotname = '$\mathcal{M}_{\delta}$'
folder[plotname] = MAINFOLDER+  TCNAME +'/resultsSpanningfw_mfw_m_eps_ilpMARG/TRW'
colormap[plotname] = 'm'
markers[plotname] = '<'

plotname = '$\mathcal{M}_{0.0001}$'
folder[plotname] = MAINFOLDER+  TCNAME + '/resultsSpanningfixed_ilpMARG/TRW'
colormap[plotname] = 'k'
markers[plotname] = '|'

plotname = '$\mathbb{L}_{\delta}$'
folder[plotname] = MAINFOLDER+  TCNAME + '/resultsSpanning_lpLOCAL/TRW'
colormap[plotname] = 'r'
markers[plotname] = 'd'

#Function to add two vectors
def add_vec(v1,v2):
    assert len(v1.shape)==1 and len(v2.shape)==1,'v1 and v2 must be of dimension 1'
    diff = np.abs(len(v2)-len(v1))
    if len(v1)==len(v2):
        return v1+v2
    elif len(v1)<len(v2):
        return np.append(v1,np.zeros(diff,)+v1[-1])+v2
    elif len(v2)<len(v1):
        return np.append(v2,np.zeros(diff,)+v2[-1])+v1
    else:
        assert False,'This should not happen'

In [9]:
#Plot the results on a single 
comparison_idx = range(N*2)[1::2]
dim = len(comparison_idx)
print "Comparison indices: ",comparison_idx,len(comparison_idx)
plot_data_l1 = {}
plot_data_logz={}

for plotname in folder:
    print "Processing : ",plotname
    avg_len = 0
    l1_err = np.zeros(1,)
    logz_err = np.zeros(1,)
    ctr = 1
    for f in glob.glob(folder[plotname]+'/*-sp0.mat'):
        exact = loadmat(EXACTDIR+'/'+os.path.basename(f).replace('-sp0.mat','')+'.mat')
        mat   = loadmat(f)
        #L1 marginals
        marginals = mat['IterSet'][:,comparison_idx]
        numIts = marginals.shape[0]
        exact_marginals = exact['exact_node_marginals'][comparison_idx].reshape(dim,1).repeat(numIts,1).transpose()
        l1_err = add_vec(np.abs(marginals-exact_marginals).sum(1)/N,l1_err)
        #Log Z
        if 'gap_full' not in mat:
            logz_err = add_vec(np.abs((-1*mat['Obj_Val']+mat['Dual_Gap']).ravel()-exact['log_partition'][0][0]),logz_err)
        else:
            logz_err = add_vec(np.abs((-1*mat['Obj_Val']+mat['gap_full']).ravel()-exact['log_partition'][0][0]),logz_err)
        avg_len += numIts
        ctr +=1
    avg_len /= ctr
    print "Average Length : ",avg_len
    plot_data_l1[plotname] = l1_err/ctr
    plot_data_logz[plotname]=logz_err/ctr


Comparison indices:  [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 10
Processing :  $\mathcal{M}_{\delta}$
Average Length :  8
Processing :  $\mathcal{M}_{0.0001}$
Average Length :  12
Processing :  $\mathcal{M}$
Average Length :  16
Processing :  $\mathbb{L}_{\delta}$
Average Length :  3
Processing :  $\mathcal{M} \mathrm{(no\;correction)}$
Average Length :  13

In [10]:
print plotname
plot_data_l1[plotname].shape


$\mathcal{M} \mathrm{(no\;correction)}$
Out[10]:
(21,)

In [12]:
import matplotlib as mpl
mpl.rcParams['lines.linewidth']=4.5
mpl.rcParams['lines.markersize']=12
mpl.rcParams['text.usetex']=True
mpl.rcParams['text.latex.unicode']=True
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Times New Roman'
mpl.rcParams['text.latex.preamble']= '\usepackage{amsfonts}'
mpl.rcParams['font.size'] = 40
mpl.rcParams['axes.labelsize']=40
mpl.rcParams['legend.fontsize']=35
f_l1 = figure(1,figsize=(10,10))
f_logz = figure(2,figsize=(10,10))
    
for plotname in folder:
    figure(1)
    Y = plot_data_l1[plotname]
    X= np.arange(Y.shape[0])
    #X = np.arange(1,Y.shape[0]+1,3)
    #Y = Y[np.arange(0,Y.shape[0],3)]
    plot(X,Y,label=plotname,color = colormap[plotname],marker = markers[plotname])
    figure(2)
    Y = plot_data_logz[plotname]
    X= np.arange(Y.shape[0])
    #X = np.arange(1,Y.shape[0]+1,3)
    #Y = Y[np.arange(0,Y.shape[0],3)]
    print Y.shape,plot_data_logz[plotname].shape
    plot(X,Y,label=plotname,color = colormap[plotname],marker = markers[plotname])
    
figure(1)
legend(bbox_to_anchor=(0.9, 0.75),bbox_transform=plt.gcf().transFigure)
xlim([0,25])
ylim([0,0.35])
xlabel('MAP calls')
ylabel('Error in Marginals $(\zeta_{\mu})$')
figure(2)
legend(bbox_to_anchor=(0.9, 0.7),bbox_transform=plt.gcf().transFigure)
ylim([0,60])
xlim([0,25])
xlabel('MAP calls')
ylabel('Error in LogZ $(\zeta_{\mathrm{\log Z}})$')

#Save to file
if not os.path.exists('./plots'):
    os.mkdir('./plots')

if os.path.exists('./plots/synthetic_M_M_eps_logz_maxrho1.pdf'):
    os.remove('./plots/synthetic_M_M_eps_logz_maxrho1.pdf')
if os.path.exists('./plots/synthetic_M_M_eps_l1_maxrho1.pdf'):
    os.remove('./plots/synthetic_M_M_eps_l1_maxrho1.pdf')
figure(2)
plt.savefig('./plots/synthetic_M_M_eps_logz_maxrho1.pdf',bbox_inches='tight')
figure(1)
plt.savefig('./plots/synthetic_M_M_eps_l1_maxrho1.pdf',bbox_inches='tight')


(17,) (17,)
(23,) (23,)
(26,) (26,)
(14,) (14,)
(21,) (21,)

In [11]: