本文整理汇总了Python中utils.start_cpp.start_cpp函数的典型用法代码示例。如果您正苦于以下问题:Python start_cpp函数的具体用法?Python start_cpp怎么用?Python start_cpp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了start_cpp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loop
def test_loop(self):
extra = """
struct Number
{
int num;
};
"""
code = start_cpp(linked_list_code+extra) + """
int errors = 0;
List<Number> wibble;
for (int i=0;i<10;i++)
{
Item<Number> * it = wibble.Append();
it->num = i;
}
if (wibble.Size()!=10) errors += 1;
int i = 0;
for (Item<Number> * targ = wibble.First(); targ->Valid(); targ = targ->Next())
{
if (i!=targ->num) errors += 1;
i += 1;
}
return_val = errors;
"""
errors = weave.inline(code, support_code=linked_list_code+extra)
self.assertEqual(errors,0)
示例2: __buildMaskPyramid
def __buildMaskPyramid(self, mask, pyramid):
"""Given a mask and a pyramid of masks makes a pyramid, where it uses the or operation for combining flags."""
code = start_cpp() + """
// Make curr all false...
for (int y=0;y<Ncurr[0];y++)
{
for (int x=0;x<Ncurr[1];x++) CURR2(y,x) = 0;
}
// Iterate prev, and update curr...
for (int y=0;y<Nprev[0];y++)
{
for (int x=0;x<Nprev[1];x++)
{
if (PREV2(y,x)!=0)
{
CURR2(y/2,x/2) = 1;
}
}
}
"""
pyramid[0][:,:] = mask
for l in xrange(1,len(pyramid)):
prev = pyramid[l-1]
curr = pyramid[l]
weave.inline(code, ['prev','curr'])
示例3: test_size_gc
def test_size_gc(self):
code = start_cpp(linked_list_gc_code) + """
int errors = 0;
ListRef<> wibble;
if (wibble.Size()!=0) errors += 1;
ItemRef<> * it = wibble.Append();
if (wibble.Size()!=1) errors += 1;
if (wibble.RefTotal()!=0) errors += 1;
it->IncRef();
it->IncRef();
if (it->RefCount()!=2) errors += 1;
if (wibble.RefTotal()!=2) errors += 1;
it->DecRef();
it->DecRef();
if (wibble.RefTotal()!=0) errors += 1;
if (wibble.Size()!=0) errors += 1;
return_val = errors;
"""
errors = weave.inline(code, support_code=linked_list_gc_code)
self.assertEqual(errors,0)
示例4: testCodeC
def testCodeC(self, name, exemplar_list):
ret = start_cpp() + """
bool %(name)s(PyObject * data, void * test, size_t test_length, int exemplar)
{
int feature = *(int*)test;
float offset = *((float*)test + 1);
%(channelType)s channel = (%(channelType)s)PyTuple_GetItem(data, %(channel)i);
float value = (float)%(channelName)s_get(channel, exemplar, feature);
return (value-offset)>=0.0;
}
"""%{'name':name, 'channel':self.channel, 'channelName':exemplar_list[self.channel]['name'], 'channelType':exemplar_list[self.channel]['itype']}
return ret
示例5: addTrain
def addTrain(self, goal, gen, es, index=slice(None), weights=None, code=None):
"""This allows you to update the nodes with more data, as though it was used for trainning. The actual tests are not affected, only the statistics at each node - part of incrimental learning. You can optionally proivde code generated by the addTrainC method to give it go faster stripes."""
if isinstance(index, slice):
index = numpy.arange(*index.indices(es.exemplars()))
if code != None:
init = (
start_cpp(code)
+ """
if (dummy==0) // To allow for a dummy run.
{
Exemplar * test_set = (Exemplar*)malloc(sizeof(Exemplar)*Nindex[0]);
for (int i=0; i<Nindex[0]; i++)
{
int ind = index[i];
test_set[i].index = ind;
test_set[i].weight = weights[ind];
test_set[i].next = &test_set[i+1];
}
test_set[Nindex[0]-1].next = 0;
addTrain(self, data, test_set);
free(test_set);
}
"""
)
data = es.tupleInputC()
dummy = 1 if self == None else 0
if weights == None:
weights = numpy.ones(es.exemplars(), dtype=numpy.float32)
return weave.inline(init, ["self", "data", "index", "weights", "dummy"], support_code=code)
else:
# Update this nodes stats...
self.stats = goal.updateStats(self.stats, es, index, weights)
# Check if it has children that need updating...
if self.test != None:
# Need to split the index and send it down the two branches, as needed...
res = gen.do(self.test, es, index)
tIndex = index[res == True]
fIndex = index[res == False]
if tIndex.shape[0] != 0:
self.true.addTrain(goal, gen, es, tIndex, weights)
if fIndex.shape[0] != 0:
self.false.addTrain(goal, gen, es, fIndex, weights)
示例6: test_size
def test_size(self):
code = start_cpp(linked_list) + """
int errors = 0;
List<> wibble;
if (wibble.Size()!=0) errors += 1;
Item<> * it = wibble.Append();
if (wibble.Size()!=1) errors += 1;
it->Suicide();
if (wibble.Size()!=0) errors += 1;
return_val = errors;
"""
errors = weave.inline(code, support_code=linked_list)
self.assertEqual(errors,0)
示例7: evaluate
def evaluate(self, out, gen, es, index=slice(None), code=None):
"""Given a set of exemplars, and possibly an index, this outputs the infered stats entities. Requires the generator so it can apply the tests. The output goes into out, a list indexed by exemplar position. If code is set to a string generated by evaluateC it uses that, for speed."""
if isinstance(index, slice):
index = numpy.arange(*index.indices(es.exemplars()))
if isinstance(code, str) and weave != None:
init = (
start_cpp(code)
+ """
if (Nindex[0]!=0)
{
// Create the Exemplar data structure...
Exemplar * test_set = (Exemplar*)malloc(sizeof(Exemplar)*Nindex[0]);
for (int i=0; i<Nindex[0]; i++)
{
test_set[i].index = index[i];
test_set[i].next = &test_set[i+1];
}
test_set[Nindex[0]-1].next = 0;
// Do the work...
evaluate(self, data, test_set, out);
// Clean up...
free(test_set);
}
"""
)
data = es.tupleInputC()
weave.inline(init, ["self", "data", "index", "out"], support_code=code)
return
if self.test == None:
# At a leaf - store this nodes stats object for the relevent nodes...
for val in index:
out[val] = self.stats
else:
# Need to split the index and send it down the two branches, as needed...
res = gen.do(self.test, es, index)
tIndex = index[res == True]
fIndex = index[res == False]
if tIndex.shape[0] != 0:
self.true.evaluate(out, gen, es, tIndex)
if fIndex.shape[0] != 0:
self.false.evaluate(out, gen, es, fIndex)
示例8: nextFrame
def nextFrame(self):
# Increase frame number - need to be 1 based for this...
self.frame += 1
if self.frame>=self.start and self.frame<=self.end:
# Fetch the provided mask...
mask = self.video.fetch(self.channel)
# Load the ground truth file...
fn = os.path.join(self.path, 'groundtruth/gt%06i.png'%self.frame)
gt = cv2array(cv.LoadImage(fn))
gt = gt.astype(numpy.uint8)
if len(gt.shape)==3: gt = gt[:,:,0]
# Loop the pixels and analyse each one, summing into the confusion matrix...
code = start_cpp() + """
for (int y=0; y<Ngt[0]; y++)
{
for (int x=0; x<Ngt[1]; x++)
{
if ((ROI2(y,x)!=0)&&(GT2(y,x)!=170))
{
int t = (GT2(y,x)==255)?1:0;
int g = (MASK2(y,x)!=0)?1:0;
CON2(t,g) += 1;
if (GT2(y,x)==50)
{
SHADOW1(g) += 1;
}
}
}
}
"""
roi = self.roi
con = self.con
shadow = self.shadow
weave.inline(code, ['mask', 'gt', 'roi', 'con', 'shadow'])
return self.frame<=self.end
示例9: testCodeC
def testCodeC(self, name, exemplar_list):
# Add the children...
ret = ''
for i, gen in enumerate(self.gens):
ret += gen.testCodeC(name + '_%i'%i, exemplar_list)
# Put in the final test function...
ret += start_cpp()
ret += 'bool %s(PyObject * data, void * test, size_t test_length, int exemplar)\n'%name
ret += '{\n'
ret += 'void * sub_test = ((char*)test) + 1;\n'
ret += 'size_t sub_test_length = test_length - 1;\n'
ret += 'int which = *(unsigned char*)test;\n'
ret += 'switch(which)\n'
ret += '{\n'
for i in xrange(len(self.gens)):
ret += 'case %i: return %s_%i(data, sub_test, sub_test_length, exemplar);\n'%(i, name, i)
ret += '}\n'
ret += 'return 0;\n' # To stop the compiler issuing a warning.
ret += '}\n'
return ret
示例10: mean
def mean(self):
"""Returns an estimate of the mean for each value of the multinomial, as an array, given the evidence provided. (Will itself sum to one - a necesary consequence of being an average of points constrained to the simplex."""
code = start_cpp(smp_code) + """
srand48(time(0));
SMP smp(NflagMat[1],NflagMat[0]);
smp.SetFIA(flagMat);
smp.SetSampleCount(sampleCount);
smp.SetPrior(priorMN, priorConc);
smp.Add(power_array);
smp.Mean(out);
"""
flagMat = self.flagMat
sampleCount = self.sampleCount
priorMN = self.priorMN
priorConc = self.priorConc
power = self.power
out = numpy.empty(flagMat.shape[1] ,dtype=numpy.float32)
weave.inline(code, ['flagMat', 'sampleCount', 'priorMN', 'priorConc', 'power', 'out'], support_code=smp_code)
return out
示例11: answer_batch
def answer_batch(self, stats_lists, which, es, indices, trees):
if weave!=None:
esAccess = es.codeC(0, 'es')
code = start_cpp() + """
// Prepare the access to the es...
%(itype)s es = (%(itype)s)PyList_GetItem(esData, 0);
// Iterate and process each stat list in turn...
int item_count = PyList_Size(stats_lists);
PyObject * ret = PyList_New(item_count);
for (int i=0; i<item_count; i++)
{
// Get the list of stats objects...
PyObject * stats = PyList_GetItem(stats_lists, i);
int statCount = PyList_Size(stats);
// Iterate the list and handle each element in turn...
float p = 0.0;
for (int j=0; j<statCount; j++)
{
// Extract the information regarding the specific stat object...
float * params = (float*)(void*)PyString_AsString(PyList_GetItem(stats, j));
float * mean = params + 3;
float * prec = mean + feats;
// Put the delta into the temporary storage...
for (int k=0; k<feats; k++)
{
TEMP2(0, k) = es_get(es, indices[i], k) - mean[k];
TEMP2(1, k) = 0.0; // Preparation for the next bit.
}
// Calculate the multiplication with the precision...
for (int k=0; k<feats; k++)
{
for (int l=0; l<feats; l++)
{
TEMP2(1, k) += prec[feats*k+l] * TEMP2(0, l);
}
}
float d = 0.0;
for (int k=0; k<feats; k++)
{
d += TEMP2(0, k) * TEMP2(1, k);
}
// Do the final parts required...
p += exp(params[2] - 0.5 * d);
}
p /= statCount;
// Store the calculated probability...
PyObject * ans = PyFloat_FromDouble(p);
PyList_SetItem(ret, i, ans);
}
// Return...
return_val = ret;
Py_XDECREF(ret);
"""%{'itype':esAccess['itype']}
feats = self.feats
esData = [esAccess['input']]
temp = self.temp
ret = weave.inline(code, ['stats_lists', 'indices', 'feats', 'esData', 'temp'], support_code = esAccess['get'])
if isinstance(which, str): return ret
else:
return map(lambda p: tuple([p] * len(which)) , ret)
else:
return map(lambda (i, stats_list): self.answer(stats_list, which, es, indices[i], trees), enumerate(stats_lists))
示例12: codeC
def codeC(self, name, escl):
cStats = start_cpp() + """
void %(name)s_stats(PyObject * data, Exemplar * index, void *& out, size_t & outLen)
{
// Make sure the output it at least as large as classCount, and zero it out...
if (outLen<(sizeof(float)*%(classCount)i))
{
outLen = sizeof(float) * %(classCount)i;
out = realloc(out, outLen);
}
for (int i=0; i<(outLen/sizeof(float)); i++)
{
((float*)out)[i] = 0.0;
}
// Iterate and play weighted histogram, growing out as needed...
%(channelType)s cData = (%(channelType)s)PyTuple_GetItem(data, %(channel)i);
int maxSeen = %(classCount)i;
while (index)
{
int cls = %(channelName)s_get(cData, index->index, 0);
int cap = cls+1;
if (cap>maxSeen) maxSeen = cap;
if ((cap*sizeof(float))>outLen)
{
int zero_start = outLen / sizeof(float);
outLen = cap*sizeof(float);
out = realloc(out, outLen);
for (int i=zero_start; i<cap; i++)
{
((float*)out)[i] = 0.0;
}
}
((float*)out)[cls] += index->weight;
index = index->next;
}
// Correct the output size if needed (It could be too large)...
outLen = maxSeen * sizeof(float);
}
"""%{'name':name, 'channel':self.channel, 'channelName':escl[self.channel]['name'], 'channelType':escl[self.channel]['itype'], 'classCount':self.classCount if self.classCount!=None else 1}
cUpdateStats = start_cpp() + """
void %(name)s_updateStats(PyObject * data, Exemplar * index, void *& inout, size_t & inoutLen)
{
// Iterate and play weighted histogram, growing out as needed...
%(channelType)s cData = (%(channelType)s)PyTuple_GetItem(data, %(channel)i);
int maxSeen = inoutLen / sizeof(float);
while (index)
{
int cls = %(channelName)s_get(cData, index->index, 0);
int cap = cls+1;
if (cap>maxSeen) maxSeen = cap;
if ((cap*sizeof(float))>inoutLen)
{
int zero_start = inoutLen / sizeof(float);
inoutLen = cap*sizeof(float);
inout = realloc(inout, inoutLen);
for (int i=zero_start; i<cap; i++)
{
((float*)inout)[i] = 0.0;
}
}
((float*)inout)[cls] += index->weight;
index = index->next;
}
}
"""%{'name':name, 'channel':self.channel, 'channelName':escl[self.channel]['name'], 'channelType':escl[self.channel]['itype']}
cEntropy = start_cpp() + """
float %(name)s_entropy(void * stats, size_t statsLen)
{
float sum = 0.0;
int length = statsLen>>2;
for (int i=0; i<length; i++)
{
sum += ((float*)stats)[i];
}
float ret = 0.0;
for (int i=0; i<length; i++)
{
float val = ((float*)stats)[i];
if (val>1e-6)
{
val /= sum;
ret -= val * log(val);
#.........这里部分代码省略.........
示例13: DAMAGES
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from utils.start_cpp import start_cpp
conc_code = start_cpp() + """
// This funky little function is used to resample the concentration parameter of a Dirichlet process, using the previous parameter - allows this parameter to be Gibbs sampled. Also works for any level of a HDP, due to the limited interactions.
// Parameters are:
// pcp - previous concentration parameter.
// n - number of samples taken from the Dirichlet process
// k - number of discretly different samples, i.e. table count in the Chinese restaurant process.
// prior_alpha - alpha value of the Gamma prior on the concentration parameter.
// prior_beta - beta value of the Gamma prior on the concentration parameter.
double sample_dirichlet_proc_conc(double pcp, double n, double k, double prior_alpha = 1.01, double prior_beta = 0.01)
{
if ((n<(1.0-1e-6))||(k<(2.0-1e-6)))
{
return pcp; // Doesn't work in this case, so just repeat.
}
示例14: test_compile
def test_compile(self):
code = start_cpp(linked_list_gc) + """
"""
weave.inline(code, support_code=linked_list_gc)
示例15: genCodeC
def genCodeC(self, name, exemplar_list):
code = ''
states = []
for i, gen in enumerate(self.gens):
c, s = gen.genCodeC(name+'_%i'%i, exemplar_list)
code += c
states.append(s)
code += start_cpp() + """
struct State%(name)s
{
void * test;
size_t length;
"""%{'name':name}
for i,s in enumerate(states):
code += ' %s gen_%i;\n'%(s,i)
code += start_cpp() + """
int upto;
};
void %(name)s_init(State%(name)s & state, PyObject * data, Exemplar * test_set)
{
state.test = 0;
state.length = 0;
"""%{'name':name}
for i in xrange(len(self.gens)):
code += '%(name)s_%(i)i_init(state.gen_%(i)i, data, test_set);\n'%{'name':name, 'i':i}
code += start_cpp() + """
state.upto = 0;
}
bool %(name)s_next(State%(name)s & state, PyObject * data, Exemplar * test_set)
{
switch (state.upto)
{
"""%{'name':name}
for i in xrange(len(self.gens)):
code += start_cpp() + """
case %(i)i:
if (%(name)s_%(i)i_next(state.gen_%(i)i, data, test_set))
{
state.length = 1 + state.gen_%(i)i.length;
state.test = realloc(state.test, state.length);
((unsigned char*)state.test)[0] = %(i)i;
memcpy((unsigned char*)state.test+1, state.gen_%(i)i.test, state.gen_%(i)i.length);
return true;
}
else state.upto += 1;
"""%{'name':name, 'i':i}
code += start_cpp() + """
}
free(state.test);
return false;
}
"""
return (code, 'State'+name)