Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.2.0] - 2022-07-02
### Added
* Added the possibility to read GAMS variables and convert them into pandas DataFrames

## [2.1.1] - 2020-08-20
### Added
* Add index names from symbol domain if defined when converting to a pandas object.
Expand Down
2 changes: 1 addition & 1 deletion gdx2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from .version import __version__
from .gdxfile import GdxFile
from .gams import GAMSSet, GAMSScalar, GAMSParameter
from .gams import GAMSSet, GAMSScalar, GAMSParameter, GAMSVariable
76 changes: 76 additions & 0 deletions gdx2py/gams.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,79 @@ def to_pandas(self):

return df


class GAMSVariable(_GAMSNDimSymbol):
"""Class for GAMS Variables

Attributes:
domain (list): List of set names that make the domain of the symbol
expl_text (str): Symbol explanatory text

Methods:
keys: Return a view to the symbol's keys
values: Return a view to the symbol's values
"""

def __init__(
self,
data: Mapping[tuple, float],
domain: Sequence[str] = None,
expl_text: str = '',
):
"""Constructor for GAMSParameter

Args:
data: Dictionay of keys and values
domain (optional): List of domain set names, use `None` for the universal set
expl_text (optional): Explanatory text

Raises:
ValueError
"""

# Check arguments
try:
super().__init__(data.keys(), domain, expl_text)
except AttributeError:
raise ValueError("Data must be a mapping")
self._type = 'Parameter'
self._data = data

def keys(self):
return self._data.keys()

def values(self):
try:
return self._data.values()
except TypeError: # Support self.data being a pandas.Series
return self._data.values

def __getitem__(self, key):
return self._data[key]

def __iter__(self):
self._iterator = iter(self._data.items())
return self

def __next__(self):
key, val = next(self._iterator)
return key, float(val)

def to_pandas(self):
try:
import pandas as pd
except ImportError:
raise ImportError(
"This feature needs pandas package. "
"Please use pip or conda to install pandas"
)
df = pd.DataFrame(iter(self.values()), self.keys())
df.name = self.expl_text

if self.domain:
if isinstance(df.index, pd.MultiIndex):
df.index.names = self.domain
else:
df.index.name = self.domain[0]

return df
40 changes: 34 additions & 6 deletions gdx2py/gdxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@

__author__ = "Erkka Rinne"


import sys
import os.path
import math
from copy import copy
from warnings import warn
from collections.abc import Mapping, Sequence, KeysView, ValuesView

from gdxcc import GMS_VAL_LEVEL, GMS_DT_SET, GMS_DT_PAR
from gdxcc import GMS_VAL_LEVEL, GMS_VAL_MARGINAL, GMS_VAL_LOWER, GMS_VAL_UPPER, \
GMS_DT_SET, GMS_DT_PAR, GMS_DT_VAR
import gdxcc

from .gams import _GAMSSymbol, GAMSSet, GAMSScalar, GAMSParameter
from .gams import _GAMSSymbol, GAMSSet, GAMSScalar, GAMSParameter, GAMSVariable

# String representations of API constants
GMS_DTYPES = {
'Set': GMS_DT_SET,
'Parameter': GMS_DT_PAR,
'Scalar': GMS_DT_PAR,
# Not used at the moments
#'Variable': GMS_DT_VAR,
#'Equation': GMS_DT_EQU,
#'Alias': GMS_DT_ALIAS:
'Variable': GMS_DT_VAR,
# 'Equation': GMS_DT_EQU,
# 'Alias': GMS_DT_ALIAS:
}

# Define data types
Expand All @@ -47,6 +47,13 @@
gdxcc.GMS_SV_NAINT: math.nan,
}

VARIABLE_DIMENSIONS = {
GMS_VAL_LEVEL: "level",
GMS_VAL_MARGINAL: "marginal",
GMS_VAL_LOWER: "lower bound",
GMS_VAL_UPPER: "upper bound"
}

GMS_USERINFO_SET_PARAMETER = 0 # UserInfo value for sets and parameters


Expand Down Expand Up @@ -346,6 +353,19 @@ def _read_symbol(self, symno: int):
) # Squeze out dimension of 1-dim keys
val = value_arr[GMS_VAL_LEVEL]
values[i] = SPECIAL_VALUES.get(val, val)
elif symtype == GMS_DT_VAR:
for i in range(recs):
_ret, key_arr, value_arr, _afdim = gdxcc.gdxDataReadStr(self._h)
keys[i] = (
key_arr[0] if dim == 1 else tuple(key_arr)
) # Squeze out dimension of 1-dim keys

value = {}
for elem in VARIABLE_DIMENSIONS:
val = value_arr[elem]
value[VARIABLE_DIMENSIONS[elem]] = SPECIAL_VALUES.get(val, val)

values[i] = value

# Done reading
gdxcc.gdxDataReadDone(self._h)
Expand All @@ -360,6 +380,10 @@ def _read_symbol(self, symno: int):
return GAMSParameter(
dict(zip(keys, values)), domain=domain, expl_text=expl_text
)
elif symtype == GMS_DT_VAR:
return GAMSVariable(
dict(zip(keys, values)), domain=domain, expl_text=expl_text
)

def _write_symbol(self, symname: str, symbol: _GAMSSymbol):
"""Write a Pandas series to a GAMS Set symbol
Expand All @@ -369,8 +393,12 @@ def _write_symbol(self, symname: str, symbol: _GAMSSymbol):

Raises:
RuntimeError
NotImplementedError
"""

if isinstance(symbol, GAMSVariable):
raise NotImplementedError

# Get number of dimensions
dim = symbol.dimension

Expand Down
2 changes: 1 addition & 1 deletion gdx2py/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.1.1'
__version__ = '2.2.0'