def plot_components_arrows(gmm, figsize=6, cols=4, colors=colors, show=True): loc_ellipses = _loc_ellipses(gmm, colors) cols = cols rows = ((len(loc_ellipses) - 1) // cols) + 1 fig, axs = plt.subplots(rows, cols) if len(loc_ellipses) <= cols: axs = [axs] [ax.axis("off") for axrow in axs for ax in axrow] fig.set_size_inches((figsize * cols * 1.5, rows * figsize)) sortedcomps = np.argsort([m[0] for m in gmm.means_]) for i, j in enumerate(sortedcomps): r = i // cols c = i % cols ax = axs[r][c] mps.field(ax=ax, show=False) plot_component_w_arrow(gmm, j, ax=ax, color=colors[j % len(colors)], show=False) ax.axis("off") plt.show()
def axesxypolar(figsize=6): fig, axs = plt.subplots(1, 2) fig.set_size_inches((figsize * 3, figsize)) mps.field(ax=axs[0], show=False) plt.axis("on") axs[1].axvline(0, color="black") axs[1].axhline(0, color="black") axs[1].grid() axs[0].set_xlim(-1, 105 + 1) axs[0].set_ylim(-1, 68 + 1) axs[1].set_xlim(-3.2, 3.2) axs[1].set_ylim(0, 50) return axs
def locmovaxes(figsize=4): fig, axs = plt.subplots(1, 2) fig.set_size_inches((figsize * 3, figsize)) mps.field(ax=axs[0], show=False) plt.axis("on") plt.axis("scaled") axs[1].axvline(0, color="black") axs[1].axhline(0, color="black") axs[1].grid() axs[0].set_xlim(-1, 105 + 1) axs[0].set_ylim(-1, 68 + 1) axs[1].set_xlim(-105, 105) axs[1].set_ylim(-68, 68) return axs
def scatter_location_models(loc_models, actions, W, samplefn="max", tol=0.1, figsize=8, alpha=0.5): """ Data-based visualization """ for model in loc_models: print(model.name, model.n_components) X = actions[["x", "y"]] probs = model.predict_proba(X, W[model.name].values) probs = np.nan_to_num(probs) pos_prob_idx = probs.sum(axis=1) > tol x = X[pos_prob_idx] w = probs[pos_prob_idx] if model.n_components > len(colors): means = [m.mean for m in model.submodels] good_colors = color_submodels(means, colors) else: good_colors = colors c = scattercolors(w, good_colors, samplefn=samplefn) ax = mps.field(show=False, figsize=figsize) ax.scatter(x.x, x.y, c=c, alpha=alpha) plt.show()
def scatter_location_model_black(loc_model, actions, W, samplefn="max", tol=0.1, figsize=6, alpha=0.5, show=True): X = actions[["x", "y"]] probs = loc_model.predict_proba(X, W[loc_model.name].values) probs = np.nan_to_num(probs) pos_prob_idx = probs.sum(axis=1) > tol x = X[pos_prob_idx] w = probs[pos_prob_idx] if loc_model.n_components > len(colors): means = [m.mean for m in loc_model.submodels] good_colors = color_submodels(means, colors) else: good_colors = colors c = scattercolors(w, good_colors, samplefn=samplefn) ax = mps.field(show=False, figsize=figsize) ax.scatter(x.x, x.y, c="black", alpha=alpha) if show: plt.show()
def show_direction_models(loc_models, dir_models, figsize=8): """ Model-based visualization """ for loc_model in loc_models: print(loc_model.name, loc_model.n_components) ax = mps.field(show=False, figsize=figsize) norm_strengths = loc_model.priors / np.max(loc_model.priors) * 0.8 for i, (strength, gauss) in enumerate(zip(norm_strengths, loc_model.submodels)): add_ellips(ax, gauss.mean, gauss.cov, alpha=strength) x, y = gauss.mean for dir_model in dir_models: if f"{loc_model.name}_{i}" == dir_model.name: print(dir_model.name, dir_model.n_components) dir_norm_strengths = (dir_model.priors / np.max(dir_model.priors) * 0.8) for strength, vonmises in zip(dir_norm_strengths, dir_model.submodels): dx = np.cos(vonmises.loc)[0] dy = np.sin(vonmises.loc)[0] r = vonmises.R[0] add_arrow( ax, x, y, 10 * r * dx, 10 * r * dy, alpha=strength, threshold=0, ) plt.show()
def show_location_model(loc_model, show=True, figsize=6): ax = mps.field(show=False, figsize=figsize) norm_strengths = loc_model.priors / np.max(loc_model.priors) * 0.8 for strength, gauss, color in zip(norm_strengths, loc_model.submodels, colors * 10): add_ellips(ax, gauss.mean, gauss.cov, color=color, alpha=strength) if show: plt.show()
def axes3(figsize=4): fig, axs = plt.subplots(1, 3) fig.set_size_inches((figsize * 4.5, figsize)) mps.field(ax=axs[0], show=False) mps.field(ax=axs[1], show=False) plt.axis("on") plt.axis("scaled") axs[2].axvline(0, color="black") axs[2].axhline(0, color="black") axs[2].grid() axs[0].set_xlim(-1, 105 + 1) axs[0].set_ylim(-1, 68 + 1) axs[1].set_xlim(-1, 105 + 1) axs[1].set_ylim(-1, 68 + 1) #axs[2].set_xlim(-105,105) #axs[2].set_ylim(-68,68) axs[2].set_xlim(-55, 55) axs[2].set_ylim(-35, 35) return axs
def plot_heatmap(model, data, axis=None): if axis is None: axis = plt.figure(figsize=(8, 10)).add_subplot(111) z = model.estimate(data)['xG'].values axis = mps.field(ax=axis, show=False) axis = mps.heatmap(z.reshape((106, 69)).T, show=False, ax=axis, cmap='viridis_r') axis.set_xlim((70, 108)) axis.set_axis_off() return axis
def show_direction_model(gauss, dir_models, show=True, figsize=6): ax = mps.field(show=False, figsize=figsize) # for gauss in loc_model.submodels: add_ellips(ax, gauss.mean, gauss.cov, alpha=0.5) x, y = gauss.mean for vonmises in dir_models.submodels: dx = np.cos(vonmises.loc)[0] dy = np.sin(vonmises.loc)[0] r = vonmises.R[0] add_arrow(ax, x, y, 10 * dx, 10 * dy, linewidth=0.5) if show: plt.show()
def show_all_models(loc_models, dir_models): for loc_model in loc_models: print(loc_model.name, loc_model.n_components) ax = mps.field(show=False, figsize=8) am_subclusters = [] for a, _ in enumerate(loc_model.submodels): for dir_model in dir_models: if f"{loc_model.name}_{a}" == dir_model.name: am_subclusters.append(dir_model.n_components) am_subclusters = np.array(am_subclusters) for i, gauss in enumerate(loc_model.submodels): if (am_subclusters == 1).all(): add_ellips(ax, gauss.mean, gauss.cov, alpha=0.5) else: add_ellips(ax, gauss.mean, gauss.cov, color='grey') x, y = gauss.mean for dir_model in dir_models: if f"{loc_model.name}_{i}" == dir_model.name: print(dir_model.name, dir_model.n_components) for j, vonmises in enumerate(dir_model.submodels): dx = np.cos(vonmises.loc)[0] dy = np.sin(vonmises.loc)[0] r = vonmises.R[0] add_arrow(ax, x, y, 10 * dx, 10 * dy, linewidth=0.5) plt.show()
def plot_component_w_arrow(gmm, i, ax=None, color=None, arrowsize=2.5, mirror=False, show=True): if ax is None: ax = mps.field(show=False) locell, _movell = component_ellipses(gmm, i, color=color) if mirror: c = locell._center locell._center = [105 - c[0], 68 - c[1]] ax.add_artist(locell) x, y, dx, dy = gmm.means_[i][:4] if mirror: x, y, dx, dy = 105 - x, 68 - y, -dx, -dy if abs(dx) > 0.5 or abs(dy) > 0.5: ax.arrow( x, y, dx, dy, head_width=arrowsize, head_length=arrowsize, linewidth=5, fc="black", #colors[i % len(colors)], ec="black", #colors[i % len(colors)], length_includes_head=True, alpha=1) if show: plt.show() return ax
actions = actions.merge(players, left_on="player_id", right_on="id") teams = pd.read_hdf(data, key="teams") actions = actions.merge(teams, left_on="team_id", right_on="id") actions = actions.sort_values( ["game_id", "period_id", "time_seconds", "timestamp"]) actions.columns player_actions = actions[actions.last_name.str.contains("Kompany")].copy() set(player_actions.soccer_name) player_actions = always_ltr(player_actions) x, y = player_actions.start_x, player_actions.start_y f = mps.field() f = mps.field(color="green", figsize=10) ax = mps.field(show=False) ax.scatter(x, y, s=2) plt.show() matrix = mps.count(x, y, n=20, m=20) hm = mps.heatmap(matrix) start = 499605 delta = 40 phase = actions[start:start + delta].copy() phase["team"] = phase.full_name phase["player"] = phase.soccer_name phase = phase[[
def show_component_differences(loc_models, dir_models, vec_p1, vec_p2, name1, name2, save=True): # determine colors of dir sub models difference = vec_p1 - vec_p2 cmap = mpl.cm.get_cmap('bwr_r') for loc_model in loc_models: mini = min(difference.loc[difference.index.str.contains( f"^{loc_model.name}_")]) maxi = max(difference.loc[difference.index.str.contains( f"^{loc_model.name}_")]) ab = max(abs(mini), abs(maxi)) if (ab == 0): ab = 0.0001 norm = mpl.colors.DivergingNorm(vcenter=0, vmin=-ab, vmax=ab) print(loc_model.name, loc_model.n_components) ax = mps.field(show=False, figsize=8) am_subclusters = [] for a, _ in enumerate(loc_model.submodels): for dir_model in dir_models: if f"{loc_model.name}_{a}" == dir_model.name: am_subclusters.append(dir_model.n_components) am_subclusters = np.array(am_subclusters) for i, gauss in enumerate(loc_model.submodels): if (am_subclusters == 1).all(): add_ellips(ax, gauss.mean, gauss.cov, color=cmap( norm( difference.loc[f"{loc_model.name}_{i}_0"])), alpha=1) else: add_ellips(ax, gauss.mean, gauss.cov, color='gainsboro') x, y = gauss.mean for dir_model in dir_models: if f"{loc_model.name}_{i}" == dir_model.name: print(dir_model.name, dir_model.n_components) for j, vonmises in enumerate(dir_model.submodels): dx = np.cos(vonmises.loc)[0] dy = np.sin(vonmises.loc)[0] add_arrow( ax, x, y, 10 * dx, 10 * dy, fc=cmap( norm(difference. loc[f"{loc_model.name}_{i}_{j}"])), arrowsize=4.5, linewidth=1) cb = plt.colorbar(plt.cm.ScalarMappable(cmap=cmap, norm=norm), ax=ax, fraction=0.065, pad=-0.05, orientation='horizontal') cb.ax.xaxis.set_ticks_position('bottom') cb.ax.tick_params(labelsize=16) plt.axis("scaled") if save: savefigure(f"../figures/{name1}-{name2}-{loc_model.name}.png") else: plt.show()
def field(ax): ax = mps.field(ax=ax, show=False) ax.set_xlim(-1, 105 + 1) ax.set_ylim(-1, 68 + 1) return ax