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
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Chess Masters\n\nAn example of the MultiDiGraph class.\n\nThe function `chess_pgn_graph` reads a collection of chess matches stored in\nthe specified PGN file (PGN =\"Portable Game Notation\"). Here the (compressed)\ndefault file::\n\n chess_masters_WCC.pgn.bz2\n\ncontains all 685 World Chess Championship matches from 1886--1985.\n(data from http://chessproblem.my-free-games.com/chess/games/Download-PGN.php)\n\nThe `chess_pgn_graph()` function returns a `MultiDiGraph` with multiple edges.\nEach node is the last name of a chess master. Each edge is directed from white\nto black and contains selected game info.\n\nThe key statement in `chess_pgn_graph` below is::\n\n G.add_edge(white, black, game_info)\n\nwhere `game_info` is a `dict` describing each game.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport networkx as nx\n\n# tag names specifying what game info should be\n# stored in the dict on each digraph edge\ngame_details = [\"Event\", \"Date\", \"Result\", \"ECO\", \"Site\"]\n\n\ndef chess_pgn_graph(pgn_file=\"chess_masters_WCC.pgn.bz2\"):\n \"\"\"Read chess games in pgn format in pgn_file.\n\n Filenames ending in .bz2 will be uncompressed.\n\n Return the MultiDiGraph of players connected by a chess game.\n Edges contain game data in a dict.\n\n \"\"\"\n import bz2\n\n G = nx.MultiDiGraph()\n game = {}\n with bz2.BZ2File(pgn_file) as datafile:\n lines = [line.decode().rstrip(\"\\r\\n\") for line in datafile]\n for line in lines:\n if line.startswith(\"[\"):\n tag, value = line[1:-1].split(\" \", 1)\n game[str(tag)] = value.strip('\"')\n else:\n # empty line after tag set indicates\n # we finished reading game info\n if game:\n white = game.pop(\"White\")\n black = game.pop(\"Black\")\n G.add_edge(white, black, **game)\n game = {}\n return G\n\n\nG = chess_pgn_graph()\n\nprint(\n f\"Loaded {G.number_of_edges()} chess games between {G.number_of_nodes()} players\\n\"\n)\n\n# identify connected components of the undirected version\nH = G.to_undirected()\nGcc = [H.subgraph(c) for c in nx.connected_components(H)]\nif len(Gcc) > 1:\n print(f\"Note the disconnected component consisting of:\\n{Gcc[1].nodes()}\")\n\n# find all games with B97 opening (as described in ECO)\nopenings = {game_info[\"ECO\"] for (white, black, game_info) in G.edges(data=True)}\nprint(f\"\\nFrom a total of {len(openings)} different openings,\")\nprint(\"the following games used the Sicilian opening\")\nprint('with the Najdorff 7...Qb6 \"Poisoned Pawn\" variation.\\n')\n\nfor (white, black, game_info) in G.edges(data=True):\n if game_info[\"ECO\"] == \"B97\":\n summary = f\"{white} vs {black}\\n\"\n for k, v in game_info.items():\n summary += f\" {k}: {v}\\n\"\n summary += \"\\n\"\n print(summary)\n\n# make new undirected graph H without multi-edges\nH = nx.Graph(G)\n\n# edge width is proportional number of games played\nedgewidth = [len(G.get_edge_data(u, v)) for u, v in H.edges()]\n\n# node size is proportional to number of games won\nwins = dict.fromkeys(G.nodes(), 0.0)\nfor (u, v, d) in G.edges(data=True):\n r = d[\"Result\"].split(\"-\")\n if r[0] == \"1\":\n wins[u] += 1.0\n elif r[0] == \"1/2\":\n wins[u] += 0.5\n wins[v] += 0.5\n else:\n wins[v] += 1.0\nnodesize = [wins[v] * 50 for v in H]\n\n# Generate layout for visualization\npos = nx.kamada_kawai_layout(H)\n# Manual tweaking to limit node label overlap in the visualization\npos[\"Reshevsky, Samuel H\"] += (0.05, -0.10)\npos[\"Botvinnik, Mikhail M\"] += (0.03, -0.06)\npos[\"Smyslov, Vassily V\"] += (0.05, -0.03)\n\nfig, ax = plt.subplots(figsize=(12, 12))\n# Visualize graph components\nnx.draw_networkx_edges(H, pos, alpha=0.3, width=edgewidth, edge_color=\"m\")\nnx.draw_networkx_nodes(H, pos, node_size=nodesize, node_color=\"#210070\", alpha=0.9)\nlabel_options = {\"ec\": \"k\", \"fc\": \"white\", \"alpha\": 0.7}\nnx.draw_networkx_labels(H, pos, font_size=14, bbox=label_options)\n\n# Title/legend\nfont = {\"fontname\": \"Helvetica\", \"color\": \"k\", \"fontweight\": \"bold\", \"fontsize\": 14}\nax.set_title(\"World Chess Championship Games: 1886 - 1985\", font)\n# Change font color for legend\nfont[\"color\"] = \"r\"\n\nax.text(\n 0.80,\n 0.10,\n \"edge width = # games played\",\n horizontalalignment=\"center\",\n transform=ax.transAxes,\n fontdict=font,\n)\nax.text(\n 0.80,\n 0.06,\n \"node size = # games won\",\n horizontalalignment=\"center\",\n transform=ax.transAxes,\n fontdict=font,\n)\n\n# Resize figure for label readibility\nax.margins(0.1, 0.05)\nfig.tight_layout()\nplt.axis(\"off\")\nplt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|