This file is indexed.

/usr/share/doc/dipy/examples/segment_clustering_metrics.py is in python-dipy 0.10.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
===========================================
Tractography Clustering - Available Metrics
===========================================

This page lists available metrics that can be used by the tractography
clustering framework. For every metric a brief description is provided
explaining: what it does, when it's useful and how to use it. If you are not
familiar with the tractography clustering framework, check this tutorial
:ref:`clustering-framework`.

.. contents:: Available Metrics
    :local:
    :depth: 1

**Note**:
All examples assume a function `get_streamlines` exists. We defined here a
simple function to do so. It imports the necessary modules and load a small
streamline bundle.
"""


def get_streamlines():
    from nibabel import trackvis as tv
    from dipy.data import get_data

    fname = get_data('fornix')
    streams, hdr = tv.read(fname)
    streamlines = [i[0] for i in streams]
    return streamlines

"""
.. _clustering-examples-AveragePointwiseEuclideanMetric:

Average of Pointwise Euclidean Metric
=====================================
**What:** Instances of `AveragePointwiseEuclideanMetric` first compute the
pointwise Euclidean distance between two sequences *of same length* then
return the average of those distances. This metric takes as inputs two features
that are sequences containing the same number of elements.

**When:** By default the `QuickBundles` clustering will resample your
streamlines on-the-fly so they have 12 points. If for some reason you want
to avoid this and you made sure all your streamlines have already the same
number of points, you can manually provide an instance of
`AveragePointwiseEuclideanMetric` to `QuickBundles`. Since the default
`Feature` is the `IdentityFeature` the streamlines won't be resampled thus
saving some computational time.

**Note:** Inputs must be sequences of same length.
"""

from dipy.viz import fvtk
from dipy.segment.clustering import QuickBundles
from dipy.segment.metric import AveragePointwiseEuclideanMetric

# Get some streamlines.
streamlines = get_streamlines()  # Previously defined.

# Make sure our streamlines have the same number of points.
from dipy.tracking.streamline import set_number_of_points
streamlines = set_number_of_points(streamlines, nb_points=12)

# Create the instance of `AveragePointwiseEuclideanMetric` to use.
metric = AveragePointwiseEuclideanMetric()
qb = QuickBundles(threshold=10., metric=metric)
clusters = qb.cluster(streamlines)

print("Nb. clusters:", len(clusters))
print("Cluster sizes:", map(len, clusters))

"""

::

    Nb. clusters: 4

    Cluster sizes: [64, 191, 44, 1]

.. _clustering-examples-SumPointwiseEuclideanMetric:

Sum of Pointwise Euclidean Metric
=================================
**What:** Instances of `SumPointwiseEuclideanMetric` first compute the
pointwise Euclidean distance between two sequences *of same length* then
return the sum of those distances.

**When:** This metric mainly exists because it is used internally by
`AveragePointwiseEuclideanMetric`.

**Note:** Inputs must be sequences of same length.
"""

from dipy.segment.clustering import QuickBundles
from dipy.segment.metric import SumPointwiseEuclideanMetric

# Get some streamlines.
streamlines = get_streamlines()  # Previously defined.

# Make sure our streamlines have the same number of points.
from dipy.tracking.streamline import set_number_of_points
nb_points = 12
streamlines = set_number_of_points(streamlines, nb_points=nb_points)

# Create the instance of `SumPointwiseEuclideanMetric` to use.
metric = SumPointwiseEuclideanMetric()
qb = QuickBundles(threshold=10.*nb_points, metric=metric)
clusters = qb.cluster(streamlines)

print("Nb. clusters:", len(clusters))
print("Cluster sizes:", map(len, clusters))

"""

::

    Nb. clusters: 4

    Cluster sizes: [64, 191, 44, 1]

.. _clustering-examples-MinimumAverageDirectFlipMetric:

Minimum Average Direct Flip Metric (MDF)
========================================
**What:** It is the metric used in the QuickBundles algorithm [Garyfallidis12]_.
Instances of `MinimumAverageDirectFlipMetric` first compute the
direct distance *d1* by taking the average of the pointwise
Euclidean distances between two sequences *of same length*. Reverse
one of the two sequences and compute the flip distance *d2* using the same
approach as for *d1*. Then, return the minimum between *d1* and *d2*.

**When:** This metric mainly exists because it is used internally by
`AveragePointwiseEuclideanMetric`.

**Note:** Inputs must be sequences of same length.
"""

from dipy.segment.metric import MinimumAverageDirectFlipMetric

# Get some streamlines.
streamlines = get_streamlines()  # Previously defined.

# Make sure our streamlines have the same number of points.
from dipy.tracking.streamline import set_number_of_points
streamlines = set_number_of_points(streamlines, nb_points=20)

# Create the instance of `MinimumAverageDirectFlipMetric` to use.
metric = MinimumAverageDirectFlipMetric()
d = metric.dist(streamlines[0], streamlines[1])

print("MDF distance between the first two streamlines: ", d)

"""

::

    MDF distance between the first two streamlines: 11.681308709622542

.. _clustering-examples-MinimumAverageDirectFlipMetric:

Cosine Metric
=============
**What:** Instances of `CosineMetric` compute the cosine distance between two
vectors (for more information see the
`wiki page <https://en.wikipedia.org/wiki/Cosine_similarity>`_).

**When:** This metric can be useful when you *only* need information about the
orientation of a streamline.

**Note:** Inputs must be vectors (i.e. 1D array).
"""

import numpy as np
from dipy.viz import fvtk
from dipy.segment.clustering import QuickBundles
from dipy.segment.metric import VectorOfEndpointsFeature
from dipy.segment.metric import CosineMetric

# Get some streamlines.
streamlines = get_streamlines()  # Previously defined.

feature = VectorOfEndpointsFeature()
metric = CosineMetric(feature)
qb = QuickBundles(threshold=0.1, metric=metric)
clusters = qb.cluster(streamlines)

# Color each streamline according to the cluster they belong to.
colormap = fvtk.create_colormap(np.arange(len(clusters)))
colormap_full = np.ones((len(streamlines), 3))
for cluster, color in zip(clusters, colormap):
    colormap_full[cluster.indices] = color

# Visualization
ren = fvtk.ren()
fvtk.clear(ren)
ren.SetBackground(0, 0, 0)
fvtk.add(ren, fvtk.streamtube(streamlines, colormap_full))
fvtk.record(ren, n_frames=1, out_path='cosine_metric.png', size=(600, 600))

"""
.. figure:: cosine_metric.png
   :align: center

   **Showing the streamlines colored according to their orientation**.

.. include:: ../links_names.inc

.. [Garyfallidis12] Garyfallidis E. et al., QuickBundles a method for
                    tractography simplification, Frontiers in Neuroscience, vol
                    6, no 175, 2012.
"""