Skip to content

Commit

Permalink
Fix LGTM alerts (#186)
Browse files Browse the repository at this point in the history
* LGTM warning: Unguarded next in generator

Call to next() in a generator

Additionnlly, dicom_file cannot be None:
• os.path.splitext(dicom_file) would raise TypeException
• In the case of a ZIP file, which I believe is the one the existing
  code was trying to address, we continue on StopIteration if the ZIP
  file is empty.

* LGTM recommendation: Unused import

Import of 'Sequence' is not used.
Import of 'remove_sequences' is not used.
Import of 'os' is not used.
Import of 'read_json' is not used.
Import of 'DeidRecipe' is not used.
Import of 'get_private' is not used.
Import of 'remove_sequences' is not used.
Import of 'extract_fields_list' is not used.
Import of 'extract_values_list' is not used.
Import of 'get_fields' is not used.
Import of 'DataElement' is not used.
Import of 'operator' is not used.
Import of 'get_installdir' is not used.
Import of 'os' is not used.
mport of 'read_file' is not used.
Import of 'os' is not used.

* LGTM recommendation: Except block handles 'BaseException'

Except block directly handles BaseException.

Function dateutil.parser.parse() raises the following exceptions:
• ParserError – Raised for invalid or unknown string formats, if the provided tzinfo is not in a valid format, or if an invalid date would be created.
• OverflowError – Raised if the parsed date exceeds the largest valid C integer on your system.
  • Loading branch information
DimitriPapadopoulos authored Oct 7, 2021
1 parent 54c8f68 commit 18f5b45
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 32 deletions.
1 change: 0 additions & 1 deletion deid/dicom/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""

from pydicom.dataset import Dataset
from pydicom.sequence import Sequence
from deid.logger import bot
import re

Expand Down
3 changes: 0 additions & 3 deletions deid/dicom/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@


from deid.logger import bot
from .tags import remove_sequences
from .fields import get_fields, expand_field_expression
from pydicom.multival import MultiValue

import os


def extract_values_list(dicom, actions, fields=None):
"""Given a list of actions for a named group (a list) extract values from
Expand Down
7 changes: 1 addition & 6 deletions deid/dicom/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@


from deid.logger import bot
from deid.utils import read_json

from deid.config import DeidRecipe

from pydicom import read_file

from deid.dicom.utils import save_dicom
from deid.dicom.tags import remove_sequences, get_private
from deid.dicom.groups import extract_values_list, extract_fields_list
from deid.dicom.fields import get_fields
from deid.dicom.tags import remove_sequences
from deid.dicom.parser import DicomParser

import os
Expand Down
1 change: 0 additions & 1 deletion deid/dicom/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from deid.logger import bot
from pydicom.tag import tag_in_exception
from pydicom.sequence import Sequence
from pydicom.dataelem import DataElement
from pydicom._dicom_dict import DicomDictionary, RepeatersDictionary
from pydicom.tag import Tag
import re
Expand Down
32 changes: 17 additions & 15 deletions deid/dicom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ def get_files(contenders, check=True, pattern=None, force=False, tempdir=None):
if dextension == ".zip":
with zipfile.ZipFile(dicom_file, "r") as compressedFile:
compressedFile.extractall(tempdir)
dicom_file = next(
os.path.join(tempdir, f)
for f in os.listdir(tempdir)
if os.path.isfile(os.path.join(tempdir, f))
)

if dicom_file is not None:
if check:
validated_files = validate_dicoms(dicom_file, force=force)
else:
validated_files = [dicom_file]

for validated_file in validated_files:
bot.debug("Found contender file %s" % (validated_file))
yield validated_file
try:
dicom_file = next(
os.path.join(tempdir, f)
for f in os.listdir(tempdir)
if os.path.isfile(os.path.join(tempdir, f))
)
except StopIteration:
continue # ZIP file does not contain any file

if check:
validated_files = validate_dicoms(dicom_file, force=force)
else:
validated_files = [dicom_file]

for validated_file in validated_files:
bot.debug("Found contender file %s" % (validated_file))
yield validated_file


def save_dicom(dicom, dicom_file, output_folder=None, overwrite=False):
Expand Down
2 changes: 1 addition & 1 deletion deid/utils/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_timestamp(item_date, item_time=None, jitter_days=None, format=None):

try:
timestamp = dateutil.parser.parse("%s%s" % (item_date, item_time))
except:
except (dateutil.parser.ParserError, OverflowError):
timestamp = datetime.strptime("%s%s" % (item_date, item_time), format)

if jitter_days is not None:
Expand Down
1 change: 0 additions & 1 deletion examples/dicom/dicom-extract/create-dicom-csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import platform
import csv
import operator
from collections.abc import Sequence
from collections import OrderedDict

Expand Down
2 changes: 0 additions & 2 deletions examples/dicom/header-manipulation/func-replacement.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env python3

from deid.dicom import get_files, replace_identifiers
from deid.utils import get_installdir
from deid.data import get_dataset
import os

# This is an example of replacing fields in dicom headers,
# but via a function instead of a preset identifier.
Expand Down
2 changes: 0 additions & 2 deletions examples/dicom/pixels/run-inspect-pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

# This will get a set of example cookie dicoms
from deid.dicom import get_files, has_burned_pixels
from pydicom import read_file
from deid.data import get_dataset
from deid.logger import bot
import os

bot.level = 3

Expand Down

0 comments on commit 18f5b45

Please sign in to comment.