spam-filterPhiField 17.4 KB
Newer Older
Olga Stamati's avatar
Olga Stamati committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python

"""
This script manipulates a Phi field:

Gridded Phi values:
  - spam-pixelSearch
  - spam-pixelSearchPropagate
  - spam-ldic

Phis defined at labels centres:
  - spam-pixelSearch
  - spam-pixelSearchPropagate
  - spam-ddic


This script allows you to:
  - correct bad points inside a PhiField based on RS, or CC
  - correct incoherent points inside a PhiField based on LQC
  - apply a median filter to the PhiField

Outputs are:
  - TSV files
  - (optional) VTK files for visualisation
  - (optional) TIF files in the case of gridded data

Copyright (C) 2020 SPAM Contributors

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.
"""

43
44
import os
os.environ['OPENBLAS_NUM_THREADS'] = '1'
Olga Stamati's avatar
Olga Stamati committed
45
46
47
48
49
50
51
52
53
54

import spam.DIC
import spam.deformation
import spam.helpers
#import spam.mesh
import spam.label

import numpy
import multiprocessing
import scipy.spatial
Edward Andò's avatar
Edward Andò committed
55
import scipy.ndimage
56
import progressbar
Olga Stamati's avatar
Olga Stamati committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import argparse
import tifffile

tol = 1e-6

# Define argument parser object
parser = argparse.ArgumentParser(description="spam-filterPhiField "+spam.helpers.optionsParser.GLPv3descriptionHeader +\
                                             "This script process Phi fields by\n"+
                                             "correcting bad or incoherent points or filtering",
                                 formatter_class=argparse.RawTextHelpFormatter)

# Parse arguments with external helper function
args = spam.helpers.optionsParser.filterPhiField(parser)

71
72
if args.PROCESSES is None: args.PROCESSES = multiprocessing.cpu_count()

Olga Stamati's avatar
Olga Stamati committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
print("spam-filterPhiField -- Current Settings:")
argsDict = vars(args)
for key in sorted(argsDict):
    print("\t{}: {}".format(key, argsDict[key]))

###############################################################
### Step 1 (mandatory) read input Phi File
###############################################################
PhiFromFile = spam.helpers.readCorrelationTSV(args.PHIFILE.name, readConvergence=True, readPixelSearchCC=True, readError=True)
if PhiFromFile is None:
    print(f"\tFailed to read your TSV file passed with -pf {args.PHIFILE.name}")
    exit()

# If the read Phi-file has only one line -- it's a single point registration!
# We can either apply it to a grid or to labels
if PhiFromFile['fieldCoords'].shape[0] == 1:
89
    print(f"\tYour TSV passed with -pf {args.PHIFILE.name} is single line file (a registration). A field is required")
Olga Stamati's avatar
Olga Stamati committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    exit()

# Check if it is a discrete or gridded field
grid = True
discrete = False
if PhiFromFile["numberOfLabels"] != 0:
    discrete = True
    grid = False

###############################################################
### Input Phi file is a Phi FIELD
###############################################################
inputNodesDim      = PhiFromFile["fieldDims"]
inputNodePositions = PhiFromFile["fieldCoords"]
inputPhiField      = PhiFromFile["PhiField"]
inputDisplacements = PhiFromFile["PhiField"][:, 0:3, -1]
inputReturnStatus  = PhiFromFile["returnStatus"]
inputPixelSearchCC = PhiFromFile["pixelSearchCC"]
inputDeltaPhiNorm  = PhiFromFile["deltaPhiNorm"]
inputIterations    = PhiFromFile["iterations"]
inputError         = PhiFromFile["error"]
Edward Andò's avatar
Edward Andò committed
111
### Empty arrays for masking points
112
113
114
inputGood          = numpy.zeros(inputNodePositions.shape[0], dtype=bool)
inputBad           = numpy.zeros(inputNodePositions.shape[0], dtype=bool)
inputIgnore        = numpy.zeros(inputNodePositions.shape[0], dtype=bool)
Olga Stamati's avatar
Olga Stamati committed
115
116
117

# output arrays
outputPhiField      = numpy.zeros((inputNodePositions.shape[0], 4, 4))
118
119
outputReturnStatus  = numpy.ones( (inputNodePositions.shape[0]), dtype=float)
outputDeltaPhiNorm  = numpy.ones( (inputNodePositions.shape[0]), dtype=float)*100
Olga Stamati's avatar
Olga Stamati committed
120
outputIterations    = numpy.zeros((inputNodePositions.shape[0]), dtype=float)
121
outputError         = numpy.ones( (inputNodePositions.shape[0]), dtype=float)*100
Olga Stamati's avatar
Olga Stamati committed
122
123
124
125
126
outputPixelSearchCC = numpy.zeros((inputNodePositions.shape[0]), dtype=float)
# Check neighbour inputs, either args.NEIGHBOUR_RADIUS or args.NUMBER_OF_NEIGHBOURS should be set.
if args.NEIGHBOUR_RADIUS is not None and args.NUMBER_OF_NEIGHBOURS is not None:
    print("Both number of neighbours and neighbour radius are set, I'm taking the radius and ignoring the number of neighbours")
    args.NUMBER_OF_NEIGHBOURS = None
