Skip to content

Commit aec7a3f

Browse files
committed
Polish code
1 parent 16fc123 commit aec7a3f

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

Snippets/NN_Distances.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#@Context context
22
#@UIService uiService
3+
#@LogService logService
34

45
# NN_Distances.py
56
# IJ BAR snippet https://github.com/tferr/Scripts/tree/master/Snippets
@@ -11,63 +12,76 @@
1112
# TF 20150810
1213

1314
import math, sys
14-
from ij import IJ
1515
from bar import Utils
1616
import ij.measure.ResultsTable as RT
1717

18+
# Column headings listing x,y,z positions
19+
xHeading, yHeading, zHeading = "X", "Y", "Z"
20+
1821

1922
def plot_distributions():
20-
from bar import Runner
21-
runner = Runner(context)
22-
runner.runIJ1Macro("Data_Analysis/Distribution_Plotter.ijm", "NN distance")
23-
if not runner.scriptLoaded():
24-
uiService.showDialog("Distribution of NN distances not plotted.\nCheck console for details", "Error")
23+
from bar import Runner
24+
runner = Runner(context)
25+
runner.runIJ1Macro("Data_Analysis/Distribution_Plotter.ijm", "NN distance")
26+
if not runner.scriptLoaded():
27+
uiService.showDialog("Distribution of NN distances not plotted.\n"
28+
+ "Check console for details", "Error")
2529

2630

27-
# Specify column headings listing x,y,z positions
28-
xHeading, yHeading, zHeading = "X", "Y", "Z"
31+
def distance(x1, y1, z1, x2, y2, z2):
32+
""" Retrieves the distances betwenn two points """
33+
dx = (x1-x2)**2
34+
dy = (y1-y2)**2
35+
dz = (z1-z2)**2
36+
return math.sqrt(dx+dy+dz)
2937

30-
# Retrieve valid data from the Results table
31-
rt = Utils.getResultsTable();
3238

33-
# Retrive x,y positions
34-
try:
35-
x = rt.getColumn(rt.getColumnIndex(xHeading))
36-
y = rt.getColumn(rt.getColumnIndex(yHeading))
37-
except:
38-
x = y = None
39+
def getXYZPositons(rt, xcol_header, ycol_header, zcol_header):
40+
""" Retrieves valid data from the Results table """
41+
try:
42+
x = y = z = None
43+
x = rt.getColumn(rt.getColumnIndex(xcol_header))
44+
y = rt.getColumn(rt.getColumnIndex(ycol_header))
45+
z = rt.getColumn(rt.getColumnIndex(zcol_header))
46+
finally:
47+
return x, y, z
3948

40-
if not None in (x, y):
4149

42-
# Retrive z positions. Ignore positions if column is not found
43-
try:
44-
z = rt.getColumn(rt.getColumnIndex(zHeading))
45-
except:
46-
IJ.log("Z-column not found: Assuming 2D distances...")
50+
def calcNNDistances(rt, x, y, z=None):
51+
""" Calculates NN distances and adds them to the Results table """
52+
# Ignore Z positions?
53+
if z is None:
54+
logService.info("NN: Assuming 2D distances...")
4755
z = [0]*len(x)
48-
49-
# Calculate distances for all positions. Retrieve NNs distances
56+
# Calculate distances for all positions. Retrieve NNs
5057
for i in range(len(x)):
5158
minDx = sys.maxint
5259
nearest = 0
5360
for j in range(len(x)):
54-
if i==j:
61+
if i == j:
5562
continue
56-
dx = (x[i]-x[j])**2
57-
dy = (y[i]-y[j])**2
58-
dz = (z[i]-z[j])**2
59-
dst = math.sqrt(dx+dy+dz)
60-
if dst>0 and dst<minDx:
63+
dst = distance(x[i], x[j], y[i], y[j], z[i], z[j])
64+
if dst > 0 and dst < minDx:
6165
minDx = dst
6266
nearest = j+1
6367
rt.setValue("NN pair", i, nearest)
6468
rt.setValue("NN distance", i, minDx)
6569

66-
# Display appended results
67-
rt.showRowNumbers(True)
68-
rt.show("Results")
6970

70-
plot_distributions()
71+
def main():
72+
rt = Utils.getResultsTable()
73+
if rt is None:
74+
return
75+
x, y, z = getXYZPositons(rt, xHeading, yHeading, zHeading)
76+
if not None in (x, y):
77+
# Do the calculations and sisplay appended results
78+
calcNNDistances(rt, x, y, z)
79+
rt.showRowNumbers(True)
80+
rt.show("Results")
81+
plot_distributions()
82+
else:
83+
uiService.showDialog("Data for X,Y positions not found.",
84+
"Invalid Results Table")
85+
7186

72-
else:
73-
IJ.error("Invalid Results Table","Data for X,Y positions not found.")
87+
main()

0 commit comments

Comments
 (0)