gdxpds 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gdxpds/__init__.py ADDED
@@ -0,0 +1,34 @@
1
+ __version__ = "1.5.0"
2
+
3
+ from gdxpds.tools import (
4
+ Error,
5
+ GamsLoadError,
6
+ GamsDirFinder,
7
+ load_gdxcc,
8
+ info,
9
+ )
10
+ from gdxpds.read_gdx import (
11
+ to_dataframes,
12
+ list_symbols,
13
+ to_dataframe,
14
+ get_data_types,
15
+ get_subset_relationships,
16
+ )
17
+ from gdxpds.write_gdx import to_gdx
18
+ from gdxpds.gdx import GdxError
19
+
20
+ __all__ = [
21
+ "__version__",
22
+ "load_gdxcc",
23
+ "info",
24
+ "Error",
25
+ "GamsLoadError",
26
+ "GdxError",
27
+ "GamsDirFinder",
28
+ "to_dataframes",
29
+ "to_dataframe",
30
+ "list_symbols",
31
+ "get_data_types",
32
+ "get_subset_relationships",
33
+ "to_gdx",
34
+ ]
File without changes
Binary file
gdxpds/cli/__init__.py ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ import argparse
2
+ import os
3
+ import warnings
4
+
5
+ import gdxpds
6
+
7
+ import pandas as pd
8
+
9
+
10
+ def convert_csv_to_gdx(input_files, output_file, gams_dir=None):
11
+ # check input files
12
+ for ifile in input_files:
13
+ if not os.path.splitext(ifile)[1] in ['.csv','.txt']:
14
+ msg = "Input file '{}' is of unexpected type. Expected .csv or .txt.".format(ifile)
15
+ raise RuntimeError(msg)
16
+ if not os.path.isfile(ifile):
17
+ raise RuntimeError("'{}' is not a file.".format(ifile))
18
+
19
+ # convert input_files into one list of csvs
20
+ ifiles = []
21
+ for ifile in input_files:
22
+ if os.path.splitext(ifile)[1] == '.csv':
23
+ ifiles.append(ifile)
24
+ else:
25
+ # must be .txt
26
+ f = open(ifile, 'r')
27
+ for line in f:
28
+ if not line == '':
29
+ if os.path.splitext(line.strip())[1] == '.csv':
30
+ ifiles.append(line.strip())
31
+ else:
32
+ print("Skipping '{}' found in '{}'.".format(line,ifile))
33
+ f.close()
34
+ if len(ifiles) == 0:
35
+ raise RuntimeError("Nothing to convert.")
36
+
37
+ # convert list of csvs to map of dataframes
38
+ dataframes = {}
39
+ for ifile in ifiles:
40
+ dataframes[os.path.splitext(os.path.basename(ifile))[0]] = \
41
+ pd.read_csv(ifile, index_col=None)
42
+
43
+ gdxpds.to_gdx(dataframes, output_file, gams_dir)
44
+
45
+
46
+ def main(argv=None):
47
+ parser = argparse.ArgumentParser(description='''Accepts one or more input
48
+ csv files as input. Writes each csv as a separate symbol to an output
49
+ gdx.''')
50
+ parser.add_argument('-i', '--input', nargs='+', help='''List one or more
51
+ .csv or .txt files. The latter are assumed to be a line-delimited list
52
+ of .csv files.''')
53
+ parser.add_argument('-o', '--output', default='export.gdx', help='''Path
54
+ to the output gdx file. Will be overwritten if it already exists.''')
55
+ parser.add_argument('-g', '--gams_dir', help='''Path to GAMS installation
56
+ directory.''', default=None)
57
+
58
+ args = parser.parse_args(argv)
59
+
60
+ convert_csv_to_gdx(args.input, args.output, args.gams_dir)
61
+
62
+
63
+ def main_py_alias(argv=None):
64
+ warnings.warn(
65
+ "The 'csv_to_gdx.py' command is deprecated; use 'csv_to_gdx' (no .py). "
66
+ "This alias will be removed in a future release.",
67
+ DeprecationWarning,
68
+ stacklevel=2,
69
+ )
70
+ return main(argv)
71
+
72
+
73
+ if __name__ == "__main__":
74
+ main()
@@ -0,0 +1,58 @@
1
+ import argparse
2
+ import logging
3
+ import os
4
+ import warnings
5
+
6
+ import gdxpds
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ def convert_gdx_to_csv(in_gdx, out_dir, gams_dir=None):
12
+ # check inputs
13
+ if not os.path.exists(os.path.dirname(out_dir)):
14
+ raise RuntimeError("Parent directory of output directory '{}' does not exist.".format(out_dir))
15
+
16
+ # convert to pandas.DataFrames
17
+ dataframes = gdxpds.to_dataframes(in_gdx, gams_dir)
18
+
19
+ # write to files
20
+ if not os.path.exists(out_dir):
21
+ os.mkdir(out_dir)
22
+
23
+ for symbol_name, df in dataframes.items():
24
+ csv_path = os.path.join(out_dir, symbol_name + ".csv")
25
+ if os.path.exists(csv_path):
26
+ logger.info("Overwriting '{}'".format(csv_path))
27
+ df.to_csv(csv_path,
28
+ na_rep='NaN',
29
+ index=False)
30
+
31
+
32
+ def main(argv=None):
33
+ parser = argparse.ArgumentParser(description='''Reads a gdx file into
34
+ pandas dataframes, and then writes them out as csv files.''')
35
+ parser.add_argument('-i', '--in_gdx', help='''Input gdx file to be read
36
+ and exported as one csv per symbol.''')
37
+ parser.add_argument('-o', '--out_dir', default='./gdx_data/',
38
+ help='''Directory to which csvs are to be written.''')
39
+ parser.add_argument('-g', '--gams_dir', help='''Path to GAMS installation
40
+ directory.''', default=None)
41
+
42
+ args = parser.parse_args(argv)
43
+
44
+ convert_gdx_to_csv(args.in_gdx, os.path.realpath(args.out_dir), args.gams_dir)
45
+
46
+
47
+ def main_py_alias(argv=None):
48
+ warnings.warn(
49
+ "The 'gdx_to_csv.py' command is deprecated; use 'gdx_to_csv' (no .py). "
50
+ "This alias will be removed in a future release.",
51
+ DeprecationWarning,
52
+ stacklevel=2,
53
+ )
54
+ return main(argv)
55
+
56
+
57
+ if __name__ == "__main__":
58
+ main()
gdxpds/cli/main.py ADDED
@@ -0,0 +1,196 @@
1
+ """Top-level `gdxpds` CLI.
2
+
3
+ For now only one subcommand exists: `gdxpds test`, which verifies a fresh
4
+ installation against the local GAMS environment. If more subcommands are
5
+ added, split the dispatcher and per-command logic into separate modules.
6
+ """
7
+ import argparse
8
+ import os
9
+ import sys
10
+ import tempfile
11
+ from importlib.resources import as_file, files
12
+
13
+ import gdxpds
14
+ import gdxpds.gdx
15
+ import gdxpds.tools
16
+
17
+ import numpy as np
18
+
19
+
20
+ def main(argv=None):
21
+ parser = argparse.ArgumentParser(
22
+ prog="gdxpds",
23
+ description="gdx-pandas command-line utilities.",
24
+ )
25
+ parser.add_argument(
26
+ "--version", action="version", version=f"gdxpds {gdxpds.__version__}",
27
+ )
28
+ subparsers = parser.add_subparsers(dest="command", required=True)
29
+
30
+ info_parser = subparsers.add_parser(
31
+ "info",
32
+ help="Print gdxpds environment info (Python, bindings, GAMS_DIR, load status).",
33
+ )
34
+ info_parser.add_argument(
35
+ "-g", "--gams_dir", default=None,
36
+ help="Probe this GAMS directory instead of the loaded / discovered one.")
37
+
38
+ test_parser = subparsers.add_parser(
39
+ "test",
40
+ help="Verify the gdxpds installation against the local GAMS environment.",
41
+ )
42
+ test_parser.add_argument(
43
+ "-v", "--verbose", action="store_true",
44
+ help="Print exception tracebacks on failure.")
45
+ test_parser.add_argument(
46
+ "-g", "--gams_dir", default=None,
47
+ help="Use this GAMS directory for the verification run.")
48
+
49
+ args = parser.parse_args(argv)
50
+ if args.command == "info":
51
+ print(gdxpds.info(gams_dir=args.gams_dir))
52
+ return 0
53
+ if args.command == "test":
54
+ return _run_verify_install(args)
55
+ raise AssertionError(f"unhandled CLI command: {args.command!r}")
56
+
57
+
58
+ def _run_verify_install(args) -> int:
59
+ print(gdxpds.info(gams_dir=args.gams_dir))
60
+ print()
61
+ print("Verifying gdxpds installation...")
62
+ failures = []
63
+
64
+ gams_dir = _check_gams_install(args, failures)
65
+ if gams_dir is None:
66
+ return _verdict(failures)
67
+
68
+ if not _check_bindings(args, failures):
69
+ return _verdict(failures)
70
+
71
+ with as_file(files("gdxpds._verify_install") / "sample.gdx") as sample_path:
72
+ sample_path = str(sample_path)
73
+ if not _check_read(sample_path, args, failures):
74
+ return _verdict(failures)
75
+
76
+ with tempfile.TemporaryDirectory() as tmp:
77
+ roundtripped = os.path.join(tmp, "roundtrip.gdx")
78
+ if _check_roundtrip(sample_path, roundtripped, args, failures):
79
+ _check_specials(roundtripped, args, failures)
80
+
81
+ return _verdict(failures)
82
+
83
+
84
+ def _check_gams_install(args, failures):
85
+ try:
86
+ finder = gdxpds.tools.GamsDirFinder(gams_dir=args.gams_dir)
87
+ gdxpds.tools._require_gams_installation(finder)
88
+ _ok(f"GAMS install found at {finder.gams_dir}")
89
+ return finder.gams_dir
90
+ except Exception as exc:
91
+ _fail("Could not locate a GAMS installation", exc, args)
92
+ _hint("Set $env:GAMS_DIR (PowerShell) or $GAMS_DIR (POSIX) to your "
93
+ "GAMS install directory, put `gams` on PATH, or pass -g/--gams_dir.")
94
+ failures.append("gams_install")
95
+ return None
96
+
97
+
98
+ def _check_bindings(args, failures):
99
+ source = gdxpds.tools._bindings_source
100
+ if source is None:
101
+ _fail("GDX bindings not loaded",
102
+ RuntimeError("see info() output above for details"),
103
+ args)
104
+ _hint("Install `gamsapi` matched to your GAMS version: "
105
+ "pip install gamsapi[transfer]==<your GAMS version>")
106
+ failures.append("bindings")
107
+ return False
108
+ _ok(f"GDX bindings loaded: {source}")
109
+ return True
110
+
111
+
112
+ def _check_read(sample_path, args, failures):
113
+ try:
114
+ with gdxpds.gdx.GdxFile(gams_dir=args.gams_dir, lazy_load=False) as gdx:
115
+ gdx.read(sample_path)
116
+ names = {s.name for s in gdx}
117
+ assert names == {"t", "sub_t", "p", "v"}, f"unexpected symbols: {names}"
118
+ assert gdx["p"].num_records == 6
119
+ assert gdx["t"].domain_type == gdxpds.gdx.GamsDomainType.NONE, (
120
+ f"t.domain_type = {gdx['t'].domain_type}, expected NONE")
121
+ sub_t = gdx["sub_t"]
122
+ assert sub_t.domain_type == gdxpds.gdx.GamsDomainType.REGULAR, (
123
+ f"sub_t.domain_type = {sub_t.domain_type}, expected REGULAR")
124
+ assert sub_t.domain is not None and sub_t.domain[0] is gdx["t"], (
125
+ "sub_t.domain[0] does not resolve to gdx['t']")
126
+ _ok(f"Read embedded sample.gdx ({sample_path})")
127
+ return True
128
+ except Exception as exc:
129
+ _fail("Could not read embedded sample.gdx", exc, args)
130
+ failures.append("read")
131
+ return False
132
+
133
+
134
+ def _check_roundtrip(sample_path, out_path, args, failures):
135
+ try:
136
+ with gdxpds.gdx.GdxFile(gams_dir=args.gams_dir, lazy_load=False) as gdx:
137
+ gdx.read(sample_path)
138
+ with gdx.clone() as gdx2:
139
+ gdx2.write(out_path)
140
+ with gdxpds.gdx.GdxFile(gams_dir=args.gams_dir, lazy_load=False) as gdx:
141
+ gdx.read(out_path)
142
+ assert {s.name for s in gdx} == {"t", "sub_t", "p", "v"}
143
+ assert gdx["sub_t"].domain_type == gdxpds.gdx.GamsDomainType.REGULAR, (
144
+ "sub_t.domain_type did not survive round-trip as REGULAR")
145
+ _ok("Round-trip write->read preserves all symbols")
146
+ return True
147
+ except Exception as exc:
148
+ _fail("Round-trip write->read failed", exc, args)
149
+ failures.append("roundtrip")
150
+ return False
151
+
152
+
153
+ def _check_specials(out_path, args, failures):
154
+ try:
155
+ with gdxpds.gdx.GdxFile(gams_dir=args.gams_dir, lazy_load=False) as gdx:
156
+ gdx.read(out_path)
157
+ values = gdx["p"].dataframe["Value"].tolist()
158
+
159
+ assert any(v == 1.0 for v in values), "normal value 1.0 missing"
160
+ assert any(v == np.inf for v in values), "+Inf missing"
161
+ assert any(v == -np.inf for v in values), "-Inf missing"
162
+ # NA and UNDEF both collapse to NaN under pandas.
163
+ nan_count = sum(1 for v in values if v != v)
164
+ assert nan_count >= 1, f"expected at least one NaN, found {nan_count}"
165
+ _ok("Special values (+Inf, -Inf, NaN) survive round-trip")
166
+ except Exception as exc:
167
+ _fail("Special-value preservation failed", exc, args)
168
+ failures.append("specials")
169
+
170
+
171
+ def _ok(msg):
172
+ print(f" [OK] {msg}")
173
+
174
+
175
+ def _fail(msg, exc, args):
176
+ print(f" [FAIL] {msg}: {exc}", file=sys.stderr)
177
+ if args.verbose:
178
+ import traceback
179
+ traceback.print_exception(exc, file=sys.stderr)
180
+
181
+
182
+ def _hint(msg):
183
+ print(f" hint: {msg}", file=sys.stderr)
184
+
185
+
186
+ def _verdict(failures) -> int:
187
+ print()
188
+ if failures:
189
+ print(f"FAILED: {len(failures)} check(s) failed: {', '.join(failures)}")
190
+ return 1
191
+ print("PASSED: gdxpds installation verified.")
192
+ return 0
193
+
194
+
195
+ if __name__ == "__main__":
196
+ sys.exit(main())