def get_cmds(self, assessor, jobdir):
        """Method to generate the spider command for cluster job.

        :param assessor: pyxnat assessor object
        :param jobdir: jobdir where the job's output will be generated
        :return: command to execute the spider in the job script
        """
        assessor_label = assessor.label()
        proj_label = assessor.parent().parent().parent().label()
        subj_label = assessor.parent().parent().label()
        sess_label = assessor.parent().label()

        csess = XnatUtils.CachedImageSession(assessor._intf, proj_label,
                                             subj_label, sess_label)

        gad_cscans = XnatUtils.get_good_cscans(csess, self.gad_type)
        gad = self.get_file_path(gad_cscans[0], SCAN_RESOURCE)

        vessels_cscans = XnatUtils.get_good_cscans(csess, self.vessels_type)
        vessels = self.get_file_path(vessels_cscans, SCAN_RESOURCE)
        vessel_ids = [ves.info()['ID'] for ves in vessels_cscans]

        cmd = SPIDER_FORMAT.format(spider=self.spider_path,
                                   assr=assessor_label,
                                   dir=jobdir,
                                   exe=self.exe,
                                   gad=','.join(gad),
                                   vessels=','.join(vessels),
                                   vesselsids=','.join(vessel_ids),
                                   number_core=self.ppn,
                                   working_dir=self.working_dir)

        return [cmd]
 def finish(self):
     """Method to copy the results in dax.RESULTS_DIR."""
     out_dir = os.path.join(self.jobdir, 'outputs')
     # Organise the outputs:
     ala_dir = XnatUtils.makedir(os.path.join(out_dir, 'REG_ALA'))
     aff_dir = XnatUtils.makedir(os.path.join(out_dir, 'AFF'))
     reg_dir = XnatUtils.makedir(os.path.join(out_dir, 'REG_F3D'))
     cpp_dir = XnatUtils.makedir(os.path.join(out_dir, 'CPP'))
     # Copy files:
     for scan_id, res_dict in self.sources.items():
         for folder in ['REG_ALA', 'REG_F3D', 'AFF', 'CPP']:
             old_path = glob.glob(os.path.join(out_dir, scan_id,
                                               folder, '*'))[0]
             new_path = os.path.join(out_dir, folder)
             shutil.copy(old_path, new_path)
     # Zipping all the dicoms in the OSIRIX folder and keep the zip
     zip_osirix = os.path.join(out_dir, 'OSIRIX', 'osirix.zip')
     results_dict = {'PDF': self.pdf_final,
                     'REG_ALA': ala_dir,
                     'AFF': aff_dir,
                     'REG_F3D': reg_dir,
                     'CPP': cpp_dir,
                     'OSIRIX': zip_osirix}
     # Upload data:
     self.upload_dict(results_dict)
     self.end()
    def run(self):
        """Method running the process for the spider on the inputs data."""
        output_dir = XnatUtils.makedir(os.path.join(self.jobdir, 'outputs'))
        osirix_dir = XnatUtils.makedir(os.path.join(self.jobdir, 'OsiriX'))
        self.time_writer('Dicom folder: %s' % os.path.dirname(self.inputs[0]))
        mat_lines = DEFAULT_ADC_TEMPLATE.format(
                matlab_code=self.matlab_code,
                input_path=os.path.dirname(self.inputs[0]),
                output=output_dir,
                pdf_name=self.pdf_final)
        matlab_script = os.path.join(output_dir, 'run_matlab_verdict.m')
        with open(matlab_script, "w") as f:
            f.writelines(mat_lines)
        self.run_matlab(matlab_script, verbose=True)

        # Zip outputs:
        # dcm_files = glob.glob(os.path.join(output_dir, '*', '*.dcm'))
        dcm_files = get_dicom_list(output_dir)
        for dicom in dcm_files:
            shutil.copy(dicom, osirix_dir)

        self.time_writer('Zipping OsiriX resource ...')
        # Zip the DICOMs output:
        initdir = os.getcwd()
        # Zip all the files in the directory
        zip_name = os.path.join(osirix_dir, 'osirix.zip')
        os.chdir(osirix_dir)
        os.system('zip -r %s * > /dev/null' % zip_name)
        # return to the initial directory:
        os.chdir(initdir)
Esempio n. 4
0
    def run(self):
        """Method running the process for the spider on the inputs data."""
        matlabdir = os.path.join(self.jobdir, 'MATLAB/')
        if not os.path.exists(matlabdir):
            os.makedirs(matlabdir)
        outputdir = os.path.join(self.jobdir, 'Outputs/')
        if not os.path.exists(outputdir):
            os.makedirs(outputdir)

        matlab_script = os.path.join(matlabdir, 'callfMRIQA_v2.m')
        f = open(matlab_script, "w")
        try:
            lines = ['% Matlab Script to call vufMRIQAGUI function\n',
                     'addpath(genpath(\''+str(self.fmatlab)+'\'));\n',
                     'outputdir = \''+str(outputdir)+'\';\n',
                     'imgfile=\''+str(self.inputs[0])+'\';\n',
                     'spm_path=\''+str(self.spm)+'\';\n',
                     'project=\''+str(self.xnat_project)+'\';\n',
                     'subject=\''+str(self.xnat_subject)+'\';\n',
                     'session=\''+str(self.xnat_session)+'\';\n',
                     'scan=\''+str(self.xnat_scan)+'\';\n',
                     'fMRIQA_Pipeline_v2(imgfile,outputdir,spm_path,project,\
subject,session,scan);\n'
                     ]
            f.writelines(lines)
        finally:
            f.close()

        # Running Matlab script:
        XnatUtils.run_matlab(matlab_script, True)
        print '============================================================\n'
    def run(self):
        """Method running the process for the spider on the inputs data."""
        # Gzip files
        input_file = ''
        for filepath in self.inputs:
            if filepath.endswith('.nii.gz'):
                os.system("gzip -f -d %s" % (filepath))
                input_file = filepath[:-3]

        self.input_file = input_file
        folder = os.path.join(self.jobdir, 'Sample_GM_Segment')

        mat_lines = MAT_TEMPLATE.format(
            matlab_code=self.matlab_code,
            input_file=input_file,
            spm12=self.spm12
        )

        matlab_script = os.path.join(folder, 'run_sample_GM.m')
        with open(matlab_script, "w") as _fobj:
            _fobj.writelines(mat_lines)
        XnatUtils.run_matlab(matlab_script, verbose=True)

        # Make report:
        self.make_pdf()

        # Gzip nii:
        XnatUtils.gzip_nii(folder)
