Beispiel #1
0
def BuildTree(path_tuple):
    """
    Build a tree structure from a given path tuple
    """
    if _debug: print "##\nBuild Tree...\ntuple:", path_tuple
    
    node_to_split,y_value = FindBestGain(path_tuple)
    
    if node_to_split < 0:
        if _debug: print "found leaf for tuple: ", path_tuple
        training_tree = Tree("leaf")
        training_tree.y_value = y_value
        training_tree.is_leaf = True
    else:
        # Create node for attribute to split on
        training_tree = Tree(_attrib_dict[str(node_to_split)])
        
        # Build left side of tree for node
        left_tuple = path_tuple + [(node_to_split,0)]
        left_tree = BuildTree(left_tuple)
        training_tree.AddLeftChild(left_tree)
            
        # Build right side of tree for node
        right_tuple = path_tuple + [(node_to_split, 1)]
        right_tree = BuildTree(right_tuple)
        training_tree.AddRightChild(right_tree)

    return training_tree
Beispiel #2
0
 def read_tree(self, line):
     parents = map(int,line.split())
     trees = dict()
     root = None
     for i in xrange(1,len(parents)+1):
         #if not trees[i-1] and parents[i-1]!=-1:
         if i-1 not in trees.keys() and parents[i-1]!=-1:
             idx = i
             prev = None
             while True:
                 parent = parents[idx-1]
                 if parent == -1:
                     break
                 tree = Tree()
                 if prev is not None:
                     tree.add_child(prev)
                 trees[idx-1] = tree
                 tree.idx = idx-1
                 #if trees[parent-1] is not None:
                 if parent-1 in trees.keys():
                     trees[parent-1].add_child(tree)
                     break
                 elif parent==0:
                     root = tree
                     break
                 else:
                     prev = tree
                     idx = parent
     return root
def get_best_TC_tree(
    dv_file,
    gm_file,
    label_file,
    tree_files,
    name='unnamed_tree',
    ):
    """
    Given a distance-variance file, a genome-map file, a label file
    and a number of tree files
    """

    if not isinstance(tree_files, list):
        return Tree.new_treecollection_tree(dv_file, gm_file,
                label_file, tree_files, name)
    best_score = float('inf')
    best_tree = None
    starts = len(tree_files)

    for i in range(starts):
        guide = tree_files[i]
        current_tree = Tree.new_treecollection_tree(dv_file, gm_file,
                label_file, guide, name)
        if current_tree.score < best_score:
            best_score = current_tree.score
            best_tree = current_tree

    return best_tree
Beispiel #4
0
    def fitness_eval(self, pacman, ghost):
        # Create three identical ghosts
        ghost.board = self.board
        pacman.board = self.board
        ghost.score = 0
        pacman.score = 0
        ghosts = [copy.deepcopy(ghost) for i in range(3)]

        self.init_log(pacman, ghosts)

        # Run turns until the game is over
        while not self.game_over and self.time > 0 and len(self.board.pills) > 0:
            self.turn(pacman, ghosts)
            self.time -= 1
            if len(self.board.pills) == 0:
                pacman.score += int(self.time/float(self.tot_time) * 100)
            self.log_turn(pacman, ghosts)

        pacman.score = max(pacman.score, 1)

        ghost.score = -pacman.score
        ghost.score -= self.parsimony_coef_ghost*Tree.find_depth(ghost.tree)

        # Parsimony pressure
        pacman.score -= self.parsimony_coef_pacman*Tree.find_depth(pacman.tree)
        pacman.score = max(pacman.score, 1)

        return pacman.score
Beispiel #5
0
        def make_master_tree(
            n,
            method,
            names=None,
            inner_edge_params=(1, 1),
            leaf_params=(1, 1),
            distribution_func=np.random.gamma,
            ):
            """
            Function returns a tree object with n tips,
            named according to `names`, and constructed
            according to `method`, which is one of 'random_topology',
            'random_yule' and 'random_coal'
            """

            if method == 'random_topology':
                master_topology = Tree.new_random_topology(n,
                        names=names, rooted=True)
                master_tree = \
                    master_topology.randomise_branch_lengths(inner_edges=inner_edge_params,
                        leaves=leaf_params,
                        distribution_func=branch_length_func)
                master_tree.newick = '[&R] ' + master_tree.newick
            elif method == 'random_yule':
                master_tree = Tree.new_random_yule(n, names=names)
            elif method == 'random_coal':
                master_tree = Tree.new_random_coal(n, names=names)
            return master_tree
Beispiel #6
0
def TdiExecute(expression,args=None):
    """Compile and execute a TDI expression. Format: TdiExecute('expression-string')"""
    from _descriptor import descriptor_xd,descriptor,MdsGetMsg
    from tree import Tree
    xd=descriptor_xd()
    done=False
    try:
        Tree.lock()
        restoreContext()
        if args is None:
            status=TdiShr.TdiExecute(pointer(descriptor(expression)),pointer(xd),c_void_p(-1))
        else:
            if isinstance(args,tuple):
                if len(args) > 0:
                    if isinstance(args[0],tuple):
                        ans = TdiExecute(expression,args[0])
                        done=True
                if not done:
                    exp='TdiShr.TdiExecute(pointer(descriptor(expression))'
                    for i in range(len(args)):
                        exp=exp+',pointer(descriptor(args[%d]))' % i
                    exp=exp+',pointer(xd),c_void_p(-1))'
                    status=eval(exp)
            else:
                raise TypeError('Arguments must be passed as a tuple')
    finally:
        Tree.unlock()
    if done:
        return ans
    if (status & 1 != 0):
            return xd.value
    else:
        raise TdiException(MdsGetMsg(status,"Error compiling expression"))
Beispiel #7
0
def main():
	bordVariables = bord(2) # return [carsList,width,exit]
	global WIDTH; WIDTH = bordVariables[1]
	global CARS_LIST; CARS_LIST = bordVariables[0]
	global INITIAL_STATE; INITIAL_STATE = CARS_LIST.getFirstState()
	global STATES_ARCHIVE; STATES_ARCHIVE = Tree(WIDTH, CARS_LIST.getDirectionsList())

	aantalCars = len(CARS_LIST.cars)
	# voeg vaak een state toe aan de tree
	for i in range(0, 100000):
		randomState = []
		for j in range(0, aantalCars):
			randomState.append(randint(0,WIDTH-2)*WIDTH + randint(0,WIDTH - 2))

		# print randomState

		STATES_ARCHIVE.addState(randomState, INITIAL_STATE)
	print "ik ben klaar met states toevoegen"
	
	# # zoek een state in de tree
	# for i in range(0, 100000):
	# 	randomState = []
	# 	for j in range(0, aantalCars):
	# 		randomState.append(randint(0,WIDTH-2)*WIDTH + randint(0,WIDTH - 2))
	# 	STATES_ARCHIVE.checkState(randomState)
	# print "klaar met states checken"



	STATES_ARCHIVE.addState(INITIAL_STATE, INITIAL_STATE)
Beispiel #8
0
def main():

	# Car1 = Car(22, False, 3)
	# Car2 = Car(9, False, 2)
	# Car3 = Car(10, True, 2)
	# RedCar = Car(20, True, 2)

	# CARS_LIST.cars.append(Car1)
	# CARS_LIST.cars.append(Car2)
	# CARS_LIST.cars.append(Car3)
	# CARS_LIST.cars.append(RedCar)

	RedCar = Car(20, True, 2)
	Car1 = Car(0, False, 2)
	Car2 = Car(12, True, 2)
	Car3 = Car(3, False, 2)
	Car4 = Car(4, True, 2)
	Car5 = Car(10, True, 2)
	Car6 = Car(14, True, 2)
	Car7 = Car(16, False, 2)
	Car8 = Car(17, False, 3)
	Car9 = Car(25, True, 2)
	Car10 = Car(27, True, 2)
	Car11 = Car(32, True, 2)
	Car12 = Car(34, True, 2)

	CARS_LIST.cars.append(Car1)
	CARS_LIST.cars.append(Car2)
	CARS_LIST.cars.append(Car3)
	CARS_LIST.cars.append(Car4)
	CARS_LIST.cars.append(Car5)
	CARS_LIST.cars.append(Car6)
	CARS_LIST.cars.append(Car7)
	CARS_LIST.cars.append(Car8)
	CARS_LIST.cars.append(Car9)
	CARS_LIST.cars.append(Car10)
	CARS_LIST.cars.append(Car11)
	CARS_LIST.cars.append(Car12)
	CARS_LIST.cars.append(RedCar)

	# TEST dept first algorithme:
	global INITIAL_STATE; INITIAL_STATE = CARS_LIST.getFirstState()
	global STATES_ARCHIVE; STATES_ARCHIVE = Tree(WIDTH, CARS_LIST.getDirectionsList())
	algorithm(INITIAL_STATE)
	path1 = SOLUTION_PATHS[0]

	# print results
	# print "length solutions ", len(SOLUTIONS)
	#rint SOLUTIONS
	print len(SOLUTION_PATHS)
	listSolutionsPaths = []
	for sols in SOLUTION_PATHS:
		listSolutionsPaths.append(len(sols))
	print listSolutionsPaths

	pathDepth = []
	for state in path1[:-1]:
		print len(state), state
		pathDepth.append(STATES_ARCHIVE.goToEndNode(state).depth)
	print pathDepth
Beispiel #9
0
def eval_decmodel(s):
    d = eval(s)
    v = d["lagrange_version"] 
    if v != VERSION:
        print >> sys.stderr, "***Version mismatch: %s (expecting %s)***" \
              % (v, VERSION)
        print >> sys.stderr, "Things may not work as expected"
    for x in ("area_labels", "taxon_range_data", "newick_trees"):
        assert x in d, "required for analysis, but missing: %s" % x
    labels = d["area_labels"]
    nareas = len(labels)
    data = d["taxon_range_data"]
    maxareas = d.get("max_range_size") or len(labels)
    dists = d["ranges"]
    excluded = d["excluded_ranges"]
    dm = d["area_dispersal"]
    periods = d["dispersal_durations"]
    model = DECModel(nareas, labels, periods=periods, dists=dists)
    model.Dmask[:] = dm
    trees = d["newick_trees"]
    newicktree = trees[0]["newick"]
    nodelabels = trees[0]["included"]
    root_age = trees[0]["root_age"] or None
    tree = Tree(newicktree, periods=periods, root_age=root_age)
    tree.set_default_model(model)
    tree.set_tip_conditionals(data)
    base_rates = d["base_rates"]
    if base_rates == "__estimate__":
        pass
    else:
        base_rates = (d["base_rates"]["dispersal"], d["base_rates"]["extinction"])
    return model, tree, data, nodelabels, base_rates
Beispiel #10
0
def check_fitness(trees):
    errors = []
    for one_tree in trees:
        if len(one_tree.tree_map) == 0:
            continue
        sum_error = 0
        good_individual = True
        for i in range(0, len(VARIABLE_VALUES_SET)):
            error = Reproductor.get_error(one_tree, TARGET_VALUES[i], VARIABLE_VALUES_SET[i])
            if error > ALLOWABLE_ERROR:
                good_individual = False
            sum_error += error
        if isinf(sum_error):
            continue
        errors.append(sum_error)
        if good_individual or sum_error < TARGET_RESULT:
            print("RESULT")
            print(Tree.tree_map_to_string(one_tree.tree_map))
            print(one_tree.init_tree)
            one_tree.fitness = sum_error
            results.append(one_tree)
    print("MIN FITNESS RESULT: ", min(errors))
    print "AVERAGE FITNESS: ", sum(errors)/len(errors)
    print "length ", len(errors)
    print "results "
    for r in results:
        print "fitness ", r.fitness
        print Tree.tree_map_to_string(r.tree_map)
Beispiel #11
0
    def testTraverse(self):
        tree = Tree()
        for word in insert_queue:
            tree.insert(word)

        insert_queue.sort()
        self.assertEqual(tree.inorder_traverse(), " ".join(insert_queue))
Beispiel #12
0
def getObjectsTree(qTreeView, table, indexes, extract):
    """Create an object tree representation from QTreeView.
    """
    tree = Tree()
    model = qTreeView.model()
    extracted = tree.fromQStandardItemModel(model, table, indexes, extract)
    return tree, extracted
def filelists_from_spec(spec, specpath):
    res = Tree()
    for pkg in spec.packages:
        name = "%s.install.in" % mappkgname.map_package_name(pkg.header)
        res.append("debian/%s" % name, 
                   files_from_pkg(spec.sourceHeader['name'], pkg, specpath))
    return res
Beispiel #14
0
    def testProperty(self):

        x = Tree(value=None)
        self.assertTrue(x.value == None)

        x.value = 5
        self.assertTrue(x.value == 5)
Beispiel #15
0
def test_traverse_returns_none_when_operation_always_returns_false():
    t = Tree("a")
    t.add_child("b")

    node = t.traverse(lambda n: False)

    assert_that(node, is_(None))
def main():
    tree = Tree('pie')
    tree.add_item('cake')
    tree.add_item('cookie')
    for item in tree:
        print('{}: {}'.format(item.datum, item.height()))
    #Should print:
    #cake: 0
    #pie: 1
    #cookie: 0

    tree.add_item('cupcake')
    for item in tree:
        print('{}: {}'.format(item.datum, item.height()))
    #Should print:
    #cupcake: 0
    #cake: 1
    #pie: 2
    #cookie: 0

    #this constructs the tree shown in the exercise
    other_tree = Tree(1)
    for i in [2, 3, 4, 6, 5, 7]:
        other_tree.add_item(i)
    print(list(map(lambda d: d.datum, other_tree)))
Beispiel #17
0
def main():
    a = Tree(1)
    b = Tree(4)
    c = Tree(6)
    d = Tree(8)
    e = Tree(2, a)
    f = Tree(3, e, b)
    g = Tree(7, c, d)
    h = Tree(5, f, g)

    print "####### Left View ########"
    print_next = False
    for node in Tree.get_level_order_traversal(h):
        if not isinstance(node, Tree):
            print_next = True
        elif print_next:
            print node.v
            print_next = False
            
    print "####### Right View ########"
    prev = None
    for node in Tree.get_level_order_traversal(h):
        if not isinstance(node, Tree) and prev:
            print prev.v
        prev = node
    print node.v
Beispiel #18
0
def main():

    init_population = generate_init_population()
    result = False
    counter = 0

    while counter < ITERATIONS_COUNT:
        print("************************************************************************************************************"
              "************************************************************************************************************")
        print(counter)
        best_individuals = deepcopy(reproduce(init_population))

        if len(best_individuals) == 0:
            print("Reproduction empty")
            break

        new_generation = deepcopy(create_new_generation(best_individuals))
        mutated_trees = deepcopy(mutate_trees(new_generation))
        check_fitness(mutated_trees)
        init_population = deepcopy(mutated_trees)

        if len(init_population) == 0:
            print("Empty population")
            break
        counter += 1

    print("End")
    if len(results) > 0:
        print("")
        print "MIN RESULT"
        print Tree.tree_map_to_string(min(results))
        print min(results).init_tree
        print min(results).fitness
