Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

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

-ns optional now cleaned up and propagate compatible with strict ddic reading

parent 6ec06eb3
Pipeline #65112 passed with stages
in 13 minutes and 34 seconds
......@@ -71,11 +71,38 @@ im2 = tifffile.imread(args.im2.name)
if len(im2.shape) == 2: im2 = im2[numpy.newaxis, ...]
###############################################################
### Main switch for LAB or GRID mode
###############################################################
####If there is a labelled image, load that...
if args.LAB1 is not None:
lab1 = tifffile.imread(args.LAB1.name).astype(spam.label.labelType)
boundingBoxes = spam.label.boundingBoxes(lab1)
nodePositions = spam.label.centresOfMass(lab1, boundingBoxes=boundingBoxes)
numberOfNodes = nodePositions.shape[0]
im1mask = None
im2mask = None
### Otherwise we are in node spacing and half-window size mode
else:
if args.MASK1 is not None:
im1mask = tifffile.imread(args.MASK1.name) != 0
if len(im1mask.shape) == 2:
im1mask = im1mask[numpy.newaxis, ...]
else:
im1mask = None
REG = False
nodePositionsFile = None
PhiField = None
if args.MASK2 is not None:
im2mask = tifffile.imread(args.MASK2.name) != 0
if len(im2mask.shape) == 2:
im2mask = im2mask[numpy.newaxis, ...]
else:
im2mask = None
# Three cases to handle:
# 1. phi file is reg -> define nodes and apply reg
# 2. phi file is field -> take everything and check NS if passed
# 3. no phi file -> define nodes
if args.PHIFILE is not None:
PhiFromFile = spam.helpers.readCorrelationTSV(args.PHIFILE.name, fieldBinRatio=args.PHIFILE_BIN_RATIO)
if PhiFromFile is None:
......@@ -96,98 +123,70 @@ if args.PHIFILE is not None:
print("\t\t", decomposedPhiInit['z'])
del decomposedPhiInit
REG = True
if args.LAB1 is None:
# Create nodes if in regular mode, in label mode these are already defined
if args.NS is None:
print(f"spam-pixelSearch: You passed a registration file {args.PHIFILE.name}, I need -ns to be defined")
exit()
nodePositions, nodesDim = spam.DIC.makeGrid(im1.shape, args.NS)
numberOfNodes = nodePositions.shape[0]
# We have a registration to apply to all points.
# This is done in 2 steps:
# 1. by copying the registration's little F to the Fs of all points
# 2. by calling the decomposePhi function to compute the translation of each point
# Now that we know how many points we want to correlate, initalise PhiField
PhiField = numpy.zeros((numberOfNodes, 4, 4))
for node in range(numberOfNodes):
if args.APPLY_F == "all":
PhiField[node] = PhiInit.copy()
elif args.APPLY_F == "rigid":
PhiField[node] = spam.deformation.computeRigidPhi(PhiInit.copy())
else:
PhiField[node] = numpy.eye(4)
# trasnlation application
PhiField[node][0:3, -1] = spam.deformation.decomposePhi(PhiInit.copy(), PhiCentre=PhiFromFile["fieldCoords"][0], PhiPoint=nodePositions[node])["t"]
# If the read Phi-file contains multiple lines it's an F field!
else:
#print("spam-pixelSearch: Assuming loaded PhiFile is coherent with the current run.")
nodePositionsFile = PhiFromFile["fieldCoords"]
numberOfNodes = nodePositionsFile.shape[0]
nodeSpacingFile = numpy.array([numpy.unique(nodePositionsFile[:, i])[1] - numpy.unique(nodePositionsFile[:, i])[0] if len(numpy.unique(nodePositionsFile[:, i])) > 1 else numpy.unique(nodePositionsFile[:, i])[0] for i in range(3)])
PhiField = PhiFromFile["PhiField"]
###############################################################
### Main switch for LAB or GRID mode
###############################################################
####If there is a labelled image, load that...
if args.LAB1 is not None:
lab1 = tifffile.imread(args.LAB1.name).astype(spam.label.labelType)
boundingBoxes = spam.label.boundingBoxes(lab1)
nodePositions = spam.label.centresOfMass(lab1, boundingBoxes=boundingBoxes)
numberOfNodes = nodePositions.shape[0]
im1mask = None
im2mask = None
if nodePositionsFile is not None:
if not numpy.allclose(nodePositionsFile, nodePositions, atol=1.0):
print(f"spam-pixelSearch: Input PhiField positions from {args.PHIFILE.name} are not within 1px of the centre of mass of the labels from {args.LAB1}, this seems dangerous.")
print(f"\tplease consider using spam-passPhiField to apply your PhiField to a new labelled image")
exit()
### Otherwise we are in node spacing and half-window size mode
else:
if args.MASK1 is not None:
im1mask = tifffile.imread(args.MASK1.name) != 0
if len(im1mask.shape) == 2:
im1mask = im1mask[numpy.newaxis, ...]
else:
im1mask = None
if args.MASK2 is not None:
im2mask = tifffile.imread(args.MASK2.name) != 0
if len(im2mask.shape) == 2:
im2mask = im2mask[numpy.newaxis, ...]
else:
im2mask = None
# different checks to be done for lab and grid:
if args.LAB1 is None:
# In case NS is also defined, complain, but if it's the same as the loaded data, continue
if args.NS is not None:
# compare them
if not numpy.allclose(numpy.array(args.NS), nodeSpacingFile, atol=0.0):
print(f"spam-ldic: you passed a -ns={args.NS} which contradicts the node spacing in your Phi Field TSV of {nodeSpacingFile}")
print(f"\thint 1: if you pass a Phi Field TSV you don't need to also define the node spacing")
print(f"\thint 2: if you want to use your Phi Field TSV {args.PHIFILE.name} on a finer node spacing, pass it with spam-passPhiField")
exit()
else:
print(f"spam-ldic: passing -ns with a Phi Field TSV is not needed")
nodePositions = nodePositionsFile
else:
# Lab phi-field consistency check
if not numpy.allclose(nodePositionsFile, nodePositions, atol=1.0):
print(f"spam-pixelSearch: Input PhiField positions from {args.PHIFILE.name} are not within 1px of the centre of mass of the labels from {args.LAB1}, this seems dangerous.")
print(f"\tplease consider using spam-passPhiField to apply your PhiField to a new labelled image")
exit()
if nodePositionsFile is None:
else: # no Phi file
if args.LAB1 is None:
if args.NS is None:
print("spam-pixelSearch: You're in regular grid mode, but no -ns is set and no Phi Field TSV has been passed, exiting.")
exit()
nodePositions, nodesDim = spam.DIC.makeGrid(im1.shape, args.NS)
numberOfNodes = nodePositions.shape[0]
else: # nodePositionsFile is defined -- i.e., we read a regular PhiField TSV
nodeSpacingFile = numpy.array([numpy.unique(nodePositionsFile[:, i])[1] - numpy.unique(nodePositionsFile[:, i])[0] if len(numpy.unique(nodePositionsFile[:, i])) > 1 else numpy.unique(nodePositionsFile[:, i])[0] for i in range(3)])
nodePositions = nodePositionsFile
if args.NS is not None:
# compare them
if not numpy.allclose(numpy.array(args.NS), nodeSpacingFile, atol=0.0):
print(f"spam-pixelSearch: you passed a -ns={args.NS} which contradicts the node spacing in your Phi Field TSV of {nodeSpacingFile}")
print(f"\thint 1: if you pass a Phi Field TSV you don't need to also define the node spacing")
print(f"\thint 2: if you want to use your Phi Field TSV {args.PHIFILE.name} on a finer node spacing, pass it with spam-passPhiField")
exit()
else:
print(f"spam-pixelSearch: passing -ns with a Phi Field TSV is not needed")
else:
args.NS = nodeSpacingFile
###############################################################
numberOfNodes = nodePositions.shape[0]
if PhiField is None: # Either REG mode, or not TSV at all
# Now that we know how many points we want to correlate, initalise PhiField
PhiField = numpy.zeros((numberOfNodes, 4, 4))
for node in range(numberOfNodes):
PhiField[node] = numpy.eye(4)
if REG:
# If we were passed a registration Phi TSV, apply it to each point
for node in range(numberOfNodes):
if args.APPLY_F == "all":
PhiField[node] = PhiInit.copy()
elif args.APPLY_F == "rigid":
PhiField[node] = spam.deformation.computeRigidPhi(PhiInit.copy())
PhiField[node][0:3, -1] = spam.deformation.decomposePhi(PhiInit.copy(), PhiCentre=PhiFromFile["fieldCoords"][0], PhiPoint=nodePositions[node])["t"]
### This vvv is the pixelSearchOnGrid function from grid.py
def pixelSearchOneNode(nodeNumber):
"""
......
......@@ -233,6 +233,7 @@ for point in range(1, numberOfPoints):
returnStatus[point] = imagetteReturns['returnStatus']
# Detect regular grid mode
#if args.GUIDING_POINTS_FILE is None:
if args.GUIDING_POINTS_FILE is None and args.LAB1 is None:
rowNumbers = rowNumbers[1:]
......@@ -258,27 +259,30 @@ if args.TSV:
# 3 node positions,
# F[0:3,0:3]
# Pixel-search CC
#if args.LAB1 is None:
outMatrix = numpy.array([numpy.array(range(guidingPoints.shape[0])),
guidingPoints[:, 0], guidingPoints[:, 1], guidingPoints[:, 2],
PhiField[:, 0, 0], PhiField[:, 0, 1], PhiField[:, 0, 2], PhiField[:, 0, 3],
PhiField[:, 1, 0], PhiField[:, 1, 1], PhiField[:, 1, 2], PhiField[:, 1, 3],
PhiField[:, 2, 0], PhiField[:, 2, 1], PhiField[:, 2, 2], PhiField[:, 2, 3],
pixelSearchCC,
returnStatus,
error,
deltaPhiNorm,
iterations]).T
if args.LAB1 is None:
TSVheader = "NodeNumber\tZpos\tYpos\tXpos\tFzz\tFzy\tFzx\tZdisp\tFyz\tFyy\tFyx\tYdisp\tFxz\tFxy\tFxx\tXdisp\tpixelSearchCC\treturnStatus\terror\tdeltaPhiNorm\titerations"
outMatrix = numpy.array([numpy.array(range(guidingPoints.shape[0])),
guidingPoints[:, 0], guidingPoints[:, 1], guidingPoints[:, 2],
PhiField[:, 0, 0], PhiField[:, 0, 1], PhiField[:, 0, 2], PhiField[:, 0, 3],
PhiField[:, 1, 0], PhiField[:, 1, 1], PhiField[:, 1, 2], PhiField[:, 1, 3],
PhiField[:, 2, 0], PhiField[:, 2, 1], PhiField[:, 2, 2], PhiField[:, 2, 3],
pixelSearchCC,
returnStatus,
error,
deltaPhiNorm,
iterations]).T
numpy.savetxt(args.OUT_DIR+"/"+args.PREFIX+".tsv",
outMatrix,
fmt='%.7f',
delimiter='\t',
newline='\n',
comments='',
header=TSVheader)
else: # Lab mode, need to pad one 0 row to the matrix
TSVheader = "Label\tZpos\tYpos\tXpos\tFzz\tFzy\tFzx\tZdisp\tFyz\tFyy\tFyx\tYdisp\tFxz\tFxy\tFxx\tXdisp\tpixelSearchCC\treturnStatus\terror\tdeltaPhiNorm\titerations"
outMatrix[0, :] = 0
numpy.savetxt(args.OUT_DIR+"/"+args.PREFIX+".tsv",
outMatrix,
fmt='%.7f',
delimiter='\t',
newline='\n',
comments='',
header=TSVheader)
# Collect data into VTK output
if args.VTK:
......
......@@ -50,8 +50,8 @@ class testAll(unittest.TestCase):
rm(testFolder+"balls-5a-pixelSearch.tsv")
rm(testFolder+"balls-5b-ddic.tsv")
rm(testFolder+"balls-5b-ddic.vtk")
#rm(testFolder+"balls-5c-pixelSearchPropagate.tsv")
#rm(testFolder+"balls-5d-ddic.tsv")
rm(testFolder+"balls-5c-pixelSearchPropagate.tsv")
rm(testFolder+"balls-5d-ddic.tsv")
rm(testFolder+"balls-5d-ddic.vtk")
rm(testFolder+"balls-6a-pixelSearch.tsv")
rm(testFolder+"extreme-im1-extreme-im2-ddic.tsv")
......@@ -125,7 +125,7 @@ class testAll(unittest.TestCase):
pass
def _test_gridDICtools(self):
def test_gridDICtools(self):
# Make test directory
if not os.path.isdir(testFolder):
os.mkdir(testFolder)
......@@ -548,7 +548,7 @@ class testAll(unittest.TestCase):
#for component in ["U", "e", "vol", "volss", "dev", "devss"]:
#self.assertTrue(numpy.nanmean(strain6b[component]), numpy.nanmean(VTK6b[component]), places=2)
def _test_ITKwatershed(self):
def test_ITKwatershed(self):
# Run on snow data
# load 3D image
snowRef = spam.datasets.loadSnow()
......@@ -568,7 +568,7 @@ class testAll(unittest.TestCase):
#def _test_gdic(self):
#pass
def _test_deformImage(self):
def test_deformImage(self):
im = spam.datasets.loadSnow()
tifffile.imsave(testFolder+"snow-ref.tif", im)
......@@ -1011,7 +1011,7 @@ class testAll(unittest.TestCase):
for label in [1,2]:
self.assertAlmostEqual(numpy.abs(TSVextreme['PhiField'][label, axis, -1] - 4.0), 0.0, places=1)
def _test_discreteStrain(self):
def test_discreteStrain(self):
# make sure it runs the help without error
exitCode = subprocess.call(["spam-discreteStrain", "--help"],
stdout=FNULL,
......
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