<commit_before>#!/usr/bin/env python

import argparse
import requests
import collections
import dateutil.parser

DIST_TO_STOP = 300


class ShapeInfo(object):
    def __init__(self,stop_idx=None,dist=None):
        if stop_idx is not None:
            assert dist,'Dist cannot be None if stop_idx is given'
        self.stop_idx = stop_idx
        self.dist = dist

    def __nonzero__(self):
        return self.stop_idx is not None

class Replayer(object):
    def __init__(self, gtfs_trip_id, device_id,server=None):
        self.gtfs_trip_id = gtfs_trip_id
        self.server = server or 'opentrain.hasadna.org.il'
        if device_id.startswith('dummy'):
            device_id = 'dummy_%s' % device_id
        self.device_id = device_id

    def do_get(self, url, params=None):
        final_url = 'http://%s%s' % (self.server, url)
        resp = requests.get(final_url, params=params)
        if not resp.ok:
            with open('/tmp/replay_error.html', 'w') as fh:
                fh.write(resp.content)
            raise Exception('failed in get %s , written to file:///tmp/replay_error.html' % resp.status_code)
        return resp.json()

    def calc_distance(self,latlon1,latlon2):
        """
        Calculate the great circle distance between two points
        on the earth (specified in decimal degrees)
        """
        from math import radians, cos, sin, asin, sqrt
        # convert decimal degrees to radians
        lat1,lon1 = latlon1
        lat2,lon2 = latlon2
        lat1 = radians(lat1)
        lon1 = radians(lon1)
        lat2 = radians(lat2)
        lon2 = radians(lon2)

        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a))
        km = 6367 * c
        return km * 1000


    def build_trip(self):
        self.trip_details = self.do_get('/api/1/trips/%s/details/'  % self.gtfs_trip_id)
        stop_times = self.trip_details['stop_times']
        shapes = self.trip_details['shapes']
        self.shape_infos = [None] * len(shapes)
        for shape_idx,shape in enumerate(shapes):
            dists = [self.calc_distance(shape,stop_time['stop']['latlon']) for stop_time in stop_times]
            val, idx = min((val, idx) for (idx, val) in enumerate(dists))
            if val < DIST_TO_STOP:
                self.shape_infos[shape_idx] = ShapeInfo(stop_idx=idx,dist=val)
            else:
                self.shape_infos[shape_idx] = ShapeInfo(stop_idx=None)

        print '# of stops = %s' % (len(stop_times))
        #for stop in stop_times:
        #    print stop
        self.check_shapes()
        #self.compute_shape_times()


    def check_shapes(self):
        print 'Checking shapes'<commit_msg>replay<commit_after>#!/usr/bin/env python

import argparse
import requests
import collections
from datetime import timedelta
import dateutil.parser

DIST_TO_STOP = 300


class ShapeInfo(object):
    def __init__(self,stop_idx=None,prev_stop_idx=None,dist=None):
        if stop_idx is not None:
            assert dist,'Dist cannot be None if stop_idx is given'
        else:
            assert prev_stop_idx is not None
        self.stop_idx = stop_idx
        self.dist = dist
        self.prev_stop_idx = None

    def __nonzero__(self):
        return self.stop_idx is not None

class Replayer(object):
    def __init__(self, gtfs_trip_id, device_id,server=None):
        self.gtfs_trip_id = gtfs_trip_id
        self.server = server or 'opentrain.hasadna.org.il'
        if device_id.startswith('dummy'):
            device_id = 'dummy_%s' % device_id
        self.device_id = device_id

    def do_get(self, url, params=None):
        final_url = 'http://%s%s' % (self.server, url)
        resp = requests.get(final_url, params=params)
        if not resp.ok:
            with open('/tmp/replay_error.html', 'w') as fh:
                fh.write(resp.content)
            raise Exception('failed in get %s , written to file:///tmp/replay_error.html' % resp.status_code)
        return resp.json()

    def calc_distance(self,latlon1,latlon2):
        """
        Calculate the great circle distance between two points
        on the earth (specified in decimal degrees)
        """
        from math import radians, cos, sin, asin, sqrt
        # convert decimal degrees to radians
        lat1,lon1 = latlon1
        lat2,lon2 = latlon2
        lat1 = radians(lat1)
        lon1 = radians(lon1)
        lat2 = radians(lat2)
        lon2 = radians(lon2)

        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a))
        km = 6367 * c
        return km * 1000


    def build_trip(self):
        self.trip_details = self.do_get('/api/1/trips/%s/details/'  % self.gtfs_trip_id)
        stop_times = self.trip_details['stop_times']
        shapes = self.trip_details['shapes']
        self.shape_infos = [None] * len(shapes)
        last_stop_idx = -1
        for shape_idx,shape in enumerate(shapes):
            dists = [self.calc_distance(shape,stop_time['stop']['latlon']) for stop_time in stop_times]
            val, idx = min((val, idx) for (idx, val) in enumerate(dists))
            if val < DIST_TO_STOP:
                self.shape_infos[shape_idx] = ShapeInfo(stop_idx=idx,dist=val)
                last_stop_idx = idx
            else:
                assert last_stop_idx >= 0
                self.shape_infos[shape_idx] = ShapeInfo(stop_idx=None,prev_stop_idx=last_stop_idx)

        print '# of stops = %s' % (len(stop_times))
        #for stop in stop_times:
        #    print stop
        self.check_shapes()
        self.compute_shape_times()

    def compute_shape_times(self):
        stop_times = self.trip_details['stop_times']
        for idx,st in enumerate(stop_times):
            sis = [si for si in self.shape_infos is si.stop_idx == idx]
            start_time = st.arrival_time - timedelta(second=30)
            offset = 0
            for si in sis:
                si.time = start_time + timedelta(second=offset)
                offset += 60.0 / len(sis)
            sis


    def check_shapes(self):
        print 'Checking shapes'
{'fraction_non_alphanumeric': 0.10446570972886762, 'fraction_numerical': 0.013716108452950558, 'mean_word_length': 2.2358101135190918, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '2742034', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2001, 'n_words': 477}