Esempio n. 6
0
    def download(self, obj_label, resource, folder):
        """
        Return a python list of the files downloaded for the resource on the scan
            example:
              download(scan_id, "DICOM", "/Users/test")
             or
              download(assessor_label, "DATA", "/Users/test")

        :param obj_label: xnat object label (scan ID or assessor label)
        :param resource: folder name under the xnat object
        :param folder: download directory
        :return: python list of files downloaded

        """
        # Open connection to XNAT
        xnat = XnatUtils.get_interface(host=self.host,
                                       user=self.user,
                                       pwd=self.pwd)
        resource_obj = self.select_obj(intf=xnat,
                                       obj_label=obj_label,
                                       resource=resource)
        list_files = XnatUtils.download_files_from_obj(
            directory=folder, resource_obj=resource_obj)
        # close connection
        xnat.disconnect()
        return list_files
    def has_inputs(self, csess):
        """Method overridden from base class.

        By definition:
            status = 0  -> NEED_INPUTS,
            status = 1  -> NEED_TO_RUN
            status = -1 -> NO_DATA
            qcstatus needs a value only when -1 or 0.
        You need to set qcstatus to a short string that explain
        why it's no ready to run. e.g: No NIFTI

        :param csess: object csess define in dax.XnatUtils
                      (see XnatUtils in dax for information)
        :return: status, qcstatus
        """
        gad_cscans = XnatUtils.get_good_cscans(csess, self.gad_type)
        if not gad_cscans:
            LOGGER.debug('Vessel Registration: No GAD scan found.')
            return -1, 'No GAD found'
        elif len(gad_cscans) > 1:
            LOGGER.debug('Vessel Registration: Too many GAD scans found.')
            return 0, 'Too many GAD found'

        vessels_cscans = XnatUtils.get_good_cscans(csess, self.vessels_type)
        if not vessels_cscans:
            LOGGER.debug('Vessel Registration: No Vessels scans found.')
            return -1, 'No Vessels found'

        return 1, None
    def generate_big_nifti(self):
        """Generate big nifti with all VERDICT acquisition files."""
        for i in range(1, len(self.acquisitions.keys()) + 1):
            f_img = nib.load(self.acquisitions[i][0]['4D'])
            f_img_data = f_img.get_data()
            data = np.zeros(
                    shape=(f_img_data.shape[0],
                           f_img_data.shape[1],
                           f_img_data.shape[2],
                           f_img_data.shape[3]*len(self.acquisitions[i])))

            sorted_list = sorted(self.acquisitions[i],
                                 key=lambda k: int(k['ID']))
            for index, scan_info in enumerate(sorted_list):
                # Open niftis with nibabel
                f_img = nib.load(scan_info['reg'])
                f_img_data = f_img.get_data()

                for vol in range(0, f_img_data.shape[3]):
                    # Draw
                    vol_index = index*f_img_data.shape[3] + vol
                    data[:, :, :, vol_index] = f_img_data[:, :, :, vol]

            nii_5d = nib.Nifti1Image(data, affine=f_img.affine)
            acq_dir = XnatUtils.makedir(os.path.join(self.jobdir,
                                                     'outputs',
                                                     'ACQ%d' % i))
            filename = '%s_acquisition%d.nii' % (self.xnat_session, i)
            nii_file = os.path.join(acq_dir, filename)
            nib.save(nii_5d, nii_file)
            # gzip the nifti:
            XnatUtils.gzip_file(nii_file)
    def get_cmds(self, assessor, jobdir):
        """Method to generate the spider command for cluster job.

        :param assessor: pyxnat assessor object
        :param jobdir: jobdir where the job's output will be generated
        :return: command to execute the spider in the job script
        """
        proj_label = assessor.parent().parent().parent().label()
        subj_label = assessor.parent().parent().label()
        sess_label = assessor.parent().label()

        csess = XnatUtils.CachedImageSession(assessor._intf, proj_label,
                                             subj_label, sess_label)
        target_cscans = XnatUtils.get_good_cscans(csess, self.target_type)
        target_id = target_cscans[0].info()['ID']
        source_cscans = XnatUtils.get_good_cscans(csess, self.sources_type)
        sources_id = ','.join([sc.info()['ID'] for sc in source_cscans])

        cmd = SPIDER_FORMAT.format(spider=self.spider_path,
                                   proj=proj_label,
                                   subj=subj_label,
                                   sess=sess_label,
                                   dir=jobdir,
                                   target=target_id,
                                   sources=sources_id,
                                   regaladin=self.regaladin,
                                   number_core=self.ppn,
                                   suffix_proc=self.suffix_proc)

        return [cmd]
    def get_cmds(self, assessor, jobdir):
        """Method to generate the spider command for cluster job.

        :param assessor: pyxnat assessor object
        :param jobdir: jobdir where the job's output will be generated
        :return: command to execute the spider in the job script
        """
        proj_label = assessor.parent().parent().parent().label()
        subj_label = assessor.parent().parent().label()
        sess_label = assessor.parent().label()
        assr_label = assessor.label()

        csess = XnatUtils.CachedImageSession(assessor._intf, proj_label,
                                             subj_label, sess_label)
        dti_cscans = XnatUtils.get_good_cscans(csess, self.dtitypes)
        dtis = ','.join([cscan.info()['ID'] for cscan in dti_cscans])
        gif_cassrs = XnatUtils.get_good_cassr(csess, self.giftypes)
        working_dir = os.path.join(self.working_dir, assr_label)

        cmd = SPIDER_FORMAT.format(spider=self.spider_path,
                                   proj=proj_label,
                                   subj=subj_label,
                                   sess=sess_label,
                                   dir=jobdir,
                                   suffix_proc=self.suffix_proc,
                                   dti=dtis,
                                   gif=gif_cassrs[0].info()['label'],
                                   exe=self.exe,
                                   gif_path=self.exe_args,
                                   number_core=self.ppn,
                                   working_dir=working_dir)

        return [cmd]
    def get_cmds(self, assessor, jobdir):
        """Method to generate the spider command for cluster job.

        :param assessor: pyxnat assessor object
        :param jobdir: jobdir where the job's output will be generated
        :return: command to execute the spider in the job script
        """
        proj_label = assessor.parent().parent().parent().label()
        subj_label = assessor.parent().parent().label()
        sess_label = assessor.parent().label()

        nb_acq = 1
        csess = XnatUtils.CachedImageSession(assessor._intf, proj_label,
                                             subj_label, sess_label)
        reg_verdict = ''
        for cassr in csess.assessors():
            if XnatUtils.is_cassessor_good_type(cassr, [self.proctype]):
                reg_verdict = cassr

        if XnatUtils.has_resource(reg_verdict, 'ACQ2'):
            nb_acq = 2

        cmd = SPIDER_FORMAT.format(spider=self.spider_path,
                                   proj=proj_label,
                                   subj=subj_label,
                                   sess=sess_label,
                                   dir=jobdir,
                                   nb_acq=nb_acq,
                                   proctype=self.proctype,
                                   mc=self.mc,
                                   camino=self.camino,
                                   scheme=self.scheme_file,
                                   suffix_proc=self.suffix_proc)

        return [cmd]
    def has_inputs(self, csess):
        """Method overridden from base class.

        By definition:
            status = 0  -> NEED_INPUTS,
            status = 1  -> NEED_TO_RUN
            status = -1 -> NO_DATA
            qcstatus needs a value only when -1 or 0.
        You need to set qcstatus to a short string that explain
        why it's no ready to run. e.g: No NIFTI

        :param csess: object csess define in dax.XnatUtils
                      (see XnatUtils in dax for information)
        :return: status, qcstatus
        """
        # Check that there is only one scan usable with the reference type and
        # that the reference file as a NIFTI
        verdict_cscans = XnatUtils.get_good_cscans(csess, self.modalities)
        if not verdict_cscans:
            LOGGER.debug('Processor_Registration_Verdict: \
        cannot run at all, no VERDICT image found')
            return -1, 'VERDICT not found'
        for cscan in verdict_cscans:
            if not XnatUtils.has_resource(cscan, 'NIFTI'):
                LOGGER.debug('Processor_Registration_Verdict: \
        cannot run, no NIFTI found for %s scan', cscan.info()['ID'])
                return 0, "Missing NIFTI"

        return 1, None
    def make_pdf(self):
        """Method to make the PDF for the spider.

        :return: None
        """
        output_folder = os.path.join(self.jobdir, 'outputs')
        pdfs_dir = XnatUtils.makedir(os.path.join(output_folder, 'pdfs'))
        fpages = list()
        # Run matlab function
        for nb_acq in range(1, self.nb_acquisition + 1):
            pdf_page = os.path.join(output_folder, str(nb_acq),
                                    'VerdictMapAcq%d.pdf' % nb_acq)
            mat_lines = DEFAULT_PDF_MAKER.format(
                matlab_code=self.matlab_code,
                maps_folder=os.path.join(output_folder, str(nb_acq),
                                         'AMICO', self.model),
                subject=self.xnat_subject,
                output_folder=pdfs_dir,
                acq=nb_acq)
            matlab_script = os.path.join(output_folder,
                                         'run_pdf_page_%d.m' % nb_acq)
            with open(matlab_script, "w") as f:
                f.writelines(mat_lines)
            XnatUtils.run_matlab(matlab_script, verbose=True)
            # Get all PDFs:
            pdf_pages = XnatUtils.find_files(pdfs_dir, '.pdf')
            # Merge all pdfs into one:
            self.merge_pdf_pages(pdf_pages, pdf_page)
            fpages.append(pdf_page)

        if len(fpages) > 1:
            self.merge_pdf_pages(fpages, self.pdf_final)
        else:
            shutil.move(fpages[0], self.pdf_final)
