Skip to content

Commit

Permalink
Automate naming from within function-style augmenters
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraving committed Jul 13, 2018
1 parent ca5f327 commit 8d3215e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
18 changes: 9 additions & 9 deletions imgaug/augmenters/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def AdditiveGaussianNoise(loc=0, scale=0, per_channel=False, name=None, determin
raise Exception("Expected float, int, tuple/list with 2 entries or StochasticParameter for argument 'scale'. Got %s." % (type(scale),))

if name is None:
name = "UnnamedAdditiveGaussianNoise"
name = "Unnamed%s" % (ia.caller_name(),)

return AddElementwise(Normal(loc=loc2, scale=scale2), per_channel=per_channel, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -644,7 +644,7 @@ def Dropout(p=0, per_channel=False, name=None, deterministic=False,
raise Exception("Expected p to be float or int or StochasticParameter, got %s." % (type(p),))

if name is None:
name = "UnnamedDropout"
name = "Unnamed%s" % (ia.caller_name(),)

return MultiplyElementwise(p2, per_channel=per_channel, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -779,7 +779,7 @@ def CoarseDropout(p=0, size_px=None, size_percent=None,
raise Exception("Either size_px or size_percent must be set.")

if name is None:
name = "UnnamedCoarseDropout"
name = "Unnamed%s" % (ia.caller_name(),)

return MultiplyElementwise(p3, per_channel=per_channel, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -953,7 +953,7 @@ def SaltAndPepper(p=0, per_channel=False, name=None, deterministic=False, random
"""
if name is None:
name = "UnnamedSaltAndPepper"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=p,
Expand Down Expand Up @@ -1068,7 +1068,7 @@ def CoarseSaltAndPepper(p=0, size_px=None, size_percent=None,
replacement = iap.Beta(0.5, 0.5) * 255

if name is None:
name = "UnnamedCoarseSaltAndPepper"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=mask_low,
Expand Down Expand Up @@ -1128,7 +1128,7 @@ def Salt(p=0, per_channel=False, name=None, deterministic=False, random_state=No
replacement = replacement01 * 255

if name is None:
name = "UnnamedSalt"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=p,
Expand Down Expand Up @@ -1248,7 +1248,7 @@ def CoarseSalt(p=0, size_px=None, size_percent=None,
replacement = replacement01 * 255

if name is None:
name = "UnnamedCoarseSalt"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=mask_low,
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def Pepper(p=0, per_channel=False, name=None, deterministic=False, random_state=
replacement = replacement01 * 255

if name is None:
name = "UnnamedPepper"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=p,
Expand Down Expand Up @@ -1430,7 +1430,7 @@ def CoarsePepper(p=0, size_px=None, size_percent=None,
replacement = replacement01 * 255

if name is None:
name = "UnnamedCoarsePepper"
name = "Unnamed%s" % (ia.caller_name(),)

return ReplaceElementwise(
mask=mask_low,
Expand Down
4 changes: 2 additions & 2 deletions imgaug/augmenters/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def AddToHueAndSaturation(value=0, per_channel=False, from_colorspace="RGB", cha
"""
if name is None:
name = "UnnamedAddToHueAndSaturation"
name = "Unnamed%s" % (ia.caller_name(),)

return WithColorspace(
to_colorspace="HSV",
Expand Down Expand Up @@ -412,6 +412,6 @@ def Grayscale(alpha=0, from_colorspace="RGB", name=None, deterministic=False, ra
"""
if name is None:
name = "UnnamedGrayscale"
name = "Unnamed%s" % (ia.caller_name(),)

return ChangeColorspace(to_colorspace=ChangeColorspace.GRAY, alpha=alpha, from_colorspace=from_colorspace, name=name, deterministic=deterministic, random_state=random_state)
8 changes: 4 additions & 4 deletions imgaug/augmenters/convolutional.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def create_matrices(image, nb_channels, random_state_func):
return [matrix] * nb_channels

if name is None:
name = "UnnamedSharpen"
name = "Unnamed%s" % (ia.caller_name(),)

return Convolve(create_matrices, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -327,7 +327,7 @@ def create_matrices(image, nb_channels, random_state_func):
return [matrix] * nb_channels

if name is None:
name = "UnnamedEmboss"
name = "Unnamed%s" % (ia.caller_name(),)

return Convolve(create_matrices, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -393,7 +393,7 @@ def create_matrices(image, nb_channels, random_state_func):
return [matrix] * nb_channels

if name is None:
name = "UnnamedEdgeDetect"
name = "Unnamed%s" % (ia.caller_name(),)

return Convolve(create_matrices, name=name, deterministic=deterministic, random_state=random_state)

Expand Down Expand Up @@ -534,6 +534,6 @@ def create_matrices(image, nb_channels, random_state_func):
return [matrix] * nb_channels

if name is None:
name = "UnnamedDirectedEdgeDetect"
name = "Unnamed%s" % (ia.caller_name(),)

return Convolve(create_matrices, name=name, deterministic=deterministic, random_state=random_state)
4 changes: 2 additions & 2 deletions imgaug/augmenters/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2268,7 +2268,7 @@ def func_keypoints_assert(keypoints_on_images, random_state, parents, hooks):
ia.do_assert(func_keypoints(keypoints_on_images, random_state, parents=parents, hooks=hooks), "Input keypoints did not fulfill user-defined assertion in AssertLambda.")
return keypoints_on_images
if name is None:
name = "UnnamedAssertLambda"
name = "Unnamed%s" % (ia.caller_name(),)
return Lambda(func_images_assert, func_keypoints_assert, name=name, deterministic=deterministic, random_state=random_state)


Expand Down Expand Up @@ -2379,6 +2379,6 @@ def func_keypoints(keypoints_on_images, random_state, parents, hooks):
return keypoints_on_images

if name is None:
name = "UnnamedAssertShape"
name = "Unnamed%s" % (ia.caller_name(),)

return Lambda(func_images, func_keypoints, name=name, deterministic=deterministic, random_state=random_state)
4 changes: 2 additions & 2 deletions imgaug/augmenters/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ def SimplexNoiseAlpha(first=None, second=None, per_channel=False,
)

if name is None:
name = "UnnamedSimplexNoiseAlpha"
name = "Unnamed%s" % (ia.caller_name(),)

return AlphaElementwise(
factor=noise, first=first, second=second, per_channel=per_channel,
Expand Down Expand Up @@ -926,7 +926,7 @@ def FrequencyNoiseAlpha(exponent=(-4, 4),
)

if name is None:
name = "UnnamedFrequencyNoiseAlpha"
name = "Unnamed%s" % (ia.caller_name(),)

return AlphaElementwise(
factor=noise, first=first, second=second, per_channel=per_channel,
Expand Down
4 changes: 2 additions & 2 deletions imgaug/augmenters/size.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def recursive_validate(v):
percent = recursive_validate(percent)

if name is None:
name = "UnnamedPad"
name = "Unnamed%s" % (ia.caller_name(),)

aug = CropAndPad(
px=px, percent=percent,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ def recursive_negate(v):
percent = recursive_negate(percent)

if name is None:
name = "UnnamedCrop"
name = "Unnamed%s" % (ia.caller_name(),)

aug = CropAndPad(
px=px, percent=percent,
Expand Down
12 changes: 12 additions & 0 deletions imgaug/imgaug.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ def is_callable(val):
else:
return callable(val)

def caller_name():
"""
Returns the name of the caller, e.g. a function.
Returns
-------
name : str
The name of the caller as a string
"""
return sys._getframe(1).f_code.co_name

def seed(seedval):
"""
Set the seed used by the global random state and thereby all randomness
Expand Down

0 comments on commit 8d3215e

Please sign in to comment.