Skip to content

Commit

Permalink
first test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozef Tran committed May 28, 2023
1 parent dd10566 commit 461e256
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"-s",
"./tests",
"-p",
"*_test.py"
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
Expand Down
Binary file added jws2txt/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added jws2txt/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file modified jws2txt/helpers/__pycache__/helpers.cpython-310.pyc
Binary file not shown.
Binary file modified jws2txt/helpers/__pycache__/helpers.cpython-38.pyc
Binary file not shown.
30 changes: 15 additions & 15 deletions jws2txt/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,25 @@ def __init__(self, path: str) -> None:
fmt = 'f'*self.npoints

try:
self.unpack_y_data(y_data, format=fmt, num_chanels=self.numchanels)
self.__unpack_y_data(y_data, format=fmt, num_chanels=self.numchanels)

except:
except Exception:
try:
# works in the case of *.jwb file
self.numchanels = data_tuple[1]
self.unpack_y_data(y_data, format=fmt, num_chanels=self.numchanels)
except:
print("Incorrect number of channels.")
self.__unpack_y_data(y_data, format=fmt, num_chanels=self.numchanels)
except Exception as e:
print(f"Incorrect number of channels. {e}")

sample_info = file.openstream('SampleInfo').read()[8:].split(b'\x00\x00\x08\x00')

try:
self.decode_sample_info(sample_info)
except:
self.__decode_sample_info(sample_info)
except Exception:
self.sample_name = ''
self.comment = ''

def unpack_y_data(self, y_data: bytes, format: str, num_chanels: int) -> None:
def __unpack_y_data(self, y_data: bytes, format: str, num_chanels: int) -> None:
"""Unpacks the Y-Data from the JWS file.
"""
chunk_size = int(len(y_data)/num_chanels)
Expand All @@ -107,22 +107,22 @@ def unpack_y_data(self, y_data: bytes, format: str, num_chanels: int) -> None:
unpacked_data.insert(0, tuple(x_data))
self.unpacked_data = unpacked_data

def decode_sample_info(self, sample_info_bytes: list) -> None:
def __decode_sample_info(self, sample_info_bytes: list) -> None:
"""Decodes the SampleInfo"""
if len(sample_info_bytes) == 2:
sample_name = sample_info_bytes[0].split(b'\x00\x00')[0]
self.sample_name = (self.unpack_sample_info(sample_name))
self.sample_name = (self.__unpack_sample_info(sample_name))

comment = sample_info_bytes[1].split(b'\x00\x00')[1]
self.comment = self.unpack_sample_info(comment)
self.comment = self.__unpack_sample_info(comment)

elif len(sample_info_bytes) == 1:
sample_name = sample_info_bytes[0].split(b'\x00\x00')[0]
self.sample_name = self.unpack_sample_info(sample_name)
self.sample_name = self.__unpack_sample_info(sample_name)

self.comment = ''

