Skip to content

Commit

Permalink
first case with txt works. this refs #5
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanBilheux committed May 1, 2024
1 parent b0ecae4 commit a6b97cc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
File renamed without changes.
File renamed without changes.
43 changes: 30 additions & 13 deletions notebooks/xrd_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,34 @@
}


class XrdFileType:
ras = '.ras'
asc = '.asc'
txt = '.txt'


def file_content(file_name):
with open(file_name, 'r', errors='replace') as f:
content = f.readlines()

return content


def xrd_file_parser(xrd_file_name):
if not os.path.exists(xrd_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):
return None

name, extension = os.path.splitext(xrd_file_name)
if xrd_file_name:
name, extension = os.path.splitext(xrd_file_name)
else:
extension = xrd_file_type

if extension == '.ras':
return ras_file_parser(xrd_file_name)
elif extension == '.asc':
return asc_file_parser(xrd_file_name)
elif extension == '.txt':
return txt_file_parser(xrd_file_name)
if extension == XrdFileType.ras:
return ras_file_parser(xrd_file_name, xrd_file_content)
elif extension == XrdFileType.asc:
return asc_file_parser(xrd_file_name, xrd_file_content)
elif extension == XrdFileType.txt:
return txt_file_parser(xrd_file_name, xrd_file_content)

return None

Expand All @@ -61,7 +70,7 @@ def _pattern_match(pattern=None, line_starts_with=None, line=None):
return None


def asc_file_parser(xrd_file_name):
def asc_file_parser(xrd_file_name=None, xrd_file_content=None):
"""retrieve the following metadata from the ASC file"""
metadata = {'alpha1': None,
'alpha2': None,
Expand Down Expand Up @@ -117,7 +126,7 @@ def asc_file_parser(xrd_file_name):
return metadata


def ras_file_parser(xrd_file_name):
def ras_file_parser(xrd_file_name=None, xrd_file_content=None):
"""retrieve the following metadata from the RAS file"""
metadata = {'alpha1': None,
'alpha2': None,
Expand Down Expand Up @@ -158,8 +167,16 @@ def ras_file_parser(xrd_file_name):
return metadata


def txt_file_parser(xrd_file_name):
data = pd.read_csv(xrd_file_name, names=['2theta', 'intensity'], skiprows=1, sep='\t')
def txt_file_parser(xrd_file_name=None, xrd_file_content=None):
if xrd_file_name is None:
if xrd_file_content is None:
raise AttributeError("Provide either xrd_file_name or xrd_file_content")

data = pd.read_csv(xrd_file_content, names=['2theta', 'intensity'], skiprows=1, sep='\t')

else:
data = pd.read_csv(xrd_file_name, names=['2theta', 'intensity'], skiprows=1, sep='\t')

return {'data': {'2theta': np.array(data['2theta']),
'intensity': np.array(data['intensity']),
},
Expand Down
36 changes: 36 additions & 0 deletions tests/test_xrd_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import TestCase
import os
import numpy as np
from io import StringIO

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
Expand Down Expand Up @@ -207,3 +208,38 @@ def test_txt_file_parser(self):

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


class TestContentFileParser(TestCase):

ASC_FILE_NAME = "data/xrd_file.asc"
RAS_FILE_NAME = "data/xrd_file.ras"
TXT_FILE_NAME = "data/xrd_file.txt"

def setUp(self):
_file_path = os.path.dirname(__file__)
self.asc_file_name = os.path.abspath(os.path.join(_file_path, self.ASC_FILE_NAME))
self.ras_file_name = os.path.abspath(os.path.join(_file_path, self.RAS_FILE_NAME))
self.txt_file_name = os.path.abspath(os.path.join(_file_path, self.TXT_FILE_NAME))

def test_txt(self):

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

with pytest.raises(AttributeError):
txt_file_parser()

metadata_returned = txt_file_parser(xrd_file_content=content_of_file)

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

0 comments on commit a6b97cc

Please sign in to comment.