def drawingStayAreaCenter(data, fname, dir='imgresults', title='Result for Finding StayArea'): ''' 画用户停留区域结果图,只画出停留区域中心点-v1.1 :param data: 结果数据集 list[type, lat,longti,time]类型 :param fname: 保存图片文件名 :param dir: 文件保存目录名 :param title: 图的标题 :return: ''' loop = 1 for line in data: print('Drawing center point [%d], left [%d]' % (loop, len(data) - loop)) content = line.rstrip().split(',') if content[0] == 'Center': color_r = random.uniform(0, 1) color_b = random.uniform(0, 1) color_g = random.uniform(0, 1) plt.plot(float(content[1]), float(content[2]), '.', color=(color_r, color_g, color_b), linewidth=0.5) loop += 1 plt.xlabel('Latitude') plt.ylabel('Lontitude') plt.title(title) plt.grid(True) fname = combileFileName(dirs=dir, filename=fname) plt.savefig(fname) plt.clf() print('Drawing end')
def seperateUserTrajectory(user_trajectory, dir=r'classifytxtresults', filename=r'10&8&40user.txt'): ''' 将用户轨迹分离,分为被检测用户的轨迹以及其他用户的轨迹 :param user_trajectory: 一组相似用户的所有轨迹类型为:dict{'user', list[Trajectory]} :param dir: 原始相似用户保存的文件目录名 :param filename: 原始相似用户保存的文件名,文件的第一条用户即为待检测的用户 :return: 被检测用户的轨迹列表类型为:list[Trajectory]和其他用户的轨迹列表类型为:list[Trajectory] ''' filename = combileFileName(dirs=dir, filename=filename) input_file = open(filename) detected_user = input_file.readline().rstrip() input_file.close() detected_trajectorys = [] others_trajectorys = [] for key in user_trajectory.keys(): if key == detected_user: trajectorys = user_trajectory.get(key).copy() for trajectory in trajectorys: detected_trajectorys.append(trajectory) else: trajectorys = user_trajectory.get(key).copy() for trajectory in trajectorys: others_trajectorys.append(trajectory) return detected_trajectorys, others_trajectorys
def drawingStayAreaDetail(data, fname, dir='imgresults', minlat=31, maxlat=32, minlon=121, maxlon=122, title='StayArea Result for Finding Staypoint'): ''' 画用户停留区域结果图,画出某范围内的停留区域中心点以及其邻居节点的详细画图-v1.1 :param data: 结果数据集 list[type, lat,longti,time]类型 :param fname: 保存图片文件名 :param dir: 文件保存目录名 :param minlat: 最小纬度 :param maxlat: 最大纬度 :param minlon: 最小经度 :param maxlon: 最大经度 :param title: 图的标题 :return: ''' loop = 1 color_index = 0 ret_color = [] skip = 0 for line in data: print('Drawing detail point [%d], left [%d], minlat [%d]' % (loop, len(data) - loop, minlat)) content = line.rstrip().split(',') lat = float(content[1]) lon = float(content[2]) if (maxlat >= lat >= minlat) and (maxlon >= lon >= minlon): if content[0] == 'Center': color_r = random.uniform(0, 1) color_b = random.uniform(0, 1) color_g = random.uniform(0, 1) ret_color.append([color_r, color_g, color_b]) color_index += 1 plt.plot(lat, lon, '.', color=(color_r, color_g, color_b)) plt.text(lat, lon, 'C' + str(color_index)) else: if skip % 2 == 0: plt.plot(lat, lon, '.', color=(ret_color[color_index - 1][0], ret_color[color_index - 1][1], ret_color[color_index - 1][2])) loop += 1 skip += 1 plt.xlabel('Latitude/°') plt.ylabel('Lontitude/°') plt.title(title) plt.grid(True) fname = combileFileName(dirs=dir, filename=fname) plt.savefig(fname) plt.clf() print('Drawing end')
def loadTxtSimilarityUser(dir=r'classifytxtresults', filename=r'10&8&40user.txt'): ''' 加载用户分类结果,用轨迹挖掘提供用户信息 :param dir: 目录名 :param filename: 文件名 :return: 将相似用户按组划分,类型为list[user] ''' filename = combileFileName(dirs=dir, filename=filename) similarity_user = [] for line in open(filename): if line != '': similarity_user.append(line.rstrip()) return similarity_user
def outputClassifyPklResults(data, dir=r'classifypklresults', filename=r'classifyresult.pkl'): ''' 保存用户分类结果数据信息,以二进制形式保存,将结果序列化保存-v1.0 :param data: 用户分类结果数据信息 list[UserClassify] :param dir: 目录名 :param filename: 保存数据的文件名 :return: ''' print('Output beginlocation') filename = str(len(data)) + '&' + filename filename = combileFileName(dir, filename) out_file = open(filename, 'wb') pickle.dump(data, out_file) out_file.close() print('Output endlocation')
def outputPklResults(data, user='******', dir=r'saveresults', filename=r'dbscanresult.pkl'): ''' 保存停留区域或者停留位置挖掘结果,以二进制形式保存,将结果序列化保存-v1.1 :param data: 需要保存的停留区域以及停留位置数据 list[StayArea]或list[StayPoint] :param dir: 目录名 :param filename: 保存数据的文件名 :return: ''' print('Output beginlocation') filename = user + '&' + str(len(data)) + '&' + filename filename = combileFileName(dir, filename) out_file = open(filename, 'wb') pickle.dump(data, out_file) out_file.close() print('Output endlocation')
def drawingStayAreaCenter(data, fname, dir='imgresults', title='MSUserA Rawdata Distribute', minlat_2=39, maxlat_2=41, minlon_2=116, maxlon_2=117): ''' minlat_2=6.6, maxlat_2=7, minlon_2=46, maxlon_2=48 minlat_2=39, maxlat_2=41, minlon_2=116, maxlon_2=117 画用户停留区域结果图,只画出停留区域中心点-v1.1 :param data: 结果数据集 list[type, lat,longti,time]类型 :param fname: 保存图片文件名 :param dir: 文件保存目录名 :param title: 图的标题 :return: ''' loop = 1 for line in data: print('Drawing center point [%d], left [%d]' % (loop, len(data) - loop)) content = line.rstrip().split(',') lat = float(content[0]) lon = float(content[1]) if (maxlat_2 >= lat >= minlat_2) and (maxlon_2 >= lon >= minlon_2): color_r = random.uniform(0, 1) color_b = random.uniform(0, 1) color_g = random.uniform(0, 1) plt.plot(lat, lon, '.', color=(color_r, color_g, color_b), linewidth=0.5) loop += 1 plt.xlabel('Latitude/°') plt.ylabel('Lontitude/°') plt.title(title) plt.grid(True) fname = combileFileName(dirs=dir, filename=fname) plt.savefig(fname) plt.clf() print('Drawing end')
def outputClassifyTxtResults(data, dir=r'classifytxtresults'): ''' 保存用户分类结果数据信息,以文本文件形式保存-v1.0 :param data: 用户分类结果数据信息 list[UserClassify] :param dir: 目录名 :return: ''' index = 1 for item in data: print('Output begin user similarity %d group' % index) filename = str(index) + '&' + str(len( item.similarityuser)) + '&' + item.user + '.txt' filename = combileFileName(dir, filename) outfile = open(filename, 'w') outfile.write(item.user) outfile.write('\n') for key in item.similarityuser.keys(): outfile.write(key) outfile.write('\n') outfile.close() index += 1
def loadUserData(similarity_user, dirs=r'rawsdata'): ''' 加载一组相似用户的地理位置数据 :param similarity_user: 一组相似用户类型为:list[user] :param dirs: 原始文件目录名 :return: 加载完一组相似用户地理位置数据类型为:dict{'user':list[TLocation]} ''' user_location = {} for item in similarity_user: filename = item + '.txt' print('begins %s' % filename) filename = combileFileName(dirs, filename) locations = [] for line in open(filename): content = line.rstrip().split(',') tlocation = TLocation() tlocation.lat = float(content[0]) tlocation.longti = float(content[1]) tlocation.timer = datetime.strptime(content[2], str_formate) locations.append(tlocation) user_location[item] = locations.copy() return user_location
def outputTxtResults(data, user='******', dir=r'showresults', filename=r'dbscanresult.txt'): ''' 将挖掘停留区域结果数据导出为文本文件,给画图程序提供源数据-v1.1 :param data: 挖掘停留区域或停留位置的结果集,类型为list[StayArea]或list[StayPoint] :param dir: 目录名 :param filename: 输出数据文件名 :return: ''' filename = user + '&' + str(len(data)) + '&' + filename filename = combileFileName(dir, filename) print('Output beginlocation') outfile = open(filename, 'w') print('Total StayArea|StayPoint center: %d ' % (len(data))) for item in data: print('StayArea|StayPoint [%d] has [%d] nerborhoods' % (data.index(item), len(item.nerborhoods))) outfile.write('Center') outfile.write(',') outfile.write(str(item.centerlat)) outfile.write(',') outfile.write(str(item.centerlon)) outfile.write('\n') for nerbor in item.nerborhoods: outfile.write('Nerborhood') outfile.write(',') outfile.write(str(nerbor.lat)) outfile.write(',') outfile.write(str(nerbor.longti)) outfile.write(',') outfile.write(str(nerbor.timer)) outfile.write('\n') outfile.close() print('Output endlocation')
def drawingStayArea(data, fname, dir='imgresults', title_1='Result for All StayArea Center(a)', title_2='Leftup Part(b)', title_3='Rightdown Part(c_1)', title_4='Rightdown Part(c_2)', minlat_1=31, maxlat_1=32, minlon_1=121, maxlon_1=122, minlat_2=39, maxlat_2=41, minlon_2=116, maxlon_2=117, minlat_3=39.8, maxlat_3=40.4, minlon_3=116.25, maxlon_3=116.45): ''' 画用户停留区域结果图,将中心点,详细点全部画于一张图上-v1.1 :param data: list[type,lat,longti,time]类型 :param fname: 保存结果文件名 :param dir: 保存结果目录名 :param title_1: 标题1 :param title_2: 标题2 :param title_3: 标题3 :param title_4: 标题4 :param minlat_1: 最小纬度1 :param maxlat_1: 最大纬度1 :param minlon_1: 最小经度1 :param maxlon_1: 最大经度1 :param minlat_2: 最小纬度2 :param maxlat_2: 最大纬度2 :param minlon_2: 最小经度2 :param maxlon_2: 最大经度2 :param minlat_3: 最小纬度3 :param maxlat_3: 最大纬度3 :param minlon_3: 最小经度3 :param maxlon_3: 最大经度3 :return: ''' gc.collect() fig = plt.figure() xlable = 'Latitude' ylable = 'Lontitude' skip_3 = 0 skip_4 = 0 ax_1 = fig.add_subplot(221, title=title_1, xlabel=xlable, ylabel=ylable, autoscale_on=True) ax_2 = fig.add_subplot(222, title=title_2, xlabel=xlable, ylabel=ylable, autoscale_on=True) ax_3 = fig.add_subplot(223, title=title_3, xlabel=xlable, ylabel=ylable, autoscale_on=True) ax_4 = fig.add_subplot(224, title=title_4, xlabel=xlable, ylabel=ylable, autoscale_on=True) loop = 1 color_index = 0 ret_color = [] all_num = len(data) if all_num >= 300000: skip_num = 6 elif 300000 > all_num >= 200000: skip_num = 5 elif 200000 > all_num >= 100000: skip_num = 4 elif 100000 > all_num >= 70000: skip_num = 2 else: skip_num = 1 for line in data: print('Drawing detail point [%d], left [%d]' % (loop, len(data) - loop)) content = line.rstrip().split(',') lat = float(content[1]) lon = float(content[2]) if content[0] == 'Center': color_r = random.uniform(0, 1) color_b = random.uniform(0, 1) color_g = random.uniform(0, 1) ret_color.append([color_r, color_g, color_b]) color_index += 1 ax_1.plot(lat, lon, 'o', color=(color_r, color_g, color_b)) if (maxlat_1 >= lat >= minlat_1) and (maxlon_1 >= lon >= minlon_1): ax_2.plot(lat, lon, '.', color=(color_r, color_g, color_b)) ax_2.text(lat, lon, 'C' + str(color_index)) elif (maxlat_2 >= lat >= minlat_2) and (maxlon_2 >= lon >= minlon_2): skip_3 = 0 ax_3.plot(lat, lon, '.', color=(color_r, color_g, color_b)) ax_3.text(lat, lon, 'C' + str(color_index)) if (maxlat_3 >= lat >= minlat_3) and (maxlon_3 >= lon >= minlon_3): skip_4 = 0 ax_4.plot(lat, lon, '.', color=(color_r, color_g, color_b)) ax_4.text(lat, lon, 'C' + str(color_index)) else: if (maxlat_1 >= lat >= minlat_1) and (maxlon_1 >= lon >= minlon_1): ax_2.plot(lat, lon, '.', color=(ret_color[color_index - 1][0], ret_color[color_index - 1][1], ret_color[color_index - 1][2])) elif (maxlat_2 >= lat >= minlat_2) and ( maxlon_2 >= lon >= minlon_2) and skip_3 % skip_num == 0: #and skip_3 % 2 ==0 ax_3.plot(lat, lon, '.', color=(ret_color[color_index - 1][0], ret_color[color_index - 1][1], ret_color[color_index - 1][2])) if (maxlat_3 >= lat >= minlat_3) and ( maxlon_3 >= lon >= minlon_3) and skip_4 % skip_num == 0: #and skip_4 % 2 ==0 ax_4.plot(lat, lon, '.', color=(ret_color[color_index - 1][0], ret_color[color_index - 1][1], ret_color[color_index - 1][2])) skip_3 += 1 skip_4 += 1 loop += 1 ax_1.grid(True) ax_2.grid(True) ax_3.grid(True) ax_4.grid(True) fname = combileFileName(dirs=dir, filename=fname) fig.tight_layout(pad=1.1) fig.set_figheight(6.5) fig.set_figwidth(12) fig.savefig(fname) fig.clf() print('Drawing end')
def drawingTrajectory(trajectorys, fname, type, dir='imgresults', title_1='All normal trajectory(a)', title_2='Leftup Part(b)', title_3='Rightdown Part(c_1)', title_4='Rightdown Part(c_2)', minlat_1=31, maxlat_1=32, minlon_1=121, maxlon_1=122, minlat_2=39, maxlat_2=41, minlon_2=116, maxlon_2=117, minlat_3=39.8, maxlat_3=40.4, minlon_3=116.25, maxlon_3=116.45): ''' 画用户停留区域结果图,将中心点,详细点全部画于一张图上-v1.1 :param trajectorys: list[type,lat,longti,time]类型 :param fname: 保存结果文件名 :param type: 轨迹类型,正常和异常 :param dir: 保存结果目录名 :param title_1: 标题1 :param title_2: 标题2 :param title_3: 标题3 :param title_4: 标题4 :param minlat_1: 最小纬度1 :param maxlat_1: 最大纬度1 :param minlon_1: 最小经度1 :param maxlon_1: 最大经度1 :param minlat_2: 最小纬度2 :param maxlat_2: 最大纬度2 :param minlon_2: 最小经度2 :param maxlon_2: 最大经度2 :param minlat_3: 最小纬度3 :param maxlat_3: 最大纬度3 :param minlon_3: 最小经度3 :param maxlon_3: 最大经度3 :return: ''' gc.collect() fig = plt.figure() xlable = 'Latitude' ylable = 'Lontitude' ax_1 = fig.add_subplot(111, title=title_1, xlabel=xlable, ylabel=ylable, autoscale_on=True) loop = 1 for trajectory in trajectorys: # if loop == 1: minlat_1 = trajectory.beginlocation.lat minlon_1 = trajectory.beginlocation.longti maxlat_1 = trajectory.endlocation.lat maxlon_1 = trajectory.endlocation.longti print('Drawing %s trajectory [%d], left [%d]' % (type, loop, len(trajectorys) - loop)) for point in trajectory.points: ax_1.plot(point.lat, point.longti, 'r.', alpha=0.2) # ax_1.text(point.lat, point.longti, str(trajectory.tid)) nerbors = trajectory.nerbors.copy() for nerbors_trajectory in nerbors: for point in nerbors_trajectory.points: if (maxlat_1 >= point.lat >= minlat_1) and (maxlon_1 >= point.longti >= minlon_1): ax_1.plot(point.lat, point.longti, 'k.') loop += 1 ax_1.grid(True) fname = combileFileName(dirs=dir, filename=fname) # fig.tight_layout(pad=1.1) # fig.set_figheight(6.5) # fig.set_figwidth(12) fig.savefig(fname) fig.clf() print('Drawing end')