def goThroughCandidateDB(self): """Go through candidate event db and classify whatever is left""" ei = EventInterface(self.candidate_db, self.candidate_collection) ei_classified = EventInterface(self.classified_event_db, self.classified_event_collection) cnt = 0 # consider past 2 hours for merge low_bound = str(int(getCurrentStampUTC()) - 60 * 60 * 2) condition = {'created_time':{ '$gte': low_bound}} for e in ei.getAllDocuments(condition=condition): logging.warning("Classifying %d-th candidate event..." % cnt) e = Event(e) cnt += 1 region = Region(e.getRegion()) corpus = self.all_corpus[region.getKey()] ef = BaseFeatureProduction(e, corpus) prob = self.clf.classify(ef.extractFeatures()) if ei_classified.getEventByID(e.getID()) is not None: if prob > 0.5: print 'already in front end collection, merge it' ei_classified.addEvent(e) else: print 'after merge it becomes none event, delete it' ei_classified.deleteEventByID(e.getID()) else: if prob > 0.5: print 'new events find in collection but not in front end , add it' ei_classified.addEvent(e)
def getEventByID(self, event_id): event = self.ei.getEventByID(event_id) event = Event(event) event.selectOnePhotoForOneUser() event_dic = event.toJSON() event_dic['_id'] = str(event_dic['_id']) return json.dumps(event_dic)
def fireAlarm(self): prediction = self.getNearestPrediction() self._getFiftenMiniutesPhotos() if prediction is None: #print 'None data for this region: details as follow' #self.region.display() #print 'time:' ,self.cur_time return else: print 'Data!' mu = float(prediction['mu'])/4.0 std = float(prediction['std'])/4.0 time_stamp = prediction['time'] zscore = (self.current_value - mu)*1.0/std if zscore > 3: e = Event() e.setPredictedValues(mu, std) e.setZscore(zscore) e.setRegion(self.region) e.setCreatedTime(self.cur_time) e.setActualValue(self.current_value) for p in self.photos: e.addPhoto(p) #print 'current value ',4.0*self.current_value, ' predict = ',mu*4.0,' std = ',std*4.0 ei = EventInterface( ) ei.setCollection(self.candidate_collection) print e.getEarliestPhotoTime(),e.getLatestPhotoTime() #print e.toJSON()['region'] #ei.addEvent(e) ei.addEventWithoutMerge(e)
def fireAlarm(self): self._getFiftenMiniutesPhotos() #get current_value cur_hour = datetime.utcfromtimestamp(float(self.cur_time)).hour #print 'cur_hour = ',cur_hour, 'time = ',self.cur_time mu = self.means[cur_hour] / 4.0 std = self.stds[cur_hour] / 4.0 #print 'mu is ',mu, 'std is ',std, 'cur_value = ',self.current_value zscore = (self.current_value - mu) * 1.0 / std if zscore > 3 and self.current_value >= 8: e = Event() e.setPredictedValues(mu, std) e.setZscore(zscore) e.setRegion(self.region) e.setCreatedTime(self.cur_time) e.setActualValue(self.current_value) for p in self.photos: e.addPhoto(p) ei = EventInterface() ei.setCollection(self.candidate_collection) #print datetime.utcfromtimestamp(float(e.getEarliestPhotoTime())), datetime.utcfromtimestamp(float(e.getLatestPhotoTime())) #print e.getEarliestPhotoTime(),e.getLatestPhotoTime() #print e.toJSON()['region'] ei.addEvent(e)
def fireAlarm(self): prediction = self.getNearestPrediction() self._getFiftenMiniutesPhotos() if prediction is None: print 'None data for this region: details as follow' self.region.display() print 'time:', self.cur_time return mu = float(prediction['mu']) / 4.0 std = float(prediction['std']) / 4.0 time_stamp = prediction['time'] zscore = (self.current_value - mu) * 1.0 / std if zscore > 3: e = Event() e.setPredictedValues(mu, std) e.setZscore(zscore) e.setRegion(self.region) e.setCreatedTime(self.cur_time) e.setActualValue(self.current_value) for p in self.photos: e.addPhoto(p) #print 'current value ',4.0*self.current_value, ' predict = ',mu*4.0,' std = ',std*4.0 ei = EventInterface() ei.setCollection(self.candidate_collection) print e.getEarliestPhotoTime(), e.getLatestPhotoTime() #print e.toJSON()['region'] #ei.addEvent(e) ei.addEventWithoutMerge(e)
def fireAlarm(self): self._getFiftenMiniutesPhotos() #get current_value cur_hour = datetime.utcfromtimestamp(float(self.cur_time)).hour #print 'cur_hour = ',cur_hour, 'time = ',self.cur_time mu = self.means[cur_hour]/4.0 std = self.stds[cur_hour]/4.0 #print 'mu is ',mu, 'std is ',std, 'cur_value = ',self.current_value zscore = (self.current_value - mu)*1.0/std if zscore > 3 and self.current_value>=8: e = Event() e.setPredictedValues(mu, std) e.setZscore(zscore) e.setRegion(self.region) e.setCreatedTime(self.cur_time) e.setActualValue(self.current_value) for p in self.photos: e.addPhoto(p) ei = EventInterface( ) ei.setCollection(self.candidate_collection) #print datetime.utcfromtimestamp(float(e.getEarliestPhotoTime())), datetime.utcfromtimestamp(float(e.getLatestPhotoTime())) #print e.getEarliestPhotoTime(),e.getLatestPhotoTime() #print e.toJSON()['region'] ei.addEvent(e)