コード例 #1
0
    def getPoi(self) :
        #---- Step 1 : Eliminate 0-weighted cell ------------------------------------------------------------#
        for key in self.weights.keys() :
            if (self.weights[key]["weight"]==0) : self.weights.pop(key, None)
        #----------------------------------------------------------------------------------------------------#
        
        keys=self.weights.keys()
        indexes=np.array(keys)
        positions=np.array([self.weights[key]["center"] for key in keys])
        weights=np.array([self.weights[key]["weight"] for key in keys])

        #---- Step 2 : Mode clustering of cells (meanshift) -------------------------------------------------#
        data=np.array([[position.longitude(),position.latitude()] for position in positions])
        ms = MeanShift(bandwidth=self.bandwidth,bin_seeding=True)
        ms.fit(data)
        labels = ms.labels_
        cluster_centers = ms.cluster_centers_
        labels_unique = np.unique(labels)
        n_clusters_ = len(labels_unique) - (1 if -1 in labels else 0)
        #----------------------------------------------------------------------------------------------------#

        #---- Step 3 : Creating the list of Poi -------------------------------------------------------------#   
        self.poi=[]
        infrequentVisits=[]
        for k in range(n_clusters_) :
            #center = cluster_centers[k]
            my_members = labels == k
            weightedPositions=zip(list(indexes[my_members]),list(positions[my_members]),list(weights[my_members]))
            maximum=max(weightedPositions,key=lambda element : element[2])
            poi=Poi(k,maximum[1].longitude(),maximum[1].latitude())
            
            temporaryVisits=[]
            for element in weightedPositions :
                for visit in self.weights[tuple(element[0])]["visits"] :
                    temporaryVisits.append(visit)
            temporaryVisits=sorted(temporaryVisits,key=lambda visit : visit.arrival)

            visits=[temporaryVisits[0]]
            for i in range(1,len(temporaryVisits)) :
                lastVisit=visits[-1]
                visit=temporaryVisits[i]
                if (visit.arrival<=lastVisit.departure) :
                    lastVisit.departure=max(lastVisit.departure,visit.departure)
                else : visits.append(visit)

            if (len(visits)>=self.freqThres) :    
                poi.setVisits(visits)
                self.poi.append(poi)
            else : infrequentVisits.extend(visits)

        if (infrequentVisits) : self.poi.append(Poi('I',float('nan'),float('nan'),sorted(infrequentVisits,key = lambda visit : visit.arrival)))
        #----------------------------------------------------------------------------------------------------#

        self.finalize(self.poi,mergeVisits=False)
        return self.poi
コード例 #2
0
    def getPoi(self):
        self.poi = []

        # ---- Step 1 : Get events near to each stop point ---------------------------------------------------#
        associatedVisits = []
        neighboorsServerAll = NearestNeighbors(radius=self.distanceThres, algorithm="auto", leaf_size=10)
        neighboorsServerAll.fit(np.array([event.position for event in self.events]))
        for i in range(len(self.stops)):
            associatedVisits.append(set(neighboorsServerAll.radius_neighbors(self.stops[i].position)[1][0]))
        # ----------------------------------------------------------------------------------------------------#

        # ---- Step 2 : Merge stop points onto poi and merge there visits ------------------------------------#
        aggregatedPOI = np.array([-1] * len(self.stops))
        numberOfPOI = 0
        for i in range(len(self.stops)):
            inIntersection = []
            for j in range(len(self.stops)):
                if associatedVisits[i] & associatedVisits[j]:
                    inIntersection.append(j)
            poiId = min(aggregatedPOI[inIntersection])
            if poiId == -1:
                numberOfPOI += 1
                poiId = numberOfPOI
            aggregatedPOI[inIntersection] = poiId
        listPoiandNeighboorsIndex = []
        for poiId in set(aggregatedPOI):
            members = self.stops[aggregatedPOI == poiId]
            indices = np.array(range(len(self.stops)))[aggregatedPOI == poiId]
            neighboors = set()
            for i in indices:
                neighboors |= associatedVisits[i]
            poi = Poi(
                poiId,
                sum([p.longitude() for p in members]) / len(members),
                sum([p.latitude() for p in members]) / len(members),
            )
            listPoiandNeighboorsIndex.append((poi, sorted(neighboors)))
        # ----------------------------------------------------------------------------------------------------#

        # ---- Step 3 : Creating the list of Poi -------------------------------------------------------------#
        infrequentVisits = []
        for item in listPoiandNeighboorsIndex:
            poi, neighborsIndex = item
            j = neighborsIndex[0]
            for i in range(1, len(neighborsIndex)):
                if neighborsIndex[i] > neighborsIndex[i - 1] + 1:
                    visit = Visit(-1, self.events[j].time, self.events[neighborsIndex[i - 1] + 1].time)
                    j = neighborsIndex[i]
                    if visit.duration() >= self.stayTimeThres:
                        poi.addVisit(visit)
            k = neighborsIndex[-1] + 1 if (neighborsIndex[-1] + 1 < len(self.events)) else neighborsIndex[-1]
            visit = Visit(-1, self.events[j].time, self.events[k].time)
            if visit.duration() >= self.stayTimeThres:
                poi.addVisit(visit)

            if poi.visits:
                if len(poi.visits) >= self.freqThres:
                    self.poi.append(poi)
                else:
                    infrequentVisits.extend(poi.visits)

        if infrequentVisits:
            self.poi.append(
                Poi("I", float("nan"), float("nan"), sorted(infrequentVisits, key=lambda visit: visit.arrival))
            )
        # ----------------------------------------------------------------------------------------------------#
        self.finalize(self.poi, mergeVisits=False)
        return self.poi