Skip to content

Commit

Permalink
more tests written
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozef Tran committed May 31, 2023
1 parent 7061bd9 commit 3005523
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 29 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added jws2txt/__pycache__/jws2txt.cpython-38.pyc
Binary file not shown.
Binary file modified jws2txt/helpers/__pycache__/helpers.cpython-38.pyc
Binary file not shown.
6 changes: 4 additions & 2 deletions jws2txt/helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from struct import unpack
import csv
import olefile as ofio

from typing import Iterator

DATAINFO_FMT = '<LLLLLLdddLLLLdddd'

Expand All @@ -16,7 +16,7 @@



def frange(start: float, stop: float = 0, step: float = 1.0):
def frange(start: float, stop: float = 0, step: float = 1.0) -> Iterator[float]:
"""Return evenly spaced number over specified range.
Args:
Expand Down Expand Up @@ -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.
"""
Expand Down
2 changes: 1 addition & 1 deletion jws2txt/jws2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions temp/test.py
Original file line number Diff line number Diff line change
@@ -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)


#%%
# %%

Expand All @@ -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]
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

Check failure on line 41 in temp/test.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Ruff (E402)

temp/test.py:41:1: E402 Module level import not at top of file

Check failure on line 41 in temp/test.py

View workflow job for this annotation

GitHub Actions / build (3.11)

Ruff (E402)

temp/test.py:41:1: E402 Module level import not at top of file


tuple(frange(2, 0.9, -0.1))
# %%
Binary file modified tests/__pycache__/test_helpers.cpython-38.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_main.cpython-38.pyc
Binary file not shown.
File renamed without changes.
File renamed without changes.
102 changes: 100 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
19 changes: 19 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 3005523

Please sign in to comment.