Esempio n. 1
0
    def test_load_stream_empty(self, mongo_mock):
        """Should return None if stream is empty"""
        mongo_mock.db.streams.find_one.return_value = {
            '_id': '123',
            'activity_id': 456,
            'data': [],
            'type': 'velocity_smooth'
        }

        self.assertIsNone(load_stream(mongo_mock, activity_id=456, stream_type='velocity_smooth'))
Esempio n. 2
0
    def test_load_stream_duplicate_timestamps(self, mongo_mock):
        """Should use the last observation in case of non-unique timestamps"""
        mongo_mock.db.streams.find_one.return_value = {
            '_id': '123',
            'activity_id': 456,
            'data': [101, 101, 103],
            'type': 'velocity_smooth'
        }

        result = load_stream(mongo_mock, activity_id=456, stream_type='velocity_smooth')
        self.assertEqual(len(result), 2)
        self.assertItemsEqual(result.velocity_smooth, [101, 103])
Esempio n. 3
0
    def test_load_stream_by_activity_id_and_type(self, mongo_mock):
        """Should load single stream as 1-column dataframe, indexed by time"""
        mongo_mock.db.streams.find_one.return_value = {
            '_id': '123',
            'activity_id': 456,
            'data': [101, 102, 103],
            'type': 'velocity_smooth'
        }

        result = load_stream(mongo_mock, activity_id=456, stream_type='velocity_smooth')
        self.assertIs(type(result), pd.DataFrame)
        self.assertEqual(list(result.columns.values), ['velocity_smooth'])
        self.assertEqual(list(result.velocity_smooth.values), [101, 102, 103])
        self.assertEqual(list(result.index.values), [101, 102, 103])
Esempio n. 4
0
    def max_value_maintained_for_n_minutes(self, activity_df, measurement='heartrate', window_size=5):
        all_max_values = [float('NaN')] * len(activity_df)

        for i in range(len(activity_df)):
            stream_df = load_stream(self.database_driver, activity_df.strava_id[i], '%s' % measurement)
            if stream_df is not None:
                min_index = stream_df.index.min()
                all_max_values[i] = np.nanmax(
                    [stream_df[measurement].loc[subtract_n_minutes(second,
                                                                   minutes=window_size,
                                                                   minimum_value=min_index):second].min()
                     for second in stream_df.index])

        activity_df['max_%s_%d_minutes' % (measurement, window_size)] = all_max_values
        return activity_df
Esempio n. 5
0
    def __total_deviation(self, activity_id):
        stream_df = load_stream(self.database_driver, activity_id, 'latlng')

        if stream_df is not None:
            stream_df = lat_long_to_x_y(stream_df)
            stream_df = smooth(stream_df, smooth_colname='x')
            stream_df = smooth(stream_df, smooth_colname='y')
            stream_df = derivative(stream_df, derivative_colname='x_smooth')
            stream_df = derivative(stream_df, derivative_colname='y_smooth')
            stream_df = rolling_similarity(stream_df, cosine_similarity, 'dx_smooth_dt', 'dy_smooth_dt')

            try:
                return np.nansum([cosine_to_deviation(cos)
                                  for cos in stream_df.cosine_similarity_dx_smooth_dt_dy_smooth_dt])
            except ValueError:
                logging.warning('Failed to compute route deviation for activity %d, returning NaN' % activity_id)
                return np.nan
        else:
            logging.warning('No gps stream available for activity %d, returning NaN' % activity_id)
            return np.nan
Esempio n. 6
0
    def test_load_stream_non_existing(self, mongo_mock):
        """Should return None if stream does not exist"""
        mongo_mock.db.streams.find_one.return_value = None

        self.assertIsNone(load_stream(mongo_mock, activity_id=000, stream_type='non_existing'))