Beispiel #19
0
 def test_new_tree_add_2_nodes_and_print_it(self):
     t = Tree()
     n = Node(title='test', id='1', parent_id='root')
     t.add(n)
     n = Node(title='test2', id='2', parent_id='1')
     t.add(n)
     print(t)
Beispiel #20
0
 def testIsRoot(self):
   if IGNORE_TEST:
     return
   self.assertTrue(self.root.isRoot())
   new_tree = Tree("DUMMY_TREE")
   self.root.addChild(new_tree)
   self.assertFalse(new_tree.isRoot())
   self.assertTrue(self.root.isRoot())
Beispiel #21
0
 def ttree(self,name,varnames):
     """ book andf store a Tree object (from tree.py)
     """
     self.cd()
     t1 = Tree(name=name)
     t1.book(varnames)
     self.put(t1)
     return t1
Beispiel #22
0
def test_add_child_appends_node_to_the_child_list():
    t = Tree("a")
    t.add_child("b")

    t.add_child("c")

    assert_that(t.children[0].value, is_("b"))
    assert_that(t.children[1].value, is_("c"))
Beispiel #23
0
def antlrTree2Tree(antlrTree):
	t = Tree(antlrTree.type, antlrTree.text, antlrTree.line, antlrTree.charPositionInLine)

	for i in range(antlrTree.getChildCount()):
		subT = antlrTree2Tree(antlrTree.getChild(i))
		t.addChild(subT)

	return t
Beispiel #24
0
 def apply(self, value_root):
     source_parent = Tree._navigate_to_index_parent(self._index, value_root)
     moved_node = source_parent.pop_child(self._index[-1])
     dest_parent = Tree._navigate_to_index_parent(
         self._dest_index,
         value_root)
     dest_parent.insert_child(self._dest_index[-1], moved_node)
     event = self.Event(source_parent, moved_node)
     source_parent.trigger_event(event)
Beispiel #25
0
 def openTrees(self):
     self.pytree=Tree('pytree',self.shot)
     self.pytree.getNode('.pytreesub').include_in_pulse=True
     self.assertEqual(str(self.pytree),'Tree("PYTREE",'+str(self.shot)+',"Normal")')
     self.pytree.createPulse(self.shot+1)
     Tree.setCurrent('pytree',self.shot+1)
     self.pytree2=Tree('pytree',self.shot+1)
     self.assertEqual(str(self.pytree2),'Tree("PYTREE",'+str(self.shot+1)+',"Normal")')
     return
 def test_2_vertices(self):
     n = 2
     root = 1
     adjacent = [(0, 1)]
     tree = Tree(n, adjacent, root)
     self.assertEqual(tree.lca(0, 1), 1)
     self.assertEqual(tree.lca(1, 0), 1)
     self.assertEqual(tree.lca(0, 0), 0)
     self.assertEqual(tree.lca(1, 1), 1)
Beispiel #27
0
    def __init__(self, tree, mp='', ml=''):
        """
        Convert sequences in MSA file into ancestral state for each site and attach them to each node

        @param tree: string, a newick format string or a filename of a newick format file (in this version this only accept a newick format file generated by FastML instead of a untouched newick format file which only contains aligned sequence for all terminal nodes)
        @param mp: string, filename of a MSA file, MP method only
        @param ml: string, filename of a MSA file, ML method only
        """
        Tree.__init__(self, tree, mp, ml)
 def test_example(self):
     n = 8
     root = 2
     adjacent = [(0, 6), (3, 6), (6, 2), (4, 2), (1, 4), (5, 1), (7, 1)]
     tree = Tree(n, adjacent, root)
     self.assertEqual(tree.lca(0, 7), 2)
     self.assertEqual(tree.lca(5, 1), 1)
     self.assertEqual(tree.lca(1, 5), 1)
     self.assertEqual(tree.lca(0, 3), 6)
Beispiel #29
0
    def testSearch(self):
        tree = Tree()
        for word in insert_queue:
            tree.insert(word)

        for i in insert_queue:
            self.assertEqual(tree.search(i), i)

        self.assertEqual(tree.search("not found"), None)
Beispiel #30
0
def analyze(settings):
    start_time = time.time()

    f = Filters(settings['code'], settings['filters'])
    tree = Tree(settings['path'], f.filters(), settings['antifilters'], settings)
    tree.output()

    time_sec = time.time() - start_time
    print ("\nanalyze time : {0:2.5f}sec".format(time_sec))
Beispiel #31
0
from tree import Tree

import subprocess
import sys

if __name__ == "__main__":
    tree = Tree(5)
    tree.add_value(3)
    tree.add_value(9)
    tree.add_value(10)
    tree.add_value(6)
    tree.add_value(7)


    tree.traverse()
    tree.rotate_to_root(7)

    tree.traverse()
    tree.rotate_to_root(7)
    tree.traverse()

    tree.add_value(2)
    tree.add_value(4)

    tree.add_value(8)
    tree.traverse()

    tree.rotate_to_root(4)
    tree.delete(4)
    tree.traverse()
 def select_item(self, n):
     Tree.select_item(self, n)
     self.client.directory = self.get_item_path(self.selected_item)
Beispiel #33
0
    def initUI(self):
        config = Foo.readConfig('options')
        self.timeOut = -1
        self.radio = False
        self.statusBar().showMessage('Ready')
        self.createMenu()
        self.setWindowTitle("Foo.cd")

        self.player = Player(Foo.readConfig('audio'))
        self.player.bus.connect('message::eos', self.stop)
        self.player.bus.connect('message::duration-changed',
                                self.onDurationChanged)

        self.tree = Tree(self, config['tree_order'])
        self.tree.addSongs.connect(self.addSongsFromTree)
        self.tree.customContextMenuRequested.connect(self.tmpTag)

        if not self.radio:
            self.table = Table(self, config)
            self.handlerATF = self.player.playbin.connect(
                "about-to-finish", self.onAboutToFinish)
            self.table.runAction.connect(self.tableAction)
        else:
            configRadio = Foo.readConfigRadios()
            self.table = TableRadio(self, configRadio)
            self.table.runAction.connect(self.tableAction)
            self.handlerT = self.player.bus.connect('message::tag',
                                                    self.table.onTag)

        self.playbackButtons = PlaybackButtons(None)
        self.playbackButtons.buttonPlay.clicked.connect(self.toggleSong)
        self.playbackButtons.buttonStop.clicked.connect(self.stop)
        self.playbackButtons.buttonPrev.clicked.connect(self.previous)
        self.playbackButtons.buttonNext.clicked.connect(self.next)

        self.volumeSlider = VolumeSlider(self)
        self.volumeSlider.sliderMoved.connect(self.player.setVolume)
        self.scrollSlider = ScrollSlider(self)
        self.scrollSlider.sliderPressed.connect(self.player.toggle)
        self.scrollSlider.sliderReleased.connect(self.player.toggle)
        self.scrollSlider.sliderMoved.connect(self.player.seek)

        self.pixmap = Image(self, config['cover_names'], config['extensions'])

        # Album cover connections
        self.tree.selectionModel().selectionChanged.connect(
            lambda: self.pixmap.onSelectionChanged(self.tree.getChildren()[0].
                                                   get('file', None)))
        self.table.selectionModel().selectionChanged.connect(
            lambda: self.pixmap.onSelectionChanged(self.table.getSelection().
                                                   get('file', None)))

        self.searchArea = SearchArea(self)
        self.searchArea.searchLine.returnPressed.connect(self.startSearch)

        self.playbackButtons.addWidget(self.volumeSlider)
        self.playbackButtons.addWidget(self.scrollSlider)

        splitterLeftRight = QtGui.QSplitter()
        self.splitterTopBottom = QtGui.QSplitter(Qt.Vertical, self)

        self.infoFrame = QtGui.QFrame()
        infoLayout = QtGui.QVBoxLayout()
        infoLayout.setContentsMargins(0, 0, 0, 0)
        infoLayout.addLayout(self.playbackButtons)
        infoLayout.addWidget(self.pixmap)
        self.infoFrame.setLayout(infoLayout)

        libLayout = QtGui.QVBoxLayout()
        libLayout.setContentsMargins(0, 0, 0, 0)
        libLayout.addWidget(self.tree)
        libLayout.addLayout(self.searchArea)
        libFrame = QtGui.QFrame()
        libFrame.setLayout(libLayout)

        self.splitterTopBottom.addWidget(self.table)
        self.splitterTopBottom.addWidget(self.infoFrame)
        self.splitterTopBottom.setStretchFactor(0, 1)
        #self.splitterTopBottom.setStretchFactor(1,0)

        splitterLeftRight.addWidget(libFrame)
        splitterLeftRight.addWidget(self.splitterTopBottom)
        splitterLeftRight.setStretchFactor(0, 2)
        splitterLeftRight.setStretchFactor(1, 3)

        mainLayout = QtGui.QGridLayout()
        mainLayout.setContentsMargins(4, 4, 4, 4)
        mainLayout.addWidget(splitterLeftRight)

        dummyWidget = QtGui.QWidget()
        dummyWidget.setLayout(mainLayout)
        self.setCentralWidget(dummyWidget)

        self.setTabOrder(self.tree, self.table)

        dictShortcuts = self.readConfig('shortcuts')
        modifier = dictShortcuts['modifier'] + '+'
        self.shortQuit = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['quit']), self,
            self.close)
        self.shortStop = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['stop']), self,
            self.stop)
        self.shortPlayPause = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['play_pause']), self,
            self.toggleSong)
        self.shortSongPrevious = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['previous']), self,
            self.previous)
        self.shortSongNext = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['next']), self,
            self.next)
        self.shortVolDown = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['volume_down']), self,
            self.volumeSlider.decr)
        self.shortVolUp = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['volume_up']), self,
            self.volumeSlider.incr)
        self.shortRadioMode = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['radio_mode']), self,
            self.toggleRadio)
        self.shortEqualizer = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['equalizer']), self,
            self.openEqualizer)

        thread = QtCore.QThread(self)
        thread.worker = WorkThreadPipe()
        thread.worker.moveToThread(thread)
        thread.started.connect(thread.worker.process)
        thread.worker.hotKey.connect(self.onHotKey)
        thread.worker.finished.connect(thread.quit)
        thread.worker.finished.connect(thread.worker.deleteLater)
        thread.finished.connect(thread.deleteLater)
        thread.start()

        self.show()
Beispiel #34
0
class FCMdata(object):
    """
    Object representing flow cytometry data
    FCMdata.pnts : a numpy array of data points
    FCMdata.channels : a list of which markers/scatters are on which column of
    the array.
    FCMdata.scatters : a list of which indexes in fcmdata.channels are scatters
    """
    def __init__(self, name, pnts, channels, scatters=None, notes=None):
        """
        fcmdata(name, pnts, channels, scatters=None)
        name: name of corresponding FCS file minus extension
        pnts: array of data points
        channels: a list of which markers/scatters are on which column of
                    the array.
        scatters: a list of which indexes in channels are scatters

        """
        self.name = name
        self.tree = Tree(pnts, channels)

        #TODO add some default intelligence for determining scatters if None
        self.scatters = scatters
        self.markers = []
        if self.scatters is not None:
            for chan in range(len(channels)):
                if chan in self.scatters:
                    pass
                elif self.tree.root.channels[chan] in self.scatters:
                    pass
                else:
                    self.markers.append(chan)
        if notes == None:
            notes = Annotation()
        self.notes = notes

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return self.name

    def _lookup_item(self, item):
        if isinstance(item, tuple):

            item = list(item)  # convert to be mutable.
            if isinstance(item[1], basestring):

                item[1] = self.name_to_index(item[1])
            elif isinstance(item[1], tuple) or isinstance(item[1], list):

                item[1] = list(item[1])  # convert to be mutable.
                for i, j in enumerate(item[1]):
                    if isinstance(j, basestring):
                        item[1][i] = self.name_to_index(j)
            item = tuple(item)
        return item

    def __getitem__(self, item):
        """return FCMdata points"""
        item = self._lookup_item(item)

        return self.tree.view()[item]

    def __setitem__(self, key, value):
        item = self._lookup_item(key)
        self.tree.view()[item] = value

    @property
    def channels(self):
        return [i for i in self.current_node.channels]

    def __len__(self):
        return self.current_node.view().__len__()

    def __getattr__(self, name):
        if name in dir(self.current_node.view()):
            #return Node.__getattribute__(self.current_node,'view')().__getattribute__(name)
            return self.current_node.view().__getattribute__(name)
        else:
            raise AttributeError("'%s' has no attribue '%s'" %
                                 (str(self.__class__), name))

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, dict):
        for i in dict.keys():
            self.__dict__[i] = dict[i]

    def name_to_index(self, channels):
        """Return the channel indexes for the named channels"""

        if isinstance(channels, basestring):
            try:
                if channels in self.channels:
                    return self.channels.index(channels)
                else:
                    raise ValueError('%s is not in list' % channels)

            except ValueError:
                for j in range(1, int(self.notes.text['par']) + 1):
                    if channels == self.notes.text['p%dn' % j]:
                        return self.channels.index(self.notes.text['p%ds' % j])
                raise ValueError('%s is not in list' % channels)

        idx = []
        for i in channels:
            if i in self.channels:
                idx.append(self.channels.index(i))
            else:
                raise ValueError('%s is not in list' % channels)

        if idx:
            return idx
        else:
            raise ValueError('field named a not found: %s' % str(channels))

    def get_channel_by_name(self, channels):
        """Return the data associated with specific channel names"""

        return self.tree.view()[:, self.name_to_index(channels)]

    def get_markers(self):
        """return the data associated with all the markers"""

        return self.view()[:, self.markers]

    def get_spill(self):
        """return the spillover matrix from the original fcs used in compisating"""

        try:
            return self.notes.text['spill']
        except KeyError:
            return None

    def view(self):
        """return the current view of the data"""

        return self.tree.view()

    def visit(self, name):
        """Switch current view of the data"""

        self.tree.visit(name)

    @property
    def current_node(self):
        """return the current node"""

        return self.tree.current

    def copy(self):
        """return a copy of fcm data object"""

        tname = self.name
        tpnts = self.tree.root.data
        tnotes = self.notes.copy()
        tchannels = self.channels[:]

        tscchannels = self.scatters[:]
        tmp = FCMdata(tname, tpnts, tchannels, tscchannels, tnotes)
        from copy import deepcopy
        tmp.tree = deepcopy(self.tree)
        return tmp

    def logicle(self,
                channels=None,
                T=262144,
                m=4.5,
                r=None,
                w=0.5,
                a=0,
                scale_max=1e5,
                scale_min=0,
                rquant=None):
        """return logicle transformed channels"""

        if channels is None:
            channels = self.markers
        return _logicle(self, channels, T, m, r, scale_max, scale_min, w, a,
                        rquant)

    def hyperlog(self, channels, b, d, r, order=2, intervals=1000.0):
        """return hyperlog transformed channels"""

        return _hyperlog(self, channels, b, d, r, order, intervals)

    def log(self, channels=None):
        """return log base 10 transformed channels"""

        if channels is None:
            channels = self.markers
        return _log(self, channels)

    def gate(self, g, chan=None):
        """return gated region of fcm data"""

        return g.gate(self, chan)

    def subsample(self, s, model='random', *args, **kwargs):
        """return subsampled/sliced fcm data"""
        if isinstance(s, Subsample):
            return s.subsample(self)
        elif isinstance(s, slice):
            r = Subsample(s)
            return r.subsample(self)
        else:
            if model == 'random':
                r = RandomSubsample(s)
            elif model == 'anomaly':
                r = AnomalySubsample(s, *args, **kwargs)
            elif model == 'bias':
                r = BiasSubsample(s, *args, **kwargs)
            return r.subsample(self, *args, **kwargs)

    def compensate(self, sidx=None, spill=None):
        """Compensate the fcm data"""

        compensate(self, S=spill, markers=sidx)
        return self

    def get_cur_node(self):
        """ get current node """

        return self.current_node

    def add_view(self, node):
        """add a new node to the view tree"""

        self.tree.add_child(node.name, node)
        return self

    def summary(self):
        """returns summary of current view"""

        pnts = self.view()
        means = pnts.mean(0)
        stds = pnts.std(0)
        mins = pnts.min(0)
        maxs = pnts.max(0)
        medians = median(pnts, 0)
        dim = pnts.shape[1]
        summary = ''
        for i in range(dim):
            summary = summary + self.channels[i] + ":\n"
            summary = summary + "    max: " + str(maxs[i]) + "\n"
            summary = summary + "   mean: " + str(means[i]) + "\n"
            summary = summary + " median: " + str(medians[i]) + "\n"
            summary = summary + "    min: " + str(mins[i]) + "\n"
            summary = summary + "    std: " + str(stds[i]) + "\n"
        return summary

    def boundary_events(self):
        """returns dictionary of fraction of events in first and last
        channel for each channel"""

        boundary_dict = {}
        for k, chan in enumerate(self.channels):
            col = self.view()[:, k]
            boundary_dict[chan] = \
                sum((col == min(col)) | (col == max(col))) / len(col)
        return boundary_dict

    def export(self, file_name):
        """
        export out current view to a fcs file
        """
        from fcm.io import export_fcs
        export_fcs(file_name, self.view(), self.current_node.channels,
                   self.notes.text)

    def extract_channels(self, channels, keep=False):
        """
        create a view without the specified channels or with if keep==True
        """
        if isinstance(channels, basestring) or isinstance(channels, int):
            channels = [channels]
        for i, j in enumerate(channels):
            if isinstance(j, str):
                channels[i] = self.name_to_index(j)
        if keep:
            channels = [
                i for i in range(len(self.channels)) if i not in channels
            ]
        d = DropChannel(channels)
        d.drop(self)
        return self

    def add_channel(self, name, channel=None):
        if channel is None:
            channel = zeros((self.shape[0], 1))

        print channel.shape

        node = AddChannel(channel, name)
        node.add(self)
        return self