127

Olga Stamati's avatar
Olga Stamati committed
128
129
130
131
132
133
134
135
136
137
138
if args.NEIGHBOUR_RADIUS is None and args.NUMBER_OF_NEIGHBOURS is None:
    if grid:
        # Gridded input field
        nodeSpacing = numpy.array([numpy.unique(inputNodePositions[:, i])[1] - numpy.unique(inputNodePositions[:, i])[0] if len(numpy.unique(inputNodePositions[:, i])) > 1 else numpy.unique(inputNodePositions[:, i])[0] for i in range(3)])
        args.NEIGHBOUR_RADIUS = 4*int(numpy.mean(nodeSpacing))
        print(f"Neither number of neighbours nor neighbour distance set, using default distance of 4*mean(nodeSpacing) = {args.NEIGHBOUR_RADIUS}")
    else:
        # Discrete input field
        args.NUMBER_OF_NEIGHBOURS = 27
        print("Neither number of neighbours nor neighbour distance set, using default 27 neighbours")

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

###############################################################
### Define IGNORE points:
###############################################################
if args.MASK:
    inputIgnore = inputReturnStatus < -4

###############################################################
### Apply threshold to select good and bad points
###############################################################
if args.SRS:
    print(f"\n\nSelecting bad points as Return Status <= {args.SRST}")
    inputGood = numpy.logical_and(inputReturnStatus >  args.SRST, ~inputIgnore)
    inputBad  = numpy.logical_and(inputReturnStatus <= args.SRST, ~inputIgnore)
    if args.SLQC:
        print("\tYou passed -slqc but you can only have one selection at a time")
    if args.SCC:
        print("\tYou passed -scc but you can only have one selection at a time")

elif args.SCC:
    print(f"\n\nSelecting bad points with Pixel Search CC <= {args.SCCT}")
    inputGood = numpy.logical_and(inputPixelSearchCC >  args.SCCT, ~inputIgnore)
    inputBad  = numpy.logical_and(inputPixelSearchCC <= args.SCCT, ~inputIgnore)
    if args.SLQC:
        print("\tYou passed -slqc but you can only have one selection at a time")

elif args.SLQC:
Olga Stamati's avatar
Olga Stamati committed
166
    print("\n\nCalculate coherency")
167
168
169
170
171
172
    LQC = spam.DIC.estimateLocalQuadraticCoherency(inputNodePositions[~inputIgnore],
                                                   inputDisplacements[~inputIgnore],
                                                   neighbourRadius=args.NEIGHBOUR_RADIUS,
                                                   nNeighbours=args.NUMBER_OF_NEIGHBOURS,
                                                   nProcesses=args.PROCESSES,
                                                   verbose=True)
173
174
    #print(LQC.shape)
    #print(inputGood[~inputIgnore].shape)
175
176
177
    inputGood[~inputIgnore] = LQC <  0.1
    inputBad[~inputIgnore]  = LQC >= 0.1

Olga Stamati's avatar
Olga Stamati committed
178
179


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
###############################################################
### Copy over the values for good AND ignore to output
###############################################################
gandi = numpy.logical_or(inputGood, inputIgnore)

outputPhiField[gandi]      = inputPhiField[gandi]
outputReturnStatus[gandi]  = inputReturnStatus[gandi]
outputDeltaPhiNorm[gandi]  = inputDeltaPhiNorm[gandi]
outputIterations[gandi]    = inputIterations[gandi]
outputError[gandi]         = inputError[gandi]
outputPixelSearchCC[gandi] = inputPixelSearchCC[gandi]

if (args.CINT + args.CLQF) > 0 and numpy.sum(inputBad) == 0:
    print("No points to correct, exiting")
    exit()

196
197
198
else:
    print(f"\n\nCorrecting {numpy.sum(inputBad)} points ({100*numpy.sum(inputBad)/numpy.sum(inputGood):03.1f}%)")

199
200
201
202
###############################################################
### Correct those bad points
###############################################################
if args.CINT:
203
    print(f"\n\nCorrection based on local interpolation (filterF = {args.FILTER_F})")
