Exemplo n.º 1
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('in_file')
    args = arg_parser.parse_args(args)
    print(args)

    p = pydicom.read_file(args['in_file'])
    pixels = p.pixel_array
Exemplo n.º 2
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    args = arg_parser.parse_args(args)

    os.makedirs(args['out_dir'], exist_ok=True)

    print('Calculate ratios')
    fp = os.path.join(args['src_dir'], 'features_all.csv')
    df = pd.read_csv(fp, index_col='SID')

    tot_area = \
        df['muscle_original_shape_SurfaceArea'] + \
        df['SAT_original_shape_SurfaceArea'] + \
        df['VAT_original_shape_SurfaceArea']
    mus_area = df['muscle_original_shape_SurfaceArea']
    vat_area = df['VAT_original_shape_SurfaceArea']
    sat_area = df['SAT_original_shape_SurfaceArea']
    fat_area = \
        df['SAT_original_shape_SurfaceArea'] + \
        df['VAT_original_shape_SurfaceArea']

    columns = {
        'mus_tot': mus_area / tot_area,
        'vat_tot': vat_area / tot_area,
        'sat_tot': sat_area / tot_area,
        'fat_tot': fat_area / tot_area,
        'sat_vat': sat_area / vat_area,
        'mus_vat': mus_area / vat_area,
        'mus_sat': mus_area / sat_area,
        'mus_fat': mus_area / fat_area,
    }

    df = pd.DataFrame()
    df = df.from_dict(
        {
            'mus_tot': columns['mus_tot'],
            'vat_tot': columns['vat_tot'],
            'sat_tot': columns['sat_tot'],
            'fat_tot': columns['fat_tot'],
            'sat_vat': columns['sat_vat'],
            'mus_vat': columns['mus_vat'],
            'mus_sat': columns['mus_sat'],
            'mus_fat': columns['mus_fat'],
        },
        orient='columns')
    fp = os.path.join(args['src_dir'], 'features_area_ratios.csv')
    df.to_csv(fp, index=True)
    print(df.shape)
Exemplo n.º 3
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    args = arg_parser.parse_args(args)

    data = {}

    # For each patient ID, load file path
    with open('{}/pat_id_file.csv'.format(args['src_dir']), 'r') as f:
        header = True
        for line in f.readlines():
            if header:
                header = False
                continue
            if line.startswith('#'):
                continue
            items = line.strip().split(',')
            data[items[0]] = [items[1]]

    # For each patient ID, load random ID
    with open('{}/pat_id_random_id.csv'.format(args['src_dir']), 'r') as f:
        header = True
        for line in f.readlines():
            if header:
                header = False
                continue
            if line.startswith('#'):
                continue
            items = line.strip().split(',')
            data[items[0]].append(items[1])

    os.makedirs(args['out_dir'], exist_ok=True)

    # Copy files to randomized new name
    for pat_id in data.keys():
        dcm_file_path = data[pat_id][0]
        tag_file_path = get_tag_file_path(dcm_file_path)
        random_id = data[pat_id][1]
        dcm_file_name = replace_with_random_id(dcm_file_path, random_id)
        tag_file_name = replace_with_random_id(tag_file_path, random_id)
        shutil.copyfile(dcm_file_path,
                        os.path.join(args['out_dir'], dcm_file_name))
        shutil.copyfile(tag_file_path,
                        os.path.join(args['out_dir'], tag_file_name))
        print('Renamed {}'.format(dcm_file_path))
Exemplo n.º 4
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('ctp_dir')
    arg_parser.add_arg('tag_dir')
    arg_parser.add_arg('out_dir')
    arg_parser.add_arg('id_file')
    args = arg_parser.parse_args(args)

    random_ids = []
    with open(args['id_file'], 'r') as f:
        header = True
        for line in f.readlines():
            if header:
                header = False
                continue
            random_ids.append(line.strip())

    os.makedirs(args['out_dir'], exist_ok=True)

    for random_id in random_ids:
        file_path = get_file_path(os.path.join(args['ctp_dir'], random_id + '_dcm'))
        shutil.copyfile(
            file_path,
            os.path.join(args['out_dir'], random_id + '.dcm'))
        shutil.copyfile(
            os.path.join(args['tag_dir'], random_id + '.tag'),
            os.path.join(args['out_dir'], random_id + '.tag'))
