diff --git a/README.md b/README.md index 096d683..044c22d 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,18 @@ # jws2txt -JASCO file to text file converter -jws2txt converts JASCO spectrophotometer *.jws files to *.txt files. +A program to convert binary JASCO SpectraManager (JWS and JWB) files to text files. - -Introduction +## Introduction ------------ -jws2txt is a simple command line tool allowing conversion -JWS and JWB files from Jasco SpectraManager software. jws2txt converts JWS and JWB files to -text files that can be used in any data analysis workflows and software. +jws2txt is a simple command line tool that allows for the conversion of JWS and JWB files from JASCO SpectraManager software to text files. These text files can be used in any data analysis workflows and software. -Contrary to Jasco SpectraManager software jws2txt allows for batch conversion -of JWS/JWB files. +Unlike JASCO SpectraManager software, jws2txt enables batch conversion of JWS/JWB files. -Data unpack is based on Víctor M. Hernández-Rocamora's jwsProcessor -(https://github.com/vhernandez/jwsProcessor). +Data unpacking is based on Víctor M. Hernández-Rocamora's jwsProcessor (https://github.com/vhernandez/jwsProcessor). -Installation and usage +## Installation and Usage ------------ -1. Download python, preferentially in Anaconda distribution. -2. Download this repository, unzip it. -3. Run the 'pip install -r requirements.txt' command in the command line. -4. To convert the files run the 'python jws2txt.py path_to_folder' command, where path_to_folder -is a folder containing JWS or JWB files. -5. Input the number of channels the files have. -6. Converted files will be in the folder containing source JWS or JWB files +1. Download Python. +2. Run the 'pip install jws2txt' command in the command line. +3. To convert the files, run the 'jws2txt --in-path path_to_folder --out-dir output_folder_path' command, where path_to_folder is the folder containing the JWS or JWB files. +4. The converted files will be located in the folder containing the source JWS or JWB files. \ No newline at end of file diff --git a/jws2txt/__pycache__/jws2txt.cpython-38.pyc b/jws2txt/__pycache__/jws2txt.cpython-38.pyc new file mode 100644 index 0000000..0a0bdf8 Binary files /dev/null and b/jws2txt/__pycache__/jws2txt.cpython-38.pyc differ diff --git a/jws2txt/helpers/__pycache__/helpers.cpython-38.pyc b/jws2txt/helpers/__pycache__/helpers.cpython-38.pyc index 5f4337d..a8ad555 100644 Binary files a/jws2txt/helpers/__pycache__/helpers.cpython-38.pyc and b/jws2txt/helpers/__pycache__/helpers.cpython-38.pyc differ diff --git a/jws2txt/helpers/helpers.py b/jws2txt/helpers/helpers.py index 28a0c59..64c7bc8 100644 --- a/jws2txt/helpers/helpers.py +++ b/jws2txt/helpers/helpers.py @@ -1,7 +1,7 @@ from struct import unpack import csv import olefile as ofio - +from typing import Iterator DATAINFO_FMT = ' Iterator[float]: """Return evenly spaced number over specified range. Args: @@ -96,6 +96,8 @@ def __init__(self, path: str) -> None: self.sample_name = '' self.comment = '' + file.close() + def __unpack_y_data(self, y_data: bytes, format: str, num_chanels: int) -> None: """Unpacks the Y-Data from the JWS file. """ diff --git a/jws2txt/jws2txt.py b/jws2txt/jws2txt.py index 58f94b5..b46f80b 100644 --- a/jws2txt/jws2txt.py +++ b/jws2txt/jws2txt.py @@ -3,7 +3,7 @@ from .helpers.helpers import JWSFile -def str2bool(v) -> bool: +def str2bool(v: str) -> bool: """Converts string to bool.""" if isinstance(v, bool): return v diff --git a/temp/test.py b/temp/test.py index 6e09619..18510ae 100644 --- a/temp/test.py +++ b/temp/test.py @@ -1,12 +1,21 @@ #%% import jws2txt import matplotlib.pyplot as plt - +from io import StringIO test_file = jws2txt.JWSFile(r'001Hg.jws') + + x, cd, ht, abs = test_file.unpacked_data +outfile = StringIO() +test_file.write_data(outfile) +outfile.seek(0) +content = outfile.read() +print(content) + + #%% # %% @@ -22,7 +31,15 @@ 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] \ No newline at end of file +test_file = jws2txt.JWSFile(r'../tests/files/sample_fluorescence.jws') +test_file = jws2txt.JWSFile(r'../tests/files/2022_05_14-1.jwb') + + +# %% + + +from jws2txt.helpers.helpers import frange + + +tuple(frange(2, 0.9, -0.1)) +# %% diff --git a/tests/__pycache__/test_helpers.cpython-38.pyc b/tests/__pycache__/test_helpers.cpython-38.pyc index 9770138..55cbb53 100644 Binary files a/tests/__pycache__/test_helpers.cpython-38.pyc and b/tests/__pycache__/test_helpers.cpython-38.pyc differ diff --git a/tests/__pycache__/test_main.cpython-38.pyc b/tests/__pycache__/test_main.cpython-38.pyc new file mode 100644 index 0000000..7bd44c0 Binary files /dev/null and b/tests/__pycache__/test_main.cpython-38.pyc differ diff --git a/temp/2022_05_14-1.jwb b/tests/files/2022_05_14-1.jwb similarity index 100% rename from temp/2022_05_14-1.jwb rename to tests/files/2022_05_14-1.jwb diff --git a/temp/sample_fluorescence.jws b/tests/files/sample_fluorescence.jws similarity index 100% rename from temp/sample_fluorescence.jws rename to tests/files/sample_fluorescence.jws diff --git a/tests/test_helpers.py b/tests/test_helpers.py index f5d3335..0702ec2 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,12 +1,19 @@ import unittest import jws2txt +from jws2txt.helpers.helpers import frange +import csv +import os + + +test_file = 'test.txt' class JWSFileTest(unittest.TestCase): def setUp(self) -> None: self.three_channels = jws2txt.JWSFile(r'tests/files/001Hg.jws') - - + self.single_channel = jws2txt.JWSFile(r'tests/files/sample_fluorescence.jws') + self.jwb_file = jws2txt.JWSFile(r'tests/files/2022_05_14-1.jwb') + def test_three_channels_creation(self): # Test file creation and initialization @@ -42,6 +49,97 @@ def test_three_channels_creation(self): self.assertEqual(self.three_channels.unpacked_data[2][2], 206.2129669189453) self.assertEqual(self.three_channels.unpacked_data[3][2], 0.11851496249437332) + # todo test for 1 ch file + # todo test for saving + + def test_single_channel(self): + # Test file creation and initialization + + self.assertEqual(self.single_channel.numchanels, 1) + self.assertEqual(self.single_channel.npoints, 301) + self.assertEqual(self.single_channel.x_for_first_point, 400.0) + self.assertEqual(self.single_channel.x_for_last_point, 700.0) + self.assertEqual(self.single_channel.x_increment, 1) + self.assertEqual(self.single_channel.header_codes, (268435715, 14)) + self.assertEqual(self.single_channel.header_names, ['WAVELENGTH', + 'FLUORESCENCE']) + self.assertEqual(self.single_channel.data_list, [3, + 1, + 0, + 1, + 1, + 301, + 400.0, + 700.0, + 1.0, + 268435715, + 14, + 268435715, + 14, + 400.0, + 700.0, + 700.0, + 0.0]) + self.assertEqual(self.single_channel.sample_name, 'photonic wire') + self.assertEqual(self.single_channel.comment, '') + #test x-axis and 3 channels + self.assertEqual(self.single_channel.unpacked_data[0][2], 402.0) + self.assertEqual(self.single_channel.unpacked_data[1][2], 24.556852340698242) + + def test_jwb_file_creation(self): + self.assertEqual(self.jwb_file.numchanels, 8) + self.assertEqual(self.jwb_file.npoints, 1001) + self.assertEqual(self.jwb_file.x_for_first_point, 900.0) + self.assertEqual(self.jwb_file.x_for_last_point, 400.0) + self.assertEqual(self.jwb_file.x_increment, -0.5) + self.assertEqual(self.jwb_file.header_codes, (268435715, 3, 0, 8)) + self.assertEqual(self.jwb_file.header_names, ['WAVELENGTH', 'ABSORBANCE', + 'undefined', 'undefined']) + self.assertEqual(self.jwb_file.data_list, [3, + 8, + 1, + 1, + 1, + 1001, + 900.0, + 400.0, + -0.5, + 268435715, + 3, + 0, + 8, + 24.979999542236328, + 60.0099983215332, + 0.0, + 2.00000035762821]) + self.assertEqual(self.jwb_file.sample_name, '') + self.assertEqual(self.jwb_file.comment, '') + # test x-axis and 3 channels + self.assertEqual(self.jwb_file.unpacked_data[0][2], 899.0) + # test 7th channel + self.assertEqual(self.jwb_file.unpacked_data[7][2], 0.07367147505283356) + + def test_writing(self): + self.three_channels.write_data(test_file) + with open(test_file, 'r') as file: + reader = csv.reader(file, dialect='excel') + self.assertEqual(next(reader), ['HK14_CN']) + os.remove(test_file) + + + +class FrangeTest(unittest.TestCase): + + def test_frange(self): + self.assertEqual(tuple(frange(1, 2, 0.2)), (1.0, 1.2, 1.4, 1.6, 1.8)) + self.assertEqual(tuple(frange(2, 0.9, -0.1)), (2.0, 1.9, 1.8, 1.7, 1.6, 1.5, + 1.4, 1.2999999999999998, 1.2, + 1.1, 1.0)) + + + + + if __name__=='__main__': unittest.main() \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..88ce087 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,19 @@ +import unittest +from jws2txt.jws2txt import str2bool +from argparse import ArgumentTypeError + + +class TestStr2Bool(unittest.TestCase): + + def test_str2bool(self): + self.assertTrue(str2bool('y')) + self.assertTrue(str2bool('true')) + self.assertFalse(str2bool('n')) + self.assertFalse(str2bool('f')) + with self.assertRaises(ArgumentTypeError): + str2bool('blurb') + + + +if __name__=='__main__': + unittest.main() \ No newline at end of file