class TestDBquery(unittest2.TestCase): def setUp(self): self.dbi = DBI() self.dbi.connect() # try: # self.deleteData() # except OperationalError as e: # print(e.args[0]) # self.tearDown() def deleteData(self): # Reset all data so tests will work - so only use with a test db self.dbi.deleteData('seriesprocess') self.dbi.deleteData('dicomfiles') self.dbi.deleteData('dicomdata') def tearDown(self): self.dbi.conn.close() def test_getCaptions(self): data = self.dbi.getCaptions() expected = 0 print('Captions: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getRefs(self): data = self.dbi.getRefs() expected = 0 print('Refs: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getDescription(self): caption = 'QSM' data = self.dbi.getDescription(caption) expected = 0 print('Description: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getCaption(self): caption = 'qsm' data = self.dbi.getCaption(caption) expected = 0 print('Caption: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getProcessModule(self): caption = 'qsm' data = self.dbi.getProcessModule(caption) expected = 0 print('Module: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getProcessClass(self): caption = 'qsm' data = self.dbi.getProcessClass(caption) expected = 0 print('Class: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getFiles(self): uuid = '5d74a20b44ec1dfd0af4fbc6bb680e0f557c14a08a143b843ef40977697e2bea' data = self.dbi.getFiles(uuid) expected = 0 print('Files: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getNoFiles(self): uuid = 't10001' data = self.dbi.getFiles(uuid) expected = 0 print('Files: ', data) self.assertEqual(expected, len(data)) def test_addDicomdata(self): dicomdata = { 'uuid': 't10000', 'patientid': 'p1', 'patientname': 'test patient', 'seriesnum': '1.001.111', 'sequence': 'ftest', 'protocol': 'aaa', 'imagetype': 'M' } try: rtn = self.dbi.addDicomdata(dicomdata) self.assertEqual(rtn, 1, 'Dicom data add failed') except IntegrityError as e: self.skipTest(e.args[0]) def test_hasUuid(self): uuid = 't10000' self.assertIs(self.dbi.hasUuid(uuid), True, 'Data already added') def test_addDicomdataExisting(self): dicomdata = { 'uuid': 't10000', 'patientid': 'p1', 'patientname': 'test patient', 'seriesnum': '1.001.111', 'sequence': 'ftest', 'protocol': 'aaa', 'imagetype': 'M' } try: self.assertRaises(IntegrityError, self.dbi.addDicomdata, dicomdata) except AssertionError as e: self.skipTest(e.args[0]) def test_addDicomfileExisting(self): uuid = 't10000' dicomfile = "D:\\Projects\\XNAT\\data\\S1\\scans\\3\\1001DS.MR.RESEARCH_16022_OPTIMIZING_EXERCISE.0003.0001.2017.02.24.15.41.05.593750.93525560.IMA" try: self.assertRaises(IntegrityError, self.dbi.addDicomfile, uuid, dicomfile) #self.assertEqual(rtn, 1, 'Dicom file add failed') except AssertionError as e: self.skipTest(e.args[0]) def test_addDicomfile(self): uuid = 't10000' dicomfile = "D:\\Projects\\XNAT\\data\\S1\\scans\\3\\1001DS.MR.RESEARCH_16022_OPTIMIZING_EXERCISE.0003.0001.2017.02.24.15.41.05.593750.93525560.IMA" try: rtn = self.dbi.addDicomfile(uuid, dicomfile) self.assertEqual(rtn, 1, 'Dicom file add failed') except IntegrityError as e: self.skipTest(e.args[0]) def test_getUuids(self): data = self.dbi.getUuids() expected = 0 print('UUIDS: ', data) self.assertIsNotNone(data) self.assertGreater(len(data), expected) def test_getNumberFiles(self): uuid = '5d74a20b44ec1dfd0af4fbc6bb680e0f557c14a08a143b843ef40977697e2bea' data = self.dbi.getNumberFiles(uuid) expected = 0 print('Num files: ', data) self.assertIsNotNone(data) self.assertGreater(data, expected) def test_getDicomdataAll(self): uuid = 't10000' field = 'all' data = self.dbi.getDicomdata(uuid, field) expected = 0 print('Dicomdata for: ', field, '=', data) self.assertIsNotNone(data) self.assertGreater(data, expected) def test_getDicomdata(self): uuid = 't10000' field = 'protocol' data = self.dbi.getDicomdata(uuid, field) expected = 0 print('Dicomdata for: ', field, '=', data) self.assertIsNotNone(data) self.assertGreater(data, expected) def test_getRef(self): pname = 'QSM' expected = 'qsm' data = self.dbi.getRef(pname) self.assertEqual(expected, data) # INSERTING DATA - USE TEST DB ONLY # def test_setSeriesProcess(self): # uuid = '5d74a20b44ec1dfd0af4fbc6bb680e0f557c14a08a143b843ef40977697e2bea' # pid = 1 # server = 'AWS' # status = 1 # starttime = datetime.datetime.now() # try: # rtn = self.dbi.setSeriesProcess(uuid, pid,server,status,starttime) # self.assertEqual(rtn, 1, 'Series Process add failed') # except IntegrityError as e: # self.skipTest(e.args[0]) def test_getActiveProcesses(self): data = self.dbi.getActiveProcesses() print(data) def test_deleteSeriesData(self): uuid = '5d74a20b44ec1dfd0af4fbc6bb680e0f557c14a08a143b843ef40977697e2bea' self.dbi.deleteSeriesData(uuid)
class Controller(): def __init__(self): self.logger = self.__loadLogger() self.db = DBI() if self.db.c is None: self.db.connect() def __loadLogger(self, outputdir=None): #### LoggingConfig logger.setLevel(logging.DEBUG) homedir = expanduser("~") if outputdir is not None and access(outputdir, R_OK): homedir = outputdir if not access(join(homedir, ".d2c", "logs"), R_OK): mkdir(join(homedir, ".d2c", "logs")) self.logfile = join(homedir, ".d2c", "logs", 'd2c.log') handler = RotatingFileHandler(filename=self.logfile, maxBytes=10000000, backupCount=10) formatter = logging.Formatter( '[ %(asctime)s %(levelname)-4s ] %(filename)s %(lineno)d : (%(threadName)-9s) %(message)s' ) handler.setFormatter(formatter) logger.addHandler(handler) return logger # ---------------------------------------------------------------------- def RunProcess(self, wxGui, inputdir, uuid, processname, server, row): """ Run processing in a thread :param wxGui: Process Panel ref :param inputdir: Directory with DICOM files :param uuid: unique ID from series number :param processname: Process eg QSM :param server: Cloud server to use eg AWS :param row: Row number in progress table in Process Panel :return: """ filenames = self.db.getSessionFiles(uuid) try: if len(filenames) > 0: msg = "Load Process Threads: %s [row: %d]" % (processname, row) print(msg) # Run thread t = ProcessThread(wxGui, processname, inputdir, uuid, server, row) t.start() msg = "Running Thread: %s" % processname print(msg) # Load to database for remote monitoring self.db.setSessionProcess(uuid, self.db.getProcessId(processname), server, 1, datetime.datetime.now(), inputdir) else: msg = "No files to process" logger.error(msg) raise ValueError(msg) except ValueError as e: raise e except Exception as e: raise e def removeInputFiles(self, uuid, inputdir): """ Remove temporary files in outputdir (assumes tar file created and uploaded to Docker) Remove file entries? :return: """ files = iglob(join(inputdir, '*.IMA')) for f in files: os.remove(f) # remove database entry - dicomdata and dicomfiles self.db.deleteSessionData(uuid) def checkRemote(self): # Check if cloud processing is done and update database seriesprocesses = self.db.getActiveProcesses() for series in seriesprocesses: print("CheckRemote:", series) seriesid = series[0] server = series[2].lower() outputdir = series[6] if outputdir is None or len(outputdir) <= 0: files = self.db.getFiles(seriesid) outputdir = dirname(files[0]) # Get uploader class and query uploaderClass = get_class(server) if uploaderClass is not None: uploader = uploaderClass(seriesid) if uploader.isDone(): downloadfile = join(outputdir, seriesid, 'download.tar') uploader.download(downloadfile) msg = 'Series: %s \n\tSTATUS: Complete (%s)\n' % ( seriesid, downloadfile) print(msg) self.db.setSessionProcessFinished(seriesid) else: # Still in progress self.db.setSeriesProcessInprogress(seriesid) else: # assume done msg = 'Series: %s \n\tSTATUS: Complete\n' % seriesid print(msg) self.db.setSessionProcessFinished(seriesid) def parseDicom(self, wxObject, filelist): ''' Read DICOM headers for filelist and load series info to db :param filelist: :return: ''' t = DicomThread(wxObject, filelist) t.start()