Beispiel #35
0
 def __init__(self):
     Tree.__init__(self)
     self.root = None  # Set up the tree with no root
     self._n_nodes = 0
Beispiel #36
0
def parse(input, grammar, actions, gotos):
    convert = {
        'IDENTIFIER': 'identifier',
        'ADDITION': '+',
        'ASSIGNMENT': ':=',
        'BEGIN': 'begin',
        'BOOLEAN_TYPE': 'boolean_type',
        'COLON': ':',
        'DO': 'do',
        'ELSE': 'else',
        'END': 'end',
        'EQUAL': '=',
        'FALSE': 'false',
        'GREATER': '>',
        'GREATER_EQUAL': '>=',
        'IF': 'if',
        'INTEGER_TYPE': 'integer_type',
        'LESS': '<',
        'LESS_EQUAL': '<=',
        'MULTIPLICATION': '*',
        'PERIOD': '.',
        'PROGRAM': 'program',
        'READ': 'read',
        'SEMICOLON': ';',
        'SUBTRACTION': '-',
        'THEN': 'then',
        'TRUE': 'true',
        'VAR': 'var',
        'WHILE': 'while',
        'WRITE': 'write',
        'BOOLEAN_EXPRESSION': 'be',
        'INTEGER_LITERAL': 'INTEGER_LITERAL'
    }

    # TODOd #1: create a list of trees
    trees = []
    stack = []
    stack.append(0)
    while True:
        state = stack[-1]
        token = convert[input[0][1].name]
        if token == ';':
            stack.append(input.pop(0))
            stack.append(7)
            state = stack[-1]
            token = convert[input[0][1].name]
        if token == ':':
            stack.append(input.pop(0))
            stack.append(54)
            state = 54
            token = convert[input[0][1].name]
        if token == 'begin':
            stack.append(input.pop(0))
            stack.append(7)
            state = stack[-1]
            token = convert[input[0][1].name]
        if token == 'INTEGER_LITERAL':
            stack.append(input.pop(0))
            stack.append(3)
            state = stack[-1]
            token = convert[input[0][1].name]
        if token == '<=':
            state = 47
        if token == 'do':
            stack.append(input.pop(0))
            stack.append(7)
            state = stack[-1]
            token = convert[input[0][1].name]
        if token == '+':
            state = 58
        if ((token == 'end') and (state > 15)):
            temp = input.pop(0)
            tempt = input.pop(0)
            state = 5
            token = convert[input[0][1].name]
        action = actions[(state, token)]
        if action == 'r1':
            for i in input:
                temp = input.pop(0)

            action = 'x'

        if action is None:
            return None  # tree building update

        # shift operation
        if action[0] == 's':
            input.pop(0)
            stack.append(token)
            state = int(action[1:])
            stack.append(state)

            # TODOd #2: create a new tree, set data to token, and append it to the list of trees
            tree = Tree()
            tree.data = token
            trees.append(tree)
            #print(state)

        # reduce operation
        elif action[0] == 'r':
            production = grammar[int(action[1])]
            lhs = getLHS(production)
            rhs = getRHS(production)
            for i in range(len(rhs) * 2):
                stack.pop()
            state = stack[-1]
            stack.append(lhs)
            stack.append(int(gotos[(state, lhs)]))

            # TODOd #3: create a new tree and set data to lhs
            newTree = Tree()
            newTree.data = lhs

            # TODOd #4: get "len(rhs)" trees from the right of the list of trees and add each of them as child of the new tree you created, preserving the left-right order
            for tree in trees[-len(rhs):]:
                newTree.add(tree)

            # TODOd #5: remove "len(rhs)" trees from the right of the list of trees
            trees = trees[:-len(rhs)]

            # TODOd #6: append the new tree to the list of trees
            trees.append(newTree)

        # not a shift or reduce operation, must be an "accept" operation
        else:
            production = grammar[0]
            lhs = getLHS(production)
            rhs = getRHS(production)

            # TODOd #7: same as reduce but using the 1st rule of the grammar
            root = Tree()
            root.data = lhs
            for tree in trees:
                root.add(tree)

            # TODOd #8: return the new tree
            return root
Beispiel #37
0
import json
import copy
import pandas as pd
from tree import Tree

if __name__ == "__main__":
    # execute only if run as a script
    with open('output.json') as json_file:
        json_data = json.load(json_file)

    links = json_data["links"]
    nodes = json_data["nodes"]

    # Load dataframe of eawag rules called "paths"
    df_paths = pd.read_pickle('paths.pkl')

    tree = Tree(nodes, links, df_paths)
    tree.build_tree()
    print(tree.root_node)
