def main(argv): chart = Chart("bittrex", "BTC-LTC", "fiveMin", False) logger = Logger strategy = Strategy(capital=0.01, pair="BTC-LTC", client=chart.conn) # candlesticks = [] # developing_candlestick = BotCandlestick() while True: try: price = chart.get_current_price() except Exception as e: logger.log("ERROR: Exception occurred: " + e.message, "error") time.sleep(int(1)) price = chart.get_current_price() logger.log("Received price: " + str(price)) strategy.live_tick(price) # if developing_candlestick.is_closed(): # candlesticks.append(developing_candlestick) # strategy.tick(developing_candlestick) # developing_candlestick = BotCandlestick() time.sleep(int(1))
def __init__(self, data, parent=None): super(Navigation, self).__init__(parent) self.gridLayout = QtWidgets.QGridLayout() self.gridLayout.setObjectName("gridLayout") #self.gridLayout.addWidget(self.listview, 0, 0, 2, 1) self.frame = QtWidgets.QFrame() self.chart = Chart('pie1', data, self) self.parent().addToolBar(self.chart.navigationBar(data)) self.ly = QtWidgets.QVBoxLayout() self.frame.setLayout(self.ly) self.gridLayout1 = QtWidgets.QGridLayout() self.gridLayout1.addWidget(self.frame) self.gridLayout2 = QtWidgets.QGridLayout() self.gridLayout2.addWidget(self.chart.chartview) self.gridLayout.addLayout(self.gridLayout2, 0, 2, 0, 1) self.gridLayout.addLayout(self.gridLayout1, 0, 2, 0, 1) self.horizontalGroupBox = QGroupBox() self.horizontalGroupBox.setLayout(self.gridLayout)
def entries_by_date(country, date): day_chart = DayChart.get_by_id('{}/{}'.format(country, date.strftime('%Y/%m/%d'))) entries = [] if day_chart == None: chart = Chart(country) results = chart.read(date.strftime('%Y/%m/%d')) if not results: return None day_chart = DayChart(id='{}/{}'.format(country, date.strftime('%Y/%m/%d')), date=date, country=country) day_chart_key = day_chart.put() for result in results: entry = Entry(rank=result['rank'], artist=result['artist'], song=result['song'], parent=day_chart_key) entry_key = entry.put() entries.append(entry) else: day_chart_key = day_chart.key query = Entry.query(ancestor=day_chart.key).order(Entry.rank) entries = query.fetch(100) return (day_chart, entries)
def regionAnalyzerByContinuous(self, game, timeSlot, botRaws, humanRaws): botTimeCons = getTimeContinuouses(timeSlot, botRaws) humanTimeCons = getTimeContinuouses(timeSlot, humanRaws) cnt = 0 for times in botTimeCons[0]: sTime = times[0] eTime = times[1] print sTime print eTime x = [] y = [] z = "bot" for node in botRaws[0].nodes: if node.time >= sTime: if node.time >= eTime: break x.append(node.time) y.append(node.region) if len(x) >= 5: Chart.draw_line_marker( "result/" + game + "_frag(" + str(cnt) + ")", [(x, y, z)], "touch region", "time(s)", "region") cnt += 1 if cnt == 6: break
def draw(self) -> None: try: self.parse_options() except GeometryError: print('Incorrect polygon') return chart = Chart(self.width, self.height, pen_size=2) chart.colors.append(Qt.red) painter = chart.create_painter() inside, outside = self.get_symmetric_difference() for chain in inside: self.draw_chain(chain, painter) painter.setPen(QPen(Qt.red, 2)) for chain in outside: self.draw_chain(chain, painter) back_painter = ChartBackgroundPainter(chart, revers=True) grid_painter = GridPainter(self, back_painter.offset, self.unit) back_painter.draw(grid_painter) self.chart_area.update(chart)
def create_chart(self) -> Chart: self.calculate() chart = Chart(self.width, self.height) painter = chart.create_painter() if self.min_y == self.max_y: self.draw_constant_chart(painter) painter.end() self.add_background(chart) return chart dy = self.max_y - self.min_y y = self.f_x_y[self.alpha] yy = (self.max_y - y) * self.height / dy current_point = QPoint(0, yy) for xx in range(1, self.width): x = self.f_xx_x[xx] y = self.f_x_y[x] yy = (self.max_y - y) * self.height / dy if y != inf else inf move_point = QPoint(xx, round(yy)) if yy != inf else None if None not in (current_point, move_point): painter.drawLine(current_point, move_point) current_point = move_point back_painter = ChartBackgroundPainter(chart) grid_painter = GridPainter(self, back_painter.offset) back_painter.draw(grid_painter) return chart
def score_rest(self, time_slot, fun, botRaws, humanRaws): botClusters = groupClusteredDensity(time_slot, botRaws) humanClusters = groupClusteredDensity(time_slot, humanRaws) botFitSize = fitSize(botClusters, toMin=True) humanFitSize = fitSize(humanClusters, toMin=True) minFitSize = botFitSize if minFitSize > humanFitSize: minFitSize = humanFitSize botClusters = groupFit(minFitSize, botClusters) humanClusters = groupFit(minFitSize, humanClusters) botMA = groupMovingAverage(botClusters) humanMA = groupMovingAverage(humanClusters) botActs = groupActivation(botClusters, botMA) humanActs = groupActivation(humanClusters, humanMA) botScores = groupScore(botActs, fun) humanScores = groupScore(humanActs, fun) t = [] for i in range(0, len(botClusters)): t.append( Chart.trace_generator(botClusters[i][0], botScores[i], "lines", "bot(" + str(i + 1) + ")_score")) for i in range(0, len(humanClusters)): t.append( Chart.trace_generator(humanClusters[i][0], humanScores[i], "lines", "human(" + str(i + 1) + ")_score")) filepath = self.path + os.sep + "score(" + str(time_slot) + ").html" Chart.draw_simpleLine(filepath, t)
def touch_density(self, time_slot, botRaws, humanRaws): botDensities = groupClusteredDensity(time_slot, botRaws) humanDensities = groupClusteredDensity(time_slot, humanRaws) botFitSize = fitSize(botDensities, toMin=True) humanFitSize = fitSize(humanDensities, toMin=True) minFitSize = botFitSize if minFitSize > humanFitSize: minFitSize = humanFitSize botDensities = groupFit(minFitSize, botDensities) humanDensities = groupFit(minFitSize, humanDensities) data = [] cnt = 1 for cluster in botDensities: data.append((cluster[0], cluster[1], "bot(" + str(cnt) + ")")) cnt += 1 cnt = 1 for cluster in humanDensities: data.append((cluster[0], cluster[1], "human(" + str(cnt) + ")")) cnt += 1 Chart.draw_line2("result/density.html", \ data, "touchDensity", "time(s)", "NumberOfTouches")
def main(): c = Chart() buying = 'y' while buying == 'y': c.buySeat() buying = input("Continue to buy seats? y/n: ")
def restRatio(self, game, timeSlot, botRaws, humanRaws): bS = restRatioSimilarities(timeSlot, botRaws) hS = restRatioSimilarities(timeSlot, humanRaws) bD = getDeviations(bS) hD = getDeviations(hS) filepath = self.path + os.sep + game + "_restratio.html" Chart.draw_box(filepath, [(bD, "macro"), (hD, "human")], "RestRatio Similarity", "Similarity")
def current_day(): chart = Chart(title='Dag X', labels=['one', 'two', 'three']) washes, http_code = stats.get_todays_washes() ok, commented, unknown = filtered_count(washes) chart.add_serie('Tvättade', ok, "#4BC0C0") chart.add_serie('Kommenterade', commented, "#36A2EB") chart.add_serie('Okända', unknown, "#FF6384") return render_template('day.html', series_names=chart.series_names(), bars=chart.get_series(), title=chart.title)
def distributionAnalyzer(self, timeSlot, botRaws, humanRaws): botActRatios = getActRatios(timeSlot, botRaws) humanActRatios = getActRatios(timeSlot, humanRaws) botDistributions = getDistributions(botActRatios, "bot") humanDistributions = getDistributions(humanActRatios, "human") Chart.draw_line("result/distribution(" + str(timeSlot) + ")", \ botDistributions + humanDistributions)
def _show_scan_chart(self, event=None): items = self.scan_result_treeview.selection() if len(items) == 0: return item = items[0] item_text = self.scan_result_treeview.item(item, "values") chart = Chart(item_text[1], self.db, self.cursor) chart.gui_arrang() tkinter.mainloop()
def actSeqNum(self, timeSlot, botRaws, humanRaws): botClusters = groupClusteredDensity(timeSlot, botRaws) humanClusters = groupClusteredDensity(timeSlot, humanRaws) botFitSize = fitSize(botClusters, toMin=True) humanFitSize = fitSize(humanClusters, toMin=True) minFitSize = botFitSize if minFitSize > humanFitSize: minFitSize = humanFitSize botClusters = groupFit(minFitSize, botClusters) humanClusters = groupFit(minFitSize, humanClusters) botMA = groupMovingAverage(botClusters) humanMA = groupMovingAverage(humanClusters) botActs = groupActivation(botClusters, botMA) humanActs = groupActivation(humanClusters, humanMA) botCons = groupContinuous(botClusters, botActs) humanCons = groupContinuous(humanClusters, humanActs) data = [] for con in botCons: i = 1 j = 1 x = [0] y = [0] while j < len(con) and i <= con[-1][3]: x.append(i) if i >= con[j][3]: y.append(y[-1] + 1) j += 2 else: y.append(y[-1]) i += 1 z = "macro" data.append((x, y, z)) for con in humanCons: i = 1 j = 1 x = [0] y = [0] while j < len(con) and i <= con[-1][3]: x.append(i) if i >= con[j][3]: y.append(y[-1] + 1) j += 2 else: y.append(y[-1]) i += 1 z = "human" data.append((x, y, z)) Chart.draw_line2("result/actseqnum(" + str(timeSlot) + ")", \ data, "", "time(s)", "Number of Input Sequence")
def regionSimilarityByTime(self, timeSlot, time, regionSize, botRaws, humanRaws): botClusters = getTimeClusters(timeSlot, botRaws) humanClusters = getTimeClusters(timeSlot, humanRaws) botCons = [] for cluster in botClusters: endTime = cluster.nodes[-1].endTime con = [] j = 0 while j < endTime: con.append((j, j + time)) j += time botCons.append(con) humanCons = [] for cluster in humanClusters: endTime = cluster.nodes[-1].endTime con = [] j = 0 while j < endTime: con.append((j, j + time)) j += time humanCons.append(con) botRegionVectors = getRegionVectorByTimes(regionSize, botClusters, botCons) humanRegionVectors = getRegionVectorByTimes(regionSize, humanClusters, humanCons) botSimilarities = getRegionSimilarities(regionSize, botRegionVectors) humanSimilarities = getRegionSimilarities(regionSize, humanRegionVectors) data = [] botStds = [] for i in range(0, len(botSimilarities)): actRatios = [] for j in range(0, len(botSimilarities[i])): actRatios.append(botSimilarities[i][j]) botStds.append(1.0 - numpy.std(actRatios) / 2) print "[" + str(i + 1) + "] - " + str(1.0 - numpy.std(actRatios) / 2) data.append((botStds, "macro")) humanStds = [] for i in range(0, len(humanSimilarities)): actRatios = [] for j in range(0, len(humanSimilarities[i])): actRatios.append(humanSimilarities[i][j]) humanStds.append(1.0 - numpy.std(actRatios) / 2) print "[" + str(i + 1) + "] - " + str(1.0 - numpy.std(actRatios) / 2) data.append((humanStds, "human")) filepath = self.path + os.sep + "Timesimilarity(" + str( timeSlot) + ").html" Chart.draw_box(filepath, data, "Similarities", "Similarity")
def overview(request, context): """ Gives a general overview of a user's habits between two dates. """ start = context.get('start') end = context.get('end') user = context.get('user') #def new_favourites_string(num=3): #artists = [(a, c) for a, c, _ in \ # (WeekData.objects.new_artists_in_timeframe(user, start, end, num))] #def to_s((a, c)): # return "%s, with %d plays" % (a, c) #return 'Top %d new artists in this time: %s and %s' % \ # (num, ', '.join(to_s(item) for item in artists[:-1]), # to_s(artists[-1])) # vital stats. TODO: Rework. total_plays = WeekData.objects.total_plays_between(user, start, end) total_weeks = float(end - start) + 1 vitals = [ "<b>%d</b> weeks, <b>%d</b> days" % (total_weeks, total_weeks * 7), "<b>%d</b> plays: an average of <b>%.2f</b> songs per week and <b>%.2f</b> per year" \ % (total_plays, total_plays / total_weeks, total_plays / (total_weeks / 52) if total_weeks >= 52 else total_plays * (52 / total_weeks)), # TODO: Reinstate if ever more efficient. # new_favourites_string(), ] # weekly playcounts image and monthly playcounts bar chart wpcjs = WeekData.objects.weekly_play_counts_js(user, start, end) mcjs = WeekData.objects.monthly_counts_js(user, start, end) # record weeks and overall chart record_single_artist = WeekData.objects.record_weeks(user, start, end) record_total_plays = WeekData.objects.record_week_totals(user, start, end) record_unique_artists = WeekData.objects.record_unique_artists_in_week(user, start, end) chart = Chart(user, start, end) # weekly playcounts histogram wpc_hist, wpc_hist_step = WeekData.objects.weekly_play_counts_histogram(user, start, end) return { 'context' : context, 'wpc_hist' : wpc_hist, 'wpc_hist_step' : wpc_hist_step, 'chart' : chart.chart(), 'record_single_artist' : record_single_artist, 'record_total_plays' : record_total_plays, 'record_unique_artists' : record_unique_artists, 'mcjs' : mcjs, 'wpcjs' : wpcjs, 'total_weeks' : total_weeks, 'vitals' : vitals, }
def parse(self, start_symbol, lexicon, grammar, sentence): agenda = Agenda(self.logger) chart = Chart(grammar, agenda, logger=self.logger) chart.introduce_symbol(start_symbol, 0) position = 0 while position < len(sentence) or agenda.size() > 0: if agenda.size() == 0: agenda.add_alternatives( lexicon.get_interpretations(sentence[position]), position) position = position + 1 chart.extend_arcs(agenda.next_constituent())
def createChart(): g = Chart(scheme.shape[0]) for index, row in scheme.iterrows(): if list(row[row.apply(lambda x: x == 1)].index): for dest in row[row.apply(lambda x: x == 1)].index: g.addEdge(index, dest) return g
def main(_argv): # Uśmiech usmiech = Chart(None, True) x = np.linspace(-2, 2, 8) y = (x + 2) * (x - 2) x = np.concatenate((x, np.flip(x, 0))) y = np.concatenate((y, np.flip(-y, 0))) usmiech.wykres_linie_rysuj(x, y, 'glowa') usmiech.wykres_linie_rysuj([-1, 0, 1], [0, -1, 0], 'usmiech') usmiech.wykres_punkty_rysuj([-1, 0, 1], [1, 0, 1], 'oczy i usta') plt.legend(loc='upper right') usmiech.show()
def nbest_parse(self, tokens, n=None): self._grammar.check_coverage(tokens) chart = Chart(list(tokens)) grammar = self._grammar # Chart parser rules. bu_init = ProbabilisticBottomUpInitRule() bu = ProbabilisticBottomUpPredictRule() fr = SingleEdgeProbabilisticFundamentalRule() # Our queue! queue = [] # Initialize the chart. for edge in bu_init.apply_iter(chart, grammar): if self._trace > 1: print ' %-50s [%s]' % (chart.pp_edge(edge,width=2), edge.prob()) queue.append(edge) while len(queue) > 0: # Re-sort the queue. self.sort_queue(queue, chart) # Prune the queue to the correct size if a beam was defined if self.beam_size: self._prune(queue, chart) # Get the best edge. edge = queue.pop() if self._trace > 0: print ' %-50s [%s]' % (chart.pp_edge(edge,width=2), edge.prob()) # Apply BU & FR to it. queue.extend(bu.apply(chart, grammar, edge)) queue.extend(fr.apply(chart, grammar, edge)) # Get a list of complete parses. parses = chart.parses(grammar.start(), ProbabilisticTree) # Assign probabilities to the trees. prod_probs = {} for prod in grammar.productions(): prod_probs[prod.lhs(), prod.rhs()] = prod.prob() for parse in parses: self._setprob(parse, prod_probs) # Sort by probability parses.sort(lambda a,b: cmp(b.prob(), a.prob())) return parses[:n]
def __init__(self, width=1920, height=1080): # Create Screen with the provided resolution self.width = width #1920 self.height = height #1080 self.screen = Screen(width, height) self.logo = self.screen.load_image("logo.png") #self.logo = self.screen.scale(self.logo, 500,100) #self.screen.set_fps(10) # NOTE: Remove comment on next line to add Full screen #self.screen.set_full_screen() size = self.screen.get_size() self.border = 10 b = self.border # Just to make the next line more readable self.battery = Battery(self.screen, x=20, y=self.height - 355, width=140, height=300) # Creates a wyndow with a margin to make it pritty <3 self.window = Window(self.screen, b + 180, b + 130, size[0] - (b * 2 + 270), size[1] - (b * 2 + 180)) self.window.set_all_colors((100, 100, 100)) self.window.set_border_color((200, 200, 200)) self.window.set_border_width(10) self.chart_og = Chart() self.chart_og.set_width(8) self.chart_og.set_color((255, 150, 0)) self.chart_og.add_point([1, 0]) self.chart_og.add_point([2, 0]) self.window.add_chart(self.chart_og) self.chart_optimised = Chart() self.chart_optimised.set_width(8) self.chart_optimised.set_color((0, 150, 255)) self.chart_optimised.add_point([1, 0]) self.chart_optimised.add_point([2, 0]) self.window.add_chart(self.chart_optimised) # Next we add a function that converts the X values to # something the window class can work with, simple numbers # the window class does not know how to handle dates # so they need to be converted to intigers. These X values # are still used as labels in the x axis so format them # wisely. # NOTE: Remove comment on the fallowing line to add convertion function #self.chart.set_conv_funct(self.convert) self.data = Value(self.screen, 22, 150)
def __init__(self, grid): self.__grid = grid self.__score = 0 self.__chart = Chart() self.__history = [] self.__state = State.game_waiting self.__undo_counter = 0 self.__difficulty = Difficulty.NORMAL self.__slider = { 'left': self.__grid.slide_left, 'right': self.__grid.slide_right, 'up': self.__grid.slide_up, 'down': self.__grid.slide_down }
class Charts(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.xlist = list(range(1,100)) self.grid() self.create_widgets() def create_widgets(self): self.tabs = ttk.Notebook(self, width=600, height=450) self.tabs.grid() self.total_exp_page = tk.Frame(self.tabs) self.exp_per_lvl_page = tk.Frame(self.tabs) self.total_exp_chart = Chart(self.total_exp_page) self.exp_per_lvl_chart = Chart(self.exp_per_lvl_page, ylabel_text='Exp Per Level') self.total_exp_chart.pack(fill=tk.BOTH) self.exp_per_lvl_chart.pack(fill=tk.BOTH) self.tabs.add(self.total_exp_page, text="Total Exp") self.tabs.add(self.exp_per_lvl_page, text="Exp Level") self.tabs.grid() def on_slider_value_changed(self, total_exp_list, exp_per_lvl_list): self.total_exp_chart.redraw_chart(self.xlist, total_exp_list) self.exp_per_lvl_chart.redraw_chart(self.xlist, exp_per_lvl_list)
def __init__(self): #average income self.averageIncome = AverageValue() #average unspent ressource AUR self.averageUnspent = AverageValue() #ressource banks, army values (detailed) and supply count self.stats = {} self.workersBuilt = 0 self.unitsTrained = 0 self.structuresBuilt = 0 self.unitsKilled = 0 self.structuresRazed = 0 self.baseCount = 1 self.baseTimings = [] self.saturationTimings = [] self.charts = {} #army value chart (Total value of the current army) self.charts["armyValChart"] = Chart("armyValChart") #mineral income chart (Mineral collection rate) self.charts["incomeChartMin"] = Chart("incomeChartMin") #gas income chart (Vespene gas collection rate) self.charts["incomeChartGas"] = Chart("incomeChartGas") #workerCountChart: active worker count self.charts["workerCountChart"] = Chart("workerCountChart") #spendTechChart: spent ressources on technology self.charts["spendTechChart"] = Chart("spendTechChart") #resLostChart: total ressources lost self.charts["resLostChart"] = Chart("resLostChart") #resKilledArmyChart: ressources Killed in army value self.charts["resKilledArmyChart"] = Chart("resKilledArmyChart") #basecount chart self.charts["baseCountChart"] = Chart("baseCountChart") self.charts["baseCountChart"].add(0, 1)
def parse_chart_lines(song, tags, diff_str, settings): chart = Chart() chart.num_columns = 5 # TODO: idk if GH can have non-5key charts too # Split e.g. "EasyEnhancedGuitar" into "Easy" and "EnhancedGuitar" diff_type = None chart_type = None for i in range(1, len(diff_str)): if diff_str[i].isupper(): diff_type = diff_str[:i] chart_type = diff_str[i:] break else: raise Exception(f"Unknown diff string '{diff_str}'") # Set diff type and, in case of Edit, also set chart string to best # represent the GH diff if chart_type == "Single": # In case of the basic chart type just use the standard .sm diff # types. Map diff_type to .sm diff types and write into Chart diff_type = { "Easy": DiffType.EASY, "Medium": DiffType.MEDIUM, "Hard": DiffType.HARD, "Expert": DiffType.EXPERT, }.get(diff_type, None) if diff_type is None: raise Exception(f"Unknown diff type '{diff_str}'") chart.diff_type = diff_type else: # If we have DoubleBass or EnhancedGuitar we have to get fancy # with Edit diff chart.diff_type = DiffType.EDIT if settings.get("jolemode", False): # jole wanted to discard everything except ExpertDoubleBass # which should be saved as "EXPERT+" Edit if diff_type == "Expert" and chart_type == "DoubleBass": chart.chart_string = "EXPERT+" else: return # Discard everything else else: # In the general case I think it#s sensible to add a "+" to # DoubleBass and add " G" to EnhancedGuitar charts if chart_type == "DoubleBass": chart.chart_string = diff_type + "+" elif chart_type == "EnhancedGuitar": chart.chart_string = diff_type + " G" else: raise Exception(f"Unknown chart type '{chart_type}'") # STUB: parse the actual notes chart.notes = [] song.charts.append(chart)
def create_chart(self) -> Chart: u0, u1, r = self.alpha, self.beta, self.f x0, y0, unit = self.width // 2, self.height // 2, 40 chart = Chart(self.width, self.height) painter = chart.create_painter() u, du, d, a, b = u0, 1, 10, 3, 2 ru = r(u) if not None else None point = PixelPoint(ru * cos(rad(u)), ru * sin(rad(u)), unit) if \ ru is not None else None while u < u1: u += du ru = r(u) if not ru: point = None continue new_point = PixelPoint(ru * cos(rad(u)), ru * sin(rad(u)), unit) if fabs(new_point.x) > x0 or fabs(new_point.y) > y0: point = None continue if not point: point = new_point continue if point.get_distance(new_point) > d: u -= du du /= a continue if point == new_point: u -= du du *= b continue xx0, yy0 = point.x + x0, y0 - point.y xx1, yy1 = new_point.x + x0, y0 - new_point.y point = new_point painter.drawLine(xx0, yy0, xx1, yy1) back_painter = ChartBackgroundPainter(chart, h_axis='r', v_axis='φ') grid_painter = GridPainter(self, back_painter.offset) back_painter.draw(grid_painter) return chart
def create_widgets(self): self.tabs = ttk.Notebook(self, width=600, height=450) self.tabs.grid() self.total_exp_page = tk.Frame(self.tabs) self.exp_per_lvl_page = tk.Frame(self.tabs) self.total_exp_chart = Chart(self.total_exp_page) self.exp_per_lvl_chart = Chart(self.exp_per_lvl_page, ylabel_text='Exp Per Level') self.total_exp_chart.pack(fill=tk.BOTH) self.exp_per_lvl_chart.pack(fill=tk.BOTH) self.tabs.add(self.total_exp_page, text="Total Exp") self.tabs.add(self.exp_per_lvl_page, text="Exp Level") self.tabs.grid()
def __init__(self, file): """ Parse course.yml contents into instances. """ self.config = Config() self._dict = yaml.load(file) if not self.config.helm_args: self.config.helm_args = self._dict.get('helm_args') self.helm = HelmClient(default_helm_arguments=self.config.helm_args) self._repositories = [] self._charts = [] for name, repository in self._dict.get('repositories', {}).iteritems(): repository['name'] = name self._repositories.append(Repository(repository, self.helm)) for name, chart in self._dict.get('charts', {}).iteritems(): self._charts.append(Chart({name: chart}, self.helm)) for repo in self._repositories: type(repo) if not self.config.local_development: logging.debug("Installing repository: {}".format(repo)) repo.install() self.helm.repo_update() if not self.config.local_development: try: self._compare_required_versions() except MinimumVersionException as e: logging.error(e) sys.exit(1)
def init(self): self.setHasConfigurationInterface(True) self.readConfig() self.setAspectRatioMode(Plasma.IgnoreAspectRatio) cfg = self.config() width, ok = cfg.readEntry('width', 256).toInt() height, ok = cfg.readEntry('height', 256).toInt() self.resize(width, height) self.applet.geometryChanged.connect(self.saveGeometry) self._theme = Plasma.Svg(self) self._theme.setImagePath('widgets/background') self.setBackgroundHints(Plasma.Applet.DefaultBackground) self._layout = QGraphicsGridLayout(self.applet) self.applet.setLayout(self._layout) self._chart = Chart(self.applet) self._chart.setZValue(1) self._layout.addItem(self._chart, 0, 0) self._data = Data(self) self.update()
def __init__(self, grid): self.__grid = grid self.__score = 0 self.__chart = Chart() self.__history = [] self.__state = State.running self.__undo_counter = 0
def __init__(self, ambient_manifold, n, coordinates, chart_name, embedding_functions, name=None, latex_name=None, ambient_chart=None, start_index=0): from sage.symbolic.ring import SR Manifold.__init__(self, n, name, latex_name, start_index) if not isinstance(ambient_manifold, Manifold): raise TypeError( "The argument ambient_manifold must be a manifold.") self.ambient_manifold = ambient_manifold Chart(self, coordinates, chart_name) if ambient_chart is None: ambient_chart = ambient_manifold.def_chart.name n_amb = ambient_manifold.dim if len(embedding_functions) != n_amb: raise ValueError( str(n_amb) + " coordinate functions must be provided.") embedding_expressions = [ SR(embedding_functions[i]) for i in range(n_amb) ] self.embedding = DiffMapping(self, ambient_manifold, embedding_expressions, chart_name, ambient_chart)
def __init__(self, file): """ Parse course.yml contents into instances. """ self.config = Config() self.helm = Helm() self._dict = yaml.load(file) self._repositories = [] self._charts = [] for name, repository in self._dict.get('repositories', {}).iteritems(): repository['name'] = name self._repositories.append(Repository(repository)) for name, chart in self._dict.get('charts', {}).iteritems(): self._charts.append(Chart({name: chart})) for repo in self._repositories: type(repo) if not self.config.local_development: logging.debug("Installing repository: {}".format(repo)) repo.install() self.helm.repo_update() if not self.config.local_development: self._compare_required_versions()
def query_chart(request): """ Returns query chart for given request """ # TODO: Move this to one line e.g. queries to query query = request.GET.get("query", None) queries = request.GET.getlist("queries[]") if query: queries = [query] request_timeframe = Timeframe(start=request.GET.get("start", None), end=request.GET.get("end", None), interval=request.GET.get("interval", "hour")) response_chart = None try: response_chart = Chart(queries=queries, start=request_timeframe.start, end=request_timeframe.end, interval=request_timeframe.interval) except GNIPQueryError as e: return handleQueryError(e) response_data = {} response_data['days'] = request_timeframe.days response_data['start'] = request_timeframe.start.strftime(DATE_FORMAT_JSON) response_data['end'] = request_timeframe.end.strftime(DATE_FORMAT_JSON) response_data['columns'] = response_chart.columns response_data['total'] = response_chart.total return HttpResponse(json.dumps(response_data), content_type="application/json")
def __init__(self, coordinate=None, name='R', latex_name=r'\RR', start_index=0, names=None): r""" Construct the real line manifold. TESTS:: sage: R = RealLine() ; R field R of real numbers sage: R.category() Category of sets sage: TestSuite(R).run() """ from chart import Chart Manifold.__init__(self, 1, name, latex_name=latex_name, start_index=start_index) if coordinate is None: if names is None: coordinate = 't' else: coordinate = names[0] self._canon_chart = Chart(self, coordinates=coordinate) self._lower = minus_infinity # for compatibility with OpenInterval self._upper = infinity # idem
def user_chart(request, context): """ Creates a standard chart. """ user = context.get('user') start = context.get('start') end = context.get('end') count = context.get('count', 100) isWeek = start == end G = request.GET only_new = 'newmusic' in G exclude_months = 'exclude_months' in G chart = Chart(user, start, end, count) if only_new: chart.set_exclude_before_start() exclusion = G.get('num_excluded', '') max_scrobbles = G.get('max_scrobbles', '') if exclude_months: if exclusion.isdigit(): exclusion = int(exclusion) max_scrobbles = int(max_scrobbles) if max_scrobbles.isdigit() else 0 chart.set_exclude_months(exclusion, max_scrobbles) back = { 'context': context, 'chart' : chart, 'isWeek' : isWeek, 'count' : count, 'only_new': only_new, 'exclude_months': exclude_months, 'num_excluded': exclusion, 'max_scrobbles': max_scrobbles } if isWeek: back['prevW'] = start - 1 back['nextW'] = start + 1 return back
def __init__(self, timeline, parent): TopicMessageView.__init__(self, timeline, parent) self._topic = None self._message = None self._plot_paths = [] self._playhead = None self._chart = Chart() self._data_loader = None self._x_view = None self._dirty_count = 0 self._csv_data_loader = None self._csv_path = None self._csv_row_stride = None self._clicked_pos = None self._dragged_pos = None self._configure_frame = None self._max_interval_pixels = 1.0 tb = self.parent.ToolBar icons_dir = roslib.packages.get_pkg_dir(PKG) + "/icons/" tb.AddSeparator() tb.Bind( wx.EVT_TOOL, lambda e: self.configure(), tb.AddLabelTool(wx.ID_ANY, "", wx.Bitmap(icons_dir + "cog.png")) ) self.parent.Bind(wx.EVT_SIZE, self._on_size) self.parent.Bind(wx.EVT_PAINT, self._on_paint) self.parent.Bind(wx.EVT_LEFT_DOWN, self._on_left_down) self.parent.Bind(wx.EVT_MIDDLE_DOWN, self._on_middle_down) self.parent.Bind(wx.EVT_RIGHT_DOWN, self._on_right_down) self.parent.Bind(wx.EVT_LEFT_UP, self._on_left_up) self.parent.Bind(wx.EVT_MIDDLE_UP, self._on_middle_up) self.parent.Bind(wx.EVT_RIGHT_UP, self._on_right_up) self.parent.Bind(wx.EVT_MOTION, self._on_mouse_move) self.parent.Bind(wx.EVT_MOUSEWHEEL, self._on_mousewheel) self.parent.Bind(wx.EVT_CLOSE, self._on_close) wx.CallAfter(self.configure)
def test___init__(self): """ Case: count, no start, no end """ COUNT = 100 GRANULARITY = 'S5' NUM_EXTRA_SECONDS = 300 sample_instrument = Instrument(4) # USD_JPY # Initialize a sample chart. chart = Chart( in_instrument=sample_instrument, count=COUNT ) # check success self.assertNotEqual(chart._granularity, None) # check indecies self.assertTrue( chart._start_index == 0 and chart._get_end_index() == COUNT - 1 ) # check instrument self.assertEqual(sample_instrument.get_id(), chart._instrument.get_id()) self.assertEqual(sample_instrument.get_name(), chart._instrument.get_name()) # check granularity self.assertEqual(chart._granularity, GRANULARITY) # Oanda's default (S5) # check count self.assertEqual(chart.get_size(), COUNT) # check candle format # If 'bidask', then the midpoints will be None, and vice-versa self.assertNotEqual(chart[0].open_bid, None) # Oanda's default start = None chart = None """ Case: count, start, no end """ COUNT = 100 GRANULARITY = 'M1' WIGGLE_MINUTES = 5 # no price change -> no candle ADJUSTMENT = 120 sample_instrument = Instrument(4) # USD_JPY # Initialize a sample chart with no market close gaps. # start = now - time since close - chart size # - (adjustment to busy time to avoid gaps) - skipped candle slack start = datetime.utcnow() \ - Broker.get_time_since_close() \ - timedelta(minutes = COUNT + ADJUSTMENT + WIGGLE_MINUTES) chart = Chart( in_instrument=sample_instrument, count=COUNT, start=start, granularity=GRANULARITY ) # check success self.assertNotEqual(chart._granularity, None) # check indecies self.assertTrue( chart._start_index == 0 and chart._get_end_index() == COUNT - 1 ) # check instrument self.assertEqual(sample_instrument.get_id(), chart._instrument.get_id()) self.assertEqual(sample_instrument.get_name(), chart._instrument.get_name()) # check granularity self.assertEqual(chart._granularity, GRANULARITY) # check count self.assertEqual(chart.get_size(), COUNT) # check start time self.assertTrue( # Candles gap if there were no ticks, so allow some wiggle room. abs(start - chart.get_start_timestamp()) < timedelta(minutes=WIGGLE_MINUTES) ) # check end time end_expected = start + timedelta(minutes=COUNT) end_real = chart.get_end_timestamp() self.assertTrue( # Candles gap if there were no ticks, so allow some wiggle room. abs(end_expected - end_real) < timedelta(minutes=WIGGLE_MINUTES) ) # check candle format self.assertNotEqual(chart[0].open_bid, None) """ count, no start, end """ COUNT = 100 GRANULARITY = 'H2' sample_instrument = Instrument(4) # USD_JPY # Initialize a sample chart. chart = Chart( in_instrument=sample_instrument, count=COUNT, end=datetime.utcnow(), granularity=GRANULARITY ) # check success self.assertNotEqual(chart._granularity, None) # check indecies self.assertTrue( chart._start_index == 0 and chart._get_end_index() == COUNT - 1 ) # check instrument self.assertEqual(sample_instrument.get_id(), chart._instrument.get_id()) self.assertEqual(sample_instrument.get_name(), chart._instrument.get_name()) # check granularity self.assertEqual(chart._granularity, GRANULARITY) # check count self.assertEqual(chart.get_size(), COUNT) # check start time """self.assertTrue( # Candles gap if there were no ticks, so allow some wiggle room. abs(start - chart.get_start_timestamp()) < timedelta(minutes=5) )""" # check end time end_expected = datetime.utcnow() end_real = chart.get_end_timestamp() if Broker.get_time_until_close() == timedelta(): self.assertTrue( abs(end_expected - end_real) < timedelta(days=3) ) else: self.assertTrue( # Candles gap if there were no ticks, so allow some wiggle room. abs(end_expected - end_real) < timedelta(hours=5) ) # check candle format # If 'bidask', then the midpoints will be None, and vice-versa self.assertNotEqual(chart[0].open_bid, None) # Oanda's default """ no count, start, no end """ COUNT = 24 GRANULARITY = 'M' # month (Oanda) sample_instrument = Instrument(4) # USD_JPY # Initialize a sample chart. # start = now - 2 years start = datetime.utcnow() - timedelta(days=365*2) chart = Chart( in_instrument=sample_instrument, start=start, granularity=GRANULARITY ) # check success self.assertNotEqual(chart._granularity, None) # check indecies self.assertTrue( chart._start_index == 0 and abs(chart._get_end_index() - COUNT) <= 1 ) # check instrument self.assertEqual(sample_instrument.get_id(), chart._instrument.get_id()) self.assertEqual(sample_instrument.get_name(), chart._instrument.get_name()) # check granularity self.assertEqual(chart._granularity, GRANULARITY) # check count self.assertTrue( abs(chart.get_size() - COUNT) <= 1 ) # check start time self.assertTrue( # allow wiggle room. abs(start - chart.get_start_timestamp()) < timedelta(days=32) ) # check end time end_expected = datetime.utcnow() end_real = chart.get_end_timestamp() self.assertTrue( # Allow wiggle room for market close. abs(end_expected - end_real) < timedelta(days=32) ) # check candle format # If 'bidask', then the midpoints will be None, and vice-versa self.assertNotEqual(chart[0].open_bid, None) # Oanda's default
def test_pearson(self): """ case: empty chart """ c = Chart( Instrument(4) ) self.assertRaises(Exception, c.pearson, 0, c.get_size() ) """ case: one candle """ # TODO """ case: straight line, positive slope """ c = Chart(in_instrument=Instrument(4), count=0) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=i), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson > 0.99999 and pearson <= 1 ) """ case: straight line, negative slope """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp - timedelta(seconds=i), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson < -0.99999 and pearson >= -1 ) """ case: V shape """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 seconds_elapsed = 0 for i in range(9,-1,-1): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) seconds_elapsed += 1 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) seconds_elapsed += 1 c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson > -0.000001 and pearson < 0.000001 ) """ case: random """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100 seconds_elapsed = 0 for i in range(0,10000): offset = random.randrange(1, fake_price) fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + offset, low_bid=(fake_price + offset / 2) ) ) seconds_elapsed += 1 c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float rounding print('pearson of random chart: {}'.format(pearson) ) self.assertTrue( pearson > -0.02 and pearson < 0.02 )
def test_update(self): """ test: Chart.update() Constraints to verify: - Data is as recent as possible - start index has earliest timestamp - end index has latest timestamp - timestamps from start to end are sequential Cases: - old chart (complete update) - somewhat outdated chart (partially updated) - new chart (no updates other than last (incomplete) candle) """ """ case: old chart that gets completely updated """ # initial "outdated" chart chart = Chart( in_instrument=Instrument(4), granularity='M1', count=4999, end=datetime(year=2017, month=12, day=5) ) # Update chart chart.update() # Verify data is most recent time_since_close = Broker.get_time_since_close() now = datetime.utcnow() end_timestamp = chart.get_end_timestamp() if (Broker.get_time_until_close() == timedelta()): # Time since last candle should be close to time since market # close. The leniency is high to allow for periods of no new # candles. self.assertTrue( abs((now - end_timestamp) - (time_since_close)) < timedelta(minutes=62) ) else: # Time since last candle should be close to now. self.assertTrue(abs(now - end_timestamp) < timedelta(minutes=2)) # verify candle at start index has earliest timestamp. earliest_timestamp = datetime.utcnow() for i in range(0, chart.get_size()): if chart[i].timestamp < earliest_timestamp: earliest_timestamp = chart[i].timestamp self.assertTrue(chart.get_start_timestamp() == earliest_timestamp) # verify candle at end index has latest timestamp. latest_timestamp = datetime(year=1999, month=1, day=1) for i in range(0, chart.get_size()): if chart[i].timestamp > latest_timestamp: latest_timestamp = chart[i].timestamp self.assertTrue(chart.get_end_timestamp() == latest_timestamp) # Verify sequential timestamps for i in range(0, chart.get_size() - 1): self.assertTrue(chart[i].timestamp < chart[i + 1].timestamp) """ Chart that gets partially updated """ # TODO """
class BtcChartPlasmoid(plasmascript.Applet): def __init__(self, parent, args=None): plasmascript.Applet.__init__(self, parent) self._market = None self._thread = None self._interval = 1 def init(self): self.setHasConfigurationInterface(True) self.readConfig() self.setAspectRatioMode(Plasma.IgnoreAspectRatio) cfg = self.config() width, ok = cfg.readEntry('width', 256).toInt() height, ok = cfg.readEntry('height', 256).toInt() self.resize(width, height) self.applet.geometryChanged.connect(self.saveGeometry) self._theme = Plasma.Svg(self) self._theme.setImagePath('widgets/background') self.setBackgroundHints(Plasma.Applet.DefaultBackground) self._layout = QGraphicsGridLayout(self.applet) self.applet.setLayout(self._layout) self._chart = Chart(self.applet) self._chart.setZValue(1) self._layout.addItem(self._chart, 0, 0) self._data = Data(self) self.update() def update(self): self._timer = self.startTimer(self._interval*1000*60) self._chart.clearTrades() try: trades = self._data.init(self._market) except: pass self._chart.addTrades(trades) def timerEvent(self, event): try: self._chart.addTrades(self._data.getData()) except: pass def createConfigurationInterface(self, parent): parent.setButtons(KDialog.ButtonCode(KDialog.Ok | KDialog.Cancel | KDialog.Apply)) self._generalConfig = QWidget() self._generalConfigUi = uic.loadUi(self.package().filePath('ui', 'general_config.ui'), self._generalConfig) parent.addPage(self._generalConfigUi, 'General', self.icon(), QString(), False) self._generalConfigUi.intervalEdit.setValue(self._interval) if self._data: for market in self._data.markets(): self._generalConfigUi.marketCombo.addItem(market) if market == self._market: self._generalConfigUi.marketCombo.setCurrentIndex(self._generalConfigUi.marketCombo.count() - 1) self.connect(parent, SIGNAL('applyClicked()'), self.configUpdated) self.connect(parent, SIGNAL('okClicked()'), self.configUpdated) def configUpdated(self): self._market = self._generalConfigUi.marketCombo.currentText() self._interval = self._generalConfigUi.intervalEdit.value() self.update() self._data.init(self._market) self.writeConfig() def readConfig(self): cfg = self.config() self._market = cfg.readEntry('market', '').toString() self._interval, ok = cfg.readEntry('interval', 1).toInt() def writeConfig(self): cfg = self.config() cfg.writeEntry('market', self._market) cfg.writeEntry('interval', self._interval) self.emit(SIGNAL('configNeedsSaving()')) def saveGeometry(self): cfg = self.config() cfg.writeEntry('width', self.applet.geometry().width()) cfg.writeEntry('height', self.applet.geometry().height())
def stats(request, timeframe_url): regular_form = StatsForm() custom_form = CustomStatsForm() #get form data if user already has been on stats page if request.method == 'POST': form = StatsForm(request.POST) if form.is_valid(): timeframe = form.cleaned_data["timeframe"] period = form.cleaned_data["period"] if(timeframe == "timeframe_cus"): custom_stats_form = CustomStatsForm(request.POST) if custom_stats_form.is_valid(): start = datetime.datetime.strptime(request.POST.get('startfrom', datetime.datetime.now()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0)), "%m/%d/%Y") end = datetime.datetime.strptime(request.POST.get('endby', datetime.datetime.now()), "%m/%d/%Y") #we usually mean the end of that day end += relativedelta(days=1, hour=0, minute=0, second=0, microsecond=0) end -= relativedelta(day=0, hour=0, minute=0, seconds=1, microsecond=0) else: print "Invalid form input" print form.errors return render_to_response('charts/stats.html', vars(), RequestContext(request)) else: if timeframe == "timeframe_hrs": start = datetime.datetime.utcnow()+relativedelta(minute=0, second=0, microsecond=0) elif timeframe == "timeframe_day": if period == 'period_min' or period == 'period_hrs': localTime = datetime.datetime.now() localMidnight = datetime.datetime.combine(localTime, datetime.time(0)) timeSinceStartOfDay = localTime - localMidnight start = datetime.datetime.utcnow() - timeSinceStartOfDay # local midnight in UTC else: start = datetime.datetime.utcnow() + relativedelta(hour=0, minute=0, second=0, microsecond=0) # UTC midnight (1:00 here in Germany)) elif timeframe == "timeframe_mon": start = datetime.datetime.utcnow()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0) elif timeframe == "timeframe_yrs": start = datetime.datetime.utcnow()+relativedelta(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) end = datetime.datetime.utcnow() else: print "Invalid form input" print form.errors return render_to_response('charts/stats.html', vars(), RequestContext(request)) #user navigates to stats from main menu else: timeframe = timeframe_url #determine start and end date for the chart if timeframe != "timeframe_cus": if timeframe == "timeframe_hrs": start = datetime.datetime.utcnow()+relativedelta(minute=0, second=0, microsecond=0) period = 'period_min' elif timeframe == "timeframe_day": start = datetime.datetime.utcnow()+relativedelta(hour=0, minute=0, second=0, microsecond=0) period = 'period_hrs' elif timeframe == "timeframe_mon": start = datetime.datetime.utcnow()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0) period = 'period_day' elif timeframe == "timeframe_yrs": start = datetime.datetime.utcnow()+relativedelta(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) period = 'period_mon' end = datetime.datetime.utcnow() else: regular_form.fields["timeframe"].initial = timeframe return render_to_response('charts/stats.html', vars(), RequestContext(request)) #TODO: DRY regular_form.fields["timeframe"].initial = timeframe regular_form.fields["period"].initial = period #create a chart and fetch its data chart = Chart(start, end, period) graphs = [] for i in chart.getDeviceIDList(): chart.fetchTimeSeries(i) timetuples = chart.getTimeSeries(i) graphs.append({"label":"Einspeisung WR"+str(i), "data":timetuples}) timeseries = json.dumps(graphs) plotsettings = json.dumps(chart.chartOptions()) #TODO: the next couple of lines are pretty ugly stats = chart.getStatTable() hd =str(start)+" - "+str(end)+" | "+str(period) return render_to_response('charts/stats.html', vars(), RequestContext(request))
def _create_chart(self, *args, **kwargs): self.chart = Chart(self.grammar, *args, **kwargs) return self.chart
def initialize_chart(self): ''' Initialize chart Size of chart will be sentence_length+1 in both dimensions ''' self.chart = Chart(self.sentence_length)
class Game: def __init__(self, grid): self.__grid = grid self.__score = 0 self.__chart = Chart() self.__history = [] self.__state = State.running self.__undo_counter = 0 def slide_to(self, direction): if direction not in ['left', 'right', 'up', 'down']: return try: grid_before_slide = self.__grid.copy() points_gained, must_generate = { 'left': self.__grid.slide_left, 'right': self.__grid.slide_right, 'up': self.__grid.slide_up, 'down': self.__grid.slide_down }.get(direction)() except GridWinScoreReachedException as e: self.__state = State.game_won points_gained, must_generate = e.value if must_generate: self.__history.append((grid_before_slide, points_gained)) if len(self.__history) > constants.DEFAULT_HISTORY_LENGTH: self.__history.pop(0) self.__grid.generate_number() self.__score += points_gained else: if not self.__grid.can_slide(): if self.__state != State.game_won: self.__state = State.game_over def undo(self): if self.__undo_counter == constants.DEFAULT_HISTORY_LENGTH: return if len(self.__history) > 0: grid, score = self.__history.pop() self.__grid = grid self.__score -= score self.__undo_counter += 1 def start(self): self.__grid.generate_number() self.__grid.generate_number() def get_value_at(self, position): return self.__grid[position] def score(self): return self.__score def reset(self): self.__grid.reset() self.__history = [] self.__state = State.running self.__score = 0 self.__undo_counter = 0 self.start() def grid_dimensions(self): return self.__grid.dimensions() def get_state(self): return self.__state def change_state(self, state): if state in [State.running, State.game_over, State.game_won]: self.__state = state def undos_left(self): return constants.DEFAULT_HISTORY_LENGTH - self.__undo_counter def check_score(self): return self.__chart.is_top_score(self.__score) def add_player_to_chart(self, name, score): self.__chart.add(name, score) # TODO file not found handling def save_top_scores(self, filename): self.__chart.save(filename) def load_top_scores(self, filename): self.__chart.load(filename) def get_top_players(self): return self.__chart def reset_high_scores(self): self.__chart.reset()
def setUp(self): self.test_chart = Chart()
def setupUi(self, MainWindow): MainWindow.showMaximized() MainWindow.setObjectName(_fromUtf8("MainWindow")) #MainWindow.resize(1014, 682) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setLayoutDirection(QtCore.Qt.LeftToRight) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.gridLayout_4 = QtGui.QGridLayout(self.centralwidget) self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4")) self.frame_2 = QtGui.QFrame(self.centralwidget) self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtGui.QFrame.Raised) self.frame_2.setObjectName(_fromUtf8("frame_2")) self.gridLayout_3 = QtGui.QGridLayout(self.frame_2) self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3")) self.scrollArea_2 = QtGui.QScrollArea(self.frame_2) self.scrollArea_2.setWidgetResizable(True) self.scrollArea_2.setObjectName(_fromUtf8("scrollArea_2")) self.scrollAreaWidgetContents_2 = QtGui.QWidget() self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 459, 448)) self.scrollAreaWidgetContents_2.setObjectName(_fromUtf8("scrollAreaWidgetContents_2")) self.gridLayout_6 = QtGui.QGridLayout(self.scrollAreaWidgetContents_2) self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6")) self.modifided_image = ImageWidget(self.scrollAreaWidgetContents_2) self.modifided_image.setObjectName(_fromUtf8("modifided_image")) self.gridLayout_6.addWidget(self.modifided_image, 0, 0, 1, 1) self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2) self.gridLayout_3.addWidget(self.scrollArea_2, 0, 0, 1, 1) self.gridLayout_4.addWidget(self.frame_2, 0, 1, 1, 1) self.frame = QtGui.QFrame(self.centralwidget) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName(_fromUtf8("frame")) self.gridLayout_2 = QtGui.QGridLayout(self.frame) self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) self.scrollArea = QtGui.QScrollArea(self.frame) self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName(_fromUtf8("scrollArea")) self.scrollAreaWidgetContents = QtGui.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 459, 448)) self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents")) self.gridLayout_5 = QtGui.QGridLayout(self.scrollAreaWidgetContents) self.gridLayout_5.setObjectName(_fromUtf8("gridLayout_5")) self.original_image = ImageWidget(self.scrollAreaWidgetContents) self.original_image.setObjectName(_fromUtf8("original_image")) self.gridLayout_5.addWidget(self.original_image, 0, 0, 1, 1) self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.gridLayout_2.addWidget(self.scrollArea, 0, 0, 1, 1) self.gridLayout_4.addWidget(self.frame, 0, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.dockWidget = QtGui.QDockWidget(MainWindow) self.dockWidget.setObjectName(_fromUtf8("dockWidget")) self.dockWidget.setFloating(True) self.dockWidget.setGeometry(QtCore.QRect(0, 0, 620, 220)) self.dockWidgetContents = QtGui.QWidget() self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents")) self.horizontalLayout = QtGui.QHBoxLayout(self.dockWidgetContents) self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.frame_4 = QtGui.QFrame(self.dockWidgetContents) self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_4.setFrameShadow(QtGui.QFrame.Raised) self.frame_4.setObjectName(_fromUtf8("frame_4")) self.gridLayout_8 = QtGui.QGridLayout(self.frame_4) self.gridLayout_8.setObjectName(_fromUtf8("gridLayout_8")) self.original_histogram = Chart(self.frame_4) self.original_histogram.setObjectName(_fromUtf8("original_histogram")) self.gridLayout_8.addWidget(self.original_histogram, 0, 0, 1, 1) self.horizontalLayout.addWidget(self.frame_4) self.frame_3 = QtGui.QFrame(self.dockWidgetContents) self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_3.setFrameShadow(QtGui.QFrame.Raised) self.frame_3.setObjectName(_fromUtf8("frame_3")) self.gridLayout_7 = QtGui.QGridLayout(self.frame_3) self.gridLayout_7.setObjectName(_fromUtf8("gridLayout_7")) self.modifided_histogram = Chart(self.frame_3) self.modifided_histogram.setObjectName(_fromUtf8("modifided_histogram")) self.gridLayout_7.addWidget(self.modifided_histogram, 0, 0, 1, 1) self.horizontalLayout.addWidget(self.frame_3) self.dockWidget.setWidget(self.dockWidgetContents) MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1014, 22)) self.menubar.setObjectName(_fromUtf8("menubar")) self.menuFile = QtGui.QMenu(self.menubar) self.menuFile.setObjectName(_fromUtf8("menuFile")) self.menuOperations = QtGui.QMenu(self.menubar) self.menuOperations.setObjectName(_fromUtf8("menuOperations")) self.menuOperations.setEnabled(False) self.menuHistogram_Modification = QtGui.QMenu(self.menuOperations) self.menuHistogram_Modification.setObjectName(_fromUtf8("menuHistogram_Modification")) self.menuSlide_Histogram = QtGui.QMenu(self.menuHistogram_Modification) self.menuSlide_Histogram.setObjectName(_fromUtf8("menuSlide_Histogram")) self.menuMapping_Equations = QtGui.QMenu(self.menuOperations) self.menuMapping_Equations.setObjectName(_fromUtf8("menuMapping_Equations")) self.menuLocal_Mask_Operations = QtGui.QMenu(self.menuOperations) self.menuLocal_Mask_Operations.setObjectName(_fromUtf8("menuLocal_Mask_Operations")) self.menuSegmentation = QtGui.QMenu(self.menuOperations) self.menuSegmentation.setObjectName(_fromUtf8("menuSegmentation")) MainWindow.setMenuBar(self.menubar) self.actionOpen = QtGui.QAction(MainWindow) self.actionOpen.setObjectName(_fromUtf8("action_Open")) self.actionOpen.setShortcut('Ctrl+O') self.actionOpen.triggered.connect(self.showOpen) self.actionExit = QtGui.QAction(MainWindow) self.actionExit.setObjectName(_fromUtf8("actionExit")) self.actionExit.setShortcut('Ctrl+Q') self.actionExit.setStatusTip('Exit application') self.actionExit.triggered.connect(QtGui.qApp.quit) self.menuFile.addAction(self.actionOpen) self.menuFile.addAction(self.actionExit) self.actionStretch_Histogram = QtGui.QAction(MainWindow) self.actionStretch_Histogram.setObjectName(_fromUtf8("actionStretch_Histogram")) self.actionStretch_Histogram.triggered.connect(self.stretchHisto) self.actionShrink_Histogram = QtGui.QAction(MainWindow) self.actionShrink_Histogram.setObjectName(_fromUtf8("actionShrink_Histogram")) self.actionShrink_Histogram.triggered.connect(self.shrinkHisto) self.actionLeft = QtGui.QAction(MainWindow) self.actionLeft.setObjectName(_fromUtf8("actionLeft")) self.actionLeft.triggered.connect(self.slidesLeftHisto) self.actionRight = QtGui.QAction(MainWindow) self.actionRight.setObjectName(_fromUtf8("actionRight")) self.actionRight.triggered.connect(self.slidesRightHisto) self.actionHistogram_Equalization = QtGui.QAction(MainWindow) self.actionHistogram_Equalization.setObjectName(_fromUtf8("actionHistogram_Equalization")) self.actionHistogram_Equalization.triggered.connect(self.histogramEqualization) self.actionNegative = QtGui.QAction(MainWindow) self.actionNegative.setObjectName(_fromUtf8("actionNegative")) self.actionNegative.triggered.connect(self.negative) self.actionMean_Local_Filtering = QtGui.QAction(MainWindow) self.actionMean_Local_Filtering.setObjectName(_fromUtf8("actionMean_Local_Filtering")) self.actionMean_Local_Filtering.triggered.connect(self.meanFilter) self.actionMedian_Local_Filtering = QtGui.QAction(MainWindow) self.actionMedian_Local_Filtering.setObjectName(_fromUtf8("actionMedian_Local_Filtering")) self.actionMedian_Local_Filtering.triggered.connect(self.medianFilter) self.actionSobel_Edge_Detector = QtGui.QAction(MainWindow) self.actionSobel_Edge_Detector.setObjectName(_fromUtf8("actionSobel_Edge_Detector")) self.actionSobel_Edge_Detector.triggered.connect(self.edgeDetection) self.menuSlide_Histogram.addAction(self.actionLeft) self.menuSlide_Histogram.addAction(self.actionRight) self.menuHistogram_Modification.addAction(self.actionStretch_Histogram) self.menuHistogram_Modification.addAction(self.actionShrink_Histogram) self.menuHistogram_Modification.addAction(self.menuSlide_Histogram.menuAction()) self.menuHistogram_Modification.addAction(self.actionHistogram_Equalization) self.menuMapping_Equations.addAction(self.actionNegative) self.menuLocal_Mask_Operations.addAction(self.actionMean_Local_Filtering) self.menuLocal_Mask_Operations.addAction(self.actionMedian_Local_Filtering) self.menuSegmentation.addAction(self.actionSobel_Edge_Detector) self.menuOperations.addAction(self.menuHistogram_Modification.menuAction()) self.menuOperations.addAction(self.menuMapping_Equations.menuAction()) self.menuOperations.addAction(self.menuLocal_Mask_Operations.menuAction()) self.menuOperations.addAction(self.menuSegmentation.menuAction()) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuOperations.menuAction()) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow)
class ChartTests(unittest.TestCase): def setUp(self): self.test_chart = Chart() def test_empty_chart(self): for top_player in self.test_chart: self.assertEqual(top_player[0], '') self.assertEqual(top_player[1], 0) def test_chart_add(self): self.test_chart.add('User1', 100) self.test_chart.add('User2', 200) self.test_chart.add('User3', 300) self.test_chart.add('User4', 400) self.test_chart.add('User5', 500) self.test_chart.add('User6', 600) self.test_chart.add('User7', 700) self.test_chart.add('User8', 800) self.test_chart.add('User9', 900) self.test_chart.add('User10', 1000) self.test_chart.add('User11', 50) for index in range(10): self.assertEqual( self.test_chart.top_players[index][0], ('User' + str(10 - index))) self.assertEqual( self.test_chart.top_players[index][1], (1000 - index * 100)) for top_player in self.test_chart.top_players: self.assertNotEqual(top_player[0], 'User11') def test_chart_is_top_score(self): self.test_chart.add('User1', 100) self.test_chart.add('User2', 200) self.test_chart.add('User3', 300) self.test_chart.add('User4', 400) self.test_chart.add('User5', 500) self.test_chart.add('User6', 600) self.test_chart.add('User7', 700) self.test_chart.add('User8', 800) self.test_chart.add('User9', 900) self.test_chart.add('User10', 1000) self.assertTrue(self.test_chart.is_top_score(1500)) self.assertTrue(self.test_chart.is_top_score(500)) self.assertTrue(self.test_chart.is_top_score(100)) self.assertFalse(self.test_chart.is_top_score(50)) def test_chart_reset(self): self.test_chart.add('User1', 100) self.test_chart.add('User2', 200) self.test_chart.add('User3', 300) self.test_chart.add('User4', 400) self.test_chart.add('User5', 500) self.test_chart.add('User6', 600) self.test_chart.add('User7', 700) self.test_chart.add('User8', 800) self.test_chart.add('User9', 900) self.test_chart.add('User10', 1000) self.test_chart.reset() for top_player in self.test_chart: self.assertEqual(top_player[0], '') self.assertEqual(top_player[1], 0) def test_chart_save(self): self.test_chart.add('User10', 100) self.test_chart.add('User9', 200) self.test_chart.add('User8', 300) self.test_chart.add('User7', 400) self.test_chart.add('User6', 500) self.test_chart.add('User5', 600) self.test_chart.add('User4', 700) self.test_chart.add('User3', 800) self.test_chart.add('User2', 900) self.test_chart.add('User1', 1000) self.test_chart.save('save_test.dat') import os self.assertTrue(os.path.isfile('save_test.dat')) def test_chart_load(self): self.test_chart.load('save_test.dat') for index in range(10): self.assertEqual( self.test_chart.top_players[index][0], ('User' + str(index + 1))) self.assertEqual( self.test_chart.top_players[index][1], (1000 - index * 100))
class PlotView(TopicMessageView): name = 'Plot' rows = [( 1, 'All'), ( 2, 'Every 2nd message'), ( 3, 'Every 3rd message'), ( 4, 'Every 4th message'), ( 5, 'Every 5th message'), ( 10, 'Every 10th message'), ( 20, 'Every 20th message'), ( 50, 'Every 50th message'), ( 100, 'Every 100th message'), (1000, 'Every 1000th message')] def __init__(self, timeline, parent): TopicMessageView.__init__(self, timeline, parent) self._topic = None self._message = None self._plot_paths = [] self._playhead = None self._chart = Chart() self._data_loader = None self._x_view = None self._dirty_count = 0 self._csv_data_loader = None self._csv_path = None self._csv_row_stride = None self._clicked_pos = None self._dragged_pos = None self._configure_frame = None self._max_interval_pixels = 1.0 tb = self.parent.ToolBar icons_dir = roslib.packages.get_pkg_dir(PKG) + '/icons/' tb.AddSeparator() tb.Bind(wx.EVT_TOOL, lambda e: self.configure(), tb.AddLabelTool(wx.ID_ANY, '', wx.Bitmap(icons_dir + 'cog.png'))) self.parent.Bind(wx.EVT_SIZE, self._on_size) self.parent.Bind(wx.EVT_PAINT, self._on_paint) self.parent.Bind(wx.EVT_LEFT_DOWN, self._on_left_down) self.parent.Bind(wx.EVT_MIDDLE_DOWN, self._on_middle_down) self.parent.Bind(wx.EVT_RIGHT_DOWN, self._on_right_down) self.parent.Bind(wx.EVT_LEFT_UP, self._on_left_up) self.parent.Bind(wx.EVT_MIDDLE_UP, self._on_middle_up) self.parent.Bind(wx.EVT_RIGHT_UP, self._on_right_up) self.parent.Bind(wx.EVT_MOTION, self._on_mouse_move) self.parent.Bind(wx.EVT_MOUSEWHEEL, self._on_mousewheel) self.parent.Bind(wx.EVT_CLOSE, self._on_close) wx.CallAfter(self.configure) ## TopicMessageView implementation def message_viewed(self, bag, msg_details): TopicMessageView.message_viewed(self, bag, msg_details) topic, msg, t = msg_details if not self._data_loader: self._topic = topic self.start_loading() self._message = msg self.playhead = (t - self.timeline.start_stamp).to_sec() def message_cleared(self): self._message = None TopicMessageView.message_cleared(self) wx.CallAfter(self.parent.Refresh) def timeline_changed(self): # If timeline end_stamp is within the plot view, then invalidate the data loader if self._x_view is not None: end_elapsed = (self.timeline.end_stamp - self.timeline.start_stamp).to_sec() if end_elapsed > self._x_view[0] and end_elapsed < self._x_view[1] and self._data_loader: self._data_loader.invalidate() wx.CallAfter(self.parent.Refresh) # property: plot_paths def _get_plot_paths(self): return self._plot_paths def _set_plot_paths(self, plot_paths): self._plot_paths = plot_paths # Update the data loader with the paths to plot if self._data_loader: paths = [] for plot in self._plot_paths: for path in plot: if path not in paths: paths.append(path) self._data_loader.paths = paths # Update the chart with the new areas self._chart.create_areas(self._plot_paths) self._update_max_interval() wx.CallAfter(self.parent.Refresh) plot_paths = property(_get_plot_paths, _set_plot_paths) # property: playhead def _get_playhead(self): return self._playhead def _set_playhead(self, playhead): self._playhead = playhead # Check if playhead is visible. If not, then move the view region. if self._x_view is not None: if self._playhead < self._x_view[0]: x_view = self._x_view[1] - self._x_view[0] self._x_view = (self._playhead, self._playhead + x_view) self._update_data_loader_interval() elif self._playhead > self._x_view[1]: x_view = self._x_view[1] - self._x_view[0] self._x_view = (self._playhead - x_view, self._playhead) self._update_data_loader_interval() wx.CallAfter(self.parent.Refresh) playhead = property(_get_playhead, _set_playhead) def _update_max_interval(self): if not self._data_loader: return if len(self._chart.areas) > 0: secs_per_px = (self._data_loader.end_stamp - self._data_loader.start_stamp).to_sec() / self._chart._width self._data_loader.max_interval = secs_per_px * self._max_interval_pixels ## Events def _on_paint(self, event): if not self._data_loader or len(self._chart._areas) == 0: return self._update_chart_info() dc = wx.lib.wxcairo.ContextFromDC(wx.PaintDC(self.parent)) self._chart.paint(dc) def _update_chart_info(self): for area_index, plot in enumerate(self._plot_paths): area = self._chart.areas[area_index] area.x_view = self._x_view if self._message is not None: area.x_indicator = self._playhead else: area.x_indicator = None area.x_range = (0.0, (self.timeline.end_stamp - self.timeline.start_stamp).to_sec()) if self._data_loader.is_load_complete: area.data_alpha = 1.0 else: area.data_alpha = 0.5 area._series_list = plot data = {} for plot_path in plot: if plot_path in self._data_loader._data: data[plot_path] = self._data_loader._data[plot_path] area._series_data = data def _on_size(self, event): self._chart.set_size(self.parent.ClientSize) self._update_max_interval() def _on_left_down(self, event): self._clicked_pos = self._dragged_pos = event.Position if len(self._chart.areas) > 0 and self._chart.areas[0].view_min_x is not None and self._chart.areas[0].view_max_x is not None: self.timeline.playhead = self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._chart.areas[0].x_chart_to_data(event.Position[0]))) def _on_middle_down(self, event): self._clicked_pos = self._dragged_pos = event.Position def _on_right_down(self, event): self._clicked_pos = self._dragged_pos = event.Position self.parent.PopupMenu(PlotPopupMenu(self.parent, self), self._clicked_pos) def _on_left_up (self, event): self._on_mouse_up(event) def _on_middle_up(self, event): self._on_mouse_up(event) def _on_right_up (self, event): self._on_mouse_up(event) def _on_mouse_up(self, event): self.parent.Cursor = wx.StockCursor(wx.CURSOR_ARROW) def _on_mouse_move(self, event): x, y = event.Position if event.Dragging(): if event.MiddleIsDown() or event.ShiftDown(): # Middle or shift: zoom dx_click, dy_click = x - self._clicked_pos[0], y - self._clicked_pos[1] dx_drag, dy_drag = x - self._dragged_pos[0], y - self._dragged_pos[1] if dx_drag != 0 and len(self._chart.areas) > 0: dsecs = self._chart.areas[0].dx_chart_to_data(dx_drag) # assuming areas share x axis self._translate_plot(dsecs) if dy_drag != 0: self._zoom_plot(1.0 + 0.005 * dy_drag) self._update_data_loader_interval() wx.CallAfter(self.parent.Refresh) self.parent.Cursor = wx.StockCursor(wx.CURSOR_HAND) elif event.LeftIsDown(): if len(self._chart.areas) > 0 and self._chart.areas[0].view_min_x is not None and self._chart.areas[0].view_max_x is not None: self.timeline.playhead = self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._chart.areas[0].x_chart_to_data(x))) wx.CallAfter(self.parent.Refresh) self._dragged_pos = event.Position def _on_mousewheel(self, event): dz = event.WheelRotation / event.WheelDelta self._zoom_plot(1.0 - dz * 0.2) self._update_data_loader_interval() wx.CallAfter(self.parent.Refresh) def _on_close(self, event): if self._configure_frame: self._configure_frame.Close() self.stop_loading() event.Skip() ## def _update_data_loader_interval(self): self._data_loader.set_interval(self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._x_view[0])), self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._x_view[1]))) def _zoom_plot(self, zoom): if self._x_view is None: self._x_view = (0.0, (self.timeline.end_stamp - self.timeline.start_stamp).to_sec()) x_view_interval = self._x_view[1] - self._x_view[0] if x_view_interval == 0.0: return playhead_fraction = (self._playhead - self._x_view[0]) / x_view_interval new_x_view_interval = zoom * x_view_interval # Enforce zoom limits (0.1s, 1.5 * range) max_zoom_interval = (self.timeline.end_stamp - self.timeline.start_stamp).to_sec() * 1.5 if new_x_view_interval > max_zoom_interval: new_x_view_interval = max_zoom_interval elif new_x_view_interval < 0.1: new_x_view_interval = 0.1 interval_0 = self._playhead - playhead_fraction * new_x_view_interval interval_1 = interval_0 + new_x_view_interval timeline_range = (self.timeline.end_stamp - self.timeline.start_stamp).to_sec() interval_0 = min(interval_0, timeline_range - 0.1) interval_1 = max(interval_1, 0.1) self._x_view = (interval_0, interval_1) self._update_max_interval() def _translate_plot(self, dsecs): if self._x_view is None: self._x_view = (0.0, (self.timeline.end_stamp - self.timeline.start_stamp).to_sec()) new_start = self._x_view[0] - dsecs new_end = self._x_view[1] - dsecs timeline_range = (self.timeline.end_stamp - self.timeline.start_stamp).to_sec() new_start = min(new_start, timeline_range - 0.1) new_end = max(new_end, 0.1) self._x_view = (new_start, new_end) ## def stop_loading(self): if self._data_loader: self._data_loader.stop() self._data_loader = None def start_loading(self): if self._topic and not self._data_loader: self._data_loader = PlotDataLoader(self.timeline, self._topic) self._data_loader.add_progress_listener(self._data_loader_updated) self._data_loader.add_complete_listener(self._data_loader_complete) self._data_loader.start() def _data_loader_updated(self): self._dirty_count += 1 if self._dirty_count > 5: wx.CallAfter(self.parent.Refresh) self._dirty_count = 0 def _data_loader_complete(self): wx.CallAfter(self.parent.Refresh) def configure(self): if self._configure_frame is not None or self._message is None: return self._configure_frame = PlotConfigureFrame(self) frame = self.parent.TopLevelParent self._configure_frame.Position = (frame.Position[0] + frame.Size[0] + 10, frame.Position[1]) self._configure_frame.Show() ## Export to CSV... def export_csv(self, rows): dialog = wx.FileDialog(self.parent.Parent, 'Export to CSV...', wildcard='CSV files (*.csv)|*.csv', style=wx.FD_SAVE) if dialog.ShowModal() == wx.ID_OK: if self.timeline.start_background_task('Exporting to "%s"' % dialog.Path): wx.CallAfter(wx.GetApp().GetTopWindow().StatusBar.gauge.Show) export_series = set() for plot in self._plot_paths: for path in plot: export_series.add(path) if self._x_view is None: self._x_view = (0.0, (self.timeline.end_stamp - self.timeline.start_stamp).to_sec()) self._csv_path = dialog.Path self._csv_row_stride = rows self._csv_data_loader = PlotDataLoader(self.timeline, self._topic) self._csv_data_loader.add_complete_listener(self._csv_data_loaded) self._csv_data_loader.paths = export_series self._csv_data_loader.set_interval(self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._x_view[0])), self.timeline.start_stamp + rospy.Duration.from_sec(max(0.01, self._x_view[1]))) self._csv_data_loader.start() dialog.Destroy() def _csv_data_loaded(self): # Collate data i = 0 series_dict = {} unique_stamps = set() for series in self._csv_data_loader._data: d = {} series_dict[series] = d point_num = 0 for x, y in self._csv_data_loader._data[series].points: if point_num % self._csv_row_stride == 0: d[x] = y unique_stamps.add(x) point_num += 1 i += 1 series_columns = sorted(series_dict.keys()) try: csv_writer = csv.DictWriter(open(self._csv_path, 'w'), ['elapsed'] + series_columns) # Write header row header_dict = { 'elapsed' : 'elapsed' } for column in series_columns: header_dict[column] = column csv_writer.writerow(header_dict) # Initialize progress monitoring progress = 0 def update_progress(v): wx.GetApp().TopWindow.StatusBar.progress = v total_stamps = len(unique_stamps) stamp_num = 0 # Write data for stamp in sorted(unique_stamps): if self.timeline.background_task_cancel: break row = { 'elapsed' : stamp } for column in series_dict: if stamp in series_dict[column]: row[column] = series_dict[column][stamp] csv_writer.writerow(row) new_progress = int(100.0 * (float(stamp_num) / total_stamps)) if new_progress != progress: progress = new_progress wx.CallAfter(update_progress, progress) stamp_num += 1 except Exception, ex: print >> sys.stderr, 'Error writing to CSV file: %s' % str(ex) # Hide progress monitoring if not self.timeline.background_task_cancel: wx.CallAfter(wx.GetApp().TopWindow.StatusBar.gauge.Hide) self.timeline.stop_background_task()
class CkyParser(Parser): """ CkyParser is the central class for the jazz chord sequence recogniser parsing mechanism. It constitutes the "algorithm" module of the system. It begins with a set of signs assigned to the input by the tagger and parses to produce a chart, from which the resultant signs can be extracted. """ shell_tools = [ ChartTool(), InteractiveChartTool(), ] PARSER_OPTIONS = Parser.PARSER_OPTIONS + [ ModuleOption('max_iter', filter=int, help_text="Maximum number of parser iterations to perform "\ "before giving up. If 0 or unspecified, continues "\ "until parse is complete.", usage="max_iter=X, where X is an integer.", default=0, ), ModuleOption('min_iter', filter=int, help_text="Usually, the parser will stop as soon as it finds a "\ "full parse. Use min_iter to make it continue parsing until "\ "it has done min_iter iterations or the tagger has ceased to "\ "return any categories. Use -1 to keep going until the tagger "\ "gives no more categories.", usage="min_iter=X, where X is an integer.", default=0, ), ModuleOption('parses', filter=int, help_text="Number of parses to require before we terminate. "\ "Default is 1: the parser will terminate as soon as it finds "\ "at least one full parse (unless another option, like "\ "min_iter, prevents it", usage="parses=X, where X is an integer", default=1, ), ModuleOption('timeout', filter=int, help_text="Maximum time allowed for the main parse loop, in "\ "minutes. If this is exceded, the backoff will kick "\ "in, if one is specified. Otherwise, no results will be "\ "returned. The parser will not stop as soon as the timeout "\ "expires, but after finishing processing the current input "\ "word. 0 (default) imposes no timeout.", usage="timeout=X, where X is an integer number of seconds.", default=0, ), ModuleOption('inspect', filter=str_to_bool, help_text="If true, the graphical chart inspector will be "\ "displayed during parsing.", usage="inspect=X, where X is a boolean value.", default=False ), ModuleOption('inspect_persist', filter=str_to_bool, help_text="Makes the chart inspector window persist after parsing "\ "is completed. By default, it will be killed", usage="inspect_persist=X, where X is a boolean value.", default=False ), ModuleOption('dump_chart', filter=new_file_option, help_text="A file to dump the chart state to during parsing. "\ "The first dump will be when the chart is created and "\ "new dumps will be made throughout the parse.", usage="dump_chart=X, where X is a filename." ), ModuleOption('derivations', filter=str_to_bool, help_text="Store derivation traces along with the results", usage="derivations=X, where X is a boolean value", default=None, ), ] def _create_chart(self, *args, **kwargs): self.chart = Chart(self.grammar, *args, **kwargs) return self.chart def _add_signs(self, offset=0, prob_adder=None): """ Adds new signs to the chart from the supertagger, using the given offset when requesting them from the tagger. @rtype: list of tuples @return: all the signs that were actually added. Each is represented by a tuple (start_node, end_node, sign) """ signs = self.tagger.get_signs(offset) words = self.tagger.get_string_input() if signs is None or len(signs) == 0: return [] # Add each new sign to the chart added = [] for (start,end,signtup) in signs: word_list = words[start:end] word = " ".join(w for w in word_list) # Add the probabilities as an attribute to the signs cat,tag,prob = signtup if prob_adder is not None: prob_adder(start, end, signtup, word_list) # Add the signs to the chart newadd = self.chart.add_word_signs([signtup[0]], start, word, end_node=end) # Keep a record of those that got added if newadd: added.append((start,end,signtup)) return added def parse(self, derivations=False, summaries=False, inspect=False): """ Run the parser on the input, using the specified tagger. Runs the CKY parsing algorithm to do chart parsing. For details of chart parsing, see Chart class. If the parser was given a maximum number of iterations, the routine will return as usual after this number is completed, even if no parses have been found. @type derivations: bool @param derivations: store derivation traces, which can subsequently be used to trace all the derivations that led to any given sign in the chart. Overridden by the module option if it's given @type summaries: int/bool @param summaries: output chart summary information to stderr during parsing to track progress. Set to 2 to output some info, but not the full chart. @type inspect: bool @param inspect: launch a graphical chart inspector during the parse to display interactive chart information. @return: a list of signs that span the full input. """ if 'derivations' in self.options and self.options['derivations'] is not None: derivations = self.options['derivations'] # Time excecution if we're showing any summaries time = bool(summaries) # Find out from the tagger how long the input it read in was input_length = self.tagger.input_length # Create and initialise a chart for parsing # Don't initialise the chart with signs - we'll add signs gradually instead chart = self._create_chart( [[]]*input_length, derivations=derivations) # Launch a chart inspector if requested if self.options['inspect'] or inspect: # Get a string form of the input to display input_strs = self.tagger.get_string_input() chart.launch_inspector(input=input_strs) # Start dumping the chart if requested if self.options['dump_chart']: # Make the first dump of the empty chart from .chart import dump_chart dump_chart(chart, self.options['dump_chart']) # Stop after a given number of iterations if self.options['max_iter'] == 0: max_iter = None else: max_iter = self.options['max_iter'] if self.options['min_iter'] == -1: # Special case: never stop until we've got all the categories min_iter = None else: min_iter = self.options['min_iter'] required_parses = self.options['parses'] timeout = 60*self.options['timeout'] check_timeout = timeout>0 # Make sure the timed out flag is unset to start with self.timed_out = False # This is where progress output will go # Note that it's not the same as logger, which is the main system logger prog_logger = self.logger if check_timeout: prog_logger.info("Due to timeout after %d mins" % self.options['timeout']) ################################################## ### Here is the parser itself. # Keep track of how long since we started for timing out timeout_timer = ExecutionTimer(clock=True) signs_taken = [0]*input_length offset = 0 last_lexicals = [0]*(input_length) try: # Keep adding signs until none left, or we get a full parse, # or we complete the maximum iterations allowed # Keep going if min_iter is None (special value meaning don't stop # when we get a parse while (min_iter is None or (offset < min_iter) \ or len(chart.parses) < required_parses): if max_iter is not None and offset >= max_iter: # Exceded maximum number of iterations: give up prog_logger.info("Reached maximum number of iterations: "\ "continuing to backoff/fail") break prog_logger.info(">>> Parsing iteration: %d" % (offset+1)) # Get new signs from the tagger added = self._add_signs(offset=offset) # Note whether we added anything new if added: # Apply unary rules to these new signs added_spans = set([(start,end) for (start,end,sign) in added]) for (start,end) in added_spans: chart.apply_unary_rules(start,end) else: # No new signs added by the tagger: no point in continuing prog_logger.info("No new signs added: ending parse") break ##### Main parser loop: produce all possible results # Set end point to each node for end in range(1,input_length+1): if time: # Start a timer timer = ExecutionTimer() chart.apply_unary_rules(end-1, end) # Set start point to each node before the end, in reverse order for start in range(end-2,-1,-1): for middle in range(start+1,end): chart.apply_binary_rules(start, middle, end) # Check whether the timeout has expired and don't process # any more if it has if check_timeout: # Check whether the timeout has passed if int(timeout_timer.get_time()) > timeout: # Move on to post-parse stuff raise ParserTimeout # Check for new unary rule applications chart.apply_unary_rules(start, end) if summaries: prog_logger.info("Completed parsing up to node %d / %d (%.2f secs)" % (end,input_length, timer.get_time())) if summaries != 2: prog_logger.info(chart.summary) if self.options['dump_chart']: # Dump an update of the chart to the file dump_chart(chart, self.options['dump_chart']) if summaries: prog_logger.info("Completed parsing to end of sequence") if summaries != 2: prog_logger.info(chart.summary) offset += 1 except ParserTimeout: # The given timeout elapsed: just continue with no parses prog_logger.info("Parse timeout (%d mins) expired: continuing "\ "to backoff/fail" % self.options['timeout']) # Set the timed_out flag so we can check later whether we timed out self.timed_out = True except KeyboardInterrupt: # We pass the interrupt on to a higher level, but first kill # the inspector window, so it doesn't hang around and mess up self.chart.kill_inspector() raise parses = chart.parses if len(parses) == 0 and self.backoff is not None: prog_logger.info("Using backoff model") backoff_results = self.run_backoff() if len(backoff_results) > 0: for res in backoff_results: # Put the semantics result into a sign, with a dummy # syntactic category sign = self.grammar.formalism.Syntax.Sign( self.grammar.formalism.Syntax.DummyCategory(), res) # If the semantics has a probability, put this on the sign if hasattr(res, "probability"): sign.probability = res.probability parses.append(sign) elif len(parses): prog_logger.info("Parse finished with %d results" % len(parses)) else: prog_logger.info("Parse finished with no results") # Close the inspector window if one was opened if not self.options['inspect_persist']: self.chart.kill_inspector() return parses
cli.draw_table_header() cli.cprint_row([row[0], "", row[1], "", row[2], "", row[3], ""]) else: print "File content not readable at iteration: " + str(i) i = i + 1 except KeyboardInterrupt: print "\n" cli.print_centered_with_symbol("", "-") print "Acquisition has stopped." print "\n\n" if cli.plot_menu().upper() == "Y": # processes data and plots chart in plotly data = txt.parse() chart1 = Chart(txt.get_name(), "default", data) chart1.plot_mult() else: pass ard.write("C0") # sends command to Arduino stop acquisition print "\nExiting...\n" if op.upper() == "Y": path = os.path.dirname(os.path.abspath(__file__)) print "Acquired data saved in " + path + "/" + txt.get_name() + ".txt" print "\n" # plotChart(data[0], data[1], txt.get_name())
def __init__( self, fname ): f = open( fname ).read() self.grammar = CFG.fromstring( f ) self.chart = Chart() self.chart.trace = True return
def reset( self, tokens ): self.chart = Chart() self.words = tokens self.todo = [] return
class BottomUpChartParser: grammar = None # Grammar object that includes lexicon and # production rules queue = None # Queue object on which new edges are stacked chart = None # Chart object in which edges are stored for the # final parse generation sentence_length = 0 will_print_chart = True # Set to false if you want to deactivate printing of the found parses def __init__(self, grammar): self.grammar = Grammar(grammar) def parse(self, sentence, number_of_parses, strategy): ''' Parse the input sentence This is the central method to be called from outside. ''' ### Preprocessing ### # Tokenize input sentence tokens = self.tokenize(sentence) self.sentence_length = len(tokens) # Check for unknown tokens unknown_words = self.get_unknown_words(tokens) if unknown_words: # TODO: Run fallback solutions to fix unknown words, else # raise exception raise ParseException("Sentence contains unknown words (%s). Please try again!" % ', '.join(unknown_words)) ### Main steps ### # (1) Initialize empty chart and queue self.initialize_chart() self.initialize_queue(strategy) # (2) For every token, create a complete edge and push it to # the queue self.init_rule(tokens) # Iteration counter for evaluation purposes iters = 0 # (3) Repeat until no more edges are added # or sufficient number of parses has been found: while not self.queue.is_empty() and not self.enough_parses_found(number_of_parses): iters = iters + 1 # (3.1) Add next element on queue to the chart edge = self.queue.get_next_edge() self.chart.add_edge(edge) # (3.2) If input edge is complete, # apply predict rule and fundamental rule. # If input edge is incomplete, # apply fundamental rule only if edge.is_complete(): self.predict_rule(edge) self.fundamental_rule(edge) # (3.3) For alt search strategy, run search rule # if input edge is a complete parse # or last element of priority queue if strategy == 'altsearch': if ( ( (not self.queue.is_priority_active()) # Case 1: Complete parse was added to chart and edge.get_prod_rule().get_lhs() == 'S' and edge.is_complete() and edge.get_start() == 0 and edge.get_end() == self.sentence_length ) or (self.queue.is_priority_active() # Case 2: Priority queue emptied and self.queue.is_priority_empty() ) ): self.search_rule(edge) # 4) Display generated parses s_edges = self.chart.get_s_edges() print '%s parses found after %s iterations:' % (len(s_edges),iters) if self.will_print_chart : self.display_parses() else: for s_edge in s_edges: print 'Found s-edge: %s' % s_edge def tokenize(self, sentence): ''' Separate a sentence into a list of tokens and return the list. Currently this simply splits at each whitespace character with no special preprocessing ''' return sentence.split() def get_unknown_words(self, tokens): ''' Check list of tokens for unknown words by consulting the lexicon and return them ''' lexicon = self.grammar.get_lexicon() unknown_words = [token for token in tokens if token not in lexicon] return unknown_words def initialize_chart(self): ''' Initialize chart Size of chart will be sentence_length+1 in both dimensions ''' self.chart = Chart(self.sentence_length) def initialize_queue(self, strategy): ''' Initialize queue according to the parsing strategy chosen by the user ''' if strategy == 'fifo': self.queue = Queue() elif strategy == 'bestfirst': self.queue = BestFirstQueue() elif strategy == 'altsearch': self.queue = AltSearchQueue(self.sentence_length+1) else: raise QueueException('Invalid strategy (%s). Please try again and choose a strategy from the following set: {fifo, bestfirst, altsearch}' % strategy) def init_rule(self, tokens): ''' Generate initial edges for all given tokens and add them to the queue Formal definition: For every word w_i add the edge [w_i -> . , (i, i+1)] ''' node = -1 # Position between tokens of sentence # (0 is start of sentence) for token in tokens: node += 1 rule = ProductionRule(token, [], 1.0) edge = Edge(node, node+1, rule, 0, []) self.queue.add_edge(edge) def enough_parses_found(self, number_of_parses): ''' Check if enough parses have been found for the input sentence Return True if the number of complete S edges that the chart contains is >= the number of parses that the user wants, else False ''' return False if (number_of_parses == -1 or len(self.chart.get_s_edges()) < number_of_parses) else True def predict_rule(self, complete_edge): ''' If the LHS of a complete edge can be the first RHS element of a production rule, create a self-loop edge with that rule and push it to the queue Input: Complete edge Push to queue: Incomplete self-loop edges Formal definition: For each complete edge [A -> alpha . , (i, j)] and each production rule B -> A beta, add the self-loop edge [B -> . A beta , (i, i)] ''' start = complete_edge.get_start() lhs = complete_edge.get_prod_rule().get_lhs() parent_rules = self.grammar.get_possible_parent_rules(lhs) for parent_rule in parent_rules: new_edge = Edge(start, start, parent_rule, 0, []) if not self.queue.has_edge(new_edge) and not self.chart.has_edge(new_edge): self.queue.add_edge(new_edge) def fundamental_rule(self, input_edge): ''' If an incomplete edge can be advanced by a complete edge, create a new edge with the advanced dot. Create new edges (which can be complete or incomplete) by "advancing the dot", i.e. by matching incomplete edges with appropriate complete ones: (1) If the input edge is incomplete, find all complete edges - whose start node equals the end node of the input edge - whose LHS matches the RHS element that the input edge is currently looking for. If the input edge is complete, find all incomplete edges - whose end node equals the start node of the input edge - whose dot can be advanced by pairing them with the input edge. (2) From every pairing, create a new edge with the dot advanced over the RHS element that has just been found. (3) Push that edge to the queue IFF it does not exist already, i.e. if it has not been added to the chart or the queue before. This constraint keeps the parser from entering an infinite loop when using left-recursive grammar rules. Input: Single edge Push to queue: Complete and incomplete edges Formal definition: If the chart contains the edges [A -> alpha . B beta, (i, j)] and [B -> gamma . , (j, k)] then add a new edge [A -> alpha B . beta, (i, k)]. ''' if input_edge.is_complete(): j = input_edge.get_start() incomplete_edges = [edge for edge \ in self.chart.get_edges_ending_at(j) \ if not edge.is_complete()] complete_edges = [input_edge] else: j = input_edge.get_end() incomplete_edges = [input_edge] complete_edges = [edge for edge \ in self.chart.get_edges_starting_at(j) \ if edge.is_complete()] ### New Edges ### for incomp_edge in incomplete_edges: # Prepare info from incomplete edge that is necessary to ... prod_rule = incomp_edge.get_prod_rule() dot = incomp_edge.get_dot() next_missing_dtr = prod_rule.get_rhs_element(dot) for comp_edge in complete_edges: # ... check for compatibility with complete edges: if next_missing_dtr == comp_edge.get_prod_rule().get_lhs(): # Prepare additional info from incomplete edge i = incomp_edge.get_start() known_dtrs = incomp_edge.get_known_dtrs() # Prepare info from complete edge k = comp_edge.get_end() # Combine info from both edges, # and use it to create new edge new_dtrs = known_dtrs + [comp_edge] new_edge = Edge(i, k, prod_rule, dot+1, new_dtrs) # Add new edge to queue if not self.queue.has_edge(new_edge) and not self.chart.has_edge(new_edge): self.queue.add_edge(new_edge) def search_rule(self, s_edge): 'Scans queue for priority edges. See project report for detailed explanation' self.queue.activate_priority_queue() # Check for further complete s-edges on queue new_s_edge = self.queue.get_next_particular_edge('S', 0, self.sentence_length) if not new_s_edge == None: # Further s-edge was found self.queue.add_edge(new_s_edge) else: # No further s-edge remained on queue s_edges = self.chart.get_s_edges() # Sort s-edges by likelihood sorted_edges = [] for s_edge in s_edges: pos = bisect([edge.get_prob() for edge in sorted_edges], s_edge) sorted_edges.insert(pos, s_edge) # Check s-edges until alternative parses are found while len(s_edges) > 0 and self.queue.is_empty: s_edge = s_edges.pop() dtrs = s_edge.get_known_dtrs() # Iterate through depths until alternatives are found # or tree is exhausted while len(dtrs) > 0: # print "---" for dtr in dtrs: lhs = dtr.get_prod_rule().get_lhs() start = dtr.get_start() end = dtr.get_end() alt_dtr = self.queue.get_next_particular_edge(lhs, start, end) while not alt_dtr == None: self.queue.add_edge(alt_dtr) alt_dtr = self.queue.get_next_particular_edge(lhs, start, end) if self.queue.is_empty: # If no alt edge was found on this level, # check all elements of next lower level. for dtr in dtrs: mthrs = dtrs dtrs = [] for mthr in mthrs: dtrs.extend(mthr.get_known_dtrs()) def display_parses(self): ''' Display parse trees for all successful parses ''' s_edges = self.chart.get_s_edges() if len(s_edges) == 0: raise ParseException("No parse could be found.") for s_edge in s_edges: parse_string = self.build_parse_string_from_edge(s_edge, 'S') print self.add_indentation_to_parse_string(parse_string) + '\t' + str(s_edge.get_prob()) def build_parse_string_from_edge(self, edge, root): ''' Recursively work your way down through the known daughters of the input edge; return a bracketed structure representing the parse tree. In order to obtain a complete structure, this method needs to be called with a string representing the appropriate tree root (as the second argument) ''' if not edge.get_known_dtrs() == []: for dtr in edge.get_known_dtrs(): root += ' [ ' + dtr.get_prod_rule().get_lhs() + self.build_parse_string_from_edge(dtr, '') + ' ]' return root def add_indentation_to_parse_string(self, parse_string): ''' Convert flat string representation of parse to appropriately indented structure ''' parse_string = '[ ' + parse_string + ' ]' indented_string = '' level = -1 for char in parse_string: if char == '[': level += 1 indented_string += '\n' + '\t'*level + char elif char == ']': level -= 1 indented_string += char else: indented_string += char return indented_string
class Parser(): # 1.a. Define a class Parser. The constructor should take a filename # as argument, and do the following. Load a CFG from the file and # store it in the member grammar. Create a chart and store it in # the member chart def __init__( self, fname ): f = open( fname ).read() self.grammar = CFG.fromstring( f ) self.chart = Chart() self.chart.trace = True return # 1.b. Also define the method reset. It takes a sentence (a list of tokens) # as input, and stores it in the member words. It also resets the chart, # and sets the value of the memebr todo to the empty list. def reset( self, tokens ): self.chart = Chart() self.words = tokens self.todo = [] return # 2. Implement create_node and create_edge. They take the same arguments # as the corresponding Chart methods. Each should simply call the # corresponding Chart method, and append the resulting node or edge to # the todo list. (However, do not append None to the todo list.) def create_node(self, start, category, end, expansion): node = self.chart.create_node(start, (category), end, expansion) if node: self.todo.append(node) return def create_edge(self, edge_or_rule, child): edge = self.chart.create_edge(edge_or_rule, child) if edge: self.todo.append(edge) return # 3. Implement the shift method. Contrary to the handout, its argument # should be the index of the word to shift, i, and it should create nodes # spanning positions i to i + 1. Create one node for each part of speech # that the grammar assigns to words[i]. You may assume that the members # grammar, chart, and words are all appropriately set. def shift(self, i): foo = [] for rule in self.grammar.productions(): if(rule.rhs()[0] == self.words[i]): foo.append(rule.lhs()) j = i + 1 for bar in foo: self.create_node(i, bar, j, self.words[i]) return # 4. Implement the bu_predict method. It takes a Node as input. Let # X be the node's category. For each rule r whose righthand side begins # with X, call create_edge on r and the node def bu_predict(self, node): rules = self.grammar.productions(rhs=node.cat) for rule in rules: self.create_edge(rule, node) return # 5. Implement the extend_edges method. It takes a Node as input. It # iterates through the edge e taht end where the node begins, and if # the node's category is the same as the category after the dot in e, # then a new edge is created that combines e and the node. (Use create_edge.) def extend_edges(self, node): for e in self.chart.get_edges_at(node.i): if e.after_dot() == node.cat: self.create_edge(e, node) return # 6. Implement the complete method. It takes an Edge as input. For # safety, it should signal an error if the dot is not at the end. Create # a node corresponding to the lefthand side of the rule, with the edge as # its expansion, covering the same span as the edge. def complete(self, edge): if not edge.dot_at_end(): raise ValueError('Dot not at the end') else: self.create_node(edge.i, edge.rule.lhs(), edge.j, edge) # 7. The method next_task takes no input. It expects todo to be non-empty. # It removes the last item from todo and process it. If the item is a node, # it calls bu_predict and extend_edges on it, and if the item is an edge, # and the dot is at the end, it calls complete on it. def next_task(self): while len(self.todo) > 0: item = self.todo.pop() if type(item) == Node: self.bu_predict(item) self.extend_edges(item) elif type(item) == Edge and item.dot_at_end(): self.complete(item) return # 8. Implement the method fill_chart. It should call shift for each word, # and after each time it calls shift, it should call next_task repeatedly # until the todo list is empty def fill_chart(self): for num in range(len(self.words)): self.shift(num) while self.todo != []: self.next_task() # 9. Make the parser callable. When it is called as a function, it should take # a sentence ( that is, a list of tokens ) as input. It should call reset and # fill_chart. Then, if there is a node that spans the whole sentence and whose # category is the grammar's start symbol, it should return an iteration over the # tree of that node. Otherwise, it should return an empty iteration. Note: if # a method calls yield for some inputs but not others, the result will be an # empty iteration for the inputs where it never calls yield. def __call__(self, sentence): self.reset(sentence) self.fill_chart() new = self.chart.get_node(0, self.grammar.start(), 6).unwind() return new
class Ui_MainWindow(QtGui.QMainWindow): def __init__(self): super(Ui_MainWindow, self).__init__() self.imgPreProc = imagePreProcessor() def setupUi(self, MainWindow): MainWindow.showMaximized() MainWindow.setObjectName(_fromUtf8("MainWindow")) #MainWindow.resize(1014, 682) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setLayoutDirection(QtCore.Qt.LeftToRight) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.gridLayout_4 = QtGui.QGridLayout(self.centralwidget) self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4")) self.frame_2 = QtGui.QFrame(self.centralwidget) self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtGui.QFrame.Raised) self.frame_2.setObjectName(_fromUtf8("frame_2")) self.gridLayout_3 = QtGui.QGridLayout(self.frame_2) self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3")) self.scrollArea_2 = QtGui.QScrollArea(self.frame_2) self.scrollArea_2.setWidgetResizable(True) self.scrollArea_2.setObjectName(_fromUtf8("scrollArea_2")) self.scrollAreaWidgetContents_2 = QtGui.QWidget() self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 459, 448)) self.scrollAreaWidgetContents_2.setObjectName(_fromUtf8("scrollAreaWidgetContents_2")) self.gridLayout_6 = QtGui.QGridLayout(self.scrollAreaWidgetContents_2) self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6")) self.modifided_image = ImageWidget(self.scrollAreaWidgetContents_2) self.modifided_image.setObjectName(_fromUtf8("modifided_image")) self.gridLayout_6.addWidget(self.modifided_image, 0, 0, 1, 1) self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2) self.gridLayout_3.addWidget(self.scrollArea_2, 0, 0, 1, 1) self.gridLayout_4.addWidget(self.frame_2, 0, 1, 1, 1) self.frame = QtGui.QFrame(self.centralwidget) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName(_fromUtf8("frame")) self.gridLayout_2 = QtGui.QGridLayout(self.frame) self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) self.scrollArea = QtGui.QScrollArea(self.frame) self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName(_fromUtf8("scrollArea")) self.scrollAreaWidgetContents = QtGui.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 459, 448)) self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents")) self.gridLayout_5 = QtGui.QGridLayout(self.scrollAreaWidgetContents) self.gridLayout_5.setObjectName(_fromUtf8("gridLayout_5")) self.original_image = ImageWidget(self.scrollAreaWidgetContents) self.original_image.setObjectName(_fromUtf8("original_image")) self.gridLayout_5.addWidget(self.original_image, 0, 0, 1, 1) self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.gridLayout_2.addWidget(self.scrollArea, 0, 0, 1, 1) self.gridLayout_4.addWidget(self.frame, 0, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.dockWidget = QtGui.QDockWidget(MainWindow) self.dockWidget.setObjectName(_fromUtf8("dockWidget")) self.dockWidget.setFloating(True) self.dockWidget.setGeometry(QtCore.QRect(0, 0, 620, 220)) self.dockWidgetContents = QtGui.QWidget() self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents")) self.horizontalLayout = QtGui.QHBoxLayout(self.dockWidgetContents) self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.frame_4 = QtGui.QFrame(self.dockWidgetContents) self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_4.setFrameShadow(QtGui.QFrame.Raised) self.frame_4.setObjectName(_fromUtf8("frame_4")) self.gridLayout_8 = QtGui.QGridLayout(self.frame_4) self.gridLayout_8.setObjectName(_fromUtf8("gridLayout_8")) self.original_histogram = Chart(self.frame_4) self.original_histogram.setObjectName(_fromUtf8("original_histogram")) self.gridLayout_8.addWidget(self.original_histogram, 0, 0, 1, 1) self.horizontalLayout.addWidget(self.frame_4) self.frame_3 = QtGui.QFrame(self.dockWidgetContents) self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_3.setFrameShadow(QtGui.QFrame.Raised) self.frame_3.setObjectName(_fromUtf8("frame_3")) self.gridLayout_7 = QtGui.QGridLayout(self.frame_3) self.gridLayout_7.setObjectName(_fromUtf8("gridLayout_7")) self.modifided_histogram = Chart(self.frame_3) self.modifided_histogram.setObjectName(_fromUtf8("modifided_histogram")) self.gridLayout_7.addWidget(self.modifided_histogram, 0, 0, 1, 1) self.horizontalLayout.addWidget(self.frame_3) self.dockWidget.setWidget(self.dockWidgetContents) MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1014, 22)) self.menubar.setObjectName(_fromUtf8("menubar")) self.menuFile = QtGui.QMenu(self.menubar) self.menuFile.setObjectName(_fromUtf8("menuFile")) self.menuOperations = QtGui.QMenu(self.menubar) self.menuOperations.setObjectName(_fromUtf8("menuOperations")) self.menuOperations.setEnabled(False) self.menuHistogram_Modification = QtGui.QMenu(self.menuOperations) self.menuHistogram_Modification.setObjectName(_fromUtf8("menuHistogram_Modification")) self.menuSlide_Histogram = QtGui.QMenu(self.menuHistogram_Modification) self.menuSlide_Histogram.setObjectName(_fromUtf8("menuSlide_Histogram")) self.menuMapping_Equations = QtGui.QMenu(self.menuOperations) self.menuMapping_Equations.setObjectName(_fromUtf8("menuMapping_Equations")) self.menuLocal_Mask_Operations = QtGui.QMenu(self.menuOperations) self.menuLocal_Mask_Operations.setObjectName(_fromUtf8("menuLocal_Mask_Operations")) self.menuSegmentation = QtGui.QMenu(self.menuOperations) self.menuSegmentation.setObjectName(_fromUtf8("menuSegmentation")) MainWindow.setMenuBar(self.menubar) self.actionOpen = QtGui.QAction(MainWindow) self.actionOpen.setObjectName(_fromUtf8("action_Open")) self.actionOpen.setShortcut('Ctrl+O') self.actionOpen.triggered.connect(self.showOpen) self.actionExit = QtGui.QAction(MainWindow) self.actionExit.setObjectName(_fromUtf8("actionExit")) self.actionExit.setShortcut('Ctrl+Q') self.actionExit.setStatusTip('Exit application') self.actionExit.triggered.connect(QtGui.qApp.quit) self.menuFile.addAction(self.actionOpen) self.menuFile.addAction(self.actionExit) self.actionStretch_Histogram = QtGui.QAction(MainWindow) self.actionStretch_Histogram.setObjectName(_fromUtf8("actionStretch_Histogram")) self.actionStretch_Histogram.triggered.connect(self.stretchHisto) self.actionShrink_Histogram = QtGui.QAction(MainWindow) self.actionShrink_Histogram.setObjectName(_fromUtf8("actionShrink_Histogram")) self.actionShrink_Histogram.triggered.connect(self.shrinkHisto) self.actionLeft = QtGui.QAction(MainWindow) self.actionLeft.setObjectName(_fromUtf8("actionLeft")) self.actionLeft.triggered.connect(self.slidesLeftHisto) self.actionRight = QtGui.QAction(MainWindow) self.actionRight.setObjectName(_fromUtf8("actionRight")) self.actionRight.triggered.connect(self.slidesRightHisto) self.actionHistogram_Equalization = QtGui.QAction(MainWindow) self.actionHistogram_Equalization.setObjectName(_fromUtf8("actionHistogram_Equalization")) self.actionHistogram_Equalization.triggered.connect(self.histogramEqualization) self.actionNegative = QtGui.QAction(MainWindow) self.actionNegative.setObjectName(_fromUtf8("actionNegative")) self.actionNegative.triggered.connect(self.negative) self.actionMean_Local_Filtering = QtGui.QAction(MainWindow) self.actionMean_Local_Filtering.setObjectName(_fromUtf8("actionMean_Local_Filtering")) self.actionMean_Local_Filtering.triggered.connect(self.meanFilter) self.actionMedian_Local_Filtering = QtGui.QAction(MainWindow) self.actionMedian_Local_Filtering.setObjectName(_fromUtf8("actionMedian_Local_Filtering")) self.actionMedian_Local_Filtering.triggered.connect(self.medianFilter) self.actionSobel_Edge_Detector = QtGui.QAction(MainWindow) self.actionSobel_Edge_Detector.setObjectName(_fromUtf8("actionSobel_Edge_Detector")) self.actionSobel_Edge_Detector.triggered.connect(self.edgeDetection) self.menuSlide_Histogram.addAction(self.actionLeft) self.menuSlide_Histogram.addAction(self.actionRight) self.menuHistogram_Modification.addAction(self.actionStretch_Histogram) self.menuHistogram_Modification.addAction(self.actionShrink_Histogram) self.menuHistogram_Modification.addAction(self.menuSlide_Histogram.menuAction()) self.menuHistogram_Modification.addAction(self.actionHistogram_Equalization) self.menuMapping_Equations.addAction(self.actionNegative) self.menuLocal_Mask_Operations.addAction(self.actionMean_Local_Filtering) self.menuLocal_Mask_Operations.addAction(self.actionMedian_Local_Filtering) self.menuSegmentation.addAction(self.actionSobel_Edge_Detector) self.menuOperations.addAction(self.menuHistogram_Modification.menuAction()) self.menuOperations.addAction(self.menuMapping_Equations.menuAction()) self.menuOperations.addAction(self.menuLocal_Mask_Operations.menuAction()) self.menuOperations.addAction(self.menuSegmentation.menuAction()) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuOperations.menuAction()) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) self.menuFile.setTitle(_translate("MainWindow", "File", None)) self.menuOperations.setTitle(_translate("MainWindow", "Operations", None)) self.menuHistogram_Modification.setTitle(_translate("MainWindow", "Histogram Modification", None)) self.menuSlide_Histogram.setTitle(_translate("MainWindow", "Slide Histogram", None)) self.menuMapping_Equations.setTitle(_translate("MainWindow", "Mapping Equations", None)) self.menuLocal_Mask_Operations.setTitle(_translate("MainWindow", "Local (Mask) Operations", None)) self.menuSegmentation.setTitle(_translate("MainWindow", "Segmentation", None)) self.actionOpen.setText(_translate("MainWindow", "Open", None)) self.actionExit.setText(_translate("MainWindow", "Exit", None)) self.actionStretch_Histogram.setText(_translate("MainWindow", "Stretch Histogram", None)) self.actionShrink_Histogram.setText(_translate("MainWindow", "Shrink Histogram", None)) self.actionLeft.setText(_translate("MainWindow", "Left", None)) self.actionRight.setText(_translate("MainWindow", "Right", None)) self.actionHistogram_Equalization.setText(_translate("MainWindow", "Histogram Equalization", None)) self.actionNegative.setText(_translate("MainWindow", "Negative", None)) self.actionMean_Local_Filtering.setText(_translate("MainWindow", "Mean Local Filtering", None)) self.actionMedian_Local_Filtering.setText(_translate("MainWindow", "Median Local Filtering", None)) self.actionSobel_Edge_Detector.setText(_translate("MainWindow", "Sobel Edge Detector", None)) def showOpen(self): """Shows the file opening dialog.""" filepath = QtGui.QFileDialog.getOpenFileName( self, 'Open File', '', 'All Files (*.*);;jpeg (*.jpeg);;jpg (*.jpg);;png (*.png)') if filepath: self.open(filepath) def showIntDialog(self, title, text): amount, ok = QtGui.QInputDialog.getInt(self, title, text) if ok: return amount else: return 0 def open(self, filepath): self.imgPreProc.loadImage(str(filepath)) self.original_image.Qimg = ImageQt.ImageQt(self.imgPreProc.img.convert("RGB") if self.imgPreProc.img.mode == "L" else self.imgPreProc.img) self.original_image.repaint() self.original_histogram.setData(self.imgPreProc.histo) self.refreshAll() self.menuOperations.setEnabled(True) def repaintImage(self): self.modifided_image.Qimg = ImageQt.ImageQt(self.imgPreProc.img.convert("RGB") if self.imgPreProc.img.mode == "L" else self.imgPreProc.img) self.modifided_image.repaint() def rebuildHisto(self): self.modifided_histogram.setData(self.imgPreProc.histo) def refreshAll(self): self.rebuildHisto() self.repaintImage() def stretchHisto(self): self.imgPreProc.stretchHisto() self.refreshAll() def shrinkHisto(self): minR = self.showIntDialog('Histogram Min Shrink', 'Shrink Min:') if minR: maxR = self.showIntDialog('Histogram Max Shrink', 'Shrink Max:') if maxR: self.imgPreProc.shrinkHisto(minR, maxR) self.refreshAll() else : QtGui.QMessageBox.critical(self, "Error", "Provide the the Max") else : QtGui.QMessageBox.critical(self, "Error", "Provide the the Min") def slidesLeftHisto(self): amount = self.showIntDialog('Slide Histogram, Left', 'Sliding Amount:') if amount: self.imgPreProc.slidesLeftHisto(amount) self.refreshAll() else : QtGui.QMessageBox.critical(self, "Error", "Provide the amount of sliding") def slidesRightHisto(self): amount = self.showIntDialog('Slide Histogram, Right', 'Sliding Amount:') if amount: self.imgPreProc.slidesRightHisto(amount) self.refreshAll() else : QtGui.QMessageBox.critical(self, "Error", "You have to fill the By") def histogramEqualization(self): self.imgPreProc.histogramEqualization() self.refreshAll() def negative(self): self.imgPreProc.negative() self.refreshAll() def meanFilter(self): self.imgPreProc.meanFilter() self.refreshAll() def medianFilter(self): self.imgPreProc.medianFilter() self.refreshAll() def edgeDetection(self): self.imgPreProc.edgeDetection() self.refreshAll()