KataGo icon indicating copy to clipboard operation
KataGo copied to clipboard

How to use katago from other python application in linux?

Open fluffy-stuff opened this issue 2 years ago • 4 comments

How can I use katago from other applications on Linux?

I am not very familiar with programming, but I would like to create a GUI side application that uses katago as an engine. if I want to use Python to send commands to katago and get the returned strings, how should I do it? I have tried using subprocesser.Popen, but when I call katago, the process is waiting for katago's command and subsequent processes are not called.

thank you.

fluffy-stuff avatar May 18 '23 05:05 fluffy-stuff

Hi there, thanks for the interest! Very recently added was this example file, does it help? https://github.com/lightvector/KataGo/blob/master/python/query_analysis_engine_example.py

lightvector avatar May 18 '23 05:05 lightvector

I'll check it out, thanks!

fluffy-stuff avatar May 18 '23 05:05 fluffy-stuff

@lightvector Looks like in this example, we use a lot of string parsing to query the running process. There's really no easier way to do this, i.e. a library call?

lgnashold avatar Aug 17 '23 18:08 lgnashold

You're welcome to wrap up the example code into a library if you wish! Presumably it would have sgfmill as a dependency? Unless you have your own personal code with custom rules implementation and data structures for Go that is separate from sgfmill.

KataGo is a separate running program, and it's C++. Sice C++ and python use very different in-memory structures, a natural choice is for the communication to be in a common data format for which well-developed libraries exist in both languages, such as JSON. So that's what we have here.

What string parsing are you referring to? JSON is already handling most of that for you, the little bit that remains is handled by the provided sgfmill_to_str, and maybe one more function you need for the inverse, to convert move formats between KataGo and sgfmill.

def str_to_sgfmill(s: str) -> Move:
    if move == "pass":
        return "pass"
    x = "ABCDEFGHJKLMNOPQRSTUVWXYZ".index(s.upper()[0])
    y = int(s[1:]) - 1
    return (y,x)

At that point, you can just call katago.query(board, moves, komi), and you should get back fully structured dicts, lists, etc. with the results of whatever analysis you set in your query, according to the spec in https://github.com/lightvector/KataGo/blob/master/docs/Analysis_Engine.md.

lightvector avatar Aug 17 '23 23:08 lightvector