Beispiel #1
0
def normalize(stations):
    extract = tb.extract_element
    normalized = lambda station: {
        "name": extract(station, "name"),
        "address": extract(station, "name"),
        "lat": float(extract(station, "lat")),
        "lon": float(extract(station, "long")),
        "status": "OPEN" if extract(station, "locked") == "false" else "CLOSED",
        "bikes": int(extract(station, "nbbikes")),
        "stands": int(extract(station, "nbemptydocks")),
        "update": tb.epoch_to_datetime(int(extract(station, "latestupdatetime")), divisor=1000).isoformat(),
    }
    return [normalized(station) for station in stations.find_all("station")]
Beispiel #2
0
def normalize(stations):
    normalized = lambda station: {
        'name': station['name'],
        'address': station['address'],
        'lat': station['position']['lat'],
        'lon': station['position']['lng'],
        'status': station['status'],
        'bikes': station['available_bikes'],
        'stands': station['available_bike_stands'],
        'update': tb.epoch_to_datetime(station['last_update'],
                                       divisor=1000).isoformat()
    }
    return [normalized(station) for station in stations]
Beispiel #3
0
def normalize(stations):
    extract = tb.extract_element
    normalized = lambda station: {
        'name': extract(station, 'name'),
        'address': extract(station, 'name'),
        'lat': float(extract(station, 'lat')),
        'lon': float(extract(station, 'long')),
        'status': 'OPEN' if extract(station, 'locked') == 'false' else 'CLOSED',
        'bikes': int(extract(station, 'nbbikes')),
        'stands': int(extract(station, 'nbemptydocks')),
        'update': tb.epoch_to_datetime(int(extract(station,
                                                   'latestupdatetime')),
                                       divisor=1000).isoformat()
    }
    return [normalized(station) for station
            in stations.find_all('station')]
Beispiel #4
0
def choose(situation, target, distance, mode, stationFirst, nbCandidates=5):
    """
    Choose the best station around the arrival and then define a trip
    between the departure and the station.
    """
    city = situation["city"]
    people = situation["people"]
    time = tb.convert_time(situation["time"])
    # Will convert the positions to (lat, lon) if they are textual addresses
    departure = geography.convert_to_point(city, situation["departure"])
    arrival = geography.convert_to_point(city, situation["arrival"])
    # Find the close stations with MongoDB
    stations = [station for station in query.close_points(city, arrival, number=nbCandidates)]
    # Get the distances to the stations

    candidates = geography.compute_distances_manual(arrival, stations, distance)

    # Sort the stations by distance
    candidates.sort(key=lambda station: station["duration"])
    # Find an appropriate solution through the sorted candidates
    trip = False
    for candidate in candidates:
        # Calculate what time it would be when reaching the candidate station
        forecast = tb.epoch_to_datetime(time + candidate["duration"])
        # Extract features from the forecasted time
        variables = munging.temporal_features(forecast)
        features = [variables["hour"], variables["minute"], variables["weekday"]]
        # Check if the prediction is satisfying
        settings = tb.read_json("common/settings.json")
        method = eval(settings["learning"]["method"])
        # Make a prediction
        prediction = method.predict(features, target, city, candidate["_id"])
        bias = tb.read_json("common/settings.json")["learning"]["bias"]
        if target == "bikes":
            bias *= -1
        if prediction + bias >= people:
            stationPosition = list(reversed(candidate["p"]))
            if stationFirst is False:
                trip = tb.reshape(mode, departure, stationPosition)
            else:
                trip = tb.reshape(mode, stationPosition, departure)
            break
    return trip