https://www.slicer.org/w/api.php?action=feedcontributions&user=76.111.179.75&feedformat=atom
Slicer Wiki - User contributions [en]
2024-03-29T08:19:08Z
User contributions
MediaWiki 1.33.0
https://www.slicer.org/w/index.php?title=Slicer3:Python:DemianExamples&diff=10782
Slicer3:Python:DemianExamples
2009-09-27T16:22:28Z
<p>76.111.179.75: 8</p>
<hr />
<div>__TOC__<br />
<br />
Very good site. Thanks!, http://vapajau.angelfire.com/you-play59/map.html 48 delay fin, 4370, http://jeatara.angelfire.com/dish-recd9/map.html southwest egg roll, >:]], http://wkbwcql.angelfire.com/muffin-m13/map.html easy fried chicken, =(((, http://afixodk.angelfire.com/preschoof6/map.html chronotropic definition, yip, http://usieiil.angelfire.com/team-usa49/map.html definition edtv, 819797,<br />
<br />
Very good site. Thanks!, http://dnaoiou.angelfire.com/teaching67/map.html cherry jones picture, %-[[[, http://enatauk.angelfire.com/oz-bar-h69/map.html banana pudding with sour cream, 8DDD, http://ykjebdb.angelfire.com/center-fcd/map.html accessory ion paintball, >:P, http://hnhkdnj.angelfire.com/cucumberd3/map.html realtor school virginia, 3180, http://tuauowu.angelfire.com/fine-art23/map.html c m production, :-(((,<br />
<br />
=K-Medoids Fiber Clustering=<br />
<pre><br />
XML = """<?xml version="1.0" encoding="utf-8"?><br />
<executable><br />
<br />
<category>Demo Scripted Modules</category><br />
<title>K-Medoids fiber clustering</title><br />
<description><br />
Fiber Clustering simple K-Medoids <br />
</description><br />
<version>1.0</version><br />
<documentation-url></documentation-url><br />
<license></license><br />
<contributor>Demian Wassermann</contributor><br />
<br />
<parameters><br />
<label>IO</label><br />
<description>Input/output parameters</description><br />
<br />
<geometry type = "fiberbundle" ><br />
<name>inputFiberBundle</name><br />
<longflag>inputFiberBundle</longflag><br />
<label>Input Fiber Bundle</label><br />
<channel>input</channel><br />
<description>Input bundle</description><br />
</geometry><br />
<br />
<geometry ><br />
<name>outputFiberBundle</name><br />
<longflag>outputFiberBundle</longflag><br />
<label>Output Fiber Bundle</label><br />
<channel>output</channel><br />
<description>Clustered bundle</description><br />
</geometry><br />
<br />
<integer><br />
<name>numberOfClusters</name><br />
<longflag>numberOfClusters</longflag><br />
<label>Number of clusters for K-Medoids</label><br />
<default>5</default><br />
<step>1</step><br />
<channel>input</channel><br />
<constraints><br />
<minimum>2</minimum><br />
<maximum>100</maximum><br />
</constraints><br />
</integer><br />
<br />
</parameters><br />
<parameters advanced="true"><br />
<label>Advanced</label><br />
<integer><br />
<name>subsampling</name><br />
<longflag>subsampling</longflag><br />
<label>Number of fiber points to keep</label><br />
<default>15</default><br />
<step>1</step><br />
<channel>input</channel><br />
<constraints><br />
<minimum>2</minimum><br />
<maximum>1000</maximum><br />
</constraints><br />
</integer><br />
<integer><br />
<name>minimumFiberLength</name><br />
<longflag>minimumFiberLength</longflag><br />
<label>minimum fiber length to consider valid</label><br />
<default>15</default><br />
<step>1</step><br />
<channel>input</channel><br />
<constraints><br />
<minimum>2</minimum><br />
<maximum>1000</maximum><br />
</constraints><br />
</integer><br />
</parameters><br />
<br />
</executable><br />
"""<br />
<br />
from Slicer import slicer<br />
import numpy<br />
<br />
# Warning, this example needs the package Pycluster <br />
# http://bonsai.ims.u-tokyo.ac.jp/~mdehoon/software/cluster/software.htm#pycluster<br />
import Pycluster<br />
<br />
<br />
<br />
<br />
def Execute (inputFiberBundle="", outputFiberBundle="", numberOfClusters=2, subsampling=15, minimumFiberLength=15 ):<br />
<br />
scene = slicer.MRMLScene<br />
<br />
inputFiberBundleNode = scene.GetNodeByID(inputFiberBundle)<br />
outputFiberBundleNode = scene.GetNodeByID(outputFiberBundle)<br />
<br />
<br />
#Prepare the output fiber bundle and the Arrays for the atlas labeling and clustering<br />
<br />
clusters = setupTheOutputNode( inputFiberBundleNode, outputFiberBundleNode )<br />
clusters_array = clusters.ToArray().squeeze()<br />
<br />
#Get the fibers form the Polydata and susbsample them<br />
fibers, lines = fibers_from_vtkPolyData( inputFiberBundleNode.GetPolyData(), minimumFiberLength )<br />
<br />
subsampledFibers = []<br />
<br />
for fiber in fibers:<br />
subsampledFibers.append( fiber[::max( len(fiber)/subsampling, len(fiber) ) ] )<br />
<br />
<br />
#Generate the distance matrix<br />
distanceMatrix = numpy.zeros( (len(fibers),len(fibers)), dtype=float )<br />
for i in xrange( len(fibers) ):<br />
for j in xrange( 0, i):<br />
distanceMatrix[ i, j ] = dist_hausdorff_min( subsampledFibers[i], subsampledFibers[j] )<br />
distanceMatrix[ j, i ] = distanceMatrix[ i, j ] <br />
<br />
<br />
<br />
<br />
#Perform the clustering<br />
fiberClusters = renumberLabels(Pycluster.kmedoids( distanceMatrix, numberOfClusters, npass=100 )[0])<br />
print fiberClusters <br />
clusters_array[:]=0<br />
<br />
<br />
for i in xrange(len(lines)):<br />
clusters_array[ lines[i] ] = fiberClusters[i]<br />
<br />
clusters.Modified()<br />
<br />
<br />
dist2 = lambda i,j : numpy.sqrt(((i-j)**2).sum(j.ndim-1))<br />
dist_hausdorff_asym_mean = lambda i,j: numpy.apply_along_axis( lambda k: dist2(k,j).min(), 1,i).mean()<br />
dist_hausdorff_min = lambda i,j : numpy.min(dist_hausdorff_asym_mean(i,j),dist_hausdorff_asym_mean(j,i))<br />
<br />
<br />
<br />
def fibers_from_vtkPolyData(vtkPolyData, minimumFiberLength):<br />
#Fibers and Lines are the same thing<br />
<br />
lines = vtkPolyData.GetLines().GetData().ToArray().squeeze()<br />
points = vtkPolyData.GetPoints().GetData().ToArray()<br />
<br />
fibersList = []<br />
linesList = []<br />
actualLineIndex = 0<br />
numberOfFibers = vtkPolyData.GetLines().GetNumberOfCells()<br />
for l in xrange( numberOfFibers ):<br />
if lines[actualLineIndex]>minimumFiberLength:<br />
fibersList.append( points[ lines[actualLineIndex+1: actualLineIndex+lines[actualLineIndex]+1] ] )<br />
linesList.append( lines[actualLineIndex+1: actualLineIndex+lines[actualLineIndex]+1] )<br />
actualLineIndex += lines[actualLineIndex]+1<br />
<br />
return fibersList, linesList<br />
<br />
def setupTheOutputNode( inputFiberBundleNode, outputFiberBundleNode ):<br />
if ( outputFiberBundleNode.GetPolyData()==[] ):<br />
outputFiberBundleNode.SetAndObservePolyData(slicer.vtkPolyData())<br />
<br />
outputPolyData = outputFiberBundleNode.GetPolyData()<br />
outputPolyData.SetPoints( inputFiberBundleNode.GetPolyData().GetPoints() )<br />
outputPolyData.SetLines( inputFiberBundleNode.GetPolyData().GetLines() )<br />
outputPolyData.Update()<br />
<br />
<br />
clusters = outputFiberBundleNode.GetPolyData().GetPointData().GetScalars('Cluster')<br />
if (clusters==[] or clusters.GetNumberOfTuples()!=outputPolyData.GetPoints().GetNumberOfPoints() ):<br />
clusters = slicer.vtkUnsignedIntArray()<br />
clusters.SetNumberOfComponents(1)<br />
clusters.SetNumberOfTuples( outputPolyData.GetPoints().GetNumberOfPoints() )<br />
clusters.SetName('Cluster')<br />
outputPolyData.GetPointData().AddArray( clusters )<br />
<br />
return clusters<br />
<br />
def renumberLabels(labelArray):<br />
newLabeling=[]<br />
for a in labelArray:<br />
if not(a in newLabeling):<br />
newLabeling.append(a)<br />
<br />
newLabelArray=labelArray.copy()<br />
for i in range(len(labelArray)):<br />
newLabelArray[i]=newLabeling.index(labelArray[i])+1<br />
<br />
return newLabelArray<br />
</pre></div>
76.111.179.75