Skip to content

Clip

Clip ¤

Bases: Subfunction_Base

A Subfunction class which which can be used for clipping intensity pixel values on a certain range.

Typical use case is clipping Hounsfield Units (HU) in CT scans for focusing on tissue types of interest.

Source code in aucmedi/data_processing/subfunctions/clip.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Clip(Subfunction_Base):
    """ A Subfunction class which which can be used for clipping intensity pixel
        values on a certain range.

    Typical use case is clipping Hounsfield Units (HU) in CT scans for focusing
    on tissue types of interest.
    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    def __init__(self, min=None, max=None, per_channel=False):
        """ Initialization function for creating a Clip Subfunction which can be passed to a
            [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

        Args:
            min (float or int or list):     Desired minimum value for clipping (if `None`, no lower limit is applied).
                                            Also possible to pass a list of minimum values if `per_channel=True`.
            max (float or int or list):     Desired maximum value for clipping (if `None`, no upper limit is applied).
                                            Also possible to pass a list of maximum values if `per_channel=True`.
            per_channel (bool):             Option if clipping should be applied per channel with different clipping ranges.
        """
        self.min = min
        self.max = max
        self.per_channel = per_channel

    #---------------------------------------------#
    #                Transformation               #
    #---------------------------------------------#
    def transform(self, image):
        # Perform clipping on all channels
        if not self.per_channel:
            image_clipped = np.clip(image, a_min=self.min, a_max=self.max)
        # Perform clipping on each channel
        else:
            image_clipped = image.copy()
            for c in range(0, image.shape[-1]):
                # Identify minimum to clip
                if self.min is not None and \
                    type(self.min) in [list, tuple, np.ndarray]:
                    min = self.min[c]
                else : min = self.min
                # Identify maximum to clip
                if self.max is not None and \
                    type(self.max) in [list, tuple, np.ndarray]:
                    max = self.max[c]
                else : max = self.max
                # Perform clipping
                image_clipped[..., c] = np.clip(image[...,c], 
                                                a_min=min, 
                                                a_max=max)
        # Return clipped image
        return image_clipped

__init__(min=None, max=None, per_channel=False) ¤

Initialization function for creating a Clip Subfunction which can be passed to a DataGenerator.

Parameters:

Name Type Description Default
min float or int or list

Desired minimum value for clipping (if None, no lower limit is applied). Also possible to pass a list of minimum values if per_channel=True.

None
max float or int or list

Desired maximum value for clipping (if None, no upper limit is applied). Also possible to pass a list of maximum values if per_channel=True.

None
per_channel bool

Option if clipping should be applied per channel with different clipping ranges.

False
Source code in aucmedi/data_processing/subfunctions/clip.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(self, min=None, max=None, per_channel=False):
    """ Initialization function for creating a Clip Subfunction which can be passed to a
        [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

    Args:
        min (float or int or list):     Desired minimum value for clipping (if `None`, no lower limit is applied).
                                        Also possible to pass a list of minimum values if `per_channel=True`.
        max (float or int or list):     Desired maximum value for clipping (if `None`, no upper limit is applied).
                                        Also possible to pass a list of maximum values if `per_channel=True`.
        per_channel (bool):             Option if clipping should be applied per channel with different clipping ranges.
    """
    self.min = min
    self.max = max
    self.per_channel = per_channel