Beispiel #38
0
class Query:
    """ A Query is initialized by parsing a query string using QueryRoot as the
        grammar root nonterminal. The Query can then be executed by processing
        the best parse tree using the nonterminal handlers given above, returning a
        result object if successful. """

    _parser = None
    _processors = []
    _help_texts = dict()

    def __init__(self, session, query, voice, auto_uppercase, location,
                 client_id):
        q = self._preprocess_query_string(query)
        self._session = session
        self._query = q or ""
        self._location = location
        # Prepare a "beautified query" string that can be
        # shown in a client user interface. By default, this
        # starts with an uppercase letter and ends with a
        # question mark, but this can be modified during the
        # processing of the query.
        self.set_beautified_query(beautify_query(q))
        self._voice = voice
        self._auto_uppercase = auto_uppercase
        self._error = None
        # A detailed answer, which can be a list or a dict
        self._response = None
        # A single "best" displayable text answer
        self._answer = None
        # A version of self._answer that can be
        # fed to a voice synthesizer
        self._voice_answer = None
        self._tree = None
        self._qtype = None
        self._key = None
        self._toklist = None
        # Expiration timestamp, if any
        self._expires = None
        # URL assocated with query, can be set by query response handler
        # and subsequently provided to the remote client
        self._url = None
        # Client id, if known
        self._client_id = client_id
        # Source of answer to query
        self._source = None
        # Query context, which is None until fetched via self.fetch_context()
        # This should be a dict that can be represented in JSON
        self._context = None

    def _preprocess_query_string(self, q):
        """ Preprocess the query string prior to further analysis """
        if not q:
            return q
        qf = re.sub(_IGNORED_PREFIX_RE, "", q, flags=re.IGNORECASE)
        # If stripping the prefixes results in an empty query,
        # just return original query string unmodified.
        return qf or q

    @classmethod
    def init_class(cls):
        """ Initialize singleton data, i.e. the list of query
            processor modules and the query parser instance """
        procs = []
        # Load the query processor modules found in the
        # queries directory
        modnames = modules_in_dir("queries")
        for modname in sorted(modnames):
            try:
                m = importlib.import_module(modname)
                procs.append(m)
            except ImportError as e:
                logging.error(
                    "Error importing query processor module {0}: {1}".format(
                        modname, e))
        cls._processors = procs

        # Obtain query grammar fragments from those processors
        # that handle parse trees. Also collect topic lemmas that
        # can be used to provide context-sensitive help texts
        # when queries cannot be parsed.
        grammar_fragments = []
        help_texts = defaultdict(list)
        for processor in procs:
            handle_tree = getattr(processor, "HANDLE_TREE", None)
            if handle_tree:
                # Check whether this processor supplies
                # a query grammar fragment
                fragment = getattr(processor, "GRAMMAR", None)
                if fragment and isinstance(fragment, str):
                    # Looks legit: add it to our list
                    grammar_fragments.append(fragment)
            # Collect topic lemmas and corresponding help text functions
            topic_lemmas = getattr(processor, "TOPIC_LEMMAS", None)
            if topic_lemmas:
                help_text_func = getattr(processor, "help_text", None)
                # If topic lemmas are given, a help_text function
                # should also be present
                assert help_text_func is not None
                if help_text_func is not None:
                    for lemma in topic_lemmas:
                        help_texts[lemma].append(help_text_func)
        cls._help_texts = help_texts

        # Coalesce the grammar additions from the fragments
        grammar_additions = "\n".join(grammar_fragments)
        # Initialize a singleton parser instance for queries,
        # with the nonterminal 'QueryRoot' as the grammar root
        cls._parser = QueryParser(grammar_additions)

    @staticmethod
    def _parse(toklist):
        """ Parse a token list as a query """
        bp = Query._parser
        num_sent = 0
        num_parsed_sent = 0
        rdc = Reducer(bp.grammar)
        trees = dict()
        sent = []

        for t in toklist:
            if t[0] == TOK.S_BEGIN:
                sent = []
            elif t[0] == TOK.S_END:
                slen = len(sent)
                if not slen:
                    continue
                num_sent += 1
                # Parse the accumulated sentence
                num = 0
                try:
                    # Parse the sentence
                    forest = bp.go(sent)
                    if forest is not None:
                        num = Fast_Parser.num_combinations(forest)
                        if num > 1:
                            # Reduce the resulting forest
                            forest = rdc.go(forest)
                except ParseError:
                    forest = None
                if num > 0:
                    num_parsed_sent += 1
                    # Obtain a text representation of the parse tree
                    trees[num_sent] = ParseForestDumper.dump_forest(forest)

            elif t[0] == TOK.P_BEGIN:
                pass
            elif t[0] == TOK.P_END:
                pass
            else:
                sent.append(t)

        result = dict(num_sent=num_sent, num_parsed_sent=num_parsed_sent)
        return result, trees

    def parse(self, result):
        """ Parse the query from its string, returning True if valid """
        self._tree = None  # Erase previous tree, if any
        self._error = None  # Erase previous error, if any
        self._qtype = None  # Erase previous query type, if any
        self._key = None
        self._toklist = None

        q = self._query.strip()
        if not q:
            self.set_error("E_EMPTY_QUERY")
            return False

        toklist = tokenize(q,
                           auto_uppercase=self._auto_uppercase and q.islower())
        toklist = list(toklist)
        # The following seems not to be needed and may complicate things
        # toklist = list(recognize_entities(toklist, enclosing_session=self._session))

        actual_q = correct_spaces(" ".join(t.txt for t in toklist if t.txt))
        if actual_q:
            actual_q = actual_q[0].upper() + actual_q[1:]
            if not any(actual_q.endswith(s) for s in ("?", ".", "!")):
                actual_q += "?"

        # Update the beautified query string, as the actual_q string
        # probably has more correct capitalization
        self.set_beautified_query(actual_q)

        if Settings.DEBUG:
            # Log the query string as seen by the parser
            print("Query is: '{0}'".format(actual_q))

        parse_result, trees = Query._parse(toklist)

        if not trees:
            # No parse at all
            self.set_error("E_NO_PARSE_TREES")
            return False

        result.update(parse_result)

        if result["num_sent"] != 1:
            # Queries must be one sentence
            self.set_error("E_MULTIPLE_SENTENCES")
            return False
        if result["num_parsed_sent"] != 1:
            # Unable to parse the single sentence
            self.set_error("E_NO_PARSE")
            return False
        if 1 not in trees:
            # No sentence number 1
            self.set_error("E_NO_FIRST_SENTENCE")
            return False
        # Looks good
        # Store the resulting parsed query as a tree
        tree_string = "S1\n" + trees[1]
        if Settings.DEBUG:
            print(tree_string)
        self._tree = Tree()
        self._tree.load(tree_string)
        # Store the token list
        self._toklist = toklist
        return True

    def execute_from_plain_text(self):
        """ Attempt to execute a plain text query, without having to parse it """
        if not self._query:
            return False
        for processor in self._processors:
            handle_plain_text = getattr(processor, "handle_plain_text", None)
            if handle_plain_text is not None:
                # This processor has a handle_plain_text function:
                # call it
                if handle_plain_text(self):
                    # Successfully handled: we're done
                    return True
        return False

    def execute_from_tree(self):
        """ Execute the query contained in the previously parsed tree;
            return True if successful """
        if self._tree is None:
            self.set_error("E_QUERY_NOT_PARSED")
            return False
        for processor in self._processors:
            self._error = None
            self._qtype = None
            # If a processor defines HANDLE_TREE and sets it to
            # a truthy value, it wants to handle parse trees
            handle_tree = getattr(processor, "HANDLE_TREE", None)
            if handle_tree:
                # Process the tree, which has only one sentence
                self._tree.process(self._session, processor, query=self)
                if self._answer and self._error is None:
                    # The processor successfully answered the query
                    return True
        # No processor was able to answer the query
        return False

    def last_answer(self, *, within_minutes=5):
        """ Return the last answer given to this client, by default
            within the last 5 minutes (0=forever) """
        if not self._client_id:
            # Can't find the last answer if no client_id given
            return None
        # Find the newest non-error, no-repeat query result for this client
        q = (self._session.query(QueryRow.answer, QueryRow.voice).filter(
            QueryRow.client_id == self._client_id).filter(
                QueryRow.qtype != "Repeat").filter(QueryRow.error == None))
        if within_minutes > 0:
            # Apply a timestamp filter
            since = datetime.utcnow() - timedelta(minutes=within_minutes)
            q = q.filter(QueryRow.timestamp >= since)
        # Sort to get the newest query that fulfills the criteria
        last = q.order_by(desc(QueryRow.timestamp)).limit(1).one_or_none()
        return None if last is None else tuple(last)

    def fetch_context(self, *, within_minutes=10):
        """ Return the context from the last answer given to this client,
            by default within the last 10 minutes (0=forever) """
        if not self._client_id:
            # Can't find the last answer if no client_id given
            return None
        # Find the newest non-error, no-repeat query result for this client
        q = (self._session.query(QueryRow.context).filter(
            QueryRow.client_id == self._client_id).filter(
                QueryRow.qtype != "Repeat").filter(QueryRow.error == None))
        if within_minutes > 0:
            # Apply a timestamp filter
            since = datetime.utcnow() - timedelta(minutes=within_minutes)
            q = q.filter(QueryRow.timestamp >= since)
        # Sort to get the newest query that fulfills the criteria
        ctx = q.order_by(desc(QueryRow.timestamp)).limit(1).one_or_none()
        if ctx is None:
            return None
        # This function normally returns a dict that has been decoded from JSON
        return None if ctx is None else ctx[0]

    @property
    def query(self):
        return self._query

    @property
    def query_lower(self):
        return self._query.lower()

    @property
    def beautified_query(self):
        """ Return the query string that will be reflected back to the client """
        return self._beautified_query

    def set_beautified_query(self, q):
        """ Set the query string that will be reflected back to the client """
        self._beautified_query = (
            q.replace("embla", "Embla").replace("miðeind", "Miðeind").replace(
                "Guðni Th ", "Guðni Th. ")  # By presidential request :)
        )

    def lowercase_beautified_query(self):
        """ If we know that no uppercase words occur in the query,
            except the initial capital, this function can be called
            to adjust the beautified query string accordingly. """
        self.set_beautified_query(self._beautified_query.capitalize())

    def query_is_command(self):
        """ Called from a query processor if the query is a command, not a question """
        # Put a period at the end of the beautified query text
        # instead of a question mark
        if self._beautified_query.endswith("?"):
            self._beautified_query = self._beautified_query[:-1] + "."

    @property
    def expires(self):
        """ Expiration time stamp for this query answer, if any """
        return self._expires

    def set_expires(self, ts):
        self._expires = ts

    @property
    def url(self):
        """ URL answer associated with this query """
        return self._url

    def set_url(self, u):
        self._url = u

    @property
    def source(self):
        """ Source of answer to this query """
        return self._source

    def set_source(self, s):
        self._source = s

    @property
    def location(self):
        return self._location

    @property
    def token_list(self):
        return self._toklist

    def set_qtype(self, qtype):
        """ Set the query type ('Person', 'Title', 'Company', 'Entity'...) """
        self._qtype = qtype

    def set_answer(self, response, answer, voice_answer=None):
        """ Set the answer to the query """
        # Detailed response (this is usually a dict)
        self._response = response
        # Single best answer, as a displayable string
        self._answer = answer
        # A voice version of the single best answer
        self._voice_answer = voice_answer

    def set_key(self, key):
        """ Set the query key, i.e. the term or string used to execute the query """
        # This is for instance a person name in nominative case
        self._key = key

    def set_error(self, error):
        """ Set an error result """
        self._error = error

    def qtype(self):
        """ Return the query type """
        return self._qtype

    @property
    def is_voice(self):
        """ Return True if this is a voice query """
        return self._voice

    def response(self):
        """ Return the detailed query answer """
        return self._response

    def answer(self):
        """ Return the 'single best' displayable query answer """
        return self._answer

    def voice_answer(self):
        """ Return a voice version of the 'single best' answer, if any """
        return self._voice_answer

    def key(self):
        """ Return the query key """
        return self._key

    def error(self):
        """ Return the query error, if any """
        return self._error

    def set_context(self, ctx):
        """ Set a query context that will be stored and made available
            to the next query from the same client """
        self._context = ctx

    @property
    def context(self):
        """ Return the context that has been set by self.set_context() """
        return self._context

    @classmethod
    def try_to_help(cls, query, result):
        """ Attempt to help the user in the case of a failed query,
            based on lemmas in the query string """
        # Collect a set of lemmas that occur in the query string
        lemmas = set()
        with BIN_Db.get_db() as db:
            for token in query.lower().split():
                if token.isalpha():
                    m = db.meanings(token)
                    if not m:
                        # Try an uppercase version, just in case (pun intended)
                        m = db.meanings(token.capitalize())
                    if m:
                        lemmas |= set(mm.stofn.lower() for mm in m)
        # Collect a list of potential help text functions from the query modules
        help_text_funcs = []
        for lemma in lemmas:
            help_text_funcs.extend([
                (lemma, help_text_func)
                for help_text_func in cls._help_texts.get(lemma, [])
            ])
        if help_text_funcs:
            # Found at least one help text func matching a lemma in the query
            # Select a function at random and invoke it with the matched
            # lemma as a parameter
            lemma, help_text_func = random.choice(help_text_funcs)
            result["answer"] = result["voice"] = help_text_func(lemma)
            result["valid"] = True

    def execute(self):
        """ Check whether the parse tree is describes a query, and if so,
            execute the query, store the query answer in the result dictionary
            and return True """
        if Query._parser is None:
            Query.init_class()
        # By default, the result object contains the 'raw' query
        # string (the one returned from the speech-to-text processor)
        # as well as the beautified version of that string - which
        # usually starts with an uppercase letter and has a trailing
        # question mark (or other ending punctuation).
        result = dict(q_raw=self.query, q=self.beautified_query)
        # First, try to handle this from plain text, without parsing:
        # shortcut to a successful, plain response
        if not self.execute_from_plain_text():
            if not self.parse(result):
                # Unable to parse the query
                if Settings.DEBUG:
                    print("Unable to parse query, error {0}".format(
                        self.error()))
                result["error"] = self.error()
                result["valid"] = False
                return result
            if not self.execute_from_tree():
                # This is a query, but its execution failed for some reason:
                # return the error
                # if Settings.DEBUG:
                #     print("Unable to execute query, error {0}".format(q.error()))
                result["error"] = self.error() or "E_UNABLE_TO_EXECUTE_QUERY"
                result["valid"] = True
                return result
        # Successful query: return the answer in response
        if self._answer:
            result["answer"] = self._answer
        if self._voice and self._voice_answer:
            # This is a voice query and we have a voice answer to it
            result["voice"] = self._voice_answer
        if self._voice:
            # Optimize the response to voice queries:
            # we don't need detailed information about alternative
            # answers or their sources
            result["response"] = dict(answer=self._answer or "")
        else:
            # Return a detailed response if not a voice query
            result["response"] = self._response
        # Re-assign the beautified query string, in case the query processor modified it
        result["q"] = self.beautified_query
        # ...and the query type, as a string ('Person', 'Entity', 'Title' etc.)
        result["qtype"] = qt = self.qtype()
        # ...and the key used to retrieve the answer, if any
        result["key"] = self.key()
        # ...and a URL, if any has been set by the query processor
        if self.url:
            result["open_url"] = self.url
        # .. and the source, if set by query processor
        if self.source:
            result["source"] = self.source
        if not self._voice and qt == "Person":
            # For a person query, add an image (if available)
            img = get_image_url(self.key(), enclosing_session=self._session)
            if img is not None:
                result["image"] = dict(
                    src=img.src,
                    width=img.width,
                    height=img.height,
                    link=img.link,
                    origin=img.origin,
                    name=img.name,
                )
        result["valid"] = True
        if Settings.DEBUG:
            # Dump query results to the console
            def converter(o):
                """ Ensure that datetime is output in ISO format to JSON """
                if isinstance(o, datetime):
                    return o.isoformat()[0:16]
                return None

            print("{0}".format(
                json.dumps(result,
                           indent=3,
                           ensure_ascii=False,
                           default=converter)))
        return result
Beispiel #39
0
    def parse(self, result):
        """ Parse the query from its string, returning True if valid """
        self._tree = None  # Erase previous tree, if any
        self._error = None  # Erase previous error, if any
        self._qtype = None  # Erase previous query type, if any
        self._key = None
        self._toklist = None

        q = self._query.strip()
        if not q:
            self.set_error("E_EMPTY_QUERY")
            return False

        toklist = tokenize(q,
                           auto_uppercase=self._auto_uppercase and q.islower())
        toklist = list(toklist)
        # The following seems not to be needed and may complicate things
        # toklist = list(recognize_entities(toklist, enclosing_session=self._session))

        actual_q = correct_spaces(" ".join(t.txt for t in toklist if t.txt))
        if actual_q:
            actual_q = actual_q[0].upper() + actual_q[1:]
            if not any(actual_q.endswith(s) for s in ("?", ".", "!")):
                actual_q += "?"

        # Update the beautified query string, as the actual_q string
        # probably has more correct capitalization
        self.set_beautified_query(actual_q)

        if Settings.DEBUG:
            # Log the query string as seen by the parser
            print("Query is: '{0}'".format(actual_q))

        parse_result, trees = Query._parse(toklist)

        if not trees:
            # No parse at all
            self.set_error("E_NO_PARSE_TREES")
            return False

        result.update(parse_result)

        if result["num_sent"] != 1:
            # Queries must be one sentence
            self.set_error("E_MULTIPLE_SENTENCES")
            return False
        if result["num_parsed_sent"] != 1:
            # Unable to parse the single sentence
            self.set_error("E_NO_PARSE")
            return False
        if 1 not in trees:
            # No sentence number 1
            self.set_error("E_NO_FIRST_SENTENCE")
            return False
        # Looks good
        # Store the resulting parsed query as a tree
        tree_string = "S1\n" + trees[1]
        if Settings.DEBUG:
            print(tree_string)
        self._tree = Tree()
        self._tree.load(tree_string)
        # Store the token list
        self._toklist = toklist
        return True
Beispiel #40
0
def main(options):
    """Родительский процесс инициализирует настройки логера, порождает заданное
    количество фетчеров и воркеров, ложит начальную ссылку для запроса
    фетчером(начала работы всей системы), далее по заданной паузе просматривает
    содержимое очередей для ворекров\фетчеров и подаёт команду на мягкое завершение,
    если очереди пустые(интерпритация завершения работы системы) или пришел сигнал на завершение.
    """

    r = Redis(options.redis_host, options.redis_port)
    STOP = False
    logger.info('Cluster node process is started. Args: {}'.format(options))

    def graceful_stop(signum, frame):
        nonlocal STOP
        if STOP:
            # signal.signal(signal.SIGTERM, signal.SIG_DFL)
            # os.killpg(signal.SIGTERM)
            sys.exit(0)
        logger.info('Graceful stopping...')
        STOP = True

    signal.signal(signal.SIGINT, graceful_stop)
    signal.signal(signal.SIGTERM, graceful_stop)

    logger.info('Start {} fetchers'.format(options.fetcher_count))
    fetchers = []
    for i in range(options.fetcher_count):
        stop_flag = Value(c_bool, False)
        p = Process(target=fetcher,
                    name='Fetcher',
                    args=(options, options.fetcher_concurrent, stop_flag),
                    kwargs={'use_curl': options.use_curl})
        p.start()
        fetchers.append((p, stop_flag))

    logger.info('Start {} workers'.format(options.worker_count))
    workers = []
    for i in range(options.worker_count):
        stop_flag = Value(c_bool, False)
        w = Worker(options, stop_flag)
        w.start()
        workers.append((w, stop_flag))  # remove stop_flag, use from w

    Tree(r).add_root(options.url)

    logger.info(
        '=================================Start parsing================================='
    )
    start_time = time.time()
    r.lpush(cns.URL_QUEUE_KEY, options.url)
    while True:
        time.sleep(options.check_period)
        uq_size = r.llen(cns.URL_QUEUE_KEY)
        dq_size = r.llen(cns.DATA_QUEUE_KEY)
        logger.debug('Url queue size: {}; Data queue size: {}'.format(
            uq_size, dq_size))
        if STOP or not (
                uq_size or dq_size
        ):  #TODO при большом периоде проверки check_period придется ожидать
            for p, stop_flag in chain(fetchers, workers):
                stop_flag.value = True
            break

    logger.info('Waiting for a processes terminating')
    for p, stop_flag in chain(fetchers, workers):
        p.join(cns.TERMINATING_TIMEOUT)
    if not STOP:
        r.delete(
            *r.keys(cns.NAMESPACE +
                    '*'))  # чистка ключей только в пространстве имен парсера

    end_time = time.time()
    logger.info('End parsing. Duration: {}'.format(end_time - start_time))
