diff --git a/.gitignore b/.gitignore index b91910d..b9c9a15 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,7 @@ /*.egg # Documentation -docs/build \ No newline at end of file +docs/build + +# Build +build/ \ No newline at end of file diff --git a/mritopng/__init__.py b/mritopng/__init__.py index 4886872..39dec62 100644 --- a/mritopng/__init__.py +++ b/mritopng/__init__.py @@ -29,7 +29,8 @@ def mri_to_png(mri_file, png_file): for row in image_2d: row_scaled = [] for col in row: - col_scaled = int((float(col) / float(max_val)) * 255.0) + # cliping pixel value if below 0 + col_scaled = int((float(max(col, 0)) / float(max_val)) * 255.0) row_scaled.append(col_scaled) image_2d_scaled.append(row_scaled) diff --git a/setup.py b/setup.py index 5daa345..6eb16f8 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ def readme(): setup( name='mritopng', - version='2.0', + version='2.1', description='Easily convert MRI filed based on the DICOM format to a PNG image', long_description=readme(), classifiers=[ diff --git a/tests/data/expected/000012.dcm.png b/tests/data/expected/000012.dcm.png new file mode 100644 index 0000000..df8e28a Binary files /dev/null and b/tests/data/expected/000012.dcm.png differ diff --git a/tests/data/expected/000017.dcm.png b/tests/data/expected/000017.dcm.png new file mode 100644 index 0000000..88f06cc Binary files /dev/null and b/tests/data/expected/000017.dcm.png differ diff --git a/tests/data/samples/000012.dcm b/tests/data/samples/000012.dcm new file mode 100644 index 0000000..395398c Binary files /dev/null and b/tests/data/samples/000012.dcm differ diff --git a/tests/data/samples/000017.dcm b/tests/data/samples/000017.dcm new file mode 100644 index 0000000..b65c67a Binary files /dev/null and b/tests/data/samples/000017.dcm differ diff --git a/tests/test_mritopng.py b/tests/test_mritopng.py index b7da464..2329a0a 100644 --- a/tests/test_mritopng.py +++ b/tests/test_mritopng.py @@ -8,9 +8,10 @@ import mritopng class TestMRIToPNG(unittest.TestCase): + """ Basic tests for mritopng """ def test_no_syntax_errors(self): - """ A dummy test to make sure the mritopng library got built """ + """ Test whether mritopng library got built """ if 'mri_to_png' not in dir(mritopng): self.fail() @@ -31,3 +32,27 @@ def test_convert_file(self): self.assertTrue(filecmp.cmp(actual_path, expected_path), 'PNG generated from dicom1 does not match the expected version') + + + def test_convert_file_with_negative_values(self): + """ Tests DICOM files with negative values, which are clipped to 0 """ + + cases = ['000012.dcm', '000017.dcm'] + curr_path = os.path.dirname(os.path.realpath(__file__)) + + for case in cases: + + sample_path = os.path.join(curr_path, 'data', 'samples', case) + expected_path = os.path.join(curr_path, 'data', 'expected', case + '.png') + actual_path = os.path.join(tempfile.gettempdir(), '%s.%s' % (uuid.uuid4(), "png")) + + print('Actual File Path: %s' % actual_path) + + # Try the file conversion + try: + mritopng.convert_file(sample_path, actual_path) + except Exception as err: + self.fail('%s' % err) + + self.assertTrue(filecmp.cmp(actual_path, expected_path), + 'PNG generated from dicom1 does not match the expected version')