Exemplo n.º 5
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_file')
    arg_parser.add_arg('out_file')
    args = arg_parser.parse_args(args)
    print(args)

    lines = []
    with open(args['src_file'], 'r') as f:
        for line in f.readlines():
            line = line.strip()
            ix = line.split(',')[0]
            fp = line.split(',')[1]
            fn = os.path.split(fp)[1]
            lines.append([ix, fn])

    with open(args['out_file'], 'w') as f:
        for line in lines:
            f.write('{},{}\n'.format(line[0], line[1]))
Exemplo n.º 6
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    args = arg_parser.parse_args(args)

    os.makedirs(args['out_dir'], exist_ok=True)

    print('Create feature file per tissue')
    for tissue in ['muscle', 'SAT', 'VAT']:
        body = []
        index = []
        header = None
        for f in os.listdir(args['src_dir']):
            expression = '_tag_{}.txt'.format(tissue)
            if f.endswith(expression):
                sid = os.path.splitext(f)[0][:-(len(expression) - 4)]
                index.append(sid)
                feature_file = os.path.join(args['src_dir'], f)
                row, cols = get_row(feature_file)
                if not header:
                    header = cols
                body.append(row)
        fp = os.path.join(args['out_dir'], 'features_{}.csv'.format(tissue))
        df = pd.DataFrame(data=body, columns=header, index=index)
        df.index.name = 'SID'
        df.to_csv(fp, index=True)
        # print(fp)

    print('Creating feature file for ALL tissues')
    dfs = []
    for tissue in ['muscle', 'SAT', 'VAT']:
        fp = os.path.join(args['out_dir'], 'features_{}.csv'.format(tissue))
        df = pd.read_csv(fp, index_col='SID')
        df.columns = ['{}_'.format(tissue) + c for c in df.columns]
        dfs.append(df)
    fp = os.path.join(args['out_dir'], 'features_all.csv')
    df = pd.concat(dfs, axis=1)
    df.index.name = 'SID'
    df.to_csv(fp, index=True)
Exemplo n.º 7
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    args = arg_parser.parse_args(args)

    for f in os.listdir(args['src_dir']):
        if f.endswith('.dcm'):
            sid = os.path.splitext(f)[0]
            tag_file = os.path.join(args['src_dir'], sid + '.tag')
            dcm_file = os.path.join(args['src_dir'], sid + '.dcm')
            # Convert TAG file to DICOM
            tag2dcm = Tag2Dcm()
            tag2dcm.set_input('tag_file', tag_file)
            tag2dcm.set_input('dcm_file', dcm_file)
            tag2dcm.set_param('out_dir', args['out_dir'])
            out_file = tag2dcm.get_output('out_file')
            # Copy DICOM to output directory as well
            shutil.copyfile(dcm_file, os.path.join(args['out_dir'], sid + '.dcm'))
            print(out_file)
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    args = arg_parser.parse_args(args)
    print(args)

    for tissue in ['muscle', 'SAT', 'VAT']:
        for f in os.listdir(args['src_dir']):
            expression = '_tag_{}.dcm'.format(tissue)
            if f.endswith(expression):
                sid = os.path.splitext(f)[0][:-(len(expression) - 4)]
                in_file = os.path.join(args['src_dir'], sid + '.dcm')
                mask_file = os.path.join(args['src_dir'],
                                         sid + '_tag_{}.dcm'.format(tissue))
                crf = CalculateRadiomicsFeatures()
                crf.set_input('in_file', in_file)
                crf.set_input('mask_file', mask_file)
                crf.set_param('out_dir', args['out_dir'])
                out_file = crf.get_output('out_file')
                print(out_file)
Exemplo n.º 9
0
def run(args):

    arg_parser = ArgParser()
    arg_parser.add_arg('src_dir')
    arg_parser.add_arg('out_dir')
    arg_parser.add_arg('label_map')
    args = arg_parser.parse_args(args)
    print(args)

    for f in os.listdir(args['src_dir']):
        if f.endswith('_tag.dcm'):
            sid = os.path.splitext(f)[0][:-4]
            tag_file = os.path.join(args['src_dir'], f)
            dcm_file = os.path.join(args['src_dir'], sid + '.dcm')
            dcm2masks = Dcm2Masks()
            dcm2masks.set_input('dcm_file', tag_file)
            dcm2masks.set_param('label_map', args['label_map'])
            dcm2masks.set_param('out_dir', args['out_dir'])
            out_files = dcm2masks.get_output('out_files')
            # Copy DICOM to output directory as well
            shutil.copyfile(dcm_file,
                            os.path.join(args['out_dir'], sid + '.dcm'))
            print(out_files)