<s> import copy <EOL> import json <EOL> import math <EOL> import os <EOL> import pygame <EOL> import random <EOL> import sys <EOL> import time <EOL> from collections import namedtuple <EOL> from datetime import timedelta , datetime <EOL> from operator import itemgetter <EOL> from state_abbrev import STATE_MAP <EOL> pygame . init ( ) <EOL> pygame . font . init ( ) <EOL> font = pygame . font . SysFont ( "<STR_LIT>" , <NUM_LIT:12> ) <EOL> bold_font = pygame . font . SysFont ( "<STR_LIT>" , <NUM_LIT:12> , bold = True ) <EOL> iphone_dims = <NUM_LIT> , <NUM_LIT> <EOL> event_height = <NUM_LIT> <EOL> window_height = iphone_dims [ <NUM_LIT:1> ] <EOL> target_box_width = <NUM_LIT:30> <EOL> target_box_padding = <NUM_LIT:15> <EOL> right_margin = target_box_width + target_box_padding <EOL> min_tick_x_offset = <NUM_LIT> <EOL> draw_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> line_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> circle_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> backdrop_color = pygame . Color ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> text_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> place_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> cur_time_color = pygame . Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> max_places = <NUM_LIT:15> <EOL> HOUR = <NUM_LIT> * <NUM_LIT> <EOL> DAY = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> MONTH = DAY * <NUM_LIT:30> <EOL> YEAR = DAY * <NUM_LIT> <EOL> class VFTime ( object ) : <EOL> """<STR_LIT>""" <EOL> _MIN_TOTAL_SECS = iphone_dims [ <NUM_LIT:1> ] <EOL> def __init__ ( self , min_time , max_time , reverse_x = True , reverse_y = True ) : <EOL> """<STR_LIT>""" <EOL> self . _total_secs = max_time - min_time <EOL> self . _min_time = min_time <EOL> self . _max_time = max_time <EOL> self . _cur_time = max_time <EOL> self . _width = ( iphone_dims [ <NUM_LIT:0> ] - right_margin ) <EOL> self . _height = iphone_dims [ <NUM_LIT:1> ] <EOL> self . _roc_max = float ( max_time - min_time ) / self . _height <EOL> self . _roc_min = float ( VFTime . _MIN_TOTAL_SECS ) / self . _height <EOL> self . _roc_slope = float ( VFTime . _MIN_TOTAL_SECS - self . _total_secs ) / ( self . _width * self . _height ) <EOL> self . _reverse_x = reverse_x <EOL> self . _reverse_y = reverse_y <EOL> self . _active = False <EOL> def Start ( self , start_pos ) : <EOL> """<STR_LIT>""" <EOL> self . _last_pos = self . _TransformPos ( start_pos ) <EOL> self . _cur_time = self . _min_time + self . _last_pos [ <NUM_LIT:1> ] * self . _roc_max <EOL> self . _active = True <EOL> def Stop ( self ) : <EOL> self . _active = False <EOL> def IsActive ( self ) : <EOL> return self . _active <EOL> def AdjustTime ( self , touch_pos ) : <EOL> """<STR_LIT>""" <EOL> if not self . _active : <EOL> return <EOL> new_pos = self . _TransformPos ( touch_pos ) <EOL> delta_time = self . _IntegrateTime ( new_pos ) <EOL> self . _cur_time = self . _EnforceBounds ( self . _cur_time + delta_time , new_pos ) <EOL> self . _last_pos = new_pos <EOL> def ComputeTimeAtPos ( self , pos , log = False ) : <EOL> """<STR_LIT>""" <EOL> new_pos = self . _TransformPos ( pos ) <EOL> delta_time = self . _IntegrateTime ( new_pos , log ) <EOL> return self . _cur_time + delta_time <EOL> def GetMaxInterval ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _min_time , self . _max_time ) <EOL> def GetInterval ( self ) : <EOL> """<STR_LIT>""" <EOL> interval = self . _height * ( self . _roc_max + self . _roc_slope * self . _last_pos [ <NUM_LIT:0> ] ) <EOL> y_ratio = float ( self . _last_pos [ <NUM_LIT:1> ] ) / float ( self . _height ) <EOL> return ( self . _cur_time - y_ratio * interval , <EOL> self . _cur_time + ( <NUM_LIT:1.0> - y_ratio ) * interval ) <EOL> def GetPos ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _UntransformPos ( ) <EOL> def GetTime ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cur_time <EOL> def GetDatetime ( self ) : <EOL> """<STR_LIT>""" <EOL> return datetime . fromtimestamp ( self . _cur_time ) <EOL> def _IntegrateTime ( self , new_pos , log = False ) : <EOL> """<STR_LIT>""" <EOL> x_delta = float ( new_pos [ <NUM_LIT:0> ] - self . _last_pos [ <NUM_LIT:0> ] ) <EOL> y_delta = ( new_pos [ <NUM_LIT:1> ] - self . _last_pos [ <NUM_LIT:1> ] ) <EOL> if y_delta == <NUM_LIT:0.0> : <EOL> return <NUM_LIT:0.0> <EOL> m = x_delta / y_delta <EOL> a = self . _roc_max <EOL> b = self . _roc_slope <EOL> c = float ( self . _last_pos [ <NUM_LIT:1> ] ) <EOL> def _ComputeIntegral ( y ) : <EOL> return <NUM_LIT:0.5> * y * ( <NUM_LIT:2> * a + b * ( <NUM_LIT:2> * self . _last_pos [ <NUM_LIT:0> ] + m * ( y - <NUM_LIT:2> * self . _last_pos [ <NUM_LIT:1> ] ) ) ) <EOL> if log : <EOL> max_diff = ( new_pos [ <NUM_LIT:1> ] - self . _last_pos [ <NUM_LIT:1> ] ) * ( self . _roc_max + self . _roc_slope * min ( new_pos [ <NUM_LIT:0> ] , self . _last_pos [ <NUM_LIT:0> ] ) ) <EOL> print '<STR_LIT>' % ( self . _max_time - self . _min_time ) <EOL> print '<STR_LIT>' % ( x_delta , y_delta , <NUM_LIT:1> / m ) <EOL> print '<STR_LIT>' % max_diff <EOL> print '<STR_LIT>' % _ComputeIntegral ( new_pos [ <NUM_LIT:1> ] ) <EOL> print '<STR_LIT>' % _ComputeIntegral ( self . _last_pos [ <NUM_LIT:1> ] ) <EOL> print '<STR_LIT>' % ( _ComputeIntegral ( new_pos [ <NUM_LIT:1> ] ) - _ComputeIntegral ( self . _last_pos [ <NUM_LIT:1> ] ) ) <EOL> return _ComputeIntegral ( new_pos [ <NUM_LIT:1> ] ) - _ComputeIntegral ( self . _last_pos [ <NUM_LIT:1> ] ) <EOL> def _EnforceBounds ( self , cur_time , new_pos ) : <EOL> """<STR_LIT>""" <EOL> interval = self . _height * ( self . _roc_max + self . _roc_slope * new_pos [ <NUM_LIT:0> ] ) <EOL> y_ratio = float ( new_pos [ <NUM_LIT:1> ] ) / float ( self . _height ) <EOL> if cur_time - y_ratio * interval < self . _min_time : <EOL> cur_time = self . _min_time + y_ratio * interval <EOL> if cur_time + ( <NUM_LIT:1.0> - y_ratio ) * interval >= self . _max_time : <EOL> cur_time = self . _max_time - ( <NUM_LIT:1.0> - y_ratio ) * interval <EOL> return cur_time <EOL> def _UntransformPos ( self ) : <EOL> """<STR_LIT>""" <EOL> pos = [ self . _last_pos [ <NUM_LIT:0> ] + right_margin , self . _last_pos [ <NUM_LIT:1> ] ] <EOL> if self . _reverse_x : <EOL> pos [ <NUM_LIT:0> ] = iphone_dims [ <NUM_LIT:0> ] - pos [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> if self . _reverse_y : <EOL> pos [ <NUM_LIT:1> ] = iphone_dims [ <NUM_LIT:1> ] - pos [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> return pos <EOL> def _TransformPos ( self , pos ) : <EOL> """<STR_LIT>""" <EOL> new_pos = copy . copy ( pos ) <EOL> if self . _reverse_x : <EOL> new_pos [ <NUM_LIT:0> ] = iphone_dims [ <NUM_LIT:0> ] - new_pos [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> if self . _reverse_y : <EOL> new_pos [ <NUM_LIT:1> ] = iphone_dims [ <NUM_LIT:1> ] - new_pos [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> new_pos [ <NUM_LIT:0> ] = min ( self . _width , max ( <NUM_LIT:0> , new_pos [ <NUM_LIT:0> ] - right_margin ) ) <EOL> new_pos [ <NUM_LIT:1> ] = min ( self . _height , max ( <NUM_LIT:0> , int ( new_pos [ <NUM_LIT:1> ] ) ) ) <EOL> return new_pos <EOL> def DimensionToPixel ( dimension ) : <EOL> return int ( ( <NUM_LIT:1.0> * dimension * window_height ) / iphone_dims [ <NUM_LIT:1> ] ) <EOL> def DimensionsToPixels ( dimensions ) : <EOL> return [ DimensionToPixel ( x ) for x in dimensions ] <EOL> def PixelToDimension ( pixel ) : <EOL> return int ( ( <NUM_LIT:1.0> * pixel * iphone_dims [ <NUM_LIT:1> ] ) / window_height ) <EOL> def PixelsToDimensions ( pixels ) : <EOL> return [ PixelToDimension ( x ) for x in pixels ] <EOL> window_dimensions = [ d for d in DimensionsToPixels ( iphone_dims ) ] <EOL> window = pygame . display . set_mode ( window_dimensions , pygame . DOUBLEBUF | pygame . HWSURFACE | pygame . RESIZABLE ) <EOL> pygame . display . set_caption ( '<STR_LIT>' ) <EOL> def GetSurface ( ) : <EOL> return pygame . display . get_surface ( ) <EOL> def ClearSurface ( ) : <EOL> GetSurface ( ) . fill ( backdrop_color ) <EOL> def DrawTargetBoxes ( ) : <EOL> box = [ iphone_dims [ <NUM_LIT:0> ] - target_box_width - target_box_padding , <EOL> target_box_padding , target_box_width , iphone_dims [ <NUM_LIT:1> ] - target_box_padding * <NUM_LIT:2> ] <EOL> pygame . draw . rect ( GetSurface ( ) , draw_color , DimensionsToPixels ( box ) , <NUM_LIT:1> ) <EOL> def DrawLabel ( label , x , y , color , font , transparency , bold = False , border = False , left_justify = False ) : <EOL> color = pygame . Color ( * [ int ( i + transparency * ( j - i ) ) for i , j in zip ( color , backdrop_color ) ] ) <EOL> offset = DimensionsToPixels ( [ x , y ] ) <EOL> ts = bold_font . render ( label , True , color ) if bold else font . render ( label , True , color ) <EOL> if y > iphone_dims [ <NUM_LIT:1> ] - ts . get_height ( ) : <EOL> offset [ <NUM_LIT:1> ] -= ( ts . get_height ( ) + <NUM_LIT:2> ) <EOL> else : <EOL> offset [ <NUM_LIT:1> ] += <NUM_LIT:2> <EOL> if left_justify : <EOL> offset [ <NUM_LIT:0> ] = min ( iphone_dims [ <NUM_LIT:0> ] , offset [ <NUM_LIT:0> ] ) <EOL> else : <EOL> offset [ <NUM_LIT:0> ] = max ( <NUM_LIT:0> , offset [ <NUM_LIT:0> ] - ts . get_width ( ) ) <EOL> GetSurface ( ) . blit ( ts , offset ) <EOL> if border : <EOL> pygame . draw . rect ( GetSurface ( ) , color , <EOL> [ offset [ <NUM_LIT:0> ] - <NUM_LIT:2> , offset [ <NUM_LIT:1> ] - <NUM_LIT:2> , ts . get_width ( ) + <NUM_LIT:4> , ts . get_height ( ) + <NUM_LIT:4> ] , <NUM_LIT:1> ) <EOL> def DrawLine ( line , color , transparency ) : <EOL> color = pygame . Color ( * [ int ( i + transparency * ( j - i ) ) for i , j in zip ( color , backdrop_color ) ] ) <EOL> tx_line = [ DimensionsToPixels ( pt ) for pt in line ] <EOL> pygame . draw . aaline ( GetSurface ( ) , color , tx_line [ <NUM_LIT:0> ] , tx_line [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> def EnforceBounds ( val , min_val , max_val ) : <EOL> val = max ( val , min_val ) <EOL> return min ( val , max_val ) <EOL> def GetNextMonth ( date ) : <EOL> if date . month == <NUM_LIT:12> : <EOL> return date . replace ( year = date . year + <NUM_LIT:1> , month = <NUM_LIT:1> , day = <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> else : <EOL> return date . replace ( month = date . month + <NUM_LIT:1> , day = <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> def DrawGuides ( vf_time ) : <EOL> """<STR_LIT>""" <EOL> pos = vf_time . GetPos ( ) <EOL> cur_time = vf_time . GetTime ( ) <EOL> interval = vf_time . GetInterval ( ) <EOL> interval_size = interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] <EOL> max_interval = vf_time . GetMaxInterval ( ) <EOL> max_interval_size = max_interval [ <NUM_LIT:1> ] - max_interval [ <NUM_LIT:0> ] <EOL> def _DrawGuideLine ( timestamp , fmt , transparency = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> left_ratio = float ( timestamp - interval [ <NUM_LIT:0> ] ) / interval_size <EOL> left_y = int ( iphone_dims [ <NUM_LIT:1> ] * ( <NUM_LIT:1.0> - left_ratio ) ) <EOL> right_ratio = float ( timestamp - max_interval [ <NUM_LIT:0> ] ) / ( max_interval [ <NUM_LIT:1> ] - max_interval [ <NUM_LIT:0> ] ) <EOL> right_y = int ( iphone_dims [ <NUM_LIT:1> ] * ( <NUM_LIT:1.0> - right_ratio ) ) <EOL> line = [ ( pos [ <NUM_LIT:0> ] , left_y ) , ( iphone_dims [ <NUM_LIT:0> ] , right_y ) ] <EOL> DrawLine ( line , line_color , <NUM_LIT:0.0> ) <EOL> DrawLabel ( datetime . fromtimestamp ( timestamp ) . strftime ( fmt ) , <EOL> iphone_dims [ <NUM_LIT:0> ] , right_y , text_color , font , <NUM_LIT:0.0> ) <EOL> def _DrawHorizontalGuideLine ( timestamp , fmt , transparency = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> left_ratio = float ( timestamp - interval [ <NUM_LIT:0> ] ) / interval_size <EOL> left_y = int ( iphone_dims [ <NUM_LIT:1> ] * ( <NUM_LIT:1.0> - left_ratio ) ) <EOL> right_ratio = float ( timestamp - max_interval [ <NUM_LIT:0> ] ) / ( max_interval [ <NUM_LIT:1> ] - max_interval [ <NUM_LIT:0> ] ) <EOL> right_y = int ( iphone_dims [ <NUM_LIT:1> ] * ( <NUM_LIT:1.0> - right_ratio ) ) <EOL> line = [ ( iphone_dims [ <NUM_LIT:0> ] - <NUM_LIT> , right_y ) , ( iphone_dims [ <NUM_LIT:0> ] , right_y ) ] <EOL> DrawLine ( line , line_color , <NUM_LIT:0.0> ) <EOL> DrawLabel ( datetime . fromtimestamp ( timestamp ) . strftime ( fmt ) , <EOL> iphone_dims [ <NUM_LIT:0> ] - <NUM_LIT> , right_y , text_color , font , <NUM_LIT:0.0> ) <EOL> [ _DrawHorizontalGuideLine ( x , '<STR_LIT>' ) for x in interval ] <EOL> _DrawHorizontalGuideLine ( cur_time , '<STR_LIT>' ) <EOL> interval = max_interval <EOL> interval_size = max_interval [ <NUM_LIT:1> ] - max_interval [ <NUM_LIT:0> ] <EOL> def _DrawIntervalTick ( timestamp , fmt , transparency = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> label = datetime . fromtimestamp ( timestamp ) . strftime ( fmt ) <EOL> ratio = float ( timestamp - interval [ <NUM_LIT:0> ] ) / interval_size <EOL> x = iphone_dims [ <NUM_LIT:0> ] <EOL> y = int ( iphone_dims [ <NUM_LIT:1> ] * ( <NUM_LIT:1.0> - ratio ) ) <EOL> DrawLabel ( label , x , y , text_color , font , transparency ) <EOL> start = datetime . fromtimestamp ( interval [ <NUM_LIT:0> ] ) <EOL> end = datetime . fromtimestamp ( interval [ <NUM_LIT:1> ] ) <EOL> fade_interval = [ <NUM_LIT> , <NUM_LIT:4> ] <EOL> if interval_size < DAY * fade_interval [ <NUM_LIT:0> ] : <EOL> transparency = max ( <NUM_LIT:0.0> , ( float ( interval_size - DAY * fade_interval [ <NUM_LIT:1> ] ) / DAY ) / ( fade_interval [ <NUM_LIT:0> ] - fade_interval [ <NUM_LIT:1> ] ) ) <EOL> cur = start . replace ( hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> while cur < end : <EOL> _DrawIntervalTick ( time . mktime ( cur . timetuple ( ) ) , '<STR_LIT>' , transparency ) <EOL> cur = cur + timedelta ( days = <NUM_LIT:1> ) <EOL> fade_interval = [ <NUM_LIT> , <NUM_LIT:12> ] <EOL> if interval_size < MONTH * fade_interval [ <NUM_LIT:0> ] : <EOL> transparency = max ( <NUM_LIT:0.0> , ( float ( interval_size - MONTH * fade_interval [ <NUM_LIT:1> ] ) / MONTH ) / ( fade_interval [ <NUM_LIT:0> ] - fade_interval [ <NUM_LIT:1> ] ) ) <EOL> cur = start . replace ( day = <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> while cur < end : <EOL> _DrawIntervalTick ( time . mktime ( cur . timetuple ( ) ) , '<STR_LIT>' , transparency ) <EOL> cur = GetNextMonth ( cur ) <EOL> if interval_size >= MONTH * <NUM_LIT:12> : <EOL> cur = start . replace ( month = <NUM_LIT:1> , day = <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> while cur < end : <EOL> _DrawIntervalTick ( time . mktime ( cur . timetuple ( ) ) , '<STR_LIT>' ) <EOL> cur = cur . replace ( year = cur . year + <NUM_LIT:1> , month = <NUM_LIT:1> , day = <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> def GetPlace ( pm ) : <EOL> locality = pm . get ( '<STR_LIT>' , None ) <EOL> state = pm . get ( '<STR_LIT:state>' , None ) <EOL> country = pm . get ( '<STR_LIT>' , None ) <EOL> if country == '<STR_LIT>' : <EOL> if locality and state : <EOL> return '<STR_LIT>' % ( locality , STATE_MAP [ state ] ) <EOL> elif locality and country : <EOL> return '<STR_LIT>' % ( locality , pm [ '<STR_LIT>' ] ) <EOL> return locality or state or pm . get ( '<STR_LIT>' , None ) <EOL> def CombinePlaces ( places , time_resolution ) : <EOL> """<STR_LIT>""" <EOL> sorted_places = sorted ( [ { '<STR_LIT>' : GetPlace ( p [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : p [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : p [ '<STR_LIT>' ] } for p in places ] , key = itemgetter ( '<STR_LIT>' ) ) <EOL> place_list = [ ] <EOL> def _GetAvgTimestamp ( p ) : <EOL> return p [ '<STR_LIT>' ] / p [ '<STR_LIT>' ] <EOL> def _MaybeCombinePlace ( p ) : <EOL> if place_list : <EOL> last_place = place_list [ - <NUM_LIT:1> ] <EOL> if p [ '<STR_LIT>' ] == last_place [ '<STR_LIT>' ] and abs ( p [ '<STR_LIT>' ] - _GetAvgTimestamp ( last_place ) ) < time_resolution : <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> last_place [ attr ] += p [ attr ] <EOL> return <EOL> place_list . append ( p ) <EOL> for p in sorted_places : <EOL> _MaybeCombinePlace ( p ) <EOL> for p in place_list : <EOL> p [ '<STR_LIT>' ] = _GetAvgTimestamp ( p ) <EOL> return place_list <EOL> def GetCircle ( pos ) : <EOL> """<STR_LIT>""" <EOL> if pos [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> pos [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> a , b = float ( pos [ <NUM_LIT:0> ] ) , <NUM_LIT:0.0> <EOL> c , d = <NUM_LIT:0.0> , iphone_dims [ <NUM_LIT:1> ] / <NUM_LIT> <EOL> e , f = float ( pos [ <NUM_LIT:0> ] ) , float ( iphone_dims [ <NUM_LIT:1> ] ) <EOL> k = ( <NUM_LIT:0.5> ) * ( ( a ** <NUM_LIT:2> + b ** <NUM_LIT:2> ) * ( e - c ) + ( c ** <NUM_LIT:2> + d ** <NUM_LIT:2> ) * ( a - e ) + ( e ** <NUM_LIT:2> + f ** <NUM_LIT:2> ) * ( c - a ) ) / ( b * ( e - c ) + d * ( a - e ) + f * ( c - a ) ) <EOL> h = ( <NUM_LIT:0.5> ) * ( ( a ** <NUM_LIT:2> + b ** <NUM_LIT:2> ) * ( f - d ) + ( c ** <NUM_LIT:2> + d ** <NUM_LIT:2> ) * ( b - f ) + ( e ** <NUM_LIT:2> + f ** <NUM_LIT:2> ) * ( d - b ) ) / ( a * ( f - d ) + c * ( b - f ) + e * ( d - b ) ) <EOL> rsqr = ( a - h ) ** <NUM_LIT:2> + ( b - k ) ** <NUM_LIT:2> <EOL> theta = math . acos ( ( ( a - h ) * ( e - h ) + ( b - k ) * ( f - k ) ) / rsqr ) <EOL> return ( ( h , k ) , math . sqrt ( rsqr ) , theta ) <EOL> def RenderEvents ( vf_time , places ) : <EOL> """<STR_LIT>""" <EOL> c , r , theta = GetCircle ( vf_time . GetPos ( ) ) <EOL> def _GetArcCoords ( radians ) : <EOL> """<STR_LIT>""" <EOL> return [ c [ <NUM_LIT:0> ] + r * math . cos ( radians ) , c [ <NUM_LIT:1> ] + r * math . sin ( radians ) ] <EOL> def _GetTimestamp ( radians ) : <EOL> """<STR_LIT>""" <EOL> return vf_time . ComputeTimeAtPos ( _GetArcCoords ( radians ) ) <EOL> def _ApproximateAngle ( timestamp , x , x1 ) : <EOL> """<STR_LIT>""" <EOL> def f ( x ) : <EOL> return _GetTimestamp ( x ) - timestamp <EOL> error = <NUM_LIT:1> <EOL> x2 = <NUM_LIT:0> <EOL> for attempt in xrange ( <NUM_LIT:20> ) : <EOL> d = f ( x1 ) - f ( x ) <EOL> if d < error : <EOL> return x1 <EOL> x2 = x1 - f ( x1 ) * ( x1 - x ) / d <EOL> x = x1 <EOL> x1 = x2 <EOL> dx = x1 - x <EOL> assert False , '<STR_LIT>' <EOL> def _ComputeDecay ( distance , half_distance ) : <EOL> return math . exp ( - math . log ( <NUM_LIT> ) * distance / half_distance ) <EOL> interval = vf_time . GetInterval ( ) <EOL> combined_places = CombinePlaces ( places , float ( interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] ) / <NUM_LIT> ) <EOL> start_angle = math . pi - theta / <NUM_LIT:2> <EOL> end_angle = math . pi + theta / <NUM_LIT:2> <EOL> text_height = <NUM_LIT> <EOL> half_distance = <NUM_LIT> <EOL> places = [ ] <EOL> for p in combined_places : <EOL> place_angle = _ApproximateAngle ( p [ '<STR_LIT>' ] , start_angle , end_angle ) <EOL> if place_angle > start_angle and place_angle < end_angle : <EOL> p [ '<STR_LIT:x>' ] , p [ '<STR_LIT:y>' ] = _GetArcCoords ( place_angle ) <EOL> places . append ( p ) <EOL> for p in places : <EOL> y = p [ '<STR_LIT:y>' ] <EOL> sub_places = [ sp for sp in places if sp [ '<STR_LIT:y>' ] > ( y - text_height ) and sp [ '<STR_LIT:y>' ] < ( y + text_height ) ] <EOL> max_count = max ( [ sp [ '<STR_LIT>' ] for sp in sub_places ] ) <EOL> total_weight = <NUM_LIT:0> <EOL> for sp in sub_places : <EOL> total_weight += ( float ( sp [ '<STR_LIT>' ] ) / max_count ) * _ComputeDecay ( abs ( sp [ '<STR_LIT:y>' ] - y ) , half_distance ) <EOL> p [ '<STR_LIT>' ] = ( float ( p [ '<STR_LIT>' ] ) / max_count ) / total_weight <EOL> if places : <EOL> min_weight = min ( p [ '<STR_LIT>' ] for p in places ) <EOL> max_weight = max ( p [ '<STR_LIT>' ] for p in places ) <EOL> sorted_places = sorted ( places , key = itemgetter ( '<STR_LIT>' ) ) <EOL> for p in sorted_places : <EOL> if min_weight == max_weight : <EOL> opacity = <NUM_LIT:1.0> <EOL> else : <EOL> opacity = ( p [ '<STR_LIT>' ] - min_weight ) / ( max_weight - min_weight ) <EOL> DrawLabel ( '<STR_LIT:%s>' % ( p [ '<STR_LIT>' ] ) , p [ '<STR_LIT:x>' ] , p [ '<STR_LIT:y>' ] , place_color , <EOL> font , <NUM_LIT:1.0> - opacity , left_justify = True ) <EOL> def ProcessMotion ( vf_time ) : <EOL> """<STR_LIT>""" <EOL> last_pos = None <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == pygame . QUIT or event . type == pygame . KEYDOWN : <EOL> return sys . exit ( <NUM_LIT:0> ) <EOL> if vf_time . IsActive ( ) : <EOL> if event . type == pygame . MOUSEMOTION : <EOL> if event . buttons [ <NUM_LIT:0> ] : <EOL> last_pos = event . pos <EOL> elif event . type == pygame . MOUSEBUTTONUP : <EOL> if event . button == <NUM_LIT:1> : <EOL> vf_time . Stop ( ) <EOL> else : <EOL> if event . type == pygame . MOUSEBUTTONDOWN : <EOL> if event . button == <NUM_LIT:1> : <EOL> pos = PixelsToDimensions ( event . pos ) <EOL> x , y = pos <EOL> if x > iphone_dims [ <NUM_LIT:0> ] - target_box_width - target_box_padding and x < iphone_dims [ <NUM_LIT:0> ] - target_box_padding and y > target_box_padding and y < iphone_dims [ <NUM_LIT:1> ] - target_box_padding : <EOL> vf_time . Start ( pos ) <EOL> if last_pos : <EOL> vf_time . AdjustTime ( PixelsToDimensions ( last_pos ) ) <EOL> def main ( ) : <EOL> vp_data = [ ] <EOL> with open ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) , '<STR_LIT:r>' ) as f : <EOL> vp_data = json . load ( f ) <EOL> vp_data = [ vp for vp in vp_data if '<STR_LIT>' in vp ] <EOL> for index in xrange ( len ( vp_data ) ) : <EOL> vp = vp_data [ index ] <EOL> vp [ '<STR_LIT:index>' ] = index <EOL> vp [ '<STR_LIT>' ] = index * event_height <EOL> vp [ '<STR_LIT>' ] = datetime . fromtimestamp ( vp [ '<STR_LIT>' ] ) <EOL> max_date = vp_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> min_date = vp_data [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> vf_time = VFTime ( vp_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , vp_data [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> levels = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> level = levels [ - <NUM_LIT:1> ] <EOL> display_places = False <EOL> while True : <EOL> ClearSurface ( ) <EOL> ProcessMotion ( vf_time ) <EOL> if not vf_time . IsActive ( ) : <EOL> DrawTargetBoxes ( ) <EOL> else : <EOL> label = vf_time . GetDatetime ( ) . strftime ( '<STR_LIT>' ) <EOL> DrawGuides ( vf_time ) <EOL> interval = vf_time . GetInterval ( ) <EOL> start = interval [ <NUM_LIT:0> ] - ( interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> end = interval [ <NUM_LIT:1> ] + ( interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> places = [ vp for vp in vp_data if vp [ '<STR_LIT>' ] > start and vp [ '<STR_LIT>' ] < end ] <EOL> if vf_time . GetPos ( ) [ <NUM_LIT:0> ] < <NUM_LIT> : <EOL> display_places = True <EOL> if display_places : <EOL> RenderEvents ( vf_time , vp_data ) <EOL> pygame . display . flip ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