Beispiel #41
0
    def load(filename, lower=False, sentid=0):
        '''now return a generator! use load().next() for singleton.
		   and read the last line as the gold tree -- TODO: optional!
		   and there is an empty line at the end
		'''

        file = getfile(filename)
        line = None
        total_time = 0
        num_sents = 0

        while True:

            start_time = time.time()
            ##'\tThe complicated language in ...\n"
            ## tag is often missing
            try:
                if line is None or line == "\n":
                    line = "\n"
                    while line == "\n":
                        line = file.readline()  # emulate seek
                tag, sent = line.split("\t")
            except:
                ## no more forests
                break

            num_sents += 1

            sent = sent.split()
            cased_sent = sent[:]
            if lower:
                sent = [w.lower()
                        for w in sent]  # mark johnson: lowercase all words
            num = int(file.readline())

            forest = Forest(num, sent, cased_sent, tag)
            forest.labelspans = {}
            forest.short_edges = {}

            delta = num_spu = 0
            for i in xrange(1, num + 1):

                ## '2\tDT* [0-1]\t1 ||| 1232=2 ...\n'
                ## node-based features here: wordedges, greedyheavy, word(1), [word(2)], ...
                line = file.readline()
                try:
                    keys, fields = line.split(" ||| ")
                except:
                    keys = line
                    fields = ""

                iden, labelspan, size = keys.split(
                    "\t")  ## iden can be non-ints
                size = int(size)

                fvector = FVector(fields)
                node = Node(iden, labelspan, size, fvector, sent)
                forest.add_node(node)

                if cache_same:
                    if labelspan in forest.labelspans:
                        node.same = forest.labelspans[labelspan]
                        node.fvector = node.same.fvector
                    else:
                        forest.labelspans[labelspan] = node

                for j in xrange(size):
                    is_oracle = False

                    ## '\t1 ||| 0=8.86276 1=2 3\n'
                    tails, fields = file.readline().strip().split(" ||| ")

                    if tails[0] == "*":  #oracle edge
                        is_oracle = True
                        tails = tails[1:]

                    tails = tails.split()  ## could be non-integers
                    tailnodes = []

                    for x in tails:
                        assert x in forest.nodes, "BAD TOPOL ORDER: node #%s is referred to " % x + \
                            "(in a hyperedge of node #%s) before being defined" % iden
                        ## topological ordering
                        tail = forest.nodes[x]
                        tailnodes.append(tail)

                    use_same = False
                    if fields[-1] == "~":
                        use_same = True
                        fields = fields[:-1]

                    fvector = FVector(fields)
                    edge = Hyperedge(node, tailnodes, fvector)

                    if cache_same:

                        short_edge = edge.shorter()
                        if short_edge in forest.short_edges:
                            edge.same = forest.short_edges[short_edge]
                            if use_same:
                                edge.fvector += edge.same.fvector
                        else:
                            forest.short_edges[short_edge] = edge

                    node.add_edge(edge)
                    if is_oracle:
                        node.oracle_edge = edge

                if node.sp_terminal():
                    node.word = node.edges[0].subs[0].word

            ## splitted nodes 12-3-4 => (12, 3, 4)
            tmp = sorted([(map(int, x.iden.split("-")), x)
                          for x in forest.nodeorder])
            forest.nodeorder = [x for (_, x) in tmp]

            forest.rehash()
            sentid += 1

            ##			print >> logs, "sent #%d %s, %d words, %d nodes, %d edges, loaded in %.2lf secs" \
            ##				  % (sentid, forest.tag, forest.len, num, forest.num_edges, time.time() - basetime)

            forest.root = node
            node.set_root(True)

            line = file.readline()

            if line is not None and line.strip() != "":
                if line[0] == "(":
                    forest.goldtree = Tree.parse(line.strip(),
                                                 trunc=True,
                                                 lower=False)
                    line = file.readline()
            else:
                line = None

            total_time += time.time() - start_time

            if num_sents % 100 == 0:
                print >> logs, "... %d sents loaded (%.2lf secs per sent) ..." \
                   % (num_sents, total_time/num_sents)

            yield forest

        Forest.load_time = total_time
        print >> logs, "%d forests loaded in %.2lf secs (avg %.2lf per sent)" \
           % (num_sents, total_time, total_time/num_sents)
Beispiel #42
0
if __name__ == '__main__':
    """
        Potrebno je:
        1. Inicijalizovati stablo
        2. Pozvati build_tree sa parametrima:
           stablo, veličina (63 za redovan, 127 za dodatni zadatak), rečnik sa ključevima za svaki karakter
           (char_keys za redovan, dodatni za dodatni zadatak)
        Ukoliko se enkoduje/dekoduje samo jedan znak/sekvenca dovoljno je pozvati search_tree/find_character
        uz parametre:
         - stablo, znak, rečnik (koji je korišćen za pravljenje stabla) za search_tree
         - stablo, sekvenca za find_character
        Ukoliko se enkoduje/dekoduje niz karaktera/kodovanih karaktera potrebno je pozvati encode/decode uz parametre:
         - stablo, niz karaktera, rečnik (koji je korišćen za pravljenje stabla) za encode
         - stablo, niz kodovanih karaktera
    """
    tree = Tree()

    build_tree(tree, 63, char_keys)

    inorder_tree_walk(tree)

    text = "JA SAM STUDENT. CH 9 +"
    print(f"Coding: {text}")
    coded = encode(tree, text, char_keys)
    print(f"Coded: {coded}")
    print()

    print(f"Decoding: {coded}")
    decoded = decode(tree, coded)
    print(f"Deoded: {decoded}")
    print()
Beispiel #43
0
def main(source_file, output_file, verbose, tuples, ast, decorated, bonus, mid):
	file = source_file

	if verbose:
		print('Creating lexical analyser...')
	analyser = LexicalAnalyser()

	if verbose:
		print('Loading categories...')
	analyser.load_categories(lexical_categories_path)

	if verbose:
		print('Generating tuples...')
	parsed_tuples = analyser.parse_input(file)

	if verbose:
		print('Creating syntax analyser...')
	syntax = SyntaxAnalyser()

	if verbose:
		print('Loading syntax rules...')
	syntax.load_rules(syntax_rules_path)
		
	if verbose:
		print('Generating syntax tree...')
	tree = syntax.create_tree(parsed_tuples)

	if verbose:
		print('Creating semantic analyser...')
	semantic = SemanticAnalyser()

	if verbose:
		print('Loading semantic rules...')
	semantic.load_rules(semantic_rules_path)

	if verbose:
		print('Decorating syntax tree...')
	semantic.check_type(tree)

	if verbose:
		print('Creating mid-code tuple generator...')
	generator = TupleGenerator()

	if verbose:
		print('Generating assembly tuples...')
	assembly_tuples = generator.create_tuples(tree)

	if tuples:
		print_tuples(parsed_tuples)

	if ast:
		print_node(tree, 0)

	if decorated:
		print_node(tree, 0, True)

	if mid:
		print_assembly_tuples(assembly_tuples)

	if bonus:
		tree = Tree()
		print(tree.tree)
Beispiel #44
0
 def reallyRedraw(self):
     Tree.reallyRedraw(self)
     if self.newTreeWidth > self.treeWidth:
         self.setTreeWidth(self.newTreeWidth)
         self.newTreeWidth = 0
         self.redraw()
Beispiel #45
0
    def fit(self,
            X,
            y,
            eta=0.01,
            num_boost_round=1000,
            max_depth=5,
            rowsample=0.8,
            colsample_bytree=0.8,
            colsample_bylevel=0.8,
            min_sample_split=10,
            loss="logisticloss",
            l2_regularization=1.0,
            gamma=0.1,
            num_thread=-1,
            eval_metric=None):
        """
        :param X: pandas.core.frame.DataFrame
        :param y: pandas.core.series.Series
        :param eta: learning rate
        :param num_boost_round: number of boosting round
        :param max_depth: max depth of each tree
        :param rowsample: row sample rate when building a tree
        :param colsample_bytree: column sample rate when building a tree
        :param colsample_bylevel: column sample rate when spliting each tree node,
                                  the number of features = total_features*colsample_bytree*colsample_bylevel
        :param min_sample_split: min number of samples in a leaf node
        :param loss: loss object
                     logisticloss,squareloss, or customize loss
        :param l2_regularization: lambda
        :param gamma: gamma
        :param seed: random seed
        :param num_thread: number of thread to parallel
        :param eval_metric: evaluation metric, provided: "accuracy"
        """
        self.eta = eta
        self.num_boost_round = num_boost_round
        self.max_depth = max_depth
        self.rowsample = rowsample
        self.colsample_bytree = colsample_bytree
        self.colsample_bylevel = colsample_bylevel
        self.l2_regularization = l2_regularization
        self.gamma = gamma
        self.min_sample_split = min_sample_split
        self.num_thread = num_thread
        self.eval_metric = eval_metric

        if loss == "logisticloss":
            self.loss = LogisticLoss(l2_regularization)
        elif loss == "squareloss":
            self.loss = SquareLoss(l2_regularization)
        else:
            try:
                self.loss = CustomizeLoss(loss, l2_regularization)
            except:
                raise NotImplementedError(
                    "loss should be 'logisticloss','squareloss', or customize loss function"
                )

        self.first_round_pred = y.mean()

        # Y stores label, y_pred, grad, hess
        Y = pd.DataFrame(y.values,
                         columns=['label'])  # only one column "label"
        Y['y_pred'] = self.first_round_pred
        Y['grad'] = self.loss.grad(Y.y_pred.values, Y.label.values)
        Y['hess'] = self.loss.hess(Y.y_pred.values, Y.label.values)

        for i in range(self.num_boost_round):
            # sample samples and features to train current tree
            data = X.sample(frac=self.colsample_bytree, axis=1)
            data = pd.concat([data, Y], axis=1)
            data = data.sample(frac=self.rowsample, axis=0)
            Y_selected = data[['label', 'y_pred', 'grad', 'hess']]
            X_selected = data.drop(['label', 'y_pred', 'grad', 'hess'], axis=1)

            # train current tree
            tree = Tree()
            tree.fit(X_selected,
                     Y_selected,
                     max_depth=self.max_depth,
                     colsample_bylevel=self.colsample_bylevel,
                     min_sample_split=self.min_sample_split,
                     l2_regularization=self.l2_regularization,
                     gamma=self.gamma,
                     num_thread=self.num_thread)

            # predict the whole dataset and update y_pred,grad,hess
            preds = tree.predict(X)
            Y['y_pred'] += self.eta * preds
            Y['grad'] = self.loss.grad(Y.y_pred.values, Y.label.values)
            Y['hess'] = self.loss.hess(Y.y_pred.values, Y.label.values)

            if self.eval_metric is not None:
                try:
                    mertric_func = get_metric(self.eval_metric)
                except:
                    raise NotImplementedError(
                        "The given eval_metric is not provided")
                metric_value = mertric_func(
                    self.loss.transform(Y.y_pred.values), Y.label.values)
                print "TGBoost round {iteration}, {eval_metric} is {metric_value}".format(
                    iteration=i,
                    eval_metric=self.eval_metric,
                    metric_value=metric_value)
            else:
                print "TGBoost round {iteration}"

            # update feature importance
            for k in tree.feature_importance.iterkeys():
                self.feature_importance[k] += tree.feature_importance[k]

            self.trees.append(tree)
Beispiel #46
0
 def redraw(self, event=None):
     Tree.redraw(self)
     if self.crosshairTk and event:
         self.move_cb(event)
