Skip to content

Commit

Permalink
Remove Python 2 support (#367)
Browse files Browse the repository at this point in the history
* Removed Python 2 support.

* Updated documentation to indicate removal of Python 2 support.
  • Loading branch information
bastula authored Jun 13, 2023
1 parent c04d0a7 commit e6dd12d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 50 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ History

0.5.7 (unreleased)
------------------
- Dropped support for Python 2.

0.5.6 (2023-05-08)
------------------
Expand Down
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Dependencies
- `numpy <http://www.numpy.org>`__ 1.2 or higher
- `pydicom <https://pydicom.github.io>`__ 0.9.9 or higher (pydicom 1.0 compatible)
- `matplotlib <http://matplotlib.org>`__ 1.3.0 or higher (for DVH calculation)
- `six <https://pythonhosted.org/six/>`__ 1.5 or higher
- Optional:

- `Pillow <https://pillow.readthedocs.io>`__ (for image display)
Expand Down
50 changes: 11 additions & 39 deletions dicompylercore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,24 @@
# See the file license.txt included with this distribution, also
# available at https://github.com/dicompyler/dicompyler-core/

from six import PY2

mpl_available = True
pil_available = True
shapely_available = True
skimage_available = True
scipy_available = True

if PY2:
import imp
try:
imp.find_module('matplotlib')
except ImportError:
mpl_available = False

try:
imp.find_module('PIL')
except ImportError:
pil_available = False

try:
imp.find_module('shapely')
except ImportError:
shapely_available = False

try:
imp.find_module('skimage')
except ImportError:
skimage_available = False
import importlib

try:
imp.find_module('scipy')
except ImportError:
scipy_available = False
else:
import importlib
mpl_available = importlib.util.find_spec("matplotlib") is not None
pil_available = importlib.util.find_spec('PIL') is not None
shapely_available = importlib.util.find_spec('shapely') is not None
skimage_available = importlib.util.find_spec('skimage') is not None
scipy_available = importlib.util.find_spec('scipy') is not None
mpl_available = importlib.util.find_spec("matplotlib") is not None
pil_available = importlib.util.find_spec("PIL") is not None
shapely_available = importlib.util.find_spec("shapely") is not None
skimage_available = importlib.util.find_spec("skimage") is not None
scipy_available = importlib.util.find_spec("scipy") is not None


# DICOM UID prefix
dicompyler_uid_prefix = '1.2.826.0.1.3680043.8.1070.'
dicompyler_uid_prefix_image = dicompyler_uid_prefix + '1.'
dicompyler_uid_prefix_rtstruct = dicompyler_uid_prefix + '2.'
dicompyler_uid_prefix_rtplan = dicompyler_uid_prefix + '3.'
dicompyler_uid_prefix_rtdose = dicompyler_uid_prefix + '4.'
dicompyler_uid_prefix = "1.2.826.0.1.3680043.8.1070."
dicompyler_uid_prefix_image = dicompyler_uid_prefix + "1."
dicompyler_uid_prefix_rtstruct = dicompyler_uid_prefix + "2."
dicompyler_uid_prefix_rtplan = dicompyler_uid_prefix + "3."
dicompyler_uid_prefix_rtdose = dicompyler_uid_prefix + "4."
10 changes: 4 additions & 6 deletions dicompylercore/dicomparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from dicom.dataset import Dataset
import random
from numbers import Number
from six import PY2, iterkeys, string_types, BytesIO
from six.moves import range
from io import BytesIO
from pathlib import Path
from dicompylercore import dvh, util
from dicompylercore.config import pil_available, shapely_available

Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(self, dataset, memmap_pixel_array=False):
self.memmap_pixel_array = memmap_pixel_array
if isinstance(dataset, Dataset):
self.ds = dataset
elif isinstance(dataset, (string_types, BytesIO)):
elif isinstance(dataset, (str, BytesIO, Path)):
try:
with open(dataset, "rb") as fp:
self.ds = read_file(fp, defer_size=100, force=True,
Expand Down Expand Up @@ -203,8 +203,6 @@ def GetDemographics(self):
'birth_date': None,
'gender': 'Other'}
if 'PatientName' in self.ds:
if PY2:
self.ds.decode()
name = self.ds.PatientName
patient['name'] = str(name)
patient['given_name'] = name.given_name
Expand Down Expand Up @@ -668,7 +666,7 @@ def CalculatePlaneThickness(self, planesDict):
planes = []

# Iterate over each plane in the structure
for z in iterkeys(planesDict):
for z in planesDict.keys():
planes.append(float(z))
planes.sort()

Expand Down
5 changes: 2 additions & 3 deletions dicompylercore/dvhcalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from collections.abc import Sequence
except ImportError:
from collections import Sequence
from six import iteritems
import logging
logger = logging.getLogger('dicompylercore.dvhcalc')

Expand Down Expand Up @@ -140,10 +139,10 @@ def _calculate_dvh(structure,
This is an internal function called by `get_dvh` and
should not be called directly.
"""
planes = collections.OrderedDict(sorted(iteritems(structure['planes'])))
calcdvh = collections.namedtuple('DVH', ['notes', 'histogram'])
logger.debug("Calculating DVH of %s %s", structure['id'],
structure['name'])
planes = collections.OrderedDict(sorted(structure["planes"].items()))

# Create an empty array of bins to store the histogram in cGy
# only if the structure has contour data or the dose grid exists
Expand Down Expand Up @@ -207,7 +206,7 @@ def _calculate_dvh(structure,
'thickness'] / (interpolation_segments_between_planes + 1)

# Iterate over each plane in the structure
for z, plane in iteritems(planes):
for z, plane in planes.items():
# Get the dose plane for the current structure plane
if interpolation_resolution or use_structure_extents:
doseplane = get_interpolated_dose(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27, py37
envlist = py37

[testenv]
setenv =
Expand Down

0 comments on commit e6dd12d

Please sign in to comment.