204
205
206
207
208
    PhiFieldCorrected = spam.DIC.interpolatePhiField(inputNodePositions[inputGood],
                                                     inputPhiField[inputGood],
                                                     inputNodePositions[inputBad],
                                                     nNeighbours=args.NUMBER_OF_NEIGHBOURS,
                                                     neighbourRadius=args.NEIGHBOUR_RADIUS,
209
                                                     interpolateF=args.FILTER_F,
210
211
212
213
214
215
216
217
                                                     nProcesses=args.PROCESSES,
                                                     verbose=True)
    outputPhiField[inputBad]     = PhiFieldCorrected
    outputReturnStatus[inputBad] = 1
    if args.CLQF:
        print("\tYou asked to correct with local QC fitting with -clqf, but only one correciton mode is supported")

elif args.CLQF:
218
    if args.FILTER_F != 'no':
219
220
        print("WARNING: non-displacement quadratic coherency correction not implemented, only doing displacements, and returning F=eye(3)\n")

221
222
223
224
225
226
227
228
    print("\n\nCorrection based on local quadratic coherency")
    dispLQC = spam.DIC.estimateDisplacementFromQuadraticFit(inputNodePositions[inputGood],
                                                            inputDisplacements[inputGood],
                                                            inputNodePositions[inputBad],
                                                            neighbourRadius=args.NEIGHBOUR_RADIUS,
                                                            nNeighbours=args.NUMBER_OF_NEIGHBOURS,
                                                            nProcesses=args.PROCESSES,
                                                            verbose=True)
Olga Stamati's avatar
Olga Stamati committed
229
    # pass the displacements
230
    outputPhiField[inputBad, 0:3, 0:3] = numpy.eye(3)
231
232
233
234
    outputPhiField[inputBad, 0:3, -1] = dispLQC
    outputReturnStatus[inputBad] = 1


Olga Stamati's avatar
Olga Stamati committed
235

Edward Andò's avatar
Edward Andò committed
236
237
238
239
240
241
242
243
244
245
246
247
if args.FILTER_MEDIAN:
    if discrete:
        print("Median filter for discrete mode not implemented... does it even make sense?")
    else:
        # Filter ALL POINTS
        # if asked, apply a median filter of a specific size in the Phi field
        print("\nApplying median filter...")
        filterPointsRadius = int(args.FILTER_MEDIAN_RADIUS)

        if args.MASK:
            inputPhiField[inputIgnore] = numpy.nan

248
        if args.FILTER_F == 'rigid':
Edward Andò's avatar
Edward Andò committed
249
250
251
            print("Rigid mode not well defined for overall median filtering, exiting")
            exit()

252
        if args.FILTER_F == 'all':
