Skip to content

Commit

Permalink
Fix logic to deal with DICOM pixel values below zero (#6)
Browse files Browse the repository at this point in the history
* clip dicom pixel value to 0 if below 0

* added test cases for the change

* update version

* add build to gitignore
  • Loading branch information
danishm committed Nov 5, 2017
1 parent 2602ec3 commit 8664ba7
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
/*.egg

# Documentation
docs/build
docs/build

# Build
build/
3 changes: 2 additions & 1 deletion mritopng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
Binary file added tests/data/expected/000012.dcm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/expected/000017.dcm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/samples/000012.dcm
Binary file not shown.
Binary file added tests/data/samples/000017.dcm
Binary file not shown.
27 changes: 26 additions & 1 deletion tests/test_mritopng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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')

0 comments on commit 8664ba7

Please sign in to comment.