Exemplo n.º 1
0
ratio = 100
step_size = 2
peak_processes=1
get_params=False
doprune = False
#mask, _ = getmask(os.path.dirname(input_diff_file), subject, masktype, verbose)

#check_dif_ratio(outpath, subject, strproperty, ratio)

if os.path.exists(output_trk_file) and overwrite is False:
    print("The tract creation of subject " + subject + " is already done")

if verbose:
    print('Running the ' + subject + ' file')

diff_data, affine, vox_size, header, ref_info = extract_nii_info(input_diff_file, verbose)
gtab = getgtab(os.path.dirname(input_diff_file), subject, bvec_orient)

if np.size(np.shape(mask)) == 1:
    mask = mask[0]
if np.size(np.shape(mask)) == 4:
    mask = mask[:, :, :, 0]
print("Mask shape is " + str(np.shape(mask)))

#if classifier == "FA":
#    outpathbmfa, mask = make_tensorfit(diff_data ,mask ,gtab ,affine ,subject ,outpath=diffpath ,verbose=verbose)

print(verbose)
if verbose:
    txt = ("The QCSA Tractmake is ready to launch for subject " + subject)
    print(txt)
Exemplo n.º 2
0
				    affine=np.eye(4), verbose=verbose)

		affine_mat_path = os.path.join(path_transforms, f'{subj}_affine.txt')
		affine_mat_s = read_affine_txt(affine_mat_path)
		affine_mat = np.eye(4)
		affine_mat[:3, :3] = affine_mat_s
		#streamlines_postrigid, header_postrigid = unload_trk(trk_preprocess_postrigid)
		streamlines_postrigidaffine = transform_streamlines(streamlines_postrigid, np.linalg.inv(affine_mat))

		if (not os.path.exists(trk_preprocess_postrigid_affine) or overwrite) and save_temp_files:
		    save_trk_header(filepath=trk_preprocess_postrigid_affine, streamlines=streamlines_postrigidaffine, header=header,
				    affine=np.eye(4), verbose=verbose)

		#streamlines_postrigidaffine, header_postrigidaffine = unload_trk(trk_preprocess_postrigid_affine)

		warp, affine, vox_size, header_warp, ref_info = extract_nii_info(runno_to_MDT)
		warp = warp[:,:,:,0,:]

		vox_size = tuple(np.linalg.eigvals(affine[:3,:3]))
		vox_size = vox_size[0]
		target_isocenter = np.diag(np.array([-vox_size, vox_size, vox_size, 1]))
		streamlines_post_warp = deform_streamlines(
		    streamlines_postrigidaffine, deform_field=warp,
		    stream_to_current_grid=target_isocenter,
		    current_grid_to_world=np.eye(4), stream_to_ref_grid=target_isocenter,
		    ref_grid_to_world=np.eye(4))

		if (not os.path.exists(trk_MDT_space) or overwrite):
		    save_trk_header(filepath=trk_MDT_space, streamlines=streamlines_post_warp, header=header,
				    affine=np.eye(4), verbose=verbose)
	else:
Exemplo n.º 3
0
def header_superpose_trk(target_path, origin_path, outpath=None):

    if not isinstance(origin_path, str):
        origin_trk = origin_path
    else:
        origin_trk = load_trk(origin_path, 'same')

    target_data, target_affine, vox_size, target_header, target_ref_info = extract_nii_info(
        target_path)

    if outpath is None:
        if isinstance(origin_path, str):
            warnings.warn("Will copy over old trkfile, if this what you want?")
            permission = input("enter yes or y if you are ok with this")
            if permission.lower() == "yes" or permission.lower() == "y":
                outpath = origin_trk
            else:
                raise Exception("Will not copy over old trk file")
        else:
            raise Exception("Need to specify a output path of some kind")

    trk_header = origin_trk.space_attributes
    trk_affine = origin_trk._affine
    trkstreamlines = origin_trk.streamlines
    if np.any(trk_header[1][0:3] != np.shape(target_data)[0:3]):
        raise TypeError(
            'Size of the originating matrix are difference, recalculation not implemented'
        )
    if np.any(trk_affine != target_affine):
        test = 3
        if test == 1:
            trk_header = list(trk_header)
            trk_header[0] = target_affine
            trk_header = tuple(trk_header)
            myheader = create_tractogram_header(outpath, *trk_header)
            trk_sl = lambda: (s for s in trkstreamlines)
            save_trk_heavy_duty(outpath,
                                streamlines=trk_sl,
                                affine=target_affine,
                                header=myheader)
        elif test == 2:
            transform_matrix = (
                np.inverse(np.transpose(trk_affine) * trk_affine) *
                np.transpose(trk_affine)) * target_affine
            from dipy.tracking.streamline import transform_streamlines
            myheader = create_tractogram_header(outpath, *trk_header)
            new_streamlines = transform_streamlines(trkstreamlines,
                                                    transform_matrix)
            trk_sl = lambda: (s for s in new_streamlines)
            save_trk_heavy_duty(outpath,
                                streamlines=trkstreamlines,
                                affine=trk_affine,
                                header=myheader)
        elif test == 3:
            myheader = create_tractogram_header(outpath, *target_ref_info)
            trk_sl = lambda: (s for s in trkstreamlines)
            save_trk_heavy_duty(outpath,
                                streamlines=trk_sl,
                                affine=target_affine,
                                header=myheader)
    else:
        print("No need to change affine, bring to new path")
        if isinstance(origin_path, str):
            copyfile(origin_path, outpath)
        else:
            myheader = create_tractogram_header(outpath, *trk_header)
            save_trk_heavy_duty(outpath,
                                streamlines=trkstreamlines,
                                affine=target_affine,
                                header=myheader)