コード例 #1
0
def get_device_details(device_id, tenant):
    """Returns a device's details given the ID

    Parameters
    ----------
    device_id : str
        The channel ID of the device
    owner: str
        The owner of the device

    Returns
    -------
    lat : float
        Latitude coordinate of the device's location
    lon: float
        Longitude coordinate of the device's location
    name: str
        Name of the device
    """
    db = connect_mongo(tenant, db_host=configuration.MONGO_URI_DEVICE_REGISTRY)
    query = {'channelID': device_id}
    projection = {
        '_id': 0,
        'latitude': 1,
        'longitude': 1,
        'name': 1,
        'channelID': 1
    }
    records = list(db.devices.find(query, projection))
    lat, lon, name = records[0]['latitude'], records[0]['longitude'], records[
        0]['name']
    return lat, lon, name
コード例 #2
0
ファイル: main.py プロジェクト: airqo-platform/AirQo-api
def predict_model(m, tenant, airqloud, aq_id, poly, x1, x2, y1, y2):
    '''
    Makes the predictions and stores them in a database
    '''
    time = datetime.now().replace(microsecond=0, second=0,
                                  minute=0).timestamp() / 3600

    longitudes = np.linspace(x1, x2, 100)
    latitudes = np.linspace(y1, y2, 100)
    locations = np.meshgrid(longitudes, latitudes)
    locations_flat = np.c_[locations[0].flatten(), locations[1].flatten()]

    df = pd.DataFrame(locations_flat, columns=['longitude', 'latitude'])
    df['point_exists'] = df.apply(lambda row: point_in_polygon(row, poly),
                                  axis=1)
    new_df = df[df.point_exists == 'True']
    new_df.drop('point_exists', axis=1, inplace=True)
    new_df.reset_index(drop=True, inplace=True)

    new_array = np.asarray(new_df)
    pred_set = np.c_[new_array, np.full(new_array.shape[0], time)]
    mean, var = m.predict_f(pred_set)

    means = mean.numpy().flatten()
    variances = var.numpy().flatten()
    std_dev = np.sqrt(variances)
    interval = 1.96 * std_dev

    result = []
    result_builder = {
        'airqloud': airqloud,
        'airqloud_id': aq_id,
        'created_at': datetime.now()
    }
    values = []
    for i in range(pred_set.shape[0]):
        values.append({
            'latitude': new_array[i][1],
            'longitude': new_array[i][0],
            'predicted_value': means[i],
            'variance': variances[i],
            'interval': interval[i]
        })
    result_builder['values'] = values
    result.append(result_builder)

    db = connect_mongo(tenant)
    collection = db['gp_predictions']

    if collection.count_documents({'airqloud': airqloud}) != 0:
        collection.delete_many({'airqloud': airqloud})

    collection.insert_many(result)

    return result
コード例 #3
0
ファイル: models.py プロジェクト: airqo-platform/AirQo-api
 def get_events(self, start_date, end_date):
     tenant = self.tenant
     db = connect_mongo(tenant)
     return db.events.aggregate([{
         "$match": {
             "values.time": {
                 "$gte": start_date,
                 "$lt": end_date
             }
         }
     }, {
         "$unwind": "$values"
     }, {
         "$replaceRoot": {
             "newRoot": "$values"
         }
     }, {
         "$project": {
             "pm2_5": 1,
             "pm10": 1,
             "no2": 1,
             "site_id": {
                 "$toString": "$site_id"
             }
         }
     }, {
         "$group": {
             "_id": "$site_id",
             "reading": {
                 "$push": {
                     "pm2_5": "$pm2_5.value",
                     "pm10": "$pm10.value",
                     "no2": "$no2.value",
                 }
             }
         }
     }])
コード例 #4
0
ファイル: models.py プロジェクト: airqo-platform/AirQo-api
 def save_exceedance(self, records):
     tenant = self.tenant
     db = connect_mongo(tenant)
     return db.exceedances.insert(records)
コード例 #5
0
from datetime import datetime
from google.cloud import bigquery
import gcsfs
import numpy as np
import pandas as pd
import joblib
from joblib import Parallel, delayed
from config import connect_mongo, configuration

db = connect_mongo()


def query_prediction_data(test_lag_last_date_hour):
    client = bigquery.Client()
    sql_query = """SELECT created_at ,channel_id,pm2_5
        FROM `airqo-250220.thingspeak.clean_feeds_pms`
        WHERE created_at >= CAST({0} AS DATETIME)"""
    last_date = "'" + test_lag_last_date_hour + "'"
    sql_query = sql_query.format(last_date)
    job_config = bigquery.QueryJobConfig()
    job_config.use_legacy_sql = False

    df = client.query(sql_query, job_config=job_config).to_dataframe()
    df['created_at'] = pd.to_datetime(df['created_at'],
                                      format='%Y-%m-%d %H:%M:%S')
    return df


def get_lag_features(df_tmp, TARGET_COL, N_HRS_BACK, SEQ_LEN):

    df_tmp = df_tmp.sort_values(by='created_at')
コード例 #6
0
ファイル: models.py プロジェクト: airqo-platform/AirQo-api
 def _connect(self):
     return connect_mongo(self.tenant, 'device_registry')
コード例 #7
0
ファイル: models.py プロジェクト: airqo-platform/AirQo-api
 def _connect(self):
     return connect_mongo(self.tenant, 'device_monitoring')