diff --git a/calculate_hist_and_eCDF.py b/calculate_hist_and_eCDF.py index a704a91..9c86aba 100644 --- a/calculate_hist_and_eCDF.py +++ b/calculate_hist_and_eCDF.py @@ -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() diff --git a/db_scan.py b/db_scan.py index a324d32..5e4f84e 100644 --- a/db_scan.py +++ b/db_scan.py @@ -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 @@ -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) @@ -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) @@ -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)): @@ -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 diff --git a/tools/draw_clusters.ijm b/tools/draw_clusters.ijm new file mode 100644 index 0000000..b6e9043 --- /dev/null +++ b/tools/draw_clusters.ijm @@ -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(); \ No newline at end of file diff --git a/tools/draw_nearest_neighbors.ijm b/tools/draw_nearest_neighbors.ijm index d406150..0dd3444 100644 --- a/tools/draw_nearest_neighbors.ijm +++ b/tools/draw_nearest_neighbors.ijm @@ -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); @@ -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");