def bin_into_days_by_local_time(self, trip_gj_list): """ While binning the trips into days, we really want to use local time. This is because that is what the user experienced. For example, consider a hypothetical user who travels from the West Coast to the East Coast of the US, leaving at 11pm local time, which would be 2am Eastern time. When the user looks at their timeline the next day, when they are on East Coast local time, they still want to see the trip to the airport as occurring on the previous day, not the same day, because that is when they experienced it. So, as corny as it seems, we are going to use the start and end formatted times, split them and extract the date part. :param trip_gj_list: List of trips :return: Map of date string -> trips that start or end on date """ ret_val = {} for trip_gj in trip_gj_list: trip = ecwt.Trip(trip_gj.properties) # TODO: Consider extending for both start and end day_string = BuiltinUserCacheHandler.get_local_day_from_fmt_time( trip) if day_string not in ret_val: ret_val[day_string] = [trip_gj] else: list_for_curr_day = ret_val[day_string] list_for_curr_day.append(trip_gj) logging.debug( "After binning, we have %s bins, of which %s are empty" % (len(ret_val), len([ds for ds, dl in ret_val.iteritems() if len(dl) == 0]))) return ret_val
def get_trips(user_id, time_query): curr_query = _get_ts_query(time_query) curr_query.update({"user_id": user_id}) trip_doc_cursor = edb.get_trip_new_db().find(curr_query).sort( time_query.timeType, pymongo.ASCENDING) # TODO: Fix "TripIterator" and return it instead of this list return [ecwt.Trip(doc) for doc in trip_doc_cursor]
def read_data(uuid=None, size=None, old=True): data = [] trip_db = edb.get_trip_db() if not old: trip_db = edb.get_trip_new_db() trips = trip_db.find({"user_id": uuid}) else: if uuid: trips = trip_db.find({'user_id': uuid, 'type': 'move'}) else: trips = trip_db.find({'type': 'move'}) for t in trips: try: trip = Trip.trip_from_json(t) except: continue if not (trip.trip_start_location and trip.trip_end_location and trip.start_time): continue data.append(trip) if size: if len(data) == size: break return data return [ecwt.Trip(trip) for trip in trips]
def testGetLocalDay(self): adt = arrow.get(pydt.datetime(2016, 1, 1, 9, 46, 0, 0)) test_dt = ecsdlq.get_local_date(adt.timestamp, "America/Los_Angeles") test_trip = ecwt.Trip({'start_local_dt': test_dt, 'start_fmt_time': adt.isoformat()}) test_handler = enuah.UserCacheHandler.getUserCacheHandler(self.testUserUUID1) self.assertEqual(test_handler.get_local_day_from_fmt_time(test_trip), "2016-01-01") self.assertEqual(test_handler.get_local_day_from_local_dt(test_trip), "2016-01-01")
def get_reps(self): self.reps = [] if not self.data: return for i, cluster in enumerate(self.clusters): logging.debug("Considering cluster %d = %s" % (i, cluster)) points = [[], [], [], []] # If this cluster has no points, we skip it if len(cluster) == 0: logging.info("Cluster %d = %s, has length %d, skipping" % (i, cluster, len(cluster))) continue for j, c in enumerate(cluster): logging.debug("Consider point %d = %s" % (j, c)) start_place = esda.get_entry(esda.CLEANED_PLACE_KEY, c.data.start_place) end_place = esda.get_entry(esda.CLEANED_PLACE_KEY, c.data.end_place) points[0].append(start_place.data.location["coordinates"][1]) # lat points[1].append(start_place.data.location["coordinates"][0]) # lng points[2].append(end_place.data.location["coordinates"][1]) # lat points[3].append(end_place.data.location["coordinates"][0]) # lng logging.debug("in representatives, endpoints have len = %s" % len(points)) centers = numpy.mean(points, axis=1) logging.debug("For cluster %d, centers are %s" % (i, centers)) t = ecwt.Trip({ "start_loc": gj.Point([centers[1], centers[0]]), "end_loc": gj.Point([centers[3], centers[2]]) }) a = ecwe.Entry.create_entry(c.user_id, "analysis/cleaned_trip", t) self.reps.append(a)
def setUp(self): # We need to access the database directly sometimes in order to # forcibly insert entries for the tests to pass. But we put the import # in here to reduce the temptation to use the database directly elsewhere. import emission.core.get_database as edb import uuid self.testUUID = uuid.uuid4() self.trips = json.load( open("emission/tests/data/smoothing_data/trip_list.txt"), object_hook=bju.object_hook) for trip in self.trips: trip["user_id"] = self.testUUID edb.get_trip_new_db().save(trip) self.trips = [ecwt.Trip(t) for t in self.trips] self.sections = json.load( open("emission/tests/data/smoothing_data/section_list.txt"), object_hook=bju.object_hook) for section in self.sections: section["user_id"] = self.testUUID edb.get_section_new_db().save(section) self.sections = [ecws.Section(s) for s in self.sections] self.ts = esta.TimeSeries.get_time_series(self.testUUID)
def get_aggregate_trips(time_query, box=None): curr_query = _get_ts_query(time_query) if box: curr_query.update({"start_loc": {"$geoWithin": {"$box": box}}}) curr_query.update({"end_loc": {"$geoWithin": {"$box": box}}}) trip_doc_cursor = edb.get_trip_new_db().find(curr_query).sort( time_query.timeType, pymongo.ASCENDING) return [ecwt.Trip(doc) for doc in trip_doc_cursor]
def testWrapTrip(self): testTripJSON = { '_id': bo.ObjectId("55d8c47b7d65cb39ee983c2d"), 'start_ts': 1436826360.200, 'start_fmt_time': '2015-07-13 15:26:00.200000-07:00', 'end_ts': 1436826360.493, 'end_fmt_time': '2015-07-13 15:26:00.493000-07:00', 'start_place': bo.ObjectId("55d8c47b7d65cb39ee983c2d"), 'end_place': bo.ObjectId("55d8c47b7d65cb39ee983c2d"), 'start_loc': {"coordinates": [-122, 37], "type": "Point"}, 'user_id': UUID('0763de67-f61e-3f5d-90e7-518e69793954') } trip = ecwt.Trip(testTripJSON) self.assertEqual(trip.get_id(), bo.ObjectId("55d8c47b7d65cb39ee983c2d")) self.assertEqual(trip.start_place, bo.ObjectId("55d8c47b7d65cb39ee983c2d")) self.assertEqual(trip.end_place, bo.ObjectId("55d8c47b7d65cb39ee983c2d")) self.assertTrue(isinstance(trip.start_loc, gj.Point))
def testGetLocalDay(self): test_dt = pydt.datetime(2016, 1, 1, 9, 46, 0, 0) test_trip = ecwt.Trip({'start_local_dt': test_dt, 'start_fmt_time': test_dt.isoformat()}) test_handler = enuah.UserCacheHandler.getUserCacheHandler(self.testUserUUID1) self.assertEqual(test_handler.get_local_day_from_fmt_time(test_trip), "2016-01-01") self.assertEqual(test_handler.get_local_day_from_local_dt(test_trip), "2016-01-01")
def get_trip(trip_id): """ Returns the trip for specified trip id. :rtype : emission.core.wrapper.Trip """ return ecwt.Trip(edb.get_trip_new_db().find_one({"_id": trip_id}))
def create_new_trip(user_id): _id = edb.get_trip_new_db().save({"user_id": user_id}) return ecwt.Trip({"_id": _id, "user_id": user_id})