def unpack_sample_info(self, packed_bytes: bytes) -> str:
def __unpack_sample_info(self, packed_bytes: bytes) -> str:
"""Unpacks SampleInfo bytes"""
if packed_bytes[-1:] not in {b'\x00', b''}:
packed_bytes += b'\x00'
Expand All @@ -140,11 +140,11 @@ def write_data(self, out_file: str,
with open(out_file, 'x', newline='') as f:
writer = csv.writer(f, delimiter=delimiter)

if write_comments == True:
if write_comments is True:
writer.writerow([self.sample_name])
writer.writerow([self.comment])

if write_header == True:
if write_header is True:
if len(self.header_names) == len(self.unpacked_data):
writer.writerow(self.header_names)
elif len(self.header_names) < len(self.unpacked_data):
Expand Down
17 changes: 11 additions & 6 deletions jws2txt/jws2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ def str2bool(v) -> bool:

def main():

parser = argparse.ArgumentParser(description='Converts *.jws or *jwb files to human-readable text files.')
parser = argparse.ArgumentParser(description='Converts *.jws or *jwb files to human\
-readable text files.')
parser.add_argument('--in-path', type=str, required=True,
help="Path to file or folder containing files for conversion.")
parser.add_argument('--out-dir', type=str, help='Output folder')
parser.add_argument('--delimiter', type=str, default='\t', help='Delimiter used in saved text files.')
parser.add_argument('--comments', type=str2bool, choices=[True, False], default=True, help='Write sample information.')
parser.add_argument('--header', type=str2bool, choices=[True, False], default=True, help='Write header.')
parser.add_argument('--delimiter', type=str, default='\t', help='Delimiter used in\
saved text files.')
parser.add_argument('--comments', type=str2bool, choices=[True, False], default=True, # noqa: E501
help='Write sample information.')
parser.add_argument('--header', type=str2bool, choices=[True, False], default=True,
help='Write header.')

args = parser.parse_args()

if not os.path.exists(args.in_path):
raise FileNotFoundError(f"{args.in_path} does not exist.")

if args.out_dir == None:
if args.out_dir is None:
args.out_dir = args.in_path

if not os.path.exists(args.out_dir):
Expand All @@ -40,7 +44,8 @@ def main():

for file in gen:

out_file_path = os.path.join(args.out_dir, ''.join((os.path.splitext(file)[0], '.txt')))
out_file_path = os.path.join(args.out_dir, ''.join((os.path.splitext(file)[0], # noqa: E501
'.txt')))
jws_file_path = os.path.join(root, file)

JWSFile(path=jws_file_path).write_data(out_file=out_file_path,
Expand Down
11 changes: 9 additions & 2 deletions temp/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import jws2txt
import matplotlib.pyplot as plt

test_file = jws2txt.JWSFile(r'sample_CD_HT_Abs.jws')
test_file = jws2txt.JWSFile(r'001Hg.jws')


x, cd, ht, abs = test_file.unpacked_data


#%%
# %%

# %%
Expand All @@ -19,3 +19,10 @@
plt.show()
# %%


test_file.decode_sample_info()
# %%
test_file.unpacked_data[0][2]
test_file.unpacked_data[1][2]
test_file.unpacked_data[2][2]
test_file.unpacked_data[3][2]
Binary file added tests/__pycache__/test_helpers.cpython-310.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_helpers.cpython-38.pyc
Binary file not shown.
Binary file added tests/files/001Hg.jws
Binary file not shown.
File renamed without changes.
48 changes: 47 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
from unittest import TestCase
import unittest
import jws2txt

class JWSFileTest(unittest.TestCase):

def setUp(self) -> None:
self.three_channels = jws2txt.JWSFile(r'tests/files/001Hg.jws')


def test_three_channels_creation(self):
# Test file creation and initialization

self.assertEqual(self.three_channels.numchanels, 3)
self.assertEqual(self.three_channels.npoints, 1201)
self.assertEqual(self.three_channels.x_for_first_point, 320.0)
self.assertEqual(self.three_channels.x_for_last_point, 200.0)
self.assertEqual(self.three_channels.x_increment, -0.1)
self.assertEqual(self.three_channels.header_codes, (268435715, 4097, 8193, 3))
self.assertEqual(self.three_channels.header_names, ['WAVELENGTH', 'CD', 'HT VOLTAGE', 'ABSORBANCE'])
self.assertEqual(self.three_channels.data_list, [3,
1,
0,
3,
1,
1201,
320.0,
200.0,
-0.1,
268435715,
4097,
8193,
3,
8.6939493804496e-311,
200.0,
1.0,
320.0])
self.assertEqual(self.three_channels.sample_name, 'HK14_CN')
self.assertEqual(self.three_channels.comment, '1mm')
#test x-axis and 3 channels
self.assertEqual(self.three_channels.unpacked_data[0][2], 319.8)
self.assertEqual(self.three_channels.unpacked_data[1][2], 0.1975005567073822)
self.assertEqual(self.three_channels.unpacked_data[2][2], 206.2129669189453)
self.assertEqual(self.three_channels.unpacked_data[3][2], 0.11851496249437332)


if __name__=='__main__':
unittest.main()

0 comments on commit 461e256

Please sign in to comment.