def temporalLocalityTemporalDistanceExample(lattice=NEW_YORK): distances = defaultdict(dict) for latticeObject in FileIO.iterateJsonFromFile(hashtagsLatticeGraphFile%('training_world','%s_%s'%(2,11))): if latticeObject['id']==lattice: latticeHashtagsSet = set(latticeObject['hashtags']) for neighborLattice, neighborHashtags in latticeObject['links'].iteritems(): distances[neighborLattice] = {} neighborHashtags = filterOutNeighborHashtagsOutside1_5IQROfTemporalDistance(latticeObject['hashtags'], neighborHashtags, findLag=False) neighborHashtagsSet = set(neighborHashtags) distances[neighborLattice]['similarity']=len(latticeHashtagsSet.intersection(neighborHashtagsSet))/float(len(latticeHashtagsSet.union(neighborHashtagsSet))) distances[neighborLattice]['temporalDistance']=np.mean([abs(latticeObject['hashtags'][k][0]-neighborHashtags[k][0]) for k in neighborHashtags if k in latticeObject['hashtags']])/(60.*60.) distances[neighborLattice]['geoDistance']=getHaversineDistanceForLids(latticeObject['id'].replace('_', ' '), neighborLattice.replace('_', ' ')) break dataPoints = [] ax = plt.subplot(111) for k, data in distances.iteritems(): dataPoints.append((getLocationFromLid(k.replace('_', ' ')), data['temporalDistance'])) points, colors = zip(*sorted(dataPoints, key=itemgetter(1))) sc = plotPointsOnWorldMap(points, blueMarble=False, bkcolor='#CFCFCF', cmap='RdPu', c=colors, lw = 0, alpha=1.0) plotPointsOnWorldMap([getLocationFromLid(lattice.replace('_', ' '))], blueMarble=False, bkcolor='#CFCFCF', c='#64FF1C', lw = 0) divider = make_axes_locatable(ax) plt.title('Average time difference from New York') cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(sc, cax=cax) # plt.show() plt.savefig('../images/temporalDistanceExample.png')
def _getDistances(): distances = {} for latticeObject in FileIO.iterateJsonFromFile(hashtagsLatticeGraphFile%('training_world','%s_%s'%(2,11))): latticeHashtagsSet = set(latticeObject['hashtags']) for neighborLattice, neighborHashtags in latticeObject['links'].iteritems(): key = '_'.join(sorted([latticeObject['id'], neighborLattice])) if key not in distances: distances[key] = {} neighborHashtags = filterOutNeighborHashtagsOutside1_5IQROfTemporalDistance(latticeObject['hashtags'], neighborHashtags, findLag=False) neighborHashtagsSet = set(neighborHashtags) distances[key]['similarity']=len(latticeHashtagsSet.intersection(neighborHashtagsSet))/float(len(latticeHashtagsSet.union(neighborHashtagsSet))) distances[key]['temporalDistance']=np.mean([abs(latticeObject['hashtags'][k][0]-neighborHashtags[k][0]) for k in neighborHashtags if k in latticeObject['hashtags']]) distances[key]['geoDistance']=getHaversineDistanceForLids(latticeObject['id'].replace('_', ' '), neighborLattice.replace('_', ' ')) return distances
def sharing_probability_examples(model_ids, kNoOfLocations = 3): tuo_target_location_and_target_location_label_and_tuo_target_nearby_location_and_nearby_location_label = [ ('29.7250_-97.1500', 'austin' ,[('32.6250_-96.4250', 'dallas'), ('29.0000_-97.8750', 'san_antonio'), ('29.7250_-94.9750','houston')]), ] target_location = '29.7250_-97.1500' for model_id in model_ids: tuo_location_and_tuo_neighbor_location_and_sharing_affinity_score \ = Experiments.load_tuo_location_and_tuo_neighbor_location_and_sharing_affinity_score(model_id) # all_locations = zip(*tuo_location_and_tuo_neighbor_location_and_sharing_affinity_score)[0] for target_location, target_location_label, tuo_target_nearby_location_and_nearby_location_label in \ tuo_target_location_and_target_location_label_and_tuo_target_nearby_location_and_nearby_location_label: for location, tuo_neighbor_location_and_sharing_affinity_score in \ tuo_location_and_tuo_neighbor_location_and_sharing_affinity_score: if location==target_location: mf_neighbor_location_to_sharing_affinity_score = dict(tuo_neighbor_location_and_sharing_affinity_score) print [( nearby_location_label, getHaversineDistanceForLids(target_nearby_location.replace('_', ' '), location.replace('_', ' ')), '%0.2f'%mf_neighbor_location_to_sharing_affinity_score[target_nearby_location] ) for target_nearby_location, nearby_location_label in tuo_target_nearby_location_and_nearby_location_label] print [(a, '%0.2f'%b)for a,b in tuo_neighbor_location_and_sharing_affinity_score[1:kNoOfLocations+1]]