예제 #1
0
def sample_set(items, weights, to_sample):
    if type(weights) != std.vector("double"):
        weights = std.vector("double")(weights)
    sampler = Sampler(weights, to_sample)

    while sampler.advanceToNextConfiguration():
        what = items[sampler.index()]
        for _ in range(sampler.count()):
            yield what
예제 #2
0
def BooleanMat(mat):
    out = std.vector(std.vector("bool"))()
    for row in mat:
        v = std.vector("bool")()
        for x in row:
            v.push_back(x)
        out.push_back(v)
    bmat_type = cppyy.gbl.libsemigroups.BMatHelper(len(mat)).type
    bmat_type.short_name = "BooleanMat"
    bmat_type.__pow__ = detail.generic_pow
    return bmat_type(out)
예제 #3
0
def BooleanMat(mat):
    if isinstance(mat, list) and all(isinstance(row, list) for row in mat):
        out = std.vector(std.vector("bool"))()
        for row in mat:
            v = std.vector("bool")()
            for x in row:
                assert isinstance(x, int)
                v.push_back(x)
            out.push_back(v)
    else:
        raise ValueError("mat must be a list of lists")

    out = cppyy.gbl.libsemigroups.BMat(len(mat)).type(out)
    return out
예제 #4
0
    def test01_builtin_vector_iterators(self):
        """Test iterator comparison with operator== reflected"""

        import cppyy
        from cppyy.gbl import std

        v = std.vector(int)()
        v.resize(1)

        b1, e1 = v.begin(), v.end()
        b2, e2 = v.begin(), v.end()

        assert b1 == b2
        assert not b1 != b2

        assert e1 == e2
        assert not e1 != e2

        assert not b1 == e1
        assert b1 != e1

        b1.__preinc__()
        assert not b1 == b2
        assert b1 == e2
        assert b1 != b2
        assert b1 == e2
예제 #5
0
    def test01_builtin_vector_iterators(self):
        """Test iterator comparison with operator== reflected"""

        import cppyy
        from cppyy.gbl import std

        v = std.vector(int)()
        v.resize(1)

        b1, e1 = v.begin(), v.end()
        b2, e2 = v.begin(), v.end()

        assert b1 == b2
        assert not b1 != b2

        assert e1 == e2
        assert not e1 != e2

        assert not b1 == e1
        assert b1 != e1

        b1.__preinc__()
        assert not b1 == b2
        assert b1 == e2
        assert b1 != b2
        assert b1 == e2
예제 #6
0
    def test01_write_stdvector(self):
        """Test writing of a single branched TTree with an std::vector<double>"""

        from cppyy import gbl  # bootstraps, only needed for tests
        from cppyy.gbl import TFile, TTree
        from cppyy.gbl.std import vector

        f = TFile(self.fname, "RECREATE")
        mytree = TTree(self.tname, self.title)
        mytree._python_owns = False

        v = vector("double")()
        raises(TypeError, TTree.Branch, None, "mydata", v.__class__.__name__,
               v)
        raises(TypeError, TTree.Branch, v, "mydata", v.__class__.__name__, v)

        mytree.Branch("mydata", v.__class__.__name__, v)

        for i in range(self.N):
            for j in range(self.M):
                v.push_back(i * self.M + j)
            mytree.Fill()
            v.clear()
        f.Write()
        f.Close()
예제 #7
0
    def test01_builtin_vector_iterators(self):
        """Test iterator comparison for vector"""

        from cppyy.gbl import std

        v = std.vector(int)()
        v.resize(1)

        self.__run_tests(v)
예제 #8
0
    def test01_builtin_vector_iterators(self):
        """Test iterator comparison for vector"""

        from cppyy.gbl import std

        v = std.vector(int)()
        v.resize(1)

        self.__run_tests(v)
예제 #9
0
    def test04_empty_vector(self):
        """Test behavior of empty vector<int> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector(int)()
        assert len(a) == 0
        for arg in a:
            pass
예제 #10
0
    def test04_empty_vector(self):
        """Test behavior of empty vector<int> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector(int)()
        assert len(a) == 0
        for arg in a:
            pass
