Commit cdb8b01f authored by Edward Andò's avatar Edward Andò
Browse files

[skip-ci] applyPhiField() parallel, only pixelMask remains to be implemented

parent dac7532e
Pipeline #68864 skipped
......@@ -286,38 +286,42 @@ def applyPhiField(im, fieldCoordsRef, PhiField, displacementMode="interpolate",
pixCoordsRef = numpy.zeros_like(pixCoordsDef, dtype='f4')
# for each group of pixels (divide mask==True into nProcesses chunks):
for pixNumber, pixCoordDef in enumerate(pixCoordsDef):
# get nNeighbours and compute distance weights
distances, indices = treeCoordDef.query(pixCoordDef, k=nNeighbours)
weights = (1/(distances+tol))
displacement = numpy.zeros(3, dtype='float')
#print()
# for each neighbour
for neighbour, index in enumerate(indices):
# apply PhiInv to current point with PhiCentre = fieldCoordsREF <- this is important
# -> this gives a displacement for each neighbour
PhiInv = PhiFieldInvMasked[index]
#print("PhiInv", PhiInv)
translationTmp = PhiInv[0:3, -1].copy()
dist = pixCoordDef - fieldCoordsRefMasked[index]
#print(f"dist = {dist}")
translationTmp -= dist - numpy.dot(PhiInv[0:3, 0:3], dist)
#print(f"translationTmp ({neighbour}): {translationTmp} (weight = {weights[neighbour]})")
displacement += translationTmp*weights[neighbour]
# compute resulting displacement as weights * displacements
# compute pixel coordinates in reference config
#print("pixCoordDef", pixCoordDef)
#print("displacement", displacement)
pixCoordsRef[pixNumber] = pixCoordDef + displacement / weights.sum()
#pixCoordsRef[pixNumber] = pixCoordDef + numpy.sum(displacements*weights[:, numpy.newaxis], axis=0)
#global inter
#def dispOnePoint(defCoord):
global computeDisplacementForSeriesOfPixels
def computeDisplacementForSeriesOfPixels(seriesNumber):
pixelNumbers = splitPixNumbers[seriesNumber]
pixCoordsRefSeries = numpy.zeros((len(pixelNumbers), 3), dtype='f4')
for localPixelNumber, globalPixelNumber in enumerate(pixelNumbers):
pixCoordDef = pixCoordsDef[globalPixelNumber]
# get nNeighbours and compute distance weights
distances, indices = treeCoordDef.query(pixCoordDef, k=nNeighbours)
weights = (1/(distances+tol))
displacement = numpy.zeros(3, dtype='float')
# for each neighbour
for neighbour, index in enumerate(indices):
# apply PhiInv to current point with PhiCentre = fieldCoordsREF <- this is important
# -> this gives a displacement for each neighbour
PhiInv = PhiFieldInvMasked[index]
#print("PhiInv", PhiInv)
translationTmp = PhiInv[0:3, -1].copy()
dist = pixCoordDef - fieldCoordsRefMasked[index]
#print(f"dist = {dist}")
translationTmp -= dist - numpy.dot(PhiInv[0:3, 0:3], dist)
#print(f"translationTmp ({neighbour}): {translationTmp} (weight = {weights[neighbour]})")
displacement += translationTmp*weights[neighbour]
# compute resulting displacement as weights * displacements
# compute pixel coordinates in reference config
#print("pixCoordDef", pixCoordDef)
#print("displacement", displacement)
pixCoordsRefSeries[localPixelNumber] = pixCoordDef + displacement / weights.sum()
return pixelNumbers, pixCoordsRefSeries
#pixCoordsRef[pixNumber] = pixCoordDef + numpy.sum(displacements*weights[:, numpy.newaxis], axis=0)
## Check if we've hit the same point
#if numpy.any(distance == 0):
......@@ -368,22 +372,25 @@ def applyPhiField(im, fieldCoordsRef, PhiField, displacementMode="interpolate",
#pass
#qResults.put([point, coordNew])
## Iterate through flat field of Fs
#if verbose:
#pbar = progressbar.ProgressBar(maxval=pointsToEstimate.shape[0]).start()
#finishedPoints = 0
splitPixNumbers = numpy.array_split(numpy.arange(pixCoordsDef.shape[0]), nProcesses)
## Run multiprocessing filling in FfieldFlatGood, which will then update FfieldFlat
#with multiprocessing.Pool(processes=nProcesses) as pool:
#for returns in pool.imap_unordered(dispOnePoint, range(pointsToEstimate.shape[0])):
#if verbose:
#finishedPoints += 1
#pbar.update(finishedPoints)
#estimatedDisplacements[returns[0]] = returns[1]
#pool.close()
#pool.join()
#if verbose: pbar.finish()
# Iterate through flat field of Fs
if verbose:
pbar = progressbar.ProgressBar(maxval=len(splitPixNumbers)).start()
finishedPoints = 0
# Run multiprocessing filling in FfieldFlatGood, which will then update FfieldFlat
with multiprocessing.Pool(processes=nProcesses) as pool:
for returns in pool.imap_unordered(computeDisplacementForSeriesOfPixels, range(nProcesses)):
if verbose:
finishedPoints += 1
pbar.update(finishedPoints)
pixCoordsRef[returns[0]] = returns[1]
pool.close()
pool.join()
if verbose: pbar.finish()
## overwrite bad points displacements
#return estimatedDisplacements
......
Markdown is supported
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