Esempio n. 14
0
    def define_spider_process_handler(self):
        """
        Define the SpiderProcessHandler for the end of spider
         using the init attributes about XNAT
        :return: None
        """
        # Create the SpiderProcessHandler if first time upload
        if self.datatype == 'scan':
            self.spider_handler = XnatUtils.SpiderProcessHandler(
                self.spider_path,
                self.suffix,
                self.xnat_project,
                self.xnat_subject,
                self.xnat_session,
                self.xnat_scan,
                time_writer=self.time_writer)

        else:
            self.spider_handler = XnatUtils.SpiderProcessHandler(
                self.spider_path,
                self.suffix,
                self.xnat_project,
                self.xnat_subject,
                self.xnat_session,
                time_writer=self.time_writer)
Esempio n. 15
0
 def test_assessor_out_resources(self):
     with XnatUtils.get_interface(host=host) as intf:
         cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id,
                                               sess_id)
         for asr in cisess.assessors():
             asrobj = intf.select_assessor(proj_id, subj_id, sess_id,
                                           asr.label())
             print((asrobj.out_resources()))
    def run(self):
        """Method running the process for the spider on the inputs data."""
        output_folder = XnatUtils.makedir(os.path.join(self.jobdir, 'outputs'))
        osirix_folder = XnatUtils.makedir(os.path.join(output_folder,
                                                       'OsiriX'))
        for nb_acq in range(1, self.nb_acquisition+1):
            folder = os.path.join(output_folder, str(nb_acq))
            os.makedirs(folder)
            mat_lines = DEFAULT_VERDICT_TEMPLATE.format(
                    matlab_code=self.matlab_code,
                    input_path=os.path.dirname(self.inputs[nb_acq]),
                    subject=self.xnat_subject,
                    filename=os.path.basename(self.inputs[nb_acq]),
                    output=folder,
                    project=self.xnat_project,
                    camino=self.camino,
                    spams=self.spams,
                    scheme_filename=self.scheme_filename,
                    model=self.model)
            matlab_script = os.path.join(output_folder,
                                         'run_verdict_map%d.m' % nb_acq)
            with open(matlab_script, "w") as f:
                f.writelines(mat_lines)
            self.run_matlab(matlab_script, verbose=True)

            # Generate Dicom for OsiriX
            outdir = os.path.join(output_folder, str(nb_acq), 'AMICO',
                                  self.model)
            # Load dicom headers
            if not os.path.isfile(self.inputs['dcm']):
                err = "DICOM File %s not found."
                raise Exception(err % self.inputs['dcm'])
            sour_obj = dicom.read_file(self.inputs['dcm'])

            # Convert all niftis to dicoms
            convert_niftis_2_dicoms(
                outdir,
                sour_obj,
                osirix_folder,
                nb_acq)

            # Subtract the Cobj to the maps:
            subtract_obj_to_map(outdir, sour_obj, osirix_folder, nb_acq)

        # Make pdf:
        self.make_pdf()

        # Zip the DICOMs output:
        initdir = os.getcwd()
        # Zip all the files in the directory
        zip_name = os.path.join(self.jobdir, 'outputs', 'OsiriX', 'osirix.zip')
        os.chdir(os.path.join(self.jobdir, 'outputs', 'OsiriX'))
        os.system('zip -r %s * > /dev/null' % zip_name)
        # return to the initial directory:
        os.chdir(initdir)

        # Gzip nii:
        XnatUtils.gzip_nii(outdir)
    def run(self):
        """Method running the process for the spider on the inputs data."""
        output_folder = XnatUtils.makedir(os.path.join(self.jobdir, 'outputs'))
        osirix_folder = XnatUtils.makedir(os.path.join(output_folder,
                                                       'OsiriX'))
        for nb_acq in range(1, self.nb_acquisition+1):
            folder = os.path.join(output_folder, str(nb_acq))
            os.makedirs(folder)
            mat_lines = DEFAULT_VERDICT_TEMPLATE.format(
                    matlab_code=self.matlab_code,
                    input_path=self.inputs[nb_acq],
                    output=folder,
                    scheme_filename=self.scheme_filename,
                    camino=self.camino)
            matlab_script = os.path.join(output_folder,
                                         'run_matlab_adc_%d.m' % nb_acq)
            with open(matlab_script, "w") as f:
                f.writelines(mat_lines)
            self.run_matlab(matlab_script, verbose=True)

            # Generate Dicom for OsiriX
            res_nii = os.path.join(folder, 'FIT_ADC.nii')
            out_nii = os.path.join(folder,
                                   '%s_FIT_ADC_%d.nii' % (self.xnat_session,
                                                          nb_acq))
            shutil.move(res_nii, out_nii)

            # Load dicom headers
            if not os.path.isfile(self.inputs['dcm']):
                err = "DICOM File %s not found."
                raise Exception(err % self.inputs['dcm'])
            sour_obj = dicom.read_file(self.inputs['dcm'])

            # Convert all niftis to dicoms
            convert_nifti_2_dicoms(
                out_nii,
                sour_obj,
                osirix_folder,
                nb_acq,
                self.xnat_project,
                self.xnat_subject,
                self.xnat_session)

            # Gzip nii:
            XnatUtils.gzip_nii(folder)

        # Make pdf:
        self.make_pdf()

        # Zip the DICOMs output:
        initdir = os.getcwd()
        # Zip all the files in the directory
        zip_name = os.path.join(self.jobdir, 'outputs', 'OsiriX', 'osirix.zip')
        os.chdir(os.path.join(self.jobdir, 'outputs', 'OsiriX'))
        os.system('zip -r %s * > /dev/null' % zip_name)
        # return to the initial directory:
        os.chdir(initdir)
Esempio n. 18
0
    def run_matlab(self, mat_template, filename):
        filepath = os.path.join(self.script_dir, filename)
        template = Template(mat_template)
        
        # Write the script
        with open(filepath, 'w') as f:
            f.write(template.substitute(self.run_inputs))

        # Run the script
        XnatUtils.run_matlab(filepath, verbose=True)
Esempio n. 19
0
    def run_matlab(self, mat_template, filename):
        filepath = os.path.join(self.script_dir, filename)
        template = Template(mat_template)

        # Write the script
        with open(filepath, 'w') as f:
            f.write(template.substitute(self.run_inputs))

        # Run the script
        XnatUtils.run_matlab(filepath, verbose=True)