예제 #11
0
    def test06_branch_activation(self):
        """Test of automatic branch activation"""

        from cppyy import gbl
        from cppyy.gbl import TFile, TTree
        from cppyy.gbl.std import vector

        L = 5

        # writing
        f = TFile(self.fname, "RECREATE")
        mytree = TTree(self.tname, self.title)
        mytree._python_owns = False

        for i in range(L):
            v = vector("double")()
            mytree.Branch("mydata_%d" % i, v.__class__.__name__, v)
            mytree.__dict__["v_%d" % i] = v

        for i in range(self.N):
            for k in range(L):
                v = mytree.__dict__["v_%d" % k]
                for j in range(self.M):
                    mytree.__dict__["v_%d" % k].push_back(i * self.M + j * L +
                                                          k)
            mytree.Fill()
            for k in range(L):
                v = mytree.__dict__["v_%d" % k]
                v.clear()
        f.Write()
        f.Close()

        del mytree, f
        import gc
        gc.collect()

        # reading
        f = TFile(self.fname)
        mytree = f.Get(self.tname)

        # force (initial) disabling of all branches
        mytree.SetBranchStatus("*", 0)

        i = 0
        for event in mytree:
            for k in range(L):
                j = 0
                data = getattr(mytree, "mydata_%d" % k)
                assert len(data) == self.M
                for entry in data:
                    assert entry == i * self.M + j * L + k
                    j += 1
                assert j == self.M
            i += 1
        assert i == self.N

        f.Close()
예제 #12
0
    def test04_builtin_vector_iterators_bool(self):
        """Test iterator comparison for vector of bool"""

        from cppyy.gbl import std

        v = std.vector(bool)()
        v.resize(1)

        self.__run_tests(v)
예제 #13
0
파일: test_cint.py 프로젝트: Darriall/pypy
    def test06_branch_activation(self):
        """Test of automatic branch activation"""

        from cppyy import gbl
        from cppyy.gbl import TFile, TTree
        from cppyy.gbl.std import vector

        L = 5

        # writing
        f = TFile(self.fname, "RECREATE")
        mytree = TTree(self.tname, self.title)
        mytree._python_owns = False

        for i in range(L):
            v = vector("double")()
            mytree.Branch("mydata_%d"%i, v.__class__.__name__, v)
            mytree.__dict__["v_%d"%i] = v

        for i in range(self.N):
            for k in range(L):
                v = mytree.__dict__["v_%d"%k]
                for j in range(self.M):
                    mytree.__dict__["v_%d"%k].push_back(i*self.M+j*L+k)
            mytree.Fill()
            for k in range(L):
                v = mytree.__dict__["v_%d"%k]
                v.clear()
        f.Write()
        f.Close()

        del mytree, f
        import gc
        gc.collect()

        # reading
        f = TFile(self.fname)
        mytree = f.Get(self.tname)

        # force (initial) disabling of all branches
        mytree.SetBranchStatus("*",0);

        i = 0
        for event in mytree:
            for k in range(L):
                j = 0
                data = getattr(mytree, "mydata_%d"%k)
                assert len(data) == self.M
                for entry in data:
                    assert entry == i*self.M+j*L+k
                    j += 1
                assert j == self.M
            i += 1
        assert i == self.N

        f.Close()
예제 #14
0
def sample_with_replacement(weights, to_sample):
    '''Weighted sampling with replacement

    Given an interable (preferably cppyy's std.vector<double>, others will be converted)
    samples to_sample indexes, according to the weights. Weights are proportional, 
    and need not sum to 1.

    Returns list of tuples: (index, number if times selected)

    For example:

    sample_with_replacement([0.0001, 0.9, 0.1, 0.000001], 100) might produce:
    [(1, 91), (2, 9)]
    '''

    if type(weights) != std.vector("double"):
        weights = std.vector("double")(weights)
    sampler = Sampler(weights, to_sample)
    while sampler.advanceToNextConfiguration():
        yield (sampler.index(), sampler.count())
예제 #15
0
    def test02_builtin_vector_type(self):
        """Test access to a vector<double> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector('double')()
        for i in range(self.N):
            a.push_back(i)
            assert a.size() == i + 1
            assert a[i] == i
            assert a.at(i) == i

        assert a.size() == self.N
        assert len(a) == self.N
예제 #16
0
    def test06_vector_return_downcasting(self):
        """Pointer returns of vector indexing must be down cast"""

        from cppyy.gbl import std, PR_Test

        v = PR_Test.mkVect()
        assert type(v) == std.vector('PR_Test::Base*')
        assert len(v) == 1
        assert type(v[0]) == PR_Test.Derived
        assert PR_Test.checkType(v[0]) == PR_Test.checkType(PR_Test.Derived())

        p = PR_Test.check()
        assert type(p) == PR_Test.Derived
        assert PR_Test.checkType(p) == PR_Test.checkType(PR_Test.Derived())
예제 #17
0
    def test04_string_hash(self):
        """Test that std::string has the same hash as the equivalent Python str"""
        # ROOT-10830

        from cppyy.gbl import std

        assert hash(std.string("test")) == hash("test")

        # Somehow redundant, but for completeness
        v = std.vector(std.string)()
        v.push_back('a')
        v.push_back('b')
        v.push_back('c')
        assert set(v) == set('abc')
예제 #18
0
    def test02_builtin_vector_type(self):
        """Test access to a vector<double> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector('double')()
        for i in range(self.N):
            a.push_back( i )
            assert a.size() == i+1
            assert a[i] == i
            assert a.at(i) == i

        assert a.size() == self.N
        assert len(a) == self.N
