|
1 | 1 | #@Context context
|
2 | 2 | #@UIService uiService
|
| 3 | +#@LogService logService |
3 | 4 |
|
4 | 5 | # NN_Distances.py
|
5 | 6 | # IJ BAR snippet https://github.com/tferr/Scripts/tree/master/Snippets
|
|
11 | 12 | # TF 20150810
|
12 | 13 |
|
13 | 14 | import math, sys
|
14 |
| -from ij import IJ |
15 | 15 | from bar import Utils
|
16 | 16 | import ij.measure.ResultsTable as RT
|
17 | 17 |
|
| 18 | +# Column headings listing x,y,z positions |
| 19 | +xHeading, yHeading, zHeading = "X", "Y", "Z" |
| 20 | + |
18 | 21 |
|
19 | 22 | 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") |
25 | 29 |
|
26 | 30 |
|
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) |
29 | 37 |
|
30 |
| -# Retrieve valid data from the Results table |
31 |
| -rt = Utils.getResultsTable(); |
32 | 38 |
|
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 |
39 | 48 |
|
40 |
| -if not None in (x, y): |
41 | 49 |
|
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...") |
47 | 55 | z = [0]*len(x)
|
48 |
| - |
49 |
| - # Calculate distances for all positions. Retrieve NNs distances |
| 56 | + # Calculate distances for all positions. Retrieve NNs |
50 | 57 | for i in range(len(x)):
|
51 | 58 | minDx = sys.maxint
|
52 | 59 | nearest = 0
|
53 | 60 | for j in range(len(x)):
|
54 |
| - if i==j: |
| 61 | + if i == j: |
55 | 62 | 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: |
61 | 65 | minDx = dst
|
62 | 66 | nearest = j+1
|
63 | 67 | rt.setValue("NN pair", i, nearest)
|
64 | 68 | rt.setValue("NN distance", i, minDx)
|
65 | 69 |
|
66 |
| - # Display appended results |
67 |
| - rt.showRowNumbers(True) |
68 |
| - rt.show("Results") |
69 | 70 |
|
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 | + |
71 | 86 |
|
72 |
| -else: |
73 |
| - IJ.error("Invalid Results Table","Data for X,Y positions not found.") |
| 87 | +main() |
0 commit comments