def OnGenerateReports(self, event):
        """
        Download CSVs
        :param event:
        :return:
        """
        downloaddirname = self.m_downloaddir.GetPath()
        deltas = self.chDelta.GetValue()
        if downloaddirname is not None and self.db is not None and self.proj is not None:
            self.Destroy()  # close window
            xnat = XnatConnector(self.configfile, self.db)
            xnat.connect()
            subjects = xnat.getSubjectsDataframe(self.proj)
            op = OPEXReport(subjects=subjects)
            op.xnat = xnat
            if op.generateCantabReport(projectcode=self.proj,
                                       outputdir=downloaddirname,
                                       deltas=True):
                msg = "***CANTAB report Completed: %s" % downloaddirname
                logging.info(msg)
            else:
                msg = "Error during CANTAB report"
                logging.error(msg)

            if op.generateBloodReport(projectcode=self.proj,
                                      outputdir=downloaddirname):
                msg = "***BLOOD report Completed: %s" % downloaddirname
                logging.info(msg)
            else:
                msg = "Error during BLOOD report"
                logging.error(msg)

            dlg = wx.MessageDialog(self, msg, "Download CSVs", wx.OK)
            dlg.ShowModal()  # Show it
            dlg.Destroy()
Example #2
0
    def __init__(self,
                 listofexperiments,
                 columns=None,
                 criteria=None,
                 **kwargs):
        XnatConnector.__init__(self, **kwargs)
        self.connect()

        if listofexperiments is None:
            listofexperiments = self.conn.inspect.datatypes('opex:')

        xnatcompiler = {}
        for experiment in listofexperiments:
            print(('Compiling {}'.format(experiment)))
            init = fetchData(connector=self,
                             experiment=experiment,
                             columns=columns,
                             criteria=criteria)

            init.connector.conn.disconnect()

            xnatcompiler[experiment] = {
                'data': init.data,
                'fields': init.fields,
                'ID': init.IDS
            }

        self.compiler = xnatcompiler
        dflist = [
            self.compiler[key]['data'] for key in list(self.compiler.keys())
        ]
        self.data = reduce(
            lambda x, y: pd.merge(
                x, y, on=['Subject', 'interval', 'Exercise', 'Gender']),
            dflist)
    def OnCSVDownload(self, event):
        """
        Download CSVs
        :param event:
        :return:
        """
        downloaddirname = self.m_downloaddir.GetPath()
        deltas = self.chDelta.GetValue()
        if downloaddirname is not None and self.db is not None and self.proj is not None:

            xnat = XnatConnector(self.configfile, self.db)
            xnat.connect()
            subjects = xnat.getSubjectsDataframe(self.proj)
            op = OPEXReport(subjects=subjects)
            op.xnat = xnat
            if op.downloadOPEXExpts(self.proj, downloaddirname, deltas):
                msg = "***Downloads Completed: %s" % downloaddirname
                logging.info(msg)
            else:
                msg = "Error during download"
                logging.error(msg)

            dlg = wx.MessageDialog(self, msg, "Download CSVs", wx.OK)
            dlg.ShowModal()  # Show it
            dlg.Destroy()
 def setUp(self):
     configfile = join(expanduser('~'), '.xnat.cfg')
     server = 'xnat-dev'
     self.projectcode = 'TEST'
     self.xnat = XnatConnector(configfile, server)
     self.xnat.connect()
     if not self.xnat.conn:
         self.skipTest("Database not connecting")
Example #5
0
class TestScanUploader(unittest.TestCase):
    '''
    Tests require a test XNAT instance to connect to with:
    1. Login details in a file in the user home directory (.xnat.cfg)
    2. Configuration name of the server to use for login
    3. A project ID with subject as sampledata to test access
    '''
    def setUp(self):
        """
        Requires MRI scans to upload to test XNAT instance
        :return:
        """
        scandir="D:\\Projects\\XNAT\\data\\S1\\scans\\3"
        self.seriesnum = basename(scandir)
        scanfiles = [f for f in listdir(scandir) if not isdir(join(scandir, f))]
        if len(scanfiles) > 0:
            self.dicomfile=join(scandir,scanfiles[0])
        else:
            self.skipTest('No DICOM test files found')



    def tearDown(self):
        print('Done')

    def test_getScanType(self):
        scanuploader = ScanUploader()
        type = scanuploader.getScanType(scanuploader.default_scantype, self.dicomfile)
        self.assertEqual(type, scanuploader.default_scantype, "Scan type not default")

    def test_getModality(self):
        scanuploader = ScanUploader()
        type = scanuploader.getModality(self.dicomfile)
        self.assertIsNotNone(type, 'Modality is none')

    def test_getSeriesNumber(self):
        scanuploader = ScanUploader()
        type = scanuploader.getSeriesNumber(self.seriesnum,self.dicomfile)
        self.assertIsNotNone(type, 'SeriesNumber is none')
        self.assertEqual(int(type), int(self.seriesnum), 'Series number not matching')

    def test_getSeriesDatestamp(self):
        scanuploader = ScanUploader()
        do = scanuploader.getSeriesDatestamp(self.dicomfile)
        self.assertIsNotNone(do, 'Date is none')
        self.assertIsInstance(do, datetime, 'Date is not datetime instance')

    def testUpload(self):
        '''
        Will upload scan series - so don't run this often
        :return:
        '''
        configfile = join(expanduser('~'), '.xnat.cfg')
        server = 'xnat-dev'
        self.xnat = XnatConnector(configfile, server)
        self.xnat.connect()
        if not self.xnat.conn:
            self.skipTest("Database not connecting")
