Skip to content

Commit

Permalink
all cases are working using general xrd_file_parser. this refs #5
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanBilheux committed May 1, 2024
1 parent 46c1039 commit 03aa5c5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
8 changes: 6 additions & 2 deletions notebooks/xrd_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ def file_content(file_name):


def xrd_file_parser(xrd_file_name=None, xrd_file_content=None, xrd_file_type=XrdFileType.ras):
if (not os.path.exists(xrd_file_name)) and (xrd_file_content is None):
if (xrd_file_name is None) and (xrd_file_content is None):
return None

if xrd_file_name:
name, extension = os.path.splitext(xrd_file_name)
if os.path.exists(xrd_file_name):
name, extension = os.path.splitext(xrd_file_name)
else:
raise ValueError("XRD file does not exist!")

else:
extension = xrd_file_type

Expand Down
78 changes: 78 additions & 0 deletions tests/test_xrd_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from notebooks.xrd_file_parser import file_content, _pattern_match, xrd_file_parser
from notebooks.xrd_file_parser import txt_file_parser, ras_file_parser, asc_file_parser
from notebooks.xrd_file_parser import XrdFileType


class TestXrdRasFileParser(TestCase):
Expand Down Expand Up @@ -243,6 +244,27 @@ def test_txt(self):
for _exp, _ret in zip(y_axis_expected, y_axis_returned):
assert _exp == _ret

def test_txt_via_xrd_file_parser(self):

content_of_file = StringIO("".join(file_content(self.txt_file_name)))

with pytest.raises(AttributeError):
txt_file_parser()

metadata_returned = xrd_file_parser(xrd_file_content=content_of_file, xrd_file_type=XrdFileType.txt)

x_axis_returned = metadata_returned['data']['2theta']
y_axis_returned = metadata_returned['data']['intensity']

x_axis_expected = np.array([7.0144, 7.0314, 7.0484, 7.0654, 7.0824])
y_axis_expected = np.array([24868.9923, 24723.026, 24776.6393, 24816.3415, 24855.5135])

for _exp, _ret in zip(x_axis_expected, x_axis_returned):
assert _exp == _ret

for _exp, _ret in zip(y_axis_expected, y_axis_returned):
assert _exp == _ret

def test_asc(self):

content_of_file = file_content(self.asc_file_name)
Expand Down Expand Up @@ -271,6 +293,34 @@ def test_asc(self):
for _exp, _ret in zip(data_expected, data_returned):
assert _exp == _ret

def test_asc_via_xrd_file_parser(self):

content_of_file = file_content(self.asc_file_name)
with pytest.raises(AttributeError):
asc_file_parser()

metadata_returned = xrd_file_parser(xrd_file_content=content_of_file, xrd_file_type=XrdFileType.asc)

metadata_expected = {'alpha1': '1.54059',
'alpha2': '1.54441',
'data_first_line': 28,
}
for key in metadata_expected.keys():
assert metadata_returned[key] == metadata_expected[key]

twotheta_expected = {'start': '20',
'stop': '120',
'step': '0.01',
}
for key in twotheta_expected.keys():
assert metadata_returned['2theta'][key] == twotheta_expected[key]

data_expected = [165, 187, 159, 160, 153, 203, 168, 161, 153, 167, 159, 175]
data_returned = metadata_returned['data']

for _exp, _ret in zip(data_expected, data_returned):
assert _exp == _ret

def test_ras(self):

content_of_file = file_content(self.ras_file_name)
Expand All @@ -294,6 +344,34 @@ def test_ras(self):

data_returned = metadata_returned['data']

for key in data_returned.keys():
for _exp, _return in zip(data_returned[key], data_expected[key]):
assert _exp == _return

def test_ras_via_xrd_file_parser(self):

content_of_file = file_content(self.ras_file_name)
with pytest.raises(AttributeError):
ras_file_parser()

metadata_returned = xrd_file_parser(xrd_file_content=content_of_file,
xrd_file_type=XrdFileType.ras)

metadata_expected = {'alpha1': '1.540593',
'alpha2': '1.544414',
'beta': '1.392250',
'data_first_line': 19,
}

data_expected = {'2theta': [20, 20.01, 20.02, 20.03, 20.04, 20.05, 20.06],
'intensity': [165., 187., 159., 160., 153., 203., 168.],
'error': [1., 1., 1., 1., 1., 1., 1.]}

for key in metadata_expected.keys():
assert metadata_returned[key] == metadata_expected[key]

data_returned = metadata_returned['data']

for key in data_returned.keys():
for _exp, _return in zip(data_returned[key], data_expected[key]):
assert _exp == _return

0 comments on commit 03aa5c5

Please sign in to comment.