-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace_event.py
1055 lines (923 loc) · 37 KB
/
race_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
race_event.py
Class file for managing race events. These classes
include:
racer: A class for holding the information for a racer
heat: A class for holding a group of racers who are compeating against
each other. Heats can be any size, and each race may involve racers from
multiple heats.
race: A class for holding the information for a single race where one or
more racers go down the track.
event: A class for holding all racers, heats, and races that comprise a
single event.
Generally, the user will create an instance of an event, and populate that
event from a text file.
Copyright [2019] [Lee R. Burchett]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import numpy as np
import yaml
from typing import List
from pdflatex import PDFLaTeX
from typing import Iterable
default_heat_name = "No_Heat"
mc_sheet_header = r"""\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[landscape]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[table]{xcolor}
\begin{document}
\rowcolors{2}{gray!25}{white}"""
mc_table_footer = r"""\end{tabular}
\end{document}"""
current_car_number = 1
def next_car_number():
global current_car_number
out = current_car_number
if current_car_number == 12:
current_car_number = 14
else:
current_car_number += 1
return out
# CLASS STUFF
class Racer:
global default_heat_name
def __init__(self,
car_number=0,
name="No_Name",
rank="No_Rank",
heat_name=default_heat_name,
heat_index=-1,
n_lanes=4,
car_status=None):
self.name = name
self.rank = rank
self.n_lanes = n_lanes
self.heat_name = heat_name
self.index_in_heat = heat_index
self.race_times = np.zeros(self.n_lanes)
self.race_counts = np.zeros(self.n_lanes)
self.race_plan_nums = np.zeros(self.n_lanes)
self.race_log_nums = np.zeros(self.n_lanes)
self.race_positions = np.zeros(self.n_lanes)
if car_number > 0:
self.car_number = car_number
elif heat_name == "Empty":
self.car_number = 0
else:
self.car_number = next_car_number()
self.hist = {}
if car_status is None:
self.car_status = {
'passed_weight': [False, 'less than 5 oz.'],
'passed_length': [False, 'less than 7"'],
'passed_height': [False, 'less than 3 1/2"'],
'passed_underbody_clearance': [False, 'greater than 3/8"'],
'passed_width': [False, 'less than 2 3/4"'],
'wheel_diameter': [False, 'greater than 1.170"'],
'wheel_base': [False, 'less than 4 3/4" (axle to axle)'],
'passed_nose': [False, 'no notch'],
'questions': {
'made_this_year': False,
'use_kit_nail': False,
'use_official_wheel': False,
'no_liquid_lubricant': False,
'no_loose_materials': False,
'no_wheel_bearings': False,
'no_wheel_washers': False,
'no_solid_axles': False,
'no_springs_or_shocks': False,
'no_removal_of_wheel_tread': False,
'no_rounding_of_outside_wheel_edge': False,
'no_rounding_of_inside_wheel_edge': False,
'no_rounding_of_the_inside_wheel_hub': False,
'no_bore_or_cone_for_the_wheel_hub': False,
'no_dome_on_outside_hub': False,
'no_wheel_reshaping': False,
'no_nail_groove': False,
'no_graphite_paint_on_nail': False
},
'notes': ''
}
else:
self.car_status = car_status
def to_dict(self):
return {
'name': self.name,
'rank': self.rank,
'car_number': self.car_number
}
def from_dict(self, dict):
if 'name' in dict.keys():
self.name = dict['name']
if 'rank' in dict.keys():
self.rank = dict['rank']
if 'car_number' in dict.keys():
self.car_number = dict['car_number']
if 'race_times' in dict.keys():
self.race_times = dict['race_times']
if 'race_counts' in dict.keys():
self.race_counts = dict['race_counts']
if 'race_plan_nums' in dict.keys():
self.race_plan_nums = dict['race_plan_nums']
if 'race_log_nums' in dict.keys():
self.race_log_nums = dict['race_log_nums']
if 'race_positions' in dict.keys():
self.race_positions = dict['race_positions']
if 'heat_name' in dict.keys():
self.heat_name = dict['heat_name']
if 'heat_index' in dict.keys():
self.index_in_heat = dict['heat_index']
if 'car_status' in dict.keys():
self.car_status = dict['car_status']
def chip(self):
chip = {"text": "{}\n#{}:{}".format(self.name, self.car_number, self.heat_name),
"font": ("Serif", 16)}
return chip
def mc_sheet_label(self):
return r"\#" + "{} {}".format(self.car_number, self.name)
def set_heat(self, heat_name, heat_index):
self.clear_races() # Must do this BEFORE setting the new heat name!
self.heat_name = heat_name
self.heat_index = heat_index
def post_result(self, lane_idx, race_log_num, race_plan_num, time,
count, position):
self.race_log_nums[lane_idx] = race_log_num
self.race_plan_nums[lane_idx] = race_plan_num
self.race_times[lane_idx] = time
self.race_counts[lane_idx] = count
self.race_positions[lane_idx] = position
def get_average(self):
if any(self.race_times > 0.0):
return np.mean(self.race_times[self.race_times > 0.0])
else:
return 0.0
def get_worst(self):
return np.max(self.race_times)
def get_best(self):
if any(self.race_times > 0.0):
return np.min(self.race_times[self.race_times > 0.0])
else:
return 0.0
def save_heat(self):
self.hist[self.heat_name] = [self.race_log_nums, self.race_plan_nums,
self.race_times, self.race_positions]
def clear_races(self):
if self.get_worst() > 0.0:
self.save_heat()
self.race_times = np.zeros(self.n_lanes)
self.race_log_nums = np.zeros(self.n_lanes)
self.race_plan_nums = np.zeros(self.n_lanes)
self.race_positions = np.zeros(self.n_lanes)
def passed_inspection(self):
for key in self.car_status.keys():
if key == 'questions' or key == 'notes' or self.car_status[key]:
continue
return False
for key in self.car_status['questions'].keys():
if self.car_status['questions'][key]:
continue
return False
return True
class Heat:
def __init__(self,
name=default_heat_name,
racers: List[Racer] = [],
ability_rank=-1):
self.name = name
self.racers = racers
for ri, racer in enumerate(racers):
racer.set_heat(name, ri)
# self.races = []
self.ability_rank = ability_rank
def add_racer(self, racer):
if racer.heat_name != self.name:
raise ValueError("Unable to add a racer to '{}' because their heat is '{}'".format(
self.name, racer.heat_name
))
for existing_racer in self.racers:
if racer.name == existing_racer.name:
raise ValueError(f"A racer named '{racer.name}' already exists in the {self.name} heat.")
idx = len(self.racers)
self.racers.append(racer)
racer.set_heat(self.name, idx)
def remove_racer(self, racer=None, racer_name=None):
if racer is not None:
for racer_idx, existing_racer in enumerate(self.racers):
if existing_racer is racer:
self.racers.pop(racer_idx)
elif racer_name is not None:
for racer_idx, existing_racer in enumerate(self.racers):
if existing_racer.name == racer_name:
self.racers.pop(racer_idx)
else:
raise ValueError("You must provide a racer or racer name to be removed.")
def to_dict(self):
racers = []
for racer in self.racers:
racers.append(racer.to_dict())
return {
'name': self.name,
'racers': racers,
'ability_rank': self.ability_rank
}
def add_race(self, racers, race_idx):
# Racers are expected to be in order of lane number
new_race = {"Race_Number": -1,
"Racers": racers,
"Times": self.times}
self.races.insert(race_idx, new_race)
def racer_index(self, racer=None, racer_name=None):
if racer is None and racer_name is None:
raise ValueError("When calling racer_index, you must provide either a racer or racer_name.")
for racer_idx, each_racer in enumerate(self.racers):
if racer is None:
if racer_name == each_racer.name:
return racer_idx
else:
if racer is each_racer:
return racer_idx
return -1
def get_racer(self, racer_name):
idx = self.racer_index(racer_name=racer_name)
return self.racers[idx]
# def set_current_race_idx(self, idx):
# self.current_race_idx = idx
def get_ranks(self):
times = []
names = []
for racer in self.racers:
times.append(racer.mean_time())
names.append(racer.name)
times = np.array(times)
idx = np.argsort(times)
return names[idx], times[idx]
"""def swap_racer(self, old_racer, new_racer):
for i in range(len(self.racers)):
if self.racers[i] == old_racer:
self.racers[i] = new_racer
self.racers[i].set_heat(self.name, new_racer.heat_index + 1)"""
class Race:
def __init__(self,
heats: Iterable[Heat],
racers: Iterable[Racer],
number: int,
is_empty: bool,
n_lanes: int=4):
self.heats = heats # 1 x n_lanes
self.racers = racers # 1 x n_lanes
self.plan_number = number # The number from the race plan
self.race_number = [] # The race number(s) from the track recorder
self.times = []
self.counts = []
self.placements = []
self.current_race = 0
self.is_empty = is_empty # 1 x n_lanes
self.accepted_result_idx = -1 # The index of the race result that
self.n_lanes = n_lanes
# was accepted or -1 if none have been.
def get_placements(self, times):
return np.argsort(times) + 1
def save_results(self, race_number, race_times, counts):
# Post results to the current race number
self.race_number.append(race_number)
self.times.append(race_times)
self.counts.append(counts)
self.placements.append(self.get_placements(race_times))
self.current_race = len(self.race_number) - 1
def set_current_race(self, idx):
if idx <= 0:
self.current_race = 0
elif idx >= len(self.race_times):
self.current_race = len(self.race_times) - 1
else:
self.current_race = idx
def post_results_to_racers(self, i=-1):
if i < 0:
i = self.current_race
for lane_idx, racer, time, count, placement in zip(range(self.n_lanes), self.racers,
self.times[i], self.counts[i], self.placements[i]):
racer.post_result(lane_idx, self.race_number[i], self.plan_number,
time, count, placement)
self.accepted_result_idx = i
def to_dict(self):
entries = []
for heat, racer, is_empty in zip(self.heats, self.racers, self.is_empty):
entries.append({'racer': racer.name,
'heat': heat.name,
'empty_lane': is_empty})
if self.accepted_result_idx < 0:
out = {'planned_number': self.plan_number,
'entries': entries,
'accepted_result_idx': self.accepted_result_idx}
else:
self.set_current_race(self.accepted_result_idx)
out = {'planned_number': self.plan_number,
'entries': entries,
'accepted_result_idx': self.accepted_result_idx,
'times': self.times,
'counts': self.counts,
'index_of_race(s)_in_log': self.race_number,
'placements': self.placements}
return out
def get_racer_list(self, out=[]):
for racer in self.racers:
if racer.heat_name == "Empty":
out.append("")
else:
out.append(f"{racer.name} : {racer.heat_name}")
return out
def as_mc_sheet(self, i):
out = str(i)
for racer in self.racers:
out += " & " + racer.mc_sheet_label()
out += "\\\\\n"
return out
def has_participants(self, racers: Iterable):
for racer in racers:
if type(racer) is Racer:
is_in_race = False
for valid_racer in self.racers:
if valid_racer == racer:
is_in_race = True
if not is_in_race:
return False
else:
is_in_race = False
for valid_racer in self.racers:
if racer == valid_racer.name:
is_in_race = True
if not is_in_race:
return False
return True
class Event:
counts: List = []
def __init__(self,
event_file: str = None,
log_file: str = None,
n_lanes: int = 4,
verbose: bool = False):
"""
Event holds all the information for the race day.
:param event_file:
:param log_file:
:param n_lanes:
:param verbose:
:param check_log_file:
"""
self.verbose = verbose
self.n_lanes = n_lanes
# Load the race data
self.heats = [self.create_empty_lane_heat(), ]
self.races = []
self.current_race = None
self.current_race_idx = 0 # Race plan race number
self.current_race_log_idx = 0 # Race log race number
self.last_race = 0
self.plan_dictionary = None
if event_file is not None:
self.load_races_from_file(event_file)
# Load the log file that gives what part of the race has
# already run
if log_file is not None:
self.read_log_file(log_file)
# we will be recording race data as it comes in, so open
# the logfile for appending.
try:
self.race_log_file = open(log_file, "a+")
except OSError:
print("Unable to open {} for writing.")
pass
else:
print("Logging disabled")
self.race_log_file = open("/dev/null", "w")
self.log_file_name = "/dev/null"
def create_empty_lane_heat(self,
ability_rank=100000000000000):
racer_names = ["empty {}".format(i + 1) for i in range(self.n_lanes)]
racers = [Racer(name=x, heat_name="Empty") for x in racer_names]
return Heat(name="Empty",
racers=racers,
ability_rank=ability_rank)
def load_races_from_file(self, file_name):
try:
f = open(file_name)
except FileNotFoundError:
print("Unable to open {} for reading.")
return
if '.yaml' in file_name:
self.load_races_from_yaml(file_name)
with open(file_name) as infile:
for line in infile:
if 'Heat' in line:
self.add_heat(create_heat_from_line(line))
if self.verbose:
self.print_heats()
infile.seek(0, 0)
for line in infile:
if 'Race' in line:
self.races.append(
create_race_from_line(line, self.heats))
try:
self.current_race = self.races[0]
except IndexError:
self.current_race = None
self.last_race = len(self.races) - 1
def load_races_from_yaml(self, file_name):
with open(file_name, 'r') as infile:
self.plan_dictionary = yaml.safe_load(infile)
for heat in self.plan_dictionary['heats']:
self.add_heat(create_heat_from_dict(heat))
if self.verbose:
self.print_heats()
if 'races' in self.plan_dictionary.keys():
for race in self.plan_dictionary['races']:
self.races.append(create_race_from_dict(race, self.heats))
def print_heats(self):
print("The race heats are as follows.")
for heat in self.heats:
print(heat.name, end=': ')
racer_names = [x.name for x in heat.racers]
print(racer_names)
def heat_index(self, heat=None, heat_name=None):
if heat is None and heat_name is None:
raise ValueError("You must provide a heat or heat name to be removed.")
elif heat is not None:
for heat_idx, test in enumerate(self.heats):
if heat is test:
return heat_idx
else:
for heat_idx, test, in enumerate(self.heats[:-1]):
if test.name == heat_name:
return heat_idx
return -1
def racer_index(self, heat=None, heat_name=None, racer=None, racer_name=None):
heat_idx = self.heat_index(heat=heat, heat_name=heat_name)
if heat_idx < 0:
return heat_idx
return self.heats[heat_idx].racer_index(racer=racer, racer_name=racer_name)
def add_heat(self, heat):
for existing_heat in self.heats:
if existing_heat.name == heat.name:
raise ValueError(f"A heat with the name '{heat.name}' already exists.")
self.heats.insert(-1, heat)
def add_racer(self, racer):
global default_heat_name
was_added = False
for heat in self.heats:
if heat.name == racer.heat_name:
was_added = heat.add_racer(racer)
break
if was_added is False:
if racer.heat_name == default_heat_name:
raise ValueError('No heat was found to match heat {} of {}. '.format(
racer.heat_name, racer.name) +
'Since {} is the default heat name you may need to set the'.format(default_heat_name) +
' heat name of {}.'.format(racer.name))
else:
raise ValueError(
'No heat was found to match heat {} of {}. '.format(
racer.heat_name, racer.name) +
'If this is a new heat, then add the heat before adding the racer.')
def add_race(self, race, location=-1):
if location == 'next':
self.races.insert(self.current_race_idx, race)
elif location == 'end':
self.races.append(race)
else:
self.races.insert(location, race)
def remove_heat(self, heat=None, heat_name=None):
removed = False
if heat is None and heat_name is None:
raise ValueError("You must provide a heat or heat name to be removed.")
elif heat is not None:
if heat is self.heats[-1]:
print("You may not remove the empty heat.")
for heat_idx, test in enumerate(self.heats[:-1]):
if heat is test:
self.heats.pop(heat_idx)
removed = True
else:
if heat_name == 'Empty':
print("You may not remove the empty heat.")
for heat_idx, test, in enumerate(self.heats[:-1]):
if test.name == heat_name:
self.heats.pop(heat_idx)
removed = True
return removed
def remove_racer(self, racer=None, racer_name=None):
removed = False
if racer is None and racer_name is None:
raise ValueError("You must provide a racer or racer name to be removed.")
elif racer is not None:
for heat in self.heats:
for racer_idx, test in enumerate(heat.racers):
if racer is test:
heat.racers.pop(racer_idx)
removed = True
else:
for heat in self.heats:
for racer_idx, test in enumerate(heat.racers):
if racer_name == test.name:
heat.racers.pop(racer_idx)
removed = True
return removed
def remove_race(self, race=None, idx=None):
removed = False
if race is None and idx is None:
raise ValueError("You must provide a race or index.")
elif race is not None:
for race_idx, test in enumerate(self.races):
if test is race:
self.races.pop(race_idx)
removed = True
else:
if idx == -1:
idx = -2
self.races.pop(idx)
removed = True
return removed
def record_race_results(self, times, counts, accept):
race = self.current_race
racers = self.current_race.racers
if self.race_log_file:
self.race_log_file.write("{},{}".format(
self.current_race_log_idx,
self.current_race_idx))
for ri in range(self.n_lanes):
self.race_log_file.write(",{},{},{}".format(
racers[ri].name, times[ri], counts[ri]))
if accept:
self.race_log_file.write(",Accepted\n");
else:
self.race_log_file.write(",NA\n");
race.save_results(self.current_race_log_idx, times, counts)
if accept:
self.accept_results()
# TODO Here were are saving counts in a second data array (they are also saved in the races). This was a bug fix
# so that we could switch races and get the times to switch. Really, we should simplify things so that the data
# are only stored in one place. LRB Oct 10 2020
while self.current_race_log_idx >= len(self.counts):
self.counts.append([0] * self.n_lanes)
for li in range(self.n_lanes):
self.counts[self.current_race_log_idx][li] = counts[li]
self.current_race_log_idx += 1
def get_counts_for_race(self, race_idx):
if race_idx >= len(self.counts):
return [0] * self.n_lanes
else:
return self.counts[race_idx]
def set_counts_for_race(self, lane_idx, count):
while len(self.counts) <= self.current_race_log_idx:
self.counts.append([0] * self.n_lanes)
self.counts[self.current_race_log_idx][lane_idx] = count
def mc_table_header(self):
out = r"\begin{tabular}{l|" + " c" * self.n_lanes + "}\n"
out += r"\textbf{Race}"
for i in range(self.n_lanes):
out += r" & \textbf{Lane " + str(i + 1) + "}"
out += r" \\" + "\n" + r"\hline " + "\n"
return out
def print_plan_mc_sheet(self, file_name):
with open('mc_sheet.tex', 'w') as tempfile:
tempfile.write(mc_sheet_header + self.mc_table_header())
for idx, race in enumerate(self.races):
tempfile.write(race.as_mc_sheet(idx + 1))
tempfile.write(mc_table_footer)
pdfl = PDFLaTeX.from_texfile('mc_sheet.tex')
pdf, log, complete = pdfl.create_pdf()
with open(file_name, 'wb') as outfile:
outfile.write(pdf)
def print_status_report(self, fname):
racer_names = []
times = []
heat_names = []
for heat in self.heats:
for racer in heat.racers:
racer_names.append(racer.name)
times.append(racer.get_average())
heat_names.append(heat.name)
times = np.array(times)
racer_names = np.array(racer_names)
heat_names = np.array(heat_names)
si = np.argsort(times)
with open(fname, "w") as outfile:
header = ''.join(("Rank".rjust(8), "Name".rjust(30),
"Rank".rjust(12), "Time".rjust(10), '\n'))
outfile.write(header)
rank = 1
for i in range(len(times)):
if times[si[i]] > 0:
line = "{0:8d}{1}{2}{3:10.4f}\n".format(rank,
racer_names[si[i]].rjust(30),
heat_names[si[i]].rjust(12),
times[si[i]])
outfile.write(line)
rank += 1
return 0
def read_log_file(self, logfile):
print("Inputting previous results from {}:".format(logfile))
try:
infile = open(logfile, "r")
except OSError:
print("No previous results were found.")
return
for line in infile:
self.get_results_from_line(line)
infile.close()
def get_results_from_line(self, line):
fields = line.split(',')
racer_names = [x for x in fields[2::3]]
times = [float(x) for x in fields[3::3]]
counts = [int(x) for x in fields[4::3]]
try:
race = self.races[int(fields[1])]
except IndexError:
# This is not a valid line, so skip it
return
if race.has_participants(racer_names):
self.current_race_log_idx = int(fields[0])
self.goto_race(int(fields[1]))
if "Accepted" in line:
self.record_race_results(times, counts, True)
else:
self.record_race_results(times, counts, False)
def close_log_file(self):
if self.race_log_file:
self.race_log_file.close()
def get_chips_for_race(self, race_number):
chips = [[], [], [], []]
if race_number < 0:
print("""illegal race number, {}, requested. Returning
race[0]""".format(race_number))
race_number = 0
elif race_number > self.last_race:
print("""illegal race number, {}, requested. Returning the last race,
race[{}]""".format(race_number, self.last_race))
race_number = self.last_race
race = self.races[race_number]
for i in range(self.n_lanes):
chips[i] = race.racers[i].chip()
return chips
def goto_next_race(self):
self.goto_race(self.current_race_idx + 1)
def goto_prev_race(self):
self.goto_race(self.current_race_idx - 1)
def goto_race(self, idx):
if self.last_race < 0: # If we call this before we populate races the next two statements lead to an infinite recursion.
self.current_race_idx = 0
elif idx < 0:
self.goto_race(0)
elif idx > self.last_race:
self.goto_race(self.last_race)
else:
self.current_race_idx = idx
self.current_race = self.races[idx]
def accept_results(self):
self.current_race.post_results_to_racers()
self.goto_next_race()
def print_plan_yaml(self,
file_name='race_plan.yaml',
revised_plan=None):
if revised_plan is None:
self.generate_race_plan()
else:
self.adopt_revised_plan(revised_plan)
plan_dict = {
'heats': [],
'races': []
}
for heat in self.heats[:-1]:
plan_dict['heats'].append(heat.to_dict())
for race in self.races:
plan_dict['races'].append(race.to_dict())
with open(file_name, 'w') as outfile:
yaml.safe_dump(plan_dict, outfile, indent=2)
def sort_heats(self):
" Put heats in order of ability index. "
ar = []
for heat in self.heats:
ar.append(heat.ability_rank)
# We want our "empty heat" to come out last so we make its
# ability index the highest.
max_ar = np.max(ar)
if ar[-1] < max_ar:
ar[-1] = max_ar + 1
order = np.argsort(ar)
new_heats = []
for idx in order:
new_heats.append(self.heats[idx])
self.heats = new_heats
def generate_race_plan(self):
new_plan = []
"""
TODO Figure out how to add racers in the middle
of an event!
# Copy over any races that have already been recorded
for old_race in self.races:
if old_race.accepted_result_idx >= 0:
new_plan.append(old_race)
"""
self.sort_heats()
# We want to know who still needs to run in each lane
# The commented line is only useful if you want to track
# which lanes that racers have already run in.
needs_lane = [[], [], [], []]
racer_count = 0
for hi, heat in enumerate(self.heats[:-1]):
for lane_idx in range(self.n_lanes):
for ri, racer in enumerate(heat.racers):
# if racer.race_counts[lane_idx] == 0:
needs_lane[lane_idx].append((hi, ri, ri + racer_count))
racer_count += len(heat.racers)
# We have to figure out how to 'cycle' all the racers.
# A method is to make sure that there are N racers so
# that N%n_lanes = 1 or N%n_lanes = n_lanes-1. We can
# always add 'empty slots', so here we figure out how
# man empty slots to add.
remainder = len(needs_lane[0]) % self.n_lanes
n_empty = 0
if remainder == 0:
n_empty += 1
elif remainder > 1:
n_empty = self.n_lanes - 1 - remainder
# Add the empty heat slots.
empty_heat = len(self.heats) - 1
for lane_idx in range(self.n_lanes):
for idx in range(n_empty):
needs_lane[lane_idx].append((empty_heat, idx, idx + racer_count))
# Line all the entries up by re-arranging the entries
new_races = []
for xi in range(len(needs_lane[0])):
heats = []
racers = []
is_empty = []
for yi in range(self.n_lanes):
# We need to calculate the index for a transposed
# array.
linear_idx = xi * 4 + yi
li = linear_idx // len(needs_lane[0])
ri = linear_idx % len(needs_lane[0])
data = needs_lane[li][ri]
heats.append(self.heats[data[0]])
racers.append(self.heats[data[0]].racers[data[1]])
is_empty.append(data[2] == empty_heat)
new_races.append(Race(heats, racers, xi, is_empty, n_lanes=self.n_lanes))
self.races = new_races
if self.current_race is None:
try:
self.current_race = self.races[0]
except IndexError:
self.current_race = None
self.last_race = len(self.races) - 1
def parse_cell_text(self, text):
try:
racer_name, heat_name = text.split(":")
except ValueError:
return self.heats[-1], self.heats[-1].racers[0]
out_heat = self.heats[-1]
out_racer = out_heat.racers[0]
for heat in self.heats:
if heat.name in heat_name:
out_heat = heat
break
for racer in out_heat.racers:
if racer.name in racer_name:
out_racer = racer
break
if racer.heat_name in heat_name:
return out_heat, out_racer
else:
return self.heats[-1], self.heats[-1].racers[0]
def create_race_from_list(self, race_list, idx):
racers = []
heats = []
is_empty = []
empty_idx = 0
for entry in race_list:
heat, racer = self.parse_cell_text(entry)
heats.append(heat)
if heats[-1] is self.heats[-1]:
is_empty.append(True)
racers.append(self.heats[-1].racers[empty_idx])
empty_idx += 1
else:
is_empty.append(False)
racers.append(racer)
return Race(heats, racers, idx, is_empty)
def adopt_revised_plan(self, revised_plan):
new_races = []
for idx, race in enumerate(revised_plan):
new_races.append(self.create_race_from_list(race, idx))
self.races = new_races
if self.current_race is None:
try:
self.current_race = self.races[0]
except IndexError:
self.current_race = None
def get_race_plan(self):
self.generate_race_plan()
out_list = []
for idx, race in enumerate(self.races):
out_list.append(race.get_racer_list([]))
return out_list
# DATA LOAD
def create_heat_from_line(line):
entries = line.split(',')
heat_name = ' '.join(entries[0].split(' ')[:-1])
racers = []
for rcr in list(filter(None, entries[1:])):
if len(rcr) < 2:
continue
name = ' '.join(rcr.split(':')[:-1])
rank = rcr.split(':')[-1]
racers.append(Racer(name=name, rank=rank, heat_name=heat_name))
return Heat(name=heat_name, racers=racers)
def create_racer_from_dict(rcr_dict, heat_name):
out = Racer(name=rcr_dict['name'],
rank=rcr_dict['rank'],
car_number=next_car_number(),
heat_name=heat_name)
if 'car_status' in rcr_dict.keys():
car_status = rcr_dict['car_status']
for key in car_status:
out.car_status[key] = car_status[key]
return out
def create_heat_from_dict(heat):
racers = []
out = Heat(name=heat['name'],
racers=[],
ability_rank=heat['ability_rank'])
try:
for rcr in heat['racers']:
racer = create_racer_from_dict(rcr, heat['name'])
out.add_racer(racer)
except TypeError:
pass
except KeyError:
pass
return out
def create_race_from_line(line, all_heats):
entries = line.split(',')