U
    ¹mœdã  ã                   @   s  d Z ddlmZ ddlZdddddgZdJd	d
„Zeƒ Ze	de 
¡ › de ¡ › d�ƒ e ¡ Zdd„ e e¡D ƒZeeƒdkr’e	ded  ¡ › �ƒ dd„ ejdd�D ƒZe	deeƒ› d�ƒ e	dƒ e	dƒ ejdd�D ]`\ZZZed dkrØe› de› d�Ze ¡ D ] \ZZede› de› d�7 Z�qed7 Ze	eƒ qØe e¡Zdd„ e ¡ D ƒZe e ¡ d ¡Zejdd�D ]z\Z ZZ!e!d  "d!¡Z#e#d d"k�rªee   d#7  < n@e#d d$k�rÚee   d%7  < ee  d%7  < nee  d#7  < �qrd&d„ eD ƒZ$e %e¡Z&e&d'  d(7  < e&d)  d*7  < e&d+  d,7  < ej'd-d.�\Z(Z)ej*ee&d/ed0d1� ej+ee&e$d2d3d4� d5d6d7d8œZ,ej-ee&d9e,d:� d;d5d<d9d=œZ.e) /d>e.¡ d?e.d@< e)j0dAdBdCdDe)j1e.dE� e)j0dAdFdGdDe)j1e.dE� e) 2dBdH¡ e( 3¡  e 4dI¡ e 5¡  dS )Ka
  
=============
Chess Masters
=============

An example of the MultiDiGraph class.

The function `chess_pgn_graph` reads a collection of chess matches stored in
the specified PGN file (PGN ="Portable Game Notation").  Here the (compressed)
default file::

    chess_masters_WCC.pgn.bz2

contains all 685 World Chess Championship matches from 1886--1985.
(data from http://chessproblem.my-free-games.com/chess/games/Download-PGN.php)

The `chess_pgn_graph()` function returns a `MultiDiGraph` with multiple edges.
Each node is the last name of a chess master. Each edge is directed from white
to black and contains selected game info.

The key statement in `chess_pgn_graph` below is::

    G.add_edge(white, black, game_info)

where `game_info` is a `dict` describing each game.
é    NÚEventÚDateÚResultÚECOZSiteúchess_masters_WCC.pgn.bz2c              	   C   s¨   ddl }t ¡ }i }| | ¡�}dd„ |D ƒ}W 5 Q R X |D ]f}| d¡rv|dd…  dd¡\}}| d	¡|t|ƒ< q<|r<| d
¡}	| d¡}
|j	|	|
f|Ž i }q<|S )zÏRead chess games in pgn format in pgn_file.

    Filenames ending in .bz2 will be uncompressed.

    Return the MultiDiGraph of players connected by a chess game.
    Edges contain game data in a dict.

    r   Nc                 S   s   g | ]}|  ¡  d ¡‘qS )z
)ÚdecodeÚrstrip)Ú.0Úline© r   ú|/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/drawing/plot_chess_masters.pyÚ
<listcomp>2   s     z#chess_pgn_graph.<locals>.<listcomp>ú[é   éÿÿÿÿú ú"ÚWhiteZBlack)
Úbz2ÚnxZMultiDiGraphÚBZ2FileÚ
startswithÚsplitÚstripÚstrÚpopZadd_edge)Zpgn_filer   ÚGZgameZdatafileÚlinesr
   ÚtagÚvalueÚwhiteÚblackr   r   r   Úchess_pgn_graph$   s    	


r"   zLoaded z chess games between z	 players
c                 C   s   g | ]}t  |¡‘qS r   )ÚHZsubgraph)r	   Úcr   r   r   r   J   s     r   r   z/Note the disconnected component consisting of:
c                 C   s   h | ]\}}}|d  ’qS )r   r   )r	   r    r!   Ú	game_infor   r   r   Ú	<setcomp>O   s     r&   T)Údataz
From a total of z different openings,z-the following games used the Sicilian openingz5with the Najdorff 7...Qb6 "Poisoned Pawn" variation.
ZB97z vs Ú
z   z: c                 C   s    g | ]\}}t t ||¡ƒ‘qS r   )Úlenr   Zget_edge_data)r	   ÚuÚvr   r   r   r   `   s     g        ú-Ú1g      ð?z1/2g      à?c                 C   s   g | ]}t | d  ‘qS )é2   )Úwins)r	   r+   r   r   r   r   m   s     zReshevsky, Samuel H)çš™™™™™©?gš™™™™™¹¿zBotvinnik, Mikhail M)g¸…ëQ¸ž?g¸…ëQ¸®¿zSmyslov, Vassily V)r0   g¸…ëQ¸ž¿)é   r1   )Zfigsizeg333333Ó?Úm)ÚalphaÚwidthZ
edge_colorz#210070gÍÌÌÌÌÌì?)Z	node_sizeZ
node_colorr3   Úkr    gffffffæ?)ÚecÚfcr3   é   )Z	font_sizeZbboxZ	HelveticaÚbold)ZfontnameÚcolorZ
fontweightZfontsizez+World Chess Championship Games: 1886 - 1985Úrr:   gš™™™™™é?gš™™™™™¹?zedge width = # games playedÚcenter)ZhorizontalalignmentZ	transformZfontdictg¸…ëQ¸®?znode size = # games wonr0   Úoff)r   )6Ú__doc__Zmatplotlib.pyplotZpyplotZpltZnetworkxr   Zgame_detailsr"   r   ÚprintZnumber_of_edgesZnumber_of_nodesZto_undirectedr#   Zconnected_componentsZGccr)   ZnodesÚedgesZopeningsr    r!   r%   ÚsummaryÚitemsr5   r+   ZGraphZ	edgewidthÚdictÚfromkeysr/   r*   Údr   r;   ZnodesizeZkamada_kawai_layoutÚposZsubplotsZfigZaxZdraw_networkx_edgesZdraw_networkx_nodesZlabel_optionsZdraw_networkx_labelsZfontÚ	set_titleÚtextZ	transAxesZmarginsZtight_layoutZaxisÚshowr   r   r   r   Ú<module>   s†   
ÿ


úú

