<commit_before>from __future__ import division, print_function, absolute_import

from dipy.utils.six.moves import xrange

import numpy as np
import nose

from dipy.io.bvectxt import orientation_from_string
from dipy.tracking.utils import (affine_for_trackvis, connectivity_matrix,
                                 density_map, length, move_streamlines,
                                 ndbincount, reduce_labels,
                                 reorder_voxels_affine, seeds_from_mask,
                                 random_seeds_from_mask, target,
                                 target_line_based, unique_rows, near_roi,
                                 reduce_rois, path_length, flexi_tvis_affine,
                                 get_flexi_tvis_affine, _min_at,
                                 cluster_confidence)

from dipy.tracking.streamline import Streamlines

from dipy.tracking._utils import _to_voxel_coordinates

import dipy.tracking.metrics as metrix

from dipy.tracking.vox2track import streamline_mapping
import numpy.testing as npt
from numpy.testing import assert_array_almost_equal, assert_array_equal
from nose.tools import assert_equal, assert_raises, assert_true


def make_streamlines():
    streamlines = [np.array([[0, 0, 0],
                             [1, 1, 1],
                             [2, 2, 2],
                             [5, 10, 12]], 'float'),
                   np.array([[1, 2, 3],
                             [3, 2, 0],
                             [5, 20, 33],
                             [40, 80, 120]], 'float')]
    return streamlines


def test_cluster_confidence():
    # two identical streamlines should raise an error
    mysl = np.array([np.arange(10)] * 3).T
    test_streamlines = Streamlines([mysl])*2
    assert_raises(ValueError, cluster_confidence, test_streamlines)

    # 3 offset collinear streamlines
    test_streamlines = Streamlines([mysl])+[mysl+1]+[mysl+2]
    cci = cluster_confidence(test_streamlines)
    assert_equal(cci[0], cci[2])
    assert_true(cci[1] > cci[0])

    # 3 parallel streamlines
    mysl = np.zeros([10, 3])
    mysl[:, 0] = np.arange(10)
    mysl2 = mysl.copy()
    mysl2[:, 1] = 1
    mysl3 = mysl.copy()
    mysl3[:, 1] = 2
    mysl4 = mysl.copy()
    mysl4[:, 1] = 4
    mysl5 = mysl.copy()
    mysl5[:, 1] = 5000

    test_streamlines_p1 = Streamlines([mysl])+[mysl2]+[mysl3]
    test_streamlines_p2 = Streamlines([mysl])+[mysl3]+[mysl4]
    test_streamlines_p3 = Streamlines([mysl])+[mysl2]+[mysl3]+[mysl5]

    cci_p1 = cluster_confidence(test_streamlines_p1)
    cci_p2 = cluster_confidence(test_streamlines_p2)

    # test relative distance
    assert_array_equal(cci_p1, cci_p2*2)

    # test simple cci calculation
    expected_p1 = np.array([1/1+1/2, 1/1+1/1, 1/1+1/2])
    expected_p2 = np.array([1/2+1/4, 1/2+1/2, 1/2+1/4])
    assert_array_equal(expected_p1, cci_p1)
    assert_array_equal(expected_p2, cci_p2)

    # test power variable calculation (dropoff with distance)
    cci_p1_pow2 = cluster_confidence(test_streamlines_p1, power=2)
    expected_p1_pow2 = np.array([np.power(1/1, 2)+np.power(1/2, 2),
                                 np.power(1/1, 2)+np.power(1/1, 2),
                                 np.power(1/1, 2)+np.power(1/2, 2)])

    assert_array_equal(cci_p1_pow2, expected_p1_pow2)

    # test max distance (ignore distant sls)<commit_msg>combined array sequences instead of list manipulation<commit_after>from __future__ import division, print_function, absolute_import

from dipy.utils.six.moves import xrange

import numpy as np
import nose

from dipy.io.bvectxt import orientation_from_string
from dipy.tracking.utils import (affine_for_trackvis, connectivity_matrix,
                                 density_map, length, move_streamlines,
                                 ndbincount, reduce_labels,
                                 reorder_voxels_affine, seeds_from_mask,
                                 random_seeds_from_mask, target,
                                 target_line_based, unique_rows, near_roi,
                                 reduce_rois, path_length, flexi_tvis_affine,
                                 get_flexi_tvis_affine, _min_at,
                                 cluster_confidence)

from dipy.tracking.streamline import Streamlines

from dipy.tracking._utils import _to_voxel_coordinates

import dipy.tracking.metrics as metrix

from dipy.tracking.vox2track import streamline_mapping
import numpy.testing as npt
from numpy.testing import assert_array_almost_equal, assert_array_equal
from nose.tools import assert_equal, assert_raises, assert_true

import nibabel as nib


def make_streamlines():
    streamlines = [np.array([[0, 0, 0],
                             [1, 1, 1],
                             [2, 2, 2],
                             [5, 10, 12]], 'float'),
                   np.array([[1, 2, 3],
                             [3, 2, 0],
                             [5, 20, 33],
                             [40, 80, 120]], 'float')]
    return streamlines


def test_cluster_confidence():
    # two identical streamlines should raise an error
    mysl = np.array([np.arange(10)] * 3).T
    test_streamlines = Streamlines([mysl]).append([mysl])
    assert_raises(ValueError, cluster_confidence, test_streamlines)

    # 3 offset collinear streamlines
    test_streamlines = Streamlines([mysl]).append([mysl+1]).append([mysl+2])
    cci = cluster_confidence(test_streamlines)
    assert_equal(cci[0], cci[2])
    assert_true(cci[1] > cci[0])

    # 3 parallel streamlines
    mysl = np.zeros([10, 3])
    mysl[:, 0] = np.arange(10)
    mysl2 = mysl.copy()
    mysl2[:, 1] = 1
    mysl3 = mysl.copy()
    mysl3[:, 1] = 2
    mysl4 = mysl.copy()
    mysl4[:, 1] = 4
    mysl5 = mysl.copy()
    mysl5[:, 1] = 5000

    test_streamlines_p1 = Streamlines([mysl]).append([mysl2]).append([mysl3])
    test_streamlines_p2 = Streamlines([mysl]).append([mysl3]).append([mysl4])
    test_streamlines_p3 = Streamlines([mysl]).append(
        [mysl2]).append([mysl3]).append([mysl5])

    cci_p1 = cluster_confidence(test_streamlines_p1)
    cci_p2 = cluster_confidence(test_streamlines_p2)

    # test relative distance
    assert_array_equal(cci_p1, cci_p2*2)

    # test simple cci calculation
    expected_p1 = np.array([1/1+1/2, 1/1+1/1, 1/1+1/2])
    expected_p2 = np.array([1/2+1/4, 1/2+1/2, 1/2+1/4])
    assert_array_equal(expected_p1, cci_p1)
    assert_array_equal(expected_p2, cci_p2)

    # test power variable calculation (dropoff with distance)
    cci_p1_pow2 = cluster_confidence(test_streamlines_p1, power=2)
    expected_p1_pow2 = np.array([np.power(1/1, 2)+np.power(1/2, 2),
                                 np.power(1/1, 2)+np.power(1/1, 2),
                                 np.power(1/1, 2)+np.power(1/2, 2)])

    assert_array_equal(cci_p1_pow2, expected_p1_pow2)

    # test max distance (ignore distant sls)
{'fraction_non_alphanumeric': 0.10741762229641458, 'fraction_numerical': 0.04195093627522137, 'mean_word_length': 2.3626159102000974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607908', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2278, 'n_words': 454}