API reference¶
Augmentation¶
augmentation ¶
__all__
module-attribute
¶
__all__ = ['Transform', 'OneOf', 'Compose', 'RandomFlip', 'RandomRotation', 'ColorJitter', 'GaussianBlur', 'MedianBlur', 'MotionBlur', 'JPEGCompression', 'SaltAndPepper', 'GaussianNoise', 'StainNormalizationTransform']
Transform ¶
Bases: ABC
Base class for image augmentations.
Every augmentation that inherits from this class should implement
the apply method, which contains the actual image augmentation logic.
The augmentation is applied with probability prob. If the augmentation
is not applied, the original image is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float, (default=0.5)
|
Probability of applying the augmentation. The value should be in the range [0.0, 1.0]. |
0.5
|
Source code in src/histokit/augmentation/base.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | |
__init__ ¶
__init__(prob=0.5)
Initialize the augmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Probability of applying the augmentation. |
0.5
|
Source code in src/histokit/augmentation/base.py
21 22 23 24 25 26 27 28 29 30 | |
__call__ ¶
__call__(img: ndarray) -> np.ndarray
Apply the augmentation to an image.
The augmentation is applied only when a randomly generated value
is lower than prob.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Augmented image, or the original image if the augmentation was not applied. |
Source code in src/histokit/augmentation/base.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
apply
abstractmethod
¶
apply(img: ndarray) -> np.ndarray
Perform the augmentation.
This abstract method must be implemented by all subclasses of
Transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Augmented image. |
Source code in src/histokit/augmentation/base.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
OneOf ¶
Source code in src/histokit/augmentation/base.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
__init__ ¶
__init__(transforms, prob=0.5)
Randomly select and apply one augmentation from a list.
OneOf groups multiple transformations and applies at most one of them
during a single call. The whole operation is executed with probability
prob.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list
|
List of augmentations to choose from. Each element should be callable,
for example an instance of a class inheriting from |
required |
prob
|
float
|
Probability of applying one of the augmentations. |
0.5
|
Source code in src/histokit/augmentation/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
__call__ ¶
__call__(img: ndarray) -> np.ndarray
Apply one randomly selected augmentation to an image.
If a randomly generated value is lower than prob, one augmentation
is selected from transforms and applied to the image. Otherwise,
the original image is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Image after applying one randomly selected transformation, or the original image if no transformation was applied. |
Source code in src/histokit/augmentation/base.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
Compose ¶
Combine multiple augmentations into a sequential pipeline.
Augmentations are applied one after another in the order in which they
are provided in transforms. The output of one augmentation becomes
the input of the next augmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list
|
List of augmentations to apply sequentially. Each element should be callable and accept a NumPy array as input. |
required |
Source code in src/histokit/augmentation/base.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__init__ ¶
__init__(transforms)
Initialize the augmentation pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list
|
List of augmentations to apply in sequence. |
required |
Source code in src/histokit/augmentation/base.py
134 135 136 137 138 139 140 141 142 143 | |
__call__ ¶
__call__(img: ndarray) -> np.ndarray
Apply all augmentations sequentially to an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Image after applying all augmentations in sequence. |
Source code in src/histokit/augmentation/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
GaussianBlur ¶
Bases: Transform
Apply Gaussian blur to an image.
The blur strength is controlled by the standard deviation sigma,
which is randomly sampled from sigma_range each time the transform
is applied. Size of the Gaussian kernel is automatically determined
based on the sampled sigma. Uniform random sampling is used for the sigma value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma_range
|
tuple of float
|
Range from which the Gaussian sigma value is sampled. |
(0.3, 2.0)
|
prob
|
float, (default: 1.0)
|
Probability of applying the augmentation. |
1.0
|
Source code in src/histokit/augmentation/noise.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
__init__ ¶
__init__(sigma_range=(0.3, 2.0), prob=1.0)
Initialize the Gaussian blur augmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma_range
|
tuple of float
|
Minimum and maximum sigma values used for Gaussian blur. |
(0.3, 2.0)
|
prob
|
float, (default: 1.0)
|
Probability of applying the augmentation. |
1.0
|
Source code in src/histokit/augmentation/noise.py
22 23 24 25 26 27 28 29 30 31 32 33 34 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply Gaussian blur to the input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Blurred image. |
Source code in src/histokit/augmentation/noise.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
MedianBlur ¶
Bases: Transform
Apply median blur to an image.
The kernel size is randomly sampled from size_range. If the sampled
size is even, it is increased by one (OpenCV median blur requires
an odd kernel size). Uniform random sampling is used for the kernel size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_range
|
tuple of int
|
Range from which the median blur kernel size is sampled. |
(3, 7)
|
prob
|
float, (default: 1.0)
|
Probability of applying the transformation. |
1.0
|
Source code in src/histokit/augmentation/noise.py
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
__init__ ¶
__init__(size_range=(3, 7), prob=1.0)
Initialize the median blur transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_range
|
tuple of int
|
Minimum and maximum kernel size values. |
(3, 7)
|
prob
|
float, (default: 1.0)
|
Probability of applying the transform. |
1.0
|
Source code in src/histokit/augmentation/noise.py
69 70 71 72 73 74 75 76 77 78 79 80 81 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply median blur to the input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Image after applying median blur. |
Source code in src/histokit/augmentation/noise.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
MotionBlur ¶
Bases: Transform
Apply motion blur to an image.
A linear blur kernel is created with a randomly sampled degree and then rotated by a randomly sampled angle. The resulting kernel is applied to the image using 2D convolution. Uniform random sampling is used for both the degree and angle of the motion blur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degree_range
|
tuple of int
|
Range from which the motion blur kernel size is sampled. Higher values produce stronger blur. |
(5, 15)
|
angle_range
|
tuple of float
|
Range of rotation angles in degrees used for the motion blur direction. |
(0, 360)
|
prob
|
float
|
Probability of applying the transformation. |
1.0
|
Source code in src/histokit/augmentation/noise.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply motion blur to the input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degree_range
|
tuple of int
|
Minimum and maximum motion blur kernel sizes. |
required |
angle_range
|
tuple of float
|
Minimum and maximum motion blur angles in degrees. |
required |
prob
|
float
|
Probability of applying the transform. |
required |
Source code in src/histokit/augmentation/noise.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
JPEGCompression ¶
Bases: Transform
Perform JPEG compression.
The image is compressed to JPEG format using a randomly sampled quality value The Lower quality values produce stronger compression artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quality_range
|
tuple of int
|
Range from which the JPEG quality value is sampled. Values should usually be between 0 and 100. |
(10, 50)
|
prob
|
float
|
Probability of applying the transformation. |
1.0
|
Source code in src/histokit/augmentation/noise.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply JPEG compression to the input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quality_range
|
tuple of int
|
Range from which the JPEG quality value is sampled. |
(10, 50)
|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Source code in src/histokit/augmentation/noise.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
SaltAndPepper ¶
Bases: Transform
Randomly add salt-and-pepper noise to an image.
This transform randomly replaces a fraction of image pixels with either white values, called salt, or black values, called pepper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_range
|
tuple[float, float]
|
Range used to sample the fraction of pixels affected by noise.
The sampled value is multiplied by the image area |
(0.01, 0.05)
|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
The input image is expected to have pixel values in [0, 255].
The returned image has the same shape and dtype as the input image.
Source code in src/histokit/augmentation/noise.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply salt-and-pepper noise to an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image with shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Image with randomly selected pixels replaced by 0 or 255. The shape
and dtype are the same as in |
Source code in src/histokit/augmentation/noise.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
GaussianNoise ¶
Bases: Transform
Add random Gaussian noise to an image.
This transform samples a Gaussian noise distribution with random mean and
standard deviation, adds the noise to the input image, and clips the result
to the valid image intensity range [0, 255].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean_range
|
tuple[float, float]
|
Range used to sample the Gaussian noise mean. The value is sampled as
|
(0.0, 6.0)
|
std_range
|
tuple[float, float]
|
Range used to sample the Gaussian noise standard deviation. The value is
sampled as |
(10.0, 30.0)
|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
The input image is expected to have pixel values in [0, 255].
The returned image has dtype np.uint8.
Source code in src/histokit/augmentation/noise.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply Gaussian noise to an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image with shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Image with added Gaussian noise. The output has the same shape as
|
Source code in src/histokit/augmentation/noise.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
ColorJitter ¶
Bases: Transform
Randomly adjust brightness, contrast, saturation, and hue.
This transform applies simple color augmentations to an RGB image. Brightness and contrast are adjusted directly in RGB space, while saturation and hue are adjusted in HSV color space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
brightness_range
|
tuple[float, float]
|
Range used to sample the brightness factor. The final factor is computed
as |
(0, 0.3)
|
contrast_range
|
tuple[float, float]
|
Range used to sample the contrast factor. The final factor is computed
as |
(0, 0.3)
|
saturation_range
|
tuple[float, float]
|
Range used to sample the saturation factor. The final factor is computed
as |
(0, 0.3)
|
hue_range
|
tuple[float, float]
|
Range used to sample the hue shift. Values are interpreted as fractions
of OpenCV's hue range, where hue values are in |
(0, 0.1)
|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
The input image is expected to be an RGB image with pixel values in
[0, 255]. The returned image has dtype np.uint8.
Source code in src/histokit/augmentation/color.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 82 83 84 85 86 | |
RandomFlip ¶
Bases: Transform
Randomly flip an image horizontally and/or vertically.
This transform independently applies horizontal and vertical flips with
probability 1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
The returned array is made contiguous using np.ascontiguousarray.
Source code in src/histokit/augmentation/rotations.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply random horizontal and vertical flips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image with shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Flipped image with the same shape and dtype as |
Source code in src/histokit/augmentation/rotations.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
RandomRotation ¶
Bases: Transform
Randomly rotate an image by 90-degree increments.
This transform rotates the input image by a random multiple of 90 degrees, selected from the set {0, 1, 2, 3}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
The returned array is made contiguous using np.ascontiguousarray.
Source code in src/histokit/augmentation/rotations.py
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 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply a random 90-degree rotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input image with shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Rotated image. The output has the same dtype as |
Source code in src/histokit/augmentation/rotations.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
StainNormalizationTransform ¶
Bases: Transform
Apply stain normalization as an augmentation transform.
This transform wraps a stain normalizer object and applies its
transform method to the input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalizer
|
object
|
Fitted stain normalizer object. The object is expected to implement
a |
required |
prob
|
float
|
Probability of applying the transform. |
1.0
|
Notes
If stain normalization fails, a warning is emitted and an empty white image with the same shape and dtype as the input image is returned.
Source code in src/histokit/augmentation/stain_normalization.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | |
apply ¶
apply(img: ndarray) -> np.ndarray
Apply stain normalization to an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Input RGB image with shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Stain-normalized image. If normalization fails, returns a white
image with the same shape and dtype as |
Warns:
| Type | Description |
|---|---|
RuntimeWarning
|
If the normalizer fails to transform the input image. |
Source code in src/histokit/augmentation/stain_normalization.py
29 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 | |
Cohort processing¶
cohort ¶
__all__
module-attribute
¶
__all__ = ['SlidesConfig', 'CohortConfig', 'StageConfig', 'LoggingConfig', 'SourceConfig', 'CohortLogger', 'CohortRunner']
SlidesConfig
dataclass
¶
Source code in src/histokit/cohort/config.py
9 10 11 12 13 | |
CohortConfig
dataclass
¶
Source code in src/histokit/cohort/config.py
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
StageConfig
dataclass
¶
Source code in src/histokit/cohort/config.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 | |
LoggingConfig
dataclass
¶
Source code in src/histokit/cohort/config.py
17 18 19 20 21 22 | |
SourceConfig
dataclass
¶
Source code in src/histokit/cohort/config.py
25 26 27 | |
CohortLogger ¶
Source code in src/histokit/cohort/logger.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
CohortRunner ¶
Source code in src/histokit/cohort/runner.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
Patch Extraction¶
patch_extractors ¶
ParallelExtractor ¶
Source code in src/histokit/patch_extractors/parallel_extractor.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
PatchImageWriter ¶
Source code in src/histokit/patch_extractors/patch_writer.py
5 6 7 8 9 10 11 12 13 14 15 16 | |