def test_log_file(self): configure('test', filename=RESULT) logger('test').info("Test info log message.") logger('test').debug("Test debug log message.") assert_true(os.path.exists(RESULT), "The log file was not created: %s" % RESULT) with open(RESULT) as fs: msgs = fs.readlines() assert_true(not not msgs, "No log messages in %s" % RESULT) assert_equal(len(msgs), 1, "Extraneous log messages: %s" % msgs)
def __iter__(self): """ Iterates over each DICOM data set. :yield: the next pydicom dicom object """ for f in super(DicomIterator, self).__iter__(): with qiutil.file.open(f) as fp: try: yield dicom.read_file(fp, **self.opts) except InvalidDicomError: logger(__name__).info("Skipping non-DICOM file %s" % f)
def test_config_file(self): os.makedirs(RESULTS) # Since the config fixture specifies a relative log file 'log/test.log', # run this test in the context of the results directory. with cd(RESULTS): configure('test', config=FIXTURE) logger('test').info("Test info log message.") assert_true(os.path.exists(RESULT), "The log file was not created: %s" % RESULT) with open(RESULT) as fs: msgs = fs.readlines() assert_true(not not msgs, "No log messages in %s" % RESULT) msg = msgs[0] assert_true(msg.startswith('Custom: '), "The log message format is" " incorrect: %s" % msg)
def _disconnect(): """ Closes the xnat connection and deletes the cache directory. """ connect.xnat.close() cachedir = connect.cachedir # If this connection created a cache directory, then delete it. if cachedir: # Unset the cachedir first so that it can be safely deleted. connect.cachedir = None # Delete the cache directory try: shutil.rmtree(cachedir) except Exception: # Issue a warning and move on. logger(__name__).warn("Could not delete the XNAT cache" " directory %s" % cachedir) logger(__name__).debug('Closed the XNAT connection.')
def edit(*in_files, **opts): """ Edits the given DICOM files. The *dest* option can be either the destination directory path or an output file namer function. If *dest* is a directory path, then the output file location is a file in the given destination directory with the same unqualified file name as the input file. If the *dest* option is a function, then the output file location is the result of calling that function with the input file path as an argument. The default is to edit the file in-place. :param in_files: the input DICOM files or directories containing DICOM files :param opts: the following options: :keyword dest: the destination directory or file namer function :yield: the :meth`qidicom.reader.next` pydicom dicom object """ # Prepare the destination directory. dest = opts.get('dest') if dest: if isinstance(dest, str): dest = os.path.abspath(dest) if not os.path.exists(dest): os.makedirs(dest) else: dest = os.getcwd() if isinstance(dest, str): logger(__name__).debug("The edited DICOM files will be saved to %s." % dest) # Open the DICOM store on each DICOM file (skipping non-DICOM files), # yield to the edit callback and save to the file in the destination # directory. logger(__name__).debug("Editing %d DICOM files..." % len(in_files)) for ds in reader.iter_dicom(*in_files): # Delegate the edit. yield ds # Make the output file name. if isinstance(dest, str): # The dest option is a parent directory. _, fname = os.path.split(ds.filename) out_file = os.path.join(dest, fname) else: # The dest option is a function. out_file = dest(ds.filename) # Save the modified dataset. ds.save_as(out_file) logger(__name__).debug("Saved the edited DICOM files.")
def _connect(config=None, **opts): """ Opens a XNAT connection. :param config: the XNAT configuration file, or None for the :meth:`qixnat.configuration.load` default :param opts: the :class:`qixnat.facade.XNAT` initialization options """ # Load the configuration file or default. opts.update(configuration.load(config)) # If the pyxnat cachedir is not set, then make a new temp # directory for the exclusive use of this execution process. cachedir = opts.get('cachedir') if not cachedir: cachedir = tempfile.mkdtemp() connect.cachedir = opts['cachedir'] = cachedir logger(__name__).debug("The XNAT cache directory is %s" % cachedir) logger(__name__).debug('Connecting to XNAT...') connect.xnat = XNAT(**opts) logger(__name__).debug('Connected to XNAT.')
def __init__(self, **opts): """ :param opts: the XNAT configuration options """ self._logger = logger(__name__) self.interface = pyxnat.Interface(**opts)