I have several functions inside a class that I applied augmentation to a numpy image array. I would like to know how to loop through all of them and apply those. For example:
Class Augmentation():
def rotation(data):
return rotated_image
def shear(data):
return sheared_image
def elasticity(data):
return enlarged_image
A=Augmentation()
My end result should be stacking all my functions. So for example: my data is (64,64) in shape. So after all my augmentations I should have a final numpy of (12,64,64). I currently tried creating different functions and then used
stack_of_images = np.stack(f1,f2,f3,....,f12)
stack_of_images.shape = (12,64,64)
I am using 12 different functions to augmentate numpy image arrays. I insert 1 image (64,64) and I get 12 images stacked (12,64,64).
You can do this by accessing the attribute dictionary of the type.
You can either get it with vars(Augmentation)
or Augmentation.__dict__
. Then, just iterate through the dict, and check for functions with callable
.
NOTE: querying vars(A)
or A.__dict__
(note it's the instance, not the class), will NOT include anything defined in the class, and in this case would be just {}
. You don't even have to create an instance in the first place.
NOTE2: It seems like you should tag all methods with the decorator @staticmethod
instead. Otherwise calling any method on an instance, like A.shear()
, would pass A
as data
instead, which is most likely not desired.
class foo:
@staticmethod
def bar(data):
...
Example:
methods = []
for attrname,attrvalue in vars(Augmentation).items():
if callable(attrvalue):
methods.append(attrvalue)
print([i.__name__ for i in methods])