Esempio n. 20
0
    def test_xnat_parse_session(self):
        with XnatUtils.get_interface(host=host) as intf:
            intf.connect()

            SanityChecks.__prep_project(intf)

            yamldoc = YamlDoc().from_string(yamls.proc_a)
            csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id,
                                                 sess_id)

            ap = AutoProcessor(XnatUtils, yamldoc)
            ap.parse_session(ap)
Esempio n. 21
0
def generate_bmask_whole(assessor_obj, assessor, tmpdir):
    """ Generating the bmask whole from the assessor outputs."""
    jobsdir = os.path.join(tmpdir, assessor['label'])
    if not os.path.exists(jobsdir):
        os.makedirs(jobsdir)
    else:
        shutil.rmtree(jobsdir)
        os.makedirs(jobsdir)

    files = XnatUtils.download_files_from_obj(jobsdir,
                                              assessor_obj.resource('LABELS'))
    if len(files) != 1:
        raise Exception('Too many niftis downloaded.')
    else:
        input_nii = files[0]
    tiv_files = XnatUtils.download_files_from_obj(jobsdir,
                                                  assessor_obj.resource('TIV'))

    basepath = os.path.dirname(input_nii)
    basename = os.path.basename(input_nii[:-7])
    final_seg_mats_sub_cmd = os.path.join(
                    basepath, "%s_ArtConstruction_0.nii.gz " % basename)
    for index, value in enumerate(BM_ARRAY):
        output_nii = os.path.join(
                basepath, "%s_ArtConstruction_%d.nii.gz " % (basename, index))
        val_min = float(value - 0.5)
        val_max = float(value + 0.5)
        cmd = '%s %s -thr %f -uthr %f -bin %s' % (ARGS.seg_maths_exe,
                                                  input_nii, val_min, val_max,
                                                  output_nii)
        print cmd
        os.system(cmd)
        final_seg_mats_sub_cmd += "-add %s " % output_nii

    # Seg maths add
    to_remove_nii = "%s_ToRemove.nii.gz" % basepath
    cmd = '%s %s -bin %s' % (ARGS.seg_maths_exe, final_seg_mats_sub_cmd,
                             to_remove_nii)
    print cmd
    os.system(cmd)
    # Seg maths remove
    final_brain = "%s_FinalBrain.nii.gz" % basepath
    cmd = '%s %s -sub %s -bin %s' % (ARGS.seg_maths_exe,
                                     ' '.join(tiv_files),
                                     to_remove_nii,
                                     final_brain)
    print cmd
    os.system(cmd)
    return final_brain
    def needs_run(self, cscan, xnat):
        """ needs_run function overridden from base-class
                cscan = CacheScan object from XnatUtils
            return True or False
        """
        scan_info = cscan.info()
        if XnatUtils.has_resource(cscan, 'SNAPSHOTS'):
            LOGGER.debug('Has snapshot')
            return False

        if not XnatUtils.has_resource(cscan, self.resourcename):
            LOGGER.warn('Preview NIFTI -- '+scan_info['scan_id']+' -- no '+self.resourcename+' resource')
            return False

        return True
    def register_nifti(self, source_info, target_info, output_folder,
                       acquisition_number):
        """Register the nifti source to the target.

        :param source_info: dictionary information of source image
        :param target_info: dictionary information of target image
        :param output_folder: path to the output folder
        :param acquisition_number: index of the acquisition
        :return: path to the 4D nifti register
        """
        # Variables:
        ala_dir = XnatUtils.makedir(os.path.join(output_folder, 'REG_ALA'))
        reg_dir = XnatUtils.makedir(os.path.join(output_folder, 'REG_RES'))
        b1tob0 = os.path.join(ala_dir, 'b1tob0.txt')
        volume_niis = {0: source_info['3D'][0]}
        # Step 2:
        # Register each scan b0 to the previous one
        # (e.g: b3000 <- b2000)
        cmd = REG_ALADIN_CMD.format(exe_path=self.reg_aladin_exe,
                                    ref=target_info['3D'][0],
                                    flo=source_info['3D'][0],
                                    res=source_info['3D'][0],
                                    aff=b1tob0,
                                    args=self.args_reg_aladin)
        self.run_system_cmd(cmd)

        # Step 3:
        # Propagate the transformation to the rest of the volume:
        for index, volume in enumerate(source_info['3D']):
            if index != 0:
                vol_nii = os.path.join(reg_dir, 'volume_%s_reg.nii' % index)
                cmd = REG_ALADIN_CMD.format(exe_path=self.reg_resample_exe,
                                            ref=target_info['3D'][0],
                                            flo=volume,
                                            res=vol_nii,
                                            aff=b1tob0,
                                            args=self.args_reg_resample)
                self.run_system_cmd(cmd)
                volume_niis[index] = vol_nii

        # Step 4:
        # Put back the nifti together as one volume
        final_nii = os.path.join(output_folder, '%s_%s_%d_reg.nii'
                                                % (source_info['ID'],
                                                   source_info['type'],
                                                   acquisition_number))
        join_nifti_3Ds_4D(volume_niis, final_nii)
        return final_nii
Esempio n. 24
0
 def test_scan_full_object(self):
     with SanityChecks.__get_connection() as intf:
         scanobj1 = intf.select_scan(proj_id, subj_id, sess_id, scan_id)
         csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id)
         cscan = filter(lambda x: x.label() == scan_id, csess.scans())[0]
         scanobj2 = cscan.full_object()
         self.assertEqual(scanobj1.label(), scanobj2.label())
Esempio n. 25
0
def main():
    """ Main function."""
    if ENV_SOURCE is not None and os.path.isfile(ENV_SOURCE):
        os.system('sh {}'.format(ENV_SOURCE))

    _working_dir = None
    if WORKING_DIR != 'None':
        _working_dir = os.path.join(WORKING_DIR, '${assessor_label}')
        if not os.path.exists(_working_dir):
            os.makedirs(_working_dir)

    if XnatUtils.executable_exists(NIFTYPIPE_EXE):
        _omp = ''
        _wd = ''
        if OPENMP_CORE is not None and OPENMP_CORE != 'None':
            _omp = OMP.format(number_core=OPENMP_CORE)
        if _working_dir is not None:
            _wd = WDIR.format(working_dir=_working_dir)

        cmd = EXE_CMD.format(
            dwis=' '.join(DWIS.split(',')),
            bvals=' '.join(BVALS.split(',')),
            bvecs=' '.join(BVECS.split(',')),
            omp=_omp, wdir=_wd)
        os.system(cmd)
        make_pdf()
    else:
        raise Exception("Error: executable %s not found" % (NIFTYPIPE_EXE))
Esempio n. 26
0
 def copy_inputs(self):
     self.run_inputs = self.src_inputs
 
     for _input in self.copy_list:
         src = self.src_inputs[_input]
         if src.startswith('xnat://'):
             src = src[len('xnat:/'):]
             _res, _file = src.split('/files/')
             dst = os.path.join(self.input_dir, _file)
             print('DEBUG:downloading from XNAT:'+src+' to '+dst)
             try:
                 xnat = XnatUtils.get_interface(self.host, self.user, self.pwd)
                 res = xnat.select(_res)
                 result = res.file(_file).get(dst)
             except:
                 print('ERROR:downloading from XNAT')
                 return
         else:
             dst = os.path.join(self.input_dir, os.path.basename(src))
             print('DEBUG:copying src:'+src+' to '+dst)
             copyfile(src, dst)
         
         self.run_inputs[_input] = dst
         
     return self.run_inputs
