def __evaluateSPN(self): """ bottom-up evaluate the SPN """ # fine region for region_id in self.__fineRegionId: region = Region.getRegion(region_id) region.evaluate() # coarse region for region_id in self.__coarseRegionId: region = Region.getRegion(region_id) region.evaluate()
def build_features(self, image_shape): height, width = image_shape features = [] # TODO: play with minimum feature size for w in range(1, width+1): for h in range(1, height+1): x = 0 while x + w < width: y = 0 while y + h < height: # 2 horizontally aligned blocks root = Region(x,y,w, h) right = Region(x+w, y, w,h) # check if the VJ feature can be fit into the image if x + 2 * w < width: features.append(Feature([right], [root])) bottom = Region(x, y+h, w, h) # 2 vertically aligned blocks if y + 2 * h < height: features.append(Feature([root],[bottom])) # 3 horizontally aligned blocks right2 = Region(x+2*w, y, w,h) if x + 3 * w < width: features.append(Feature([right], [right2, root])) cross_bottom = Region(x+w, y+h, w, h) if x + 2 * w < width and y + 2 * h < height: features.append(Feature([right, bottom], [root, cross_bottom])) y += 1 x += 1 return features
def load_regions_from_json(self, file_path): json_string = "" with io.open(file_path, 'r') as file: for line in file.readlines(): json_string += line.split('#')[0].replace('\n', ' ') region_json = json.loads(json_string) for region in region_json: new_region = Region(region['region_name']) new_region.world = self if 'dungeon' in region: new_region.dungeon = region['dungeon'] if 'locations' in region: for location, rule in region['locations'].items(): new_location = LocationFactory(location) new_location.parent_region = new_region if self.logic_rules != 'none': new_location.access_rule = parse_rule_string( rule, self) new_location.world = self new_region.locations.append(new_location) if 'exits' in region: for exit, rule in region['exits'].items(): new_exit = Entrance('%s -> %s' % (new_region.name, exit), new_region) new_exit.connected_region = exit if self.logic_rules != 'none': new_exit.access_rule = parse_rule_string(rule, self) new_region.exits.append(new_exit) self.regions.append(new_region)
def __completeImageByMarginal(self, instance, part): self.__setInput(instance, part) self.__evaluateSPN() self.__differentiateSPN() for l in xrange(0, Parameters.imageWidth): r = l + 1 for u in xrange(0, Parameters.imageHeight): d = u + 1 num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) if part == 'LEFT': if l < Parameters.imageWidth / 2: tmp = self.__computeMarginal(region) Utility.parseBuffer.append( \ Utility.getIntValue(instance, tmp)) else: Utility.parseBuffer.append( \ Utility.getIntValue(instance, instance.getValue(u, l))) if part == 'BOTTOM': if u < Parameters.imageHeight / 2: Utility.parseBuffer.append( \ Utility.getIntValue(instance, instance.getValue(u, l))) else: tmp = self.__computeMarginal(region) Utility.parseBuffer.append( \ Utility.getIntValue(instance, tmp))
def preprocess(): # MySQL设定 # engine = create_engine('mysql://*****:*****@localhost:3306/netease', convert_unicode=True, encoding='utf-8', # connect_args={"charset": "utf8"}) # 数据读入 date = sys.argv[1] path_prefix = "./new_data" df = pd.read_pickle(os.path.join(path_prefix, "%s.p" % date)) df_content = pd.read_pickle(os.path.join(path_prefix, "%scontent.p" % date)) df_post = pd.read_pickle(os.path.join(path_prefix, "%spost.p" % date)) print(len(df)) df = pd.merge(df, df_content, on="tie_id") print(df.head()) df = df.dropna(axis=0) print(len(df)) # 模型加载 path_region_dict = "./Dict/region_dict/region_new.txt" r = Region(path_region_dict) # df = r.ip_detect(df.iloc[:10], on=["ip"]) df = r.region_detect(df, on=["content"]) df_select = df[df["region_1"] != 0] print(len(df_select)) print(df_select) df_select = r.ip_detect(df_select.iloc[:], on=["ip"], nbworker=8) print(df_select.head()) # 模型存储 df_select.to_pickle( os.path.join(path_prefix, "%s_select_comments.p" % date))
def test(): s = Sentiment() r = Region( "/Users/aaronhe/Documents/NutStore/Aaron He/FDU/Big-Data-Communication/Stereotype-Analysis-in-NetEase-News-Comments/Dict/region_dict/region.txt" ) # 构造输入数据 text = [ ["潮汕人很帅,湖北人挺会做生意的!", "上海"], ["老铁牛逼!", "重庆"], ["我觉得很好吃啊", "北京"], ] df = pd.DataFrame(text, columns=["text", "src"]) print(df.head()) df = r.region_detect(df, on=["text"]) # dataFrame中批量添加region字段 print( s.sentiment_detect(df, on=["text"], srcs=["src"], dists=["region_1", "region_2", "region_3"])) print(s.output_record(src="北京"))
def __init__(self, caller): caller.imageObservers.append(self.UpdateObserver) self.region = Region() self.renSpline = Spline.RenderSpline() self.InitCurves() self.caller = caller
def initialize(self): """ Initialize each small region of the picture and allocate each region a Region object. """ # for coarse regions : only work with patches larger # than 4 x 4 pixels # for notation simplicity iw = Parameters.imageWidth ih = Parameters.imageHeight br = Parameters.baseResolution for colStepSize in xrange(1, self.__coarseColStep + 1): for rowStepSize in xrange(1, self.__coarseRowStep + 1): # skip the 4 x 4 pixels patches if colStepSize == 1 and rowStepSize == 1: continue # find the range of a patch # l = left, r = right # d = down, u = up # 筛选出4*4区域,l从0-61。u从0-57。按4累加 for l in xrange(0, iw - colStepSize * br + 1, br): r = l + colStepSize * br for u in xrange(0, ih - rowStepSize * br + 1, br): d = u + rowStepSize * br num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) self.__coarseRegionId.append(num_id) if colStepSize == self.__coarseColStep \ and rowStepSize == self.__coarseRowStep: # this is the root region region.allocateSumNodes(1) self.__rootRegion = region self.__rootSumNode = region.getSumNode(0) else: region.allocateSumNodes(Parameters.numSumNodePerRegion) # for fine regions: work with 4 x 4 pixels patches for colStepSize in xrange(0, self.__coarseColStep): for rowStepSize in xrange(0, self.__coarseRowStep): for pixelColStep in xrange(1, br + 1): for pixelRowStep in xrange(1, br + 1): for l in xrange(colStepSize * br, (colStepSize + 1) * br - pixelColStep + 1): r = l + pixelColStep for u in xrange(rowStepSize * br, (rowStepSize + 1) * br - pixelRowStep + 1): d = u + pixelRowStep num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) if pixelRowStep == 1 and pixelColStep == 1: self.__pixelRegionId.append(num_id) else: self.__fineRegionId.append(num_id) if pixelColStep == 1 and pixelRowStep == 1: self.__initUnitRegion(region) else: region.allocateSumNodes(Parameters.numSumNodePerRegion)
def scenario3(): np.random.seed(7) # Simple SIR model. sampler = Sampler() n_days = 365 * 1 population_size = 500_000 I_initial = 500 copenhagen = Region('Copenhagen', population_size, sampler, I_initial, cyclical=2.5) country = Country([copenhagen]) np.random.seed(7) result = simulate(country, n_days=n_days) Plotter.plot_SIR(result) plt.legend() plt.xlabel('Days') plt.tight_layout() plt.savefig(os.path.join(plot_path, '3_SEIR_periodic.png'), dpi=300) plt.show() np.random.seed(7) result = repeat_simulate(country, n_repeats=n_repeats, n_days=n_days) Plotter.plot_intervals(result['I_crit'].copy(), plot_median=False) plt.plot(result['I_crit'][0], '--k', label='Example path', lw=0.5) plt.xlabel('Days') plt.ylabel('# Hospitalized') plt.hlines(copenhagen.population_size * 0.0005, *plt.xlim()) plt.legend() plt.tight_layout() plt.savefig(os.path.join(plot_path, '3_SEIR_periodic_hospitalized.png'), dpi=300) plt.show() sampler = Sampler() n_days = 365 * 4 population_size = 500_000 I_initial = 500 copenhagen = Region('Copenhagen', population_size, sampler, I_initial, cyclical=2.5) country = Country([copenhagen]) np.random.seed(7) result = repeat_simulate(country, n_repeats=n_repeats, n_days=n_days) Plotter.plot_intervals(result['I_crit'].copy(), plot_median=False) plt.plot(result['I_crit'][0], '--k', label='Example path', lw=0.5) plt.xlabel('Days') plt.ylabel('# Hospitalized') plt.hlines(copenhagen.population_size * 0.0005, *plt.xlim()) plt.legend() plt.tight_layout() plt.savefig(os.path.join(plot_path, '3_SEIR_periodic_hospitalized_long.png'), dpi=300) plt.show()
def initialize(self): """ Initialize each small region of the picture and allocate each region a Region object. """ # for coarse regions : only work with patches larger # than 4 x 4 pixels # for notation simplicity iw = Parameters.imageWidth ih = Parameters.imageHeight br = Parameters.baseResolution for colStepSize in xrange(1, self.__coarseColStep + 1): for rowStepSize in xrange(1, self.__coarseRowStep + 1): # skip the 4 x 4 pixels patches if colStepSize == 1 and rowStepSize == 1: continue # find the range of a patch # l = left, r = right # d = down, u = up for l in xrange(0, iw - colStepSize * br + 1, br): r = l + colStepSize * br for u in xrange(0, ih - rowStepSize * br + 1, br): d = u + rowStepSize * br num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) self.__coarseRegionId.append(num_id) if colStepSize == self.__coarseColStep \ and rowStepSize == self.__coarseRowStep: # this is the root region region.allocateSumNodes(1) self.__rootRegion = region self.__rootSumNode = region.getSumNode(0) else: region.allocateSumNodes(Parameters.numSumNodePerRegion) # for fine regions: work with 4 x 4 pixels patches for colStepSize in xrange(0, self.__coarseColStep): for rowStepSize in xrange(0, self.__coarseRowStep): for pixelColStep in xrange(1, br + 1): for pixelRowStep in xrange(1, br + 1): for l in xrange(colStepSize * br, (colStepSize + 1) * br - pixelColStep + 1): r = l + pixelColStep for u in xrange(rowStepSize * br, (rowStepSize + 1) * br - pixelRowStep + 1): d = u + pixelRowStep num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) if pixelRowStep == 1 and pixelColStep == 1: self.__pixelRegionId.append(num_id) else: self.__fineRegionId.append(num_id) if pixelColStep == 1 and pixelRowStep == 1: self.__initUnitRegion(region) else: region.allocateSumNodes(Parameters.numSumNodePerRegion)
def load_regions_from_json(self, file_path): json_string = "" with io.open(file_path, 'r') as file: for line in file.readlines(): json_string += line.split('#')[0].replace('\n', ' ') json_string = re.sub(' +', ' ', json_string) try: region_json = json.loads(json_string) except json.JSONDecodeError as error: raise Exception("JSON parse error around text:\n" + \ json_string[error.pos-35:error.pos+35] + "\n" + \ " ^^\n") for region in region_json: new_region = Region(region['region_name']) new_region.world = self if 'scene' in region: new_region.scene = region['scene'] if 'hint' in region: new_region.hint = region['hint'] if 'dungeon' in region: new_region.dungeon = region['dungeon'] if 'time_passes' in region: new_region.time_passes = region['time_passes'] new_region.provides_time = TimeOfDay.ALL if new_region.name == 'Ganons Castle Grounds': new_region.provides_time = TimeOfDay.DAMPE if 'locations' in region: for location, rule in region['locations'].items(): new_location = LocationFactory(location) new_location.parent_region = new_region new_location.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_location) new_location.world = self new_region.locations.append(new_location) if 'events' in region: for event, rule in region['events'].items(): # Allow duplicate placement of events lname = '%s from %s' % (event, new_region.name) new_location = Location(lname, type='Event', parent=new_region) new_location.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_location) new_location.world = self new_region.locations.append(new_location) MakeEventItem(event, new_location) if 'exits' in region: for exit, rule in region['exits'].items(): new_exit = Entrance('%s -> %s' % (new_region.name, exit), new_region) new_exit.connected_region = exit new_exit.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_exit) new_region.exits.append(new_exit) self.regions.append(new_region)
def initializeRegions(self): for region in entrancesDictionary: regionObj = Region(region, entrancesDictionary[region]) self.regions[region] = regionObj if regionObj.isDungeon: for dungeon in self.dungeonMQData: for search in regionObj.search: if search in dungeon: if self.dungeonMQData[dungeon] == 'mq': regionObj.isMQ = True break
def AddOneCurve(self): if len(self.spline.points) > 0: self.region.SetOnePoints(self.spline) self.InitCurves() Globals.renWin.Render() # commit the region and reinit it Globals.objectSet.AddRegion(self.region) self.region = Region() return 1 return 0
def mapper(documents): try: rl=RegionList() rl.populate(False,True,True) rl2=RegionList() rl2.populate(False,False,False) db=MongoConnection().get().dialect_db n=int(db.parameters.find_one({"name":"n"},{"_id":0,"value":1})["value"]) k=int(db.parameters.find_one({"name":"k"},{"_id":0,"value":1})["value"]) rpub_regions={} for region in rl.regions: children=[region] Region.getChildren(region,children,db) for child in children: #rpub_cur=db.region_pubs.find({"exclude":False},{"_id":1,"region":1}) rpub_cur=db.region_pubs.find({"region":child},{"_id":1,"region":1}) for rpub in rpub_cur: #if rl.regions.has_key(rpub["region"]): rpub_regions[rpub["_id"]]=rpub["region"] auth_regions={} auth_cur=db.authors.find({"count_classification":{"$exists":1}}) for auth in auth_cur: auth_regions[auth["_id"]]=auth["count_classification"] count=0 for doc in documents: if doc["exclude"]==False and (doc["region_pub"] ==None or rpub_regions.has_key(doc["region_pub"])): if doc["region_pub"]!=None: region=rpub_regions[doc["region_pub"]] else: region=auth_regions[doc["author"]] post=Post(doc["_id"],region, doc["clean_text"],0.75,True) post.set_kgroup(doc["k_group"]) print >> sys.stderr, str(datetime.now()) + doc["_id"].encode("utf-8") try: post.calc(db,rl,n) except: print >> sys.stderr, doc["_id"] raise print >> sys.stderr, str(datetime.now()) #if post.maxRegion != rl2.get(post.regionId).calcParent: # db.wrong.save({"_id": post.id, "actual_region": rl2.get(post.regionId).calcParent, "predicted_region": post.maxRegion}) db.results.save({"_id": post.id, "actual_region": rl2.get(post.regionId).calcParent, "predicted_region": post.sortedRegions}) yield {'_id': {'k_group': doc["k_group"], 'region': rl2.get(post.regionId).calcParent}, 'max_region':post.sortedRegions[len(post.sortedRegions)-1][0]} except: print >> sys.stderr, "Unexpected map error " traceback.print_exc() raise
def test_region(image): regions = Region.find_regions_by_border_color(image) Region.draw_fills(regions, image, (20, 144, 15)) Region.draw_outlines(regions, image, (144, 144, 255)) print "number of regions = %s" % len(regions) # convert image between color spaces # #cvCvtColor(source, destination, conversion_code); # cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE) cv2.imshow('a_window', image) cv.WaitKey(3000)
def __init__(self, bg, screen): self.player = 'b' self.outline = pygame.Rect(45, 45, 690, 690) self.background = bg self.screen = screen self.holding_stone = [False, ' '] self.region0 = Region(0, 1, 1, self.background) self.region1 = Region(1, 11, 1, self.background) self.region2 = Region(2, 1, 11, self.background) self.region3 = Region(3, 11, 11, self.background) self.regions = [self.region0, self.region1, self.region2, self.region3] self.board = [self.region0.stones, self.region1.stones, self.region2.stones, self.region3.stones] self.draw()
def load_regions_from_json(self, file_path): region_json = read_json(file_path) for region in region_json: new_region = Region(region['region_name']) new_region.world = self if 'scene' in region: new_region.scene = region['scene'] if 'hint' in region: new_region.hint = region['hint'] if 'dungeon' in region: new_region.dungeon = region['dungeon'] if 'time_passes' in region: new_region.time_passes = region['time_passes'] new_region.provides_time = TimeOfDay.ALL if new_region.name == 'Ganons Castle Grounds': new_region.provides_time = TimeOfDay.DAMPE if 'locations' in region: for location, rule in region['locations'].items(): new_location = LocationFactory(location) new_location.parent_region = new_region new_location.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_location) if new_location.never: # We still need to fill the location even if ALR is off. logging.getLogger('').debug('Unreachable location: %s', new_location.name) new_location.world = self new_region.locations.append(new_location) if 'events' in region: for event, rule in region['events'].items(): # Allow duplicate placement of events lname = '%s from %s' % (event, new_region.name) new_location = Location(lname, type='Event', parent=new_region) new_location.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_location) if new_location.never: logging.getLogger('').debug('Dropping unreachable event: %s', new_location.name) else: new_location.world = self new_region.locations.append(new_location) MakeEventItem(event, new_location) if 'exits' in region: for exit, rule in region['exits'].items(): new_exit = Entrance('%s -> %s' % (new_region.name, exit), new_region) new_exit.connected_region = exit new_exit.rule_string = rule if self.logic_rules != 'none': self.parser.parse_spot_rule(new_exit) if new_exit.never: logging.getLogger('').debug('Dropping unreachable exit: %s', new_exit.name) else: new_region.exits.append(new_exit) self.regions.append(new_region)
def MAPinference(self, index, instance): """ Bottom-up inference """ self.__setInput(instance) # for fine regions: work with 4 x 4 pixels patches print '......Inference on fine regions' for region_id in self.__fineRegionId: region = Region.getRegion(region_id) region.MAPinference(index, instance) print '......Inference on coarse regions' for region_id in self.__coarseRegionId: region = Region.getRegion(region_id) region.MAPinference(index, instance)
def build_tree(root, input_features, with_pruning=False, alpha=0.0): leaves = [] leaves.append(root) while len(leaves) < 15: current_leaves = leaves[:] # current_leaves = leaves does not work because it copies reference and changes made to leaves are shown in current_leaves as well. temp_rss_reduction = 0 split_region = None # print('Current number of leaves: ' + str(len(current_leaves))) for region in current_leaves: # if there is no data, then there is no need for split if len(region.data) <= 1: continue split_point_feature, split_details = find_split_point( region.data, input_features, with_pruning, alpha, len(leaves)) split_rss = split_details[0] region.split_feature_id = split_point_feature region.split_point = split_details[1] region.left_region = Region(data=split_details[2], rss=split_details[3]) region.right_region = Region(data=split_details[4], rss=split_details[5]) if region.rss > split_rss: if temp_rss_reduction <= region.rss - split_rss: temp_rss_reduction = region.rss - split_rss split_region = region if split_region is not None: split_region.data = None leaves.remove(split_region) leaves.append(split_region.left_region) leaves.append(split_region.right_region) else: # There is nothing else to split. So, len(leaves) <= 25 always evaluates to true and becomes an infinite loop break for leaf in leaves: leaf.isLeaf = True y_values = [row[0] for row in leaf.data] leaf.data = sum(y_values) / len(y_values) if len(y_values) > 0 else 0 # print(leaf.data) print('Number of regions ' + ('with pruning: ' if with_pruning else 'without pruning: ') + str(len(leaves)))
def process_messages(): client = KafkaClient( hosts= f"{app_config['kafkastore']['host']}:{app_config['kafkastore']['port']}" ) topic = client.topics[f"{app_config['topic']}"] consumer = topic.get_simple_consumer(consumer_group="events", auto_commit_enable=True, auto_commit_interval_ms=1000) for message in consumer: message_string = message.value.decode('utf-8') message = json.loads(message_string) logger.info(f'New Message: {message_string}') if message['type'] == 'region': session = DB_SESSION() region = Region(message['payload']['id'], message['payload']['name']) session.add(region) session.commit() session.close() if message['type'] == 'roast': session = DB_SESSION() roast = Roast(message['payload']['id'], message['payload']['name'], message['payload']['region']) session.add(roast) session.commit() session.close()
def opprettRegion(data): navn = "" while True: print("Skriv inn region navnet.") navn = input().lower() if navn != "": break else: print("Navnet kan ikke være tomt, prov igjen.") kode = "" while True: print("Skriv kode for regionen") kode = input() if kode != "": break else: print("Region koden kan ikke være tomt, prov igjen.") leggTilLand = [] while True: print( "Skriv landene som skal legges til regionen. Separer med komma.(i.e Norway,Sweden)" ) leggTilLand = input().split(",") if len(leggTilLand) != 0: break else: print("Skriv minst et land.") flereLand = [] for i in range(1, len(data)): if data[i].hentNavn() in leggTilLand: flereLand.append(data[i]) return Region(navn, kode, flereLand)
def op_regions(asset, region, output='json', date=None, srs=None, srs_type='unknown', resample=None, region_srs=None, region_srs_type='unknown', reducers=None, region_type='json', **kwargs): outputDriver = Outputs.getDriver(output) ## Parse the region R = Region(region) if region_srs: region_SR = SpatialReference.parse(region_srs, region_srs_type) R.geom.AssignSpatialReference(region_SR) DS = Asset(asset) if srs: SR = SpatialReference.parse(srs, srs_type) DS = DS.warpTo(SR, resample) data, weights = DS.getRegion(R, date, resample) if reducers: data = Reducers.apply(reducers, data, weights) data = data.tolist() return outputDriver(data, ds=DS)
async def on_queue_flush(self, ctx, region: Region = Region(Region.ALL)): self.model.queues.clear(region.region) embed = discord.Embed(colour=discord.Colour.blue(), description=self.model.QueueStatus()) await ctx.channel.send( content=CodeSB(f'Queues cleared on: {", ".join(region.ToList())}'), embed=embed)
def mser_extract_regions(cv_image, lower_color_bound, upper_color_bound) -> [[Region, int]]: image = cv_image.copy() lower_bound = np.array(lower_color_bound) upper_bound = np.array(upper_color_bound) mask = cv2.inRange(image, lower_bound, upper_bound) mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) masked_image = image & mask_rgb mser = cv2.MSER_create(_min_area=250, _max_area=50000, _max_evolution=50000) regions, _ = mser.detectRegions(masked_image) detected_regions = [] for p in regions: xmax, ymax = np.amax(p, axis=0) xmin, ymin = np.amin(p, axis=0) detected_regions.append([Region(xmax, xmin, ymax, ymin), 0]) return detected_regions
def test_blob(image): regions = Region.find_regions_by_border_color(image) for i, region in enumerate(regions): region_mask = region.mask_image(image) blobs = region.find_blobs(image) Blob.draw_outlines(blobs, region_mask, (234, 34, 102)) utils.show_image(region_mask, time=1000)
async def on_set_stats(self, ctx, member: discord.Member, region: Region, race: Race, wins: int, loses: int, ties: int): playerName = member.name regions = region.ToList() races = race.ToList() for reg in regions: usPlayer = self.model.playerDB.Find(member.id, reg) if usPlayer == None: await ctx.send(f'Player {playerName} not registered') return if not usPlayer.lastPlayed: usPlayer.SetPlayed() for r in races: usPlayer.wins[r] = wins usPlayer.loses[r] = loses usPlayer.ties[r] = ties self.model.playerDB.UpdateStats(usPlayer) await ctx.send( CodeSB( f"Updated {playerName}'s wins={usPlayer.wins[races[0]]}, loses={usPlayer.loses[races[0]]}, ties={usPlayer.ties[races[0]]} for {', '.join(races)} on {', '.join(regions)}" ))
async def on_unregister(self, ctx, member: discord.Member, region: Region = Region(Region.ALL)): self.model.playerDB.UnRegister(member.id, region.region) await ctx.send( f'{member.name} successfully unregistered on {region.region}')
async def ValidateReg(self, ctx, region): if not Region.Valid(region): await ctx.send( CodeSB( f'Invalid region, expected: {"/".join(Region.VALID_REGIONS)}' )) return False return True
def _delimit_regions(self, text): regions_regs = { # 'START': r'(\w|-)+.*DEFINITIONS\s*::=\sBEGIN', 'START': r'(\w|-)+(.|\n)*DEFINITIONS\s*::=\s+BEGIN', "EXPORTS": r'EXPORTS(.|\n)*?;', "IMPORTS": r'IMPORTS(.|\n)*?;', "DEFS": '(.|\n)*(=?END)' } text = "".join(re.split(r'--.*(\r|\n)', text)) i = 0 for regionName, reg in regions_regs.items(): m = Region(text, reg, i) setattr(self, regionName, m) if m: i = m.end()
async def on_del_subp(self, ctx, member: discord.Member, region: Region = Region(Region.ALL)): if not self.model.ChkIsReg(ctx): return await self.model.RemPlayerSub(ctx, member.name, member.id, region)
def initialize_simulation(): covid19 = Disease() cities = [] city1 = City(disease=covid19) city2 = City(disease=covid19, population=100000, area=5, hospital_beds=200, num_infected=2) city3 = City(disease=covid19, population=50000, area=10, hospital_beds=100, num_infected=20) city4 = City(disease=covid19, population=200000, area=8, hospital_beds=400, num_infected=3) city5 = City(disease=covid19, population=20000, area=3, hospital_beds=40, num_infected=10) city6 = City(disease=covid19, population=30000, area=6, hospital_beds=60, num_infected=15) city7 = City(disease=covid19, population=125000, area=5, hospital_beds=250, num_infected=1) # city1 = City(disease = covid19, population = 200, area= .005, # hospital_beds = 5, num_infected = 3) # city2 = City(disease = covid19, population = 100, area = .001, # hospital_beds = 2, num_infected = 2) # city3 = City(disease = covid19, population = 500, area = .01, # hospital_beds = 2, num_infected = 2) # city4 = City(disease = covid19, population = 75, area = .001, # hospital_beds = 2, num_infected = 2) # city5 = City(disease = covid19, population = 50, area = .001, # hospital_beds = 2, num_infected = 2) cities.append(city1) cities.append(city2) cities.append(city3) # cities.append(city4) # cities.append(city5) # cities.append(city6) # cities.append(city7) region = Region(cities) return region
async def on_addp(self, ctx, member: discord.Member, region: Region = Region(Region.ALL)): if not self.model.ChkIsRegId(member.id): for reg in Region.REGIONS: self.model.playerDB.Register(Player(member.id, reg)) await self.model.AddPlayerQueue(ctx, member.name, member.id, region)
def __setInput(self, instance, blankPart = 'NONE'): for l in xrange(0, Parameters.imageWidth): r = l + 1 for u in xrange(0, Parameters.imageHeight): d = u + 1 num_id = Region.getRegionId(u, d, l, r) region = Region.getRegion(num_id) if blankPart == 'NONE': region.setBaseValues(instance.getValue(u, l)) elif blankPart == 'LEFT': if l < Parameters.imageWidth / 2: region.setBaseValuesForBlank() else: region.setBaseValues(instance.getValue(u, l)) elif blankPart == 'BOTTOM': if u < Parameters.imageHeight / 2: region.setBaseValues(instance.getValue(u, l)) else: region.setBaseValuesForBlank()
async def RemPlayerSub(self, ctx, playerName, playerId, region: Region): regions = region.ToList() for reg in regions: self.subs[reg].discard(playerId) await ctx.send( CodeSB( f'{playerName} no longer avaiable to sub on {", ".join(regions)}' ))
def clearUnusedDecomp(self): """ Top-down scan each region and clear all the unused decomposition in each region. """ # clear coarse region for region_id in self.__coarseRegionId: region = Region.getRegion(region_id) # first of all, gather all the decomposition in use # (the decomposition is child of one of the SumNode) alive = [] for node in region.sumNodes: if node.getNumOfChildren() > 0: alive.extend(node.getChildren().keys()) all_decomps = region.prodNodes.keys() # find dead decomp using set compliment operation dead = set(all_decomps) - set(alive) # clear these dead decompositions for ddp in dead: # remove in local region del region.prodNodes[ddp] # remove in global decomposition dict Decomposition.deleteDecomp(ddp) #clear fine region for region_id in self.__fineRegionId: region = Region.getRegion(region_id) # first of all, gather all the decomposition in use # (the decomposition is child of one of the SumNode) alive = [] for node in region.sumNodes: if node.getNumOfChildren() > 0: alive.extend(node.getChildren().keys()) all_decomps = region.prodNodes.keys() # find dead decomp using set compliment operation dead = set(all_decomps) - set(alive) # clear these dead decompositions for ddp in dead: # remove in local region del region.prodNodes[ddp] # remove in global decomposition dict Decomposition.deleteDecomp(ddp)
def __differentiateSPN(self): # all the nodes in the SPN should have initialized # the value of their logDerivatives by their super # class's contructor, or we should call # self.__initDerivative() self.__rootSumNode.setLogDerivative(0.0) self.__rootSumNode.passDerivative() for decomp_id in self.__rootSumNode.getChildren(): prodNode = self.__rootSumNode.getChild(decomp_id) prodNode.passDerivative() for region_id in reversed(self.__coarseRegionId[:-1]): region = Region.getRegion(region_id) region.passDerivative() for region_id in reversed(self.__fineRegionId): region = Region.getRegion(region_id) region.passDerivative()
def clearParseToMAPFromBuffer(self): while Utility.parseBuffer: regionRightMax = Utility.parseBuffer.pop() regionLeftMax = Utility.parseBuffer.pop() regionRightId = Utility.parseBuffer.pop() regionLeftId = Utility.parseBuffer.pop() mapSumNodeIndex = Utility.parseBuffer.pop() region_id = Utility.parseBuffer.pop() region = Region.getRegion(region_id) region.clearParseToMAPFromBuffer(mapSumNodeIndex, \ regionLeftId, \ regionRightId, \ regionLeftMax, \ regionRightMax)
from Region import Region from RegionList import RegionList from Post import Post from MongoConnection import MongoConnection rl=RegionList() rl.populate(False,True,True) rl2=RegionList() rl2.populate(False,False,False) db=MongoConnection().get().dialect_db n=int(db.parameters.find_one({"name":"n"},{"_id":0,"value":1})["value"]) k=int(db.parameters.find_one({"name":"k"},{"_id":0,"value":1})["value"]) rpub_regions={} for region in rl.regions: children=[region] Region.getChildren(region,children,db) for child in children: print >> sys.stderr, child #rpub_cur=db.region_pubs.find({"exclude":False},{"_id":1,"region":1}) rpub_cur=db.region_pubs.find({"region":child},{"_id":1,"region":1}) for rpub in rpub_cur: #if rl.regions.has_key(rpub["region"]): rpub_regions[rpub["_id"]]=rpub["region"] doc=db.posts.find_one({"_id":sys.argv[1]}) if doc.has_key("region_pub") and doc["region_pub"] != None: print(doc["region_pub"]) region=rpub_regions[doc["region_pub"]] else: region=db.authors.find_one({"_id":doc["author"]})["count_classification"] post=Post(doc["_id"],region, doc["clean_text"],0.75,True) post.set_kgroup(doc["k_group"])
def set_Regions_Info(self): for item in self.regionLoad_Total_Info_List: region = Region(item) region.set_Region_Info() self.regions_List.append(region)
def printLearnedModel(self): for region_id in self.__pixelRegionId: region = Region.getRegion(region_id) for sumNode in region.sumNodes: print sumNode.getLogValue() print region.prodNodes
class ForceAtlas2: def __init__(self,graph): self.graph = graph self.nodesIndex = {} self.p={ "linLogMode": False, "outboundAttractionDistribution": False, "adjustSizes": False, "edgeWeightInfluence": 0, "scalingRatio": 1, "strongGravityMode": False, "gravity": 1, "jitterTolerance": 1, "barnesHutOptimize": False, "barnesHutTheta": 1.2, "speed": 1, "outboundAttCompensation": 1, "totalSwinging": 0, "swingVSnode1": 0, "totalEffectiveTraction": 0, "complexIntervals": 500, "simpleIntervals": 1000 } self.state = {"step": 0, "index": 0} self.rootRegion = 0 def init(self): self.setAutoSettings() nds = self.graph["nodes"] for i in range(len(nds)): if not nds[i].has_key("fixed"): nds[i]['fixed'] = False if not nds[i].has_key("x"): nds[i]['x'] = random.uniform(0,1) if not nds[i].has_key("y"): nds[i]['y'] = random.uniform(0,1) self.nodesIndex[nds[i]['id']] = i nds[i]["fa2"] = { "mass": 1+nds[i]["degree"], "old_dx": 0, "old_dy": 0, "dx": 0, "dy": 0 } return self def go(self): while (self.atomicGo()): print "something" def printNodesFA2(self): for i in self.graph["nodes"]: print i def setAutoSettings(self): # Tuning lenNodes = len(self.graph["nodes"]) if (lenNodes >= 100): self.p["scalingRatio"] = 2.0 else: self.p["scalingRatio"] = 10.0 self.p["strongGravityMode"] = False self.p["gravity"] = 1 # Behavior self.p["outboundAttractionDistribution"] = False self.p["linLogMode"] = False self.p["adjustSizes"] = False self.p["edgeWeightInfluence"] = 1 # Performance if (lenNodes >= 50000): self.p["jitterTolerance"] = 10 elif (lenNodes >= 5000): self.p["jitterTolerance"] = 1 else: self.p["jitterTolerance"] = 0.1 if (lenNodes >= 1000): self.p["barnesHutOptimize"] = True else: self.p["barnesHutOptimize"] = False self.p["barnesHutTheta"] = 1.2 def atomicGo(self): graph = self.graph nodes = graph["nodes"] edges = graph["edges"] cInt = self.p["complexIntervals"] sInt = self.p["simpleIntervals"] case = self.state["step"] if case==0: # Initialise layout data print "the case 0" for i in range(len(nodes)): if nodes[i]["fa2"]: nodes[i]["fa2"]["mass"] = 1 + nodes[i]["degree"] nodes[i]["fa2"]["old_dx"] = nodes[i]["fa2"]["dx"] nodes[i]["fa2"]["old_dy"] = nodes[i]["fa2"]["dy"] nodes[i]["fa2"]["dx"] = 0 nodes[i]["fa2"]["dy"] = 0 else: nodes[i]["fa2"] = { "mass": 1 + nodes[i]["degree"], "old_dx": 0, "old_dy": 0, "dx": 0, "dy": 0 } # If Barnes Hut active, initialize root region if self.p["barnesHutOptimize"]: print '\tcase 0 -> ["barnesHutOptimize"]' self.rootRegion = Region(nodes, 0) self.rootRegion.buildSubRegions() # If outboundAttractionDistribution active, compensate. if self.p["outboundAttractionDistribution"]: print '\tcase 0 -> ["outboundAttractionDistribution"]' self.p["outboundAttCompensation"] = 0 for i in range(len(nodes)): self.p["outboundAttCompensation"] += nodes[i]["fa2"]["mass"] self.p["outboundAttCompensation"] /= len(nodes) self.state["step"] = 1 self.state["index"] = 0 return True elif case==1: # Repulsion print "the case 1: Repulsion" Repulsion = ForceFactory.buildRepulsion(self.p["adjustSizes"],self.p["scalingRatio"]) print "\tprinting what it is inside of Repulsion variable" print Repulsion if self.p["barnesHutOptimize"]: rootRegion = self.rootRegion barnesHutTheta = self.p["barnesHutTheta"] i = self.state["index"] while (i < len(nodes) and i < self.state["index"] + cInt): n = nodes[i] i+=1 if n["fa2"]: rootRegion.applyForce(n, Repulsion, barnesHutTheta) if (i == len(nodes)): self.state["step"] = 2 self.state["index"] = 0 else: self.state["index"] = i else: i1 = self.state["index"] while (i1 < len(nodes) and i1 < self.state["index"] + cInt): n1 = nodes[i1] i1+=1 if n1["fa2"]: for i2 in range(len(nodes)): if i2 < i1 and nodes[i2]["fa2"]: Repulsion.apply_nn(n1, nodes[i2]) if (i1 == len(nodes)): self.state["step"] = 2 self.state["index"] = 0 else: self.state["index"] = i1 return True elif case==2: # Gravity print "the case 2: Gravity" Gravity="" if self.p["strongGravityMode"]: Gravity = ForceFactory.getStrongGravity(self.p["scalingRatio"]) else: Gravity = ForceFactory.buildRepulsion(self.p["adjustSizes"],self.p["scalingRatio"]) gravity = self.p["gravity"] scalingRatio = self.p["scalingRatio"] i = self.state["index"] while (i < len(nodes) and i < self.state["index"] + sInt): n = nodes[i] i+=1 if n["fa2"]: Gravity.apply_g(n, gravity / scalingRatio) if (i == len(nodes)): self.state["step"] = 3 self.state["index"] = 0 else: self.state["index"] = i return True elif case==3: # Attraction print "the case 3: Attraction" field="" if self.p["outboundAttractionDistribution"]: field = self.p["outboundAttCompensation"] else: field = 1 Attraction = ForceFactory.buildAttraction(self.p["linLogMode"],self.p["outboundAttractionDistribution"],self.p["adjustSizes"],1*field) i = self.state["index"] if self.p["edgeWeightInfluence"] == 0: while (i < len(edges) and i < self.state["index"] + cInt): e = edges[i] i+=1 s = nodes[self.nodesIndex[e["source"]]] t = nodes[self.nodesIndex[e["target"]]] Attraction.apply_nn(s, t, 1) elif self.p["edgeWeightInfluence"] == 1: while (i < len(edges) and i < self.state["index"] + cInt): e = edges[i] i+=1 s = nodes[self.nodesIndex[e["source"]]] t = nodes[self.nodesIndex[e["target"]]] Attraction.apply_nn(s, t, (e["weight"] or 1)) else: while (i < len(edges) and i < self.state["index"] + cInt): e = edges[i] i+=1 t = nodes[self.nodesIndex[e["target"]]] Attraction.apply_nn(t,(math.pow(e["weight"] or 1), self.p["edgeWeightInfluence"])) if (i == len(edges)): self.state["step"] = 4 self.state["index"] = 0 else: self.state["index"] = i return True elif case==4: # Auto adjust speed print "the case 4: Auto-adjust speed" totalSwinging=0# How much irregular movement totalEffectiveTraction = 0 # Hom much useful movement swingingSum=0 promdxdy=0 for i in range(len(nodes)): n = nodes[i] fixed = n["fixed"] or False if not fixed and n["fa2"]: swinging = math.sqrt(math.pow(n["fa2"]["old_dx"] - n["fa2"]["dx"], 2)+math.pow(n["fa2"]["old_dy"] - n["fa2"]["dy"], 2)) totalSwinging+=n["fa2"]["mass"]*swinging totalEffectiveTraction +=n["fa2"]["mass"]*0.5*math.sqrt(math.pow(n["fa2"]["old_dx"] + n["fa2"]["dx"], 2)+math.pow(n["fa2"]["old_dy"] + n["fa2"]["dy"], 2)) self.p["totalSwinging"] = totalSwinging self.p["totalEffectiveTraction"] = totalEffectiveTraction #We want that swingingMovement < tolerance * convergenceMovement targetSpeed = math.pow(self.p["jitterTolerance"], 2)*self.p["totalEffectiveTraction"]/self.p["totalSwinging"] #But the speed shoudn't rise too much too quickly, #since it would make the convergence drop dramatically. maxRise = 0.5 self.p["speed"] = self.p["speed"]+min(targetSpeed-self.p["speed"],maxRise * self.p["speed"]) #Save old coordinates for i in range(len(nodes)): nodes[i]["old_x"] = +nodes[i]["x"] nodes[i]["old_y"] = +nodes[i]["y"] self.state["step"] = 5 return True elif case==5: # Apply forces print "the case 5: Apply forces" i = self.state["index"] if self.p["adjustSizes"]: speed = self.p["speed"] while (i < len(nodes) and i < self.state["index"] + sInt): n = nodes[i] i+=1 fixed = n["fixed"] or False if not fixed and n["fa2"]: swinging = math.sqrt((n["fa2"]["old_dx"] - n["fa2"]["dx"])*(n["fa2"]["old_dx"] - n["fa2"]["dx"]) +(n["fa2"]["old_dy"] - n["fa2"]["dy"])*(n["fa2"]["old_dy"] - n["fa2"]["dy"])) factor = 0.1 * speed / (1 + speed * math.sqrt(swinging)) df = math.sqrt(math.pow(n["fa2"]["dx"], 2)+math.pow(n["fa2"]["dy"], 2)) factor = min(factor * df, 10) / df n["x"] += n["fa2"]["dx"] * factor n["y"] += n["fa2"]["dy"] * factor else: speed = self.p["speed"] while (i < len(nodes) and i < self.state["index"] + sInt): n = nodes[i] i+=1 fixed = n["fixed"] or False if not fixed and n["fa2"]: #Adaptive auto-speed: the speed of each node is lowered #when the node swings. swinging = math.sqrt((n["fa2"]["old_dx"] - n["fa2"]["dx"])*(n["fa2"]["old_dx"] - n["fa2"]["dx"]) +(n["fa2"]["old_dy"] - n["fa2"]["dy"])*(n["fa2"]["old_dy"] - n["fa2"]["dy"])) factor = speed / (1 + speed * math.sqrt(swinging)) n["x"] += n["fa2"]["dx"] * factor n["y"] += n["fa2"]["dy"] * factor if (i == len(edges)): self.state["step"] = 0 self.state["index"] = 0 return False else: self.state["index"] = i return True else: # Do the default print "Error" return False def getGraph(self): nds = self.graph['nodes'] justNodes = [] for i in range(len(nds)): n = { "id":i, "sID":nds[i]["id"], "x":nds[i]["x"], "y":nds[i]["y"], "occ":nds[i]["degree"] } justNodes.append(n) return justNodes
import sys import pymongo import math from Region import Region from pymongo import MongoClient from MongoConnection import MongoConnection print("start") db=MongoConnection().get().dialect_db region_cur=db.regions.find()#{"$or":[{"_id":"ABN"},{"_id":"NI"},{"_id":"MANC"},{"_id":"BRIS"}]}) regions=[region for region in region_cur]#region_cur[:] for region in regions: try: ngram_counts=region["word_counts"] except KeyError: ngram_counts=None print(region["_id"]) r=Region(region["_id"],region["name"],ngram_counts,-1) r.calcLikelihoods(10, 1, 0.75,True) print("done")
def start(): # init camera cam = cv2.VideoCapture(1) # init osc communication with chuck initOSCClient() cam_thread = CameraOutThread(cam) cam_thread.start() time.sleep(0.5) raw_input("Press Enter when camera is positioned") # display numbers on regions image = cam.read() regions = Region.find_regions_by_border_color(image[1]) for region in regions: cam_thread.add_text(TextOutput(str(region.id), (region.centroid_x, region.centroid_y))) # ask for Gen type for each region dac = DAC() gen_maps = {} for i, region in enumerate(regions): while (True): try: gen_type = raw_input("Please enter UGen type for region %s: " % region.id) gen = eval(gen_type)() attr_range = [0.0, 1.0] if isinstance(gen, Osc): attr_range[0] = float(raw_input(" What is the low frequency on this oscillator? ")) attr_range[1] = float(raw_input(" What is the high frequency on this oscillator? ")) gen_maps[region.id] = GenMap(gen, region, attr_range=attr_range) break except: print 'Invalid UGen, try again.' # ask for gen links # dac special to_where while (raw_input("Add a patch? (y/n): ") == 'y'): from_where = None while (True): try: # NOTE: no range checking from_where = int(raw_input('from Region #: ')) from_where = gen_maps[from_where] break except ValueError: print 'Please enter an integer.' to_where = None while (True): try: to_where = raw_input('to Region #: ') if to_where == 'dac': to_where = dac break to_where = int(to_where) to_where = gen_maps[to_where] break except ValueError: print 'Please enter an integer.' # will handle dac later # for now, all patch paths assumed end in dac if not isinstance(to_where, DAC): (from_where.gen).add_patch_to(to_where.gen) Patch.find_patches([gen_maps[gm_id].gen for gm_id in gen_maps]) Patch.osc_send_all_patches() cam_thread.text_outputs = [] cam_thread.gen_maps = gen_maps
import pymongo from MongoConnection import MongoConnection from Region import Region db=MongoConnection().get().dialect_db db.regions.update({},{"$set":{"exclude":True,"calc_level":False}},multi=True) db.region_pubs.update({},{"$set":{"exclude":True}},multi=True) db.regions.update({"_id": {"$in": ["IRE","SCO","WAL","N_ENG","SO_ENG"]}}, {"$set": {"exclude":False,"calc_level":True}},multi=True) regions=db.regions.find({"exclude":False}) for region in regions: children=[region["_id"]] Region.getChildren(region["_id"],children, db) for c in children: res=db.region_pubs.update({"region":c},{"$set":{"exclude":False}})
import pymongo from pymongo import MongoClient from Region import Region from RegionList import RegionList import RegionNgramCache from RegionNgramCache import RegionNgramCache from MongoConnection import MongoConnection db=MongoConnection().get().dialect_db region_cur=db.regions.find() regions=[region for region in region_cur] region_cur.close() cache=RegionNgramCache() i=0 for region in regions: try: ngram_counts=region["word_counts"] except KeyError: ngram_counts=None r=Region(region["_id"],region["name"],ngram_counts,0,i,True) if ngram_counts!=None: r.populateNgrams() i+=1 for n in r.ngrams: cache.set_value(r, RegionNgramCache.NGRAM, r.ngrams[n].id, r.ngrams[n]) for s in r.startsWith: cache.set_value(r, RegionNgramCache.STARTS_WITH, s, r.startsWith[s])