KDTreee help file review

nix
Gerard 6 years ago
parent d5f692cf0e
commit c3e90529aa

@ -56,60 +56,50 @@ A function that will run when the query returns, whose argument is an array of d
EXAMPLES:: EXAMPLES::
code:: code::
//Make some 2D points and place into a dataset
s.reboot;
// Make a dataset of random 23D points
s.boot;
( (
~points = 100.collect{ [ 1.0.linrand,1.0.linrand] };
~dataset= FluidDataSet(s,\kdtree_help_rand2d);
~tmpbuf = Buffer.alloc(s,2);
fork{ fork{
s.bind{ ~ds = FluidDataSet.new(s,\kdtree_help_rand2d);
~dataset.ready.wait; d = Dictionary.with(
~points.do{|x,i| *[\cols -> 2,\data -> Dictionary.newFrom(
(""++(i+1)++"/100").postln; 100.collect{|i| [i, [ 1.0.linrand,1.0.linrand]]}.flatten)]);
~tmpbuf.setn(0,x); s.sync;
~dataset.addPoint(i,~tmpbuf); ~ds.load(d, {~ds.print});
s.sync
};
"Data loaded".postln;
}
} }
) )
// Make a new tree, and fit it to the dataset // Make a new tree, and fit it to the dataset
(
fork{
~tree = FluidKDTree(s); ~tree = FluidKDTree(s);
~tree.ready.wait;
s.sync;
~tree.fit(~dataset);
}
)
//Dims of tree should match dataset //Fit it to the dataset
~tree.cols ~tree.fit(~ds);
//Return labels of k nearest points to new data // Should be 100 points, 2 columns
( ~tree.size;
~testpoint = [ 1.0.linrand,1.0.linrand ]; ~tree.cols;
("\n\nTest point:" + ~testpoint).postln;
~tmpbuf.setn(0,~testpoint);
fork{ //Return labels of k nearest points to a new point
~tree.kNearest(~tmpbuf,5, { |a| ~p = [ 1.0.linrand,1.0.linrand ];
("Labels of nearest points" + a).postln; ~tmpbuf = Buffer.loadCollection(s, ~p, 1, {
"Nearest points".postln; ~tree.kNearest(~tmpbuf,5, { |a|~nearest = a;})
a.do{|l|
~dataset.getPoint(l,~tmpbuf,action:{
~tmpbuf.loadToFloatArray(action:{ |point|
point.postln;
})
}); });
// Labels of nearest points
~nearest.postln;
// Values
fork{
~nearest.do{|n|
~ds.getPoint(n, ~tmpbuf, {~tmpbuf.getn(0, 2, {|x|x.postln})});
s.sync; s.sync;
} }
});
} }
)
//or the distances //Distances of the nearest points
~tree.kNearestDist(~tmpbuf, 5, { |a| a.postln }); ~tree.kNearestDist(~tmpbuf, 5, { |a| a.postln });
:: ::

Loading…
Cancel
Save