Esempio n. 27
0
    def needs_run(self, cscan, xnat):
        """needs_run function overridden from base-class.

        cscan = CacheScan object from XnatUtils
        return True or False
        """
        # Check output
        if XnatUtils.has_resource(cscan, 'NIFTI'):
            LOGGER.debug('Has NIFTI')
            return False
        # Check input
        if not XnatUtils.has_resource(cscan, 'DICOM'):
            LOGGER.debug('no DICOM resource')
            return False

        return True
    def prerun(self, settings_filename=''):
        """ prerun function overridden from base-class """
        LOGGER.info('loading XNAT...')
        self.xnat = XnatUtils.get_interface()

        LOGGER.info('loading REDCap...')
        self.load_redcap()

        LOGGER.info('checking Projects of Prearchive Scans..')
        self.load_prearchive()
        self.check_projects()

        LOGGER.info('do archiving...')
        self.load_prearchive()
        self.do_archiving()

        LOGGER.info('crosscheck redcap...')
        self.crosscheck_redcap()

        self.xnat.disconnect()

        if self.send_an_email:
            LOGGER.info('sending email with report of errors')
            self.send_report()

        LOGGER.info('DONE')
    def __init__(self, spider_path, jobdir, scans_id,
                 xnat_project, xnat_subject, xnat_session,
                 reg_aladin_exe='reg_aladin', reg_resample_exe='reg_resample',
                 args_reg_aladin=DEFAULT_ARGS_REG_ALADIN,
                 args_reg_resample=DEFAULT_ARGS_REG_RESAMPLE,
                 xnat_host=None, xnat_user=None, xnat_pass=None, suffix=""):
        """Entry point for Spider_Registration_Prostate Class."""
        super(Spider_Registration_Verdict, self).__init__(spider_path, jobdir,
                                                          xnat_project,
                                                          xnat_subject,
                                                          xnat_session,
                                                          xnat_host,
                                                          xnat_user,
                                                          xnat_pass, suffix)
        # Inputs
        self.acquisitions = dict()
        self.dicom = ''
        self.scans_id = XnatUtils.get_input_list(scans_id, list())

        # Outputs
        self.pdf_final = os.path.join(self.jobdir, 'Registration_VERDICT.pdf')

        # Check Executable:
        self.reg_aladin_exe = self.check_executable(reg_aladin_exe,
                                                    'reg_aladin')
        self.reg_resample_exe = self.check_executable(reg_resample_exe,
                                                      'reg_resample')
        self.args_reg_aladin = args_reg_aladin
        self.args_reg_resample = args_reg_resample
Esempio n. 30
0
    def prerun(self, settings_filename=''):
        """ prerun function overridden from base-class """
        LOGGER.info('loading XNAT...')
        self.xnat = XnatUtils.get_interface()

        LOGGER.info('loading REDCap...')
        self.load_redcap()

        LOGGER.info('checking Projects of Prearchive Session..')
        self.load_prearchive()
        moving = self.check_projects()
        if moving:  # wait for moves to complete
            LOGGER.info('sessions moving projects, waiting '+str(MOVING_SLEEP_SECS)+' secs')
            time.sleep(MOVING_SLEEP_SECS)
        else:
            LOGGER.info('no sessions changed, continuing to archiving')

        LOGGER.info('do archiving...')
        self.load_prearchive()
        self.do_archiving()

        LOGGER.info('crosscheck redcap...')
        self.crosscheck_redcap()

        self.xnat.disconnect()

        if self.send_an_email:
            LOGGER.info('sending email with report of errors')
            self.send_report()

        LOGGER.info('DONE')
    def run(self, scan_info, scan_obj):
        """ main function to run on scan """
        if not len(scan_obj.resource(self.resourcename).files().get()) > 0:
            LOGGER.warn('Preview NIFTI -- '+scan_info['scan_id']+' -- No file(s) for '+self.resourcename)
        else:
            LOGGER.debug("downloading "+self.resourcename+"...")

            fpath = XnatUtils.download_biggest_file_from_obj(self.directory, scan_obj.resource(self.resourcename))

            if not os.path.exists(fpath):
                LOGGER.warn('Preview NIFTI -- '+scan_info['scan_id']+' -- '+self.resourcename+' file size is zero.')
            else:
                sm_gif = str(os.path.join(self.directory, os.path.splitext(os.path.basename(fpath))[0] + "_sm.gif"))
                lg_gif = str(os.path.join(self.directory, os.path.splitext(os.path.basename(fpath))[0] + "_lg.gif"))

                # Generate preview from the nifti file
                self.generate_preview(fpath, sm_gif, lg_gif)

                if os.path.isfile(sm_gif):
                    LOGGER.debug('GIF Made / upload ...')
                    scan_obj.resource('SNAPSHOTS').file('snap_t.gif').put(sm_gif, 'GIF', 'THUMBNAIL')
                    scan_obj.resource('SNAPSHOTS').file('snap.gif').put(lg_gif, 'GIF', 'ORIGINAL')
                else:
                    LOGGER.error('Preview NIFTI -- '+scan_info['scan_id']+' -- GIF FAILED')
                    self.log_warning_error('GIF failed for NIFTI', scan_info, error=True)

                self.clean_directory()
Esempio n. 32
0
    def test_setup_old_assessors(self):
        intf = XnatUtils.get_interface(host)
        proj_id = 'proj1'
        subj_id = 'subj1'
        sess_ids = ['sess2', 'sess1']

        project = intf.select_project(proj_id)
        if not project.exists():
            self.assertTrue(False, 'proj1 should be pre-created for this test')

        subject = intf.select_subject(proj_id, subj_id)
        if not subject.exists():
            self.assertTrue(False, 'subj1 should be pre-created for this test')

        for s in sess_ids:
            session = intf.select_experiment(proj_id, subj_id, s)
            if not session.exists():
                self.assertTrue(False, 'sess1 should be pre-created for this test')

            # delete and recreate scans
            scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair')]
            ComponentTestBuild._setup_scans(session, scan_descriptors)

            # delete and recreate old assessors
            ComponentTestBuild._setup_assessors(proj_id, subj_id, session)
Esempio n. 33
0
def main():
    """ Main function."""
    _working_dir = None
    if WORKING_DIR != 'None':
        _working_dir = os.path.join(WORKING_DIR, '${assessor_label}')
        if not os.path.exists(_working_dir):
            os.makedirs(_working_dir)

    if XnatUtils.executable_exists(NIFTYPIPE_EXE):
        _omp = ''
        _wd = ''
        if OPENMP_CORE is not None and OPENMP_CORE != 'None':
            _omp = OMP.format(number_core=OPENMP_CORE)
        if _working_dir is not None:
            _wd = WDIR.format(working_dir=_working_dir)

        cmd = EXE_CMD.format(exe_path=NIFTYPIPE_EXE,
                             gad=GAD_FILE,
                             mprage=MPRAGE_FILE,
                             labels=LABELS_FILE,
                             brain=BRAIN_FILE,
                             output=JOB_DIR,
                             omp=_omp,
                             wdir=_wd)
        os.system(cmd)
        rename_outputs()
        make_pdf()
    else:
        raise Exception("Error: executable %s not found" % (NIFTYPIPE_EXE))
Esempio n. 34
0
 def test_assessor_full_object(self):
     with SanityChecks.__get_connection() as intf:
         assrobj1 = intf.select_assessor(proj_id, subj_id, sess_id, assr_id)
         csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id)
         cassr = filter(lambda x: x.label() == assr_id, csess.assessors())[0]
         assrobj2 = cassr.full_object()
         self.assertEqual(assrobj1.label(), assrobj2.label())