class Algorithm:
    '''
    classdocs
    '''
    S = [
    ]  # Extracted unlabeled Sequence from File directly [timestamp , activity]
    T = dict()
    M = dict()
    Parents = dict()
    GivenConfidenceLevel = 0

    activitiesProb = dict()  # check to small letter
    traces = Tree()
    tracesLeafs = []
    root = None  # root of traces\cases tree [tree]
    startActivity = ''
    numOfCases = 0
    activitiesNotCheckBranches = dict()  # { activity:[event id]}

    def __init__(self, S, T, M, Predecessors, startActivity,
                 GivenConfidenceLevel):
        self.M = M
        self.Predecessors = Predecessors
        self.T = T
        self.S = S
        self.startActivity = startActivity
        self.traces.add_node(1, 1, 0, 0, 'start', 0, None)
        self.root = self.traces.get_root()
        self.calculate_activity_probability(self.S)
        self.GivenConfidenceLevel = GivenConfidenceLevel
        self.activitiesNotCheckBranches = dict()

    def trunc(self, number, digit=4):
        return (math.floor(number * pow(10, digit) + 0.5)) / pow(10, digit)

    '''Calculate probability of each activity withing giving sequence'''

    def calculate_activity_probability(self, sequence):
        sequnceLength = len(sequence)
        appearances = dict()
        for curr in sequence:
            if (not appearances.has_key(curr[1])):
                appearances[curr[1]] = 1.0
            else:
                appearances[curr[1]] = appearances[curr[1]] + 1.0
        for a in appearances:
            value = self.trunc(appearances[a] / sequnceLength, 4)
            self.activitiesProb[a] = value

    def distinct_possible_cases(self, possibleNodes):
        distinctNodes = []
        for n in possibleNodes:
            if (n not in distinctNodes):
                distinctNodes.append(n)
        return distinctNodes

    '''Explore all possible cases for each symbol'''

    def check_possible_branches_based_on_Model(self, eventActivity):  #symbol):
        possibleNodes = dict()  # {tree parent Node:[model predecessor node]}
        #eventActivity = symbol[1].lower()

        if (
                self.M.get(eventActivity) == None
        ):  # to check if activity is not exist in model[based on miner model], if so DCI ignores the activity
            print activity
            return possibleNodes

        if (eventActivity == self.startActivity
            ):  # if activity is the start activity
            possibleNodes[self.root] = None

        else:
            for l in self.tracesLeafs:  #self.traces.get_leafs(self.root):#contain last added nodes in tree
                node = l
                # check if the current node is marked as out of range for this activity before
                if (self.activitiesNotCheckBranches.has_key(eventActivity)):
                    if (node.eventIdentifier
                            in self.activitiesNotCheckBranches[eventActivity]):
                        continue
                # traverse branch from current node till parent
                while (node != self.root):
                    activity = node.activity  #.lower()
                    relation = self.M.get(activity).get(eventActivity)

                    #check if there is no relation object which will be rarel
                    if (relation == None):
                        print "Relation object is none :: between ", activity, "and ", eventActivity
                        break
                    # if relation is none,i wont traverse the branch , instead ignore it totally
                    elif (relation.lower() == "none"):
                        print "For Trace : this relation is none :: between ", activity, "and ", eventActivity
                        break
                    elif (relation.lower() == "xor"):
                        print "For Trace : this relation is XOR :: between ", activity, "and ", eventActivity
                        node = node.parent
                        continue

                    # atwal we a5teer part
                    elif (relation.lower() == "seq"):
                        # to be changed ::: after change predecessor list from BP object
                        # what implemented here has something wrong :(
                        print "For Trace : this relation is Sequence  :: between ", activity, "and ", eventActivity
                        arrPredecessor = self.Predecessors[eventActivity]
                        flag = 0
                        if (len(arrPredecessor) == 1):
                            possibleNodes[node] = None
                            break
                        for j in range(0, len(arrPredecessor)):
                            if (flag == 1):
                                break
                            for i in range(1, len(arrPredecessor)):
                                if i == j:
                                    continue
                                rel = self.M.get(arrPredecessor[j]).get(
                                    arrPredecessor[i])
                                if (rel == 'and'):
                                    f1 = self.traces.check_existance_in_branch(
                                        node, arrPredecessor[j])
                                    f2 = self.traces.check_existance_in_branch(
                                        node, arrPredecessor[i])
                                    if (f1 and f2):
                                        possibleNodes[node] = None
                                        flag = 1
                                        break
                                elif (rel == 'xor'):
                                    possibleNodes[node] = None
                        break

                    elif (relation.lower() == "and"):
                        print "For Trace : this relation is and :: between ", activity, "and ", eventActivity

                        isExist = self.traces.check_existance_in_branch(
                            node, eventActivity)
                        if (not isExist):
                            modelSeqNode = None
                            # to get predecessor of and gate
                            arrPredecessor = self.Predecessors[activity]
                            for p in arrParents:  # suppose to be 1 and only one node
                                modelSeqNode = self.traces.get_existed_activity_in_branch(
                                    node, p)
                            possibleNodes[node] = modelSeqNode
                        node = node.parent
                        continue
        return possibleNodes

    '''Heuristic calculation method [min, avg , max]'''

    def check_possible_branches_based_on_heuristics(self, symbol,
                                                    possibleNodes):
        symbolTimestamp = symbol[0]
        activity = symbol[1].lower()
        avgArr = []
        other = []
        metadataTime = self.T.get(activity)
        if (len(possibleNodes) == 0):
            return dict()
        for n in possibleNodes:
            node = n
            if (possibleNodes[n] !=
                    None):  # to get gate predecessor [and gate]
                node = possibleNodes[n]
            if (n == self.root):
                avgArr.append(n)
                break
            diff = 0
            if (symbolTimestamp.isdigit()):  # handling time units
                diff = int(symbolTimestamp) - int(node.timestamp)
            else:  # handling time format # to be changes take timeformat as an input
                try:
                    symbolTimestampDatetime = datetime.datetime.strptime(
                        symbolTimestamp, '%m/%d/%Y  %H:%M')
                    nodeDateTime = node.timestampDatetime  #datetime.datetime.strptime(node.timestampDatetime,'%m/%d/%Y  %I:%M:%S %p')
                    secDiff = symbolTimestampDatetime - nodeDateTime
                    diff = secDiff.total_seconds()  #/60
                except ValueError:
                    print "error in parsing datetime from string "
                    print ValueError

            avg = metadataTime[0]
            minR = avg - metadataTime[1] + 1
            maxR = avg + metadataTime[1] + 1
            if (diff == avg):
                avgArr.append(n)
            elif (diff > minR and diff < maxR):
                other.append(n)
            elif (diff > maxR):  # to handle out of range
                if (not self.activitiesNotCheckBranches.has_key(activity)):
                    self.activitiesNotCheckBranches[activity] = []
                self.activitiesNotCheckBranches[activity].append(
                    n.eventIdentifier)
        calcPool = {'avg': avgArr, 'other': other}
        return calcPool

    '''calculate given probability of  node per branch   -- check if all is avg'''

    def calculate_percentage(self, calcPool, highestPriority='avg'):
        calcPrecentage = dict()
        noOfPNodes = len(sum(calcPool.values(), []))
        #otherHeuristic = list(set(sum(calcPool.values(), [])) - set(calcPool[highestPriority]))  # to gather min and max set
        otherHeuristic = calcPool['other']
        noOfHighestPriority = len(calcPool[highestPriority])
        noOfOtherHeuristic = len(otherHeuristic)
        #total = 0
        totalNumber = noOfPNodes * noOfPNodes
        if (noOfPNodes == 1):
            totalNumber = 1  #2.0
        totalNumber *= 1.0
        if (noOfOtherHeuristic > 0):
            for aNode in calcPool[highestPriority]:
                calcPrecentage[aNode] = ((noOfPNodes + 1.0) / totalNumber)
                #total += calcPrecentage[aNode]
            for oNode in otherHeuristic:
                calcPrecentage[oNode] = (
                    (noOfPNodes -
                     (noOfHighestPriority * 1.0 / noOfOtherHeuristic * 1.0)) /
                    totalNumber) * 1.0
                #total += calcPrecentage[oNode]
        else:
            for a in calcPool[highestPriority]:
                calcPrecentage[a] = ((noOfPNodes) / totalNumber)
                #total += calcPrecentage[a]

        return calcPrecentage

    '''Return all possible nodes based on model and heuristic calculation'''

    def filter_possible_cases(self, symbol):

        possibleCasesFromM = self.check_possible_branches_based_on_Model(
            symbol[1].lower())
        if (len(possibleCasesFromM) == 0):
            return dict()
        calcPool = self.check_possible_branches_based_on_heuristics(
            symbol, possibleCasesFromM)
        possibleNodes = self.calculate_percentage(calcPool, 'avg')

        return possibleNodes

    '''building tree for given unlabeled event log and distribute the event to cases tree '''

    # add fun to remove all keys that have 1 branch from dic
    def build_branches_tree(self):
        eventIdentifier = 0
        labelCaseId = 1
        for symbol in self.S:
            dictionary = self.filter_possible_cases(symbol)
            numPossibleBranches = len(dictionary)
            eventIdentifier = eventIdentifier + 1
            print "event id", eventIdentifier
            activity = symbol[1].lower()
            for n in dictionary:
                caseId = n.caseId
                if (caseId == 0):
                    caseId = labelCaseId
                    labelCaseId = labelCaseId + 1
                percentage = dictionary[n]
                timestamp = 0
                timestampDatetime = None
                if (symbol[0].isdigit()):
                    timestamp = symbol[0]
                else:
                    try:
                        timestampDatetime = datetime.datetime.strptime(
                            symbol[0], '%m/%d/%Y  %H:%M')
                        #timestamp=timestampDatetime.hour
                        timestamp = eventIdentifier
                    except ValueError:
                        print "error in parsing datetime from string "
                        print ValueError
                symbolNode = self.traces.add_node(percentage,
                                                  1.0 / numPossibleBranches,
                                                  eventIdentifier, timestamp,
                                                  activity, caseId, n,
                                                  timestampDatetime)
                if (n in self.tracesLeafs):
                    self.tracesLeafs.remove(n)
                if (symbolNode not in self.tracesLeafs):
                    self.tracesLeafs.append(symbolNode)

    def getBranch(self, branches, caseId):
        for b in range(len(branches) - 1, -1, -1):  #branches:
            if (branches[b].caseId == caseId):
                return branches[b]
        return None

    def apply_algorithm(self):
        print "Algorithm version : Building CTree only "
        print "build cases tree"
        start_time_build_tree = time.clock()
        self.build_branches_tree()
        self.numOfCases = len(self.root.children)
        end_time_build_tree = time.clock()
        print "Algorithm execution time time_build_tree :  %s seconds " % (
            end_time_build_tree - start_time_build_tree)

        print "number of cases in given event log : ", self.numOfCases
        self.traces.display(self.root)
                    form_data[input_val] = ''
                else:
                    form_data[input_val] = message['values'][i][0]
            form_data['action'] = 'next'
            form_data['item[]'] = input_item
            response = self.parse_post_url(cookie, form_data)
        return response


if __name__ == '__main__':
    flag = FLAG
    ai_12348 = SpiderAi12348()
    cookie = conf.get_session()
    response, cookie = ai_12348.parse_url(cookie)
    if flag:
        tree = Tree(FILENAME)
        html_content = response['c']
        iw = response['iw']
        message = ai_12348.question_page(html_content, iw)
    else:
        tree = Tree(FILENAME, follow_pre=True)
        path = tree.next_path()
        print('==========', path)

    while True:
        if flag:
            path = tree.construct(message)
            print(path)
        while not path:
            message = ai_12348.process_results(response)
            print('此时的message:', message)
Beispiel #49
0
    def update_plan(self,
                    x0,
                    sample_space,
                    goal_bias=0,
                    guide=None,
                    xrand_gen=None,
                    pruning=True,
                    finish_on_goal=False,
                    specific_time=None):
        """
        A new tree is grown from the seed x0 in an attempt to plan
        a path to the goal. The returned path can be accessed with
        the interpolator functions get_state(t) and get_effort(t).

        The tree is motivated by uniform random samples in the over
        the given sample_space. The sample_space is a list of n tuples
        where n is the number of states; [(min1, max1), (min2, max2)...].

        The goal_bias is the fraction of the time the goal is sampled.
        It can be a scalar from 0 (none of the time) to 1 (all the time)
        or a list of scalars corresponding to each state dimension.

        Alternatively, you can give a function xrand_gen which takes the
        current planner instance (self) and outputs the random sample state.
        Doing this will override both sample_space and goal_bias, which you
        can set to arbitrary values only if you provide an xrand_gen function.

        Or, instead of a function, you can set xrand_gen to a single integer
        1 or greater, which will act as the number of tries allowed for
        finding a feasible random sample in the default random sampling
        routine. (Leaving the default None will set the limit to 10 tries).

        After min_time seconds, the fastest available path from x0 to
        the current goal is returned and the functions get_state(t)
        and get_effort(t) are modified to interpolate this new path.

        If no path was found yet, the search continues until max_time or
        until the node limit is breached. After the limit, a warning is
        printed and the path that gets nearest to the guide is
        used instead. If guide is left None, it defaults to goal.

        If pruning is True, then nodes can be marked as "ignore" during
        growth. Right now, only nodes on a completed path are ignored.

        If finish_on_goal is set to True, once the plan makes it to the goal
        region (goal plus buffer), it will attempt to steer one more path
        directly into the exact goal. Can fail for nonholonomic systems.

        If you want this update_plan to plan for some specific amount of
        time (instead of using the global min_time and max_time), pass it
        in as specific_time in seconds.

        This function returns True if it finished fully, or False if it was
        haulted. It can hault if it is killed or if the tree exceeds max_nodes,
        or if no goal has been set yet.

        """
        # Safety first!
        x0 = np.array(x0, dtype=np.float64)
        if self.goal is None:
            print("No goal has been set yet!")
            self.get_state = lambda t: x0
            self.get_effort = lambda t: np.zeros(self.ncontrols)
            return False

        # Store timing
        if specific_time is None:
            min_time = self.min_time
            max_time = self.max_time
        else:
            min_time = specific_time
            max_time = specific_time

        # Reset the tree
        self.tree = Tree(x0, self.lqr(x0, np.zeros(self.ncontrols)))
        ignores = np.array([])

        # If not given an xrand_gen function, make the standard one
        if xrand_gen is None or type(xrand_gen) is int:

            # Properly cast the given goal bias
            if goal_bias is None:
                goal_bias = [0] * self.nstates
            elif hasattr(goal_bias, '__contains__'):
                if len(goal_bias) != self.nstates:
                    raise ValueError(
                        "Expected goal_bias to be scalar or have same length as state."
                    )
            else:
                goal_bias = [goal_bias] * self.nstates

            # Set the number of tries for sample feasibility
            if xrand_gen > 0:
                tries_limit = xrand_gen
            else:
                tries_limit = 10

            # Properly cast the given sample space and extract statistics
            sample_space = np.array(sample_space, dtype=np.float64)
            if sample_space.shape != (self.nstates, 2):
                raise ValueError(
                    "Expected sample_space to be list of nstates tuples.")
            sampling_centers = np.mean(sample_space, axis=1)
            sampling_spans = np.diff(sample_space).flatten()

            # Standard sampling
            def xrand_gen(planner):
                tries = 0
                while tries < tries_limit:
                    xrand = sampling_centers + sampling_spans * (
                        np.random.sample(self.nstates) - 0.5)
                    for i, choice in enumerate(
                            np.greater(goal_bias, np.random.sample())):
                        if choice:
                            xrand[i] = self.goal[i]
                    if self.constraints.is_feasible(xrand,
                                                    np.zeros(self.ncontrols)):
                        return xrand
                    tries += 1
                return xrand

        # Otherwise, use given sampling function
        else:
            if not hasattr(xrand_gen, '__call__'):
                raise ValueError(
                    "Expected xrand_gen to be None, an integer >= 1,  or a function."
                )

        # Store guide state
        if guide is None:
            self.xguide = np.copy(self.goal)
        else:
            self.xguide = np.array(guide, dtype=np.float64)

        # Loop managers
        if self.printing:
            print("\n...planning...")
        self.plan_reached_goal = False
        self.T = np.inf
        time_elapsed = 0
        time_start = self.sys_time()

        # Planning loop!
        while True:

            # Random sample state
            xrand = xrand_gen(self)

            # The "nearest" node to xrand has the least cost-to-go of all nodes
            if pruning:
                nearestIDs = np.argsort(self._costs_to_go(xrand))
                nearestID = nearestIDs[0]
                for ID in nearestIDs:
                    if ID not in ignores:
                        nearestID = ID
                        break
            else:
                nearestID = np.argmin(self._costs_to_go(xrand))

            # Candidate extension to the tree
            xnew_seq, unew_seq = self._steer(nearestID,
                                             xrand,
                                             force_arrive=False)

            # If steer produced any feasible results, extend tree
            if len(xnew_seq) > 0:

                # Add the new node to the tree
                xnew = np.copy(xnew_seq[-1])
                self.tree.add_node(nearestID, xnew,
                                   self.lqr(xnew, np.copy(unew_seq[-1])),
                                   xnew_seq, unew_seq)

                # Check if the newest node reached the goal region
                if self._in_goal(xnew):

                    # Raise flag
                    self.plan_reached_goal = True

                    # Climb tree to construct sequence of states for this path
                    node_seq = self.tree.climb(self.tree.size - 1)
                    x_seq, u_seq = self.tree.trajectory(node_seq)

                    # Ignore nodes on any succeeded path
                    ignores = np.unique(np.concatenate((ignores, node_seq)))

                    # Expected time to complete this plan
                    T = len(x_seq) * self.dt

                    # Retain this plan if it is faster than the previous one
                    if T < self.T:
                        self.T = T
                        self.node_seq = node_seq
                        self.x_seq = x_seq
                        self.u_seq = u_seq
                        self.t_seq = np.arange(len(self.x_seq)) * self.dt
                        if self.printing:
                            print("Found plan at elapsed time: {} s".format(
                                np.round(time_elapsed, 6)))

            # For checking if we should stop planning
            time_elapsed = self.sys_time() - time_start

            # Abrupt termination
            if self.killed:
                break

            # Close-out for reached-goal
            elif self.plan_reached_goal and time_elapsed >= min_time:
                if finish_on_goal:
                    # Steer to exact goal
                    xgoal_seq, ugoal_seq = self._steer(self.node_seq[-1],
                                                       self.goal,
                                                       force_arrive=True)
                    # If it works, tack it onto the plan
                    if len(xgoal_seq) > 0:
                        self.tree.add_node(self.node_seq[-1], self.goal, None,
                                           xgoal_seq, ugoal_seq)
                        self.node_seq.append(self.tree.size - 1)
                        self.x_seq.extend(xgoal_seq)
                        self.u_seq.extend(ugoal_seq)
                        self.t_seq = np.arange(len(self.x_seq)) * self.dt
                # Over and out!
                if self.printing:
                    print("Tree size: {0}\nETA: {1} s".format(
                        self.tree.size, np.round(self.T, 2)))
                self._prepare_interpolators()
                break

            # Close-out for didn't-reach-goal
            elif time_elapsed >= max_time or self.tree.size > self.max_nodes:
                # Find closest node to guide state
                Sguide = self.lqr(self.xguide, np.zeros(self.ncontrols))[0]
                for i, g in enumerate(self.constraints.goal_buffer):
                    if np.isinf(g):
                        Sguide[:, i] = 0
                guide_diffs = self.erf_v(self.xguide, self.tree.state)
                closestID = np.argmin(
                    np.sum(np.tensordot(guide_diffs, Sguide, axes=1) *
                           guide_diffs,
                           axis=1))
                self.node_seq = self.tree.climb(closestID)
                # Construct plan
                self.x_seq, self.u_seq = self.tree.trajectory(self.node_seq)
                self.T = len(self.x_seq) * self.dt
                self.t_seq = np.arange(len(self.x_seq)) * self.dt
                # Over and out!
                if self.printing:
                    print("Didn't reach goal.\nTree size: {0}\nETA: {1} s".
                          format(self.tree.size, np.round(self.T, 2)))
                self._prepare_interpolators()
                break

        if self.killed or self.tree.size > self.max_nodes:
            if self.printing:
                print("Plan update terminated abruptly!")
            self.killed = False
            return False
        else:
            return True
