Commit 9d550a5d authored by Edward Andò's avatar Edward Andò
Browse files

Resolves #157 in grid.py for spam-ldic

parent 12c7a0a4
Pipeline #50379 passed with stages
in 23 minutes and 35 seconds
......@@ -403,17 +403,13 @@ if mpiRank == boss or not mpi:
# 2020-07-05 try applying F to im1 this is expected to help with pixel searching
PhiNoDisp = PhiField[label].copy()
PhiNoDisp[0:3,-1] = 0.0
imagette1def = spam.DIC.applyPhi(imagette1, PhiNoDisp)
imagette1def = spam.DIC.applyPhi(imagette1, PhiNoDisp)
# Deform imagette1, using relative COM as point of application
# EA: Padding here might be useful for badly shaped grains
imagette1def = spam.DIC.applyPhi(imagette1, PhiNoDisp, PhiPoint=getLabel['centreOfMassREL'])
#imagette1defCrop = imagette1def[args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN,
#args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN, # This is to remove edge artefacts of applyPhi
#args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN]
#imagette1crop = imagette1[args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN,
#args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN, # This is to remove edge artefacts of applyPhi
#args.LABEL_CORRELATE_MARGIN:-args.LABEL_CORRELATE_MARGIN]
if args.MASK:
imagette1toCorrelate = imagette1def.copy()
......
......@@ -350,7 +350,6 @@ class TestFunctionDVC(unittest.TestCase):
for i in range(3):
self.assertAlmostEqual(t7[c][i] - t[c][i], 0, places=1)
def test_pixelSearch(self):
tVector = numpy.random.randint(1, 4, 3)
# print("test_pixelSearch(): initial random displacement:", tVector)
......@@ -404,7 +403,7 @@ class TestFunctionDVC(unittest.TestCase):
self.assertEqual((1, 4, 4), nodesDim2D)
def test_pixelSearchOnGrid(self):
# don't pass optional paremeters
# don't pass optional parameters
nodePositions = spam.DIC.grid.makeGrid(im.shape, 10)[0]
Phi = spam.deformation.computePhi({'t': [0, 2, 1]})
imDef = spam.DIC.applyPhi(im, Phi=Phi)
......@@ -416,19 +415,40 @@ class TestFunctionDVC(unittest.TestCase):
self.assertEqual(ps["PhiField"][:, 1, -1].sum(), nodePositions.shape[0] * 2)
self.assertEqual(ps["PhiField"][:, 2, -1].sum(), nodePositions.shape[0])
# pass default optional paremeters
# pass default optional parameters
ps2 = spam.DIC.grid.pixelSearchOnGrid(im, imDef,
nodePositions, 5,
searchRange={"zRange": [0, 0], "yRange": [-3, 3], "xRange": [-3, 3]},
im1mask=numpy.ones_like(im),
PhiField=numpy.zeros((nodePositions.shape[0], 4, 4)),
minMaskCoverage=0.5,
greyThreshold=[-numpy.inf, numpy.inf])
nodePositions, 5,
searchRange={"zRange": [0, 0], "yRange": [-3, 3], "xRange": [-3, 3]},
im1mask=numpy.ones_like(im),
minMaskCoverage=0.5,
greyThreshold=[-numpy.inf, numpy.inf])
self.assertEqual(ps2["PhiField"][:, 0, -1].sum(), 0)
self.assertEqual(ps2["PhiField"][:, 1, -1].sum(), nodePositions.shape[0] * 2)
self.assertEqual(ps2["PhiField"][:, 2, -1].sum(), nodePositions.shape[0])
# small rotation to test correct creation of imagette1def
nodePositions = spam.DIC.grid.makeGrid(im.shape, 10)[0]
Phi = spam.deformation.computePhi({'r': [2, 0, 0]})
imDef = spam.DIC.applyPhi(im, Phi=Phi)
# Compute PhiField
PhiField = numpy.zeros((nodePositions.shape[0], 4, 4))
for nodeNumber, nodePosition in enumerate(nodePositions):
# I
PhiField[nodeNumber] = numpy.eye(4)
# F
PhiField[nodeNumber, 0:3, 0:3] = Phi[0:3, 0:3]
# t
PhiField[nodeNumber, 0:3, -1 ] = spam.deformation.decomposePhi(Phi.copy(), PhiCentre=imCentre, PhiPoint=nodePosition)['t']
ps2 = spam.DIC.grid.pixelSearchOnGrid(im, imDef,
nodePositions, 5,
searchRange={"zRange": [-3, 3], "yRange": [-3, 3], "xRange": [-3, 3]},
PhiField=PhiField)
for nodeNumber, nodePosition in enumerate(nodePositions):
self.assertAlmostEqual(numpy.sum(PhiField[nodeNumber] - ps2["PhiField"][nodeNumber]), 0, places=1)
def test_registerOnGrid(self):
im = spam.datasets.loadSnow()
nodePositions = spam.DIC.grid.makeGrid(im.shape, 20)[0]
......
......@@ -161,30 +161,40 @@ def pixelSearchOnGrid(im1, im2, nodePositions, halfWindowSize, searchRange, PhiF
halfWindowSize[2] - searchRangeForThisNode['xRange'][0]]
# 2020-09-25 OS and EA: Prepare startStop array for imagette 1 to be extracted with new slicePadded
## Prepare slice for im1 -- this is always in the same place
#subVolSlice1 = (slice(int(nodePositions[nodeNumber, 0] - halfWindowSize[0]), int(nodePositions[nodeNumber, 0] + halfWindowSize[0] + 1)),
#slice(int(nodePositions[nodeNumber, 1] - halfWindowSize[1]), int(nodePositions[nodeNumber, 1] + halfWindowSize[1] + 1)),
#slice(int(nodePositions[nodeNumber, 2] - halfWindowSize[2]), int(nodePositions[nodeNumber, 2] + halfWindowSize[2] + 1)))
## Extract it...
#imagette1 = im1[subVolSlice1]
startStopIm1 = [int(nodePositions[nodeNumber, 0] - halfWindowSize[0]), int(nodePositions[nodeNumber, 0] + halfWindowSize[0] + 1),
int(nodePositions[nodeNumber, 1] - halfWindowSize[1]), int(nodePositions[nodeNumber, 1] + halfWindowSize[1] + 1),
int(nodePositions[nodeNumber, 2] - halfWindowSize[2]), int(nodePositions[nodeNumber, 2] + halfWindowSize[2] + 1)]
imagette1 = spam.helpers.slicePadded(im1, startStopIm1)
PhiNoDisp = PhiField[nodeNumber]
PhiNoDisp[0:3,-1] = 0.0
# If F is not the identity, create a pad to be able to apply F to imagette 1
if numpy.allclose(PhiNoDisp, numpy.eye(4)):
applyPhiPad = 0
else:
# 2020-10-06 OS and EA: Add a pad to each dimension of 25% of max(halfWindowSize) to allow space to apply F (no displacement) to imagette1
applyPhiPad = int(0.25*numpy.ceil(max(halfWindowSize)))
startStopIm1 = [int(nodePositions[nodeNumber, 0] - halfWindowSize[0] - applyPhiPad), int(nodePositions[nodeNumber, 0] + halfWindowSize[0] + applyPhiPad + 1),
int(nodePositions[nodeNumber, 1] - halfWindowSize[1] - applyPhiPad), int(nodePositions[nodeNumber, 1] + halfWindowSize[1] + applyPhiPad + 1),
int(nodePositions[nodeNumber, 2] - halfWindowSize[2] - applyPhiPad), int(nodePositions[nodeNumber, 2] + halfWindowSize[2] + applyPhiPad + 1)]
# In either case, extract imagette1, now guaranteed to be the right size
imagette1padded = spam.helpers.slicePadded(im1, startStopIm1)
# If F is not the identity, apply F and undo crop
if numpy.allclose(PhiNoDisp, numpy.eye(4)):
# In this case there is is no padding (despite the name) and we can just keep going
imagette1def = imagette1padded
else:
# apply F to imagette 1 padded
imagette1paddedDef = spam.DIC.applyPhi(imagette1padded, PhiNoDisp)
# undo padding
imagette1def = imagette1paddedDef[applyPhiPad:-applyPhiPad,
applyPhiPad:-applyPhiPad,
applyPhiPad:-applyPhiPad]
# Make sure imagette is not 0-dimensional in any dimension
if numpy.all(numpy.array(imagette1.shape) > 0):
if numpy.nanmean(imagette1) > greyThreshold[0] and numpy.nanmean(imagette1) < greyThreshold[1] and len(imagette1.ravel()) > minMaskVolume:
if numpy.all(numpy.array(imagette1def.shape) > 0):
if numpy.nanmean(imagette1def) > greyThreshold[0] and numpy.nanmean(imagette1def) < greyThreshold[1] and len(imagette1def.ravel()) > minMaskVolume:
# Slice for image 2
## 2020-09-25 OS and EA: Prepare startStop array for imagette 2 to be extracted with new slicePadded
#subVolSlice2 = (slice(int(nodePositions[nodeNumber, 0] - halfWindowSize[0] + searchRangeForThisNode['zRange'][0]),
#int(nodePositions[nodeNumber, 0] + halfWindowSize[0] + searchRangeForThisNode['zRange'][1] + 1)),
#slice(int(nodePositions[nodeNumber, 1] - halfWindowSize[1] + searchRangeForThisNode['yRange'][0]),
#int(nodePositions[nodeNumber, 1] + halfWindowSize[1] + searchRangeForThisNode['yRange'][1] + 1)),
#slice(int(nodePositions[nodeNumber, 2] - halfWindowSize[2] + searchRangeForThisNode['xRange'][0]),
#int(nodePositions[nodeNumber, 2] + halfWindowSize[2] + searchRangeForThisNode['xRange'][1] + 1)))
## Extract it...
#imagette2 = im2[subVolSlice2]
startStopIm2 = [int(nodePositions[nodeNumber, 0] - halfWindowSize[0] + searchRangeForThisNode['zRange'][0]),
int(nodePositions[nodeNumber, 0] + halfWindowSize[0] + searchRangeForThisNode['zRange'][1] + 1),
int(nodePositions[nodeNumber, 1] - halfWindowSize[1] + searchRangeForThisNode['yRange'][0]),
......@@ -196,16 +206,16 @@ def pixelSearchOnGrid(im1, im2, nodePositions, halfWindowSize, searchRange, PhiF
# Failed minMaskVolume or greylevel condition
else:
returnStatus = -5
imagette1 = None
imagette1def = None
imagette2 = None
# Failed 0-dimensional imagette test
else:
returnStatus = -5
imagette1 = None
imagette1def = None
imagette2 = None
return {'imagette1': imagette1,
return {'imagette1': imagette1def,
'imagette2': imagette2,
'returnStatus': returnStatus,
'initialDisplacement': initialDisplacement,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment