Exemplo n.º 1
0
    def handle_savesgf(self, args):
        try:
            pathname = args[0]
        except IndexError:
            gtp_engine.report_bad_arguments()

        sgf_game = sgf.Sgf_game(self.board_size)
        root = sgf_game.get_root()
        root.set('KM', self.komi)
        root.set('AP', ("gomill", __version__))
        sgf_game.set_date()
        if self.handicap is not None:
            root.set('HA', self.handicap)
        for arg in args[1:]:
            try:
                identifier, value = arg.split("=", 1)
                if not identifier.isalpha():
                    raise ValueError
                identifier = identifier.upper()
                value = value.replace("\\_", " ").replace("\\\\", "\\")
            except Exception:
                gtp_engine.report_bad_arguments()
            root.set_raw(identifier, sgf_grammar.escape_text(value))
        sgf_moves.set_initial_position(sgf_game, self.history_base)
        for history_move in self.move_history:
            node = sgf_game.extend_main_sequence()
            node.set_move(history_move.colour, history_move.move)
            if history_move.comments is not None:
                node.set("C", history_move.comments)
        sgf_moves.indicate_first_player(sgf_game)
        try:
            self._save_file(pathname, sgf_game.serialise())
        except EnvironmentError, e:
            raise GtpError("error writing file: %s" % e)
Exemplo n.º 2
0
def convert_sgf_to_utf(content):
    game = sgf.Sgf_game.from_string(content)
    gameroot = game.get_root()
    sgf_moves.indicate_first_player(game)  # adding the PL property on the root
    if node_has(gameroot, "CA"):
        ca = node_get(gameroot, "CA")
        if ca == "UTF-8":
            # the sgf is already in UTF, so we accept it directly
            return game
        else:
            log(2, "Encoding is", ca)
            log(2, "Converting from", ca, "to UTF-8")
            encoding = (codecs.lookup(ca).name.replace(
                "_", "-").upper().replace("ISO8859",
                                          "ISO-8859"))  # from gomill code
            content = game.serialise()
            content = content.decode(
                encoding,
                errors='ignore')  # transforming content into a unicode object
            content = content.replace("CA[" + ca + "]", "CA[UTF-8]")
            game = sgf.Sgf_game.from_string(
                content.encode("utf-8")
            )  # sgf.Sgf_game.from_string requires str object, not unicode
            return game
    else:
        log(
            1,
            "the sgf has no declared encoding, we will enforce UTF-8 encoding")
        content = game.serialise()
        content = content.decode("utf", errors="replace").encode("utf")
        game = sgf.Sgf_game.from_string(content, override_encoding="UTF-8")
        return game
Exemplo n.º 3
0
def test_indicate_first_player(tc):
    g1 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];B[aa];W[ab])")
    sgf_moves.indicate_first_player(g1)
    tc.assertEqual(g1.serialise(), "(;FF[4]GM[1]SZ[9];B[aa];W[ab])\n")
    g2 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];W[aa];B[ab])")
    sgf_moves.indicate_first_player(g2)
    tc.assertEqual(g2.serialise(), "(;FF[4]GM[1]PL[W]SZ[9];W[aa];B[ab])\n")
    g3 = sgf.Sgf_game.from_string("(;AW[bc]FF[4]GM[1]SZ[9];B[aa];W[ab])")
    sgf_moves.indicate_first_player(g3)
    tc.assertEqual(g3.serialise(),
                   "(;FF[4]AW[bc]GM[1]PL[B]SZ[9];B[aa];W[ab])\n")
    g4 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];C[no game])")
    sgf_moves.indicate_first_player(g4)
    tc.assertEqual(g4.serialise(), "(;FF[4]GM[1]SZ[9];C[no game])\n")