Esempio n. 35
0
def main():
    """ Main function."""
    assr_handler = XnatUtils.AssessorHandler(ASSESSOR)
    session = assr_handler.get_session_label()

    moda = 'T1 FLAIR'
    if T2_FILE:
        moda = 'T1 FLAIR T2'

    _working_dir = None
    if WORKING_DIR != 'None':
        _working_dir = os.path.join(WORKING_DIR, ASSESSOR)
        if not os.path.exists(_working_dir):
            os.makedirs(_working_dir)

    if XnatUtils.executable_exists(NIFTYPIPE_EXE):
        _omp = ''
        _wd = ''
        _t2 = ''
        if OPENMP_CORE is not None and OPENMP_CORE != 'None':
            _omp = OMP.format(number_core=OPENMP_CORE)
        if _working_dir is not None:
            _wd = WDIR.format(working_dir=_working_dir)
        if T2_FILE is not None and T2_FILE != 'None':
            _t2 = T2.format(working_dir=T2_FILE)

        cmd = EXE_CMD.format(
            exe=NIFTYPIPE_EXE, modalities=moda, t2=_t2, omp=_omp, wdir=_wd)
        os.system(cmd)

        # PDF
        make_pdf(session)
    else:
        raise Exception("Error: executable %s not found" % (NIFTYPIPE_EXE))
Esempio n. 36
0
    def test_setup_old_assessors(self):
        intf = XnatUtils.get_interface(host)
        proj_id = 'proj1'
        subj_id = 'subj1'
        sess_ids = ['sess2', 'sess1']

        project = intf.select_project(proj_id)
        if not project.exists():
            self.assertTrue(False, 'proj1 should be pre-created for this test')

        subject = intf.select_subject(proj_id, subj_id)
        if not subject.exists():
            self.assertTrue(False, 'subj1 should be pre-created for this test')

        for s in sess_ids:
            session = intf.select_experiment(proj_id, subj_id, s)
            if not session.exists():
                self.assertTrue(False,
                                'sess1 should be pre-created for this test')

            # delete and recreate scans
            scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair')]
            ComponentTestBuild._setup_scans(session, scan_descriptors)

            # delete and recreate old assessors
            ComponentTestBuild._setup_assessors(proj_id, subj_id, session)
Esempio n. 37
0
def main():
    """ Main function."""
    _working_dir = None
    if WORKING_DIR != 'None':
        _working_dir = os.path.join(WORKING_DIR, '${assessor_label}')
        if not os.path.exists(_working_dir):
            os.makedirs(_working_dir)

    if XnatUtils.executable_exists(NIFTYPIPE_EXE):
        _omp = '' if OPENMP_CORE == 'None' \
               else OMP.format(number_core=OPENMP_CORE)
        _wd = '' if _working_dir is not None \
              else '--working_dir {0}'.format(_working_dir)
        _min = '' if MIN == 'None' else '--min {0}'.format(MIN)
        _max = '' if MAX == 'None' else '--max {0}'.format(MAX)

        cmd = EXE_CMD.format(exe_path=NIFTYPIPE_EXE,
                             vessels=" ".join(VESSELS_FILES.split(',')),
                             output=JOB_DIR,
                             omp=_omp,
                             wdir=_wd,
                             min=_min,
                             max=_max,
                             ct='--ct' if CT == '1' else '')
        os.system(cmd)
        make_pdf()
    else:
        raise Exception("Error: executable %s not found" % (NIFTYPIPE_EXE))
Esempio n. 38
0
def main():
    """ Main function."""
    if ENV_SOURCE is not None and ENV_SOURCE != 'None' \
       and os.path.isfile(ENV_SOURCE):
        os.system('sh {}'.format(ENV_SOURCE))

    _working_dir = None
    if WORKING_DIR != 'None':
        _working_dir = os.path.join(WORKING_DIR, '${assessor_label}')
        if not os.path.exists(_working_dir):
            os.makedirs(_working_dir)

    if os.path.exists(NIFTYPIPE_EXE) or \
       XnatUtils.executable_exists(NIFTYPIPE_EXE):
        if OPENMP_CORE is not None and OPENMP_CORE != 'None':
            _omp = OMP.format(number_core=OPENMP_CORE)
        if _working_dir is not None:
            _wd = WDIR.format(working_dir=_working_dir)
        cmd = EXE_CMD.format(exe=NIFTYPIPE_EXE,
                             input=IN_FILE,
                             output=JOB_DIR,
                             db_xml=DB_TEMPLATE,
                             omp=_omp, wdir=_wd)
        os.system(cmd)
        make_pdf()
    else:
        raise Exception("Error: %s not found" % (NIFTYPIPE_EXE))
Esempio n. 39
0
def generate_4D_nifti(folder_path):
    """
        Generate a 4D nifti from 3D nifti

        :param folder_path: folder containing the .nii files
    """
    nii_dict = organise_niftis(fpath)
    print "Converting 3D niftis into 4D nifti for folder %s ." % folder_path
    matlab_script = os.path.join(folder_path, 'convert3Din4D.m')
    f = open(matlab_script, "w")
    try:
        prefix_str = "{"
        for key in nii_dict.keys():
            prefix_str += "'%s'," % key
        prefix_str = prefix_str[:-1] + "}"
        lines = [ "% Matlab Script to call vufMRIQAGUI function\n",
                  "addpath(genpath('/Users/byvernault/home-local/masimatlab/trunk/xnatspiders/matlab/ext'));\n",
                  "addpath('/Users/byvernault/Documents/MATLAB');\n",
                  "folder_niis = '%s';\n" % folder_path,
                  "prefix = %s;\n" % prefix_str,
                  "dti = %s;\n" % (1 if ARGS.onedti else 0),
                  "untouch = %s;\n" % (1 if ARGS.untouch else 0),
                  "convert3Dto4DNII(folder_niis, prefix, dti, untouch);\n"]
        f.writelines(lines)
    finally:
        f.close()

    for key, nii_list in nii_dict.items():
        filename = os.path.join(folder_path, "%s_4D.nii.gz" % key)
        if os.path.isfile(filename):
            print"INFO: NIFTI already created. Skipping ... "
            return

    #Running Matlab script:
    XnatUtils.run_matlab(matlab_script, True)

    for key, nii_list in nii_dict.items():
        filename = os.path.join(folder_path, "%s_4D.nii" % key)
        if not os.path.isfile(filename):
            print"ERROR: NO NIFTI CREATED ... "
        else:
            # copying with fsl the header:
            print "Copying geometry from 3D nifti and gzipping the nifti for %s" % filename
            cmd = "fslcpgeom %s %s -d" % (nii_list[0], filename)
            os.system(cmd)
            os.system("gzip %s " % filename)
Esempio n. 40
0
    def test_xnat_has_inputs(self):
        with XnatUtils.get_interface(host=host) as intf:
            intf.connect()

            print((yamls.proc_a))
            SanityChecks.__prep_project(intf)

            yamldoc = YamlDoc().from_string(yamls.proc_a)
            ap = AutoProcessor(XnatUtils, yamldoc)
            csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id,
                                                 sess_id)

            results = []
            for cassr in csess.assessors():
                has, errors = ap.has_inputs(cassr)
                results.append((has, errors))

            print(results)
