Commit 70bf6e0e authored by Olga Stamati's avatar Olga Stamati
Browse files

multiprocessing in correctPhiField()

parent d88a54c3
Pipeline #64619 passed with stages
in 12 minutes and 45 seconds
......@@ -665,7 +665,7 @@ def decomposePhiField(PhiField, components, twoD=False, nProcesses=nProcessesDef
return output
def correctPhiField(fileName=None, fieldCoords=None, fieldValues=None, fieldRS=None, fieldDPhi=None, fieldPixelSearchCC=None, fieldIT=None, fieldBinRatio=1.0, ignoreBadPoints=False, ignoreBackGround=False, correctBadPoints=False, deltaPhiNormMin=0.001, pixelSearchCCmin=0.98, nNeighbours=12, filterPoints=False, filterPointsRadius=3, verbose=False, saveFile=False, saveFileName=None):
def correctPhiField(fileName=None, fieldCoords=None, fieldValues=None, fieldRS=None, fieldDPhi=None, fieldPixelSearchCC=None, fieldIT=None, fieldBinRatio=1.0, ignoreBadPoints=False, ignoreBackGround=False, correctBadPoints=False, deltaPhiNormMin=0.001, pixelSearchCCmin=0.98, nNeighbours=12, filterPoints=False, filterPointsRadius=3, saveFile=False, saveFileName=None, nProcesses=nProcessesDefault, verbose=True):
"""
This function corrects a field of deformation functions **Phi** calculated at a number of points.
This is typically the output of the DICdiscrete and DICregularGrid clients.
......@@ -759,7 +759,7 @@ def correctPhiField(fileName=None, fieldCoords=None, fieldValues=None, fieldRS=N
saveFile : bool, optional
save the corrected file into a tsv
Default = False
Default = True
saveFileName : string, optional
The file name for output.
......@@ -832,45 +832,27 @@ def correctPhiField(fileName=None, fieldCoords=None, fieldValues=None, fieldRS=N
# if asked, replace the bad correlation points with the weighted influence of the k nearest good neighbours
if correctBadPoints:
print("\nCorrecting...")
# create the k-d tree of the coordinates of good points, we need this to search for the k nearest neighbours easily
# for details see: https://en.wikipedia.org/wiki/K-d_tree &
# https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.KDTree.query.html
from scipy.spatial import KDTree
treeCoord = KDTree(fieldCoords[goodPoints])
import scipy.spatial
treeCoord = scipy.spatial.KDTree(fieldCoords[goodPoints])
# extract the Phi matrices of the bad points
fieldBad = numpy.zeros_like(PhiField[badPoints])
fieldBad[:, -1, :] = numpy.array([0, 0, 0, 1])
# check if we have asked only for the closest neighbour
if nNeighbours == 1:
# loop over each bad point
for badPoint in range(badPoints[0].shape[0]):
if verbose:
print("\rWorking on bad point: {} of {}".format(badPoint + 1, badPoints[0].shape[0]), end='')
# call tree.query to calculate:
# {ind}: the index of the nearest neighbour (as neighbours we consider only good points)
# {distnace}: distance (Minkowski norm 2, which is the usual Euclidean distance) of the bad point to the nearest neighbour
global correctPoint
def correctPoint(badPoint):
# check if we have asked only for the closest neighbour
if nNeighbours == 1:
distance, ind = treeCoord.query(fieldCoords[badPoints][badPoint], k=nNeighbours)
# replace bad point's Phi with the Phi of the nearest good point
fieldBad[badPoint][:-1] = PhiField[goodPoints][ind][:-1].copy()
# replace the corrected Phi field
PhiField[badPoints] = fieldBad
# if we have asked for more neighbours
else:
# loop over each bad point
for badPoint in range(badPoints[0].shape[0]):
if verbose:
print("\rWorking on bad point: {} of {}".format(badPoint + 1, badPoints[0].shape[0]), end='')
# call tree.query to calculate:
# {ind}: k nearest neighbours (as neighbours we consider only good points)
# {distnace}: distance (Minkowski norm 2, which is the usual Euclidean distance) of the bad point to each of the ith nearest neighbour
return badPoint, PhiField[goodPoints][ind][:-1].copy()
# if we have asked for more neighbours
else:
Phi = numpy.zeros((3, 4))
distance, ind = treeCoord.query(fieldCoords[badPoints][badPoint], k=nNeighbours)
# compute the "Inverse Distance Weighting" since the nearest points should have the major influence
......@@ -882,17 +864,33 @@ def correctPhiField(fileName=None, fieldCoords=None, fieldValues=None, fieldRS=N
weightInv = (1 / distance[neighbour]) / float(weightSumInv)
# replace the Phi components of the bad point with the weighted Phi components of the ith nearest good neighbour
fieldBad[badPoint][:-1] += PhiField[goodPoints][ind[neighbour]][:-1] * weightInv
Phi += PhiField[goodPoints][ind[neighbour]][:-1] * weightInv
return badPoint, Phi
# Iterate through bad points
if verbose:
pbar = progressbar.ProgressBar(maxval=badPoints[0].shape[0]).start()
finishedPoints = 0
# Run multiprocessing
with multiprocessing.Pool(processes=nProcesses) as pool:
for returns in pool.imap_unordered(correctPoint, range(badPoints[0].shape[0])):
if verbose:
finishedPoints += 1
pbar.update(finishedPoints)
fieldBad[returns[0]][0:3] = returns[1]
if verbose: pbar.finish()
# replace the corrected Phi field
PhiField[badPoints] = fieldBad
# replace the corrected Phi field
PhiField[badPoints] = fieldBad
# overwrite RS to the corrected
RS[badPoints] = 2
# if asked, apply a median filter of a specific size in the Phi field
if filterPoints:
if verbose:
print("\nFiltering...")
print("\nApplying median filter...")
import scipy.ndimage
filterPointsRadius = int(filterPointsRadius)
......
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