Skip to content

Commit

Permalink
tests: Fixed csv/xlsx examples in parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Somerandomguy10111 committed Dec 17, 2024
1 parent 901c2dd commit a772ba5
Show file tree
Hide file tree
Showing 6 changed files with 932 additions and 952 deletions.
918 changes: 459 additions & 459 deletions tests/t_parsing/t_csv/vertical.csv

Large diffs are not rendered by default.

22 changes: 4 additions & 18 deletions tests/t_parsing/t_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,10 @@ def test_dat(self):

class TestParserDatabase(ParserBaseTest):
def test_db_parsing_ok(self):
with self.assertNoLogs(level=0):
bruker_db = self.get_bruker_db()
all_db = self.get_all_db()

for db in [bruker_db, all_db]:
self.assertIsInstance(db, PatternDB)
all_db = PatternDB.load(dirpath=DataExamples.get_example_dirpath(), csv_orientation=CsvOrientations.VERTICAL, strict=True)
all_db.save(dirpath=tempfile.mktemp())

@staticmethod
def get_bruker_db() -> PatternDB:
return PatternDB.load(dirpath=DataExamples.get_datafolder_fpath())

@staticmethod
def get_all_db() -> PatternDB:
return PatternDB.load(dirpath=DataExamples.get_example_dirpath(), csv_orientation=CsvOrientations.VERTICAL)


self.assertIsInstance(all_db, PatternDB)

if __name__ == "__main__":
# TestParserDatabase.execute_all()
TestCustomFormats.execute_all()
TestParserDatabase.execute_all()
# TestCustomFormats.execute_all()
15 changes: 8 additions & 7 deletions xrdpattern/parsing/csv/csv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ def _as_matrix(self, fpath: str, pattern_orientation : str) -> Matrix:
row = [item.strip() for item in line.strip().split(seperator)]
if row and any(item for item in row):
table.append(row)
if pattern_orientation == CsvOrientations.VERTICAL:
table = [list(col) for col in zip(*table)]

if self.is_numerical(values=table[0]):
headers, numerical_part = None, table
numerical_part = table
else:
headers, numerical_part = table[0], table[1:]
data = self.to_numerical(numerical_part, row_start=0 if headers else 1)
matrix = Matrix(headers=headers, numerical_data=data)
numerical_part = table[1:]

if pattern_orientation == CsvOrientations.VERTICAL:
numerical_part = [list(col) for col in zip(*numerical_part)]
data = self.to_numerical(numerical_part)
matrix = Matrix( numerical_data=data)

delta = len(numerical_part) - len(matrix.numerical_data)
if delta > 0:
Expand Down Expand Up @@ -94,7 +95,7 @@ def has_two_columns(cls, fpath : str) -> bool:
@staticmethod
def xlsx_to_csv(xlsx_fpath : str, csv_fpath : str):
data = pd.read_excel(xlsx_fpath)
data.to_csv(csv_fpath, index=False)
data.to_csv(csv_fpath, index=False, sep=';')

# ------------------------------------------
# datatype tools
Expand Down
7 changes: 0 additions & 7 deletions xrdpattern/parsing/csv/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ class CsvOrientations:

@dataclass
class Matrix:
headers : list[str]
numerical_data : list[list[float]]

def __post_init__(self):
if self.headers:
if not len(self.headers) == len(self.numerical_data[0]):
raise ValueError(f"Number of headers ({len(self.headers)}) does not match number"
f" of data columns ({len(self.numerical_data)})")

def get_x_values(self) -> list[float]:
return self.numerical_data[0]

Expand Down
2 changes: 1 addition & 1 deletion xrdpattern/parsing/examples/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_dat_fpath(cls) -> str:

@classmethod
def get_datafolder_fpath(cls) -> str:
return os.path.join(cls.get_example_dirpath(), 'datafolder')
return os.path.join(cls.get_example_dirpath())

@staticmethod
def get_example_dirpath() -> str:
Expand Down
920 changes: 460 additions & 460 deletions xrdpattern/parsing/examples/vertical.csv

Large diffs are not rendered by default.

0 comments on commit a772ba5

Please sign in to comment.