Esempio n. 41
0
    def test_xnat_get_full_object(self):
        with XnatUtils.get_interface(host=host) as intf:
            intf.connect()

            print(map(lambda x: x['name'], intf.get_projects()))

            print(map(lambda x: x['label'], intf.get_subjects(proj_id)))

            subj = intf.select(XnatUtils.InterfaceTemp.S_XPATH.format(project=proj_id, subject=subj_id))
            print(subj.label())
            print(subj.parent().label())
Esempio n. 42
0
    def needs_run(self, cscan, xnat):
        """ needs_run function overridden from base-class
                cscan = CacheScan object from XnatUtils
            return True or False
        """

        # Check output
        if XnatUtils.has_resource(cscan, 'NIFTI'):
            LOGGER.debug('Has NIFTI')
            return False

        # Check input
        if not XnatUtils.has_resource(cscan, 'DICOM'):
            LOGGER.debug('No DICOM resource')
            return False

        if XnatUtils.is_cscan_unusable(cscan):
            LOGGER.debug('Unusable scan')
            return False

        return True
Esempio n. 43
0
    def test_setup_session(self):
        proj_id = 'proj1'
        subj_id = 'subj1'
        sess_ids = ['sess1', 'sess2']
        intf = XnatUtils.get_interface(host=host)
        scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair'),
                            ('12', 'flair'), ('21', 't2')]
        for sess_id in sess_ids:
            session = intf.select_experiment(proj_id, subj_id, sess_id)
            if not session.exists():
                self.assertTrue(False, "no such session")

            ComponentTestBuild._setup_scans(session, scan_descriptors)
Esempio n. 44
0
    def test_xnat_get_cached_image_session(self):
        with XnatUtils.get_interface(host=host) as intf:

            cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id,
                                                  sess_id)
            print(cisess)
            print((cisess.info()))
            for ciscan in cisess.scans():
                print((ciscan.info()))
                scanobj = ciscan.full_object()
                print(scanobj)
            for ciassr in cisess.assessors():
                print((ciassr.info()))
                for cirsrc in ciassr.out_resources():
                    print((cirsrc.info()))
                    asrinfo = ciassr.info()
                    rsrcobj = intf.select_assessor_resource(
                        asrinfo['project_id'], asrinfo['subject_id'],
                        asrinfo['session_id'], asrinfo['assessor_label'],
                        cirsrc.info()['label'])
                    print(rsrcobj)
            sessobj = cisess.full_object()
            print(sessobj)
Esempio n. 45
0
    def test_clean_scans_from_test_session(self):
        proj_id = 'proj1'
        subj_id = 'subj1'
        sess_ids = ['sess1', 'sess2']
        # subj_id = 'subj2'
        # sess_ids = ['sess3', 'sess4']
        intf = XnatUtils.get_interface(host=host)
        for sess_id in sess_ids:
            session = intf.select_experiment(proj_id, subj_id, sess_id)
            if not session.exists():
                self.assertTrue(False, "no such session")

            for scn in session.scans():
                scn.delete()
Esempio n. 46
0
 def get_cmds(self, assessor, job_dir_path):
     """
     This function generates the spider command for the cluster job 
     """
     project = assessor.parent().parent().parent().label()
     subject = assessor.parent().parent().label()
     session = assessor.parent().label()
     csess = XnatUtils.CachedImageSession(assessor._intf, project, subject,
                                          session)
     xnat = XnatUtils.get_interface()
     scan = csess.scans()[0].label()
     scan_data = get_scan_resource_uri(xnat, project, subject, session,
                                       scan)
     cmd = csai_CMD.format(spider_path=self.spider_path,
                           job_dir_path=job_dir_path,
                           assessor_label=assessor.label(),
                           project=project,
                           subject=subject,
                           session=session,
                           scan_data=scan_data,
                           suffix_proc=self.suffix_proc,
                           matlab_utils=self.matlab_utils,
                           matlab_bin=self.matlab_path)
     return [cmd]
Esempio n. 47
0
    def test_clean_assessors_from_test_session(self):
        proj_id = 'proj1'
        subj_id = 'subj1'
        sess_ids = ['sess1', 'sess2']
        # subj_id = 'subj2'
        # sess_ids = ['sess3', 'sess4']
        intf = XnatUtils.get_interface(host=host)
        for sess_id in sess_ids:
            session = intf.select_experiment(proj_id, subj_id, sess_id)
            if not session.exists():
                self.assertTrue(False, "no such session")

            for asr in session.assessors():
                print((assessor_utils.full_label_from_assessor(asr)))
                asr.delete()
Esempio n. 48
0
    def needs_run(self, cscan, xnat):
        """ needs_run function overridden from base-class
            cscan = CacheScan object from XnatUtils
            return True or False
        """
        _info = cscan.info()
        if _info['type'] not in self.scan_types:
            return False

        # Check for existing EDAT resource
        if XnatUtils.has_resource(cscan, 'EDAT'):
            LOGGER.debug('Has EDAT')
            return False

        return True
Esempio n. 49
0
    def test_get_assessor_inputs(self):
        class TestAssessor:
            class TestAttrs:
                def __init__(self, datatype, property):
                    self.datatype = datatype
                    self.property = property

                def get(self, name):
                    if name == self.datatype + '/' + self.property:
                        return json.dumps({'a': 'b'})
                    else:
                        raise IndexError("it's an index error")

            def __init__(self, datatype, property):
                self.attrs = TestAssessor.TestAttrs(datatype, property)
                self.datatype_ = datatype

            def datatype(self):
                return self.datatype_

        assr = TestAssessor('proc:genProcData', 'inputs')
        assr2 = TestAssessor('something', 'else')
        self.assertEqual(XnatUtils.get_assessor_inputs(assr), {'a': 'b'})
        self.assertEqual(XnatUtils.get_assessor_inputs(assr2), None)
Esempio n. 50
0
    def __init__(self,
                 mod_name=DEFAULT_MODULE_NAME,
                 directory=DEFAULT_TMP_PATH,
                 email=None,
                 text_report=DEFAULT_TEXT_REPORT,
                 limbo=DEFAULT_LIMBO_DIR,
                 scan_types='fMRI_EDP,fMRI_Posner,fMRI_NBack,fMRI_EmoStroop',
                 scan_map=DEFAULT_MAP):

        super(Module_edat_limbo2xnat, self).__init__(mod_name,
                                                     directory,
                                                     email,
                                                     text_report=text_report)
        self.limbo = limbo
        self.scan_map = scan_map
        self.scan_types = XnatUtils.get_input_list(scan_types, None)
        self.xnat = None
Esempio n. 51
0
    def test_create_assessor_with_no_input(self):
        with XnatUtils.get_interface(host=host) as intf:
            intf.connect()

            e = intf.select_experiment(proj_id, subj_id, sess_id)
            if not e.exists():
                self.assertTrue(
                    False, "Unexpected: {}//{}//{} does not exist".format(
                        proj_id, subj_id, sess_id))

            proc_a_params = {
                'xsitype': asrxsitype,
                'proctype': 'Proc_A_v1',
                'files': [('SEG', ['seg.gz'])]
            }
            SessionTools.add_assessor(e, 'proc1-x-subj1-x-sess1-x-2-Proc_A_v1',
                                      proc_a_params, 'no_inputs')
Esempio n. 52
0
    def download_xnat_resource(self, src, dst):
        result = None

        try:
            xnat = XnatUtils.get_interface(self.host, self.user, self.pwd)
            try:
                res = xnat.select(src)
                res.get(dst, extract=True)
                result = dst
            except:
                print('ERROR:downloading from XNAT')
        except:
            print('ERROR:FAILED to get XNAT connection')
        finally:
            xnat.disconnect()

        return result
