def makegraph(self, outfile, masterkey="stats", key="occurences"): """ Generate a graph. @param outfile: path to file to write to @type outfile: L{str} @param masterkey: category key of get_pairing_data()'s result to use. @type masterkey: L{str} @param key: key for value to use @type key: L{str} """ # prepare data data = self.get_pairing_data() pairingstats = data[masterkey] # purge all entries were value of key <= 0 to_purge = set() for pairing in pairingstats.keys(): v = pairingstats[pairing][key] if v <= 0: to_purge.add(pairing) for p in to_purge: del pairingstats[p] names = list( sorted( set([name for pairing in data[masterkey] for name in pairing]))) n_names = len(names) matrix = [[0] * n_names for i in range(n_names)] for pairing in pairingstats.keys(): v = pairingstats[pairing][key] name_1, name_2 = pairing i, j = names.index(name_1), names.index(name_2) matrix[i][j] = v matrix[j][i] = v c = Chord( matrix, names, width=1600, margin=200, padding=0.02, wrap_labels=False, font_size="12px", noun="FanFics", allow_download=True, #title="Ships (by {})".format(key), credit=False, ) c.to_html(filename=outfile)
def plot_diagrams(self, matrices, names, l_labels): for i, (matrix, name) in enumerate(zip(matrices, names)): colors = (get_colors(gray=name, color=l_labels)) diagram = Chord( matrix, name, colors=colors, #"d3.schemeSet1", opacity=0.9, padding=0.01, width=self.width, font_size="10px", font_size_large="10px", label_color="#454545", wrap_labels=self.wrap, credit=False, margin=self.margin, ) diagram.show() diagram.to_html(f'{self.filename}_{i}.html')
def generate_chord(team_id, season_id, season_progress, team_name): team_passes = teamdashptpass.TeamDashPtPass( team_id=team_id, season=season_id, season_type_all_star=season_progress, per_mode_simple='PerGame') # Print passes made df_team_passes_made = team_passes.get_data_frames()[0] #print(df_team_passes_made) # Print passes received df_team_passes_rec = team_passes.get_data_frames()[1] #print(df_team_passes_rec) player_ids = list(df_team_passes_made['PASS_TEAMMATE_PLAYER_ID']) #print(player_ids) # Create empty dataframes, one for passes made, another for passes received team_pp_made = pd.DataFrame() team_pp_rec = pd.DataFrame() # Cycle through each pleayer id and extract passes made and passes received for pid in player_ids: player_pass = playerdashptpass.PlayerDashPtPass( team_id=team_id, player_id=pid, season=season_id, season_type_all_star=season_progress, per_mode_simple='PerGame') # Get DataFrames player_pass_made = player_pass.get_data_frames()[0] player_pass_rec = player_pass.get_data_frames()[1] # Append team_pp_made = pd.concat([team_pp_made, player_pass_made], ignore_index=True, axis=0, sort=False) team_pp_rec = pd.concat([team_pp_rec, player_pass_rec], ignore_index=True, axis=0, sort=False) # Sleep for 5 seconds before moving to the next player time.sleep(0.5) # Create merge keys team_pp_made['key_pass_from_to'] = team_pp_made[ 'PLAYER_NAME_LAST_FIRST'] + " : " + team_pp_made['PASS_TO'] team_pp_made['key_pass_to_from'] = team_pp_made[ 'PASS_TO'] + " : " + team_pp_made['PLAYER_NAME_LAST_FIRST'] # Create a DataFrame based on key_pass_to_from team_pp_made_to_from = team_pp_made.loc[:, ['key_pass_to_from', 'PASS']] team_pp_made_to_from = team_pp_made_to_from.rename( columns={'PASS': '******'}) #print(team_pp_made_to_from) # Merge it back to the main DataFrame team_pp_comb = pd.merge(team_pp_made, team_pp_made_to_from, how='left', left_on='key_pass_from_to', right_on='key_pass_to_from') team_pp_comb['Total Passes'] = team_pp_comb['PASS'] + team_pp_comb[ 'pass_to_from_cnt'] #print(team_pp_comb) # Take a copy of DF df = team_pp_comb.copy() # Keep only required fields df = df.loc[:, ['PLAYER_NAME_LAST_FIRST', 'PASS_TO', 'Total Passes']] # Create crosstab (note, we replace NaN with 0's) df_tab = pd.crosstab(df['PLAYER_NAME_LAST_FIRST'], df['PASS_TO'], aggfunc='sum', values=df['Total Passes']).fillna(0) # Print resulting DataFrame #print(df_tab) # Player Names names = list(df_tab.columns) #names # Passing data (change from df to list of lists) matrix = df_tab.values.tolist() #matrix # Create our diagram diagram = Chord(matrix, names, font_size_large='10px', wrap_labels=False, margin=100, width=800) # Save it into HTML file team_name = team_name.replace(' ', '') season_progress = season_progress.replace(' ', '') diagram.to_html(team_name + season_id + season_progress + 'Chord.html') popupmsg_success()