示例#1
0
def lap_multiple_examples(moving_tractograms_dir, static_tractogram, ex_dir,
                          out_filename):
    """Code for LAP from multiple examples.
	"""
    moving_tractograms = os.listdir(moving_tractograms_dir)
    moving_tractograms.sort()
    examples = os.listdir(ex_dir)
    examples.sort()

    nt = len(moving_tractograms)
    ne = len(examples)

    if nt != ne:
        print(
            "Error: number of moving tractograms differs from number of example bundles."
        )
        sys.exit()
    else:
        result_lap = []
        for i in range(nt):
            moving_tractogram = '%s/%s' % (moving_tractograms_dir,
                                           moving_tractograms[i])
            example = '%s/%s' % (ex_dir, examples[i])
            tmp = np.array([
                lap_single_example(moving_tractogram, static_tractogram,
                                   example)
            ])
            result_lap.append(tmp)

        result_lap = np.array(result_lap)
        estimated_bundle_idx = np.hstack(result_lap[:, 0, 0])
        min_cost_values = np.hstack(result_lap[:, 0, 1])
        example_bundle_len_med = np.median(np.hstack(result_lap[:, 0, 2]))

        print("Ranking the estimated streamlines...")
        estimated_bundle_idx_ranked = ranking_schema(estimated_bundle_idx,
                                                     min_cost_values)
        np.save('candidate_bundle_idx_ranked_lap.npy',
                estimated_bundle_idx_ranked)
        np.save('candidate_bundle_idx_lap.npy', estimated_bundle_idx)
        np.save('min_cost_values_lap.npy', min_cost_values)

        print("Extracting the estimated bundle...")
        estimated_bundle_idx_ranked_med = estimated_bundle_idx_ranked[
            0:int(example_bundle_len_med)]
        np.save('estimated_bundle_idx_lap.npy',
                estimated_bundle_idx_ranked_med)

        with open('config.json') as f:
            data = json.load(f)
            step_size = data["step_size"]

        save_bundle(estimated_bundle_idx_ranked_med, static_tractogram,
                    step_size, out_filename)

        return result_lap
    parser.add_argument('-lE',
                        nargs='?',
                        const=1,
                        default='',
                        help='Weight of the endpoint matrix')
    parser.add_argument('-lR',
                        nargs='?',
                        const=1,
                        default='',
                        help='Weight of the waypoint matrix')
    parser.add_argument('-out',
                        nargs='?',
                        const=1,
                        default='',
                        help='The output estimated bundle filename')
    args = parser.parse_args()

    result_lap = lap_single_example(args.moving, args.static, args.ex, args.lD,
                                    args.lE, args.lR)

    np.save('result_lap', result_lap)

    if args.out:
        estimated_bundle_idx = result_lap[0]
        with open('config.json') as f:
            data = json.load(f)
            step_size = data["step_size"]
        save_bundle(estimated_bundle_idx, args.static, step_size, args.out)

    sys.exit()
示例#3
0
def lap_multiple_examples(moving_tractograms_dir, static_tractogram, ex_dir,
                          lD, lE, lR, out_filename):
    """Code for LAP from multiple examples.
	"""
    moving_tractograms = os.listdir(moving_tractograms_dir)
    moving_tractograms.sort()
    examples = os.listdir(ex_dir)
    examples.sort()

    nt = len(moving_tractograms)
    ne = len(examples)

    #moving_tractograms = os.listdir(moving_tractograms_dir)
    #moving_tractograms.sort()
    #nt = len(moving_tractograms)
    #moving_tractograms = ['%s/' %moving_tractograms_dir + moving_tractograms[i] for i in range(nt)]

    #examples = os.listdir(ex_dir)
    #examples.sort()
    #ne = len(examples)
    #examples = ['%s/' %ex_dir + examples[i] for i in range(ne)]

    if nt != ne:
        print(
            "Error: number of moving tractograms differs from number of example bundles."
        )
        sys.exit()
    else:
        result_lap = []
        for i in range(nt):
            moving_tractogram = '%s/%s' % (moving_tractograms_dir,
                                           moving_tractograms[i])
            example = '%s/%s' % (ex_dir, examples[i])
            tmp = np.array([
                lap_single_example(moving_tractogram, static_tractogram,
                                   example, lD, lE, lR)
            ])
            result_lap.append(tmp)

        result_lap = np.array(result_lap)
        estimated_bundle_idx = np.hstack(result_lap[:, 0, 0])
        min_cost_values = np.hstack(result_lap[:, 0, 1])
        example_bundle_len_med = np.median(np.hstack(result_lap[:, 0, 2]))

        #result_lap = np.array(Parallel(n_jobs=-1)(delayed(lap_single_example)(moving_tractograms[i], static_tractogram, examples[i], lD, lE, lR) for i in range(nt)))

        #estimated_bundle_idx = np.hstack(result_lap[:,0])
        #min_cost_values = np.hstack(result_lap[:,1])
        #example_bundle_len_med = np.median(np.hstack(result_lap[:,2]))

        print("Ranking the estimated streamlines...")
        estimated_bundle_idx_ranked = ranking_schema(estimated_bundle_idx,
                                                     min_cost_values)

        print("Extracting the estimated bundle...")
        estimated_bundle_idx_ranked_med = estimated_bundle_idx_ranked[
            0:int(example_bundle_len_med)]

        with open('config.json') as f:
            data = json.load(f)
            step_size = data["step_size"]

        save_bundle(estimated_bundle_idx_ranked_med, static_tractogram,
                    step_size, out_filename)

        return result_lap