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 ee68cc8a authored by Edward Andò's avatar Edward Andò
Browse files

Resolves #176

parent f442d29e
......@@ -177,34 +177,36 @@ class TestFunctionLabel(unittest.TestCase):
self.assertEqual(f[1, 1, 1], 1)
def test_getLabel(self):
# No-option get label
gl = spam.label.getLabel(threeCubedLabelVol, 1)
self.assertEqual(numpy.array([[[1]]], dtype=bool), gl['subvol'].tolist())
self.assertEqual(gl['slice'][0].start, 1)
self.assertEqual(gl['slice'][0].stop, 2)
self.assertEqual(gl['slice'][0].stop, 2)
self.assertEqual(gl['slice'][1].start, 1)
self.assertEqual(gl['slice'][1].stop, 2)
self.assertEqual(gl['slice'][1].stop, 2)
self.assertEqual(gl['slice'][2].start, 1)
self.assertEqual(gl['slice'][2].stop, 2)
# print gl['slice']
self.assertEqual(gl['slice'][2].stop, 2)
COM = spam.label.centresOfMass(threeCubedLabelVol)
self.assertEqual(COM[1].tolist(), gl['centreOfMassABS'].tolist())
self.assertEqual((COM[1] - numpy.array((1, 1, 1))).tolist(), gl['centreOfMassREL'].tolist())
self.assertEqual(gl['volume'], 1)
self.assertEqual(gl['volumeInitial'], 1)
BB = spam.label.boundingBoxes(threeCubedLabelVol)
# With extract cube
gl = spam.label.getLabel(threeCubedLabelVol, 1, boundingBoxes=BB, centresOfMass=COM, extractCube=True)
self.assertEqual(gl['slice'][0].start, 1)
self.assertEqual(gl['slice'][0].stop, 2)
self.assertEqual(gl['slice'][1].start, 1)
self.assertEqual(gl['slice'][1].stop, 2)
self.assertEqual(gl['slice'][2].start, 1)
self.assertEqual(gl['slice'][2].stop, 2)
self.assertEqual(gl['sliceCube'][0].start, 1)
self.assertEqual(gl['sliceCube'][0].stop, 2)
self.assertEqual(gl['sliceCube'][1].start, 1)
self.assertEqual(gl['sliceCube'][1].stop, 2)
self.assertEqual(gl['sliceCube'][2].start, 1)
self.assertEqual(gl['sliceCube'][2].stop, 2)
# print gl['slice']
COM = spam.label.centresOfMass(threeCubedLabelVol)
self.assertEqual(COM[1].tolist(), gl['centreOfMassABS'].tolist())
self.assertEqual((COM[1] - numpy.array((1, 1, 1))).tolist(), gl['centreOfMassREL'].tolist())
self.assertEqual(gl['volume'], 1)
self.assertEqual(gl['volumeInitial'], 1)
# Asking for a label that is not there
gl = spam.label.getLabel(threeCubedLabelVol, 100, boundingBoxes=BB, centresOfMass=COM, extractCube=True)
self.assertEqual(gl, None)
......@@ -267,7 +269,8 @@ class TestFunctionLabel(unittest.TestCase):
# Make sure with a box
vol = numpy.zeros((50,50,50), dtype=spam.label.labelType)
vol[0:40, 0:40, 0:40] = 1
self.assertAlmostEqual(spam.label.trueSphericity(vol, gaussianFilterSigma=0.5)[1], (36.0*numpy.pi*(vol.sum()**2.0))**(1/3.0) / (6*vol.sum()**(2/3.0)), places=1)
self.assertAlmostEqual(spam.label.trueSphericity(vol, gaussianFilterSigma=0.5)[1],
(36.0*numpy.pi*(vol.sum()**2.0))**(1/3.0) / (6*vol.sum()**(2/3.0)), places=1)
def test_loadDEMdata(self):
import spam.datasets
......
......@@ -437,12 +437,11 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
If unset, centres of mass are recalculated for every call.
extractCube : bool, optional
Whether returned label subvolume should be in the middle of a cube or its bounding box.
Should handle edges.
Default = no
Return label subvolume in the middle of a cube?
Default = False
extractCubeSize : int, optional
half-size of cube to exctract.
half-size of cube to extract.
Default = calculate minimum cube
margin : int, optional
......@@ -469,8 +468,13 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
subvol : 3D array of bools or ints
subvolume from labelled image
slice : tuple of 3*slices
Slice used to extract subvol -- however edge management complicates this a bit, perhaps an offset should also be returned
slice: tuple of 3*slices
Slice used to extract subvol for the bounding box mode
sliceCube: tuple of 3*slices
Slice used to extract subvol for the cube mode, warning,
if the label is near the edge, this is the slice up to the edge,
and so it will be smaller than the returned cube
centreOfMassABS : 3*float
Centre of mass with respect to ``labelVolume``
......@@ -478,9 +482,12 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
centreOfMassREL : 3*float
Centre of mass with respect to ``subvol``
volume: int
volumeInitial: int
Volume of label (before dilating)
volumeDilated: int
Volume of label (after dilating, if requested)
"""
import spam.mesh
if boundingBoxes is None:
......@@ -513,6 +520,10 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
#print("\tlabel.toolkit.getLabel(): Label {} does not exist".format(label))
else:
# Define output dictionary since we'll add different things to it
output = {}
output['centreOfMassABS'] = com
# We have a bounding box, let's extract it.
if extractCube:
# Calculate offsets between centre of mass and bounding box
......@@ -544,6 +555,12 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
sliceLimited, sliceOffset = _checkSlice(topOfSlice, botOfSlice, [0, 0, 0], numpy.array(labelledVolume.shape))
output['sliceCube'] = sliceLimited
output['centreOfMassREL'] = com - [sliceLimited[0].start - sliceOffset[0].start,
sliceLimited[1].start - sliceOffset[1].start,
sliceLimited[2].start - sliceOffset[2].start]
labSubVol[sliceOffset] = labelledVolume[sliceLimited].copy()
# We have a bounding box, let's extract it.
......@@ -551,24 +568,22 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
if margin is None:
margin = 0
topOfSlice = numpy.array([int(bbo[0] - margin),
int(bbo[2] - margin),
int(bbo[4] - margin)])
botOfSlice = numpy.array([int(bbo[1] + 1 + margin),
int(bbo[3] + 1 + margin),
int(bbo[5] + 1 + margin)])
sliceLimited = (slice(max(int(bbo[0] - margin), 0), min(int(bbo[1] + 1 + margin), labelledVolume.shape[0])),
slice(max(int(bbo[2] - margin), 0), min(int(bbo[3] + 1 + margin), labelledVolume.shape[1])),
slice(max(int(bbo[4] - margin), 0), min(int(bbo[5] + 1 + margin), labelledVolume.shape[2])))
labSubVol = numpy.zeros((botOfSlice[0] - topOfSlice[0],
botOfSlice[1] - topOfSlice[1],
botOfSlice[2] - topOfSlice[2]))
output['slice'] = sliceLimited
sliceLimited, sliceOffset = _checkSlice(topOfSlice, botOfSlice, [0, 0, 0], numpy.array(labelledVolume.shape))
output['centreOfMassREL'] = com - [sliceLimited[0].start,
sliceLimited[1].start,
sliceLimited[2].start]
labSubVol[sliceOffset] = labelledVolume[sliceLimited].copy()
labSubVol = labelledVolume[sliceLimited].copy()
# Get mask for this label
maskLab = labSubVol == label
volume = numpy.sum(maskLab)
output['volumeInitial'] = volume
# if we should mask, just return the mask.
if maskOtherLabels:
......@@ -586,14 +601,11 @@ def getLabel(labelledVolume, label, boundingBoxes=None, centresOfMass=None, marg
if labelDilate < 0:
strucuringElement = spam.mesh.structuringElement(radius=-1*labelDilate, order=2, dim=3)
labSubVol = scipy.ndimage.morphology.binary_erosion( labSubVol, structure=strucuringElement, iterations=1)
output['volumeDilated'] = labSubVol.sum()
return {'subvol': labSubVol,
'slice': sliceLimited,
'centreOfMassABS': com,
'centreOfMassREL': com - [sliceLimited[0].start - sliceOffset[0].start,
sliceLimited[1].start - sliceOffset[1].start,
sliceLimited[2].start - sliceOffset[2].start],
'volume': volume}
output['subvol'] = labSubVol
return output
def labelsOnEdges(lab):
......@@ -1152,6 +1164,8 @@ def trueSphericity(lab, boundingBoxes=None, centresOfMass=None, gaussianFilterSi
#feretOrientations[labelIndex,:] = numpy.concatenate([maxOrientation,minOrientation])
#return feretDiameters,feretOrientations
def meanOrientation(orientations):
"""
This function performs a Principal Component Analysis over a group of vectors in order to find the main direction of the set. Once the main direction is found, all the vectors are projected to the new basis.
......@@ -1297,7 +1311,7 @@ def fixUndersegmentation(imLab, imGrey, listLabels, a, c, numVect = 100, vect =
"""
This function fix undersegmented particles using directional erosion over the particle
to get the seed for a new localized watershed.
Parameters
-----------
imLab : 3D numpy array
......@@ -1343,7 +1357,6 @@ def fixUndersegmentation(imLab, imGrey, listLabels, a, c, numVect = 100, vect =
directional erosion
"""
#Check that a,c are valid inputs
if numpy.isnan(a) == True or numpy.isnan(c) == True:
print("\tlabel.fixUndersegmentation(): Parameters a or c are not valid. Some of them are NaN")
......
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