Beispiel #50
0
from game_board import GameBoard
from tree import Tree
from character import Character
from direction import Direction

game_board = GameBoard()
for i in range(9):
    tree = Tree(game_board)

player = Character(game_board)

print(player.location)
user_dir = input("What direction would you like to move?").lower()
enum_dir = None
if (user_dir == "north"):
    enum_dir = Direction.NORTH
elif (user_dir == "south"):
    enum_dir = Direction.SOUTH
elif (user_dir == "west"):
    enum_dir = Direction.WEST
elif (user_dir == "east"):
    enum_dir = Direction.EAST

user_distance = int(input("How far would you like to move?"))
player.move(enum_dir, user_distance)
print(player.location)
 def deploy(self, id):
     path = self.get_item_path(self.clicked_item)
     self.clicked_item[9] = self.parse_path(self.clicked_item[3], path)
     Tree.deploy(self, id)
Beispiel #52
0
def read_dataset(filename):
    return [
        Tree.from_sexpr(line.strip()) for line in codecs.open(filename, "r")
    ]
Beispiel #53
0
class Foo(QtGui.QMainWindow):
    def __init__(self):
        super(Foo, self).__init__()
        self.initUI()

    def initUI(self):
        config = Foo.readConfig('options')
        self.timeOut = -1
        self.radio = False
        self.statusBar().showMessage('Ready')
        self.createMenu()
        self.setWindowTitle("Foo.cd")

        self.player = Player(Foo.readConfig('audio'))
        self.player.bus.connect('message::eos', self.stop)
        self.player.bus.connect('message::duration-changed',
                                self.onDurationChanged)

        self.tree = Tree(self, config['tree_order'])
        self.tree.addSongs.connect(self.addSongsFromTree)
        self.tree.customContextMenuRequested.connect(self.tmpTag)

        if not self.radio:
            self.table = Table(self, config)
            self.handlerATF = self.player.playbin.connect(
                "about-to-finish", self.onAboutToFinish)
            self.table.runAction.connect(self.tableAction)
        else:
            configRadio = Foo.readConfigRadios()
            self.table = TableRadio(self, configRadio)
            self.table.runAction.connect(self.tableAction)
            self.handlerT = self.player.bus.connect('message::tag',
                                                    self.table.onTag)

        self.playbackButtons = PlaybackButtons(None)
        self.playbackButtons.buttonPlay.clicked.connect(self.toggleSong)
        self.playbackButtons.buttonStop.clicked.connect(self.stop)
        self.playbackButtons.buttonPrev.clicked.connect(self.previous)
        self.playbackButtons.buttonNext.clicked.connect(self.next)

        self.volumeSlider = VolumeSlider(self)
        self.volumeSlider.sliderMoved.connect(self.player.setVolume)
        self.scrollSlider = ScrollSlider(self)
        self.scrollSlider.sliderPressed.connect(self.player.toggle)
        self.scrollSlider.sliderReleased.connect(self.player.toggle)
        self.scrollSlider.sliderMoved.connect(self.player.seek)

        self.pixmap = Image(self, config['cover_names'], config['extensions'])

        # Album cover connections
        self.tree.selectionModel().selectionChanged.connect(
            lambda: self.pixmap.onSelectionChanged(self.tree.getChildren()[0].
                                                   get('file', None)))
        self.table.selectionModel().selectionChanged.connect(
            lambda: self.pixmap.onSelectionChanged(self.table.getSelection().
                                                   get('file', None)))

        self.searchArea = SearchArea(self)
        self.searchArea.searchLine.returnPressed.connect(self.startSearch)

        self.playbackButtons.addWidget(self.volumeSlider)
        self.playbackButtons.addWidget(self.scrollSlider)

        splitterLeftRight = QtGui.QSplitter()
        self.splitterTopBottom = QtGui.QSplitter(Qt.Vertical, self)

        self.infoFrame = QtGui.QFrame()
        infoLayout = QtGui.QVBoxLayout()
        infoLayout.setContentsMargins(0, 0, 0, 0)
        infoLayout.addLayout(self.playbackButtons)
        infoLayout.addWidget(self.pixmap)
        self.infoFrame.setLayout(infoLayout)

        libLayout = QtGui.QVBoxLayout()
        libLayout.setContentsMargins(0, 0, 0, 0)
        libLayout.addWidget(self.tree)
        libLayout.addLayout(self.searchArea)
        libFrame = QtGui.QFrame()
        libFrame.setLayout(libLayout)

        self.splitterTopBottom.addWidget(self.table)
        self.splitterTopBottom.addWidget(self.infoFrame)
        self.splitterTopBottom.setStretchFactor(0, 1)
        #self.splitterTopBottom.setStretchFactor(1,0)

        splitterLeftRight.addWidget(libFrame)
        splitterLeftRight.addWidget(self.splitterTopBottom)
        splitterLeftRight.setStretchFactor(0, 2)
        splitterLeftRight.setStretchFactor(1, 3)

        mainLayout = QtGui.QGridLayout()
        mainLayout.setContentsMargins(4, 4, 4, 4)
        mainLayout.addWidget(splitterLeftRight)

        dummyWidget = QtGui.QWidget()
        dummyWidget.setLayout(mainLayout)
        self.setCentralWidget(dummyWidget)

        self.setTabOrder(self.tree, self.table)

        dictShortcuts = self.readConfig('shortcuts')
        modifier = dictShortcuts['modifier'] + '+'
        self.shortQuit = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['quit']), self,
            self.close)
        self.shortStop = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['stop']), self,
            self.stop)
        self.shortPlayPause = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['play_pause']), self,
            self.toggleSong)
        self.shortSongPrevious = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['previous']), self,
            self.previous)
        self.shortSongNext = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['next']), self,
            self.next)
        self.shortVolDown = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['volume_down']), self,
            self.volumeSlider.decr)
        self.shortVolUp = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['volume_up']), self,
            self.volumeSlider.incr)
        self.shortRadioMode = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['radio_mode']), self,
            self.toggleRadio)
        self.shortEqualizer = QtGui.QShortcut(
            QtGui.QKeySequence(modifier + dictShortcuts['equalizer']), self,
            self.openEqualizer)

        thread = QtCore.QThread(self)
        thread.worker = WorkThreadPipe()
        thread.worker.moveToThread(thread)
        thread.started.connect(thread.worker.process)
        thread.worker.hotKey.connect(self.onHotKey)
        thread.worker.finished.connect(thread.quit)
        thread.worker.finished.connect(thread.worker.deleteLater)
        thread.finished.connect(thread.deleteLater)
        thread.start()

        self.show()

    def keyReleaseEvent(self, event):
        if event.key() == Qt.Key_Alt:
            self.menuBar().setVisible(not self.menuBar().isVisible())
        else:
            QWidget.keyPressEvent(self, event)

    # Triggered by player end of stream event
    # or called by hand to stop the stream
    def stop(self, bus=None, msg=None):
        self.player.stop()
        self.scrollSlider.setValue(0)
        self.table.displayPlayToStop()
        self.stopStatusEmission('Ready')

    def previous(self):
        if self.table.playingId > 0:
            self.player.stop()
            self.table.playingId -= 1
            self.player.add(self.table.model().item(self.table.playingId,
                                                    0).data()['file'])
            self.player.play()

    def next(self):
        if self.table.model().rowCount() - 1 > self.table.playingId:
            self.player.stop()
            self.table.playingId += 1
            self.player.add(self.table.model().item(self.table.playingId,
                                                    0).data()['file'])
            self.player.play()

    def toggleSong(self):
        state = self.player.playbin.get_state(Gst.State.NULL)
        if state[1] == Gst.State.PLAYING:
            self.table.displayPlayToPause()
            self.player.toggle()

            status = self.statusBar().currentMessage().replace(
                'Playing', 'Paused')
            self.stopStatusEmission(status)
        else:
            self.table.displayPauseToPlay(self.table.playingId)
            self.player.toggle()
            #self.onDurationChanged(0,0)
            status = self.table.getStatus()
            self.setStatusEmission(status)

    # Triggered by player when a song starts
    def onDurationChanged(self, bus, msg):
        self.table.displayNext()
        print('Duration changed signal !')
        filename = self.table.model().item(self.table.playingId,
                                           0).data()['file'][7:]
        data = wave.getDBData(filename)
        wave.createImg(data)
        self.scrollSlider.setStyleSheet("border-image: url(./wave.png)")

    # Triggered by player at the end of a song
    def onAboutToFinish(self, bus):
        if self.table.model().rowCount() - 1 > self.table.playingId:
            print('About to finish !')
            self.table.playingId += 1
            self.player.add(self.table.model().item(self.table.playingId,
                                                    0).data()['file'])

    def addSongsFromTree(self, list, play):
        if not self.radio:
            i = self.table.model().rowCount()
            for l in list:
                self.table.addRow(l)
            self.table.resizeRowsToContents()
            if play:
                self.stop()
                self.player.add(list[0]['file'])
                self.player.play()
                self.table.displayStopToPlay(i)
                status = self.table.getStatus()
                self.setStatusEmission(status)

    def setStatusEmission(self, status):
        if self.timeOut > 0:
            GObject.source_remove(self.timeOut)
        self.timeOut = GObject.timeout_add(1000, self.update, status)

    def stopStatusEmission(self, status):
        if self.timeOut > 0:
            GObject.source_remove(self.timeOut)
        self.timeOut = GObject.timeout_add(0, self.update, status)
        self.timeOut = -1

    def update(self, status):
        print('.')
        try:
            duration_nanosecs = self.player.getDuration()
            duration = float(duration_nanosecs) / 1000000000
            self.scrollSlider.setRange(0, duration)

            nanosecs = self.player.getPosition()
            position = float(nanosecs) // 1000000000
            self.scrollSlider.setValue(position)
            m, s = divmod(position, 60)
            self.statusBar().showMessage(
                status.replace('%', "%02d:%02d" % (m, s)))
        except Exception as e:
            print(e)
            pass
        if 'Playing' in status:
            return True
        else:
            return False

    def tableAction(self, str):
        if str == 'stop':
            self.stop()
        elif str == 'play':
            if self.table.selectedIndexes():
                index = self.table.selectedIndexes()[0]
            else:
                index = self.table.model().index(
                    self.table.selectionModel().currentIndex().row(), 0)
            songURI = index.model().itemFromIndex(index).data()['file']

            self.player.stop()
            self.player.add(songURI)
            self.player.play()
            self.table.displayStopToPlay(index.row())
            status = self.table.getStatus()
            self.setStatusEmission(status)

    @staticmethod
    def readConfig(section):
        parser = RawConfigParser()
        if getattr(sys, 'frozen', False):
            # frozen
            parser.read(
                os.path.dirname(os.path.realpath(sys.executable)) + '/config')
        else:
            # unfrozen
            parser.read(
                os.path.dirname(os.path.realpath(__file__)) + '/config')

        return dict(parser.items(section))

    #Create menu bar
    def createMenu(self):
        self.menuBar()
        self.menuBar().setVisible(False)
        actionMenu = self.menuBar().addMenu('&Action')
        scanMusicFolderAction = QtGui.QAction('Scan Music Folder', self)
        showShortcutAction = QtGui.QAction('Show Shortcut', self)
        addFolderToLibraryAction = QtGui.QAction('Add Folder to Library', self)
        scanWaveformsAction = QtGui.QAction('Scan wave froms', self)
        #self.toggleRadioAction= QtGui.QAction('Switch to Radio mode',self)
        if not self.radio:
            self.toggleRadioAction = QtGui.QAction('Switch to Radio mode',
                                                   self)
        else:
            self.toggleRadioAction = QtGui.QAction('Switch to Library mode',
                                                   self)
        scanMusicFolderAction.triggered.connect(self.scanMusicFolder)
        actionMenu.addAction(scanMusicFolderAction)
        showShortcutAction.triggered.connect(self.showShortcut)
        actionMenu.addAction(showShortcutAction)
        addFolderToLibraryAction.triggered.connect(self.addFolderToLibrary)
        actionMenu.addAction(addFolderToLibraryAction)
        scanWaveformsAction.triggered.connect(self.scanWaveforms)
        actionMenu.addAction(scanWaveformsAction)
        self.toggleRadioAction.triggered.connect(self.toggleRadio)
        actionMenu.addAction(self.toggleRadioAction)

    # Menu Action 1
    def scanMusicFolder(self):
        thread = QtCore.QThread(self)
        thread.worker = WorkThread(
            Foo.readConfig('options')['music_folder'], False)
        thread.worker.moveToThread(thread)
        thread.started.connect(thread.worker.process)
        thread.worker.finished.connect(thread.quit)
        thread.worker.finished.connect(thread.worker.deleteLater)
        thread.finished.connect(thread.deleteLater)
        thread.finished.connect(self.tree.initUI)
        thread.start()

    # Menu Action 2
    def showShortcut(self):
        dictSC = Foo.readConfig('shortcuts')
        message = '''<b>''' + dictSC['modifier'] + '''+''' + dictSC[
            'stop'] + '''</b> : Stop<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC['quit'] + '''</b> : Quit<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                'play_pause'] + '''</b> : Play/Pause    <br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                    'previous'] + '''</b> : Previous<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC['next'] + '''</b> : Next<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                        'volume_down'] + '''</b> : Volume down<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                            'volume_up'] + '''</b> : Volume up<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                                'radio_mode'] + '''</b> : Toggle radio mode<br/>''' + '''
		<b>''' + dictSC['modifier'] + '''+''' + dictSC[
                                    'equalizer'] + '''</b> : Equalizer<br/>'''
        print(len(self.findChildren(QtCore.QObject)))
        box = QMessageBox.about(self, 'About Shortcuts', message)
        print(len(self.findChildren(QtCore.QObject)))
        print('must delete')

    # Menu Action3
    # Must be subdirectory of music folder otherwise wont be rescanned
    def addFolderToLibrary(self):
        dir = QFileDialog.getExistingDirectory(
            None, "Open Directory",
            Foo.readConfig('options')['music_folder'], QFileDialog.ShowDirsOnly
            | QFileDialog.DontResolveSymlinks)
        thread = QtCore.QThread(self)
        thread.worker = WorkThread(dir, True)
        thread.worker.moveToThread(thread)
        thread.started.connect(thread.worker.process)
        thread.worker.finished.connect(thread.quit)
        thread.worker.finished.connect(thread.worker.deleteLater)
        thread.finished.connect(thread.deleteLater)
        thread.finished.connect(self.tree.initUI)
        thread.start()

    # Menu action 4
    def scanWaveforms(self):
        from wave import Wave
        thread = QtCore.QThread(self)
        thread.worker = Wave(Foo.readConfig('options')['music_folder'])
        thread.worker.moveToThread(thread)
        thread.started.connect(thread.worker.processScan)
        thread.worker.finished.connect(thread.quit)
        thread.worker.finished.connect(thread.worker.deleteLater)
        thread.finished.connect(thread.deleteLater)
        thread.finished.connect(self.tree.initUI)
        thread.start()

    # Menu Action5
    def toggleRadio(self):
        self.table.deleteLater()
        self.table.close()
        if not self.radio:
            configRadio = Foo.readConfig('radios')
            self.table = TableRadio(self.tree, configRadio)
            self.toggleRadioAction.setText('Switch to Library mode')
            self.radio = True
            self.player.playbin.disconnect(self.handlerATF)
            self.handlerT = self.player.bus.connect('message::tag',
                                                    self.table.onTag)
        else:
            config = Foo.readConfig('options')
            self.table = Table(self.tree, config)
            self.toggleRadioAction.setText('Switch to Radio mode')
            self.radio = False
            self.handlerATF = self.player.playbin.connect(
                "about-to-finish", self.onAboutToFinish)
            self.player.bus.disconnect(self.handlerT)

        self.splitterTopBottom.addWidget(self.table)
        # Since the frame is already attached to the splitter,
        # it only moves it to the new position
        self.splitterTopBottom.addWidget(self.infoFrame)
        self.table.runAction.connect(self.tableAction)
        self.setTabOrder(self.tree, self.table)
        self.splitterTopBottom.setStretchFactor(0, 10)
        #self.splitterTopBottom.setStretchFactor(3,1)

    @QtCore.pyqtSlot()
    def startSearch(self):
        input = self.searchArea.searchLine.text()

        db = thread.load()
        songList = []
        songGenerator = (Song(self.tree.comm, **dict) for dict in db)
        self.tree.model().removeRows(0, self.tree.model().rowCount())

        if self.searchArea.searchExact.isChecked():
            songList = [e for e in songGenerator if e.exactMatch(input)]
        elif self.searchArea.searchPrecise.isChecked():
            songList = [e for e in songGenerator if e.preciseMatch(input)]
        else:
            songList = [e for e in songGenerator if e.fuzzyMatch(input)]

        del db[:]
        songList.sort(key=self.tree.sortFunc)
        self.tree.populateTree(songList)

    @QtCore.pyqtSlot(str)
    def onHotKey(self, key):
        print('Hotkey was pressed', key)
        if key == 'quit':
            self.shortQuit.activated.emit()
        if key == 'stop':
            self.shortStop.activated.emit()
        if key == 'play_pause':
            self.shortPlayPause.activated.emit()
        if key == 'volume_up':
            self.shortVolUp.activated.emit()
        if key == 'volume_down':
            self.shortVolDown.activated.emit()
        if key == 'song_next':
            self.shortSongNext.activated.emit()
        if key == 'song_prev':
            self.shortSongPrev.activated.emit()
        if key == 'tree_up':
            if self.radio:
                self.table.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Up,
                                    Qt.KeyboardModifier(), ''))
            else:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Up,
                                    Qt.KeyboardModifier(), ''))
        if key == 'tree_down':
            if self.radio:
                self.table.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Down,
                                    Qt.KeyboardModifier(), ''))
            else:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Down,
                                    Qt.KeyboardModifier(), ''))
        if key == 'tree_left':
            if not self.radio:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Left,
                                    Qt.KeyboardModifier(), ''))
        if key == 'tree_right':
            if not self.radio:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Right,
                                    Qt.KeyboardModifier(), ''))
        if key == 'tree_validate':
            if self.radio:
                self.table.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Return,
                                    Qt.KeyboardModifier(), ''))
            else:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(QtCore.QEvent.KeyPress, Qt.Key_Return,
                                    Qt.KeyboardModifier(), ''))
        if key == 'tree_append':
            if not self.radio:
                self.tree.keyPressEvent(
                    QtGui.QKeyEvent(
                        QtCore.QEvent.KeyPress, Qt.Key_Return,
                        Qt.KeyboardModifier(QtCore.Qt.ShiftModifier), ''))
        if key == 'radio_mode':
            self.shortRadioMode.activated.emit()

    def tmpTag(self, position):
        menu = QtGui.QMenu()
        tagging = QtGui.QAction('Tagging', self)
        replayGain = QtGui.QAction('ReplayGain', self)

        tagging.triggered.connect(self.openTagging)
        replayGain.triggered.connect(self.startReplayGain)
        menu.addAction(tagging)
        menu.addAction(replayGain)
        menu.exec_(self.tree.viewport().mapToGlobal(position))

    def startReplayGain(self):
        children = self.tree.getChildren()
        self.RG = ReplayGain([x['file'] for x in children])
        self.RG.exec_()

    def openTagging(self):
        children = self.tree.getChildren()
        #[7:] to drop the 'file://' appended for gstreamer
        retag = Retagging([x['file'][7:] for x in children])
        res = retag.exec_()
        if res:
            self.tree.initUI()
        print(res)

    def openEqualizer(self):
        from configparser import RawConfigParser
        equa = Equalizer(self, Foo.readConfig('audio'))
        equa.equalize.connect(self.applyEqua)
        if equa.exec_():
            parser = RawConfigParser()
            parser.read(
                os.path.dirname(os.path.realpath(__file__)) + '/config')
            parser['audio']['settings'] = str(equa.config)
            with open(
                    os.path.dirname(os.path.realpath(__file__)) + '/config',
                    'w') as configfile:
                parser.write(configfile)

    def applyEqua(self, band, value):
        print('receiving equa', str(band), value)
        if str(band) == 'band0' and value == 0:
            self.player.equalizer.set_property('band0', 0.01)
        else:
            self.player.equalizer.set_property(str(band), value)
Beispiel #54
0
def dfs(board_state):
    'Uses Depth First Search to search for solution'

    startTime = time.time()
    tree = Tree()
    tree.add(Node(board_state, None, None))
    tree.frontier.append(tree.find(board_state))

    result = None

    while tree.has_frontier():
        current_node = tree.frontier.pop(0)

        if current_node.reached_goal():
            tree.result = current_node
            break

        children = current_node.children()

        for child in children[::-1]:
            if tree.already_added(child) == False:
                tree.add(child)
                tree.frontier.insert(0, child)

        tree.expanded.append(current_node)

    elapsed_time = time.time() - startTime

    tree.total_time = elapsed_time
    build_file(tree)
Beispiel #55
0
def ast(board_state):
    'Uses A* to search for solution'

    startTime = time.time()
    tree = Tree()
    tree.add(NodeHeuristic(board_state, None, None))
    tree.frontier = Queue.PriorityQueue()
    tree.frontier.put(tree.find(board_state))

    result = None

    while tree.frontier.empty() == False:
        current_node = tree.frontier.get()

        if current_node.reached_goal():
            tree.result = current_node
            break

        children = current_node.children()

        for child in children:
            if tree.already_added(child) == False:
                tree.add(child)
                tree.frontier.put(child)

        tree.expanded.append(current_node)

    elapsed_time = time.time() - startTime

    tree.total_time = elapsed_time
    build_file(tree)
Beispiel #56
0
def traverse_with_tree(edges, starting_town):
    modified = change_root(edges, starting_town)
    town = Tree()
    town.build_from_edges(modified)
    return town.show_breadth_first()
def build_tree(trainX, trainy, flags, tree=Tree(), depth=10, RF=0, K=0, gr=0):
    if depth == 0 or len(np.unique(trainy)) == 1:
        count0 = list(trainy).count(0)
        count1 = list(trainy).count(1)
        if count0 > count1:
            return 0
        else:
            return 1

    feature_score = 0
    best_feature = 0
    feature_split_point = 0

    #flags=[1]*(trainX.shape[1])
    if RF == 1:
        features = set()
        print(flags)
        # print("depth:",depth)
        # print("Before 1 loop")
        if flags.count(1) > K:
            while len(features) != K:
                random_n = np.random.randint(0, trainX.shape[1])
                #print(random_n)
                # if flags.count(1)<7:
                #     print(random_n)
                if (random_n in features) or flags[random_n] == 0:
                    continue
                else:
                    features.add(random_n)
        else:
            i = 0
            while i < len(flags):
                if flags[i] == 1:
                    features.add(i)
                i += 1
        #print(features)
        #print("after 1 loop and before 2 loop")
        print("K features", features)

        if gr == 0:
            max_info_gain = 0
            for i in features:
                temp = metrics.mutual_info_score(trainX.T[i], trainy)
                if temp > max_info_gain:
                    max_info_gain = temp
                    best_feature = i
        elif gr == 1:
            feature_info_gain = []
            for i in features:
                feature_info_gain.append(
                    metrics.mutual_info_score(trainX.T[i], trainy))

            mean = np.mean(feature_info_gain)
            good_features = [1] * len(feature_info_gain)
            j = 0
            while j < len(feature_info_gain):
                if feature_info_gain[j] < mean:
                    good_features[j] = 0
                j += 1
            j = 0
            best_gain_ratio = 0
            while j < len(good_features):
                if good_features[j] == 1:
                    temp = gain_ratio(
                        feature_info_gain[j],
                        intrinsic_value(trainX.T[list(features)[j]]))
                    if temp > best_gain_ratio:
                        best_feature = list(features)[j]
                        best_gain_ratio = temp
                j += 1

            # best_gain_ratio=0
            # for i in features:
            #     temp=gain_ratio(metrics.mutual_info_score(trainX.T[i],trainy),intrinsic_value(trainX.T[i]))
            #     if temp>best_gain_ratio:
            #         best_gain_ratio=temp
            #         best_feature=i

        else:
            for i in features:
                current_feature = metrics.mutual_info_score(
                    trainX.T[i], trainy)
                if current_feature > feature_score:
                    feature_score = current_feature
                    best_feature = i

            feature_split_point = decision_stump(trainX.T[best_feature],
                                                 trainy)
        if gr == 0 or gr == 1:
            feature_divides = []
            split_score = 0
            j = 0
            temp = list(copy.deepcopy(trainX.T[best_feature]))
            temp.sort()
            #print("before 2.1 loop")
            while j < len(trainX.T[best_feature]) - 1:
                feature_divides.append((temp[j] + temp[j + 1]) / 2)
                j += 1
            #print("after 2.1 loop and before 2.2 loop")

            k = 0
            train_feature = np.full(len(trainX.T[best_feature]), 1, dtype=int)
            while k < len(feature_divides):

                train_feature[k] = 0
                current_split = metrics.mutual_info_score(
                    train_feature, trainy)
                if current_split > split_score:
                    feature_split_point = feature_divides[k]
                k += 1
        #print(len(trainX.T[i]),len(train_feature),len(trainy))
        #print("after 2.2 loop")

    #print(trainX.T[best_feature],trainy)
    flags[best_feature] = 0
    #print(depth)
    #print(flags)
    #threshold=decision_stump(trainX.T[best_feature],trainy,step_size=0.1)
    threshold = feature_split_point
    print("feature:", best_feature)
    print("split point", threshold)
    #print(threshold)
    tree.data = [best_feature, threshold]
    left_tree = Tree()
    right_tree = Tree()
    leftsub = np.array([], dtype='int32')
    rightsub = np.array([], dtype='int32')

    # split into 2 parts: leftsub and rightsub
    #print("before split loop")
    i = 0
    while i < (trainX.shape[0]):
        if trainX[i, best_feature] <= threshold:
            leftsub = np.append(leftsub, i)
        else:
            rightsub = np.append(rightsub, i)
        i = i + 1
    #print(leftsub,rightsub)
    if len(leftsub) > 0:
        left_tree = build_tree(trainX[leftsub],
                               trainy[leftsub],
                               flags,
                               tree=left_tree,
                               depth=depth - 1,
                               RF=RF,
                               K=K,
                               gr=gr)
        tree.left = left_tree
    if len(rightsub) > 0:
        right_tree = build_tree(trainX[rightsub],
                                trainy[rightsub],
                                flags,
                                tree=right_tree,
                                depth=depth - 1,
                                RF=RF,
                                K=K,
                                gr=gr)
        tree.right = right_tree
    flags[best_feature] = 1
    return tree
Beispiel #58
0
from tree import Tree

tree1 = Tree(2)
tree2 = Tree(1)

print(tree1)
Beispiel #59
0
 def setUp(self):
     self.redis_client = Redis()
     self.tree = Tree(self.redis_client)
Beispiel #60
0
            node, level = q.dqueue()
            if node.left:
                q.queue((node.left, level + 1))
            if node.right:
                q.queue((node.right, level + 1))

            output.append((node.data, level))

        return output

    def height(self):
        return (self.run()[-1][1])


if __name__ == '__main__':
    tree = Tree()
    tree.push(Node(10))
    tree.push(Node(1))
    tree.push(Node(15))
    tree.push(Node(5))
    tree.push(Node(11))
    tree.push(Node(9))
    tree.push(Node(20))
    tree.push(Node(45))
    tree.push(Node(4))
    tree.push(Node(13))
    tree.push(Node(14))
    head = tree.head
    bfs = BFS(head)
    print(bfs.run())
    print(bfs.height())