예제 #19
0
    def test06_vector_return_downcasting(self):
        """Pointer returns of vector indexing must be down cast"""

        from cppyy.gbl import std, PR_Test

        v = PR_Test.mkVect()
        assert type(v) == std.vector('PR_Test::Base*')
        assert len(v) == 1
        assert type(v[0]) == PR_Test.Derived
        assert PR_Test.checkType(v[0]) == PR_Test.checkType(PR_Test.Derived())

        p = PR_Test.check()
        assert type(p) == PR_Test.Derived
        assert PR_Test.checkType(p) == PR_Test.checkType(PR_Test.Derived())
예제 #20
0
    def test01_builtin_vector_type(self):
        """Test access to a vector<int> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector(int)(self.N)
        assert len(a) == self.N

        for i in range(self.N):
            a[i] = i
            assert a[i] == i
            assert a.at(i) == i

        assert a.size() == self.N
        assert len(a) == self.N
예제 #21
0
    def test01_builtin_vector_type(self):
        """Test access to a vector<int> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector(int)(self.N)
        assert len(a) == self.N

        for i in range(self.N):
            a[i] = i
            assert a[i] == i
            assert a.at(i) == i

        assert a.size() == self.N
        assert len(a) == self.N
예제 #22
0
    def test04_empty_vector(self):
        """Test behavior of empty vector<int> (part of cintdlls)"""

        from cppyy.gbl import std

        a = std.vector(int)()
        assert len(a) == 0
        for arg in a:
            pass

        if self.exp_pyroot:
            # ROOT-10118
            # In current Cppyy, STL containers evaluate to True
            # if they contain at least one element
            assert not a
            a.push_back(0)
            assert a
예제 #23
0
    def test03_generated_vector_type(self):
        """Test access to a ACLiC generated vector type"""

        from cppyy.gbl import std, JustAClass

        a = std.vector(JustAClass)()
        assert hasattr(a, 'size')
        assert hasattr(a, 'push_back')
        assert hasattr(a, '__getitem__')
        assert hasattr(a, 'begin')
        assert hasattr(a, 'end')

        assert a.size() == 0

        for i in range(self.N):
            a.push_back(JustAClass())
            a[i].m_i = i
            assert a[i].m_i == i

        assert len(a) == self.N
예제 #24
0
    def test03_generated_vector_type(self):
        """Test access to a ACLiC generated vector type"""

        from cppyy.gbl import std, JustAClass

        a = std.vector( JustAClass )()
        assert hasattr(a, 'size')
        assert hasattr(a, 'push_back')
        assert hasattr(a, '__getitem__')
        assert hasattr(a, 'begin')
        assert hasattr(a, 'end')

        assert a.size() == 0

        for i in range(self.N):
            a.push_back( JustAClass() )
            a[i].m_i = i
            assert a[i].m_i == i

        assert len(a) == self.N
예제 #25
0
    def test05_pushback_iterables_with_iadd(self):
        """Test usage of += of iterable on push_back-able container"""

        from cppyy.gbl import std

        a = std.vector(int)()
        a += [1, 2, 3]
        assert len(a) == 3

        assert a[0] == 1
        assert a[1] == 2
        assert a[2] == 3

        a += ( 4, 5, 6 )
        assert len(a) == 6

        assert a[3] == 4
        assert a[4] == 5
        assert a[5] == 6

        raises(TypeError, a.__iadd__, (7, '8'))
예제 #26
0
    def test05_pushback_iterables_with_iadd(self):
        """Test usage of += of iterable on push_back-able container"""

        from cppyy.gbl import std

        a = std.vector(int)()
        a += [1, 2, 3]
        assert len(a) == 3

        assert a[0] == 1
        assert a[1] == 2
        assert a[2] == 3

        a += (4, 5, 6)
        assert len(a) == 6

        assert a[3] == 4
        assert a[4] == 5
        assert a[5] == 6

        raises(TypeError, a.__iadd__, (7, '8'))
