from rsfproj import *
try:
import numpy as np
except:
raise Exception('Cannot find numpy')
import os
matlab = WhereIs('matlab')
if not matlab:
raise Exception('Cannot find Matlab')
matROOT = '../matfcts'
N = 1024
np.random.seed(2007)
n = np.random.randint(np.round(N/5.),np.round(4./5.*N),1)
k = np.random.randint(1,np.round(N/5.),1)
min1 = 0
max1 = 150
min2 = -.1
max2 = 4
LimIters = 30
Flow('A',None,
'sfspike n1=%(n)g n2=%(N)g | sfnoise rep=y seed=2008 | sfmath output="input/57" '%vars() )
pos = np.sort(np.round(np.random.rand(k)*N))
amp = np.random.randn(k)
Flow('x0',None,
'sfspike n1=%d nsp=%d k1=%s mag=%s'
%(N,k,string.join(map(str,pos),','),string.join(map(str,amp),',')) )
nbpts = 100
matfun = 'MakeParetoCurve'
Flow('res taus',['A','x0',os.path.join(matROOT,matfun+'.m')],
matlab + '''
-nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('$SOURCE','${SOURCES[1]}','$TARGET','${TARGETS[1]}',%(nbpts)d);quit"
'''%vars(),stdin=0,stdout=-1)
Flow('pcurve','taus res','sfcmplx ${SOURCES[:-1]} | sftransp',stdin=0)
Plot('pcurve',
'''
sfgraph label1="\F15 7\F-1 x\F15 7\F-1 \_\s75 1" label2="\F15 7\F-1 y-Ax\F15 7\F-1 \_\s75 2" title=""
min1=%(min1)g max1=%(max1)g min2=%(min2)g max2=%(max2)g symbol=+ symbolsz=6 parallel2=n
'''%vars() )
suffix = ['SPGL1','IST','ISTc','IRLS']
matfun = 'Track' +suffix[0]
for Iters,case in [[100,'_UnLim'],[LimIters,'_Lim']]:
xnorms = 'xnorms_'+suffix[0]+case
rnorms = 'rnorms_'+suffix[0]+case
lplot = suffix[0]+case
Flow([xnorms,rnorms],['A','x0',os.path.join(matROOT,matfun+'.m')],
matlab + '''
-nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('$SOURCE','${SOURCES[1]}','$TARGET','${TARGETS[1]}',%(Iters)g);quit"
'''%vars(),stdin=0,stdout=-1)
Flow(lplot,[xnorms, rnorms],'sfcmplx ${SOURCES[:-1]}',stdin=0)
Plot(lplot,
'''
sfgraph label1="\F15 7\F-1 x\F15 7\F-1 \_\s75 1" label2="\F15 7\F-1 y-Ax\F15 7\F-1 \_\s75 2" title=""
min1=%(min1)g max1=%(max1)g min2=%(min2)g max2=%(max2)g
plotcol=3 dash=5 parallel2=n
'''%vars() )
matfun = 'Track' +suffix[1]
lmult = 5e-4;
for Iters,case in [[2500,'_UnLim'],[LimIters,'_Lim']]:
xnorms = 'xnorms_'+suffix[1]+case
rnorms = 'rnorms_'+suffix[1]+case
lplot = suffix[1]+case
Flow([xnorms,rnorms],['A','x0',os.path.join(matROOT,matfun+'.m'),os.path.join(matROOT,'SolveIST.m')],
matlab + '''
-nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('$SOURCE','${SOURCES[1]}','$TARGET','${TARGETS[1]}',%(Iters)g,%(lmult)g);quit"
'''%vars(),stdin=0,stdout=-1)
Flow(lplot,[xnorms,rnorms],'sfcmplx ${SOURCES[:-1]} | sftransp',stdin=0)
Plot(lplot,
'''
sfgraph label1="\F15 7\F-1 x\F15 7\F-1 \_\s75 1" label2="\F15 7\F-1 y-Ax\F15 7\F-1 \_\s75 2" title=""
min1=%(min1)g max1=%(max1)g min2=%(min2)g max2=%(max2)g plotcol=4 dash=3 parallel2=n
'''%vars() )
matfun = 'Track' +suffix[2]
for Iters,case,locInnerIters in [[500,'_UnLim',10],[LimIters,'_Lim',2]]:
xnorms = 'xnorms_'+suffix[2]+case
rnorms = 'rnorms_'+suffix[2]+case
lplot = suffix[2]+case
Flow([xnorms,rnorms,'lmults'+case],['A','x0',os.path.join(matROOT,matfun+'.m'),os.path.join(matROOT,'SolveISTc.m')],
matlab + '''
-nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('$SOURCE','${SOURCES[1]}','$TARGET','${TARGETS[1]}','${TARGETS[2]}',%(Iters)d,%(locInnerIters)d);quit"
'''%vars(),stdin=0,stdout=-1)
Flow(lplot,[xnorms,rnorms],'sfcmplx ${SOURCES[:-1]} | sftransp',stdin=0)
Plot(lplot,
'''
sfgraph label1="\F15 7\F-1 x\F15 7\F-1 \_\s75 1" label2="\F15 7\F-1 y-Ax\F15 7\F-1 \_\s75 2" title=""
min1=%(min1)g max1=%(max1)g min2=%(min2)g max2=%(max2)g parallel2=n
plotcol=5
'''%vars() )
a = .5
sigma = 0
damp = 1e-3
for InnerIters,Iters,case in [[1e2,5e3,'_UnLim'],[10,LimIters,'_Lim']]:
matfun = 'Track' +suffix[3]
xnorms = 'xnorms_'+suffix[3]+case
rnorms = 'rnorms_'+suffix[3]+case
lplot = suffix[3]+case
Flow([xnorms,rnorms],['A','x0',os.path.join(matROOT,matfun+'.m'),os.path.join(matROOT,'SolveIRLS.m')],
matlab + '''
-nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('$SOURCE','${SOURCES[1]}','$TARGET','${TARGETS[1]}',%(Iters)g,%(InnerIters)g,%(a)g,%(sigma)g,%(damp)g);quit"
'''%vars(),stdin=0,stdout=-1)
Flow(lplot,[xnorms,rnorms],'sfcmplx ${SOURCES[:-1]} | sftransp',stdin=0)
Plot(lplot,
'''
sfgraph label1="\F15 7\F-1 x\F15 7\F-1 \_\s75 1" label2="\F15 7\F-1 y-Ax\F15 7\F-1 \_\s75 2" title=""
min1=%(min1)g max1=%(max1)g min2=%(min2)g max2=%(max2)g plotcol=7 dash=2 parallel2=n
'''%vars() )
Result('plot','pcurve ISTc_UnLim IST_UnLim SPGL1_UnLim IRLS_UnLim','Overlay')
Result('plotLim','pcurve ISTc_Lim IST_Lim SPGL1_Lim IRLS_Lim','Overlay')
End()
|