Example #6
0
def rundownloads(database, projectcode, output):
    xnat = XnatConnector(join(expanduser('~'), '.xnat.cfg'), database)
    xnat.connect()
    subjects = xnat.getSubjectsDataframe(projectcode)
    op = OPEXReport(subjects=subjects)
    op.xnat = xnat
    outputdir = output
    op.downloadOPEXExpts(projectcode=projectcode,
                         outputdir=outputdir,
                         deltas=True)
Example #7
0
 def testUpload(self):
     '''
     Will upload scan series - so don't run this often
     :return:
     '''
     configfile = join(expanduser('~'), '.xnat.cfg')
     server = 'xnat-dev'
     self.xnat = XnatConnector(configfile, server)
     self.xnat.connect()
     if not self.xnat.conn:
         self.skipTest("Database not connecting")
 def OnGenerateReports(self, event):
     """
     Download CSVs
     :param event:
     :return:
     """
     downloaddirname = self.m_downloaddir.GetPath()
     deltas = self.chDelta.GetValue()
     if downloaddirname is not None and self.db is not None and self.proj is not None:
         xnat = XnatConnector(self.configfile, self.db)
         xnat.connect()
         subjects = xnat.getSubjectsDataframe(self.proj)
         if subjects is not None:
             op = OPEXReport(subjects=subjects)
             op.xnat = xnat
             msg = "Generating CANTAB report ..."
             logging.info(msg)
             print(msg)
             if op.generateCantabReport(projectcode=self.proj,
                                        outputdir=downloaddirname,
                                        deltas=deltas):
                 msg = "***CANTAB report Completed: %s" % downloaddirname
                 logging.info(msg)
             else:
                 msg = "Error during CANTAB report"
                 logging.error(msg)
             print(msg)
             msg = "Generating BLOOD report ..."
             logging.info(msg)
             print(msg)
             if op.generateBloodReport(projectcode=self.proj,
                                       outputdir=downloaddirname,
                                       deltas=deltas):
                 msg = "***BLOOD report Completed: %s" % downloaddirname
                 logging.info(msg)
             else:
                 msg = "Error during BLOOD report"
                 logging.error(msg)
             print(msg)
         else:
             msg = "Cannot list subjects from XNAT"
             logging.error(msg)
             print(msg)
         # close connection
         op.xnat.conn.disconnect()
Example #9
0
def get_maxHR(subject):
    home = expanduser("~")
    configfile = join(home, '.xnat.cfg')
    xnat = XnatConnector(configfile, 'opex')
    xnat.connect()

    criteria = [('xnat:subjectData/SUBJECT_ID', 'LIKE', '*'), 'AND']
    columns = ['xnat:subjectData/SUBJECT_LABEL', 'opex:cosmed/INTERVAL', 'opex:cosmed/HR']
    dsitype = 'opex:cosmed'

    subj = xnat.conn.select(dsitype, columns).where(criteria)
    xnat.conn.disconnect()

    df = pd.DataFrame(list(subj)).\
                        set_index(['xnat_subjectdata_subject_label', 'interval']).\
                        loc[subject]

    HR_max0, HR_max3 = float(df.loc['0']), float(df.loc['3'])

    return HR_max0, HR_max3
