how to create a labeled epd from pgn?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

zenpawn
Posts: 349
Joined: Sat Aug 06, 2016 8:31 pm
Location: United States

how to create a labeled epd from pgn?

Post by zenpawn »

I'd like to create my own set of positions with game result labels for Texel tuning, ala the quiet-labeled.epd file shared on this forum, which contains lines such as:

rn2kb1r/ppp1pp1p/2q3p1/3nN3/3P4/2N1P3/PPb2PPP/R1B1KB1R b KQkq - c9 "0-1";

The first two steps in the process are easy enough: 1) generate a file of quiet position FENs, 2) run fast games starting from these positions to get results. Is there a script that will take the PGN from #2 and create a labeled epd file combining the starting positions with their results (either in a comment or just appended to the end of the line)?

Thanks,
-Erin
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: how to create a labeled epd from pgn?

Post by AlvaroBegue »

zenpawn wrote:I'd like to create my own set of positions with game result labels for Texel tuning, ala the quiet-labeled.epd file shared on this forum, which contains lines such as:

rn2kb1r/ppp1pp1p/2q3p1/3nN3/3P4/2N1P3/PPb2PPP/R1B1KB1R b KQkq - c9 "0-1";

The first two steps in the process are easy enough: 1) generate a file of quiet position FENs, 2) run fast games starting from these positions to get results. Is there a script that will take the PGN from #2 and create a labeled epd file combining the starting positions with their results (either in a comment or just appended to the end of the line)?

Thanks,
-Erin
Since the FEN and the result are both described in the header of the PGN file, it should be very easy to parse. I would use Perl to do it. If you post an example game from your database, I'll give you a tiny piece of Perl that will do what you want.
zenpawn
Posts: 349
Joined: Sat Aug 06, 2016 8:31 pm
Location: United States

Re: how to create a labeled epd from pgn?

Post by zenpawn »

That would be great. Here's an example:

Code: Select all

[Event "SF8-SF8-STS-10sec"]
[Site "ROOK"]
[Date "2017.12.02"]
[Round "21"]
[White "Stockfish_8_x64"]
[Black "Stockfish_8_x64 [001]"]
[Result "1/2-1/2"]
[BlackElo "3456"]
[Time "05:21:30"]
[WhiteElo "3456"]
[TimeControl "10+0"]
[SetUp "1"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 5. Kg1 {(Kh2-g1 Qf5-g6+) 0.00/19
0} Qg6+ {(Qf5-g6+) 0.00/19 0} 6. Kh2 {(Kg1-h2) 0.00/26 0} Qf5 {(Qg6-f5)
0.00/23 0} 7. Kg1 {(Kh2-g1) 0.00/23 0} Qg6+ {(Qf5-g6+) 0.00/19 0 3-fold
repetition} 1/2-1/2
brtzsnr
Posts: 433
Joined: Fri Jan 16, 2015 4:02 pm

Re: how to create a labeled epd from pgn?

Post by brtzsnr »

Given a set of PGNs I use the following command (works on my Debian)

Code: Select all

cat ok/pgn-split.* | grep "FEN\|Result\|ECO" | tac | paste - - -d' ' | grep -v ECO | sed 's/\[FEN "//' | sed 's/0 1"\] \[Result/c9/' | sed 's/\]/;/' > quiet-labeled.epd

Let's split:

Code: Select all

cat ok/pgn-split.*
just concatenates everything

Code: Select all

grep "FEN\|Result\|ECO"
keeps only the lines containing FEN, ECO or RESULT

Code: Select all

tac
reverses the lines

Code: Select all

paste - - -d' ' 
merges the lines two by two

Code: Select all

grep -v ECO
removes ECO lines. Some cutechess versions output ECO instead of FEN

Code: Select all

sed 's/\[FEN "//' | sed 's/0 1"\] \[Result/c9/' | sed 's/\]/;/'
removes extra characters and adds c9.
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: how to create a labeled epd from pgn?

Post by jdart »

I have two tools for this in my source tree (https://github.com/jdart1/arasan-chess):

1. A program called pgnselect that takes a PGN file and produces a sampled set of EPD positions (unlabeled). If you run it with the -q switch, it will select quiet positions. This is built from my Makefile using "make utils".

2. A python3 script named "label_positions.py" in the tools directory that takes the unlabeled EPD positions and add labels, using cutechess-cli. Cutechess-cli should be set up with an engines.json file containing the engine configuration. This script will probably require a few edits to adapt to your environment.

--Jon
zenpawn
Posts: 349
Joined: Sat Aug 06, 2016 8:31 pm
Location: United States

Re: how to create a labeled epd from pgn?

Post by zenpawn »

Thanks, everyone. These ought to do it. :)
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: how to create a labeled epd from pgn?

Post by AlvaroBegue »

Code: Select all

cat something.pgn | perl -ne '$result = $1 if /Result (\".*\")/; print "$1 $result\n" if /FEN \"(.*)\"/'
The invocation `perl -ane' means "for each line of input, execute the following piece of Perl code". What the code does is remember the result if it finds a line that matches some pattern, and print out a line if it matches a different pattern. `$1' is the part of the pattern captured between the parentheses.
zenpawn
Posts: 349
Joined: Sat Aug 06, 2016 8:31 pm
Location: United States

Re: how to create a labeled epd from pgn?

Post by zenpawn »

Good stuff. Thanks.