def readint(s, lengths): lengths = itertools.cycle(lengths) offset = 0 for length in itertools.cycle(lengths): next = offset + length yield int(hexlify(s[offset:next]), 16) if length else None offset = next
def main(): cap = cv2.VideoCapture(0) disto = cycle(funcs) time = 0 while(True): # Capture frame-by-frame ret, frame = cap.read() if time % 3 == 0: #chooses how long to change the distortion distfunc = disto.next() frm = distfunc(frame) # Our operations on the frame come here #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',frm) if cv2.waitKey(1) & 0xFF == ord('q'): break time += int(random.random() * 10) #add a random element to the distortion if time / 50 > 1: disto = cycle(funcs) ## reshuffle the effects so it won't repeat so much. time = 0 # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def _process_plot_kwargs(self, kwargs): import matplotlib.colors from itertools import cycle kw = {} frames = kwargs.pop('frames', None) if frames is None: frames = numpy.sort(self.profiles.keys()[::kwargs.pop('step', 1)]) else: frames = asiterable(frames) kw['frames'] = frames kw['yshift'] = kwargs.pop('yshift', 0.0) kw['rmax'] = kwargs.pop('rmax', None) kw['label'] = kwargs.pop('label', True) if kw['label'] == "_nolegend_": kw['label'] = False elif kw['label'] is None: kw['label'] = True color = kwargs.pop('color', None) if color is None: cmap = kwargs.pop('cmap', matplotlib.cm.jet) normalize = matplotlib.colors.normalize(vmin=numpy.min(frames), vmax=numpy.max(frames)) colors = cmap(normalize(frames)) else: colors = cycle(asiterable(color)) kw['colors'] = colors kw['linestyles'] = cycle(asiterable(kwargs.pop('linestyle', '-'))) return kw, kwargs
def __init__(self): self.pos=Point(1400,random.randint(20,700)) self.enemyImages = itertools.cycle([Image(Point(self.pos.getX()-int(x),self.pos.getY()),"images/enemy1/" + x + ".gif") for x in '123456789']) self.ball = next(self.enemyImages) self.eliminated=False self.changeImageCycle=itertools.cycle([True,False,False,False,False]) self.killtime=200
def __init__(self, pos, *groups): super(Player, self).__init__(*groups) self.pos = pos self.move_keys = { pg.K_LEFT: "left", pg.K_RIGHT: "right", pg.K_DOWN: "down", pg.K_UP: "up"} self.direct_to_velocity = { "left": (-1, 0), "right": (1, 0), "down": (0, 1), "up": (0, -1)} self.direction_stack = [] self.direction = "left" self.last_direction = self.direction img_size = (32, 36) self.image_dict = { "left": cycle(strip(GFX["ranger_f"], (0, 108), img_size, 3)), "right": cycle(strip(GFX["ranger_f"], (0,36), img_size, 3)), "down": cycle(strip(GFX["ranger_f"], (0, 72), img_size, 3)), "up": cycle(strip(GFX["ranger_f"], (0, 0), img_size, 3))} self.images = self.image_dict[self.direction] self.image = next(self.images) self.rect = self.image.get_rect(center=self.pos) self.frame_time = 60 self.frame_timer = 0 self.speed = .1 self.footprint = pg.Rect(0, 0, 30, 6) self.footprint.midbottom = self.rect.midbottom
def _computeLinesLengthAngle(self, posn, lengthscaling): """Return set of lines to plot for length-angle.""" s = self.settings d = self.document # translate coordinates from axes or relative values xpos, ypos = self._getPlotterCoords(posn) # get lengths and angles of lines length = s.get('length').getFloatArray(d) angle = s.get('angle').getFloatArray(d) if xpos is None or ypos is None or length is None or angle is None: return None length *= lengthscaling maxlen = max( len(xpos), len(ypos), len(length), len(angle) ) if maxlen > 1: if len(xpos) == 1: xpos = itertools.cycle(xpos) if len(ypos) == 1: ypos = itertools.cycle(ypos) if len(length) == 1: length = itertools.cycle(length) if len(angle) == 1: angle = itertools.cycle(angle) out = [] for v in czip(xpos, ypos, length, angle): # skip lines which have nans if N.all( N.isfinite(v) ): out.append(v) return out
def add_curves(self, local_df, curr_y_axis, subp, legend_prepend=""): df_coalesced = local_df.groupby(self.coalesced_vars) color_it = itertools.cycle(self.colors) marker_it = itertools.cycle(self.markers) for minimal_gr in sorted(df_coalesced.groups): minimal_idx = df_coalesced.groups[minimal_gr] minimal_df = self.df.ix[minimal_idx] if type(minimal_gr) != type((0,)): minimal_gr = [minimal_gr] minimal_legend = legend_prepend + ", ".join([ "$%s = %s$" % (self.get_transformed_var(k), v) for (k, v) in zip(self.coalesced_vars, minimal_gr) ]) # print local_legend, "------>",minimal_legend if type(curr_y_axis) == type(""): subp.scatter(minimal_df[[self.x_axis]], minimal_df[[curr_y_axis]], label=minimal_legend, marker=marker_it.next(), color=color_it.next(), edgecolors="black", s=65) elif type(curr_y_axis) == type((0,)) and len(curr_y_axis) == 2: ## BROKEN curr_y_axis, curr_yerr = curr_y_axis ####### B # subp.errorbar( x=minimal_df[[self.x_axis]], y=minimal_df[[curr_y_axis]] ,yerr = minimal_df[[curr_yerr]], # label = minimal_legend, fmt = marker_it.next(), color = color_it.next(), edgecolors = "black", s = 65 ) subp.errorbar(x=minimal_df[[self.x_axis]].apply(np.float32), y=minimal_df[[curr_y_axis]].apply(np.float32), yerr=minimal_df[[curr_yerr]].apply(np.float32))
def funcs(): exceptions = itertools.cycle([ SyntaxError('foo must come before bar'), ValueError('baz is not a valid choice'), TypeError('NoneType cannot be coerced to bar'), NotImplementedError('This feature is not implemented'), ZeroDivisionError('Your math doesn\'t work'), Exception('An unknown exception'), ]) loggers = itertools.cycle(['root', 'foo', 'foo.bar']) emails = itertools.cycle(['*****@*****.**', '*****@*****.**', '*****@*****.**']) timestamps = range(24 * 60 * 60) random.shuffle(timestamps) timestamps = itertools.cycle(timestamps) # def query(client): # duration = random.randint(0, 10000) / 1000.0 # return client.capture('Query', query=queries.next(), engine=engine.next(), time_spent=duration, data={'logger': loggers.next(), 'site': 'sql'}) def exception(client): timestamp = datetime.datetime.utcnow() - datetime.timedelta(seconds=timestamps.next()) try: raise exceptions.next() except Exception: email = emails.next() return client.capture('Exception', data={ 'logger': loggers.next(), 'site': 'web', 'sentry.interfaces.User': { 'id': email, 'email': email, } }, date=timestamp) return [exception]
def __init__(self, size=20, pred=None, ran=None): ''' Create size chars choosing an SP char at i where pred(ran, i)==True where ran is an instance of random.Random supplied in the constructor or created locally (if ran==None). ''' # Generators for the BMP and SP characters base = itertools.cycle(UnicodeMaterial.base) supp = itertools.cycle(UnicodeMaterial.supp) # Each instance gets a random generator if ran is None: ran = random.Random() self.random = ran if pred is None: pred = lambda ran, j : ran.random() < DEFAULT_RATE # Generate the list r = list() for i in range(size): if pred(self.random, i): c = supp.next() else: c = base.next() r.append(c) # The list and its concatenation are our material self.ref = r self.size = len(r) self.text = u''.join(r) self.target = u''
def mainloop(ai, initial_board): b = initial_board choice = raw_input(INTRO_PROMPT) # who goes first, cpu or human? if choice.lower() == 'me': move_fns = cycle([get_human_move, ai.get_move]) else: move_fns = cycle([ai.get_move, get_human_move]) while board.calc_winner_or_tie(b) == None: move_fn = move_fns.next() b = move_fn(b) print '' print b print '' win_or_tie = board.calc_winner_or_tie(b) if win_or_tie == 'x': winner_msg = 'Winner is Xs!' elif win_or_tie == 'o': winner_msg = 'Winner is Os!' else: winner_msg = 'Tie Game!' print "Game over.", winner_msg print b print "Great Game!"
def map_within(self, other): """'other' should be a subset of 'self'. Returns a RangeSet representing what 'other' would get translated to if the integers of 'self' were translated down to be contiguous starting at zero. >>> RangeSet("0-9").map_within(RangeSet("3-4")) <RangeSet("3-4")> >>> RangeSet("10-19").map_within(RangeSet("13-14")) <RangeSet("3-4")> >>> RangeSet("10-19 30-39").map_within(RangeSet("17-19 30-32")) <RangeSet("7-12")> >>> RangeSet("10-19 30-39").map_within(RangeSet("12-13 17-19 30-32")) <RangeSet("2-3 7-12")> """ out = [] offset = 0 start = None for p, d in heapq.merge(zip(self.data, itertools.cycle((-5, +5))), zip(other.data, itertools.cycle((-1, +1)))): if d == -5: start = p elif d == +5: offset += p-start start = None else: out.append(offset + p - start) return RangeSet(data=out)
def __init__(self, data, partition='train', batch_size=32, shape=None, concat=True, name=''): """Initialize data Parameters ---------- data: DataLoader object loaded data object containing original data frames for molecular, drug and response data partition: 'train', 'val', or 'test' partition of data to generate for batch_size: integer (default 32) batch size of generated data shape: None, '1d' or 'add_1d' (default None) keep original feature shapes, make them flat or add one extra dimension (for convolution or locally connected layers in some frameworks) concat: True or False (default True) concatenate all features if set to True """ self.lock = threading.Lock() self.data = data self.partition = partition self.batch_size = batch_size self.shape = shape self.concat = concat self.name = name if partition == 'train': self.cycle = cycle(range(data.n_train)) self.num_data = data.n_train elif partition == 'val': self.cycle = cycle(range(data.total)[-data.n_val:]) self.num_data = data.n_val elif partition == 'test': self.cycle = cycle(range(data.total, data.total + data.n_test)) self.num_data = data.n_test else: raise Exception('Data partition "{}" not recognized.'.format(partition))
def test_execute_concurrent(self): for num_statements in (0, 1, 2, 7, 10, 99, 100, 101, 199, 200, 201): # write statement = SimpleStatement( "INSERT INTO test3rf.test (k, v) VALUES (%s, %s)", consistency_level=ConsistencyLevel.QUORUM) statements = cycle((statement, )) parameters = [(i, i) for i in range(num_statements)] results = self.execute_concurrent_helper(self.session, list(zip(statements, parameters))) self.assertEqual(num_statements, len(results)) for success, result in results: self.assertTrue(success) self.assertFalse(result) # read statement = SimpleStatement( "SELECT v FROM test3rf.test WHERE k=%s", consistency_level=ConsistencyLevel.QUORUM) statements = cycle((statement, )) parameters = [(i, ) for i in range(num_statements)] results = self.execute_concurrent_helper(self.session, list(zip(statements, parameters))) self.assertEqual(num_statements, len(results)) self.assertEqual([(True, [(i,)]) for i in range(num_statements)], results)
def decrypt(in_path, key, log = True): tic = time() l = len(key) print '\n- decrypt mode' backupname = os.path.splitext(in_path)[0] + '.hex' + os.path.splitext(in_path)[1] copyfile(in_path, backupname) outfile = open(in_path, 'w') infile = open(backupname, 'r') if all(c in digits + 'abcdefx' for c in infile.read()): print "\nHEXADECIMAL" for key_c in cycle(key): c = infile.read(4) if not c: break print key_c, c outfile.write(chr(int(c, 16) ^ ord(key_c))) else: print "\nDECIMAL" for key_c in cycle(key): c = infile.read(3) if not c: break outfile.write(chr(int(c, 10) ^ ord(key_c))) outfile.close() print "\nDecripted with key '{0}'\n".format(key) print "{0} s".format(round(time()-tic, 4))
def subgen(): nonlocal rows d_table = style2dict(style) if width: d_table["width"] = width tablestyle = dict2style(d_table) yield otag("table", tablestyle) td_aligns = itertools.cycle([align]) tr_styles_ = itertools.cycle(tr_styles) if header is not None: rows = itertools.chain([header], rows) td_aligns = itertools.chain([header_align], td_aligns) tr_styles_ = itertools.chain([header_style], tr_styles_) for r, row in enumerate(rows): d_tr = style2dict(next(tr_styles_)) if hasattr(row, "style"): d_tr.update(style2dict(row.style)) trstyle = dict2style(d_tr) yield indent + otag("tr", trstyle) align_ = next(td_aligns) for c, record in enumerate(row): f = {"C": center, "L": left, "R": right}[align_[c]] d_td = {} if r == 0 and widths is not None: if widths[c] is not None: d_td["width"] = widths[c] if hasattr(record, "style"): d_td.update(style2dict(record.style)) tdstyle = dict2style(d_td) yield ( 2 * indent + f'{otag("td", tdstyle)}{f(record)}[/td]' ) yield indent + f'[/tr]' yield f'[/table]'
def test_update_translation(self): """Test filters for updating a translation. We do not test here for minimum percentages. """ with patch.object(self.p, "get_resource_option") as mock: mock.return_value = None should_update = self.p._should_update_translation force = True for lang in self.langs: self.assertTrue(should_update(lang, self.stats, 'foo', force)) force = False # reminder local_file = 'foo' # unknown language self.assertFalse(should_update('es', self.stats, local_file)) # no local file with patch.object(self.p, "_get_time_of_local_file") as time_mock: time_mock.return_value = None with patch.object(self.p, "get_full_path") as path_mock: path_mock.return_value = "foo" for lang in self.langs: self.assertTrue( should_update(lang, self.stats, local_file) ) # older local files local_times = [self.p._generate_timestamp('2011-11-01 14:00:59')] results = itertools.cycle(local_times) def side_effect(*args): return next(results) with patch.object(self.p, "_get_time_of_local_file") as time_mock: time_mock.side_effect = side_effect with patch.object(self.p, "get_full_path") as path_mock: path_mock.return_value = "foo" for lang in self.langs: self.assertTrue( should_update(lang, self.stats, local_file) ) # newer local files local_times = [self.p._generate_timestamp('2011-11-01 15:01:59')] results = itertools.cycle(local_times) def side_effect(*args): return next(results) with patch.object(self.p, "_get_time_of_local_file") as time_mock: time_mock.side_effect = side_effect with patch.object(self.p, "get_full_path") as path_mock: path_mock.return_value = "foo" for lang in self.langs: self.assertFalse( should_update(lang, self.stats, local_file) )
def encrypt(in_path, key = 'loremipsum', enc_hex = True, log = True): tic = time() l = len(key) print '\n- encrypt mode' backupname = os.path.splitext(in_path)[0] + '.backup' + os.path.splitext(in_path)[1] copyfile(in_path, backupname) outfile = open(in_path, 'w') infile = open(backupname, 'r') if enc_hex: for key_c in cycle(key): c = infile.read(1) if not c: break outfile.write('0x' + str((hex(ord(c) ^ ord(key_c))[2:]).zfill(2))) else: for key_c in cycle(key): c = infile.read(1) if not c: break outfile.write((ord(c) ^ ord(key_c)).zfill(3)) outfile.close() print "\nEncrypted with key: '{0}'\n".format(key) print "{0} s".format(round(time()-tic, 4))
def _computeLinesPointToPoint(self, posn): """Return set of lines for point to point.""" s = self.settings # translate coordinates from axes or relative values xpos, ypos = self._getPlotterCoords(posn) xpos2, ypos2 = self._getPlotterCoords(posn, xsetting='xPos2', ysetting='yPos2') if None in (xpos, ypos, xpos2, ypos2): return None maxlen = max( len(xpos), len(ypos), len(xpos2), len(ypos2) ) if maxlen > 1: if len(xpos) == 1: xpos = itertools.cycle(xpos) if len(ypos) == 1: ypos = itertools.cycle(ypos) if len(xpos2) == 1: xpos2 = itertools.cycle(xpos2) if len(ypos2) == 1: ypos2 = itertools.cycle(ypos2) out = [] for v in itertools.izip(xpos, ypos, xpos2, ypos2): # skip nans again if N.all( N.isfinite(v) ): length = math.sqrt( (v[0]-v[2])**2 + (v[1]-v[3])**2 ) angle = math.atan2( v[3]-v[1], v[2]-v[0] ) / math.pi * 180. out.append( (v[0], v[1], length, angle) ) return out
def main(opts): """The main loop of the module, do the renaming in parallel etc.""" log = logging.getLogger("exif2timestream") setup_logs(opts) # beginneth the actual main loop start_time = time() cameras = parse_camera_config_csv(opts["-c"]) n_images = 0 for camera in cameras: msg = "Processing experiment {}, location {}\n".format( camera[FIELDS["expt"]], camera[FIELDS["location"]], ) msg += "Images are coming from {}, being put in {}".format( camera[FIELDS["source"]], camera[FIELDS["destination"]], ) print(msg) log.info(msg) for ext, images in find_image_files(camera).iteritems(): images = sorted(images) n_cam_images = len(images) print("{0} {1} images from this camera".format(n_cam_images, ext)) log.info("Have {0} {1} images from this camera".format( n_cam_images, ext)) n_images += n_cam_images last_date = None subsec = 0 count = 0 # TODO: sort out the whole subsecond clusterfuck if "-1" in opts and opts["-1"]: log.info("Using 1 process (What is this? F*****g 1990?)") for image in images: count += 1 print("Processed {: 5d} Images".format(count), end='\r') process_image((image, camera, ext)) else: from multiprocessing import Pool, cpu_count if "-t" in opts and opts["-t"] is not None: try: threads = int(opts["-t"]) except ValueError: threads = cpu_count() - 1 else: threads = cpu_count() - 1 # Ensure that we're using at least one thread threads = max(threads, 1) log.info("Using {0:d} processes".format(threads)) # set the function's camera-wide arguments args = zip(images, cycle([camera]), cycle([ext])) pool = Pool(threads) for _ in pool.imap(process_image, args): count += 1 print("Processed {: 5d} Images".format(count), end='\r') pool.close() pool.join() print("Processed {: 5d} Images. Finished this cam!".format(count)) secs_taken = time() - start_time print("\nProcessed a total of {0} images in {1:.2f} seconds".format( n_images, secs_taken))
def __init__(self, port="auto"): self.leftWizWheelActionCycle=itertools.cycle(self.wizWheelActions) action=self.leftWizWheelActionCycle.next() self.gestureMap.add("br(freedomScientific):leftWizWheelUp",*action[1]) self.gestureMap.add("br(freedomScientific):leftWizWheelDown",*action[2]) self.rightWizWheelActionCycle=itertools.cycle(self.wizWheelActions) action=self.rightWizWheelActionCycle.next() self.gestureMap.add("br(freedomScientific):rightWizWheelUp",*action[1]) self.gestureMap.add("br(freedomScientific):rightWizWheelDown",*action[2]) super(BrailleDisplayDriver,self).__init__() self._messageWindowClassAtom=windll.user32.RegisterClassExW(byref(nvdaFsBrlWndCls)) self._messageWindow=windll.user32.CreateWindowExW(0,self._messageWindowClassAtom,u"nvdaFsBrlWndCls window",0,0,0,0,0,None,None,appInstance,None) if port == "auto": portsToTry = itertools.chain(["USB"], self._getBluetoothPorts()) elif port == "bluetooth": portsToTry = self._getBluetoothPorts() else: # USB portsToTry = ["USB"] fbHandle=-1 for port in portsToTry: fbHandle=fbOpen(port,self._messageWindow,nvdaFsBrlWm) if fbHandle!=-1: break if fbHandle==-1: windll.user32.DestroyWindow(self._messageWindow) windll.user32.UnregisterClassW(self._messageWindowClassAtom,appInstance) raise RuntimeError("No display found") self.fbHandle=fbHandle self._configureDisplay() numCells=self.numCells self.gestureMap.add("br(freedomScientific):topRouting1","globalCommands","GlobalCommands","braille_scrollBack") self.gestureMap.add("br(freedomScientific):topRouting%d"%numCells,"globalCommands","GlobalCommands","braille_scrollForward")
def _generate_cover_to_coveree_mapping(self): # We want to assign a coveree(enemy) to every cover(ally) # We also want to know for each cover(ally), the other covers that share the same target(enemy) closest_enemies_to_ball = closest_players_to_point(self.game_state.ball_position, our_team=False) if len(closest_enemies_to_ball) > 0: closest_enemy_to_ball = closest_enemies_to_ball[0].player closest_enemies_to_our_goal = closest_players_to_point(self.game_state.field.our_goal, our_team=False) enemy_not_with_ball = [enemy.player for enemy in closest_enemies_to_our_goal if enemy.player is not closest_enemy_to_ball] else: enemy_not_with_ball = [] # If we don't have enough player we cover the ball if len(enemy_not_with_ball) == 0: cover_to_coveree = dict(zip(self.robots_in_cover_formation, cycle([self.game_state.ball]))) cover_to_formation = {cover: self.robots_in_cover_formation for cover in self.robots_in_cover_formation} # If we don't have enough enemy to cover, we group the player in formation elif len(self.robots_in_cover_formation) > len(enemy_not_with_ball): cover_to_coveree = dict(zip(self.robots_in_cover_formation, cycle(enemy_not_with_ball))) cover_to_formation = {} for cover, coveree in cover_to_coveree.items(): formation = [teamate for teamate, teamate_coveree in cover_to_coveree.items() if teamate_coveree == coveree] cover_to_formation[cover] = formation else: cover_to_coveree = dict(zip(self.robots_in_cover_formation, enemy_not_with_ball)) cover_to_formation = {cover: [cover] for cover in self.robots_in_cover_formation} return cover_to_coveree, cover_to_formation
def josephus(prisoner, kill, surviver): p = range(prisoner) k = [0] * kill k[kill-1] = 1 s = [1] * kill s[kill -1] = 0 queue = p queue = compress(queue, cycle(s)) try: while True: p.append(queue.next()) except StopIteration: pass kil=[] killed = compress(p, cycle(k)) try: while True: kil.append(killed.next()) except StopIteration: pass print 'The surviver is: ', kil[-surviver:] print 'The kill sequence is ', kil[:prisoner-surviver]
def benchcompress(arraycode): """Benchmark the compress function. """ # These are set to target a balanced execution time for the different tests. pyitercounts = calibrationdata['compress'][0] afitercounts = calibrationdata['compress'][1] cycledata = list(range(128)) compdata = array.array(arraycode, [1,0,1,0]) data = array.array(arraycode, itertools.repeat(0, ARRAYSIZE)) for x, y in zip(itertools.cycle(cycledata), itertools.repeat(0, ARRAYSIZE)): data[x] = x dataout = array.array(arraycode, itertools.repeat(0, ARRAYSIZE)) pycomp = array.array(arraycode, (x for x,y in zip(itertools.cycle(compdata), itertools.repeat(0, ARRAYSIZE)))) # Native Python time. starttime = time.perf_counter() for i in range(pyitercounts): dataout = array.array(arraycode, itertools.compress(data, pycomp)) endtime = time.perf_counter() pythontime = (endtime - starttime) / pyitercounts dataout = array.array(arraycode, itertools.repeat(0, ARRAYSIZE)) # Arrayfunc time. starttime = time.perf_counter() for i in range(afitercounts): x = arrayfunc.compress(data, dataout, compdata) endtime = time.perf_counter() functime = (endtime - starttime) / afitercounts return (pythontime, functime, pythontime / functime)
def __init__(self, name='', args=[], suffix=';', row=0, col=0, perl_type=''): super(CallExpr, self).__init__(row=row, col=col, perl_type=perl_type) self.name = name self.args = args self.suffix = suffix self.block = cycle(('(',')')) self.sep = cycle((','))
def dyn_graph_general(elist, order, vstyles=[],estyles=[]): """ Visualização dinâmica usando ubigraph Servidor Ubigraph deve estar rodando na URL indicada elist is a list of tuples: (n1,n2,w) """ U = ubigraph.Ubigraph(URL=ubiServer) U.clear() nodes = {} edges = set([]) maxw = float(max(np.array([i[2] for i in elist]))) #largest weight if not vstyles: vstyles = cycle([U.newVertexStyle(id=1,shape="sphere", color="#ff0000")]) else: vstyles = cycle(vstyles) lei_style = U.newVertexStyle(id=2,shape="sphere", color="#00ff00") for e in elist: if e[0] not in nodes: n1 = U.newVertex(style=vstyles.next(), label=str(e[0]).decode('latin-1')) nodes[e[0]] = n1 else: n1 = nodes[e[0]] if e[1] not in nodes: n2 = U.newVertex(style=lei_style, label=str(e[1])) nodes[e[1]] = n2 else: n2 = nodes[e[1]] es = e[2]/maxw if (n1,n2) not in edges: U.newEdge(n1,n2,spline=True,strength=es, width=2.0, showstrain=True) edges.add((n1,n2)) edges.add((n2,n1))
def get(request, category_id): category = Category.objects.get(id=category_id) subcategories = Category.objects.filter(parent_category_id=category_id).order_by('position') if subcategories: subcategories = CategoryView.get_categories_data_from_db(subcategories) categories_positions, categories_ids, categories_names, categories_photos = subcategories rows = PackInRows.pack_in_rows_by_position(chain([3], cycle([4, 5])), categories_positions, ['name', categories_names], ['id', categories_ids], ['photo', categories_photos]) context = {'rows': rows} template = 'catalogue_categories.html' else: items = CategoryView.get_items_data_from_db(category_id) items_names, items_ids, items_article, items_cost, items_info, items_first_photos = items rows = PackInRows.pack_in_rows_by_order(chain([3], cycle([4, 5])), ['name', items_names], ['id', items_ids], ['article', items_article], ['cost', items_cost], ['info', items_info], ['photo', items_first_photos]) context = {'rows': rows} template = 'catalogue_items.html' CategoryView.add_navigation(context, category.id, category.name) add_categories_to_context(context) return render(request, template, context)
def addTemplate(cursor, template, names, extensions): '''Adds a template to the cursor's database.''' # Insert template if template is not None: cursor.execute('INSERT INTO templates(template) VALUES(?)', [template]) template_id = cursor.lastrowid # Insert names if names: try: cursor.executemany( 'INSERT INTO names(name, template_id) VALUES(?, ?)', zip(names, cycle([template_id]))) except sqlite3.IntegrityError: pass # Insert extensions if extensions: try: cursor.executemany( 'INSERT INTO extensions(extension, template_id) VALUES(?, ?)', zip(extensions, cycle([template_id]))) except sqlite3.IntegrityError: pass
def messages(count, size): """Generator for count messages of the provided size""" import string # Make sure we have at least 'size' letters letters = islice(cycle(chain(string.lowercase, string.uppercase)), size) return islice(cycle("".join(l) for l in permutations(letters, size)), count)
def __init__(self, x, y, image, screen): pygame.sprite.Sprite.__init__(self) Lever.allLevers.add(self) self.image = pygame.image.load(image) self.animatedImages = itertools.cycle( (pygame.image.load(re.sub(r'\d', '1', image)), pygame.image.load(re.sub(r'\d', '2', image)), ) ) self.reverseAnimatedImages = itertools.cycle( (pygame.image.load(re.sub(r'\d', '1', image)), pygame.image.load(image), ) ) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.screen = screen tile = self.get_tile() tile.walkable = False tile.type = 'solid' self.off = True
def color_cycle(self, name='map', cmap=None, length=None): '''Returns the current colour cycle, creates a new one if necessary. Parameters ---------- name : str selector for colour cycle: 'cmap': use a colourmap to generate cycle, see http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html 'DTU': uses the colours from the DTU design guide 'DTU_dark': uses the colours from the DTU design guide, darker colours first, good for presentations 'cblind': A scheme for colourblind people 'matplotlib': The standard matplotlib scheme 'simple': A simple four-colour scheme that work for greyscale and colourblind people 'brewer1' and 'brewer2': See http://colorbrewer2.org, works for colourblind and greyscale ''' if name=='map': if length is None: length = self.default_lst if cmap is None: cmap = self.default_map cmap = self.get_color_map(cmap) clist = [cmap(i) for i in np.linspace(0.25, 0.75, length)] else: clist = self._color_lists[name] if length is not None: if length<1: return cycle(['none']) elif length<=len(clist): return cycle(list(clist)[0:length]) elif length>len(clist): self.autolog("Colour cycle is too short, cannot extend it.") return cycle(clist)
def run(tb, vb, lr, epochs, writer): device = os.environ['main-device'] logging.info('Training program start!') logging.info('Configuration:') logging.info('\n' + json.dumps(INFO, indent=2)) # ------------------------------------ # 1. Define dataloader train_loader, train4val_loader, val_loader, num_of_images, mapping, support_train_loader, support_val_loader = get_dataloaders( tb, vb) weights = (1 / num_of_images) / ((1 / num_of_images).sum().item()) weights = weights.to(device=device) # Build iterable mix up batch loader it = iter(train_loader) sup_it = iter(support_train_loader) mixup_batches = zip(it, cycle(sup_it)) # ------------------------------------ # 2. Define model model = EfficientNet.from_pretrained( 'efficientnet-b3', num_classes=INFO['dataset-info']['num-of-classes']) model = carrier(model) support_model = EfficientNet.from_pretrained( 'efficientnet-b3', num_classes=INFO['supportset-info']['num-of-classes']) support_model = carrier(support_model) # ------------------------------------ # 3. Define optimizer optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9) scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200) ignite_scheduler = LRScheduler(scheduler) support_optimizer = optim.SGD(support_model.parameters(), lr=lr, momentum=0.9) scheduler = optim.lr_scheduler.CosineAnnealingLR(support_optimizer, T_max=200) support_ignite_scheduler = LRScheduler(scheduler) # ------------------------------------ # 4. Define metrics train_metrics = { 'accuracy': Accuracy(), 'precision_recall': MetricsLambda(PrecisionRecallTable, Precision(), Recall(), train_loader.dataset.classes), 'cmatrix': MetricsLambda(CMatrixTable, ConfusionMatrix(INFO['dataset-info']['num-of-classes']), train_loader.dataset.classes) } support_metrics = { 'accuracy': Accuracy(), 'precision_recall': MetricsLambda(PrecisionRecallTable, Precision(), Recall(), support_val_loader.dataset.classes) } class DeepTransPrediction(metric.Metric): def __init__(self, threshold=torch.tensor([0.5]).repeat( len(train_loader.dataset.classes))): super(DeepTransPrediction, self).__init__() threshold = threshold.to(device=device) self.threshold = threshold self.prediction = torch.tensor([], dtype=torch.int) self.y = torch.tensor([], dtype=torch.int) def reset(self): self.threshold = torch.tensor([0.5]).repeat( len(train_loader.dataset.classes)).to(device=device) self.prediction = torch.tensor([]) self.y = torch.tensor([]) super(DeepTransPrediction, self).reset() def update(self, output): y_pred, y = output softmax = torch.exp(y_pred) / torch.exp(y_pred).sum(1)[:, None] values, inds = softmax.max(1) prediction = torch.where(values > self.threshold[inds], inds, torch.tensor([-1]).to(device=device)) self.prediction = torch.cat( (self.prediction.type(torch.LongTensor).to(device=device), torch.tensor([mapping[x.item()] for x in prediction]).to(device=device))) self.y = torch.cat( (self.y.type(torch.LongTensor).to(device=device), y.to(device=device))) # return self.prediction, self.y def compute(self): return self.prediction, self.y val_metrics = { 'accuracy': MetricsLambda(Labels2Acc, DeepTransPrediction()), 'precision_recall': MetricsLambda(Labels2PrecisionRecall, DeepTransPrediction(), val_loader.dataset.classes), 'cmatrix': MetricsLambda(Labels2CMatrix, DeepTransPrediction(), val_loader.dataset.classes) } # ------------------------------------ # 5. Create trainer # trainer = create_supervised_trainer(model, optimizer, nn.CrossEntropyLoss(weight=weights), device=device) def membership_loss(input, target, weights): _lambda = 5 classes = input.shape[1] sigmoid = 1 / (1 + torch.exp(-input)) part1 = 1 - sigmoid[range(sigmoid.shape[0]), target] part1 = (part1 * part1 * weights[target]).sum() sigmoid[range(sigmoid.shape[0])] = 0 part2 = (sigmoid * sigmoid * weights).sum() return part1 + _lambda * float(1 / (classes - 1)) * part2 def step(engine, batch): model.train() support_model.train() _alpha1 = 1 _alpha2 = 1 known, support = batch x_known, y_known = known x_support, y_support = support x_known = x_known.to(device=device) y_known = y_known.to(device=device) x_support = x_support.to(device=device) y_support = y_support.to(device=device) support_scores = support_model(x_support) support_cross_entropy = nn.functional.cross_entropy( support_scores, y_support) known_scores = model(x_known) known_cross_entropy = nn.functional.cross_entropy( known_scores, y_known, weights) known_membership = membership_loss(known_scores, y_known, weights) loss = support_cross_entropy + known_cross_entropy * _alpha1 + known_membership * _alpha2 model.zero_grad() support_model.zero_grad() loss.backward() optimizer.step() support_optimizer.step() return { 'Rce_loss': support_cross_entropy.item(), 'Tce_loss': known_cross_entropy.item(), 'Tm_loss': known_membership.item(), 'total_loss': loss.item() } trainer = Engine(step) # ------------------------------------ # 6. Create evaluator train_evaluator = create_supervised_evaluator(model, metrics=train_metrics, device=device) val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) support_evaluator = create_supervised_evaluator(support_model, metrics=support_metrics, device=device) desc = 'ITERATION - loss: {:.2f}|{:.2f}|{:.2f}|{:.2f}' pbar = tqdm(initial=0, leave=False, total=len(train_loader), desc=desc.format(0, 0, 0, 0)) # ------------------------------------ # 7. Create event hooks @trainer.on(Events.ITERATION_COMPLETED) def log_training_loss(engine): log_interval = 1 iter = (engine.state.iteration - 1) % len(train_loader) + 1 if iter % log_interval == 0: o = engine.state.output pbar.desc = desc.format(o['Rce_loss'], o['Tce_loss'], o['Tm_loss'], o['total_loss']) pbar.update(log_interval) @trainer.on(Events.EPOCH_STARTED) def rebuild_dataloader(engine): pbar.clear() print('Rebuild dataloader!') it = iter(train_loader) sup_it = iter(support_train_loader) engine.state.dataloader = zip(it, cycle(sup_it)) @trainer.on(Events.EPOCH_COMPLETED) def log_training_results(engine): print('Checking on training set.') train_evaluator.run(train4val_loader) metrics = train_evaluator.state.metrics avg_accuracy = metrics['accuracy'] precision_recall = metrics['precision_recall'] cmatrix = metrics['cmatrix'] prompt = """ Id: {} Training Results - Epoch: {} Avg accuracy: {:.4f} precision_recall: \n{} confusion matrix: \n{} """.format(os.environ['run-id'], engine.state.epoch, avg_accuracy, precision_recall['pretty'], cmatrix['pretty']) tqdm.write(prompt) logging.info('\n' + prompt) writer.add_text(os.environ['run-id'], prompt, engine.state.epoch) writer.add_scalars('Aggregate/Acc', {'Train Acc': avg_accuracy}, engine.state.epoch) @trainer.on(Events.EPOCH_COMPLETED) def log_support_results(engine): pbar.clear() print('* - * - * - * - * - * - * - * - * - * - * - *') print('Checking on support set.') support_evaluator.run(support_val_loader) metrics = support_evaluator.state.metrics avg_accuracy = metrics['accuracy'] precision_recall = metrics['precision_recall'] prompt = """ Id: {} Support set Results - Epoch: {} Avg accuracy: {:.4f} precision_recall: \n{} """.format(os.environ['run-id'], engine.state.epoch, avg_accuracy, precision_recall['pretty']) tqdm.write(prompt) logging.info('\n' + prompt) writer.add_text(os.environ['run-id'], prompt, engine.state.epoch) writer.add_scalars('Support/Acc', {'Train Acc': avg_accuracy}, engine.state.epoch) writer.add_scalars( 'Support/Score', { 'Avg precision': precision_recall['data'][0, -1], 'Avg recall': precision_recall['data'][1, -1] }, engine.state.epoch) @trainer.on(Events.EPOCH_COMPLETED) def log_validation_results(engine): pbar.clear() print('* - * - * - * - * - * - * - * - * - * - * - *') print('Checking on validation set.') val_evaluator.run(val_loader) metrics = val_evaluator.state.metrics avg_accuracy = metrics['accuracy'] precision_recall = metrics['precision_recall'] cmatrix = metrics['cmatrix'] unknown = precision_recall['pretty']['UNKNOWN'] print(unknown) prompt = """ Id: {} Validating Results - Epoch: {} Avg accuracy: {:.4f} Unknown precision: {:.4f} Unknown recall: {:.4f} precision_recall: \n{} confusion matrix: \n{} """.format(os.environ['run-id'], engine.state.epoch, avg_accuracy, unknown['Precision'], unknown['Recall'], precision_recall['pretty'], cmatrix['pretty']) tqdm.write(prompt) logging.info('\n' + prompt) writer.add_text(os.environ['run-id'], prompt, engine.state.epoch) writer.add_scalars('Aggregate/Acc', {'Val Acc': avg_accuracy}, engine.state.epoch) writer.add_scalars( 'Aggregate/Score', { 'Val avg Precision': precision_recall['data'][0, -1], 'Val avg Recall': precision_recall['data'][1, -1] }, engine.state.epoch) writer.add_scalars( 'Unknown/Score', { 'Unknown Precision': unknown['Precision'], 'Unknown Recall': unknown['Recall'] }, engine.state.epoch) pbar.n = pbar.last_print_n = 0 trainer.add_event_handler(Events.EPOCH_STARTED, ignite_scheduler) trainer.add_event_handler(Events.EPOCH_STARTED, support_ignite_scheduler) # ------------------------------------ # Run trainer.run(mixup_batches, max_epochs=epochs) pbar.close()
result = '_' return result another_user = '******' HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-us,en;q=0.5', } session = requests.Session() CURRENT_DIR = os.getcwd() _spin = itertools.cycle(['▲', '►', '▼', '◄']) def get_name_show_cmd(ele, title): if ele: return '(%s) %s' % (ele, title) return '(%s)' % (title) def spinner(text): spin = _spin.__next__() sys.stdout.write(text + spin) sys.stdout.flush() time.sleep(0.01) KNOWN_EXTENSIONS = (
def send(self): """Send chunk_size items from self.data to channels.""" for item, q in itertools.islice(zip(self.data, cycle(self.queues)), self.chunk_size): # cycle channels so that distribute the texts evenly q.put(item)
def __init__(self, data, queues, chunk_size): self.data = iter(data) self.queues = iter(cycle(queues)) self.chunk_size = chunk_size self.count = 0
import discord from discord.ext import commands, tasks from discord.utils import get from itertools import cycle from random import randint TOKEN = 'NzE3NzIwMzkzMDk1NDQ2NTQ5.Xt9RQw.KUSV4y0e0kpc-tD4jDUPeYCMw5A' client = commands.Bot(command_prefix='{}') status = cycle(['Prefix: {}', '{}help', 'Bot by Cichobieq']) @tasks.loop(seconds=5) async def change_status(): await client.change_presence(activity=discord.Game(next(status))) @client.event async def on_ready(): print("bot gotowy") change_status.start() @client.command(aliases=['celar']) @commands.has_permissions(administrator=True) async def clear(ctx, amount): if not amount.isdecimal(): if amount.lower() == 'all': await ctx.channel.purge(limit=9999999999999999) elif amount.lower() != 'all': await ctx.send( 'Podaj liczbę wiadomości lub "all" aby wyczyścić wszystko')
def __init__(self, sequence): if any(o < 0 for o in sequence): raise ValueError( 'Sequential distribution must sample positive numbers only.') self.sequence = sequence self.generator = cycle(self.sequence)
def rebuild_dataloader(engine): pbar.clear() print('Rebuild dataloader!') it = iter(train_loader) sup_it = iter(support_train_loader) engine.state.dataloader = zip(it, cycle(sup_it))
] #Encrypted Buffer #Random Key #File Path si = STARTUPINFO() si.cb = ctypes.sizeof(STARTUPINFO) pi = PROCESS_INFORMATION() cx = CONTEXT() cx.ContextFlags = 0x10007 key = cycle(randomkey) decryptedbuff = ''.join( chr(ord(x) ^ ord(y)) for (x, y) in izip(encryptedbuff, key)) # Get payload buffer as PE file pe = pefile.PE(data=decryptedbuff) fd_size = len(decryptedbuff) print "\n[+] Payload size : " + str(fd_size) calloc = ctypes.cdll.msvcrt.calloc p = calloc((fd_size + 1), ctypes.sizeof(ctypes.c_char)) ctypes.memmove(p, decryptedbuff, fd_size) print "[+] Pointer : " + str(hex(p)) pefilepath = pefile.PE(filepath)
def accumulate_changes(initial: int, changes: Iterable[int]) -> Iterable[int]: cycled = cycle(changes) prepended = prepend(initial, cycled) return accumulate(prepended)
# embed.add_field(name='***unban <nombre del miembro>**', value='Elimina el ban al miembro especificado', inline=False) embed.add_field(name='***warn <@member> [motivo]**', value="Avisa a alguien. Especifica motivo por favor", inline=False) embed.add_field(name='***mute <@member>**', value="Mutea a alguien", inline=False) embed.add_field(name='***tmute <@member> <tiempo(minutos)>**', value="Mutea a alguien durante el tiempo que le digas", inline=False) #embed.add_field(name='*', value=None, inline=False) await ctx.send(embed=embed) await log.log(ctx, f"Help from {ctx.author.name}") keep_alive() #Blinking current statuses activities = cycle( [f"*help | Bot Oficial | V{version}", f"*help | H4ppu Bot | By Appu"]) @client.command(hidden=True) @commands.check(commands.is_owner()) async def logout(ctx): msg = await ctx.send('Desconectando...') await msg.delete(delay=2) await client.logout() #Load all extensions extensions = [] for filename in os.listdir('./cogs'): if str(filename).endswith('.py'): if ("COG" in str(filename[:-3])): client.load_extension(f"cogs.{filename[:-3]}")
from matplotlib import pyplot as plt import numpy as np from itertools import cycle lines = ["-","--","-.",":"] linecycler = cycle(lines) markers = ['+','.','o','^'] markercycler = cycle(markers) #from matplotlib.font_manager import FontProperties #fontP = FontProperties() #fontP.set_size('large') import matplotlib matplotlib.rcParams.update({'font.size': 12}) suffix="ithaca_final" #versions = ["shared", "local_copies", "hybrid", "remote_inserts", "remote_inserts_filtered", "shared_filtered", "local_copies_filtered", "augmented_sketch", "delegation_filters", "delegation_filters_with_linked_list"] versions = ["shared", "local_copies", "augmented_sketch", "delegation_filters_with_linked_list"] #versions = ["local_copies", "delegation_filters_with_linked_list"] #versions = ["local_copies", "shared", "remote_inserts","shared_small"] #fancy_names = ["Thread-local", "Single Sketch", "Domain-spliting", "Reference"] fancy_names = ["Single-shared", "Thread-local", "Augmented Sketch", "Delegation Sketch"] #filename = "count_min_results.txt" #thread_list=range(4,76,4) thread_list=[4] #thread_list=range(1,11) def runningMeanFast(x, N): return np.convolve(x, np.ones((N,))/N)[(N-1):]
if __name__ == '__main__': import matplotlib.pyplot as plt import wom_memory from coders.simple_binary import Binary import numpy as np def generate_random_input(length, one_ratio=0.5): import random return ''.join([ "1" if random.random() < one_ratio else "0" for i in range(length) ]) memory_size = 50000 import itertools markers = itertools.cycle(('o', 's', '*', '+', '>', 'p', 'x', '<', '4')) def w_wo(val): return 'w' if val else 'wo' with_toggle = False main_L = 6 one_probs = np.linspace(0.01, 0.5, 11) for with_complement in [True, False]: for with_padding in [False, True]: label = ' L = {2}, {0} Padding, {1}. complement'.format( w_wo(with_padding), w_wo(with_complement), main_L) res = [ TwoSidedGuidedBlocks.theory(main_L, one_prob=one_prob,
vels_vec[:, i] += rand_vel_vec[i] return vels_vec def grab_property(f, part_type, field): try: prop = np.asarray(f['/PartType%d/%s' % (part_type, field)]) except KeyError: prop = np.asarray([]) #print('KeyError: PartType%d/%s' % (part_type, field)) return prop indices = itertools.cycle(np.arange(0, len(labels) + 1, 1)) Ngas = np.zeros(len(labels), dtype='uint32') Ndm = np.copy(Ngas) Ndisk = np.copy(Ngas) Nbulge = np.copy(Ngas) Nstar = np.copy(Ngas) Nbh = np.copy(Ngas) gas = {} dm = {} disk = {} bulge = {} star = {} bh = {}
def __init__(self, options): # save all options self.options = options # capture top level figure opbject # create cyclers for options in plot self.kind = cycle(options_to_list(options.kind)) self.subplot = cycle(options_to_list(options.plot)) self.colors = cycle(options_to_list(options.color)) self.linestyles = cycle(options_to_list(options.linestyle)) self.markers = cycle(options_to_list(options.marker)) self.markersizes = cycle(options_to_list(options.markersize)) self.linewidths = cycle(options_to_list(options.linewidth)) self.legends = cycle(options_to_list(options.legend)) self.legends_fontsize = cycle(options_to_list(options.legend_fontsize)) self.xmins = cycle(options_to_list(options.xmin)) self.xmaxs = cycle(options_to_list(options.xmax)) self.ymins = cycle(options_to_list(options.ymin)) self.ymaxs = cycle(options_to_list(options.ymax)) self.zmins = cycle(options_to_list(options.zmin)) self.zmaxs = cycle(options_to_list(options.zmax)) self.alphas = cycle(options_to_list(options.alpha)) # create grid of subplots plt.subplots(options.rows, options.columns) # create corresponding subplots self.subplots = {} for i in range(1, options.rows * options.columns + 1): self.subplots[i] = SubPlot(options, i) pass self.figure = plt.figure(figsize=options.figure_size, dpi=options.dpi, facecolor=None, edgecolor=None, linewidth=0.0, frameon=None, subplotpars=None, tight_layout=None) pass
def test_peak_doesnt_move_stream_forward(self): generator = cycle('abc') peaker = Peaker(stream=generator) self.assertEqual(peaker.peak(), 'a') self.assertEqual(peaker.peak(), 'a')
def get_dummies( data, prefix=None, prefix_sep="_", dummy_na: bool = False, columns=None, sparse: bool = False, drop_first: bool = False, dtype: Dtype | None = None, ) -> DataFrame: """ Convert categorical variable into dummy/indicator variables. Parameters ---------- data : array-like, Series, or DataFrame Data of which to get dummy indicators. prefix : str, list of str, or dict of str, default None String to append DataFrame column names. Pass a list with length equal to the number of columns when calling get_dummies on a DataFrame. Alternatively, `prefix` can be a dictionary mapping column names to prefixes. prefix_sep : str, default '_' If appending prefix, separator/delimiter to use. Or pass a list or dictionary as with `prefix`. dummy_na : bool, default False Add a column to indicate NaNs, if False NaNs are ignored. columns : list-like, default None Column names in the DataFrame to be encoded. If `columns` is None then all the columns with `object` or `category` dtype will be converted. sparse : bool, default False Whether the dummy-encoded columns should be backed by a :class:`SparseArray` (True) or a regular NumPy array (False). drop_first : bool, default False Whether to get k-1 dummies out of k categorical levels by removing the first level. dtype : dtype, default np.uint8 Data type for new columns. Only a single dtype is allowed. Returns ------- DataFrame Dummy-coded data. See Also -------- Series.str.get_dummies : Convert Series to dummy codes. Examples -------- >>> s = pd.Series(list('abca')) >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 >>> s1 = ['a', 'b', np.nan] >>> pd.get_dummies(s1) a b 0 1 0 1 0 1 2 0 0 >>> pd.get_dummies(s1, dummy_na=True) a b NaN 0 1 0 0 1 0 1 0 2 0 0 1 >>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], ... 'C': [1, 2, 3]}) >>> pd.get_dummies(df, prefix=['col1', 'col2']) C col1_a col1_b col2_a col2_b col2_c 0 1 1 0 0 1 0 1 2 0 1 1 0 0 2 3 1 0 0 0 1 >>> pd.get_dummies(pd.Series(list('abcaa'))) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 >>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True) b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 >>> pd.get_dummies(pd.Series(list('abc')), dtype=float) a b c 0 1.0 0.0 0.0 1 0.0 1.0 0.0 2 0.0 0.0 1.0 """ from pandas.core.reshape.concat import concat dtypes_to_encode = ["object", "category"] if isinstance(data, DataFrame): # determine columns being encoded if columns is None: data_to_encode = data.select_dtypes(include=dtypes_to_encode) elif not is_list_like(columns): raise TypeError( "Input must be a list-like for parameter `columns`") else: data_to_encode = data[columns] # validate prefixes and separator to avoid silently dropping cols def check_len(item, name): if is_list_like(item): if not len(item) == data_to_encode.shape[1]: len_msg = ( f"Length of '{name}' ({len(item)}) did not match the " "length of the columns being encoded " f"({data_to_encode.shape[1]}).") raise ValueError(len_msg) check_len(prefix, "prefix") check_len(prefix_sep, "prefix_sep") if isinstance(prefix, str): prefix = itertools.cycle([prefix]) if isinstance(prefix, dict): prefix = [prefix[col] for col in data_to_encode.columns] if prefix is None: prefix = data_to_encode.columns # validate separators if isinstance(prefix_sep, str): prefix_sep = itertools.cycle([prefix_sep]) elif isinstance(prefix_sep, dict): prefix_sep = [prefix_sep[col] for col in data_to_encode.columns] with_dummies: list[DataFrame] if data_to_encode.shape == data.shape: # Encoding the entire df, do not prepend any dropped columns with_dummies = [] elif columns is not None: # Encoding only cols specified in columns. Get all cols not in # columns to prepend to result. with_dummies = [data.drop(columns, axis=1)] else: # Encoding only object and category dtype columns. Get remaining # columns to prepend to result. with_dummies = [data.select_dtypes(exclude=dtypes_to_encode)] for (col, pre, sep) in zip(data_to_encode.items(), prefix, prefix_sep): # col is (column_name, column), use just column data here dummy = _get_dummies_1d( col[1], prefix=pre, prefix_sep=sep, dummy_na=dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype, ) with_dummies.append(dummy) result = concat(with_dummies, axis=1) else: result = _get_dummies_1d( data, prefix, prefix_sep, dummy_na, sparse=sparse, drop_first=drop_first, dtype=dtype, ) return result
def plot_ROC(true_labels, confusionPredictions, Real_labels): #Adapted from: https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html #Setup data formats for storing fpr = dict() tpr = dict() roc_auc = dict() # Binarize the output y_binary = label_binarize(true_labels, classes=np.array(range(0,Real_labels.shape[0]))) #Get the number of classes n_classes = y_binary.shape[1] print('Number of classes: ', n_classes) #Iterating over each class for i in range(n_classes): #Caluclate the roc for each class fpr[i], tpr[i], _ = roc_curve(y_binary[:,i], confusionPredictions[:,i]) roc_auc[i] = auc(fpr[i], tpr[i]) # Compute macro-average ROC curve and ROC area # First aggregate all false positive rates all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)])) # Then interpolate all ROC curves at this points mean_tpr = np.zeros_like(all_fpr) #Find the total across all classes for i in range(n_classes): mean_tpr += interp(all_fpr, fpr[i], tpr[i]) # Finally average it and compute AUC mean_tpr /= n_classes fpr["macro"] = all_fpr tpr["macro"] = mean_tpr roc_auc["macro"] = auc(fpr["macro"], tpr["macro"]) #Plotting the results plt.figure() lw = 2 colors = cycle(['yellow', 'darkorange', 'darkgreen','darkred','magenta','olive','maroon']) classes = [] for c in Real_labels: classes.append(c) y_axis = c + ' y-axis' classes.append(y_axis) plt.plot(fpr["macro"], tpr["macro"], label='macro-average ROC curve (area = {0:0.2f})' ''.format(roc_auc["macro"]), color='navy', linestyle=':', linewidth=4) plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic') plt.legend(loc="lower right") plt.savefig('ROC.png') #Get axis handle for plot axis = plt.gca() longest = 0 #Find how much data we need to save (excel formating) for i in range(1): series = axis.lines[i] new_longest = np.array(series.get_xdata()).shape[0] if new_longest > longest: longest = new_longest plot_data = np.zeros([longest,Real_labels.shape[0]*2]) #Grab the x,y data from the plot for i in range(1): series = axis.lines[i] print('shape: ', np.array(series.get_xdata()).shape) x_data = np.array(series.get_xdata()) y_data = np.array(series.get_ydata()) plot_data[0:x_data.shape[0],i*2] = x_data plot_data[0:y_data.shape[0],i*2+1] = y_data #Save the results #np.savetxt('3-ChannelROCDataFinal.csv', plot_data, delimiter =",") #plt.show() return plot_data
import itertools with open('input') as f: lines = f.readlines() numsum = 0 numseen = {0} for num in itertools.cycle(lines): numsum += int(num) if numsum in numseen: print(numsum) print(len(numseen)) break numseen.add(numsum)
def plotTIMESERIES_CATCH(DateInput, flx, flx_lbl, plt_export_fn, plt_title, hmax, hmin, cMF = None): """ Plot the time serie of the fluxes observed from the whole catchment Use Matplotlib """ monthsFmt=mpl.dates.DateFormatter('%Y-%m-%d') lblspc = 0.05 mkscale = 0.5 fig = plt.figure(num=None, figsize=(2*8.27, 2*11.7), dpi = 30) #(8.5,15), dpi=30) fig.suptitle(plt_title) ax1=fig.add_subplot(10,1,1) plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax1.get_yticklabels(), fontsize=8) ax1.bar(DateInput,flx[0],color='darkblue', linewidth=0, align = 'center', label = flx_lbl[0]) ax1.bar(DateInput,flx[2],color='deepskyblue', linewidth=0, align = 'center', label = flx_lbl[2]) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) plt.ylabel('mm', fontsize=10) ax1.xaxis.set_major_formatter(monthsFmt) ax1.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax2=fig.add_subplot(10,1,2, sharex=ax1) plt.setp(ax2.get_xticklabels(), visible=False) plt.setp(ax2.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,flx[4],'r-', c='darkblue', linewidth=2, label = flx_lbl[4]) plt.plot_date(DateInput,flx[5],'r-', c='deepskyblue', linewidth=0.75, label = flx_lbl[5]) plt.bar(DateInput, flx[14], color='lightblue', linewidth=0, align = 'center', label = flx_lbl[14]) plt.bar(DateInput, flx[3], color='blue', width=0.60, linewidth=0, align = 'center', label = flx_lbl[3]) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.ylabel('mm', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) ax2.xaxis.set_major_formatter(monthsFmt) ax2.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) E_tot = flx[8] + flx[11] ax3=fig.add_subplot(10,1,3, sharex=ax1) plt.setp(ax3.get_xticklabels(), visible=False) plt.setp(ax3.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,flx[15],'-', color='lightblue', linewidth=3, label = flx_lbl[15]) plt.plot_date(DateInput,E_tot,'-', color='darkblue', linewidth=1.5, label = 'E_tot') plt.plot_date(DateInput,flx[8],'-.', color='brown', label = flx_lbl[8]) plt.plot_date(DateInput,flx[11],'-', color='blue', label = flx_lbl[11]) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) plt.ylabel('mm', fontsize=10) ax3.xaxis.set_major_formatter(monthsFmt) ax3.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) T_tot = flx[9] + flx[12] ax4=fig.add_subplot(10,1,4, sharex=ax1) plt.setp(ax4.get_xticklabels(), visible=False) plt.setp(ax4.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,flx[16],'-', color='lightblue', linewidth=3, label = flx_lbl[16]) plt.plot_date(DateInput,T_tot,'-', color='darkblue', linewidth=1.5, label = 'T_tot') plt.plot_date(DateInput,flx[9],'-.', color='brown', label = flx_lbl[9]) plt.plot_date(DateInput,flx[12],'-', color='blue', label = flx_lbl[12]) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) plt.ylabel('mm', fontsize=10) ax4.xaxis.set_major_formatter(monthsFmt) ax4.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax5=fig.add_subplot(10,1,5, sharex=ax1) plt.setp(ax5.get_xticklabels(), visible=False) plt.setp(ax5.get_yticklabels(), fontsize=8) plt.bar(DateInput,flx[7], color='lightblue', linewidth=0, align = 'center', label = flx_lbl[7]) ax5.plot_date(DateInput, flx[17], '-', color = 'brown', label= 'Rp') if cMF != None: plt.plot_date(DateInput,flx[19],'-', c='darkblue', linewidth=2, label = flx_lbl[19]) plt.plot_date(DateInput,flx[13],'-', c='blue', linewidth=1.5, label = flx_lbl[13]) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(loc = 0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) plt.ylabel('mm', fontsize=10) ax5.xaxis.set_major_formatter(monthsFmt) ax5.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax6=fig.add_subplot(10,1,6, sharex=ax1) plt.setp(ax6.get_yticklabels(), fontsize=8) ax6.plot_date(DateInput, flx[18], '-', color = 'brown', label = flx_lbl[18]) # x axis plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) # y axis plt.ylabel('%', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) if cMF == None: plt.setp(ax6.get_xticklabels(), fontsize=8) plt.xlabel('Date', fontsize=10) labels=ax6.get_xticklabels() plt.setp(labels, 'rotation', 90) else: plt.setp(ax6.get_xticklabels(), visible=False) plt.grid(True) ax6.xaxis.set_major_formatter(monthsFmt) ax6.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) if cMF != None: # plot heads lines = itertools.cycle(['-','--','-.',':','.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_']) ax7=fig.add_subplot(10,1,7, sharex=ax1) plt.setp(ax7.get_xticklabels(), visible=False) plt.setp(ax7.get_yticklabels(), fontsize=8) i = 20 for l in range(cMF.nlay): plt.plot_date(DateInput,flx[i],lines.next(), color = 'b', label = flx_lbl[i]) i += l + 2 plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.ylim(hmin,hmax) plt.ylabel('m', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax7.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax7.xaxis.set_major_formatter(monthsFmt) # plot GW fluxes ax8=fig.add_subplot(10,1,8, sharex=ax1) plt.setp(ax8.get_xticklabels(), fontsize=8) plt.setp(ax8.get_yticklabels(), fontsize=8) i = 20 + 2*cMF.nlay for l, (e, lbl) in enumerate(zip(flx[i:], flx_lbl[i:])): plt.plot_date(DateInput,e,'-', color = mpl.colors.rgb2hex(np.random.rand(1,3)[0]), label = lbl) i += l + 2 plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) labels=ax8.get_xticklabels() plt.setp(labels, 'rotation', 90) del labels plt.xlabel('Date', fontsize=10) plt.ylabel('mm', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax8.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax8.xaxis.set_major_formatter(monthsFmt) # plot GWT lines = itertools.cycle(['-','--','-.',':','.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_']) ax10=fig.add_subplot(10,1,10, sharex=ax1) plt.setp(ax10.get_xticklabels(), visible=False) plt.setp(ax10.get_yticklabels(), fontsize=8) i = 21 for l in range(cMF.nlay): plt.plot_date(DateInput,flx[i],lines.next(), color = 'b', label = flx_lbl[i]) i += l + 2 plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.ylabel('m', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax10.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax10.xaxis.set_major_formatter(monthsFmt) plt.subplots_adjust(left=0.10, bottom=0.10, right=0.95, top=0.95, wspace=0.1, hspace=0.1) plt.savefig(plt_export_fn,dpi=150) # plt.show() plt.clf() plt.close('all')
def test_next_does_move_stream_forward(self): generator = cycle('abc') peaker = Peaker(stream=generator) self.assertEqual(peaker.next(), 'a') self.assertEqual(peaker.next(), 'b')
next(myiter) # 3 next(myiter) # StopIteration # itertools Examples import itertools as it counter = it.count(start=3) next(counter) # 3 next(counter) # 4 next(counter) # 5 mylist = [1, 2, 3] number = it.cycle(mylist) next(number) # 1 next(number) # 2 next(number) # 3 next(number) # 1 next(number) # 2 #%% Comprehension # List Comprehension mylist = [2, 3, 4, 5, 6, 7] mycomp = [i for i in mylist] mycomp #[2, 3, 4, 5, 6, 7]
def main(): parser = argparse.ArgumentParser() parser.add_argument("--title", default="boook", help="") parser.add_argument("--section", default=None, action="append", nargs=3, metavar=('name', 'amount', 'numeration'), help="") parser.add_argument("--output-path", default=".", help="") parser.add_argument("--manifest", default=None, nargs="+", choices=["csv"], help="") parser.add_argument("--verbose", action="store_true", help="") parser.add_argument("--dry-run", action="store_true", help="generate manifests, but not image files") parser.add_argument( "--rotate", nargs="+", type=int, help="rotation values to cycle through. Uses imagemagick to rotate") parser.add_argument("--rotate-jitter", type=int, default=0, help="random jitter added to rotation") args = parser.parse_args() # special sections "toc" and "index" # for example: # toc 1 partial # index 5 partial # validate section metavars, should probably # be done using argparse machinery for section_num, section in enumerate(args.section): title, amount, numeration = section try: args.section[section_num][1] = int(amount) except: raise argparse.ArgumentTypeError("value must be Integer") try: if numeration != "full" or numeration != "partial": args.section[section_num][2] = "full" except: pass # generate boook b = boook.Boook(args.title, args.section, manifest_formats=args.manifest, verbose_output=args.verbose, dry_run=args.dry_run, output_directory=args.output_path) generated_files = b.generate() if args.rotate: rotations = itertools.cycle(args.rotate) rotation = int(next(rotations)) for file in generated_files: jitter = random.randint(0, args.rotate_jitter) rotation += jitter if args.verbose: print("rotating {} to {} (jitter: {})".format( file, rotation, jitter)) # use mogrify to rotate files inplace subprocess.call(["mogrify", "-rotate", str(rotation), file]) rotation = int(next(rotations))
height=canvas_height, background='deep sky blue') #create ground c.create_rectangle(-5, canvas_height - 100, canvas_width + 5, canvas_height + 5, fill='sea green', width=0) #create sun c.create_oval(-80, -80, 120, 120, fill='orange', width=0) c.pack() #create eggs color_cycle = cycle([ 'light blue', 'light pink', 'light yellow', 'light green', 'red', 'blue', 'green', 'black' ]) egg_width = 45 egg_height = 55 egg_score = 10 egg_speed = 500 egg_interval = 4000 difficulty_factor = 0.95 #create catcher catcher_color = 'blue' catcher_width = 100 catcher_height = 100 catcher_start_x = canvas_width / 2 - catcher_width / 2 catcher_start_y = canvas_height - catcher_height - 20 catcher_start_x2 = catcher_start_x + catcher_width
def plotTIMESERIES(DateInput, P, PT, PE, Pe, dPOND, POND, Ro, Eu, Tu, Eg, Tg, S, dS, Spc, Rp, EXF, ETg, Es, MB, MB_l, dgwt, uzthick, SAT, R, h_MF, h_MF_corr, h_SF, hobs, Sobs, Sm, Sr, hnoflo, plt_export_fn, plt_title, colors_nsl, hmax, hmin, obs_name, elev, nlay): """ Plot the time serie of the fluxes observed at one point of the catchment Use Matplotlib _______________________________________________________________________________ INPUTS STATE VARIABLES SP Stress period P Daily rainfall PT Daily potential transpiration PE Daily potential evaporation Pe Daily Excess rainfall Eu Daily evaporation (bare soil) Tu Daily transpiration S Daily soil moisture Rp Daily percolation POND Daily ponding Ro Daily runoff R Daily recharge h Daily water level hobs Daily obsserved water level ______________________________________________________________________________ ______________________________________________________________________________ """ monthsFmt=mpl.dates.DateFormatter('%Y-%m-%d') lblspc = 0.05 mkscale = 0.5 fig = plt.figure(num=None, figsize=(2*8.27, 2*11.7), dpi = 30) #(8.5,15), dpi=30) fig.suptitle(plt_title) nsl = len(Tu[0]) lbl_Spc = [] lbl_Spcfull = [] lbl_S = [] lbl_dS = [] lbl_Sobs = [] lbl_Rp = [] lbl_Eu = [] lbl_Tu = [] lbl_SAT = [] lbl_MB =[] lbl_Eu.append('PE') lbl_Eu.append('E_tot') lbl_Eu.append('Eu_tot') lbl_Tu.append('PT') lbl_Tu.append('T_tot') lbl_Tu.append('Tu_tot') lbl_MB.append('MB') Sobs_m = [] Eu1 = [] Tu1 = [] dS1 = [] S1 = [] Rp1 = [] Spc1 = [] Spc1full = [] SAT1 = [] MB_l1 = [] for l in range(nsl): lbl_Spcfull.append('Su_l'+str(l+1)) lbl_S.append('Su_l'+str(l+1)) lbl_dS.append(r'$\Delta$Su_l'+str(l+1)) lbl_Eu.append('Eu_l'+str(l+1)) lbl_Tu.append('Tu_l'+str(l+1)) lbl_SAT.append('l'+str(l+1)) lbl_MB.append('MB_l'+str(l+1)) lbl_Spc.append('Su_l'+str(l+1)) lbl_Rp.append('Rp_l'+str(l+1)) Spc1full.append(Spc[:,l]) Eu1.append(Eu[:,l]) Tu1.append(Tu[:,l]) dS1.append(dS[:,l]) S1.append(S[:,l]) SAT1.append(SAT[:,l]) MB_l1.append(MB_l[:,l]) Spc1.append(Spc[:,l]) Rp1.append(Rp[:,l]) try: Sobs_m.append(np.ma.masked_values(Sobs[l], hnoflo, atol = 0.09)) lbl_Sobs.append('Su_l'+str(l+1)+'_obs') except: Sobs_m.append([]) del dS, S, SAT, MB_l del Rp, Spc Eu1 = np.asarray(Eu1) Tu1 = np.asarray(Tu1) dS1 = np.asarray(dS1) S1 = np.asarray(S1) Rp1 = np.asarray(Rp1) Spc1 = np.asarray(Spc1) SAT1 = np.asarray(SAT1) MB_l1 = np.asarray(MB_l1) lbl_Rp.append('R') lbl_Rp.append('ETg') lbl_Rp.append('EXF') lbl_Tu.append('Tg') lbl_Eu.append('Eg') ax1=fig.add_subplot(10,1,1) plt.setp(ax1.get_xticklabels(), visible=False) plt.setp(ax1.get_yticklabels(), fontsize=8) ax1.bar(DateInput,P,color='darkblue', linewidth=0, align = 'center', label='RF') ax1.bar(DateInput,Pe,color='deepskyblue', linewidth=0, align = 'center', label='RFe') plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) plt.ylabel('mm', fontsize=10) ax1.xaxis.set_major_formatter(monthsFmt) ax1.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax2=fig.add_subplot(10,1,2, sharex=ax1) plt.setp(ax2.get_xticklabels(), visible=False) plt.setp(ax2.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,Ro,'r-', c='darkblue', linewidth=2, label = 'Ro') plt.plot_date(DateInput,Es,'r-', c='deepskyblue', linewidth=0.75, label = 'Es') plt.bar(DateInput, POND, color='lightblue', linewidth=0, align = 'center', label = 'Ss') plt.bar(DateInput, dPOND, color='blue', width=0.60, linewidth=0, align = 'center', label = r'$\Delta$Ss') plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.ylabel('mm', fontsize=10) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) ax2.xaxis.set_major_formatter(monthsFmt) ax2.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) Eu_tot = [] for e in Eu: Eu_tot.append(e.sum()) Eu_tot = np.asarray(Eu_tot) E_tot = Eu_tot + Eg del Eu ax3=fig.add_subplot(10,1,3, sharex=ax1) plt.setp(ax3.get_xticklabels(), visible=False) plt.setp(ax3.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,PE,'-', color='lightblue', linewidth=3) plt.plot_date(DateInput,E_tot,'-', color='darkblue', linewidth=1.5) plt.plot_date(DateInput,Eu_tot,'-.', color=colors_nsl[len(colors_nsl)-1]) for l, (y, color, lbl) in enumerate(zip(Eu1, colors_nsl, lbl_Eu[2:len(lbl_Eu)])): ax3.plot_date(DateInput, y, '-', color=color, label=lbl) plt.plot_date(DateInput,Eg,'-', color='blue') plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(lbl_Eu, loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8) plt.grid(True) plt.ylabel('mm', fontsize=10) ax3.xaxis.set_major_formatter(monthsFmt) ax3.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) Tu_tot = [] for t in Tu: Tu_tot.append(t.sum()) Tu_tot = np.asarray(Tu_tot) T_tot = Tu_tot + Tg del Tu ax4=fig.add_subplot(10,1,4, sharex=ax1) plt.setp(ax4.get_xticklabels(), visible=False) plt.setp(ax4.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,PT,'-', color='lightblue', linewidth=3) plt.plot_date(DateInput,T_tot,'-', color='darkblue', linewidth=1.5) plt.plot_date(DateInput,Tu_tot,'-.', color=colors_nsl[len(colors_nsl)-1]) for l, (y, color, lbl) in enumerate(zip(Tu1, colors_nsl, lbl_Tu[2:len(lbl_Tu)])): ax4.plot_date(DateInput, y, '-', color=color, label=lbl) plt.plot_date(DateInput,Tg,'-', color='blue') plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(lbl_Tu, loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) plt.ylabel('mm', fontsize=10) ax4.xaxis.set_major_formatter(monthsFmt) ax4.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax5=fig.add_subplot(10,1,5, sharex=ax1) plt.setp(ax5.get_xticklabels(), visible=False) plt.setp(ax5.get_yticklabels(), fontsize=8) plt.bar(DateInput,EXF, color='lightblue', linewidth=0, align = 'center', label='EXF') for l, (y, color, lbl) in enumerate(zip(Rp1, colors_nsl, lbl_Rp[2:len(lbl_Rp)])) : ax5.plot_date(DateInput, y, '-', color=color, label=lbl) plt.plot_date(DateInput,R,'-', c='darkblue', linewidth=2) plt.plot_date(DateInput,ETg,'-', c='blue', linewidth=1.5) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) plt.legend(lbl_Rp, loc = 0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) plt.ylabel('mm', fontsize=10) ax5.xaxis.set_major_formatter(monthsFmt) ax5.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax6=fig.add_subplot(10,1,6, sharex=ax1) plt.setp(ax6.get_xticklabels(), visible=False) plt.setp(ax6.get_yticklabels(), fontsize=8) try: for l, (y, color, lbl) in enumerate(zip(Sobs_m, colors_nsl, lbl_Sobs)): if y != []: ax6.plot_date(DateInput, y, ls = 'None', color = 'None', marker='o', markersize=2, markeredgecolor = color, markerfacecolor = 'None', label=lbl) #'--', color = color, except: #print '\nWARNING!\nSoil moisture at observations point %s will not be plotted.' % obs_name pass for l, (y, color, lbl) in enumerate(zip(Spc1full, colors_nsl, lbl_S)) : y = np.ma.masked_where(y < 0.0, y) ax6.plot_date(DateInput, y, '-', color = color, label = lbl) ## for l, (y, color, lbl) in enumerate(zip(Spc1, colors_nsl, lbl_Spc)) : ## ax6.plot_date(DateInput, y, '-', color = color, label=lbl) # x axis plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) # y axis ybuffer=0.1*(max(Sm)-min(Sr)) plt.ylim(min(Sr) - ybuffer,max(Sm) + ybuffer) plt.ylabel('%', fontsize=10) # legend #lbl_Spcobs = lbl_Sobs + lbl_S #plt.legend(lbl_Spcobs, loc=0, labelspacing=lblspc, markerscale=mkscale) plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax6.xaxis.set_major_formatter(monthsFmt) ax6.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax7=fig.add_subplot(10,1,7, sharex=ax1) plt.setp(ax7.get_xticklabels(), fontsize=8) plt.setp(ax7.get_yticklabels(), fontsize=8) obs_leg = None try: hobs_m = np.ma.masked_values(hobs, hnoflo, atol = 0.09) plt.plot_date(DateInput,hobs_m, ls = 'None', color = 'None', marker='o', markeredgecolor = 'blue', markerfacecolor = 'None', markersize = 2) # ls='--', color = 'blue' obs_leg = 1 except: pass lines = itertools.cycle(['-','--','-.',':','.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_']) lbl_h = [] for l in range(nlay): plt.plot_date(DateInput,h_MF[:,l],lines.next(), color = 'b') lbl_h.append(r'h_MF_l%i' % (l+1)) plt.plot_date(DateInput,h_MF_corr,'-', color = 'g') plt.plot_date(DateInput,h_SF,'-', color = 'r') ax7.set_xticklabels(DateInput) plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) labels=ax7.get_xticklabels() plt.setp(labels, 'rotation', 90) ybuffer=0.1*(hmax-hmin) if ybuffer == 0.0: ybuffer = 1.0 plt.ylim((hmin - ybuffer, hmax + ybuffer)) plt.ylabel('m', fontsize=10) leg_lbl = '' if obs_leg == None: lbl_h.append(r'h_MF_corr') lbl_h.append(r'h_SF') plt.legend(tuple(lbl_h), loc=0, labelspacing=lblspc, markerscale=mkscale) elif obs_leg == 1: lbl_h.insert(0,r'h_obs') lbl_h.append(r'h_MF_corr') lbl_h.append(r'h_SF') plt.legend(tuple(lbl_h), loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.xlabel('Date', fontsize=10) plt.grid(True) ax7.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax7.xaxis.set_major_formatter(monthsFmt) ax8b=fig.add_subplot(20,1,16, sharex=ax1) plt.setp(ax8b.get_xticklabels(), visible=False) plt.setp(ax8b.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,MB,'-', c='r') for l, (y, color, lbl) in enumerate(zip(MB_l1, colors_nsl, lbl_MB[1:len(lbl_MB)])) : ax8b.plot_date(DateInput, y, '-', color=color, label=lbl) # y axis plt.ylabel('mm', fontsize=10) plt.grid(True) plt.xlim(DateInput[0]-1,DateInput[len(MB)-1]+1) if max(np.max(MB),np.max(MB_l1)) < 0.001 and min(np.min(MB), np.min(MB_l1)) > -0.001: plt.ylim(-0.1,0.1) else: minfact = 0.95 maxfact = 1.05 if min(np.min(MB), np.min(MB_l1)) > 0: minfact = 1.05 if max(np.max(MB), np.max(MB_l1)) < 0: maxfact = 0.95 plt.ylim(min(np.min(MB), np.min(MB_l1))*minfact,max(np.max(MB),np.max(MB_l1))*maxfact) # legend plt.legend(lbl_MB, loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) ax8b.xaxis.set_major_formatter(monthsFmt) ax8b.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax9a=fig.add_subplot(20,1,17, sharex=ax1) plt.setp(ax9a.get_xticklabels(), visible=False) plt.setp(ax9a.get_yticklabels(), fontsize=8) for l, (y, color, lbl) in enumerate(zip(SAT1, colors_nsl, lbl_SAT)) : ax9a.plot_date(DateInput, y, '-', color = color, label = lbl) # x axis plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) # y axis plt.ylim(-0.1,1.1) plt.ylabel('SAT', fontsize=10) ax9a.yaxis.set_ticks(np.arange(0,1.25,1)) ax9a.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%1d')) # legend plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax9a.xaxis.set_major_formatter(monthsFmt) ax9b=fig.add_subplot(20,1,18, sharex=ax1) plt.setp(ax9b.get_xticklabels(), visible=False) plt.setp(ax9b.get_yticklabels(), fontsize=8) obs_leg = None try: hobs_m = np.ma.masked_values(hobs, hnoflo, atol = 0.09) - elev plt.plot_date(DateInput,hobs_m, ls = 'None', color = 'None', marker='o', markeredgecolor = 'blue', markerfacecolor = 'None', markersize = 2) # ls='--', color = 'blue' obs_leg = 1 except: pass plt.plot_date(DateInput,dgwt,'-', c='b') # y axis plt.ylabel('m', fontsize=10) plt.grid(True) plt.xlim(DateInput[0]-1,DateInput[len(dgwt)-1]+1) #plt.ylim(np.min(dgwt)*1.05,0.25) # legend if obs_leg == None: plt.legend(['DGWT_MF'], loc=0, labelspacing=lblspc, markerscale=mkscale) elif obs_leg == 1: plt.legend((r'DGWT_obs','DGWT_MF'), loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() plt.ylim(ymax = 0.0) ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) ax9b.xaxis.set_major_formatter(monthsFmt) ax9b.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax10a=fig.add_subplot(20,1,19, sharex=ax1) plt.setp(ax10a.get_xticklabels(), visible=False) plt.setp(ax10a.get_yticklabels(), fontsize=8) plt.plot_date(DateInput,uzthick,'-', c='brown') # y axis plt.ylabel('m', fontsize=10) plt.grid(True) plt.xlim(DateInput[0]-1,DateInput[len(uzthick)-1]+1) minfact = 0.95 maxfact = 1.05 if np.min(uzthick) < 0: minfact = 1.05 if np.max(uzthick) < 0: maxfact = 0.95 if np.min(uzthick) == np.max(uzthick) == 0.0: plt.ylim(-0.5, 0.5) else: plt.ylim(np.min(uzthick)*minfact, np.max(uzthick)*maxfact) # legend plt.legend(['uzthick'], loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) ax10a.xaxis.set_major_formatter(monthsFmt) ax10a.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) ax10b=fig.add_subplot(20,1,20, sharex=ax1) plt.setp(ax10b.get_xticklabels(), visible=False) plt.setp(ax10b.get_yticklabels(), fontsize=8) for l, (y, color, lbl) in enumerate(zip(S1, colors_nsl, lbl_S)) : y = np.ma.masked_where( y < 0.0, y) ax10b.plot_date(DateInput, y, '-', color=color, label=lbl) # x axis plt.xlim(DateInput[0]-1,DateInput[len(DateInput)-1]+1) # y axis plt.ylim(0,np.max(S1)*1.05) plt.ylabel('mm', fontsize=10) ax10b.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2G')) # legend plt.legend(loc=0, labelspacing=lblspc, markerscale=mkscale) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=8 ) plt.grid(True) ax10b.xaxis.set_major_formatter(monthsFmt) plt.subplots_adjust(left=0.10, bottom=0.10, right=0.95, top=0.95, wspace=0.1, hspace=0.1) plt.savefig(plt_export_fn,dpi=150) # plt.show() plt.clf() plt.close('all') del fig, DateInput, P, PT, PE, Pe, dPOND, POND, Ro, Eu1, Tu1, Eg, Tg, S1, dS1, Spc1, Rp1, EXF, R, ETg, Es, MB, h_MF, h_SF, hobs, Sobs, Sm, Sr, hnoflo, plt_export_fn, plt_title, colors_nsl, hmax, hmin
plt.figure() plt.plot(fpr['micro'], tpr['micro'], label='micro-average ROC curve (area = {0:0.2f})' ''.format(i, roc_auc['micro']), color='deeppink', linestyle=':', linewidth=4) plt.plot(fpr['macro'], tpr['macro'], label='macro-average ROC curve (area = {0:0.2f})' ''.format(i, roc_auc['macro']), color='navy', linestyle=':', linewidth=4) colors = cycle(['aqua', 'darkorange', 'cornflowerblue']) for i, color in zip(range(n_classes), colors): plt.plot(fpr[i], tpr[i], color=color, lw=lw, label='ROC curve of class {0} (area = {1:0.2f})' ''.format(i, roc_auc[i])) plt.plot([0, 1], [0, 1], 'k--', lw=lw) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Some extension of Receiver operating characteristic to multi-class') plt.legend(loc='lower right')
def UniqueMarkers(): import itertools markers = itertools.cycle(('x', '1', '+', '.', '*', 'D', 'v', 'h')) return markers
class IrsaTestFixed(unittest.TestCase): COLORS = itertools.cycle(["r", "b", "g", "m", "k"]) MARKERS = itertools.cycle(["s", "o", "^", "v", "<"]) @classmethod def setUpClass(cls): # directory to store the test plots try: os.mkdir("../tests") except FileExistsError: # do nothing all good pass plt.style.use('classic') def test_varying_frame_size_fixed(self): """ We use visual benchmark by plotting the values... If more reliable benchmark values available, use assertAlmostEqual :return: """ params = { "save_to": "", "sim_duration": 100, "max_iter": 20, "traffic_type": "bernoulli", "degree_distr": [0, 0, 0.5, 0.28, 0, 0, 0, 0, 0.22] } load_range = [0.1 * x for x in range(1, 11)] plt.figure() # benchmark values taken from the IRSA paper (just visual reading from the plot) values = { 50: [0.1, 0.2, 0.3, 0.4, 0.5, 0.59, 0.65, 0.6, 0.37, 0.19], 200: [0.1, 0.2, 0.3, 0.4, 0.5, 0.59, 0.69, 0.76, 0.47, 0.19], 1000: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.79, 0.57, 0.19] } # determined arbitrary tolerance = 0.01 results_to_store = {} for m in [50, 200, 1000]: color = next(IrsaTestFixed.COLORS) marker = next(IrsaTestFixed.MARKERS) thr = [] thr_c = [] params["num_resources"] = m for load_idx, load in enumerate(load_range): params["num_ues"] = int(m * load) params["act_prob"] = 1 res = irsa.irsa_run(**params) t = np.mean(res.throughput_normalized) tc = irsa.mean_confidence_interval(res.throughput_normalized) thr.append(t) thr_c.append(tc) # FIXME it will be certainly valuable to check whether confidence interval is not too high self.assertAlmostEqual(values[m][load_idx], t, delta=tc + tolerance) results_to_store[str(m)] = thr results_to_store[str(m) + "_c"] = thr_c plt.errorbar(load_range, thr, linestyle="--", color=color, yerr=thr_c, label=r"$m=%d$" % m) plt.plot(load_range, values[m], linestyle="", color=color, markeredgecolor=color, marker=marker, label=r"IRSA, $m=%d$" % m, markerfacecolor="None") with open("../tests/varying_frame_size.json", "w") as f: json.dump(results_to_store, f) plt.ylabel("Normalized throughput") plt.xlabel("Offered Load") plt.legend(loc=0) plt.grid(True) plt.savefig("../tests/varying_frame_size_fixed.pdf") def test_packet_loss_fixed(self): """ We use visual benchmark by plotting the values... If more reliable benchmark values available, use assertAlmostEqual :return: """ params = { "save_to": "", "sim_duration": 100000, # long simulations needed to capture packet loss "num_resources": 200, "traffic_type": "bernoulli", "max_iter": 20 } load_range = [0.1 * x for x in range(1, 11)] plt.figure() degree_distrs = [ [0, 1], # slotted aloha [0, 0, 1], # 2-regular CRDSA [0, 0, 0, 0, 1], # 4-regular CRDSA [0, 0, 0.5, 0.28, 0, 0, 0, 0, 0.22], [0, 0, 0.25, 0.6, 0, 0, 0, 0, 0.15] ] degree_distr_labels = [ "s-aloha", "2-CRDSA", "4-CRDSA", r"$\Lambda_3$", r"$\Lambda_4$" ] results_to_store = {} for label, degree_distr in zip(degree_distr_labels, degree_distrs): color = next(IrsaTestFixed.COLORS) marker = next(IrsaTestFixed.MARKERS) params["degree_distr"] = degree_distr pktl = [] for load_idx, load in enumerate(load_range): params["num_ues"] = int(params["num_resources"] * load) params["act_prob"] = 1 res = irsa.irsa_run(**params) mean_pktl = np.mean(res.packet_loss) pktl.append(mean_pktl) # FIXME it will be certainly valuable to check whether confidence interval is not too high # self.assertAlmostEqual(values[m][load_idx], t, delta=tc) results_to_store[label] = pktl plt.plot(load_range, pktl, "-" + color + marker, markeredgecolor=color, markerfacecolor="None", label=label) with open("../tests/pkt_loss.json", "w") as f: json.dump(results_to_store, f) plt.ylabel("Packet loss") plt.xlabel("Offered Load") plt.yscale("log") plt.ylim((1e-4, 1e0)) plt.legend(loc=0) plt.grid(True) plt.savefig("../tests/packet_loss_fixed.pdf")
def create_models(type=None): from django.contrib.auth.models import Group map_data, user_data, people_data, layer_data, document_data = create_fixtures() anonymous_group, created = Group.objects.get_or_create(name='anonymous') u, _ = get_user_model().objects.get_or_create(username='******', is_superuser=True, first_name='admin') u.set_password('admin') u.save() users = [] for ud, pd in zip(user_data, cycle(people_data)): user_name, password, first_name, last_name = ud u, created = get_user_model().objects.get_or_create(username=user_name) if created: u.first_name = first_name u.last_name = last_name u.save() u.groups.add(anonymous_group) users.append(u) get_user_model().objects.get(username='******').groups.add(anonymous_group) if not type or type == 'map': for md, user in zip(map_data, cycle(users)): title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = md m = Map(title=title, abstract=abstract, zoom=4, projection='EPSG:4326', center_x=42, center_y=-73, owner=user, bbox_x0=bbox_x0, bbox_x1=bbox_x1, bbox_y0=bbox_y0, bbox_y1=bbox_y1, category=category, ) m.save() for kw in kws: m.keywords.add(kw) m.save() if not type or type == 'document': for dd, user in zip(document_data, cycle(users)): title, abstract, kws, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), category = dd m = Document(title=title, abstract=abstract, owner=user, bbox_x0=bbox_x0, bbox_x1=bbox_x1, bbox_y0=bbox_y0, bbox_y1=bbox_y1, category=category, doc_file=f) m.save() for kw in kws: m.keywords.add(kw) m.save() if not type or type == 'layer': for ld, owner, storeType in zip(layer_data, cycle(users), cycle(('coverageStore', 'dataStore'))): title, abstract, name, typename, (bbox_x0, bbox_x1, bbox_y0, bbox_y1), dt, kws, category = ld year, month, day = map(int, (dt[:4], dt[4:6], dt[6:])) start = datetime(year, month, day) end = start + timedelta(days=365) l = Layer(title=title, abstract=abstract, name=name, typename=typename, bbox_x0=bbox_x0, bbox_x1=bbox_x1, bbox_y0=bbox_y0, bbox_y1=bbox_y1, uuid=str(uuid4()), owner=owner, temporal_extent_start=start, temporal_extent_end=end, date=start, storeType=storeType, category=category, ) l.save() for kw in kws: l.keywords.add(kw) l.save()
import ROOT from palettable import colorbrewer from itertools import cycle import os f = ROOT.TFile( 'hist-user.cylin.L1CaloSimu.MinimumBias.tag-00-00-17_OUTPUT.root') for objType, objName in [('offline', 'AntiKt4'), ('offline', 'AntiKt10'), ('offline', 'AntiKt10Trimmed'), ('offline', 'CamKt12')]: for kinematic, xaxis_label in zip(['deltaPhi'], ['#Delta#phi']): tex = [] colors = cycle(colorbrewer.qualitative.Set1_9.colors) c = ROOT.TCanvas() leg = ROOT.TLegend(0.8, 0.8, 0.9, 0.9) for selection, drawstyle in zip(['presel', 'postsel'], ['hist', 'hist same']): color = ROOT.TColor.GetColor(*next(colors)) hist = f.Get(os.path.join(selection, objType, objName, kinematic)) hist.SetStats(0) leg.AddEntry(hist, selection, "lf") hist.SetTitle('{0:s}/{1:s}'.format(objType, objName.replace('_', ' '))) hist.GetXaxis().SetTitle( 'leading object {0:s}'.format(xaxis_label)) hist.GetYaxis().SetTitle('Events') hist.GetXaxis().SetTitleOffset(1.3) hist.GetYaxis().SetTitleOffset(1.3) hist.Draw(drawstyle) hist.SetLineColor(color)
color_num = globals()[enum_name] elif enum_name + '1' in globals(): color_num = globals()[enum_name + '1'] print('Many colors for color name %s, using first.' % color_name) else: color_num = Quantity_NOC_WHITE print('Color name not defined. Use White by default') return Quantity_Color(color_num) def to_string(_string): return TCollection_ExtendedString(_string) # some thing we'll need later modes = itertools.cycle( [TopAbs_FACE, TopAbs_EDGE, TopAbs_VERTEX, TopAbs_SHELL, TopAbs_SOLID]) class Viewer3d(Display3d): def __init__(self): Display3d.__init__(self) self.Context = self.GetContext() self.Viewer = self.GetViewer() self.View = self.GetView() self._window_handle = None self._inited = False self._local_context_opened = False self.OverLayer = None