@@ -72,7 +72,7 @@ Backend Classes
72
72
~~~~~~~~~~~~~~~
73
73
74
74
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
76
76
duplicate the GDX read functionality shown above one would write:
77
77
78
78
.. code :: python
@@ -87,27 +87,74 @@ duplicate the GDX read functionality shown above one would write:
87
87
df = symbol.dataframe
88
88
print (f " Doing work with { symbol_name} : \n { df} " )
89
89
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 :
92
92
93
93
.. code :: python
94
94
95
95
import gdxpds
96
96
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
+
97
118
out_file = ' my_new_gdx_data.gdx'
98
- with gdxpds.gdx.GdxFile() as gdx:
119
+ with GdxFile() as gdx:
120
+
99
121
# 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 )])
102
124
data[' Value' ] = True
103
125
gdx[- 1 ].dataframe = data
126
+
104
127
# 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))
108
131
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
109
148
gdx.write(out_file)
110
149
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
+
111
158
Starting with Version 1.1.0, gdxpds does not allow GdxSymbol.dims to
112
159
change once they have been firmly established (as evidenced by
113
160
GdxSymbol.num_dims > 0 or GdxSymbol.num_records > 0), but does allow
0 commit comments