In [2]:
from scipy.io import loadmat
import os,sys,glob
#Setup directories to be used
TCNAME = 'SyntheticGrids_5x5'
MAINFOLDER = '/data/ml2/rahul/fw-inference/'
N = 25
EXACTDIR   = MAINFOLDER+TCNAME+'/mat_out'
name = 'gridWi5Tr'
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'

def append_vec(v1,v2):
    if v1 == None:
        return v2
    elif v2== None:
        return v1
    else:
        return np.append(v1.ravel(),v2.ravel())

def removeIfExists(fname):
    if os.path.exists(fname):
        os.remove(fname)
def createIfAbsent(dirname):
    if not os.path.exists(dirname):
        os.mkdir(dirname)

In [6]:
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']=33

#Plot the results on a single 
comparison_idx = range(N*2)[1::2]
dim = len(comparison_idx)
f_l1 = figure(1,figsize=(10,10))
f_logz = figure(2,figsize=(10,10))
print "Comparison indices: ",comparison_idx,len(comparison_idx)
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
    figure(1)
    Y = l1_err/ctr
    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 = logz_err/ctr
    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(1)
plt.legend()#bbox_to_anchor=(0.9, 0.75),bbox_transform=plt.gcf().transFigure)
xlabel('MAP calls')
ylabel('Error in Marginals $(\zeta_{\mu})$')
ylim([0,0.4])
figure(2)
legend()#bbox_to_anchor=(0.9, 0.75),bbox_transform=plt.gcf().transFigure)
ylim([0,50])
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/gridWi5Tr_M_M_eps_logz_maxrho1.pdf'):
    os.remove('./plots/gridWi5Tr_M_M_eps_logz_maxrho1.pdf')
if os.path.exists('./plots/gridWi5Tr_M_M_eps_l1_maxrho1.pdf'):
    os.remove('./plots/gridWi5Tr_M_M_eps_l1_maxrho1.pdf')
figure(2)
plt.savefig('./plots/gridWi5Tr_M_M_eps_logz_maxrho1.pdf',bbox_inches='tight')
figure(1)
plt.savefig('./plots/gridWi5Tr_M_M_eps_l1_maxrho1.pdf',bbox_inches='tight')


Comparison indices:  [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49] 25
Processing :  $\mathcal{M}_{\delta}$
Average Length :  19
Processing :  $\mathcal{M}_{0.0001}$
Average Length :  57
Processing :  $\mathcal{M}$
Average Length :  64
Processing :  $\mathbb{L}_{\delta}$
Average Length :  14
Processing :  $\mathcal{M} \mathrm{(no\;correction)}$
Average Length :  29

In [ ]: