Skip to content

Commit 17fba2f

Browse files
committed
Improving documentation
1 parent 0ee2f4a commit 17fba2f

8 files changed

+390
-84
lines changed

bin/csv_to_gdx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Linux that causes a segmentation fault.
66
import gdxpds
77

8-
import pandas as pds
8+
import pandas as pd
99

1010
def convert_csv_to_gdx(input_files, output_file, gams_dir=None):
1111
# check input files
@@ -38,7 +38,7 @@ def convert_csv_to_gdx(input_files, output_file, gams_dir=None):
3838
dataframes = {}
3939
for ifile in ifiles:
4040
dataframes[os.path.splitext(os.path.basename(ifile))[0]] = \
41-
pds.read_csv(ifile,index_col=None)
41+
pd.read_csv(ifile,index_col=None)
4242

4343
gdxpds.to_gdx(dataframes, output_file, gams_dir)
4444

doc/source/overview.rst

+56-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Backend Classes
7272
~~~~~~~~~~~~~~~
7373

7474
The basic functionalities described above can also be achieved with
75-
direct use of the backend classes now available in ``gdxpds.gdx``. To
75+
direct use of the backend classes available in :py:mod:`gdxpds.gdx`. To
7676
duplicate the GDX read functionality shown above one would write:
7777

7878
.. code:: python
@@ -87,27 +87,74 @@ duplicate the GDX read functionality shown above one would write:
8787
df = symbol.dataframe
8888
print(f"Doing work with {symbol_name}:\n{df}")
8989
90-
The backend especially gives more control over creating new data in GDX
91-
format. For example:
90+
This interface also provides more precise control over what data is
91+
loaded at any particular time:
9292

9393
.. code:: python
9494
9595
import gdxpds
9696
97+
gdx_file = 'C:\path_to_my_gdx\data.gdx'
98+
with gdxpds.gdx.GdxFile() as f: # lazy_load defaults to True
99+
f.read(gdx_file)
100+
101+
f['param_1'].load()
102+
df_1 = f['param_1'].dataframe
103+
f['param_1'].unload()
104+
105+
f['param_12'].load()
106+
df_12 = f['param_12'].dataframe
107+
f['param_12'].unload()
108+
109+
And enables more transparent creation of new GDX files:
110+
111+
.. code:: python
112+
113+
from itertools import product
114+
115+
from gdxpds.gdx import GdxFile, GdxSymbol, GamsDataType, append_set, append_paramter
116+
import pandas as pd
117+
97118
out_file = 'my_new_gdx_data.gdx'
98-
with gdxpds.gdx.GdxFile() as gdx:
119+
with GdxFile() as gdx:
120+
99121
# Create a new set with one dimension
100-
gdx.append(gdxpds.gdx.GdxSymbol('my_set',gdxpds.gdx.GamsDataType.Set,dims=['u']))
101-
data = pds.DataFrame([['u' + str(i)] for i in range(1,11)])
122+
gdx.append(GdxSymbol('my_set',GamsDataType.Set,dims=['u']))
123+
data = pd.DataFrame([['u' + str(i)] for i in range(1,11)])
102124
data['Value'] = True
103125
gdx[-1].dataframe = data
126+
104127
# Create a new parameter with one dimension
105-
gdx.append(gdxpds.gdx.GdxSymbol('my_parameter',gdxpds.gdx.GamsDataType.Parameter,dims=['u']))
106-
data = pds.DataFrame([['u' + str(i), i*100] for i in range(1,11)],
107-
columns=(gdx[-1].dims + gdx[-1].value_col_names))
128+
gdx.append(GdxSymbol('my_parameter',GamsDataType.Parameter,dims=['u']))
129+
data = pd.DataFrame([['u' + str(i), i*100] for i in range(1,11)],
130+
columns=(gdx[-1].dims + gdx[-1].value_col_names))
108131
gdx[-1].dataframe = data
132+
133+
# Create new sets with convenience function append_set
134+
append_set(gdx, "my_other_set", pd.DataFrame(
135+
[['v' + str(i)] for i in range(1,6)], columns = ['v'])
136+
)
137+
append_set(gdx, "my_combo_set", pd.DataFrame(
138+
product(['u' + str(i) for i in range(1,11)], ['v' + str(i) for i in range(1,6)]),
139+
columns = ['u', 'v'])
140+
)
141+
142+
# Create a new parameter with convenience function append_parameter
143+
df = gdx[-1].dataframe.copy()
144+
df.loc[:,'Value'] = 1.0
145+
append_parameter(gdx, 'my_other_paramter', df)
146+
147+
# Write the file to disk
109148
gdx.write(out_file)
110149
150+
The key classes and functions for directly using the backend are:
151+
152+
- :py:class:`gdxpds.gdx.GdxFile`
153+
- :py:class:`gdxpds.gdx.GdxSymbol`
154+
- :py:class:`gdxpds.gdx.GamsDataType`
155+
- :py:func:`gdxpds.gdx.append_set`
156+
- :py:func:`gdxpds.gdx.append_parameter`
157+
111158
Starting with Version 1.1.0, gdxpds does not allow GdxSymbol.dims to
112159
change once they have been firmly established (as evidenced by
113160
GdxSymbol.num_dims > 0 or GdxSymbol.num_records > 0), but does allow

0 commit comments

Comments
 (0)