def test_lattice(datafiles): for lattice in datafiles.listdir(): lattice_dict = cex_to_list(lattice) Objects = bitset('Objects', lattice_dict['context']['objects']) Attributes = bitset( 'Attributes', lattice_dict['context']['attributes']) print(Objects.supremum.members()) print(Attributes.supremum.members()) context = Context( lattice_dict['context']['table'], Objects, Attributes) expected_concepts = [] for intent in lattice_dict['concepts']: intent = Attributes.frombools(intent) extent = context.down(intent) expected_concepts.append(Concept(extent, intent)) result = Lattice(context) assert len(expected_concepts) == len(result.get_concepts()) assert set(expected_concepts) == set(result.get_concepts())
def load_sparse_csv(filename, filename_attributes, delimiter=','): """ Loads .csv in format from dataset and universum: object1, attribute1, attribute10, attribute 20 object2, attribute2, attribute30 """ attributes = __load_items_from_file(filename_attributes) attributes_hash = { attribute: index for index, attribute in enumerate(attributes) } with open(filename, newline='') as csvfile: reader = csv.reader(csvfile, delimiter=delimiter) bools = [] objects = [] for row in reader: bool_row = [0] * len(attributes) for attr in row[1:]: bool_row[attributes_hash[attr]] = 1 bools.append(bool_row) objects.append(row[0]) Objects = bitset('Objects', tuple(objects)) Attributes = bitset('Attributes', tuple(attributes)) return Objects, Attributes, bools
def test_concept_id(): Objects = bitset('Objects', ('a', 'b')) Attributes = bitset('Attributes', ('1', '2', '3')) concept = Concept(Objects(['a']), Attributes(['3'])) assert concept.get_id() == 4
def test_lattice_creation(): bools = ((0, 1), (1, 1)) Objects = bitset('Objects', ('a', 'b')) Attributes = bitset('Attributes', ('1', '2')) context = Context(bools, Objects, Attributes) lattice = Lattice(context) assert len(lattice.get_concepts()) == 2
def test_cohesion_min(similarity_function): bools = ((0, 1), (1, 1)) Objects = bitset('Objects', ('a', 'b')) Attributes = bitset('Attributes', ('1', '2')) context = Context(bools, Objects, Attributes) concept = Concept(Objects(['a', 'b']), Attributes(['2'])) rows = context.filter_rows_by_extent(concept.extent) expected_coh = similarity_function(rows[0], rows[1]) assert cohesion_min(concept, context, similarity_function) == expected_coh
def build_graph_from_activated_layers(feature, tol, input_graph=AG): X = feature.reshape((-1, input_graph.number_of_nodes())) G = nx.Graph() nb_layers = X.shape[0] # Create the domain (input_ids) range ids = tuple(np.arange(1, X.shape[1] + 1)) input_ids = bitset('Graph', ids) # Create edges and nodes if needed for i in xrange(nb_layers - 1): current_nodes = input_ids.frombools(X[i] > tol).members() current_nodes += input_ids.frombools(X[i] < -tol).members() next_nodes = input_ids.frombools(X[i + 1] > tol).members() next_nodes += input_ids.frombools(X[i + 1] < -tol).members() for c in current_nodes: src = c + (i * len(ids)) for n in next_nodes: tgt = n + ((i + 1) * len(ids)) # Check that input_id are real adajcent # in the global adjacency matrix # beware, the global adj matrix starts at 0 if input_graph.has_edge(c - 1, n - 1) or c == n: if src not in G: data = create_node_data(i, c, X[i, c - 1]) G.add_node(src, data) if tgt not in G: data = create_node_data(i + 1, n, X[i + 1, n - 1]) G.add_node(tgt, data) G.add_edge(src, tgt) return G
def pointsto_map(self): """pointsto_map maps variables to set of heap-objects. Set of heap-objects are backed by a bitset mapping for space efficiency""" # initialize the HeapObjs to a bitvector and pointsto_map heap_ctx_pairs = set(self.all_heap_ctx_pair()) HeapObjs = bitset('HeapObjs', tuple(heap_ctx_pairs)) pointsto_map = defaultdict(lambda: HeapObjs.infimum) HeapVarRow = namedtuple('HeapVarRow', ['var', 'heap']) conn = sqlite3.connect(DATABASE_PATH) cursor = conn.cursor() try: start = time.time() query = f'select varCtx, var, heapCtx, heapObj from {self.db} order by var asc, varCtx asc' # res = set(map(HeapVarRow._make, cursor.execute(query).fetchall())) print("\t\tCreating points-to map") count = 0 for row in cursor.execute(query): hvrow = HeapVarRow((row[0], row[1]), (row[2], row[3])) var = hvrow.var heap_objs = hvrow.heap pointsto_map[var] = pointsto_map[var].union( HeapObjs([heap_objs])) count += 1 print( f"\t\tCreated {count} points-to map entries in {time.time() - start} seconds" ) return pointsto_map except Error as e: print("VarPointsToTable:select_all_heap_variables", e) print(query)
def create_node_signal(signal, baseid_name, layer_name, verbose=True): """Create signal on the node from pandas DataFrame or Graphlab SFrame""" def layers_to_long_str(x): """Convert layer number into bitstring then long str and add them as attributes on the nodes""" return str(layer_set(tuple(x['layers']))) start = time.time() if verbose: LOGGER.info('Create node signal') nb_layers = signal[layer_name].max() + 1 # Create the bitspace layer_set = bitset('Layers', tuple(range(nb_layers))) # Aggregate layers per node node_signal = signal.groupby(baseid_name, {'layers': gl.aggregate.CONCAT(layer_name)}) layer_bitstring = node_signal.apply(layers_to_long_str) # Remove old layers column and replace it with the bitstring one node_signal = node_signal.remove_column('layers').add_column( layer_bitstring, 'layers') if verbose: LOGGER.info('Create node signal done in: %s seconds', time.time() - start) return node_signal
def test_cohesion_avg_2(similarity_function): bools = ((0, 1), (1, 1), (0, 1)) Objects = bitset('Objects', ('a', 'b', 'c')) Attributes = bitset('Attributes', ('1', '2')) context = Context(bools, Objects, Attributes) concept = Concept(Objects(['a', 'b', 'c']), Attributes(['2'])) rows = context.filter_rows_by_extent(concept.extent) suma = similarity_function(rows[0], rows[1]) + \ similarity_function(rows[1], rows[2]) + \ similarity_function(rows[0], rows[2]) expected_coh = suma / (len(concept.extent) * (len(concept.extent) - 1) / 2) assert cohesion_avg(concept, context, similarity_function) == expected_coh
def __new__(cls, xname, yname, xmembers, ymembers, xbools, _ids=None): if _ids is not None: # unpickle reconstruction xid, yid = _ids X = bitsets.meta.bitset(xname, xmembers, xid, Vector, None, Vectors) Y = bitsets.meta.bitset(yname, ymembers, yid, Vector, None, Vectors) else: X = bitsets.bitset(xname, xmembers, Vector, tuple=Vectors) Y = bitsets.bitset(yname, ymembers, Vector, tuple=Vectors) x = X.Tuple.frombools(xbools) y = Y.Tuple.frombools(zip(*x.bools())) self = super(Relation, cls).__new__(cls, (x, y)) x._pair_with(self, 0, y) y._pair_with(self, 1, x) return self
def test_typicality_avg_1(similarity_function): Attributes = bitset('Attributes', range(4)) objects = [ Attributes.frommembers([0, 1, 2, 3]), ] similarities = (similarity_function(objects[0], objects[0]), ) expected = sum(similarities) / len(objects) assert typicality_avg(objects[0], objects, similarity_function) == expected
def construct_bitset(self, path): paths = [] path = os.path.abspath(path) for root, dirs, files in os.walk(path, followlinks=True): for filename in files: filepath = os.path.join(root, filename) if self.should_index(filepath): p = os.path.relpath(os.path.join(root, filename), path) paths.append(intern(str(p))) Documents = bitsets.bitset("Documents", tuple(paths)) self.Documents = Documents return Documents
def test_typicality_rosch_1(): Attributes = bitset('Attributes', range(4)) objects = [ Attributes.frommembers([0, 1, 2, 3]), ] weights = [1, 1, 1, 1] assert typicality_rosch(objects[0], objects) == sum(weights) assert typicality_rosch_ln(objects[0], objects) == sum([math.log(x) for x in weights])
def test_load_sparse_csv(tmp_path): attributes = "a\nb\nc\n" dataset = "1,b,c\n2,a,c\n3,a" filename_attributes = tmp_path / "attributes" filename_dataset = tmp_path / "dataset.csv" filename_attributes.write_text(attributes) filename_dataset.write_text(dataset) Objects_target = bitset('Objects', ('1', '2', '3')) Attributes_target = bitset('Attributes', ('a', 'b', 'c')) bools_target = [ [0, 1, 1], [1, 0, 1], [1, 0, 0], ] Objects, Attributes, bools = load_sparse_csv(filename_dataset, filename_attributes) assert bools == bools_target assert Objects.supremum == Objects_target.supremum assert Attributes.supremum == Attributes_target.supremum
def random_date_init(tree, sampling_time, rep, min_nleaf=3, seed=None): if seed is None: seed = random.randint(0, 1024) random.seed(a=seed) history = [] X = [] T0 = [] preprocess(tree, sampling_time) node_list = get_uncalibrated_nodes(tree, sampling_time, min_nleaf=min_nleaf) if not node_list: print( "Warning: few calibration points were given. Set min_nleaf to 1 to maximize the number of possible initials" ) node_list = get_uncalibrated_nodes(tree, sampling_time, min_nleaf=1) k = len(node_list) # k should always be larger than 0, by construction nrep = rep is_lacking = False if k < rep and 2**k <= rep: # include k < rep to avoid computing 2**k if k is obviously large nrep = 2**k is_lacking = True print( "Warning: do not have enough calibration points/sampling time to construct " + str(rep) + " distinct initials. Reduced to " + str(nrep)) node_bitset = bitset('node_bitset', node_list) for i in range(nrep): if is_lacking: x = i else: x = int(random.getrandbits(k)) while x <= 0 or x in history: x = int(random.getrandbits(k)) history.append(x) random_subset = list(node_bitset.fromint(x)) x0, t0 = get_random_initial(tree, random_subset, sampling_time) X.append(x0) T0.append(t0) reset(tree, sampling_time) return X, seed, T0
def test_typicality_rosch_2(): Attributes = bitset('Attributes', range(4)) objects = [ Attributes.frommembers([0]), Attributes.frommembers([1]), Attributes.frommembers([2]), Attributes.frommembers([3]), ] weights = [1, 1, 1, 1] assert typicality_rosch(objects[0], objects) == sum( compress(weights, objects[0].bools())) assert typicality_rosch_ln(objects[0], objects) == sum( compress([math.log(x) for x in weights], objects[0].bools()))
def calculateBlackInitialBoardState(): masterBitboard = [] for i in range(31, -1, -1): masterBitboard.append(i) masterBitboard = tuple(masterBitboard) masterBitboard = bitset("bitboard", masterBitboard) bitmask = masterBitboard(tuple([1])).bits() bitboard = [ BitArray('0b' + bitmask[24:32]), BitArray('0b' + bitmask[16:24]), BitArray('0b' + bitmask[8:16]), BitArray('0b' + bitmask[0:8]) ] return bitboard
def calculateMovesBitboards(classifiedMoves): movesBitboards = {} masterBitboard = [] for i in range(31, -1, -1): masterBitboard.append(i) masterBitboard = tuple(masterBitboard) masterBitboard = bitset("bitboard", masterBitboard) for name in classifiedMoves: movesFromSquares = classifiedMoves[name] movesFromSquaresBitboards = [] for boundedMoves in movesFromSquares: boundedMovesBitboards = [] for moves in boundedMoves: squares = [] for move in moves: squares.append(move[0]) bitmask = masterBitboard(tuple(squares)).bits() bitboard = [ BitArray('0b' + bitmask[24:32]), BitArray('0b' + bitmask[16:24]), BitArray('0b' + bitmask[8:16]), BitArray('0b' + bitmask[0:8]) ] boundedMovesBitboards.append(bitboard) movesFromSquaresBitboards.append(boundedMovesBitboards) movesBitboards[name] = movesFromSquaresBitboards return movesBitboards
def gen_network_policy_atom(index_rtr_rules_list): """ Gen atom predicate set for a whole network; :param index_rtr_rules_list: indexed router rule set :return: """ router_num = len(index_rtr_rules_list) ori_PS_list = list() rtr_atom_set_list = list() for i in range(router_num): rtr_atom_set_list.append([0] * router_num) ori_PS_list.append(gen_rtr_policy_ori(index_rtr_rules_list[i])) # generate atomic policy space list: atomPSList = list() numofaPS = 0 for index1 in range(router_num * router_num): # for each port: router1 = index1 / router_num port1 = index1 % router_num for index2 in range((router1 + 1) * router_num, router_num * router_num): # Noneset += 1 router2 = index2 / router_num port2 = index2 % router_num Atom = ori_PS_list[router1][port1].__and__( ori_PS_list[router2][port2]) # print (router1, port1, router2, port2) if Atom is not None: atomPSList.append(Atom) # atomPSList.append((Atom, (router1, port1, router2, port2))) rtr_atom_set_list[router1][port1] += (1 << numofaPS) rtr_atom_set_list[router2][port2] += (1 << numofaPS) numofaPS += 1 atomPSTuple = tuple(atomPSList) atom_PS_bitset = bitset('atomPSBitset', atomPSTuple) return (atom_PS_bitset, rtr_atom_set_list)
def random_date_init(tree, sampling_time, rep, rootAge=None, min_nleaf=3, seed=None): if seed is None: seed = random.randint(0, 1024) random.seed(a=seed) initial_fix_age(tree) history = [] X = [] T0 = [] node_list = get_node_list(tree, min_nleaf=min_nleaf) k = len(node_list) node_bitset = bitset('node_bitset', node_list) for i in range(rep): x = int(random.getrandbits(k)) while x <= 0 or x in history: x = int(random.getrandbits(k)) history.append(x) selected = list(node_bitset.fromint(x)) x0, t0 = date_as_selected(tree, sampling_time, selected, rootAge=rootAge) X.append(x0) T0.append(t0) return X, seed, T0
def test_wrong_base(self): with self.assertRaisesRegexp(ValueError, 'subclass'): bitset('Letters', 'abc', set)
def test_unhashable_member(self): with self.assertRaisesRegexp(TypeError, 'unhashable'): bitset('Letters', ([], None))
def test_unhashable_members(self): with self.assertRaisesRegexp(TypeError, 'unhashable'): bitset('Letters', list('abc'))
def test_duplicated_member(self): with self.assertRaisesRegexp(ValueError, 'duplicates'): bitset('Letters', 'abca')
def test_no_member(self): with self.assertRaisesRegexp(ValueError, 'one'): bitset('Letters', '')
def test_members_nonsequence(): with pytest.raises(ValueError, match=r'sequence'): bitset('Letters', frozenset('abc'))
#!/usr/bin/env python # visualize-examples.py import bitsets import bitsets.visualize ARGS = {'directory': 'visualize-output', 'format': 'pdf'} Four = bitsets.bitset('Four', (1, 2, 3, 4)) bitsets.visualize.bitset(Four, render=True, **ARGS) bitsets.visualize.bitset(Four, member_label=True, render=True, **ARGS) Six = bitsets.bitset('Six', (1, 2, 3, 4, 5, 6)) dot = bitsets.visualize.bitset(Six, member_label='iching', **ARGS) dot.graph_attr.update(ranksep='1.2', splines='false') dot.node_attr.update(shape='none', fontname='DejaVu Sans', fontsize='24') dot.render()
def Four(): # noqa: N802 return bitsets.bitset('Four', (1, 2, 3, 4))
def Attributes(): universum = range(5) Attribtes = bitset('Attributes', universum) return Attribtes
def setUpClass(cls): cls.Four = bitsets.bitset('Four', (1, 2, 3, 4))
def test_wrong_base(): with pytest.raises(ValueError, match=r'subclass'): bitset('Letters', 'abc', set)
def test_unhashable_member(): with pytest.raises(TypeError, match=r'unhashable'): bitset('Letters', ([], None))
def test_unhashable_members(): with pytest.raises(TypeError, match=r'unhashable'): bitset('Letters', list('abc'))
def test_one_member(self): letter = bitset('Letter', 'a') self.assertEqual(letter('a').real, 1) self.assertEqual(letter('').real, 0)
def test_empty_name(self): with self.assertRaisesRegexp(ValueError, 'empty'): bitset('', 'abc')
def test_members_nonsequence(self): with self.assertRaisesRegexp(ValueError, 'sequence'): bitset('Letters', frozenset('abc'))
def test_one_member(): letter = bitset('Letter', 'a') assert letter('a').real == 1 assert letter('').real == 0
def test_no_member(): with pytest.raises(ValueError, match=r'one'): bitset('Letters', '')
# Importing bitset to create universal set class and implement bitset representation from bitsets import bitset # Function to take input of set in set notation def set_input(x): try: y = list(x.split()) except: y = list(x) return y print("Enter Sets sequentially as a string with "" to separate elements- ") x = input("Enter Universal Set as string- ") U = tuple(set_input(x)) # Creating UniversalSet Class with the help of bitset() function UniversalSet = bitset('UniversalSet', U) # Function for user to get bitstring representation of any set that is a subset of the universal set U def set_as_bitstring(A): return UniversalSet(A).bits() # Function to convert bitstring stored as list to standard set form def list_to_set (A): str1 = "" for item in A: str1 += item return list(UniversalSet.frombits(str1)) # Function that shows the working of set intersect operation with sets as bitstrings
def test_empty_name(): with pytest.raises(ValueError, match=r'empty'): bitset('', 'abc')
def test_duplicated_member(): with pytest.raises(ValueError, match=r'duplicates'): bitset('Letters', 'abca')
for line in sys.stdin: if linenum == 0: students_total, courses_total, students_per_course = (int(part) for part in line.strip().split(' ')) linenum += 1 else: parts = line.strip().split(' ') student = parts[0] course_count = int(parts[1]) courses = parts[2:] for course in courses: all_courses.add(course) students[student] = (course_count, courses) all_courses = frozenset(all_courses) Students = bitsets.bitset('Students', tuple(students.keys())) # Print out sanity check statistics debug(students_total, courses_total, students_per_course) debug(len(students)) courses_sum = 0 for student in students.keys(): course_count, _ = students[student] courses_sum += course_count debug(float(courses_sum) / float(len(students))) # # Course popularity; handle courses in popularity order, assign in loop to # spots