Skip to content

Commit df5c066

Browse files
authored
Merge pull request #11 from jd-foster/fix-review-issues
Use Tables.jl for GDX record storage
2 parents 0a85d75 + d2e839b commit df5c066

6 files changed

Lines changed: 312 additions & 266 deletions

File tree

Project.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
name = "GDXInterface"
22
uuid = "b8352055-3412-4df1-864e-ee7edae71aec"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
authors = ["Martin Kirk Bonde", "James Daniel Foster"]
55

66
[deps]
7-
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
7+
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
8+
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
89
gdx_jll = "aa021fa7-5c7d-5dc1-9fa7-c90cd282ac20"
910

1011
[compat]
11-
DataFrames = "1"
12+
DataAPI = "1"
13+
Tables = "1"
1214
gdx_jll = "7.11.20"
1315
julia = "1.6"
1416

1517
[extras]
18+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1619
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1720

1821
[targets]
19-
test = ["Test"]
22+
test = ["Test", "DataFrames"]

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ list_parameters(gdx)
4848
list_variables(gdx)
4949
list_equations(gdx)
5050

51-
# Access data as DataFrames
51+
# Access data as Tables.jl-compatible column tables
5252
demand = gdx[:demand] # bracket syntax
5353
demand = gdx.demand # property syntax (with tab completion)
5454

@@ -57,17 +57,21 @@ sym = get_symbol(gdx, :demand)
5757
sym.name # "demand"
5858
sym.description # explanatory text from GAMS
5959
sym.domain # ["j"]
60-
sym.records # the DataFrame
60+
sym.records # the records table
61+
62+
# Pass DataFrame as a sink to materialize DataFrames while reading
63+
using DataFrames
64+
gdx = read_gdx("transport.gdx", DataFrame)
6165
```
6266

6367
### Writing GDX files
6468

6569
```julia
66-
using GDXInterface, DataFrames
70+
using GDXInterface
6771

68-
# Write DataFrames as parameters
69-
supply = DataFrame(i = ["seattle", "san-diego"], value = [350.0, 600.0])
70-
demand = DataFrame(j = ["new-york", "chicago", "topeka"], value = [325.0, 300.0, 275.0])
72+
# Write Tables.jl-compatible tables as parameters
73+
supply = (; i = ["seattle", "san-diego"], value = [350.0, 600.0])
74+
demand = (; j = ["new-york", "chicago", "topeka"], value = [325.0, 300.0, 275.0])
7175
write_gdx("output.gdx", "supply" => supply, "demand" => demand)
7276

7377
# Round-trip: read a GDX file and write it back (preserves all symbol types)
@@ -98,17 +102,18 @@ gdx = read_gdx("big_model.gdx", only=[:x, :demand])
98102
### Reading
99103

100104
```julia
101-
read_gdx(filepath; parse_integers=true, only=nothing) -> GDXFile
105+
read_gdx(filepath[, sink]; parse_integers=true, only=nothing) -> GDXFile
102106
```
103107

108+
- `sink`: callable that materializes a column table, defaulting to `Tables.columntable`
104109
- `parse_integers`: convert set elements like `"2020"` to `Int`
105110
- `only`: vector of symbol names to load (e.g. `[:x, :demand]`)
106111

107112
### Writing
108113

109114
```julia
110-
# Write DataFrames as parameters (convenience)
111-
write_gdx(filepath, "name" => DataFrame, ...)
115+
# Write tables as parameters (convenience)
116+
write_gdx(filepath, "name" => table, ...)
112117

113118
# Write a full GDXFile (sets, parameters, variables, equations)
114119
write_gdx(filepath, gdxfile::GDXFile)
@@ -117,8 +122,8 @@ write_gdx(filepath, gdxfile::GDXFile)
117122
### Querying a GDXFile
118123

119124
```julia
120-
gdx[:name] # records DataFrame (bracket access)
121-
gdx.name # records DataFrame (property access)
125+
gdx[:name] # records table (bracket access)
126+
gdx.name # records table (property access)
122127
get_symbol(gdx, :name) # full GDXSymbol object
123128

124129
list_sets(gdx) # list set names

0 commit comments

Comments
 (0)