예제 #27
0
파일: test_cint.py 프로젝트: Darriall/pypy
    def test01_write_stdvector(self):
        """Test writing of a single branched TTree with an std::vector<double>"""

        from cppyy import gbl               # bootstraps, only needed for tests
        from cppyy.gbl import TFile, TTree
        from cppyy.gbl.std import vector

        f = TFile(self.fname, "RECREATE")
        mytree = TTree(self.tname, self.title)
        mytree._python_owns = False

        v = vector("double")()
        raises(TypeError, TTree.Branch, None, "mydata", v.__class__.__name__, v)
        raises(TypeError, TTree.Branch, v, "mydata", v.__class__.__name__, v)

        mytree.Branch("mydata", v.__class__.__name__, v)

        for i in range(self.N):
            for j in range(self.M):
                v.push_back(i*self.M+j)
            mytree.Fill()
            v.clear()
        f.Write()
        f.Close()
예제 #28
0
def signature(f):
    try:
        temp=inspect.signature(f).parameters
        return temp
    except:
        s=f.__doc__
        mappings={  "int":int,
                    "long":int,
                    "float":float,
                    "double":float,
                    "bool":bool,
                    "std::string":str,
                    "char":str,
                    "const int&":int,
                    "const long&":int,
                    "const float&":float,
                    "const double&":float,
                    "const bool&":bool,
                    "const std::string&":str,
                    "const char&":str,
                    "std::vector<int>":List[int],
                    "std::vector<long>":List[int],
                    "std::vector<float>":List[float],
                    "std::vector<double>":List[float],
                    "std::vector<bool>":List[bool],
                    "std::vector<std::string>":List[str],
                    "std::vector<char>":List[str],
                    "const std::vector<int>&":List[int],
                    "const std::vector<long>&":List[int],
                    "const std::vector<float>&":List[float],
                    "const std::vector<double>&":List[float],
                    "const std::vector<bool>&":List[bool],
                    "const std::vector<std::string>&":List[str],
                    "const std::vector<char>&":List[str],
                    "std::vector<std::vector<std::string>>":vector(vector(str)),
                    "std::vector<std::vector<int>>":vector(vector(int)),
                    "std::vector<std::vector<char>>":vector(vector(str)),
                    "std::vector<std::vector<std::string> >":vector(vector(str)),
                    "std::vector<std::vector<char> >":vector(vector(str)),
                    "std::vector<std::vector<int> >":vector(vector(int)),
                    "const std::vector<std::vector<std::string>>&":vector(vector(str)),
                    "const std::vector<std::vector<int>>&":vector(vector(int)),
                    "const std::vector<std::vector<char>>&":vector(vector(str)),
                    "const std::vector<std::vector<std::string> >&":vector(vector(str)),
                    "const std::vector<std::vector<char> >&":vector(vector(str)),
                    "const std::vector<std::vector<int> >&":vector(vector(int)),
                    "std::list<int>":List[int],
                    "std::list<long>":List[int],
                    "std::list<float>":List[float],
                    "std::list<double>":List[float],
                    "std::list<bool>":List[bool],  
                    "std::list<std::string>":List[str], 
                    "std::list<char>":List[str], 
                    "const std::list<int>&":List[int],
                    "const std::list<long>&":List[int],
                    "const std::list<float>&":List[float],
                    "const std::list<double>&":List[float],
                    "const std::list<bool>&":List[bool],  
                    "const std::list<std::string>&":List[str], 
                    "const std::list<char>&":List[str],                         
                    "std::set<int>":Set[int],
                    "std::set<long>":Set[int],
                    "std::set<float>":Set[float],
                    "std::set<double>":Set[float],
                    "std::set<bool>":Set[bool], 
                    "std::set<std::string>":Set[str],  
                    "std::set<char>":Set[str], 
                    "const std::set<int>&":Set[int],
                    "const std::set<long>&":Set[int],
                    "const std::set<float>&":Set[float],
                    "const std::set<double>&":Set[float],
                    "const std::set<bool>&":Set[bool], 
                    "const std::set<std::string>&":Set[str],  
                    "const std::set<char>&":Set[str],                       
        }
        s=s[s.find("(")+1:-1].split(",")
        parameters={}
        for i in s:
            temp=i.strip().split()
            if temp[0]=="const":
                parameters[temp[2]]=parameter(mappings[temp[0]+" "+temp[1]],{})
            else:
                parameters[temp[1]]=parameter(mappings[temp[0]],{})
        return parameters