class TestConnector(unittest.TestCase):
    '''
    Tests require a test XNAT instance to connect to with:
    1. Login details in a file in the user home directory (.xnat.cfg)
    2. Configuration name of the server to use for login
    3. A project ID with subject as sampledata to test access
    '''
    def setUp(self):
        configfile = join(expanduser('~'), '.xnat.cfg')
        server = 'xnat-dev'
        self.projectcode = 'TEST'
        self.xnat = XnatConnector(configfile, server)
        self.xnat.connect()
        if not self.xnat.conn:
            self.skipTest("Database not connecting")

    def tearDown(self):
        self.xnat.conn.disconnect()

    def test_get_project(self):
        projectcode = self.projectcode
        proj = self.xnat.get_project(projectcode)
        self.assertIsNotNone(proj, "Project not found")
        self.assertEqual(projectcode, proj.id(), "Project code is not equal")

    def test_get_projectPI(self):
        projectcode = self.projectcode
        proj = self.xnat.get_projectPI(projectcode)
        lastname = 'Einstein'
        self.assertIsNotNone(proj, "Project PI not found")
        self.assertEqual(lastname, proj, "Project code is not equal")

    def test_get_subjectbylabel(self):
        projectcode = self.projectcode
        subjectlabel = 'S0001'
        subjectid = 'IRC5XNAT_S00001'
        sid = self.xnat.get_subjectid_bylabel(projectcode, subjectlabel)
        self.assertIsNotNone(sid, "Subject ID not found")
        self.assertEqual(sid, subjectid, "Subject id is not equal")
Example #11
0
    def loadData(self):
        output = "RefreshCounts_%s.csv" % datetime.today().strftime("%Y%m%d")
        # output = "ExptCounts.csv"
        # outputfile = join(self.cache, output)

        # if exists(outputfile):
        #     report = OPEXReport(csvfile=outputfile)
        #     logging.info("Loading via cache")
        # else:
        #     logging.info("Loading from database")
        configfile = self.dbconfig
        xnat = XnatConnector(configfile, self.database)
        try:
            xnat.connect()
            if xnat.testconnection():
                print("...Connected")
                output = "ExptCounts.csv"
                # outputfile = join(self.cache, output)
                # if access(outputfile, R_OK):
                #     csv = outputfile
                # else:
                #     csv = None
                subjects = xnat.getSubjectsDataframe(self.project)
                msg = "Loaded %d subjects from %s : %s" % (
                    len(subjects), self.database, self.project)
                logging.info(msg)
                report = OPEXReport(subjects=subjects)
                report.xnat = xnat
                # Generate dataframes for display
                self.df_participants = report.getParticipants()
                logging.info('Participants loaded')
                #print self.df_participants
                self.df_report = report.printMissingExpts(self.project)
                self.df_report.sort_values(by=['MONTH', 'Subject'],
                                           inplace=True,
                                           ascending=False)
                logging.info("Missing experiments loaded")
                #print self.df_report.head()
                # Get expts
                self.df_expts = report.getExptCollection(
                    projectcode=self.project)
                logging.info("Experiment collection loaded")
                #print self.df_expts.head()

        except IOError:
            logging.error("Connection error - terminating app")
            print("Connection error - terminating app")
            sys.exit(1)
        finally:
            xnat.conn.disconnect()
Example #12
0
 def OnTest(self, e):
     """
     Test connection is correctly configured
     :param e:
     :return:
     """
     self.tcResults.Clear()
     (db, proj) = self.__loadConnection()
     xnat = XnatConnector(self.configfile, db)
     xnat.connect()
     msg = "Connection to %s for project=%s\n" % (db, proj)
     self.tcResults.AppendText(msg)
     if xnat.testconnection():
         msg = "CONNECTION SUCCESSFUL"
         if xnat.get_project(proj).exists():
             msg += " and Project exists"
         else:
             msg += " but Project DOES NOT exist"
     else:
         msg = "CONNECTION FAILED - please check config"
     self.tcResults.AppendText(msg)
Example #13
0
 def __init__(self, pathtofile, expt, *args):
     XnatConnector.super(*args)
     self.pathtofile = pathtofile
Example #14
0
        '--config',
        action='store',
        help='database configuration file (overrides ~/.xnat.cfg)')
    parser.add_argument('--filepath',
                        action='store',
                        help='Path to file containing expts to remove',
                        default="Q:\\DATA\\expts_removed.xlsx")
    parser.add_argument('--expt',
                        action='store',
                        help='Expt (sheet) containing expts to remove',
                        default="0")

    args = parser.parse_args()

    pathtofile = args.filepath
    xnat = XnatConnector(configfile, args.database)
    xnat.connect()

    print('Welcome to Xnat deleter: any shitty text-based adventure \n')
    print('It seems you have shitty data that you wish to remove')
    single = input('Removing (s)ingle or (b)atch of expts?')

    if single == 's':
        print(
            'So you wish to only remove one experiment? Put in your expt ID tag now'
        )
        expttag = input('Experiment ID: ')
        time.sleep(1)
        print(
            '\nNow before going ahead, are you sure you are not high on drugs?'
        )
Example #15
0
 def __init__(self, rootdir, xsd, *args):
     XnatConnector.super(*args)
     self.connect()
     self.rootdir = rootdir