forked from castilhocp/arkhrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_helpers.py
304 lines (230 loc) · 16.7 KB
/
data_helpers.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
from flask import g
from flask import current_app
import os
import pandas as pd
import numpy as np
import arkhrec.general_helpers
gCycles=dict()
PACKS_WITHOUT_PLAYER_CARDS = ['promotional', 'parallel', 'side_stories']
def get_all_cards():
if 'all_cards' not in g:
all_cards = pd.read_pickle(os.path.join(current_app.root_path, 'datafiles', 'all_cards.pickle'))
all_cards.loc[:,'unique_code'] = all_cards.loc[:, 'code_str']
all_cards.loc[~all_cards['duplicate_of'].isna(), 'unique_code'] = all_cards.loc[~all_cards['duplicate_of'].isna(), 'duplicate_of'].astype(int).astype(str).apply(str.zfill, args=[5])
g.all_cards=all_cards
return g.all_cards
def get_all_decks():
if 'all_decks' not in g:
g.all_decks = pd.read_pickle(os.path.join(current_app.root_path, 'datafiles', 'all_decks_clean.pickle'))
return g.all_decks
def get_analysed_card_frequencies():
if 'analysed_card_frequencies' not in g:
g.analysed_card_frequencies = pd.read_pickle(os.path.join(current_app.root_path, 'datafiles', 'analysed_card_frequencies.pickle'))
return g.analysed_card_frequencies
def get_card_cooccurrences():
if 'card_cooccurrences' not in g:
g.card_cooccurrences = pd.read_pickle(os.path.join(current_app.root_path, 'datafiles', 'cooccurrences_calculated.pickle'))
return g.card_cooccurrences
def get_card_investigator_cooccurrences():
if 'card_investigator_cooccurrences' not in g:
g.card_investigator_cooccurrences = pd.read_pickle(os.path.join(current_app.root_path, 'datafiles', 'inv_cooccurrences_calculated.pickle'))
return g.card_investigator_cooccurrences
def get_collection():
from flask import session
if 'card_collection' in session:
return session['card_collection']
card_collection = dict()
for cycle in gCycles:
if gCycles[cycle]['code'] in PACKS_WITHOUT_PLAYER_CARDS:
continue
for pack in gCycles[cycle]['packs']:
card_collection[pack['code']] = True
session['card_collection'] = card_collection
return session['card_collection']
def get_all_investigators(index='code_str'):
if 'all_investigators' in g:
return g.all_investigators
all_cards = get_all_cards()
all_decks = get_all_decks()
all_investigators = all_cards[all_cards['type_code']=="investigator"]
# There are investigators with multiple codes (e.g. parallel versions, book versions, etc.)
# Gets the minimum code (that's the "cycle" version)
# Converts to numeric to find the minimum value
all_investigators.loc[:,'code'] = pd.to_numeric(all_investigators['code'], errors='coerce')
investigator_unique_codes = all_investigators.groupby('name')['code'].min()
all_cards.loc[:,'code'] = pd.to_numeric(all_cards['code'], errors='coerce')
investigators = all_cards[all_cards['code'].isin(investigator_unique_codes.values)].set_index('name')
# total number of decks in database
number_of_decks_per_investigator = all_decks.groupby('investigator_name').count()['id'].to_frame()
number_of_decks_per_investigator.columns = ['number_of_decks']
investigators = investigators.join(number_of_decks_per_investigator)
# gets rank between all investigators
investigators['occurrences_rank'] = investigators['number_of_decks'].rank(method="max", ascending=False)
# creates string for faction color - really useless since there is no "multi" colored investigator
investigators = arkhrec.general_helpers.set_color(investigators)
# removes investigators with no decks (currently, only the ones from the TCU prologue)
investigators = investigators.fillna("-")
investigators = investigators[investigators['number_of_decks']!='-']
investigators = investigators.reset_index().set_index(index)
g.all_investigators = investigators
return g.all_investigators
# This function returns a list of all cards with calculated cooccurrences
# Returns a Pandas Dataframe with:
# - Index: 'code_str' -> string formatted card code (5 digits)
# - Columns:
def get_analysed_cards():
all_cards = get_all_cards()
analysed_card_codes = get_analysed_card_frequencies()
# card_cooc = get_card_cooccurrences()
all_cards = all_cards.set_index('code_str')
analysed_cards = all_cards.merge(analysed_card_codes, left_index=True, right_index=True)
analysed_cards['appearances'] = analysed_cards['appearances'].astype(int)
analysed_cards['appearances_rank'] = analysed_cards['appearances'].rank(method="max", ascending=False)
analysed_cards = arkhrec.general_helpers.convert_xp_to_str(analysed_cards)
analysed_cards = arkhrec.general_helpers.set_color(analysed_cards)
analysed_cards = analysed_cards.fillna("-")
analysed_cards.loc[:,'text_icons'] = analysed_cards['text'].apply(arkhrec.general_helpers.convert_text_to_icons)
analysed_cards['cycle'] = analysed_cards.apply(arkhrec.general_helpers.set_cycle, axis=1)
analysed_cards = analysed_cards.drop('01000')
return analysed_cards
def get_similarities_with_card(card_id):
analysed_cards = get_analysed_cards()
card_cooc = get_card_cooccurrences()
c1 = card_cooc.xs(card_id, level=1).drop('occurrences_card2', axis=1).rename(columns={'occurrences_card1':'occurrences'})
c2 = card_cooc.xs(card_id, level=0).drop('occurrences_card1', axis=1).rename(columns={'occurrences_card2':'occurrences'})
c = pd.concat([c1,c2])
analysed_cards_with_similarities = analysed_cards.join(c)
analysed_cards_with_similarities = analysed_cards_with_similarities[~analysed_cards_with_similarities.index.duplicated()]
analysed_cards_with_similarities = analysed_cards_with_similarities.dropna()
analysed_cards_with_similarities.loc[:,'jaccard'] = analysed_cards_with_similarities['jaccard'].apply(lambda x: "{:0.0%}".format(x))
analysed_cards_with_similarities.loc[:,'cost_view'] = analysed_cards_with_similarities['cost'].apply(lambda x: x if (x=='' or x=='X' or x=='x' or x=='-') else "{:0.0f}".format(float(x)))
analysed_cards_with_similarities = analysed_cards_with_similarities.dropna()
return analysed_cards_with_similarities
def filter_cards_in_collection(cards_to_filter, other_cards_to_include=[]):
all_cards = get_all_cards()
card_collection = get_collection()
card_collection_codes = all_cards[all_cards['pack_code'].isin(card_collection.keys())]['unique_code'].unique()
card_collection_codes = np.append(card_collection_codes, other_cards_to_include)
filtered_cards = cards_to_filter.loc[cards_to_filter.index.intersection(card_collection_codes)]
return filtered_cards
def get_usage_by_investigators(card_id):
inv_cooc = get_card_investigator_cooccurrences()
investigators = get_all_investigators('name')
usage_by_investigators = inv_cooc.xs(card_id,level=1)
usage_by_investigators = usage_by_investigators.rename(columns={'cooccurrence':'Coocurrence', 'occurrences_investigator': 'number_of_decks'})
usage_by_investigators.loc[:,'presence'] = usage_by_investigators['presence'].apply(lambda x: "{:0.0%}".format(x))
usage_by_investigators = usage_by_investigators.join(investigators.reset_index().set_index('code_str')[['name', 'faction_code', 'faction2_code']])
usage_by_investigators = arkhrec.general_helpers.set_color(usage_by_investigators)
usage_by_investigators = usage_by_investigators.reset_index().set_index('investigator')
usage_by_investigators['cycle'] = usage_by_investigators.apply(arkhrec.general_helpers.set_cycle, axis=1)
usage_by_investigators = usage_by_investigators.reset_index().set_index('name')
usage_by_investigators.index.name = 'investigator_name'
usage_by_investigators = usage_by_investigators.dropna()
return usage_by_investigators
def get_investigator_cards_usage(investigator):
analysed_cards = get_analysed_cards()
inv_cooc = get_card_investigator_cooccurrences()
# Joins the number of cooccurrences of each card with this investigator
inv_card_cooccurrences = inv_cooc.loc[investigator.index].droplevel('investigator')
print(inv_card_cooccurrences)
investigator_cards_usage = analysed_cards.join(inv_card_cooccurrences)
investigator_cards_usage = investigator_cards_usage.dropna()
return investigator_cards_usage
def get_duplicates_unique_code():
all_cards = get_all_cards()
duplicates = all_cards.loc[~all_cards['duplicate_of'].isna(), ['code_str', 'unique_code']].set_index('code_str')
return duplicates
def get_investigator_deck_statistics(investigator):
all_decks = get_all_decks()
investigators = get_all_investigators()
# statistics for investigator
inv_mean_cost = all_decks[all_decks['investigator_name']==investigator['name'][0]]['mean_cost'].mean()
inv_assets_percentage = all_decks[all_decks['investigator_name']==investigator['name'][0]]['asset_percentages'].mean()
inv_events_percentage = all_decks[all_decks['investigator_name']==investigator['name'][0]]['event_percentages'].mean()
inv_skills_percentage = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_percentages'].mean()
inv_hand_slot = all_decks[all_decks['investigator_name']==investigator['name'][0]]['hand_slot'].mean()
inv_arcane_slot = all_decks[all_decks['investigator_name']==investigator['name'][0]]['arcane_slot'].mean()
inv_ally_slot = all_decks[all_decks['investigator_name']==investigator['name'][0]]['ally_slot'].mean()
inv_body_slot = all_decks[all_decks['investigator_name']==investigator['name'][0]]['body_slot'].mean()
inv_accessory_slot = all_decks[all_decks['investigator_name']==investigator['name'][0]]['accessory_slot'].mean()
inv_skill_willpower = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_willpower'].mean()
inv_skill_combat = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_combat'].mean()
inv_skill_agility = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_agility'].mean()
inv_skill_intellect = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_intellect'].mean()
inv_skill_wild = all_decks[all_decks['investigator_name']==investigator['name'][0]]['skill_wild'].mean()
# average statistics for all others
others_mean_cost = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['mean_cost'].mean()
others_assets_percentage = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['asset_percentages'].mean()
others_events_percentage = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['event_percentages'].mean()
others_skills_percentage = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_percentages'].mean()
others_hand_slot = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['hand_slot'].mean()
others_arcane_slot = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['arcane_slot'].mean()
others_ally_slot = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['ally_slot'].mean()
others_body_slot = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['body_slot'].mean()
others_accessory_slot = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['accessory_slot'].mean()
others_skill_willpower = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_willpower'].mean()
others_skill_combat = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_combat'].mean()
others_skill_agility = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_agility'].mean()
others_skill_intellect = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_intellect'].mean()
others_skill_wild = all_decks[all_decks['investigator_name']!=investigator['name'][0]]['skill_wild'].mean()
# average statistics for investigator's faction
all_decks = all_decks.join(investigators[['name','faction_code']].set_index('name'), on='investigator_name', rsuffix="r_")
faction_mean_cost = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['mean_cost'].mean()
faction_assets_percentage = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['asset_percentages'].mean()
faction_events_percentage = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['event_percentages'].mean()
faction_skills_percentage = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_percentages'].mean()
faction_hand_slot = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['hand_slot'].mean()
faction_arcane_slot = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['arcane_slot'].mean()
faction_ally_slot = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['ally_slot'].mean()
faction_body_slot = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['body_slot'].mean()
faction_accessory_slot = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['accessory_slot'].mean()
faction_skill_willpower = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_willpower'].mean()
faction_skill_combat = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_combat'].mean()
faction_skill_agility = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_agility'].mean()
faction_skill_intellect = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_intellect'].mean()
faction_skill_wild = all_decks[(all_decks['investigator_name']!=investigator['name'][0]) & (all_decks['faction_code']==investigator['faction_code'][0])]['skill_wild'].mean()
deck_statistics = {
'others_mean_cost': others_mean_cost,
'others_assets_percentage': others_assets_percentage*100,
'others_skills_percentage': others_skills_percentage*100,
'others_events_percentage': others_events_percentage*100,
'others_hand_slot': others_hand_slot,
'others_arcane_slot': others_arcane_slot,
'others_body_slot': others_body_slot,
'others_accessory_slot': others_accessory_slot,
'others_ally_slot': others_ally_slot,
'others_skill_willpower': others_skill_willpower,
'others_skill_agility': others_skill_agility,
'others_skill_combat': others_skill_combat,
'others_skill_intellect': others_skill_intellect,
'others_skill_wild': others_skill_wild,
'inv_mean_cost': inv_mean_cost,
'inv_assets_percentage': inv_assets_percentage*100,
'inv_events_percentage': inv_events_percentage*100,
'inv_skills_percentage': inv_skills_percentage*100,
'inv_hand_slot': inv_hand_slot,
'inv_arcane_slot': inv_arcane_slot,
'inv_body_slot': inv_body_slot,
'inv_accessory_slot': inv_accessory_slot,
'inv_ally_slot': inv_ally_slot,
'inv_skill_willpower': inv_skill_willpower,
'inv_skill_agility': inv_skill_agility,
'inv_skill_combat': inv_skill_combat,
'inv_skill_intellect': inv_skill_intellect,
'inv_skill_wild': inv_skill_wild,
'faction_mean_cost': faction_mean_cost,
'faction_assets_percentage': faction_assets_percentage*100,
'faction_skills_percentage': faction_skills_percentage*100,
'faction_events_percentage': faction_events_percentage*100,
'faction_hand_slot': faction_hand_slot,
'faction_arcane_slot': faction_arcane_slot,
'faction_body_slot': faction_body_slot,
'faction_accessory_slot': faction_accessory_slot,
'faction_ally_slot': faction_ally_slot,
'faction_skill_willpower': faction_skill_willpower,
'faction_skill_agility': faction_skill_agility,
'faction_skill_combat': faction_skill_combat,
'faction_skill_intellect': faction_skill_intellect,
'faction_skill_wild': faction_skill_wild,
}
return deck_statistics