0

I am working on a code for a colleague where I am trying to compute the Bond Orientational Order for a cluster of particles. To make life easier for processing a large number of images I wish to implement a GUI (although it is not easy in Mathematica).

I have failed to understand why for the code below, CreateDocument fails to create a new notebook if the ExpressionCell has RegionMember/@poly as argument. I can create the document when the expression is ExpressionCell[poly]and the cell displays the list of polygons. It seems like RegionMember/@poly does not evaluate whereas poly does.

Note: There is no minimal example that I can post here for the purpose of the code I am dealing with. My actual code for packagedFunction goes a bit beyond to generate the final results but I am posting it till the point where the problem originates.

for imgs kindly use the image:

enter image description here

Code

CreatePalette[
DynamicModule[{segmentImage, showFunc, deleteFalsePts, addPts, 
packagedFunction, img = imgs[[1]], ovimg, data},
Grid[{
{Button["create Voronoi", packagedFunction[data]]} ,
{Button["particle centroids", CreateDocument[{
    TextCell["Coordinate List", "Title"],
    TextCell[
     Row[{"Report generated @ ", DateString[], "\n", 
       "Particles #: ", Length@data}], "Text"],
    ExpressionCell[data, "Output"]
    }]
  ]} ,
{Pane[
  Dynamic@If[Head[img] === Image,
    LocatorPane[
     Dynamic@data, ovimg, LocatorAutoCreate -> All,
     Appearance -> Style["*", 20, Red]]
    ]
  ]
 }
}],
Initialization :> (
segmentImage[img_, circ_: 0.95, thresh_: {10, 50}] := 
 SelectComponents[
     ColorNegate@
      DeleteSmallComponents@
       MorphologicalBinarize@
        img, (First[thresh] <= #Count < 
         Last[thresh] && #Circularity > circ) &] // 
    MorphologicalComponents // 
   ComponentMeasurements[#, "Centroid"] & // Values;

data = segmentImage@img;

showFunc[img_, pts_] := 
 Show[img, 
  Graphics[{XYZColor[0, 0, 1, 0.4], Thickness[0.005], 
    Circle[#, 8] & /@ pts}]];

ovimg = showFunc[img, data];

packagedFunction[pts_?(Length@# > 1 &)] := 
 Block[{delMesh, vertexcoords, vertexconn, cellNeighCoords, angles,
   anglesC, poly, pos, polyOrdered, colourVM},
  delMesh = DelaunayMesh@pts;
  vertexcoords = <|delMesh["VertexCoordinateRules"]|>;
  vertexconn = delMesh["VertexVertexConnectivityRules"];
  cellNeighCoords = With[{vertexpts = vertexcoords},
    FlattenAt[{Lookup[vertexpts, Keys@#], 
        Lookup[vertexpts, Values@#]}, {2}] & /@ vertexconn
    ];
  angles = (Abs[(Plus @@ Exp[6.0 I #])/Length@#]) & /@ 
    Map[Map[x \[Function] VectorAngle[{1, 0}, First[#] - x ], 
       Rest@#] &, cellNeighCoords];
  anglesC = ColorData["TemperatureMap", #] & /@ angles;
  poly = MeshPrimitives[VoronoiMesh[pts, None], 2];
  CreateDocument[{ExpressionCell[RegionMember /@ poly, Automatic]}] (* PROBLEM HERE? *)
  ];
)
], WindowTitle -> "BondOrientationalOrder"]
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42
  • Is gui context related, have you tried to reproduce the problem just by running your procedure? And have you tried stripping all those image related procedures? The more you narrow it down the more likely you will focus attention? – Kuba Apr 29 '18 at 11:18
  • @Kuba thanks. this does not happen with the non-GUI based version. It is in the context of GUI that RegionMember does not evaluate – Ali Hashmi Apr 29 '18 at 11:21
  • @Kuba does not ExpressionCell handle any type of data Graphics, Text or even RegionMemberFunction? – Ali Hashmi Apr 29 '18 at 11:22
  • 1
    As linked topic states, Method -> "Queued" will fix your problem. Since the calculation is long, please take a look at Button action monitored with progress bar to learn how to show to users that something is going on. – Kuba Apr 29 '18 at 17:18
  • @Kuba thanks looking at it after a long time. i think it did the job ! – Ali Hashmi May 08 '18 at 15:43

0 Answers0