Skip to content

Commit

Permalink
Add support for CSV TAB externals
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Jul 1, 2024
1 parent 3ec6762 commit eb6ead7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 56 deletions.
72 changes: 44 additions & 28 deletions pysd/py_backend/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@ def read(cls, file_name, sheet_name):
if file_name.joinpath(sheet_name) in cls._Excels:
return cls._Excels[file_name.joinpath(sheet_name)]
else:
# get the function to read the data based on its extension
read_kwargs = {}
ext = file_name.suffix.lower()
if ext in ['.xls', '.xlsx', '.xlsm', 'xlsb', 'odf', 'ods', 'odt']:
read_func = pd.read_excel
read_kwargs['sheet_name'] = sheet_name
elif ext == '.csv':
read_func = pd.read_csv
else:
read_func = pd.read_table
# read the data
excel = np.array([
pd.to_numeric(ex, errors='coerce')
for ex in
pd.read_excel(file_name, sheet_name, header=None).values
read_func(file_name, header=None, **read_kwargs).values
])
# save data for future retrievals
cls._Excels[file_name.joinpath(sheet_name)] = excel
return excel

Expand Down Expand Up @@ -109,34 +121,27 @@ def _get_data_from_file(self, rows, cols):
depending on the shape of the requested data
"""
ext = self.file.suffix.lower()
if ext in ['.xls', '.xlsx', '.xlsm']:
# read data
data = Excels.read(
self.file,
self.sheet)[rows[0]:rows[1], cols[0]:cols[1]].copy()
# read data
data = Excels.read(
self.file,
self.sheet)[rows[0]:rows[1], cols[0]:cols[1]].copy()

shape = data.shape
shape = data.shape

# empty cells
if shape[0] == 0 or shape[1] == 0:
raise ValueError(
self.py_name + "\n"
"The cells are empty.\n"
+ self._file_sheet
)

# if it is a single row remove its dimension
if shape[1] == 1:
data = data[:, 0]
if shape[0] == 1:
data = data[0]
return data
# empty cells
if shape[0] == 0 or shape[1] == 0:
raise ValueError(
self.py_name + "\n"
"The cells are empty.\n"
+ self._file_sheet
)

raise NotImplementedError(
self.py_name + "\n"
f"The files with extension {ext} are not implemented"
)
# if it is a single row remove its dimension
if shape[1] == 1:
data = data[:, 0]
if shape[0] == 1:
data = data[0]
return data

def _get_data_from_file_opyxl(self, cellname):
"""
Expand Down Expand Up @@ -1054,8 +1059,19 @@ def get_subscripts_cell(self, row_first, col_first, lastcell):
if col_last is not None:
read_kwargs['usecols'] = np.arange(col_first, col_last+1)

data = pd.read_excel(
self.file, self.sheet,
# get the function to read the data based on its extension
ext = self.file.suffix.lower()
if ext in ['.xls', '.xlsx', '.xlsm', 'xlsb', 'odf', 'ods', 'odt']:
read_func = pd.read_excel
read_kwargs['sheet_name'] = self.sheet
elif ext == '.csv':
read_func = pd.read_csv
else:
read_func = pd.read_table

# read the data
data = read_func(
self.file,
skiprows=row_first-1,
dtype=object,
**read_kwargs
Expand Down
Binary file removed tests/data/not_implemented_file.ods
Binary file not shown.
28 changes: 0 additions & 28 deletions tests/pytest_types/external/pytest_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,34 +1968,6 @@ class TestWarningsErrors():
Test for the warnings and errors of External and its subclasses
"""

def test_not_implemented_file(self, _root):
"""
Test for not implemented file
"""
import pysd

file_name = "data/not_implemented_file.ods"
sheet = "Horizontal"
time_row_or_col = "4"
cell = "C5"
coords = {}
interp = None
py_name = "test_not_implemented_file"

data = pysd.external.ExtData(file_name=file_name,
sheet=sheet,
time_row_or_col=time_row_or_col,
root=_root,
cell=cell,
coords=coords,
interp=interp,
final_coords=coords,
py_name=py_name)

error_message = r"The files with extension .ods are not implemented"
with pytest.raises(NotImplementedError, match=error_message):
data.initialize()

def test_non_existent_file(self, _root):
"""
Test for non-existent file
Expand Down

0 comments on commit eb6ead7

Please sign in to comment.