Commit 921c3086 authored by Edward Andò's avatar Edward Andò
Browse files

safety case in getImagettesLabelled

parent 6ad539f6
Pipeline #62463 failed with stages
in 13 minutes and 23 seconds
......@@ -285,16 +285,16 @@ def contactPoints(lab, contactPairs, returnContactZones=False, boundingBoxes=Non
dilOnly = numpy.logical_xor(p2['subvol'], p1['subvol'])
labSlice = [slice(p1['slice'][0].start,p1['slice'][0].stop),
labSlice = (slice(p1['slice'][0].start,p1['slice'][0].stop),
slice(p1['slice'][1].start,p1['slice'][1].stop),
slice(p1['slice'][2].start,p1['slice'][2].stop)]
slice(p1['slice'][2].start,p1['slice'][2].stop))
labSubvol = lab[ labSlice ]
labSubvol = lab[labSlice]
if (dilOnly.shape == labSubvol.shape):
intersection = dilOnly * labSubvol
analysisVolume[ labSlice ][ intersection==label2 ] = n+1
analysisVolume[labSlice][intersection==label2] = n+1
else:
raise Exception(
......@@ -303,11 +303,11 @@ def contactPoints(lab, contactPairs, returnContactZones=False, boundingBoxes=Non
if returnContactZones:
return analysisVolume
else:
return spam.label.centresOfMass( analysisVolume )
return spam.label.centresOfMass(analysisVolume)
def labelledContacts( lab, maximumCoordinationNumber=20 ):
def labelledContacts(lab, maximumCoordinationNumber=20):
"""
Uniquely names contacts based on grains.
......
......@@ -387,7 +387,7 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
"""
Helper function to extract labels from a labelled image/volume.
A dictionary is returned with the a subvolume around the particle.
Passing boundingBoxes and centresOfMass is highly recommended.
Passing boundingBoxes and centresOfMass is highly recommended.
Parameters
----------
......@@ -738,6 +738,8 @@ def getImagettesLabelled(lab1, label, Phi, im1, im2, searchRange, boundingBoxes,
# If all of imagette2 is nans it fell outside im2 (or in any case it's going to be difficult to correlate)
if numpy.all(numpy.isnan(imagette2)):
returnStatus = -5
else:
imagette2 = None
return {'imagette1': imagette1,
'imagette1mask': imagette1mask,
......@@ -1649,7 +1651,7 @@ class Spheroid:
#imLab = imLab + labelDummy
#Update the labels
#imLab = spam.label.makeLabelsSequential(imLab)
#pbar.finish()
#print("\tlabel.fixUndersegmentation(): From "+str(len(listLabels))+" undersegmented labels, we successfully worked on "+str(successCounter))
#return imLab
......@@ -1689,7 +1691,7 @@ def convexVolume(lab, boundingBoxes=None, centresOfMass=None, volumes=None, nPro
--------
convexVolume : lab.max()x1 array of floats with the convex volume.
Note
----
convexVolume can only be computed for particles with volume greater than 3 voxels. If it is not the case, it will return 0.
......@@ -1708,16 +1710,16 @@ def convexVolume(lab, boundingBoxes=None, centresOfMass=None, volumes=None, nPro
volumes = spam.label.volumes(lab)
# Compute number of labels
nLabels = lab.max()
# Result array
convexVolume = numpy.zeros(nLabels+1, dtype='float')
if verbose:
widgets = [progressbar.FormatLabel(''), ' ', progressbar.Bar(), ' ', progressbar.AdaptiveETA()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=nLabels)
pbar.start()
finishedNodes = 0
# Function for convex volume
global computeConvexVolume
def computeConvexVolume(label):
......@@ -1735,7 +1737,7 @@ def convexVolume(lab, boundingBoxes=None, centresOfMass=None, volumes=None, nPro
return label, hullVol[-1]
except:
return label, 0
# Run multiprocessing
with multiprocessing.Pool(processes=nProcesses) as pool:
for returns in pool.imap_unordered(computeConvexVolume, range(1, nLabels+1)):
......@@ -1744,10 +1746,10 @@ def convexVolume(lab, boundingBoxes=None, centresOfMass=None, volumes=None, nPro
widgets[0] = progressbar.FormatLabel(" vol={:0>7.5f} ".format(returns[1]))
pbar.update(finishedNodes)
convexVolume[returns[0]] = returns[1]
if verbose:
pbar.finish()
return convexVolume
......@@ -1759,15 +1761,15 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
-----------
lab : 3D numpy array
Labelled image
PhiField : (multidimensional x 4 x 4 numpy array of floats)
Spatial field of Phis
returnStatus : lab.max()x1 array of ints, optional
Array with the return status for each label (usually returned by ``spam-ddic``)
If not defined (Default = None), all the labels will be moved
If returnStatus[i] == 2, the label will be moved, otherwise is omitted and erased from the final image
boundingBoxes : lab.max()x6 array of ints, optional
Bounding boxes in format returned by ``boundingBoxes``.
If not defined (Default = None), it is recomputed by running ``boundingBoxes``
......@@ -1775,28 +1777,28 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
centresOfMass : lab.max()x3 array of floats, optional
Centres of mass in format returned by ``centresOfMass``.
If not defined (Default = None), it is recomputed by running ``centresOfMass``
margin : int, optional
Margin, in pixels, to take in each label.
Default = 3
PhiCOM : bool, optional
Apply Phi to centre of mass of particle?, otherwise it will be applied in the middle of the particle\'s bounding box.
Default = True
threshold : float, optional
Threshold to keep interpolated voxels in the binary image.
Default = 0.5
labelDilate : int, optional
Number of times label should be dilated/eroded before returning it.
If ``labelDilate > 0`` a dilated label is returned, while ``labelDilate < 0`` returns an eroded label.
Default = 0
nProcesses : integer (optional, default = nProcessesDefault)
Number of processes for multiprocessing
Default = number of CPUs in the system
Returns
--------
labOut : 3D numpy array
......@@ -1824,7 +1826,7 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
else: # Add the particles
labelsToMove.append(label)
numberOfLabelsToMove += 1
# Function for moving labels
global funMoveLabels
def funMoveLabels(label):
......@@ -1836,7 +1838,7 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
centresOfMass=centresOfMass,
extractCube=True)
# Check that the label exist
if getLabelReturn is not None:
if getLabelReturn is not None:
# Get Phi field
Phi = PhiField[label].copy()
# Phi will be split into a local part and a part of floored displacements
......@@ -1866,7 +1868,7 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
topOfSlice = numpy.array([getLabelReturn['boundingBoxCube'][0] + disp[0],
getLabelReturn['boundingBoxCube'][2] + disp[1],
getLabelReturn['boundingBoxCube'][4] + disp[2]])
botOfSlice = numpy.array([getLabelReturn['boundingBoxCube'][1] + disp[0],
getLabelReturn['boundingBoxCube'][3] + disp[1],
getLabelReturn['boundingBoxCube'][5] + disp[2]])
......@@ -1881,7 +1883,7 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
grainSlice = (slice(topOfSliceCrop[0], botOfSliceCrop[0]),
slice(topOfSliceCrop[1], botOfSliceCrop[1]),
slice(topOfSliceCrop[2], botOfSliceCrop[2]))
# Update labSubvolDefMask
labSubvolDefMaskCrop = labSubvolDefMask[topOfSliceCrop[0]-topOfSlice[0] : labSubvolDefMask.shape[0]-1 + (botOfSliceCrop[0]-botOfSlice[0]),
topOfSliceCrop[1]-topOfSlice[1] : labSubvolDefMask.shape[1]-1 + (botOfSliceCrop[1]-botOfSlice[1]),
......@@ -1894,7 +1896,7 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
# Got None from getLabel()
else:
return label, 0,0,-1
# Create progressbar
widgets = [progressbar.FormatLabel(''), ' ', progressbar.Bar(), ' ', progressbar.AdaptiveETA()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=numberOfLabels)
......@@ -1909,11 +1911,11 @@ def moveLabels(lab, PhiField, returnStatus = None, boundingBoxes=None, centresOf
# Set voxels in slice to the value of the label if not in greyscale mode
if returns[0] > 0:
labOut[returns[1]][returns[2]] = returns[0]
# End progressbar
pbar.finish()
return labOut
......@@ -1925,10 +1927,10 @@ def erodeLabels(lab, erosion=1, boundingBoxes=None, centresOfMass=None, nProcess
-----------
lab : 3D numpy array
Labelled image
erosion : int, optional
Erosion level
boundingBoxes : lab.max()x6 array of ints, optional
Bounding boxes in format returned by ``boundingBoxes``.
If not defined (Default = None), it is recomputed by running ``boundingBoxes``
......@@ -1936,16 +1938,16 @@ def erodeLabels(lab, erosion=1, boundingBoxes=None, centresOfMass=None, nProcess
centresOfMass : lab.max()x3 array of floats, optional
Centres of mass in format returned by ``centresOfMass``.
If not defined (Default = None), it is recomputed by running ``centresOfMass``
nProcesses : integer (optional, default = nProcessesDefault)
Number of processes for multiprocessing
Default = number of CPUs in the system
Returns
--------
erodeImage : 3D numpy array
New labelled image with the eroded labels.
Note
----
The function makes use of spam.label.moveLabels() to generate the eroded image.
......@@ -1979,7 +1981,7 @@ def convexFillHoles(lab, boundingBoxes=None, centresOfMass=None):
-----------
lab : 3D numpy array
Labelled image
boundingBoxes : lab.max()x6 array of ints, optional
Bounding boxes in format returned by ``boundingBoxes``.
If not defined (Default = None), it is recomputed by running ``boundingBoxes``
......@@ -1992,7 +1994,7 @@ def convexFillHoles(lab, boundingBoxes=None, centresOfMass=None):
--------
labOut : 3D numpy array
New labelled image.
Note
----
The function works nicely for convex particles. For non-convex particles, it will alter the shape.
......@@ -2054,6 +2056,6 @@ def convexFillHoles(lab, boundingBoxes=None, centresOfMass=None):
# Update the progressbar
widgets[0] = progressbar.FormatLabel("{}/{} ".format(i, numberOfLabels))
pbar.update(i)
return labOut
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