<commit_before># -*- coding: utf-8 -*-
''' stravanova
taking GPX files and storing some of their attributes as JSON
the JSON format compresses the info at the expense of being non-standard:
[lat, lon, timestamp (epoch, UTC), speed (m/s)]

    routes = {
        'cow-watchin': [
            [123.456, 789.012, 1343461324, 4.523]
            , [123.467, 789.023, 1343461324, 2.160]
        ]
        , 'quadruple-century': [
            [345.456, 78.012, 1343464324, 4.523]
            , [345.467, 78.023, 1343463324, 2.160]
        ]
    }

'''
from math import radians, cos, sin, asin, sqrt, atan2, pi
import os
import time

import gpxpy
import numpy


class Condenser():
    ''' saving gpx files as json
    strips out all but latitude, longitude and time
    '''
    def __init__(self, gpx_paths, binning=False):
        self.file_paths = gpx_paths
        self.default_lat_lon_precision = 5
        self.default_time_binning = binning

    def parse(self, **kwargs):
        lat_lon_precision = kwargs.pop('lat_lon_precision', 
                self.default_lat_lon_precision)
        time_binning = kwargs.pop('time_binning',
                self.default_time_binning)

        self.filenames = [os.path.basename(f).split('.')[0] 
                for f in self.file_paths]

        point_data = {}
        for path in self.file_paths:
            gpx = gpxpy.parse(open(path, 'r'))

            filename = os.path.basename(path).split('.')[0]

            # extract the points
            for track in gpx.tracks:
                for segment in track.segments:
                    point_data[filename] = [p for p in segment.points]

        parsed_data = {}
        for filename in point_data:
            path = []
            for p in point_data[filename]:
                lat = p.latitude
                lon = p.longitude
                timestamp = time.mktime(p.time.timetuple())

                # calculate speeds
                if not path:
                    # append the first point
                    path.append([round(lat, lat_lon_precision),
                        round(lon, lat_lon_precision),
                        timestamp,
                        None])
                    continue
                # grab the previous point
                previous = path[-1]
                speed = self._speed(previous, [lat, lon, timestamp])
                if not speed:
                    continue

                # inject the calculated speed
                path.append([round(lat, lat_lon_precision),
                    round(lon, lat_lon_precision),
                    timestamp,
                    speed])

            if time_binning:
                # create a point for every N seconds
                N = 4
                # initialize the path with the first point
                binned_path = [[path[0][0], path[0][1]]]
                # array of all timestamps
                timestamps = [p[2] for p in path]

                # start the clock with the first timestamp
                tick = timestamps[0]
                while tick <= timestamps[-1]:
                    tick += N
                    index = numpy.searchsorted(timestamps, tick)

                    if index >= len(path):
                        break

                    # get previous and next points around this tick value
                    pp = path[index - 1]
                    np = path[index]

                    # find the bearing and distance
                    bearing = self._bearing([pp[0], pp[1]], [np[0], np[1]])
                    speed = self._speed(pp, np)
                    distance = speed * float(N)

                    # generate a point based on the average bearing and speed
                    # 'origin' set to the previously calculated point
                    # traveling N seconds from that point
                    binned_path.append(self.destination_point(<commit_msg>reduces bin size -- will parameterize someday<commit_after># -*- coding: utf-8 -*-
''' stravanova
taking GPX files and storing some of their attributes as JSON
the JSON format compresses the info at the expense of being non-standard:
[lat, lon, timestamp (epoch, UTC), speed (m/s)]

    routes = {
        'cow-watchin': [
            [123.456, 789.012, 1343461324, 4.523]
            , [123.467, 789.023, 1343461324, 2.160]
        ]
        , 'quadruple-century': [
            [345.456, 78.012, 1343464324, 4.523]
            , [345.467, 78.023, 1343463324, 2.160]
        ]
    }

'''
from math import radians, cos, sin, asin, sqrt, atan2, pi
import os
import time

import gpxpy
import numpy


class Condenser():
    ''' saving gpx files as json
    strips out all but latitude, longitude and time
    '''
    def __init__(self, gpx_paths, binning=False):
        self.file_paths = gpx_paths
        self.default_lat_lon_precision = 5
        self.default_time_binning = binning

    def parse(self, **kwargs):
        lat_lon_precision = kwargs.pop('lat_lon_precision', 
                self.default_lat_lon_precision)
        time_binning = kwargs.pop('time_binning',
                self.default_time_binning)

        self.filenames = [os.path.basename(f).split('.')[0] 
                for f in self.file_paths]

        point_data = {}
        for path in self.file_paths:
            gpx = gpxpy.parse(open(path, 'r'))

            filename = os.path.basename(path).split('.')[0]

            # extract the points
            for track in gpx.tracks:
                for segment in track.segments:
                    point_data[filename] = [p for p in segment.points]

        parsed_data = {}
        for filename in point_data:
            path = []
            for p in point_data[filename]:
                lat = p.latitude
                lon = p.longitude
                timestamp = time.mktime(p.time.timetuple())

                # calculate speeds
                if not path:
                    # append the first point
                    path.append([round(lat, lat_lon_precision),
                        round(lon, lat_lon_precision),
                        timestamp,
                        None])
                    continue
                # grab the previous point
                previous = path[-1]
                speed = self._speed(previous, [lat, lon, timestamp])
                if not speed:
                    continue

                # inject the calculated speed
                path.append([round(lat, lat_lon_precision),
                    round(lon, lat_lon_precision),
                    timestamp,
                    speed])

            if time_binning:
                # create a point for every N seconds
                N = 1
                # initialize the path with the first point
                binned_path = [[path[0][0], path[0][1]]]
                # array of all timestamps
                timestamps = [p[2] for p in path]

                # start the clock with the first timestamp
                tick = timestamps[0]
                while tick <= timestamps[-1]:
                    tick += N
                    index = numpy.searchsorted(timestamps, tick)

                    if index >= len(path):
                        break

                    # get previous and next points around this tick value
                    pp = path[index - 1]
                    np = path[index]

                    # find the bearing and distance
                    bearing = self._bearing([pp[0], pp[1]], [np[0], np[1]])
                    speed = self._speed(pp, np)
                    distance = speed * float(N)

                    # generate a point based on the average bearing and speed
                    # 'origin' set to the previously calculated point
                    # traveling N seconds from that point
                    binned_path.append(self.destination_point(
{'fraction_non_alphanumeric': 0.08839217678435357, 'fraction_numerical': 0.030734061468122936, 'mean_word_length': 1.3987206823027718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518737', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2016, 'n_words': 606}