def all_grids():
    mg = MongoDB()
    mg.connect()
    griddb = GridDB()
    print('querying grid volumes...')
    results = mg.group_by([{'$match': {'created_at': {'$gt': datetime.strptime('2012-10-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ'),
                                                      '$lt': datetime.strptime('2012-11-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ')}}}])   # print(results)
    griddb.add(results)

    ret = Grid.get_raw_pandas_ts(results, 'D')

    STL.seasonal_decomposition(ret)
 def filter_seasonal_outliers(tweets, aggregation):
     ts = Grid.get_raw_pandas_ts(tweets, aggregation)
     seasonal_ci = STL.seasonal_decomposition(ts)
     ret = []
     for step in ts:
         if seasonal_ci[0] <= step[1] <= seasonal_ci[1]:
             continue
         ret += step
     return ret
def single_grid(grid_id):
    mg = MongoDB()
    mg.connect()
    start_time = datetime.strptime('2012-10-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ')
    end_time = datetime.strptime('2012-11-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ')

    print('querying grid volumes...')
    # results = mg.group_by([{'$match': {'created_at': {'$gt': start_time, '$lt': end_time}, 'grid_id': grid_id}},
    #                       {'$group': {'_id': '$grid_id', 'count': {'$sum': 1}}}])
    results = mg.group_by([{'$match': {'created_at': {'$gt': datetime.strptime('2012-10-15T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ'),
                                                       '$lt': datetime.strptime('2012-11-15T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ')},
                                       'grid_id': grid_id}}])   # print(results)
    print(results)
    ret = Grid.get_raw_pandas_ts(results, 'H')
    # print('------------')
    # print(ret)
    print(STL.seasonal_decomposition(ret))
    results = mg.group_by(
        [
            {
                "$match": {
                    "created_at": {
                        "$gt": datetime.strptime("2012-10-15T20:00:02Z", "%Y-%m-%dT%H:%M:%SZ"),
                        "$lt": datetime.strptime("2012-10-22T20:00:02Z", "%Y-%m-%dT%H:%M:%SZ"),
                    }
                }
            }
        ]
    )  # print(results)

    # results = mg.group_by([{'$match': {'created_at': {'$gt': datetime.strptime('2012-10-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ'),
    #                                                   '$lt': datetime.strptime('2012-11-15T20:00:02Z', '%Y-%m-%dT%H:%M:%SZ')}, 'grid_id': '34013011500'}}])   # print(results)
    ret = Grid.get_raw_pandas_ts(results, "D")
    # print('ret: ', ret)
    # for result in Grid.get_ts(results, 'H'):
    # print(result[0])
    #     # print('result: ', result)
    #     for tup in result:
    #     obj = []
    # obj.append(result[0])
    # ret.append(result[1])
    #         # print(tup)
    #         obj['date'] = tup  #strftime('%Y-%m-%dT%H:%M:%SZ')
    #     ret.append(obj)
    STL.seasonal_decomposition(ret)
    print("grids initialized")
 def get_seasonal_outliers(tweets, aggregation):
     ts = Grid.get_raw_pandas_ts(tweets, aggregation)
     if len(ts) > 1:
         return STL.seasonal_decomposition(ts)
     else:
         return None