本文整理汇总了Python中utils.start_cpp.start_cpp函数的典型用法代码示例。如果您正苦于以下问题:Python start_cpp函数的具体用法?Python start_cpp怎么用?Python start_cpp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了start_cpp函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:zerocolar,项目名称:Project_Code,代码行数:31,代码来源:linked_list_cpp.py
示例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'])
开发者ID:zerocolar,项目名称:Project_Code,代码行数:29,代码来源:opticalflow_lk.py
示例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)
开发者ID:zerocolar,项目名称:Project_Code,代码行数:25,代码来源:linked_list_cpp.py
示例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
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:12,代码来源:tests.py
示例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)
开发者ID:jizhihang,项目名称:helit,代码行数:48,代码来源:nodes.py
示例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)
开发者ID:zerocolar,项目名称:Project_Code,代码行数:16,代码来源:linked_list_cpp.py
示例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)
开发者ID:jizhihang,项目名称:helit,代码行数:47,代码来源:nodes.py
示例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
开发者ID:zerocolar,项目名称:Project_Code,代码行数:43,代码来源:stats_cd.py
示例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
开发者ID:zerocolar,项目名称:Project_Code,代码行数:22,代码来源:generators.py
示例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
开发者ID:zerocolar,项目名称:Project_Code,代码行数:23,代码来源:smp.py
示例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))
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:76,代码来源:goals.py
示例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);
#.........这里部分代码省略.........
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:101,代码来源:goals.py
示例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.
}
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:30,代码来源:conc_cpp.py
示例14: test_compile
def test_compile(self):
code = start_cpp(linked_list_gc) + """
"""
weave.inline(code, support_code=linked_list_gc)
开发者ID:zerocolar,项目名称:Project_Code,代码行数:4,代码来源:linked_list_cpp.py
示例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)
开发者ID:zerocolar,项目名称:Project_Code,代码行数:67,代码来源:generators.py
示例16: addTrainC
def addTrainC(goal, gen, es, esclName = 'es'):
"""Provides C code that the addTrain method can use to accelerate itself - standard rules about code being unique for each combination of input types applies."""
# First do accessors for the data set...
try:
escl = es.listCodeC(esclName)
except NotImplementedError: return None
code = ''
for channel in escl:
code += channel['get'] + '\n'
code += channel['exemplars'] + '\n'
code += channel['features'] + '\n'
# Throw in the test code...
try:
code += gen.testCodeC('do_test', escl) + '\n'
except NotImplementedError: return None
# Definition of Exemplar...
code += start_cpp() + """
// So we can use an inplace modified linkied list to avoid malloc's during the real work...
struct Exemplar
{
int index;
float weight;
Exemplar * next;
};
"""
# Add the needed goal code...
try:
gDic = goal.codeC('goal', escl)
except NotImplementedError:
return None
try:
code += gDic['updateStats']
except KeyError:
return None
code += start_cpp() + """
void addTrain(PyObject * node, PyObject * data, Exemplar * test_set)
{
// Update the stats at this node...
PyObject * stats = PyObject_GetAttrString(node, "stats");
size_t stLen = PyString_Size(stats);
void * st = malloc(stLen);
memcpy(st, PyString_AsString(stats), stLen);
goal_updateStats(data, test_set, st, stLen);
PyObject * t = PyString_FromStringAndSize((char*)st, stLen);
PyObject_SetAttrString(node, "stats", t);
Py_DECREF(t);
free(st);
Py_DECREF(stats);
// If its not a leaf recurse down and do its children also...
PyObject * test = PyObject_GetAttrString(node, "test");
if (test!=Py_None)
{
// Tests...
Exemplar * pass = 0;
Exemplar * fail = 0;
void * test_ptr = PyString_AsString(test);
size_t test_len = PyString_Size(test);
while (test_set)
{
Exemplar * next = test_set->next;
if (do_test(data, test_ptr, test_len, test_set->index))
{
test_set->next = pass;
pass = test_set;
}
else
{
test_set->next = fail;
fail = test_set;
}
test_set = next;
}
// Recurse...
if (pass!=0)
{
PyObject * child = PyObject_GetAttrString(node, "true");
addTrain(child, data, pass);
Py_DECREF(child);
}
if (fail!=0)
{
PyObject * child = PyObject_GetAttrString(node, "false");
#.........这里部分代码省略.........
开发者ID:zerocolar,项目名称:Project_Code,代码行数:101,代码来源:nodes.py
示例17: test_compile
def test_compile(self):
code = start_cpp(shared_code) + """
"""
weave.inline(code, support_code=shared_code)
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:4,代码来源:solve_weave.py
示例18: DAMAGES
# 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 scipy import weave
import unittest
from utils.start_cpp import start_cpp
# Defines code for a doubly linked list - simple but works as expected... (Includes its data via templated inheritance - a little strange, but neat and saves on memory thrashing.)
linked_list_code = start_cpp() + """
// Predefinitions...
template <typename ITEM, typename BODY> class Item;
template <typename ITEM, typename BODY> class List;
// Useful default...
struct Empty {};
// Item for the linked list data structure - simply inherits extra data stuff...
template <typename ITEM = Empty, typename BODY = Empty>
class Item : public ITEM
{
public:
Item(List<ITEM,BODY> * head):head(head),next(this),prev(this) {}
开发者ID:zerocolar,项目名称:Project_Code,代码行数:31,代码来源:linked_list_cpp.py
示例19: give_birth
def give_birth(self, goal, gen, pruner, es, index = slice(None), weights = None, depth = 0, entropy = None, code = None):
"""This recursivly grows the tree until the pruner says to stop. goal is a Goal object, so it knows what to optimise, gen a Generator object that provides tests for it to choose between and pruner is a Pruner object that decides when to stop growing. The exemplar set to train on is then provided, optionally with the indices of which members to use and weights to assign to them (weights align with the exemplar set, not with the relative exemplar indices defined by index. depth is the depth of this node - part of the recursive construction and used by the pruner as a possible reason to stop growing. entropy should match up with self.stats. The static method initC can be called to generate code that can be used to accelerate test selection, but only if it is passed in."""
if entropy==None: entropy = goal.entropy(self.stats)
# Select the best test...
if isinstance(code, str) and weave!=None:
# Do things in C...
init = start_cpp(code) + """
if (Nindex[0]!=0)
{
srand48(rand);
// Create the Exemplar data structure, in triplicate!..
Exemplar * items = (Exemplar*)malloc(sizeof(Exemplar)*Nindex[0]);
Exemplar * splitItems = (Exemplar*)malloc(sizeof(Exemplar)*Nindex[0]);
Exemplar * temp = (Exemplar*)malloc(sizeof(Exemplar)*Nindex[0]);
for (int i=0; i<Nindex[0]; i++)
{
int ind = index[i];
float we = weights[ind];
items[i].index = ind;
items[i].weight = we;
items[i].next = &items[i+1];
splitItems[i].index = ind;
splitItems[i].weight = we;
splitItems[i].next = &splitItems[i+1];
temp[i].next = &temp[i+1];
}
items[Nindex[0]-1].next = 0;
splitItems[Nindex[0]-1].next = 0;
temp[Nindex[0]-1].next = 0;
// Do the work...
selectTest(out, data, items, splitItems, temp, entropy);
// Clean up...
free(temp);
free(splitItems);
free(items);
}
"""
data = es.tupleInputC()
out = dict()
rand = numpy.random.randint(-1000000000,1000000000)
if weights==None: weights = numpy.ones(es.exemplars(), dtype=numpy.float32)
weave.inline(init, ['out', 'data', 'index', 'weights', 'entropy', 'rand'], support_code=code)
if index.shape[0]==0: return
bestTest = out['bestTest']
if bestTest!=None:
bestInfoGain = out['bestInfoGain']
trueStats = out['trueStats']
trueEntropy = out['trueEntropy']
trueIndex = out['trueIndex']
falseStats = out['falseStats']
falseEntropy = out['falseEntropy']
falseIndex = out['falseIndex']
trueIndex.sort() # Not needed to work - to improve cache coherance.
falseIndex.sort() # "
else:
if index.shape[0]==0: return
# Do things in python...
## Details of best test found so far...
bestInfoGain = -1.0
bestTest = None
trueStats = None
trueEntropy = None
trueIndex = None
falseStats = None
falseEntropy = None
falseIndex = None
## Get a bunch of tests and evaluate them against the goal...
for test in gen.itertests(es, index, weights):
# Apply the test, work out which items pass and which fail..
res = gen.do(test, es, index)
tIndex = index[res==True]
fIndex = index[res==False]
# Check its safe to continue...
if tIndex.shape[0]==0 or fIndex.shape[0]==0: continue
# Calculate the statistics...
tStats = goal.stats(es, tIndex, weights)
fStats = goal.stats(es, fIndex, weights)
# Calculate the information gain...
tEntropy = goal.entropy(tStats)
fEntropy = goal.entropy(fStats)
if weights==None:
tWeight = float(tIndex.shape[0])
fWeight = float(fIndex.shape[0])
#.........这里部分代码省略.........
开发者ID:zerocolar,项目名称:Project_Code,代码行数:101,代码来源:nodes.py
示例20: start_cpp
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from utils.start_cpp import start_cpp
from utils.numpy_help_cpp import numpy_util_code
from dp_utils.sampling_cpp import sampling_code
smp_code = numpy_util_code + sampling_code + start_cpp() + """
#ifndef SMP_CODE
#define SMP_CODE
class SMP
{
public:
// Basic constructor - after construction before anything else the Init method must be called...
SMP()
:fia(0),priorMN(0),sam(0),samPos(0),samTemp(0),power(0),temp(0)
{}
// Constructor that calls the init method...
SMP(int flagSize, int fliSize)
:fia(0),priorMN(0),sam(0),samPos(0),samTemp(0),power(0),temp(0)
{
开发者ID:PeterZhouSZ,项目名称:helit,代码行数:31,代码来源:smp_cpp.py
注:本文中的utils.start_cpp.start_cpp函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论