Esempio n. 53
0
    def download_xnat_file(self, src, dst):
        result = None

        try:
            xnat = XnatUtils.get_interface(self.host, self.user, self.pwd)

            try:
                _res, _file = src.split('/files/')
                res = xnat.select(_res)
                result = res.file(_file).get(dst)
            except:
                print('ERROR:downloading from XNAT')
        except:
            print('ERROR:FAILED to get XNAT connection')
        finally:
            xnat.disconnect()

        return result
Esempio n. 54
0
    def upload_converted_images(self, dcm_dir, scan_obj, scan_info):
        """ upload images after checking them """
        nifti_list = []
        bval_path = ''
        bvec_path = ''

        LOGGER.debug('uploading the NIFTI files to XNAT...')

        # Get the NIFTI/bvec/bval files from the folder:
        for fpath in glob.glob(os.path.join(dcm_dir, '*')):
            if not os.path.isfile(fpath):
                continue

            if fpath.lower().endswith('.bval'):
                bval_path = fpath
            elif fpath.lower().endswith('.bvec'):
                bvec_path = fpath
            elif fpath.endswith('ADC.nii.gz'):
                LOGGER.warn('ignoring ADC NIFTI:' + fpath)
            elif fpath.lower().endswith('.nii.gz'):
                nifti_list.append(fpath)

        # Check
        success = self.check_outputs(scan_info, nifti_list, bval_path,
                                     bvec_path)

        if not success:
            print('not successful?')
            return

        # Upload
        XnatUtils.upload_files_to_obj(nifti_list,
                                      scan_obj.resource('NIFTI'),
                                      remove=True)

        if os.path.isfile(bval_path) and os.path.isfile(bvec_path):
            # BVAL/BVEC
            XnatUtils.upload_file_to_obj(bval_path,
                                         scan_obj.resource('BVAL'),
                                         remove=True)

            XnatUtils.upload_file_to_obj(bvec_path,
                                         scan_obj.resource('BVEC'),
                                         remove=True)

        # more than one NIFTI uploaded
        if len(nifti_list) > 1:
            LOGGER.warn('dcm2nii:{} multiple NIFTI'.format(
                scan_info['scan_id']))
            self.log_warning_error('multiple NIFTI', scan_info)
Esempio n. 55
0
    def __init__(self,
                 spider_path,
                 jobdir,
                 xnat_project,
                 xnat_subject,
                 xnat_session,
                 xnat_host=None,
                 xnat_user=None,
                 xnat_pass=None,
                 suffix="",
                 subdir=True,
                 skip_finish=False):
        """
        Entry point for the Base class for spider

        :param spider_path: spider file path
        :param jobdir: directory for temporary files
        :param xnat_project: project ID on XNAT
        :param xnat_subject: subject label on XNAT
        :param xnat_session: experiment label on XNAT
        :param xnat_host: host for XNAT if not set in environment variables
        :param xnat_user: user for XNAT if not set in environment variables
        :param xnat_pass: password for XNAT if not set in environment variables
        :param suffix: suffix to the assessor creation
        :param subdir: create a subdir Temp in the jobdir if the directory isn't empty

        """
        # Spider path:
        self.spider_path = spider_path
        # directory for temporary files + create it
        self.jobdir = XnatUtils.makedir(os.path.abspath(jobdir), subdir=subdir)
        # to copy results at the end
        self.spider_handler = None
        # Xnat info:
        self.xnat_project = xnat_project
        self.xnat_subject = xnat_subject
        self.xnat_session = xnat_session
        # Xnat connection settings:
        self.host = self.get_default_value("host", "XNAT_HOST", xnat_host)
        self.user = self.get_default_value("user", "XNAT_USER", xnat_user)
        self.pwd = self.get_pwd(xnat_pass, xnat_user)
        # Suffix
        if not suffix:
            self.suffix = ""
        else:
            # Set the suffix_proc remove any special characters and replace by '_'
            self.suffix = re.sub('[^a-zA-Z0-9]', '_', suffix)
            # Replace multiple underscores by one
            self.suffix = re.sub('_+', '_', self.suffix)
            # Remove underscore if at the end of suffix
            if self.suffix[-1] == '_': self.suffix = self.suffix[:-1]
            # Add an underscore at the beginning if not present
            if self.suffix[0] != '_': self.suffix = '_' + self.suffix
        # print time writer:
        self.time_writer = TimedWriter()
        # Export the variable:
        os.environ['XNAT_HOST'] = self.host
        os.environ['XNAT_USER'] = self.user
        os.environ['XNAT_PASS'] = self.pwd
        # run the finish or not
        self.skip_finish = skip_finish
Esempio n. 56
0
 def __get_connection():
     return XnatUtils.get_interface(host=host)
Esempio n. 57
0
import json
import sys
import pandas
import pyxnat
import argparse
import time
from dax import XnatUtils
from dax import utilities

# Specify and parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('project', help='Project name')
args = parser.parse_args()
print('Project: {}'.format(args.project))

xnat = XnatUtils.get_interface()
Assrs = XnatUtils.list_project_assessors(xnat, args.project)
xnat.disconnect()

timestamp = time.strftime("%Y%m%d%H%M%S")
outfile1 = 'status_by_assessor_{}_{}.csv'.format(args.project, timestamp)
outfile2 = 'status_by_session_{}_{}.csv'.format(args.project, timestamp)

R = list()
for assr in Assrs:

    #print(assr['assessor_label'])

    # Get desired fields
    thisR = {}
    for key in ('project_label', 'subject_label', 'session_label', 'proctype',
Esempio n. 58
0
# TODO: daxlauncher.upload()

import logging

from dax import bin, XnatUtils

# Create the launcher
settingspath = '/Users/boydb1/.dax/settings/settings-subjgenproc.yaml'
daxlauncher = bin.read_yaml_settings(settingspath, logging.getLogger())

# Get xnat connection
xnat = XnatUtils.get_interface(host=daxlauncher.xnat_host)

# Build
print('building')
project = 'DepMIND2'
daxlauncher.build_project_subjgenproc(xnat, project)

print('All Done!')
Esempio n. 59
0
 def test_session_full_object(self):
     with SanityChecks.__get_connection() as intf:
         sessobj1 = intf.select_experiment(proj_id, subj_id, sess_id)
         sessobj2 = XnatUtils.CachedImageSession(intf, proj_id, subj_id,
                                                 sess_id).full_object()
         self.assertEqual(sessobj1.label(), sessobj2.label())
Esempio n. 60
0
#!python

import sys
import pyxnat
from dax import XnatUtils

source_project = sys.argv[1]
dest_project = sys.argv[2]
subject = sys.argv[3]

xnat = XnatUtils.InterfaceTemp()

# Subject variables
source_subject = xnat.select('/projects/{0}/subjects/{1}'.format(
    source_project, subject))
dest_subject = xnat.select('/projects/{0}/subjects/{1}'.format(
    dest_project, subject))

dob = source_subject.attrs.get('xnat:demographicData/dob')
gender = source_subject.attrs.get('xnat:demographicData/gender')
handedness = source_subject.attrs.get('xnat:demographicData/handedness')
race = source_subject.attrs.get('xnat:demographicData/race')
#sid = source_subject.attrs.get("xnat:subjectData/fields/field[name='id']/field")
sid = source_subject.xpath(
    "/xnat:Subject/xnat:fields/xnat:field[@name='id']/text()[2]")[0]

dest_subject.attrs.set(
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/dob', dob)
dest_subject.attrs.set(
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/gender',
    gender)