def __get_cds_rel_pos(self, cdss, cds_id, aln_loc): '''Returns position of CDS relative to alignment. @param [Cds] List of CDSs @param (Location) cds_id CDS we are looking at @param (Location) aln_loc Alignment Location @return (string) LEFT_OF_ALN - fully left of alignment RIGHT_OF_ALN - fully right or not first which overlaps FIRST - first to overlap ''' cds_loc = Location.from_location_str(cdss[cds_id].location) if (cds_loc.end < aln_loc.start): return "LEFT_OF_ALN" if (cds_loc.start > aln_loc.end): return "RIGHT_OF_ALN" # Overlap occured - check if it is first # If it is first CDS or the previous one does not overlap if (cds_id == 0): return "FIRST" cds_prev_loc = Location.from_location_str(cdss[cds_id - 1].location) if (not self.__overlap(cds_prev_loc, aln_loc)): return "FIRST" return "RIGHT_OF_ALN"
def __get_cds_rel_pos (self, cdss, cds_id, aln_loc): '''Returns position of CDS relative to alignment. @param [Cds] List of CDSs @param (Location) cds_id CDS we are looking at @param (Location) aln_loc Alignment Location @return (string) LEFT_OF_ALN - fully left of alignment RIGHT_OF_ALN - fully right or not first which overlaps FIRST - first to overlap ''' cds_loc = Location.from_location_str(cdss[cds_id].location) if (cds_loc.end < aln_loc.start): return "LEFT_OF_ALN" if (cds_loc.start > aln_loc.end): return "RIGHT_OF_ALN" # Overlap occured - check if it is first # If it is first CDS or the previous one does not overlap if (cds_id == 0): return "FIRST" cds_prev_loc = Location.from_location_str(cdss[cds_id - 1].location) if (not self.__overlap(cds_prev_loc, aln_loc)): return "FIRST" return "RIGHT_OF_ALN"
def testInstersectionWithoutComplementInformation(self): l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('complement(5..15)') self.assertTrue(l1.intersects(l2, use_complement=False), 'The locations should intersect') self.assertFalse(l1.intersects(l2, use_complement=True), "The locations should't intersect")
def find_slots_by_district(): state, district, day, time = args if len(args) == 4 else ("", "", "", "") location = Location(state, district) state_id = location.get_state_id() district_id = location.get_district_id(state_id) date = utils.get_date(day) iterations = utils.get_iterations(time) appointment.find_slots(date, "district", district_id, iterations)
def testSingleLocation(self): location = Location.from_location_str('join(311,400..854)') self.assertTrue(location.intersects(Location.from_location((311, ))), "Location doesn't contain target point") location = Location.from_location_str('join(14424..14857,1)') self.assertTrue(location.intersects(Location.from_location((1, ))), "Location doesn't contain target point")
def __init__(self, message_pump): self.curlevel = 0 self.message_pump = message_pump self.message_pump.register(self) self.location = Location(50, 440) self.size = Location(40, 40) self.countdown = 10 self.cur_offset = 0 self.reversing = False self.COOLDOWN = 5 self.sprites = [] self.sprites.append(pygame.sprite.Sprite()) self.sprites[0].image = pygame.image.load( "lewis_character_5.png").convert_alpha() self.sprites[0].rect = self.sprites[0].image.get_rect() self.sprites.append(pygame.sprite.Sprite()) self.sprites[1].image = pygame.image.load( "lewis_character_5_left.png").convert_alpha() self.sprites[1].rect = self.sprites[1].image.get_rect() self.sprites.append(pygame.sprite.Sprite()) self.sprites[2].image = pygame.image.load( "lewis_character_5.png").convert_alpha() self.sprites[2].image = pygame.transform.scale(self.sprites[0].image, (125, 50)) self.sprites[2].rect = self.sprites[2].image.get_rect() self.sprites.append(pygame.sprite.Sprite()) self.sprites[3].image = pygame.image.load( "lewis_character_5.png").convert_alpha() self.sprites[3].image = pygame.transform.scale(self.sprites[1].image, (125, 50)) self.sprites[3].rect = self.sprites[3].image.get_rect() self.levels = [] level0 = [] self.levels.append(level0) level1 = [] self.levels.append(level1) level2 = [] bed_rect = Rect(185, 170, 195, 100) cushion_rect = Rect(0, 0, 110, 250) cushion_rect_2 = Rect(0, 0, 90, 280) stove_rect = Rect(0, 0, 66, 340) level2.append(bed_rect) level2.append(cushion_rect) level2.append(cushion_rect_2) level2.append(stove_rect) self.levels.append(level2) self.facing = "right"
def testSingleLocationWithTolerance(self): location = Location.from_location_str( 'join(<10,61..86,162..203,264..318,388..>495)', tolerance=10) self.assertTrue(location.intersects(Location.from_location((5, ))), "Location doesn't contain target point") location = Location.from_location_str('join(<1..129,>657)', tolerance=10) self.assertTrue(location.intersects(Location.from_location((660, ))), "Location doesn't contain target point")
def __init__(self, _x, _y, level): self.highlighted = False self.level_name = level self.location = Location(_x, _y) self.size = Location(80, 80) self.tokensprite = pygame.sprite.Sprite() self.tokensprite.image = pygame.image.load( "highlight_token.png").convert_alpha() self.tokensprite.image = pygame.transform.scale( self.tokensprite.image, (20, 20)) self.tokensprite.rect = self.tokensprite.image.get_rect()
def __init__(self): self.location = Location(100, 100) self.size = Location(100, 50) self.countdown = 5 self.cur_offset = 0 self.reversing = False self.sprites = [] for i in range(0, 6): self.sprites.append(pygame.sprite.Sprite()) self.sprites[i].image = pygame.image.load( "lewis_character_5.png").convert_alpha() self.sprites[i].rect = self.sprites[i].image.get_rect()
def testFastMinimum(self): l1 = Location.fast_min_str('join(1..10,11..50)') l2 = Location.fast_min_str('complement(15..20)') l3 = Location.fast_min_str('REF2:5..10') l4 = Location.fast_min_str('complement(join(1..10,11..50))') l5 = Location.fast_min_str('complement(join(15..20,1..2))') self.assertEqual(l1, 1, 'Minimum should be 1') self.assertEqual(l2, 15, 'Minimum should be 15') self.assertEqual(l3, 5, 'Minimum should be 5') self.assertEqual(l4, 1, 'Minimum should be 1') self.assertEqual(l5, 1, 'Minimum should be 1')
def testLocationMinimum(self): l1 = Location.from_location_str('join(1..10,11..50)') l2 = Location.from_location_str('complement(15..20)') l3 = Location.from_location_str('REF2:5..10') l4 = Location.from_location_str('complement(join(1..10,11..50))') l5 = Location.from_location_str('complement(join(15..20,1..2))') self.assertEqual(l1.min(), 1, 'Minimum should be 1') self.assertEqual(l2.min(), 15, 'Minimum should be 15') self.assertEqual(l3.min(), 5, 'Minimum should be 5') self.assertEqual(l4.min(), 1, 'Minimum should be 1') self.assertEqual(l5.min(), 1, 'Minimum should be 1')
def __init__(self, message_pump): self.message_pump = message_pump self.message_pump.register(self) self.visible = False self.location = Location(10,150) # self.reset() #Randomly pick a starting location self.return_level = "1" # self.size = Location(40,40) self.sprite = pygame.sprite.Sprite() self.sprite.image = pygame.image.load("yurt.png").convert_alpha() self.sprite.image= pygame.transform.scale(self.sprite.image,(300, 250)) # self.sprite.image = self.sprite.image.convert_alpha() self.sprite.rect = self.sprite.image.get_rect()
def testLocationOverlap(self): l1 = Location.from_location_str('join(1..10,11..50)') l2 = Location.from_location_str('complement(join(1..2,15..20))') self.assertEqual(1, l1.start, "Start should be 1") self.assertEqual(50, l1.end, "End should be 50") self.assertEqual(1, l2.start, "Start should be 1") self.assertEqual(20, l2.end, "End should be 20") self.assertTrue(l1.overlaps(l2, use_complement=False), 'Locations should overlap') self.assertTrue(l1.overlaps(l2, use_complement=True), "Locations shouldn't overlap")
def testParsesMultisegmentLocation(self): location = Location.from_location_str( 'join(AF178221.1:<1..60,AF178222.1:1..63,AF178223.1:1..42, 1..>90)' ) self.assertTrue(location.intersects(Location.from_location((80, ))), "Location doesn't contain target point") self.assertIn('AF178221.1', location.references(), 'Reference AF178221.1 not parsed') self.assertIn('AF178222.1', location.references(), 'Reference AF178222.1 not parsed') self.assertIn('AF178223.1', location.references(), 'Reference AF178223.1 not parsed') self.assertTrue( len(location.references()) == 3, 'Wrong number of references')
class HighlightToken(object): def __init__(self, _x, _y, level): self.highlighted = False self.level_name = level self.location = Location(_x, _y) self.size = Location(80, 80) self.tokensprite = pygame.sprite.Sprite() self.tokensprite.image = pygame.image.load( "highlight_token.png").convert_alpha() self.tokensprite.image = pygame.transform.scale( self.tokensprite.image, (20, 20)) self.tokensprite.rect = self.tokensprite.image.get_rect() def draw(self, screen): if self.highlighted: screen.blit(self.tokensprite.image, self.location.get_loc()) def hit_check(self, x, y): if x > self.location.x - (self.size.x / 2) and x < self.location.x + ( self.size.x / 2): if y > self.location.y - ( self.size.y / 2) and y < self.location.y + (self.size.y / 2): self.highlighted = True return True self.highlighted = False return False
def __find_first_overlapping_CDS_id(self, aln_location, cdss): ''' Find id of the first CDS which overlaps with the given alignment. Uses binary search algorithm. @param (Location) aln_location Alignment Location @param [Cds] cdss List of CDSs, sorted by start @returns (int|None) Id in cdss of described CDS, None if no overlap ''' lo = 0 hi = len(cdss) - 1 # If cdss is empty -> return None if (hi < 0): return None while (lo < hi): mid = lo + (hi - lo) // 2 # '//' for python 3 compatibility cds_rel_pos = self.__get_cds_rel_pos(cdss, mid, aln_location) if (cds_rel_pos == "LEFT_OF_ALN"): lo = mid + 1 if (cds_rel_pos == "RIGHT_OF_ALN"): hi = mid - 1 if (cds_rel_pos == "FIRST"): return mid # Check lo cds_location = Location.from_location_str(cdss[lo].location) if self.__overlap(cds_location, aln_location): return lo else: return None
class Player(object): def __init__(self): self.location = Location(100, 100) self.size = Location(100, 50) self.countdown = 5 self.cur_offset = 0 self.reversing = False self.sprites = [] for i in range(0, 6): self.sprites.append(pygame.sprite.Sprite()) self.sprites[i].image = pygame.image.load( "lewis_character_5.png").convert_alpha() self.sprites[i].rect = self.sprites[i].image.get_rect() def update(self): # TODO: Move through the sprites self.countdown -= 1 if self.countdown < 1: self.countdown = 5 if self.reversing is False: self.cur_offset += 50 if self.cur_offset >= 250: self.cur_offset = 150 self.reversing = True else: self.cur_offset -= 50 if self.cur_offset < 0: self.cur_offset = 50 self.reversing = False def draw(self, screen): screen.blit(self.sprites[0].image, self.location.get_loc(), (self.cur_offset, 0, 50, 100))
def __find_first_overlapping_CDS_id (self, aln_location, cdss): ''' Find id of the first CDS which overlaps with the given alignment. Uses binary search algorithm. @param (Location) aln_location Alignment Location @param [Cds] cdss List of CDSs, sorted by start @returns (int|None) Id in cdss of described CDS, None if no overlap ''' lo = 0 hi = len(cdss) - 1 # If cdss is empty -> return None if (hi < 0): return None while (lo < hi): mid = lo + (hi - lo) // 2 # '//' for python 3 compatibility cds_rel_pos = self.__get_cds_rel_pos (cdss, mid, aln_location) if (cds_rel_pos == "LEFT_OF_ALN"): lo = mid + 1 if (cds_rel_pos == "RIGHT_OF_ALN"): hi = mid - 1 if (cds_rel_pos == "FIRST"): return mid # Check lo cds_location = Location.from_location_str(cdss[lo].location) if self. __overlap(cds_location, aln_location): return lo else: return None
def __init__(self, attributes={}): self.attributes = attributes self.origin = None self.record_id = self.version #Added because of compatibility with older code if self.location: self.location_min = Location.fast_min_str(self.location) else: self.location_min = sys.maxint
def get_cds_location(self): '''Returns Location object of the associated CDS. Args: None Returns: (Location): Location of the associated CDS. ''' return Location.from_location_str(self.cds.location)
def testIntersectionLocation(self): # Test no intersection case loc1 = Location.from_location_str('complement(50..100)') self.assertEqual( None, loc1.find_intersection( Location.from_location_str('complement(200..500)'))) self.assertEqual( None, loc1.find_intersection( Location.from_location_str('complement(1..40)'))) self.assertEqual( None, loc1.find_intersection( Location.from_location_str('complement(1..49)'))) self.assertEqual( None, loc1.find_intersection( Location.from_location_str('complement(101..150)'))) # Test simple one interval intersection loc1 = Location.from_location_str('10..100') loc2 = Location.from_location_str('50..100') intersection = loc1.find_intersection(loc2) self.assertEqual(loc2.start, intersection.start, "Start intersection position doesn't match") self.assertEqual(loc2.end, intersection.end, "End intersection position doesn't match") self.assertEqual(loc2.complement, intersection.complement, "Complement intersection information doesn't match") # Test multiple interval intersection loc1 = Location.from_location_str('join(1..40,60..80,120..200)') aln_location = Location.from_location_str('30..130') loc2 = Location.from_location_str('join(30..40,60..80,120..130)') intersection = loc1.find_intersection(aln_location) for subint, subl2 in zip(intersection.sublocations, loc2.sublocations): self.assertEqual(subl2.start, subint.start, "Start intersection position doesn't match") self.assertEqual(subl2.end, subint.end, "End intersection position doesn't match") self.assertEqual(loc2.complement, intersection.complement, "Complement intersection information doesn't match")
def __init__(self, width, height, message_pump): self.message_pump = message_pump self.message_pump.register(self) self.curlevel = 1 self.player_location = Location(0, 0) self.changed_level = False self.mapsprite = pygame.sprite.Sprite() self.mapsprite.image = pygame.image.load("map.jpg").convert() self.mapsprite.image = pygame.transform.scale(self.mapsprite.image, (width, height)) self.mapsprite.rect = self.mapsprite.image.get_rect() self.location = Location(0, 0) self.tokens = [] self.tokens.append(HighlightToken(112, 183, "1")) self.tokens.append(HighlightToken(180, 243, "4")) self.tokens.append(HighlightToken(256, 267, "5")) self.tokens.append(HighlightToken(316, 310, "6")) self.tokens.append(HighlightToken(378, 275, "7")) self.tokens.append(HighlightToken(436, 250, "8"))
def _calc_coverage(self, cds_aln): """ Calculates coverage of given cds alignment. Coverage is calculated as sum of lengths of aligned regions divided by length of cds. @param (CdsAlignment) cds_aln @return (float) coverage """ # Aligned region is part of a read that intersects with cds. coverage = 0 for aln_reg in cds_aln.aligned_regions.values(): # aln_reg is of type CdsAlnSublocation location = aln_reg.location # location is of type Location coverage += location.length() coverage = coverage / float(Location.from_location_str(cds_aln.cds.location).length()) return coverage
def calc_cds_coverage(cds_aln): """ Calculates coverage of cds. Coverage is average number of reads per base of cds. @param (CdsAlignment) cds_aln @return (float) Cds coverage. """ coverage = 0 for aln_reg in cds_aln.aligned_regions.values( ): # aln_reg is of type CdsAlnSublocation location = aln_reg.location # location is of type Location coverage += location.length() coverage = coverage / float( Location.from_location_str(cds_aln.cds.location).length()) return coverage
def _calc_coverage(self, cds_aln): """ Calculates coverage of given cds alignment. Coverage is calculated as sum of lengths of aligned regions divided by length of cds. @param (CdsAlignment) cds_aln @return (float) coverage """ # Aligned region is part of a read that intersects with cds. coverage = 0 for aln_reg in cds_aln.aligned_regions.values( ): # aln_reg is of type CdsAlnSublocation location = aln_reg.location # location is of type Location coverage += location.length() coverage = coverage / float( Location.from_location_str(cds_aln.cds.location).length()) return coverage
def determine_coding_seqs(self, record_container): ''' Determines which of the CDSs in the record aligned_regions aligned to the read. @return list of tuples (cds, intersecting_location) if such exist, None if record is not available from the database ''' self.aligned_cdss = [] record = record_container.fetch_record(self.nucleotide_accession) # if not possible to fetch a record from the db, return None if not record: return None (start, stop) = self.location_span try: location = Location.from_location_str("%d..%d" % (start, stop)) except LoactionParsingException, e: print "ReadAlignment/determine_coding_seqs:", e self.aligned_cdss = [] return self.aligned_cdss
def determine_coding_seqs (self, record_container): ''' Determines which of the CDSs in the record aligned_regions aligned to the read. @return list of tuples (cds, intersecting_location) if such exist, None if record is not available from the database ''' self.aligned_cdss = [] record = record_container.fetch_record (self.nucleotide_accession) # if not possible to fetch a record from the db, return None if not record: return None (start,stop) = self.location_span try: location = Location.from_location_str("%d..%d" % (start, stop)) except LoactionParsingException, e: print "ReadAlignment/determine_coding_seqs:", e self.aligned_cdss = [] return self.aligned_cdss
def add_cds_to_organism(organism, read, target_alignment): target_cdss = target_alignment.aligned_cdss assert(len(target_cdss) >= 1) binned_read = resdata.BinnedRead(read.id) if len(target_cdss) == 1: # do stuffs (target_cds, intersection) = target_cdss[0] else: cdss = [] for (cds, intersection) in target_cdss: cdss.append(cds) sorted_cdss = sorted(cdss, key = lambda cds: Location.from_location_str(cds.location).length()) target_cds = sorted_cdss[-1] if organism.contains_identified_coding_region(target_cds): identified_cds = organism.identified_coding_regions[target_cds] identified_cds.add_binned_read(binned_read) else: identified_cds = resdata.IdentifiedCds(target_cds) identified_cds.add_binned_read(binned_read) organism.add_identified_coding_region(identified_cds)
def determine_coding_seqs_optimal (self, record): ''' Determines which of the CDSs in the record aligned_regions aligned to the read. @param (UnityRecord) record Record that is used @return list of tuples (cds, intersecting_location) if such exist, None if record is not available from the database ''' self.aligned_cdss = [] # If not possible to fetch a record from the db, return None if not record: return None # Acquire alignment Location (start, stop) = self.location_span try: aln_location = Location.from_location_str("%d..%d" % (start, stop)) except LoactionParsingException, e: print "ReadAlignment/determine_coding_seqs:", e self.aligned_cdss = [] return self.aligned_cdss
def determine_coding_seqs_optimal(self, record): ''' Determines which of the CDSs in the record aligned_regions aligned to the read. @param (UnityRecord) record Record that is used @return list of tuples (cds, intersecting_location) if such exist, None if record is not available from the database ''' self.aligned_cdss = [] # If not possible to fetch a record from the db, return None if not record: return None # Acquire alignment Location (start, stop) = self.location_span try: aln_location = Location.from_location_str("%d..%d" % (start, stop)) except LoactionParsingException, e: print "ReadAlignment/determine_coding_seqs:", e self.aligned_cdss = [] return self.aligned_cdss
def testLocationContains(self): l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('5..10') self.assertTrue(l1.contains(l2), '1..10 should contain 5..10') l1 = Location.from_location_str('complement(<23..50)') l2 = Location.from_location_str('complement(24..50)') self.assertTrue( l1.contains(l2), 'complement(<23..50) should contain ' 'complement(24..50)') l1 = Location.from_location_str('join(1..10,20..30)') l2 = Location.from_location_str('join(2..8,25..28)') self.assertTrue( l1.contains(l2), 'join(1..10,20..30) should contain ' 'join(2..8,25..28)') l1 = Location.from_location_str( 'join(complement(1..10),complement(20..30))') l2 = Location.from_location_str( 'join(complement(2..8),complement(25..28))') self.assertTrue( l1.contains(l2), 'join(complement(1..10),complement(20..30)) should contain ' 'join(complement(2..8),complement(25..28))') l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('4..20') self.assertFalse(l1.contains(l2), '1..10 should not contain ' '4..20') l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('15..20') self.assertFalse(l1.contains(l2), '1..10 should not contain ' '15..20') l1 = Location.from_location_str('complement(1..10)') l2 = Location.from_location_str('complement(4..20)') self.assertFalse( l1.contains(l2), 'complement(1..10) should not contain ' 'complement(4..20)') l1 = Location.from_location_str('complement(1..10)') l2 = Location.from_location_str('complement(15..20)') self.assertFalse( l1.contains(l2), 'complement(1..10) should not contain ' 'complement(15..20)') l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('complement(1..10)') self.assertFalse(l1.contains(l2), '1..10 should not contain ' 'complement(1..10)') l1 = Location.from_location_str('1..10') l2 = Location.from_location_str('complement(15..20)') self.assertFalse(l1.contains(l2), '1..10 should not contain ' 'complement(15..20)') l1 = Location.from_location_str('join(1..10,11..50)') l2 = Location.from_location_str('15..20') self.assertTrue(l1.contains(l2), 'join(1..10,11..50) should contain ' '15..20') l1 = Location.from_location_str('join(1..10,11..50)') l2 = Location.from_location_str('complement(15..20)') self.assertTrue( l1.contains(l2, use_complement=False), 'join(1..10,11..50) should contain ' 'complement(15..20) without complement information') l1 = Location.from_location_str('REF1:1..10') l2 = Location.from_location_str('5..10') self.assertFalse(l1.contains(l2), 'REF1:1..10 should contain 5..10') l1 = Location.from_location_str('REF1:1..10') l2 = Location.from_location_str('REF2:5..10') self.assertFalse(l1.contains(l2), '1..10 should contain 5..10')
def matches(self, location, complement, tolerance): l1 = Location.from_location_str(self.location, tolerance) return l1.intersects(Location.from_location(location, complement))
except LoactionParsingException, e: print "ReadAlignment/determine_coding_seqs:", e self.aligned_cdss = [] return self.aligned_cdss # Determine first overlapping CDS - binary search first_ovp_id = self.__find_first_overlapping_CDS_id (aln_location, record.cds) # No CDS from the list overlaps - return [] if (first_ovp_id == None): return self.aligned_cdss # Determine following overlapping CDSs - loop while overlaps for i in range(first_ovp_id, len(record.cds)): cds = record.cds[i] cds_location = Location.from_location_str(cds.location) # If this one does not overlap, the others also won't because it's sorted if not self.__overlap(cds_location, aln_location): break location_intersection = cds_location.find_intersection (aln_location) if location_intersection is not None: self.aligned_cdss.append ((cds, location_intersection)) return self.aligned_cdss # ---------------------------------------------------------------------------- # def set_type (self): """ Location can be coding or non-coding
def testIntersectionsWithTolerance(self): location = Location.from_location_str( "complement(join(<197..1301,2070..>2451))", tolerance=100) self.assertFalse(location.intersects(Location.from_location((100, )))) self.assertTrue( location.intersects( Location.from_location((100, ), complement=True))) self.assertFalse(location.intersects(Location.from_location((300, )))) self.assertTrue( location.intersects( Location.from_location((300, ), complement=True))) self.assertFalse(location.intersects(Location.from_location( (50, 100)))) self.assertTrue( location.intersects( Location.from_location((50, 100), complement=True))) self.assertFalse( location.intersects(Location.from_location((300, 400)))) self.assertTrue( location.intersects( Location.from_location((300, 400), complement=True))) self.assertFalse( location.intersects(Location.from_location((1200, 1400)))) self.assertTrue( location.intersects( Location.from_location((1200, 1400), complement=True))) self.assertFalse( location.intersects(Location.from_location((2500, 2600)))) self.assertTrue( location.intersects( Location.from_location((2500, 2600), complement=True))) self.assertFalse( location.intersects(Location.from_location((3000, 4000)))) self.assertFalse( location.intersects( Location.from_location((3000, 4000), complement=True)))
def staypoints_clustering(self): # # km = KMeans(n_clusters=self.location_num) # km.fit(self.staypoint_coords) # labels = km.labels_ # self.location_coords = km.cluster_centers_ # self.location_coords = list(self.location_coords) # self.location_coords = [tuple(list(i)) for i in self.location_coords] # self.save() # print(labels) # print(self.location_coords) # # for location_coord in self.location_coords: # lng, lat = location_coord[0], location_coord[1] # Location(lng, lat) # # for i in range(len(self.staypoint_coords)): # staypoint_coord = self.staypoint_coords[i] # staypoint = open_staypoint(staypoint_coord[0], staypoint_coord[1]) # label = labels[i] # loc_lng, loc_lat = self.location_coords[label][0], self.location_coords[label][1] # staypoint.classified_to(loc_lng, loc_lat) # location = open_location(loc_lng, loc_lat) # location.add_staypoint(staypoint) location_list = [ Location('什刹海', 39.9405445888, 116.3790198384, 7200, 4.1 + rd.uniform(-0.1, 0.1), True, True, False, True, True, True, True, True), Location('香山', 39.9899119700, 116.1807857600, 14400, 3.9 + rd.uniform(-0.1, 0.1), True, False, False, False, False, False, True, False), Location('龙庆峡', 40.5493142300, 115.9955103200, 28800, 4 + rd.uniform(-0.1, 0.1), True, False, False, False, False, True, False, False), Location('凤凰岭', 40.1102250000, 116.1037220000, 18000, 4.2 + rd.uniform(-0.1, 0.1), True, False, False, False, True, True, True, True), Location('雁栖湖', 40.3971688360, 116.6617944147, 3600, 4.1 + rd.uniform(-0.1, 0.1), True, False, False, False, False, False, True, False), Location('故宫', 39.9163448469, 116.3907955961, 10800, 5 +rd.uniform(-0.1, 0.1), False, True, True, False, True, True, True, True), Location('颐和园', 39.9910529965, 116.2623785514, 14400, 5 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('八达岭长城', 40.3568158859, 116.0033270630, 10800, 5 +rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('天坛', 39.8787321303, 116.4044345114, 10800, 4.7 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('圆明园', 39.9994553769,116.3063647107, 10800, 4.3 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('奥体中心', 39.9827989400, 116.3934758455, 7200, 4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('798艺术区', 39.9839115718, 116.4891264186, 9000, 4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('南锣鼓巷', 39.9349123949, 116.3969041290, 7200, 4.3 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('北海公园', 39.9251684951, 116.3828729377, 7200, 3.9 + rd.uniform(-0.1, 0.1), True, True, False, False, True, True, True, False), Location('北京动物园', 39.9407590788, 116.3306260182, 7200, 3.9 + rd.uniform(-0.1, 0.1), False, False, True, False, True, False, True, False), Location('世界公园', 39.8092058939, 116.2815836159, 14400, 3.7 +rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('恭王府', 39.9345239804, 116.3801733881, 7200, 4.1 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), # Location('清华大学', 40.0024941386, 116.3203957568, 7200, # False, True, False, False, # False, False, True, False), Location('清华园', 39.9998001386,116.3182237568, 1800, 4.2 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, False), Location('清华大学美术馆', 39.9983728854,116.3295761366, 3600, 3.8 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), # Location('清华大学荷塘月色', 40.0003841386,116.3132597568, 1800, # True, True, False, False, # False, True, False, False), # Location('清华大学中央主楼', 40.0004748854,116.3263611366, 1200, # True, True, False, False, # False, True, False, False), # Location('北京大学', 39.9916928135, 116.3041666458, 10800, # False, True, False, False, # False, False, True, False), Location('北京大学西门', 39.9932844900,116.2983736200, 1200, 4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('北京大学未名湖', 39.9916928135, 116.3041666458, 1800, 3.9 + rd.uniform(-0.1, 0.1), True, True, False, False, False, True, False, False), Location('北京大学图书馆', 39.9906185800,116.3044249600, 1200, 3.7 +rd.uniform(-0.1, 0.1), False, True, False, False, False, False, True, False), Location('三里屯', 39.9333058614, 116.4480927486, 10800, 4 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('王府井', 39.9121538340, 116.4053800413, 7200, 3.9 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('北京欢乐谷', 39.8664890017, 116.4889081341, 28800, 5 + rd.uniform(-0.1, 0.1), False, False, True, False, True, False, True, False), Location('雍和宫', 39.9463812475, 116.4110152834, 7200, 4.4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('中国科学技术馆', 40.0043879861, 116.3922452044, 10800, 4.1 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('大栅栏', 39.8936652019, 116.3900643781, 7200, 3.5 + rd.uniform(-0.1, 0.1), False, True, False, True, True, True, True, True), Location('景山公园', 39.9244714951, 116.3903129377, 3600, 4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('天安门广场', 39.9019029520, 116.3915506741, 5400, 4 + rd.uniform(-0.1, 0.1), False, True, True, False, True, True, True, True), Location('慕田峪长城', 40.4309372804, 116.5569841947, 10800, 4.5 + rd.uniform(-0.1, 0.1), True, True, False, False, True, False, True, False), Location('明十三陵', 40.2489675591, 116.2149996280, 7200, 4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('八大处', 39.9536760716, 116.1799551309, 3600, 3.6 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('朝阳公园', 39.9444241593, 116.4757550486, 7200, 3.8 + rd.uniform(-0.1, 0.1), False, True, False, False, False, True, True, False), Location('大观园', 39.8700414477, 116.3499541389, 7200, 3.5 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('前门', 39.8987849520, 116.3916496741, 3600, 3.9 + rd.uniform(-0.1, 0.1), False, True, False, False, True, False, True, False), Location('军事博物馆', 39.9079348952, 116.3174984949, 7200, 3.5 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('石景山游乐园', 39.9106656859, 116.2026574849, 14400, 3.8 + rd.uniform(-0.1, 0.1), False, False, True, False, False, True, True, False), Location('红螺寺', 40.3739420514, 116.6195877187, 10800, 4 + rd.uniform(-0.1, 0.1), True, True, False, False, False, False, True, False), Location('潭柘寺', 39.9035751027, 116.0250057635, 14400, 4.1 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('北京天文馆', 39.9360434365, 116.3306215891, 7200, 4.1 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('国家大剧院', 39.9016079520, 116.3836846741, 3600, 3.2 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('爨底下村', 39.9970232679, 115.6379426868, 14400, 4.2 + rd.uniform(-0.1, 0.1), False, True, False, False, False, True, True, False), Location('国子监', 39.9454482475, 116.4070142834, 3600, 3.4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('北京野生动物园', 39.4949138137, 116.3299266221, 14400, 3.9 + rd.uniform(-0.1, 0.1), False, False, True, False, True, False, True, False), Location('首都博物馆', 39.9052038578, 116.3357899005, 14400, 3.5 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('玉渊潭', 39.9131537272, 116.3104702266, 14400, 4.1 + rd.uniform(-0.1, 0.1), True, False, False, False, True, False, False, False), Location('十渡', 39.6374202062, 115.5938336213, 28800, 4 + rd.uniform(-0.1, 0.1), True, False, False, False, False, True, False, False), Location('地坛', 39.9522644472, 116.4086927961, 7200, 3.4 + rd.uniform(-0.1, 0.1), False, True, False, False, True, True, True, True), Location('西单大悦城', 39.9094916921, 116.3666249205, 7200, 3.9 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('东方普罗旺斯薰衣草庄园', 40.3383061435, 116.6137370245, 7200, 3.3 + rd.uniform(-0.1, 0.1), True, False, False, False, False, True, False, False), Location('簋街', 39.9395659441, 116.4193625707, 7200, 4 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('中央电视塔', 39.9181400976, 116.3011703764, 7200, 3.6 + rd.uniform(-0.1, 0.1), False, False, True, False, True, True, True, True), Location('世贸天阶', 39.9152148295, 116.4458219839, 7200, 3.3 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), Location('古北水镇', 40.6524533521, 117.2676082992, 28800, 4 +rd.uniform(-0.1, 0.1), True, True, False, False, True, True, True, True), Location('五道口',39.9915778400,116.3315181900, 7200, 3.5 + rd.uniform(-0.1, 0.1), False, False, False, True, True, True, True, True), # Location('清华科技园', 39.9934504100,116.3253030800, 3600, # False, False, False, True, # True, True, True, True), ] self.location_num = len(location_list) self.M = np.zeros([self.user_num, self.location_num], dtype=int) # user-location, travel_times self.transfer_times = np.zeros([self.location_num, self.location_num], dtype=list) self.classical_travel_score = np.zeros([self.location_num, self.location_num], dtype=float) self.location_coords = [] for location in location_list: self.location_coords.append((location.lng, location.lat)) self.location_name_coord_dict[location.name] = (location.lng, location.lat) self.save() i = 0 for staypoint_coord in self.staypoint_coords: dist = [get_distance(staypoint_coord[0], staypoint_coord[1], location_coord[0], location_coord[1]) for location_coord in self.location_coords] min_dist = min(dist) min_dist_idx = dist.index(min_dist) location_coord = self.location_coords[min_dist_idx] location = open_location(location_coord[0], location_coord[1]) staypoint = open_staypoint(staypoint_coord[0], staypoint_coord[1]) staypoint.classified_to(location_coord[0], location_coord[1]) location.add_staypoint(staypoint) print(i,'--->',location.name) i += 1 for location_coord in self.location_coords: location = open_location(location_coord[0], location_coord[1]) print(location.name,':', len(location.staypoint_coords))
def __init__(self, width, height, message_pump, tent): self.tent = tent self.message_pump = message_pump self.message_pump.register(self) self.curlevel = 1 self.location = Location(0, 0) self.reset() #Randomly pick a starting location self.player_location = Location(0, 0) # self.size = Location(40,40) self.forestsprite = pygame.sprite.Sprite() self.forestsprite.image = pygame.image.load( "start location.jpg").convert() self.forestsprite.image = pygame.transform.scale( self.forestsprite.image, (width, height)) self.forestsprite.rect = self.forestsprite.image.get_rect() self.forestsprite2 = pygame.sprite.Sprite() self.forestsprite2.image = pygame.image.load( "location 2.jpg").convert() self.forestsprite2.image = pygame.transform.scale( self.forestsprite2.image, (width, height)) self.forestsprite2.rect = self.forestsprite2.image.get_rect() self.forestsprite3 = pygame.sprite.Sprite() self.forestsprite3.image = pygame.image.load( "location 3.jpg").convert() self.forestsprite3.image = pygame.transform.scale( self.forestsprite3.image, (width, height)) self.forestsprite3.rect = self.forestsprite3.image.get_rect() self.forestsprite4 = pygame.sprite.Sprite() self.forestsprite4.image = pygame.image.load( "location 4.jpg").convert() self.forestsprite4.image = pygame.transform.scale( self.forestsprite4.image, (width, height)) self.forestsprite4.rect = self.forestsprite4.image.get_rect() self.forestsprite5 = pygame.sprite.Sprite() self.forestsprite5.image = pygame.image.load( "location 5.jpg").convert() self.forestsprite5.image = pygame.transform.scale( self.forestsprite5.image, (width, height)) self.forestsprite5.rect = self.forestsprite5.image.get_rect() self.forestsprite6 = pygame.sprite.Sprite() self.forestsprite6.image = pygame.image.load( "location 6.jpg").convert() self.forestsprite6.image = pygame.transform.scale( self.forestsprite6.image, (width, height)) self.forestsprite6.rect = self.forestsprite6.image.get_rect() self.yurtsprite = pygame.sprite.Sprite() self.yurtsprite.image = pygame.image.load("yurt inside.jpg").convert() self.yurtsprite.image = pygame.transform.scale(self.yurtsprite.image, (width, height)) self.yurtsprite.rect = self.yurtsprite.image.get_rect() self.mapsprite = pygame.sprite.Sprite() self.mapsprite.image = pygame.image.load("map.jpg").convert() self.mapsprite.image = pygame.transform.scale(self.mapsprite.image, (100, 45)) self.mapsprite.rect = self.mapsprite.image.get_rect() self.map = MRFMap(width, height, message_pump) self.changed_level = False
def matches(self, location, complement, tolerance): l1 = Location.from_location_str(self.location, tolerance) return l1.intersects(Location.from_location(location, complement))
class Background(object): def __init__(self, width, height, message_pump, tent): self.tent = tent self.message_pump = message_pump self.message_pump.register(self) self.curlevel = 1 self.location = Location(0, 0) self.reset() #Randomly pick a starting location self.player_location = Location(0, 0) # self.size = Location(40,40) self.forestsprite = pygame.sprite.Sprite() self.forestsprite.image = pygame.image.load( "start location.jpg").convert() self.forestsprite.image = pygame.transform.scale( self.forestsprite.image, (width, height)) self.forestsprite.rect = self.forestsprite.image.get_rect() self.forestsprite2 = pygame.sprite.Sprite() self.forestsprite2.image = pygame.image.load( "location 2.jpg").convert() self.forestsprite2.image = pygame.transform.scale( self.forestsprite2.image, (width, height)) self.forestsprite2.rect = self.forestsprite2.image.get_rect() self.forestsprite3 = pygame.sprite.Sprite() self.forestsprite3.image = pygame.image.load( "location 3.jpg").convert() self.forestsprite3.image = pygame.transform.scale( self.forestsprite3.image, (width, height)) self.forestsprite3.rect = self.forestsprite3.image.get_rect() self.forestsprite4 = pygame.sprite.Sprite() self.forestsprite4.image = pygame.image.load( "location 4.jpg").convert() self.forestsprite4.image = pygame.transform.scale( self.forestsprite4.image, (width, height)) self.forestsprite4.rect = self.forestsprite4.image.get_rect() self.forestsprite5 = pygame.sprite.Sprite() self.forestsprite5.image = pygame.image.load( "location 5.jpg").convert() self.forestsprite5.image = pygame.transform.scale( self.forestsprite5.image, (width, height)) self.forestsprite5.rect = self.forestsprite5.image.get_rect() self.forestsprite6 = pygame.sprite.Sprite() self.forestsprite6.image = pygame.image.load( "location 6.jpg").convert() self.forestsprite6.image = pygame.transform.scale( self.forestsprite6.image, (width, height)) self.forestsprite6.rect = self.forestsprite6.image.get_rect() self.yurtsprite = pygame.sprite.Sprite() self.yurtsprite.image = pygame.image.load("yurt inside.jpg").convert() self.yurtsprite.image = pygame.transform.scale(self.yurtsprite.image, (width, height)) self.yurtsprite.rect = self.yurtsprite.image.get_rect() self.mapsprite = pygame.sprite.Sprite() self.mapsprite.image = pygame.image.load("map.jpg").convert() self.mapsprite.image = pygame.transform.scale(self.mapsprite.image, (100, 45)) self.mapsprite.rect = self.mapsprite.image.get_rect() self.map = MRFMap(width, height, message_pump) self.changed_level = False def update(self): self.changed_level = False if self.curlevel == 2: if self.player_location.x > 380 and self.player_location.x < 470: if self.player_location.y >= 230 and self.player_location.y < 250: if pygame.key.get_pressed()[pygame.K_e] != 0: print("press e") self.message_pump.send_message("change level", "3") elif self.curlevel == 3: # print("update map...") self.map.update() # self.facing = "e" # # We need to move to the LEFT # self.location.x -= 2 def message(self, msg_type, msg): if msg_type == "change level" and self.changed_level is False: self.changed_level = True self.curlevel = int(msg) if msg == "1": # self.curlevel = 1 print("loading first location") elif msg == "2": # self.curlevel = 2 print("loading tent") elif msg == "3": # self.curlevel = 3 print("loading map in background.py") elif msg == "4": # self.curlevel = 4 pass else: print("ERROR: Unkown level in background.py {}".format(msg)) elif msg_type == "player location" and self.curlevel == 2: x = msg.split(" ")[0] x = int(x) y = msg.split(" ")[1] y = int(y) self.player_location.x = x self.player_location.y = y if y > 420: print("Going to level 1") self.message_pump.send_message("change level", self.tent.return_level) print("{} : {} , [{}] background.py".format(x, y, type(x))) def reset(self): # We have reached the bottom of the screen so reset # self.location.y = 0 # self.location.x = random.randint(0, 560) pass def draw(self, screen): # screen.blit(self.forestsprite.image, self.location.get_loc()) if self.curlevel == 1: screen.blit(self.forestsprite.image, self.location.get_loc()) elif self.curlevel == 2: screen.blit(self.yurtsprite.image, self.location.get_loc()) screen.blit(self.mapsprite.image, (410, 220)) elif self.curlevel == 3: self.map.draw(screen) elif self.curlevel == 4: screen.blit(self.forestsprite2.image, self.location.get_loc()) elif self.curlevel == 5: screen.blit(self.forestsprite3.image, self.location.get_loc()) elif self.curlevel == 6: screen.blit(self.forestsprite4.image, self.location.get_loc()) elif self.curlevel == 7: screen.blit(self.forestsprite5.image, self.location.get_loc()) elif self.curlevel == 8: screen.blit(self.forestsprite6.image, self.location.get_loc()) def check_collision(self, obj): # Check if the two objects are touching diffx = self.location.x - obj.location.x diffy = obj.location.y - self.location.y if diffx < self.size.x and diffx > (self.size.x * -1): if diffy < self.size.y and diffy > (self.size.y * -1): return True return False
def testIntersectionDifferentStrands(self): loc1 = Location.from_location(location_tuple=(1, 2), complement=True) loc2 = Location.from_location(location_tuple=(1, 2), complement=False) self.assertEqual(None, loc1.find_intersection(loc2))