Skip to content

Commit

Permalink
calculate the distances instead of starting from a table
Browse files Browse the repository at this point in the history
  • Loading branch information
volker-baecker committed Nov 14, 2023
1 parent a470baa commit ac3b9bf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 34 deletions.
13 changes: 6 additions & 7 deletions calculate_hist_and_eCDF.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from ij import IJ
from fr.cnrs.mri.cialib.stats import Histogram
from fr.cnrs.mri.cialib.spatialstats import NearestNeighborEmpiricalCDF
from ij.measure import ResultsTable
from jarray import array

COLUMN = "V2"
TABLE = "ClosestDistanceCCUnit"
image = IJ.getImage()
nn = NearestNeighborEmpiricalCDF(image)
nn.calculateDistances()

table = ResultsTable.getResultsTable(TABLE)
data = table.getColumn(COLUMN)
data = array(list(data), 'f')

hist = Histogram(data, start=0, end=8, nBins=100)
hist = Histogram(nn.distances, start=0, end=max(nn.distances) + 1, nBins=12)
hist.autoBinning = False
hist.calculate()
hist.getPlot().show()
Expand Down
37 changes: 18 additions & 19 deletions db_scan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
DBSCAN-clustering of 3D-points.
(c) 2019-2022, INSERM
(c) 2019-2023, INSERM
written by Volker Baecker at Montpellier Ressources Imagerie, Biocampus Montpellier, INSERM, CNRS, University of Montpellier (www.mri.cnrs.fr)
The points must be in the X, Y, Z and NR columns
Expand All @@ -27,10 +27,10 @@


def main():
XColumn = 'X'
YColumn = 'Y'
ZColumn = 'Z'
NRColumn = 'NR'
XColumn = 'Centroid.X'
YColumn = 'Centroid.Y'
ZColumn = 'Centroid.Z'
NRColumn = 'Label'
minPts = 4
maxDist = 3
run(maxDist, minPts, XColumn, YColumn, ZColumn, NRColumn)
Expand Down Expand Up @@ -58,7 +58,7 @@ def pointList3DFromRT(XColumn='X', YColumn='Y', ZColumn='Z'):
'''
Create a list of 3D-coordinates from the ImageJ system results table and return it.
'''
rt = ResultsTable.getResultsTable()
rt = ResultsTable.getActiveTable()
xIndex = getColumnIndex(XColumn)
yIndex = getColumnIndex(YColumn)
zIndex = getColumnIndex(ZColumn)
Expand All @@ -82,25 +82,23 @@ def reportClustersAsTable(clusters, allPoints, XColumn='X', YColumn='Y', ZColumn
'''
Report the clustered and unclustered points in the tables 'clusters' and 'unclustered'.
'''
rt = ResultsTable()
rt_clustered = ResultsTable()
counter = 1;
clusterCounter = 1
clusteredPoints = []
for c in clusters:
for dp in c.getPoints():
rt.incrementCounter()
rt_clustered.incrementCounter()
p = dp.getPoint()
rt.addValue(NRColumn, counter)
rt.addValue(XColumn, p[0])
rt.addValue(YColumn, p[1])
rt.addValue(ZColumn, p[2])
rt.addValue("C", clusterCounter)
rt_clustered.addValue(NRColumn, counter)
rt_clustered.addValue(XColumn, p[0])
rt_clustered.addValue(YColumn, p[1])
rt_clustered.addValue(ZColumn, p[2])
rt_clustered.addValue("C", clusterCounter)
counter = counter + 1;
clusteredPoints.append([p[0], p[1], p[2]])
clusterCounter = clusterCounter + 1
rt.show("clusters")
win = WindowManager.getWindow("Results")
rt = win.getResultsTable()
rt = ResultsTable.getActiveTable()
X, Y, Z = getColumns(XColumn, YColumn, ZColumn)
if not rt.columnExists(NRColumn):
for i in range(0, len(X)):
Expand All @@ -118,13 +116,14 @@ def reportClustersAsTable(clusters, allPoints, XColumn='X', YColumn='Y', ZColumn
rt.addValue(ZColumn, p[2])
counter = counter + 1;
rt.show("unclustered")
WindowManager.setWindow(win)
rt_clustered.show("clusters")
WindowManager.getWindow("Results").close()


def getColumnIndex(column):
rt = ResultsTable.getResultsTable()
rt = ResultsTable.getActiveTable()
headings = rt.getHeadings()
for i, heading in enumerate(headings, start=1):
for i, heading in enumerate(headings, start=0):
if heading==column:
return i
return None
Expand Down
33 changes: 33 additions & 0 deletions tools/draw_clusters.ijm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var IS_SCALED = true;
var X_COLUMN = "Centroid.X";
var Y_COLUMN = "Centroid.Y";
var Z_COLUMN = "Centroid.Z";
var CLUSTER_COLUMN = "C";
var TABLE = "clusters"

run("Duplicate...", "duplicate");
run("Select All");
setBackgroundColor(0, 0, 0);
run("Clear", "stack");
run("Select None");

X = Table.getColumn(X_COLUMN, TABLE);
Y = Table.getColumn(Y_COLUMN, TABLE);
Z = Table.getColumn(Z_COLUMN, TABLE);
C = Table.getColumn(CLUSTER_COLUMN, TABLE);
for (i = 0; i < Table.size(TABLE); i++) {
x = X[i];
y = Y[i];
z = Z[i];
if (IS_SCALED) {
toUnscaled(x, y, z);
}
setSlice(z+1);
setPixel(x, y, C[i]);
}
setOption("ScaleConversions", false);
run("16-bit");
run("glasbey on dark");
run("Morphological Filters (3D)", "operation=Dilation element=Cube x-radius=2 y-radius=2 z-radius=2");
setSlice(1);
resetMinAndMax();
17 changes: 9 additions & 8 deletions tools/draw_nearest_neighbors.ijm
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
var MAX_DIST = 999999;
var IS_SCALED = false;
var X_COLUMN = "axis-2";
var Y_COLUMN = "axis-1";
var Z_COLUMN = "axis-0";
var IS_SCALED = true;
var X_COLUMN = "Centroid.X";
var Y_COLUMN = "Centroid.Y";
var Z_COLUMN = "Centroid.Z";
var LABEL_COLUMN = "Label";


drawNearestNeighborConnections();


function drawNearestNeighborConnections() {
inputStackID = getImageID();
Stack.getDimensions(width, height, channels, slices, frames);
Expand All @@ -14,19 +17,17 @@ function drawNearestNeighborConnections() {

table = Table.title;
duplicateTable(table, "green")
Table.applyMacro("Label = index + 1", "green");
duplicateTable("green", "red")
Table.applyMacro("Label = index + 1", "red");

run("3D Distances Closest", "image_a="+title+" image_b="+title+" number=2 distance=DistCenterCenterUnit distance_maximum="+MAX_DIST);
Table.rename("ClosestDistanceCCUnit", "neighbors");

labelRed = Table.getColumn("Label", "red");
labelRed = Table.getColumn(LABEL_COLUMN, "red");
xRed = Table.getColumn(X_COLUMN, "red");
yRed = Table.getColumn(Y_COLUMN, "red");
zRed = Table.getColumn(Z_COLUMN, "red");

labelGreen = Table.getColumn("Label", "green");
labelGreen = Table.getColumn(LABEL_COLUMN, "green");
xGreen = Table.getColumn(X_COLUMN, "green");
yGreen = Table.getColumn(Y_COLUMN, "green");
zGreen = Table.getColumn(Z_COLUMN, "green");
Expand Down

0 comments on commit ac3b9bf

Please sign in to comment.