示例#1
0
def trk_fixer(trkpath, trk_newpath, verbose=False):

    if verbose:
        t1 = time()
        txt = f'Beginning to load {trkpath}'
    trkdata = load_trk_spe(trkpath, 'same')
    if verbose:
        t2 = time()
        txt = f'It took {str(t2-t1)} seconds to load {trkpath}'
        print(txt)
    if hasattr(trkdata, 'space_attribute'):
        header = trkdata.space_attribute
    elif hasattr(trkdata, 'space_attributes'):
        header = trkdata.space_attributes
    remove, keep = trkdata.remove_invalid_streamlines()
    if verbose:
        t3 = time()
        duration = t3 - t2
        txt = f"it took {duration} to do the test on {trkpath}, and {np.size(remove)} streamlines were removed. saving it to {trk_newpath}"
        print(txt)
    if np.size(remove)==0:
        shutil.copy(trkpath, trk_newpath)
    else:
        new_streamlines = trkdata.streamlines
        save_trk_header(trk_newpath, new_streamlines, header, verbose=verbose)
    if verbose:
        t4 = time()
        txt = f'It took {str(t4-t3)} seconds to save {trk_newpath}'
        print(txt)
示例#2
0
def trk_fixer_old(trkpath, trk_newpath, verbose = False):

    if verbose:
        t1 = time()
        txt = f'Beginning to load {trkpath}'
    trkdata = load_trk_spe(trkpath, 'same')
    trk_streamlines = trkdata.streamlines
    if hasattr(trkdata, 'space_attribute'):
        header = trkdata.space_attribute
    elif hasattr(trkdata, 'space_attributes'):
        header = trkdata.space_attributes

    voxdim = trkdata.dimensions

    if verbose:
        t2 = time()
        txt = f'It took {str(t2-t1)} seconds to load {trkpath}'
        print(txt)
    vverbose=True

    orig_size = np.shape(trk_streamlines)[0]

    trk_streamlines[:] = (x for x in trk_streamlines if streamline_checker(x, voxdim,vverbose))

    """
    for i, streamline in enumerate(trk_streamlines):
        mymin = np.min(trk_streamlines, axis=0)
        mymax = np.max(trk_streamlines, axis=0)
        np.asarray(list(product(*zip(mymin, mymax))))
        bbox_min = np.min(trk_streamlines, axis=0)
        bbox_max = np.max(trk_streamlines, axis=0)
        bbox_corners = np.asarray(list(product(*zip(bbox_min, bbox_max))))
        if np.any(bbox_corners[:, 0] > voxdim[0]) or \
        np.any(bbox_corners[:, 1] > voxdim[1]) or \
        np.any(bbox_corners[:, 2] > voxdim[2]):
            np.pop()
    """

    new_size = np.shape(trk_streamlines)[0]
    cut_streamlines = orig_size - new_size

    if verbose:
        t3 = time()
        duration = t3 - t2
        txt = f"it took {duration} to do the test on {trkpath}, and {str(cut_streamlines)} streamlines were removed. saving it to {trk_newpath}"
        print(txt)

    #np.asarray(list(product(*zip(bbox_min, bbox_max))))

    if cut_streamlines!=0:
        save_trk_header(trk_newpath, trk_streamlines, header, verbose = verbose)
    else:
        shutil.copy(trkpath, trk_newpath)

    if verbose:
        t4 = time()
        txt = f'It took {str(t4-t3)} seconds to save {trk_newpath}'
        print(txt)
                    groupstreamlines[group])[0]:
                raise Exception('happened from there')

            if os.path.exists(
                    streamline_file_path) and overwrite and write_streamlines:
                os.remove(streamline_file_path)
            if not os.path.exists(streamline_file_path) and write_streamlines:
                if verbose:
                    print(
                        f'Summarized the streamlines for group {group} at {streamline_file_path}'
                    )
                pickle.dump(groupstreamlines[group],
                            open(streamline_file_path, "wb"))
                save_trk_header(filepath=streamline_file_path,
                                streamlines=groupstreamlines[group],
                                header=header,
                                affine=np.eye(4),
                                verbose=verbose)
            """
            if not os.path.exists(streamline_file_path_orig) and write_streamlines:
                if verbose:
                    print(f'Summarized the streamlines for group {group} at {streamline_file_path}')
                pickle.dump(groupstreamlines_orig[group], open(streamline_file_path, "wb"))
                save_trk_header(filepath= streamline_file_path, streamlines = groupstreamlines_orig[group], header = header, affine=np.eye(4), verbose=verbose)
            """

            for ref in references:
                if overwrite:
                    if os.path.exists(grouping_files[ref, 'lines']):
                        os.remove(grouping_files[ref, 'lines'])
                    if os.path.exists(grouping_files[ref, 'points']):
示例#4
0
		#rigid = os.path.join(work_dir, "dwi", f"{subj}_rigid.mat")
		#affine = os.path.join(work_dir, "dwi", f"{subj}_affine.mat")
		#runno_to_MDT = os.path.join(work_dir, f'dwi/SyN_0p5_3_0p5_fa/faMDT_NoNameYet_n37_i6/reg_diffeo/{subj}_to_MDT_warp.nii.gz')


		_, exists = check_files([trans, rigid, affine, runno_to_MDT])
		if np.any(exists==0):
		    raise Exception('missing transform file')

		affine_map_test = get_affine_transform(reference, subj_dwi)
		streamlines, header = unload_trk(subj_trk)

		tmp2_streamlines = transform_streamlines(streamlines, np.linalg.inv(affine_map_test), in_place=False)

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

		#tmp2_streamlines, header_new = unload_trk(trk_filepath_tmp2)
		affine_transform, newaffine = get_flip_affine(orientation_in, orientation_out)
		affine_transform_new = affine_transform

		#affine_transform[:3,3] = affine_transform.diagonal()[:3]* header[0][:3,3] - header[0][:3,3]

		center1 = header[0][:3, 3]
		center2 = affine_transform.diagonal()[:3] * center1[:3]
		affine_transform_new[:3, 3] = center1 - center2
		affine_transform_new[1, 3] = affine_transform[0, 3]

		#affine_transform_new = np.eye(4)
		#affine_transform_new[:3,:3] = np.dot(affine_transform[:3,:3], affine_map_test[:3,:3])
		project_input_streamlines = transform_streamlines(tmp2_streamlines, np.linalg.inv(affine_transform_new))
示例#5
0
        new_transmat = np.eye(4)
        vox_dim = [1, 1, -1]
        #new_transmat[:3, 3] = np.squeeze(later_trans_mat[3:6]) * vox_dim
        new_transmat[:3, 3] = np.squeeze(
            np.matmul(subj_affine[:3, :3], later_trans_mat[3:6])
        )  #should be the AFFINE of the current image, to make sure the slight difference in orientation is ACCOUNTED FOR!!!!!!!!!!
        new_transmat[2, 3] = 0
        print(new_transmat)
        streamlines_posttrans = transform_streamlines(streamlines_prepro,
                                                      (new_transmat))

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

        rigid_struct = loadmat(rigid)
        var_name = list(rigid_struct.keys())[0]
        rigid_ants = rigid_struct[var_name]
        rigid_mat = convert_ants_vals_to_affine(rigid_ants)

        #streamlines_posttrans, header_posttrans = unload_trk(trk_preprocess_posttrans)
        streamlines_postrigid = transform_streamlines(streamlines_posttrans,
                                                      np.linalg.inv(rigid_mat))

        if (not os.path.exists(trk_preprocess_postrigid)
                or overwrite) and save_temp_files:
            save_trk_header(filepath=trk_preprocess_postrigid,