From ee389369d841bc7d64d992f04f1050865a2647e2 Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 18 Nov 2018 09:01:41 -0600 Subject: [PATCH] Refactor configuration file checks and template generation --- pylidc/Scan.py | 95 +++++++++++++++++++++++++++++----------------- pylidc/__init__.py | 3 +- 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/pylidc/Scan.py b/pylidc/Scan.py index 421cc57..58e65f1 100644 --- a/pylidc/Scan.py +++ b/pylidc/Scan.py @@ -1,13 +1,16 @@ +import os +import sys +import warnings + +import pydicom as dicom +import numpy as np + import sqlalchemy as sq from sqlalchemy.orm import relationship from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from ._Base import Base -import os, warnings -import pydicom as dicom -import numpy as np - import matplotlib.pyplot as plt from matplotlib.widgets import Slider, CheckButtons @@ -17,8 +20,6 @@ from scipy.stats import mode - -# Load the configuration file and get the dicom file path. try: import configparser except ImportError: @@ -26,38 +27,59 @@ configparser = ConfigParser -cfgname = 'pylidc.conf' if os.name == 'nt' else '.pylidcrc' -cfgpath = os.path.join(os.path.expanduser('~'), cfgname) -dicompath = None -warndpath = True +def _get_config_filename(): + """ + Yields the platform-specific configuration filename + """ + return 'pylidc.conf' if sys.platform.startswith('win') else '.pylidcrc' + -if os.path.exists(cfgpath): - cp = configparser.SafeConfigParser() - cp.read(cfgpath) +def _get_config_path(): + """ + Yields the path to configuration file + """ + return os.path.join(os.path.expanduser('~')) - if cp.has_option('dicom', 'path'): - dicompath = cp.get('dicom', 'path') - if cp.has_option('dicom', 'warn'): - warndpath = cp.get('dicom', 'warn') == 'True' +def _get_config_file(): + return os.path.join(_get_config_path(), + _get_config_filename()) + + +def _get_dicom_file_path_from_config_file(): + """ + Loads the dicom section of the configuration file + """ + conf_file = _get_config_file() + + parser = configparser.SafeConfigParser() + + if os.path.exists(conf_file): + parser.read(conf_file) + + try: + return parser.get(section='dicom', option='path') + except (configparser.NoSectionError, + configparser.NoOptionError): + msg = ("Could not find `dicom` configuration section or " + " `path` configuration option under that section." + "A template config file will be written to {}.") + warnings.warn(msg.format(conf_file)) + + parser.add_section('dicom') + parser.set('dicom', 'path', '') + + with open(conf_file, 'w') as f: + parser.write(f) + + return parser.get(section='dicom', option='path') -if dicompath is None: - dpath_msg = \ - '\n\n`.pylidcrc` configuration file does not exist ' + \ - 'or path is not set. CT images will not be viewable.\n' + \ - ('The file, `.pylidcrc`, should exist in %s. '%os.path.expanduser('~')) + \ - 'This file should have format:\n\n' + \ - '[dicom]\n' + \ - 'path = /path/to/dicom/data/LIDC-IDRI\n' + \ - 'warn = True\n\n' + \ - 'Set `warn` to `False` to suppress this message.\n' - if warndpath: - warnings.warn(dpath_msg) _off_limits = ['id','study_instance_uid','series_instance_uid', 'patient_id','slice_thickness','pixel_spacing', 'contrast_used','is_from_initial','sorted_dicom_file_names'] + class Scan(Base): """ The Scan model class refers to the top-level XML file from the LIDC. @@ -153,7 +175,7 @@ def __setattr__(self, name, value): `%s` a value of `%s`." % (name,value) raise ValueError(msg) else: - super(Scan,self).__setattr__(name,value) + super(Scan, self).__setattr__(name,value) def get_path_to_dicom_files(self): """ @@ -184,14 +206,19 @@ def get_path_to_dicom_files(self): Option 2 is less efficient than 1; however, option 2 is robust. """ - if dicompath is None: - raise EnvironmentError(dpath_msg) + dicompath = _get_dicom_file_path_from_config_file() + + if not os.path.exists(dicompath): + msg = ("Could not establish path to dicom files. Have you " + "specified the `path` option in the configuration " + "file {}?") + raise RuntimeError(msg.format(_get_config_file())) base = os.path.join(dicompath, self.patient_id) if not os.path.exists(base): - raise IOError("Couldn't find DICOM files for %s in %s." - % (self, base)) + msg = "Couldn't find DICOM files for {} in {}" + raise RuntimeError(msg.format(self, base)) path = os.path.join(base, self.study_instance_uid, diff --git a/pylidc/__init__.py b/pylidc/__init__.py index 3ca0b0d..6febf3a 100644 --- a/pylidc/__init__.py +++ b/pylidc/__init__.py @@ -20,7 +20,7 @@ """ from __future__ import print_function as _pf -__version__ = '0.2.0' +__version__ = '0.2.1' # Hidden stuff. import os as _os @@ -34,7 +34,6 @@ # Public stuff. from .Scan import Scan -from .Scan import dicompath from .Annotation import Annotation from .Contour import Contour from .Zval import Zval