Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python3 

 

import numpy as np 

import pandas as pd 

import matplotlib.pyplot as plt 

 

from . import model 

 

phi = 1.61803398875 

 

def plot_exchanges(session): 

df = count_exchanges(session) 

 

plot_exchanges_by_giver(df) 

plot_aggregated_exchanges(df) 

 

plt.show() 

 

def plot_exchanges_by_giver(df): 

givers = 'left', 'partner', 'right' 

fig, ax = plt.subplots(1, len(givers), figsize=(8, 8/phi)) 

 

for i, giver in enumerate(givers): 

ax[i].set_title(giver) 

plot_exchange_probs(ax[i], df[df.giver == giver]) 

 

fig.tight_layout() 

 

def plot_aggregated_exchanges(df): 

fig, ax = plt.subplots(figsize=(3, 8/phi)) 

 

def agg_prob(x): 

inv_prob = 1 - x 

return 1 - inv_prob.prod() 

 

df = df.groupby(['call', 'rank'])\ 

.agg({'prob': agg_prob, 'count': sum})\ 

.reset_index() 

 

ax.set_title('Probability of being passed') 

plot_exchange_probs(ax, df) 

 

fig.tight_layout() 

 

def plot_exchange_probs(ax, df, min_counts=20): 

""" 

Given data frame must have "call", "rank", and "prob" columns. 

""" 

df = df.set_index('call') 

 

if df.loc['no_call']['count'].sum() > min_counts: 

plot_bars(ax, 

df.loc['no_call']['rank'], 

df.loc['no_call']['prob'], 

linewidth=5, 

color='tab:blue', 

) 

 

#if df.loc['tichu_before']['count'].sum() > min_counts: 

# ax.plot( 

# df.loc['tichu_before']['rank'], 

# df.loc['tichu_before']['prob'], 

# marker='_',  

# markersize=5, 

# linestyle='none', 

# color='tab:orange', 

# ) 

 

if df.loc['grand_tichu']['count'].sum() > min_counts: 

ax.plot( 

df.loc['grand_tichu']['rank'], 

df.loc['grand_tichu']['prob'], 

marker='+', 

markersize=5, 

linestyle='none', 

color='tab:orange', 

) 

 

label_card_axis(ax) 

 

def plot_bars(ax, xs, ys, **kwargs): 

for x, y in zip(xs, ys): 

ax.plot([x, x], [0, y], **kwargs) 

 

def count_exchanges(session): 

from collections import Counter 

 

counts = Counter() 

queries = query_exchanges_by_call(session) 

givers = { 

(model.SeatTypes.south, model.SeatTypes.west): 'right', 

(model.SeatTypes.east, model.SeatTypes.south): 'right', 

(model.SeatTypes.north, model.SeatTypes.east): 'right', 

(model.SeatTypes.west, model.SeatTypes.north): 'right', 

 

(model.SeatTypes.south, model.SeatTypes.north): 'partner', 

(model.SeatTypes.east, model.SeatTypes.west): 'partner', 

(model.SeatTypes.north, model.SeatTypes.south): 'partner', 

(model.SeatTypes.west, model.SeatTypes.east): 'partner', 

 

(model.SeatTypes.south, model.SeatTypes.east): 'left', 

(model.SeatTypes.east, model.SeatTypes.north): 'left', 

(model.SeatTypes.north, model.SeatTypes.west): 'left', 

(model.SeatTypes.west, model.SeatTypes.south): 'left', 

} 

 

for call in queries: 

for exchange in queries[call].all(): 

giver = givers[exchange.giver.seat, exchange.taker.seat] 

rank = get_rank(exchange.card) 

counts[call, giver, rank] += 1 

 

df = pd.DataFrame([ 

dict( 

call=call, 

giver=giver, 

rank=rank, 

count=count, 

) 

for (call, giver, rank), count in counts.items() 

]) 

 

n = df.groupby(['call', 'giver'])['count'].transform('sum') 

df['prob'] = df['count'] / n 

 

return df 

 

def query_exchanges_by_call(session): 

from sqlalchemy import and_, or_ 

from .model import Exchange, Call, CallTypes 

 

# Outer join so we can tell which seats *didn't* make a Tichu call. 

q = session.query(Exchange).\ 

outerjoin(Call, and_( 

Exchange.round_id == Call.round_id, 

Exchange.taker_id == Call.seat_id, 

)) 

 

queries = { 

'grand_tichu': q.filter(Call.call == CallTypes.grand_tichu), 

'tichu_before': q.filter(Call.call == CallTypes.tichu_before), 

'no_call': q.filter(or_( 

Call.call == None, 

Call.call == CallTypes.tichu_after, 

)), 

} 

 

return queries 

 

def label_card_axis(ax): 

named_ranks = { 

11: 'J', 

12: 'Q', 

13: 'K', 

14: 'A', 

**{ 

v: special_names[k] 

for k,v in special_rank.items() 

} 

} 

 

ticks = list(range(NUM_RANKS)) 

tick_labels = [named_ranks.get(x, str(x)) for x in ticks] 

 

ax.set_xlim(0.5, NUM_RANKS + 0.5) 

ax.set_xticks(ticks) 

ax.set_xticklabels(tick_labels, rotation='vertical') 

 

def get_rank(card): 

if card.special: 

return special_rank[card.special] 

else: 

return card.rank 

 

NUM_RANKS = 17 

 

special_rank = { 

model.SpecialTypes.one: 1, 

model.SpecialTypes.hound: 15, 

model.SpecialTypes.phoenix: 16, 

model.SpecialTypes.dragon: 17, 

} 

special_names = { 

model.SpecialTypes.one: 'One', 

model.SpecialTypes.hound: 'Dog', 

model.SpecialTypes.phoenix: 'Phoenix', 

model.SpecialTypes.dragon: 'Dragon', 

}