Exemplo n.º 4
0
def test_indicate_first_player(tc):
    g1 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];B[aa];W[ab])")
    sgf_moves.indicate_first_player(g1)
    tc.assertEqual(g1.serialise(),
                   "(;FF[4]GM[1]SZ[9];B[aa];W[ab])\n")
    g2 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];W[aa];B[ab])")
    sgf_moves.indicate_first_player(g2)
    tc.assertEqual(g2.serialise(),
                   "(;FF[4]GM[1]PL[W]SZ[9];W[aa];B[ab])\n")
    g3 = sgf.Sgf_game.from_string("(;AW[bc]FF[4]GM[1]SZ[9];B[aa];W[ab])")
    sgf_moves.indicate_first_player(g3)
    tc.assertEqual(g3.serialise(),
                   "(;FF[4]AW[bc]GM[1]PL[B]SZ[9];B[aa];W[ab])\n")
    g4 = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];C[no game])")
    sgf_moves.indicate_first_player(g4)
    tc.assertEqual(g4.serialise(),
                   "(;FF[4]GM[1]SZ[9];C[no game])\n")
Exemplo n.º 5
0
def rsgf2csv(filename):
    game = open_sgf(filename)
    sgf_moves.indicate_first_player(game)
    gameroot = game.get_root()

    max_move = get_moves_number(gameroot)

    csv = open(filename + ".csv", "w")

    black = ""
    if gameroot.has_property("PB"):
        black = gameroot.get("PB")
    new_line = "black: " + black
    csv.write(new_line + "\n")

    white = ""
    if gameroot.has_property("PW"):
        white = gameroot.get("PW")
    new_line = "white: " + white
    csv.write(new_line + "\n")

    date = ""
    if gameroot.has_property("DT"):
        date = gameroot.get("DT")
    new_line = "date: " + date
    csv.write(new_line + "\n")

    event = ""
    if gameroot.has_property("EV"):
        event = gameroot.get("EV")
    new_line = "event: " + event
    csv.write(new_line + "\n")

    new_line = ""
    csv.write(new_line + "\n")

    headers_left = ["Move number", "Move color"]
    headers_game = [
        "Move", "Win rate", "Value Network", "Monte Carlo", "Evaluation",
        "Rave", "Score Estimation", "Policy Network", "Simulations",
        "Follow up", "Variations"
    ]
    headers_bot = headers_game[:]

    header_first_line = [""] * len(headers_left) + ["Game move"] * len(
        headers_game) + ["Bot move"] * len(headers_bot)

    table = [headers_left[:] + headers_game[:] + headers_bot[:]]

    nb_max_variations = 1

    columns_sgf_properties = [
        "CBM", "BWWR", "VNWR", "MCWR", "EVAL", "RAVE", "ES", "PNV", "PLYO",
        "nothing_here", "nothing_here"
    ]

    for m in range(1, max_move):
        one_move = get_node(gameroot, m)
        table.append(["" for i in range(len(table[0]))])
        #-- Move number
        table[m][0] = m

        #-- colour
        color = guess_color_to_play(gameroot, m)
        table[m][1] = color.upper()

        #-- game move
        one_move = get_node(gameroot, m)
        c = len(headers_left)
        for header, sgf_property in zip(headers_bot, columns_sgf_properties):
            if header == "Follow up":
                break
            try:
                next_move = one_move[0]  #taking data from the following move
                if sgf_property == "PLYO":
                    next_move = one_move[1]
                if sgf_property == "PNV":
                    for variation in one_move.parent[1:]:
                        if node_get(variation, color.upper()) == node_get(
                                one_move, color.upper()):
                            next_move = variation
                            break

                if sgf_property == "CBM":
                    if color == "w":
                        sgf_property = "B"
                    else:
                        sgf_property = "W"

                if node_has(next_move, sgf_property):
                    if sgf_property == "B" or sgf_property == "W":
                        value = node_get(one_move, color.upper())
                        value = ij2gtp(value).upper()
                    else:
                        value = node_get(next_move, sgf_property)

                    if "%/" in value:
                        if color == "b":
                            value = float(value.split("%/")[0])
                            value = round(value, 2)
                            value = str(value) + "%"
                        else:
                            value = float(value.split("/")[1][:-1])
                            value = round(value, 2)
                            value = str(value) + "%"
                    table[m][c] = value
            except:
                pass
            c += 1

        #-- variations

        if len(one_move.parent[1:]) > nb_max_variations:
            nb_max_variations = len(one_move.parent[1:])
            headers_bot = headers_game[:] * nb_max_variations
            table[
                0] = headers_left[:] + headers_game[:] + headers_bot[:] * nb_max_variations
            header_first_line = [""] * len(headers_left) + ["Game move"] * len(
                headers_game) + ["Bot move"] * len(headers_game)
            for i in range(2, nb_max_variations + 1):
                header_first_line += ["Bot move " + str(i)] * len(headers_game)

        c = len(headers_left + headers_game)
        if len(one_move.parent[1:]):
            table[m][c - 1] = len(one_move.parent[1:])
        nbv = 1
        for one_variation in one_move.parent[1:]:
            nbv += 1
            for header, sgf_property in zip(headers_bot,
                                            columns_sgf_properties):
                #if header=="Follow up":
                #	break
                try:
                    #one_move=get_node(gameroot,m)
                    #if sgf_property in ("PNV","PLYO"):
                    #	one_move=one_variation
                    one_move = one_variation

                    if sgf_property == "CBM":
                        if color == "w":
                            sgf_property = "W"
                        else:
                            sgf_property = "B"

                    if node_has(one_move, sgf_property):
                        if sgf_property == "B" or sgf_property == "W":
                            value = node_get(one_move, color.upper())
                            value = ij2gtp(value).upper()
                        else:
                            value = node_get(one_move, sgf_property)
                        if "%/" in value:
                            if color == "b":
                                value = float(value.split("%/")[0])
                                value = round(value, 2)
                                value = str(value) + "%"
                            else:
                                value = float(value.split("/")[1][:-1])
                                value = round(value, 2)
                                value = str(value) + "%"
                        table[m][c] = value

                except:
                    pass
                c += 1

    for c in range(len(table[0])):
        for m in range(len(table[1:])):
            try:
                if table[1 + m][c] != "":
                    break
            except:
                #no data (probably no variation data, so keep searching)
                pass

        if table[1 + m][c] == "":
            table[0][c] = ""

    c = len(headers_left + headers_game) - 1
    header_first_line[c] = ""
    c = 0
    for header in table[0]:
        if header != "":
            csv.write(header_first_line[c] + ",")
        c += 1

    csv.write("\n")

    for m in table:
        line = ""
        for value, header in zip(m, table[0]):
            if header != "":
                line += str(value).strip() + ","
        #print line
        csv.write(line + "\n")

    log("saving")
    csv.close()