예제 #29
0
def param_generator(t,config={}):
    
    if config==inspect._empty:
        config={}
    def get_random_alphanumeric_string(c):
        upper_count=c["upper_count"]
        digits_count=c["digits_count"]
        lower_count=c["lower_count"]
        special_count=c["special_count"]
        wspace_count=c["wspace_count"]
        sample_str = ''.join((random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ") for i in range(upper_count)))
        sample_str += ''.join((random.choice("abcdefghijklmnopqrstuvwxyz") for i in range(lower_count)))
        sample_str += ''.join((random.choice("0123456789") for i in range(digits_count)))
        sample_str += ''.join((random.choice("*&^%$#@!~`'\",\\|[];:.><?/\t\b\v") for i in range(special_count)))
        sample_str += ''.join((random.choice(" ") for i in range(wspace_count)))
        sample_list = list(sample_str)
        random.shuffle(sample_list)
        final_string = ''.join(sample_list)
        return final_string
    
    def build_missing_config(c):
        if "start" not in c:
            c["start"]=default_config["start"]
        if "end" not in c:
            c["end"]=default_config["end"]
        if "len_list" not in c:
            c["len_list"]=default_config["len_list"]
        if "upper_count" not in c:
            c["upper_count"]=default_config["upper_count"]
        if "lower_count" not in c:
            c["lower_count"]=default_config["lower_count"]
        if "digits_count" not in c:
            c["digits_count"]=default_config["digits_count"]
        if "special_count" not in c:
            c["special_count"]=default_config["special_count"]
        if "wspace_count" not in c:
            c["wspace_count"]=default_config["wspace_count"]
        
    if "generator" in config:
        try:
            temp=config["generator"]()
            return config["generator"]
        except:
            d={}
            exec(config["generator"],globals(),d)
            f=list(d.values())[0]
            return f
    
    build_missing_config(config)
    
    mappings={
        int:lambda :random.randint(config["start"],config["end"]),
        float:lambda : random.randint(config["start"],config["end"])+random.random(),
        bool:lambda : bool(random.randint(0,1)),
        list: lambda : [random.randint(config["start"],config["end"]) for i in range(config["len_list"])],
        tuple: lambda : tuple([random.randint(config["start"],config["end"]) for i in range(config["len_list"])]),
        set: lambda : {random.randint(config["start"],config["end"]) for i in range(config["len_list"])},
        str: lambda : get_random_alphanumeric_string(config),
        List[int]: lambda :[random.randint(config["start"],config["end"]) for i in range(config["len_list"])],
        Tuple[int]: lambda : tuple([random.randint(config["start"],config["end"]) for i in range(config["len_list"])]),
        Set[int]: lambda : {random.randint(config["start"],config["end"]) for i in range(config["len_list"])},
        List[float]: lambda :[random.randint(config["start"],config["end"])+random.random() for i in range(config["len_list"])],
        Tuple[float]: lambda :tuple([random.randint(config["start"],config["end"])+random.random() for i in range(config["len_list"])]),
        Set[float]: lambda :set([random.randint(config["start"],config["end"])+random.random() for i in range(config["len_list"])]),
        List[str]: lambda :[get_random_alphanumeric_string(config) for i in range(config["len_list"])],
        Tuple[str]: lambda :tuple([get_random_alphanumeric_string(config) for i in range(config["len_list"])]),
        Set[str]: lambda :set([get_random_alphanumeric_string(config) for i in range(config["len_list"])]),
        List[List[int]]: lambda : [[random.randint(0,1) for i in range(config["len_list"])] for j in range(config["len_list"])],
        List[List[str]]: lambda : [[str(random.randint(0,1)) for i in range(config["len_list"])] for j in range(config["len_list"])],
        vector(vector(int)):lambda : [[random.randint(0,1) for i in range(config["len_list"])] for j in range(config["len_list"])],
        vector(vector(int)):lambda : [[random.randint(0,1) for i in range(config["len_list"])] for j in range(config["len_list"])],
        inspect.Parameter.empty:lambda : random.randint(config["start"],config["end"])+random.random()
    }
    
    return mappings[t]
예제 #30
0
def vi(L):
    if type(L) != std.vector("size_t"):
        return std.vector("size_t")(L)
    else:
        return L
예제 #31
0
def vd(L):
    if type(L) != std.vector("double"):
        return std.vector("double")(L)
    else:
        return L
예제 #32
0
def AliasSampler(weights):
    if type(weights) != std.vector("double"):
        weights = std.vector("double")(weights)
    return AliasSamplerCpp(weights)
예제 #33
0
def sample_with_replacement_shuffled(weights, to_sample):
    if type(weights) != std.vector("double"):
        weights = std.vector("double")(weights)

    return ShuffledSample(weights, to_sample)