Hi Everyone,
I'm trying to move some C++ code over to weave, but am running into
some problems that are difficult to debug. Specifically, there are
try/catch messages referring to lines of code in my module that do not
exist. Here is the error message:
[Histrionicus:python/Biometric/tmp] chris% python2.3 Blackduck.py
file changed
Compiling code...
Blackduck.py: In function `PyObject* compiled_func(PyObject*,
PyObject*)':
Blackduck.py:426: parse error before `catch'
Blackduck.py:432: must have at least one catch per try block
Traceback (most recent call last):
File "Blackduck.py", line 833, in ?
nxt_state =
weave.inline(sd_code,['cur_state','dec','wtp'],verbose=1)
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-
packages/weave/inline_tools.py", line 335, in inline
auto_downcast = auto_downcast,
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-
packages/weave/inline_tools.py", line 439, in compile_function
verbose=verbose, **kw)
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-
packages/weave/ext_tools.py", line 340, in compile
verbose = verbose, **kw)
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-
packages/weave/build_tools.py", line 272, in build_extension
setup(name = module_name, ext_modules = [ext],verbose=verb)
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-
packages/scipy_distutils/core.py", line 42, in setup
return old_setup(**new_attr)
File
"/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/
distutils/core.py", line 166, in setup
raise SystemExit, "error: " + str(msg)
weave.build_tools.CompileError: error: command 'gcc' failed with exit
status 1
My inline code ends at line 420, but I get error messages from weave
outside those lines. I dont use 'catch' anywhere in my code, yet I am
given an error message referring to it. Here is the call and the code
for those willing to look into this:
sd_code = '''
#line 56 "Blackduck.py"
#define NPOP 3
#define NPROD 2
#define NSURV 2
#define NAGE 2
#define NSEX 2
#define NHARV 6
py::list nxt_state(NPOP);
double Harv[NHARV][NPROD][NAGE][NSEX][NPOP],
Spr_A[NSEX][NPOP],ss[NSEX],
wts[NSURV] = {0.881, 0.119},//production model weights
Harv_S[NPROD][NAGE][NSEX][NPOP],
Harv_N[NPROD][NAGE][NSEX][NPOP],
next[NPROD][NSURV][NAGE][NSEX][NPOP],
Fall_Pre[NPROD][NAGE][NSEX][NPOP],
Fall_N[NPROD][NAGE][NSEX][NPOP],
movekill[NAGE][NSEX][NPOP][NHARV],
//objective function values
Value [2] = {1., 0.} , //values for bd and mallard harvest
cripple [2] = {0.20, 0.20} ;
int i = 0,
j = 0,
k = 0,
q = 0,
m = 0,
n = 0,
age = 0,
sex = 0,
pop = 0,
pop0 = 0,
pop1 = 0,
harv = 0,
prod = 0,
surv = 0,
init = 1,
done = 0,
debug = 0;
static const double
P_am = 0.543 ,
DV[NAGE][NSEX]={
{1,0.94},
{2.03, 1.88}
},
b[NPROD][NPOP][3] ={
{
{1.755, -0.2455, -0.2345},
{ 1.164, -0.2455, -0.2345},
{0.9171, -0.2455, -0.2345}
},
{
{1.463, -0.2392, 0},
{1.674, -0.7436, 0},
{0.8289, -0.2009, 0}
}
},
Ac[NSURV][2]= {
{1.033288, 0},
{1.033288, -0.000001}
},
move_n[NAGE][NSEX][NPOP][3]
={
{
{
{0.984, .016, 0},
{.081, 0.904, .015},
{0, .026, .974}
},
{
{.974, .026, 0},
{.052, .93, .018},
{0 ,.014 ,.986}
}
},
{
{
{0.984, .016, 0},
{.081, 0.904, .015},
{0, .026, .974}
},
{
{.974, .026, 0},
{.052, .93, .018},
{0 ,.014 ,.986}
}
}
},
move_s[NAGE][NSEX][NPOP][3]=
{
{
{
{.506, .214, .280},
{.048, .689, .263},
{.002, .914, .084}
},
{
{.451, .262, 0.287},
{.062, .686, .252},
{ 0.002, .89, .108}
}
},
{
{
{.506, .214, .280},
{.048, .689, .263},
{.002, .914, .084}
},
{
{.451, .262, 0.287},
{.062, .686, .252},
{ 0.002, .89, .108}
}
}
},
fidel[NAGE][NSEX][NPOP][NPOP]=
{
{
{
{0.89,0.055,0.055},
{0.065,0.87,0.065},
{0.06,0.06,0.88}
},
{
{0.88,0.06,0.06},
{0.07,0.86,0.07},
{0.075,0.075,0.85}
}
},
{
{
{0.39,0.305,0.305},
{0.32,0.36,0.32},
{0.325,0.325, 0.35}
},
{
{0.41,0.295,0.295},
{0.31,0.38,0.31},
{0.295,0.295, 0.41}
}
}
},
ssf=0.75;
double temp;
double N[NPOP],M[NPOP],
A[NPROD][NPOP],
H[NHARV][NAGE][NSEX],
K[NHARV][NAGE][NSEX],
Kill_N[NPROD][NAGE][NSEX][NPOP],
Kill_S[NPROD][NAGE][NSEX][NPOP],
Wint[NPROD][NAGE][NSEX][NPOP],
Wint_T[NPROD][NPOP],
SS_m[NPROD][NSURV][NPOP],
SS[NPROD][NSURV][NPOP][NAGE][NSEX],
surv_joint[NPROD][NSURV][NAGE][NSEX][NPOP],
spring_pre[NPROD][NSURV][NAGE][NSEX][NPOP],
next_[NPROD][NSURV][NAGE][NSEX][NPOP][NPOP],
pwt,nval,mval,harv_dec,state,
wnext[NPROD][NSURV][NAGE][NSEX][NPOP],
hss[NAGE][NSEX][NPOP];
// Index constant values.
//Jianping add
ss[1]=ssf;
ss[0]=ssf*(P_am/(1.-P_am));
//Loop over breeding populations
for (pop=0;pop<3;pop++)
{
// Scaled population sizes
nval = py_to_float(PyList_GetItem(cur_state,pop),"nval");
N[pop] = nval;
mval = py_to_float(PyList_GetItem(cur_state,pop+3),"mval");
M[pop] = mval;
// Spring adults
Spr_A [0][pop]=P_am *N[pop];
Spr_A [1][pop]=(1-P_am)*N[pop];
//Fall adults -- loop over production models
for (prod=0;prod<2;prod++)
{
// Calculate fall age ratio
A[prod][pop]= b[prod][pop][0] + b[prod][pop][1]*N[pop]/100000. +
b[prod][pop][2]*M[pop]/100000. ;
A[prod][pop] =exp(A[prod][pop]);
for (sex=0;sex<2;sex++)
{
// Spring survival
Fall_Pre[prod][0][sex][pop] =Spr_A[sex][pop]*ss[sex];
// Recruitment
Fall_Pre[prod][1][sex][pop]= 0.5*A[prod][pop]*
(Fall_Pre[prod][0][0][pop]+Fall_Pre[prod][0][1][pop]);
}
}
for (k=0;k<6;k++)
{
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
// Harvest rates
harv_dec = py_to_float(PyList_GetItem(dec,k),"harv_dec");
H[k][age][sex]=DV[age][sex]*harv_dec;
K[k][age][sex]=H[k][age][sex]/(1-cripple[0]);
}
}
}
//Joint movement and kill rates
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
// Loop over breeding areas
for (pop=0;pop<3;pop++)
{
// Loop over harvest areas
for (harv=0;harv<3;harv++)
{
movekill[age][sex][pop][harv]=move_n[age][sex][pop][harv]
*K[harv][age][sex];
// age and sex specific?
}
for (harv=0;harv<3;harv++)
{
movekill[age][sex][pop][harv+3]=
move_s[age][sex][pop][harv]*K[harv+3][age][sex];
}
//Suvival of southern hunting
hss[age][sex][pop]=1-
(movekill[age][sex][pop][3]+movekill[age][sex][pop][4]
+movekill[age][sex][pop][5]);
}
}
}
// Movement to northern harvest areas and harvest
// Loop over production models
for (prod=0;prod<2;prod++)
{
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
//Loop over pop areas
// these are survivors of northern breeding areas
for (pop=0;pop<3;pop++)
{
// Loop over northern harvest areas
Kill_N[prod][age][sex][pop]=0;
Kill_S[prod][age][sex][pop]=0;
//Nothern kill
Kill_N[prod][age][sex][pop]=Fall_Pre[prod][age][sex][pop]*
(movekill[age][sex][pop][0]+movekill[age][sex][pop][1]+movekill[age][sex
][pop][2]);
for(harv=0;harv<3;harv++)
{
Harv[harv][prod][age][sex][pop]=Fall_Pre[prod][age][sex][pop]*
movekill[age][sex][pop][harv]*(1-cripple[0]);
}
//population after northern harvest with respect to breeding area
Fall_N[prod][age][sex][pop]=Fall_Pre[prod][age][sex][pop]-
Kill_N[prod][age][sex][pop];
//Convert kill to harvest
Harv_N[prod][age][sex][pop]=Kill_N[prod][age][sex][pop]*(1-
cripple[0]);
//Southern kill
Kill_S[prod][age][sex][pop]=Fall_N[prod][age][sex][pop]*
(movekill[age][sex][pop][3]+movekill[age][sex][pop][4]+movekill[age][sex
][pop][5]);
for(harv=3;harv<6;harv++)
{
Harv[harv][prod][age][sex][pop]=Fall_Pre[prod][age][sex][pop]*
movekill[age][sex][pop][harv]*(1-cripple[1]);
}
//population after southern harvest
Wint[prod][age][sex][pop]=Fall_N[prod][age][sex][pop]-
Kill_S[prod][age][sex][pop];
//convert kill to harvest
Harv_S[prod][age][sex][pop]=Kill_S[prod][age][sex][pop]*(1-
cripple[1]);
}
}//end of for (sex=0;sex<2;sex++)
} //end of for (age=0;age<2;age++)
for (pop=0; pop < NPOP; pop++)
{
Wint_T[prod][pop]=0;
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
// Sum populations over age and sex,
Wint_T[prod][pop]+=Wint[prod][age][sex][pop];
}
}
}
for (surv=0;surv<2;surv++)
{
for (pop=0;pop<3;pop++)
{
// Winter survival, males
SS_m[prod][surv][pop] = Ac[surv][0] +
Ac[surv][1]*3*Wint_T[prod][pop];
temp=SS_m[prod][surv][pop];
SS_m[prod][surv][pop]=exp(temp)/(1+exp(temp));
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
// # Survival by age and sex
SS[prod][surv][pop][age][sex] =SS_m[prod][surv][pop]/ss[sex];
// # Joint survival of Southern hunting
surv_joint[prod][surv][age][sex][pop]
=SS[prod][surv][pop][age][sex] *
hss[age][sex][pop];
// # Spring population pre-movement
spring_pre[prod][surv][age][sex][pop] =
Fall_N[prod][age][sex][pop] *
surv_joint[prod][surv][age][sex][pop] ;
}//for(sex=
}//for age=
}//for pop=
for (age=0;age<2;age++)
{
for (sex=0;sex<2;sex++)
{
// # Migrate back to breeding areas to determine next years
populations
for (pop1=0;pop1<3; pop1++)
{
//next_[pop][prod][surv][age][sex]=0;
for (pop0=0;pop0<3; pop0++)
{
next_[prod][surv][age][sex][pop1][pop0]=spring_pre[prod][surv][age][sex]
[pop0]
* fidel[age][sex][pop0][pop1];
}
//aggregate t return breeding area
next[prod][surv][age][sex][pop1]=next_[prod][surv][age][sex][pop1][0]+
next_[prod][surv][age][sex][pop1][1]+
next_[prod][surv][age][sex][pop1][2];
//# Weighted next year
pwt = py_to_float(PyList_GetItem(wtp,prod),"pwt");
wnext[prod][surv][age][sex][pop1]=next[prod][surv][age][sex][pop1]*pwt*w
ts[surv];
}//edn pop1
}//end of for (sex=0;sex<2;sex++)
}//end of for (age=0;age<2;age++)
}//end of for (surv=0;surv<3;surv++)
}//end of for (prod=0;prod<2;prod++)
// # Sum across age sex and models to get next year's populations
for (pop=0;pop < NPOP;pop++)
{
//nxt_state[pop]=0;
for (prod=0;prod < NPROD;prod++)
{
for (age=0;age < NAGE;age++)
{
for(surv=0;surv<2;surv++)
{
for (sex=0;sex < NSEX;sex++)
{
state = py_to_float(PyList_GetItem(nxt_state,pop),"state");
PyList_SetItem(nxt_state,pop,PyFloat_FromDouble(state +
wnext[prod][surv][age][sex][pop]));
}
}
}
}
}
return_val = nxt_state;
'''
if __name__=='__main__':
n = [100000,100000,100000]
m = [10000,1000,1000]
cur_state = n+m
dec = [0.1]*6
wtp = [0.5,0.5]
nxt_state = weave.inline(sd_code,['cur_state','dec','wtp'],verbose=1)
print nxt_state
thanks a bunch in advance,
cjf
--
Christopher J. Fonnesbeck (chris at fonnesbeck dot org)
GA Coop. Fish & Wildlife Research Unit, University of Georgia