Exemplo n.º 6
0
def rsgf2csv(filename):
	game=open_sgf(filename)
	sgf_moves.indicate_first_player(game)
	gameroot=game.get_root()

	max_move=get_moves_number(gameroot)

	csv=open(filename+".csv","w")

	black=""
	if gameroot.has_property("PB"):
		black=gameroot.get("PB")
	new_line="black: "+black
	csv.write(new_line+"\n")


	white=""
	if gameroot.has_property("PW"):
		white=gameroot.get("PW")
	new_line="white: "+white
	csv.write(new_line+"\n")

	date=""
	if gameroot.has_property("DT"):
		date=gameroot.get("DT")
	new_line="date: "+date
	csv.write(new_line+"\n")

	event=""
	if gameroot.has_property("EV"):
		event=gameroot.get("EV")
	new_line="event: "+event
	csv.write(new_line+"\n")

	new_line=""
	csv.write(new_line+"\n")
	
	headers_left=["Move number", "Move color"]
	headers_game=["Move", "Win rate", "Value Network", "Monte Carlo", "Evaluation", "Rave", "Score Estimation", "Policy Network", "Simulations", "Follow up", "Variations"]
	headers_bot=headers_game[:]
	
	header_first_line=[""]*len(headers_left)+["Game move"]*len(headers_game)+["Bot move"]*len(headers_bot)
	
	table=[headers_left[:]+headers_game[:]+headers_bot[:]]
	
	nb_max_variations=1
	
	columns_sgf_properties=["CBM","BWWR","VNWR","MCWR","EVAL","RAVE","ES","PNV","PLYO","nothing_here","nothing_here"]
	
	for m in range(1,max_move):
		one_move=get_node(gameroot,m)
		table.append(["" for i in range(len(table[0]))])
		#-- Move number
		table[m][0]=m
		
		#-- colour
		color=guess_color_to_play(gameroot,m)
		table[m][1]=color.upper()
		
		#-- game move
		one_move=get_node(gameroot,m)
		c=len(headers_left)
		for header, sgf_property in zip(headers_bot,columns_sgf_properties):
			if header=="Follow up":
				break
			try:
				next_move=one_move[0] #taking data from the following move
				if sgf_property =="PLYO":
					next_move=one_move[1]
				if  sgf_property =="PNV":
					for variation in one_move.parent[1:]:
						if node_get(variation,color.upper())==node_get(one_move,color.upper()):
							next_move=variation
							break
					
				if sgf_property=="CBM":
					if color=="w":
						sgf_property="B"
					else:
						sgf_property="W"
				
				if node_has(next_move,sgf_property):
					if sgf_property=="B" or sgf_property=="W":
						value=node_get(one_move,color.upper())
						value=ij2gtp(value)
					else:
						value=node_get(next_move,sgf_property)
						
					if "%/" in value:
						if color=="b":
							value=float(value.split("%/")[0])
							value=round(value,2)
							value=str(value)+"%"
						else:
							value=float(value.split("/")[1][:-1])
							value=round(value,2)
							value=str(value)+"%"
					table[m][c]=value
			except:
				pass
			c+=1
		
		#-- variations
		
		if len(one_move.parent[1:])>nb_max_variations:
			nb_max_variations=len(one_move.parent[1:])
			headers_bot=headers_game[:]*nb_max_variations
			table[0]=headers_left[:]+headers_game[:]+headers_bot[:]*nb_max_variations
			header_first_line=[""]*len(headers_left)+["Game move"]*len(headers_game)+["Bot move"]*len(headers_game)
			for i in range(2,nb_max_variations+1):
				header_first_line+=["Bot move "+str(i)]*len(headers_game)
		
		c=len(headers_left+headers_game)
		if len(one_move.parent[1:]):
			table[m][c-1]=len(one_move.parent[1:])
		nbv=1
		for one_variation in one_move.parent[1:]:
			nbv+=1
			for header, sgf_property in zip(headers_bot,columns_sgf_properties):
				#if header=="Follow up":
				#	break
				try:
					#one_move=get_node(gameroot,m)
					#if sgf_property in ("PNV","PLYO"):
					#	one_move=one_variation
					one_move=one_variation
					
					if sgf_property=="CBM":
						if color=="w":
							sgf_property="W"
						else:
							sgf_property="B"
					
					if node_has(one_move,sgf_property):
						if sgf_property=="B" or sgf_property=="W":
							value=node_get(one_move,color.upper())
							value=ij2gtp(value)
						else:
							value=node_get(one_move,sgf_property)
						if "%/" in value:
							if color=="b":
								value=float(value.split("%/")[0])
								value=round(value,2)
								value=str(value)+"%"
							else:
								value=float(value.split("/")[1][:-1])
								value=round(value,2)
								value=str(value)+"%"
						table[m][c]=value
						
				except:
					pass
				c+=1

	
	for c in range(len(table[0])):
		for m in range(len(table[1:])):
			try:
				if table[1+m][c]!="":
					break
			except:
				#no data (probably no variation data, so keep searching)
				pass
		
		if table[1+m][c]=="":
			table[0][c]=""
	
	c=len(headers_left+headers_game)-1
	header_first_line[c]=""
	c=0
	for header in table[0]:
		if header!="":
			csv.write(header_first_line[c]+",")
		c+=1
	
	csv.write("\n")
	
	for m in table:
		line=""
		for value, header in zip(m,table[0]):
			if header!="":
				line+=str(value).strip()+","
		#print line
		csv.write(line+"\n")
	

	log("saving")
	csv.close()