Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/pygambit.api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Creating, reading, and writing games
read_efg
read_nfg
read_agg
read_bagg

Game.new_tree
Game.new_table
Expand Down
1 change: 1 addition & 0 deletions src/pygambit/gambit.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ cdef extern from "util.h":
c_Game ParseEfgGame(string, bint) except +IOError
c_Game ParseNfgGame(string, bint) except +IOError
c_Game ParseAggGame(string, bint) except +IOError
c_Game ParseBaggGame(string, bint) except +IOError
string WriteEfgFile(c_Game)
string WriteNfgFile(c_Game)
string WriteNfgFileSupport(c_StrategySupportProfile) except +IOError
Expand Down
39 changes: 35 additions & 4 deletions src/pygambit/game.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read_gbt(filepath_or_buffer: str | pathlib.Path | io.IOBase,

See Also
--------
read_efg, read_nfg, read_agg
read_efg, read_nfg, read_agg, read_bagg
"""
return read_game(filepath_or_buffer, normalize_labels, parser=ParseGbtGame)

Expand Down Expand Up @@ -111,7 +111,7 @@ def read_efg(filepath_or_buffer: str | pathlib.Path | io.IOBase,

See Also
--------
read_gbt, read_nfg, read_agg
read_gbt, read_nfg, read_agg, read_bagg
"""
return read_game(filepath_or_buffer, normalize_labels, parser=ParseEfgGame)

Expand Down Expand Up @@ -142,7 +142,7 @@ def read_nfg(filepath_or_buffer: str | pathlib.Path | io.IOBase,

See Also
--------
read_gbt, read_efg, read_agg
read_gbt, read_efg, read_agg, read_bagg
"""
return read_game(filepath_or_buffer, normalize_labels, parser=ParseNfgGame)

Expand Down Expand Up @@ -173,11 +173,42 @@ def read_agg(filepath_or_buffer: str | pathlib.Path | io.IOBase,

See Also
--------
read_gbt, read_efg, read_nfg
read_gbt, read_efg, read_nfg, read_bagg
"""
return read_game(filepath_or_buffer, normalize_labels, parser=ParseAggGame)


def read_bagg(filepath_or_buffer: str | pathlib.Path | io.IOBase,
normalize_labels: bool = False) -> Game:
"""Construct a game from its serialised representation in a BAGG file.

Parameters
----------
filepath_or_buffer : str, pathlib.Path or io.IOBase
The path to the file containing the game representation or file-like object
normalize_labels : bool (default False)
Ensure all labels are nonempty and unique within their scopes.
This will be enforced in a future version of Gambit.

Returns
-------
Game
A game constructed from the representation in the file.

Raises
------
IOError
If the file cannot be opened or read
ValueError
If the contents of the file are not a valid game representation.

See Also
--------
read_gbt, read_efg, read_nfg, read_agg
"""
return read_game(filepath_or_buffer, normalize_labels, parser=ParseBaggGame)


@cython.cclass
class GameNodes:
"""Represents the set of nodes in a game."""
Expand Down
7 changes: 7 additions & 0 deletions src/pygambit/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <sstream>
#include "gambit.h"
#include "games/gameagg.h"
#include "games/gamebagg.h"
#include "games/nash.h"

using namespace std;
Expand Down Expand Up @@ -61,6 +62,12 @@ Game ParseAggGame(std::string const &s, bool p_normalizeLabels)
return ReadAggFile(f);
}

Game ParseBaggGame(std::string const &s, bool p_normalizeLabels)
{
std::istringstream f(s);
return ReadBaggFile(f);
}

std::string WriteEfgFile(const Game &p_game)
{
std::ostringstream f;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ def test_read_agg_invalid():
gbt.read_agg(game_path)


def test_read_bagg():
game_path = os.path.join("contrib", "games", "Bayesian-Coffee-3-2-2-3.bagg")
game = gbt.read_bagg(game_path)
assert isinstance(game, gbt.Game)


def test_read_bagg_invalid():
game_path = os.path.join(
"tests", "test_games", "2x2x2_nfg_from_local_max_cut_2_pure_1_mixed_eq.nfg"
)
with pytest.raises(ValueError):
gbt.read_bagg(game_path)


def test_read_gbt_invalid():
game_path = os.path.join(
"tests", "test_games", "2x2x2_nfg_from_local_max_cut_2_pure_1_mixed_eq.nfg"
Expand Down
Loading