Edward Andò's avatar
Edward Andò committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
            # Filter F components
            print("Filtering F components...")
            print("\t1/9")
            outputPhiField[:, 0,  0] = scipy.ndimage.generic_filter(inputPhiField[:, 0,  0].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t2/9")
            outputPhiField[:, 1,  0] = scipy.ndimage.generic_filter(inputPhiField[:, 1,  0].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t3/9")
            outputPhiField[:, 2,  0] = scipy.ndimage.generic_filter(inputPhiField[:, 2,  0].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t4/9")
            outputPhiField[:, 0,  1] = scipy.ndimage.generic_filter(inputPhiField[:, 0,  1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t5/9")
            outputPhiField[:, 1,  1] = scipy.ndimage.generic_filter(inputPhiField[:, 1,  1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t6/9")
            outputPhiField[:, 2,  1] = scipy.ndimage.generic_filter(inputPhiField[:, 2,  1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t7/9")
            outputPhiField[:, 0,  2] = scipy.ndimage.generic_filter(inputPhiField[:, 0,  2].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t8/9")
            outputPhiField[:, 1,  2] = scipy.ndimage.generic_filter(inputPhiField[:, 1,  2].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
            print("\t9/9")
            outputPhiField[:, 2,  2] = scipy.ndimage.generic_filter(inputPhiField[:, 2,  2].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()

274
        if args.FILTER_F == 'no':
Edward Andò's avatar
Edward Andò committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
            for n in range(inputNodePositions.shape[0]):
                outputPhiField[n] = numpy.eye(4)

        print("Filtering displacements...")
        print("\t1/3")
        outputPhiField[:, 0, -1] = scipy.ndimage.generic_filter(inputPhiField[:, 0, -1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
        print("\t2/3")
        outputPhiField[:, 1, -1] = scipy.ndimage.generic_filter(inputPhiField[:, 1, -1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()
        print("\t3/3")
        outputPhiField[:, 2, -1] = scipy.ndimage.generic_filter(inputPhiField[:, 2, -1].reshape(inputNodesDim), numpy.nanmedian, size=(2 * filterPointsRadius + 1)).ravel()

        if args.MASK:
            outputPhiField[inputIgnore] = numpy.nan


Olga Stamati's avatar
Olga Stamati committed
290
291
292
293
294
295
296
297
298
#Outputs are:
  #- TSV files
  #- (optional) VTK files for visualisation
  #- (optional) TIF files in the case of gridded data
if args.TSV:
    if discrete:
        TSVheader = "Label\tZpos\tYpos\tXpos\tFzz\tFzy\tFzx\tZdisp\tFyz\tFyy\tFyx\tYdisp\tFxz\tFxy\tFxx\tXdisp\tpixelSearchCC\treturnStatus\terror\tdeltaPhiNorm\titerations"
    else:
        TSVheader = "NodeNumber\tZpos\tYpos\tXpos\tFzz\tFzy\tFzx\tZdisp\tFyz\tFyy\tFyx\tYdisp\tFxz\tFxy\tFxx\tXdisp\tpixelSearchCC\treturnStatus\terror\tdeltaPhiNorm\titerations"
299
    outMatrix = numpy.array([numpy.arange(inputNodePositions.shape[0]),
Olga Stamati's avatar
Olga Stamati committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
                            inputNodePositions[:, 0],  inputNodePositions[:, 1],  inputNodePositions[:, 2],
                            outputPhiField[:, 0, 0],    outputPhiField[:, 0, 1],    outputPhiField[:, 0, 2],  outputPhiField[:, 0, 3],
                            outputPhiField[:, 1, 0],    outputPhiField[:, 1, 1],    outputPhiField[:, 1, 2],  outputPhiField[:, 1, 3],
                            outputPhiField[:, 2, 0],    outputPhiField[:, 2, 1],    outputPhiField[:, 2, 2],  outputPhiField[:, 2, 3],
                            outputPixelSearchCC,
                            outputReturnStatus,
                            outputError,
                            outputDeltaPhiNorm,
                            outputIterations]).T

    numpy.savetxt(args.OUT_DIR+"/"+args.PREFIX+".tsv",
                outMatrix,
                fmt='%.7f',
                delimiter='\t',
                newline='\n',
                comments='',
                header=TSVheader)

if args.TIFF:
    if grid:
        if inputNodesDim[0] != 1:
            tifffile.imsave(args.OUT_DIR+"/"+args.PREFIX+"-Zdisp.tif", outputPhiField[:, 0, -1].astype('<f4').reshape(inputNodesDim))
        tifffile.imsave(args.OUT_DIR+"/"+args.PREFIX+"-Ydisp.tif",     outputPhiField[:, 1, -1].astype('<f4').reshape(inputNodesDim))
        tifffile.imsave(args.OUT_DIR+"/"+args.PREFIX+"-Xdisp.tif",     outputPhiField[:, 2, -1].astype('<f4').reshape(inputNodesDim))
        #tifffile.imsave(args.OUT_DIR+"/"+args.PREFIX+"-CC.tif",                     pixelSearchCC.astype('<f4').reshape(nodesDim))
        #tifffile.imsave(args.OUT_DIR+"/"+args.PREFIX+"-returnStatus.tif",           returnStatus.astype('<f4').reshape(nodesDim))
    else:
        # Think about relabelling grains here automatically?
        pass


# Collect data into VTK output
if args.VTK:
    if grid:
        cellData = {}
        cellData['displacements'] = outputPhiField[:, :-1, 3].reshape((inputNodesDim[0], inputNodesDim[1], inputNodesDim[2], 3))

        # Overwrite nans and infs with 0, rubbish I know
        cellData['displacements'][numpy.logical_not(numpy.isfinite(cellData['displacements']))] = 0

        # This is perfect in the case where NS = 2xHWS, these cells will all be in the right place
        #   In the case of overlapping of under use of data, it should be approximately correct
        # If you insist on overlapping, then perhaps it's better to save each point as a cube glyph
        #   and actually *have* overlapping
        # HACK assume HWS is half node spacing
        nodeSpacing = numpy.array([numpy.unique(inputNodePositions[:, i])[1] - numpy.unique(inputNodePositions[:, i])[0] if len(numpy.unique(inputNodePositions[:, i])) > 1 else numpy.unique(inputNodePositions[:, i])[0] for i in range(3)])
        HWS = nodeSpacing/2
        spam.helpers.writeStructuredVTK(origin=inputNodePositions[0]-HWS, aspectRatio=nodeSpacing, cellData=cellData, fileName=args.OUT_DIR+"/"+args.PREFIX+".vtk")

    else:
        disp = outputPhiField[:, 0:3, -1]
        disp[numpy.logical_not(numpy.isfinite(disp))] = 0

        magDisp = numpy.linalg.norm(disp, axis=1)

        VTKglyphDict = {'displacements': outputPhiField[:, 0:3, -1],
                        'mag(displacements)': magDisp
                        }

        spam.helpers.writeGlyphsVTK(inputNodePositions, VTKglyphDict, fileName=args.OUT_DIR + "/" + args.PREFIX + ".vtk")