Skip to content

Commit

Permalink
Merge branch 'main' into eh/text_field_gdxdump
Browse files Browse the repository at this point in the history
  • Loading branch information
elainethale committed Jul 21, 2023
2 parents 43b9a28 + 8de37b7 commit fe83083
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gdxpds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def load_gdxcc(gams_dir=None):
"before importing pandas to avoid a library conflict.")


from gdxpds.read_gdx import to_dataframes, list_symbols, to_dataframe
from gdxpds.read_gdx import to_dataframes, list_symbols, to_dataframe, get_data_types
from gdxpds.write_gdx import to_gdx
33 changes: 28 additions & 5 deletions gdxpds/read_gdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# gdxpds needs to be imported before pandas to try to avoid library conflict on
# Linux that causes a segmentation fault.
from gdxpds.tools import Error
from gdxpds.gdx import GdxFile
from gdxpds.gdx import GdxFile, GamsDataType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,15 +47,19 @@ def gdx(self):
def dataframes(self):
if self.__dataframes is None:
self.__dataframes = OrderedDict()
for symbol in self.__gdx:
for symbol in self.gdx:
if not symbol.loaded:
symbol.load()
self.__dataframes[symbol.name] = symbol.dataframe.copy()
return self.__dataframes

@property
def symbols(self):
return [symbol_name for symbol_name in self.gdx]
return [symbol.name for symbol in self.gdx]

@property
def data_types(self):
return {symbol.name: symbol.data_type for symbol in self.gdx}

def dataframe(self, symbol_name, load_set_text=False):
if not symbol_name in self.gdx:
Expand Down Expand Up @@ -116,8 +120,27 @@ def list_symbols(gdx_file,gams_dir=None):
list of str
List of symbol names
"""
symbols = Translator(gdx_file,gams_dir=gams_dir,lazy_load=True).symbols
return symbols
return Translator(gdx_file,gams_dir=gams_dir,lazy_load=True).symbols


def get_data_types(gdx_file,gams_dir=None):
"""
Returns a dict of the symbols' :py:class:`GamsDataTypes <GamsDataType>`.
Parameters
----------
gdx_file : pathlib.Path or str
Path to the GDX file to read
gams_dir : None or pathlib.Path or str
optional path to GAMS directory
Returns
-------
dict of str to :py:class:GamsDataType`
Map of symbol names to the corresponding :py:class:GamsDataType`
"""
return Translator(gdx_file,gams_dir=gams_dir,lazy_load=True).data_types



def to_dataframe(gdx_file,symbol_name,gams_dir=None,old_interface=True,load_set_text=False):
Expand Down
20 changes: 18 additions & 2 deletions gdxpds/test/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import gdxpds.gdx
from gdxpds import to_dataframes
from gdxpds import to_dataframes, list_symbols, get_data_types
from gdxpds.test import base_dir

logger = logging.getLogger(__name__)
Expand All @@ -26,7 +26,23 @@ def test_read_path():
filename = 'all_generator_properties_input.gdx'
from pathlib import Path
gdx_file = Path(base_dir) / filename
to_dataframes(gdx_file)

symbol_names = list_symbols(gdx_file)
n = len(symbol_names)
assert isinstance(symbol_names[0], str)
assert n == 7

dfs = to_dataframes(gdx_file)
assert len(dfs) == n

# data frames are loaded in order
for i, symbol_name in enumerate(dfs):
assert symbol_names[i] == symbol_name

dtypes = get_data_types(gdx_file)
# this file is all parameters
for val in dtypes.values():
val == gdxpds.gdx.GamsDataType.Parameter

def test_unload():
filename = 'all_generator_properties_input.gdx'
Expand Down

0 comments on commit fe83083

Please sign in to comment.