Example #1
0
    def _gen_outfilename(self, name):
        if name == 'out_file':
            out_file = self.inputs.out_file
            if isdefined(out_file) and isdefined(self.inputs.input_file):
                _, _, ext = split_filename(self.inputs.input_file)
                out_file = self.inputs.out_file + ext
            if not isdefined(out_file) and isdefined(self.inputs.input_file):
                pth, fname, ext = split_filename(self.inputs.input_file)
                print(pth, fname, ext)
                out_file = os.path.join(pth, fname + '_bet' + ext)
        elif name == 'out_mask':
            out_file = self.inputs.out_file
            if isdefined(out_file) and isdefined(self.inputs.input_file):
                _, _, ext = split_filename(self.inputs.input_file)
                out_file = self.inputs.out_file + '_mask' + ext


#             if isdefined(out_file):
#                 pth, fname, ext = split_filename(out_file)
#                 out_file = os.path.join(pth, fname+'_bet_mask'+ext)
            elif not isdefined(out_file) and isdefined(self.inputs.input_file):
                pth, fname, ext = split_filename(self.inputs.input_file)
                print(pth, fname, ext)
                out_file = os.path.join(pth, fname + '_bet_mask' + ext)

        return os.path.abspath(out_file)
Example #2
0
    def _gen_outfilename(self):

        out_file = self.inputs.out_file
        if isdefined(out_file) and isdefined(self.inputs.t1):
            _, _, ext = split_filename(self.inputs.t1)
            out_file = self.inputs.out_file + ext
        if not isdefined(out_file) and isdefined(self.inputs.t1):
            pth, _, ext = split_filename(self.inputs.t1)
            print(pth, ext)
            out_file = os.path.join(pth, 'segmentation' + ext)

        return os.path.abspath(out_file)
Example #3
0
 def __init__(self, toConvert, clean=False, bin_path=''):
     print('Started image format conversion...')
     self.basedir, self.filename, _ = split_filename(toConvert)
     self.filename = self.filename.split('.')[0]
     self.toConvert = toConvert
     self.clean = clean
     self.bin_path = bin_path
Example #4
0
    def __init__(self, image, mask=None, prefix=None):
        print('\nStarting image cropping...')

        self.image = image
        self.mask = mask

        imagePath, imageFilename, imageExt = split_filename(image)
        filename = imageFilename.split('.')[0]
        if mask is not None:
            _, maskFilename, maskExt = split_filename(mask)
            maskFilename = maskFilename.replace('.', '_')
            self.maskOutname = os.path.join(
                imagePath, maskFilename + '_cropped') + maskExt

        if prefix is None and mask is not None:
            self.imageOutname = os.path.join(imagePath,
                                             filename + '_cropped') + imageExt
        elif prefix is None and mask is None:
            self.imageOutname = os.path.join(imagePath, filename + '_cropped')
        elif prefix is not None and mask is None:
            self.imageOutname = os.path.join(imagePath, prefix + '_cropped')
        elif prefix is not None and mask is not None:
            self.imageOutname = os.path.join(imagePath,
                                             prefix + '_cropped') + imageExt
Example #5
0
    def _run_interface(self, runtime):

        images = self.inputs.images
        if images:
            new_dir = os.path.abspath('data_prepared')
            os.mkdir(os.path.abspath('data_prepared'))
            for i, image in enumerate(images):
                _, _, ext = split_filename(image)
                shutil.copy2(
                    image,
                    os.path.join(new_dir,
                                 'subject1_{}'.format(str(i).zfill(4)) + ext))
        else:
            raise Exception('No images provided!Please check.')

        return runtime
Example #6
0
def put(project,
        subject,
        sessions,
        sub_folder,
        config=None,
        url=None,
        pwd=None,
        user=None,
        processed=False):

    if config is not None:
        interface = Interface(config)
    else:
        interface = Interface(server=url,
                              user=user,
                              password=pwd,
                              proxy='www-int2:80')

    uri = '/data/projects/%s/subjects/%s' % (project, subject)
    response = interface.put(uri)
    subject_uid = response.content
    print('New subject %s created!' % subject_uid)

    xnat_subs = interface.select.project(project).subjects()
    xnat_sub = [x for x in xnat_subs if x.label() == subject][0]
    sub_id = interface.select.project(project).subject(xnat_sub.id())

    for session in sessions:
        print('Processing session {}'.format(session))
        session_folder = os.path.join(sub_folder, session)
        scans = [
            x for x in sorted(glob.glob(session_folder + '/*'))
            if os.path.isfile(x)
        ]
        match = session_modality_re.match(session)
        proc = ''
        if match is None and processed:
            experiment_type = 'xnat:mrSessionData'
            scan_type = 'xnat:mrScanData'
            proc = '_processed'
        elif match.group(1) == 'MR':
            experiment_type = 'xnat:mrSessionData'
            scan_type = 'xnat:mrScanData'
        elif match.group(1) == 'CT' or match.group(1) == 'CTREF':
            experiment_type = 'xnat:ctSessionData'
            scan_type = 'xnat:ctScanData'
        elif match.group(1) == 'RT':
            experiment_type = 'xnat:rtSessionData'
            scan_type = 'xnat:rtScanData'

        experiment = sub_id.experiment('%s_%s%s' %
                                       (xnat_sub.label(), session, proc))
        if not experiment.exists():
            try:
                experiment.create(experiments=experiment_type)
            except DatabaseError:
                experiment.create(experiments=experiment_type)
            print('New experiment %s created!' % experiment.id())

        for scan in scans:

            _, scan_name, extention = split_filename(scan)
            res_format = get_resource_name(scan)
            #     scan_name = scan.split('/')[-1].split('.')[0]
            print('Uploading {}...'.format(scan_name))
            xnat_scan = experiment.scan(scan_name)
            if not xnat_scan.exists():
                done = False
                z = 1
                while not done:
                    try:
                        xnat_scan.create(scans=scan_type)
                        done = True
                    except DatabaseError:
                        print('Same Database error: {} times'.format(z))
                        z = z + 1
        #                 xnat_scan.create(scans=scan_type)
                print('New scan %s created!' % xnat_scan.id())
                resource = xnat_scan.resource(res_format)
                if not resource.exists():
                    try:
                        resource.create()
                    except DatabaseError:
                        resource.create()
                    print('New resource %s created!' % resource.id())
                else:
                    print('Resource %s already in the repository!' %
                          resource.id())
                xnat_resource = resource.file(scan_name + extention)
                response = xnat_resource.put(src=scan,
                                             format=res_format,
                                             content=res_format,
                                             extract=False,
                                             overwrite=True)
            else:
                print('Scan %s already in the repository!' % xnat_scan.id())