transforms ¶
This file contains noise generators classes for generating various types of noise.
Classes:
-
AbstractAugmentationGenerator
–Abstract class for augmentation generators.
-
AbstractNoiseGenerator
–Abstract class for noise generators.
-
AbstractSampler
–Abstract class for samplers.
-
AbstractTransform
–Abstract class for data transformers.
-
BalanceSampler
–Balance the data by sampling n samples from each class where n is the size of the smallest class.
-
GaussianChunk
–Subset data around a random midpoint.
-
GaussianNoise
–Add Gaussian noise to data.
-
RandomDownSampler
–A transformer that randomly samples a dataset to a specified size n.
-
ReverseComplement
–Reverse complement biological sequences.
-
SwapTransform
–Swap the values of pairs of elemengs in the data n-times with replacement.
-
UniformTextMasker
–Mask characters in text.
AbstractAugmentationGenerator ¶
AbstractAugmentationGenerator()
Bases: AbstractTransform
Abstract class for augmentation generators.
All augmentation function should have the seed in it. This is because the multiprocessing of them could unset the seed.
Methods:
-
transform
–Transforms a single data point.
-
transform_all
–Transforms a list of data points.
Source code in src/stimulus/data/transforming/transforms.py
88 89 90 91 92 |
|
transform abstractmethod
¶
Transforms a single data point.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(Any
) –the data to be transformed
Returns:
-
transformed_data
(Any
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
transform_all abstractmethod
¶
Transforms a list of data points.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
AbstractNoiseGenerator ¶
AbstractNoiseGenerator()
Bases: AbstractTransform
Abstract class for noise generators.
All noise function should have the seed in it. This is because the multiprocessing of them could unset the seed.
Methods:
-
transform
–Transforms a single data point.
-
transform_all
–Transforms a list of data points.
Source code in src/stimulus/data/transforming/transforms.py
75 76 77 78 79 |
|
transform abstractmethod
¶
Transforms a single data point.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(Any
) –the data to be transformed
Returns:
-
transformed_data
(Any
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
transform_all abstractmethod
¶
Transforms a list of data points.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
AbstractSampler ¶
AbstractSampler()
Bases: AbstractTransform
Abstract class for samplers.
Sampler classes are expected to return np.nan in place of datapoints to be removed.
Methods:
-
transform
–Transforms a single data point.
-
transform_all
–Transforms a list of data points.
Source code in src/stimulus/data/transforming/transforms.py
101 102 103 104 105 |
|
transform abstractmethod
¶
Transforms a single data point.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(Any
) –the data to be transformed
Returns:
-
transformed_data
(Any
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
transform_all abstractmethod
¶
Transforms a list of data points.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
AbstractTransform ¶
AbstractTransform()
Bases: ABC
Abstract class for data transformers.
Data transformers implement in_place or augmentation transformations. Whether it is in_place or augmentation is specified in the "add_row" attribute (should be True or False and set in children classes constructor)
Child classes should override the transform
and transform_all
methods.
transform_all
should always return a list
Both methods should take an optional seed
argument set to None
by default to be compliant with stimulus' core principle of reproducibility. Seed should be initialized through np.random.seed(seed)
in the method implementation.
Attributes:
-
add_row
(bool
) –whether the transformer adds rows to the data
Methods:
-
transform
–transforms a data point
-
transform_all
–transforms a list of data points
Source code in src/stimulus/data/transforming/transforms.py
32 33 34 35 36 |
|
transform abstractmethod
¶
Transforms a single data point.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(Any
) –the data to be transformed
Returns:
-
transformed_data
(Any
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
transform_all abstractmethod
¶
Transforms a list of data points.
This is an abstract method that should be implemented by the child class.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
BalanceSampler ¶
BalanceSampler()
Bases: AbstractSampler
Balance the data by sampling n samples from each class where n is the size of the smallest class.
This sampler class balances the data by sampling n samples from each class where n is the size of the smallest class.
Methods:
-
transform
–Raises NotImplementedError, as it does not make sense to balance a single data point.
-
transform_all
–Balance the data by sampling n samples from each class where n is the size of the smallest class.
Source code in src/stimulus/data/transforming/transforms.py
344 345 346 |
|
transform ¶
Raises NotImplementedError, as it does not make sense to balance a single data point.
Source code in src/stimulus/data/transforming/transforms.py
348 349 350 |
|
transform_all ¶
Balance the data by sampling n samples from each class where n is the size of the smallest class.
Parameters:
-
data
(list
) –the data to be balanced
Returns:
-
transformed_data
(list
) –the balanced data
Source code in src/stimulus/data/transforming/transforms.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
GaussianChunk ¶
Bases: AbstractAugmentationGenerator
Subset data around a random midpoint.
This augmentation strategy chunks the input sequences, for which the middle positions are obtained through a gaussian distribution.
In concrete, it changes the middle position (ie. peak summit) to another position. This position is chosen based on a gaussian distribution, so the region close to the middle point are more likely to be chosen than the rest. Then a chunk with size chunk_size
around the new middle point is returned. This process will be repeated for each sequence with transform_all
.
Methods:
-
transform
–chunk a single list
-
transform_all
–chunks multiple lists
Parameters:
-
chunk_size
(int
) –Size of chunks to extract
-
seed
(int
, default:42
) –Random seed for reproducibility
-
std
(float
, default:1
) –Standard deviation for the Gaussian distribution
Source code in src/stimulus/data/transforming/transforms.py
274 275 276 277 278 279 280 281 282 283 284 285 |
|
transform ¶
Chunks a sequence of size chunk_size from the middle position +/- a value obtained through a gaussian distribution.
Parameters:
-
data
(str
) –the sequence to be transformed
Returns:
-
transformed_data
(str
) –the chunk of the sequence
Raises:
-
AssertionError
–if the input data is shorter than the chunk size
Source code in src/stimulus/data/transforming/transforms.py
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 |
|
transform_all ¶
Adds chunks to multiple lists using multiprocessing.
Parameters:
-
data
(list
) –the sequences to be transformed
Returns:
-
transformed_data
(list
) –the transformed sequences
Source code in src/stimulus/data/transforming/transforms.py
324 325 326 327 328 329 330 331 332 333 334 335 |
|
GaussianNoise ¶
Bases: AbstractNoiseGenerator
Add Gaussian noise to data.
This noise generator adds Gaussian noise to float values.
Methods:
-
transform
–adds noise to a single data point
-
transform_all
–adds noise to a list of data points
Parameters:
-
mean
(float
, default:0
) –Mean of the Gaussian noise
-
std
(float
, default:1
) –Standard deviation of the Gaussian noise
-
seed
(int
, default:42
) –Random seed for reproducibility
Source code in src/stimulus/data/transforming/transforms.py
168 169 170 171 172 173 174 175 176 177 178 179 |
|
transform ¶
Adds Gaussian noise to a single point of data.
Parameters:
-
data
(float
) –the data to be transformed
Returns:
-
transformed_data
(float
) –the transformed data point
Source code in src/stimulus/data/transforming/transforms.py
181 182 183 184 185 186 187 188 189 190 191 |
|
transform_all ¶
Adds Gaussian noise to a list of data points.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data points
Source code in src/stimulus/data/transforming/transforms.py
193 194 195 196 197 198 199 200 201 202 203 |
|
RandomDownSampler ¶
A transformer that randomly samples a dataset to a specified size n.
This transformer reduces the total size of the dataset by randomly selecting n samples while maintaining the original ordering with np.nan for removed items.
Parameters:
-
n
(int
) –target size of the sampled dataset
Methods:
-
transform
–Raises NotImplementedError, as it does not make sense to balance a single data point.
-
transform_all
–Transform the data by randomly sampling n items.
Source code in src/stimulus/data/transforming/transforms.py
439 440 441 442 443 444 445 446 447 |
|
transform ¶
Raises NotImplementedError, as it does not make sense to balance a single data point.
Source code in src/stimulus/data/transforming/transforms.py
449 450 451 |
|
transform_all ¶
Transform the data by randomly sampling n items.
Parameters:
-
data
(list
) –the data to be sampled
Returns:
-
transformed_data
(list
) –the sampled data with np.nan for removed items
Source code in src/stimulus/data/transforming/transforms.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
ReverseComplement ¶
ReverseComplement(sequence_type: str = 'DNA')
Bases: AbstractAugmentationGenerator
Reverse complement biological sequences.
This augmentation strategy reverse complements the input nucleotide sequences.
Methods:
-
transform
–reverse complements a single data point
-
transform_all
–reverse complements a list of data points
Raises:
-
ValueError
–if the type of the sequence is not DNA or RNA
Parameters:
-
sequence_type
(str
, default:'DNA'
) –Type of sequence ('DNA' or 'RNA')
Source code in src/stimulus/data/transforming/transforms.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
transform ¶
Returns the reverse complement of a list of string data using the complement_mapping.
Parameters:
-
data
(str
) –the sequence to be transformed
Returns:
-
transformed_data
(str
) –the reverse complement of the sequence
Source code in src/stimulus/data/transforming/transforms.py
235 236 237 238 239 240 241 242 243 244 |
|
transform_all ¶
Reverse complement multiple data points using multiprocessing.
Parameters:
-
data
(list
) –the sequences to be transformed
Returns:
-
transformed_data
(list
) –the reverse complement of the sequences
Source code in src/stimulus/data/transforming/transforms.py
246 247 248 249 250 251 252 253 254 255 256 257 |
|
SwapTransform ¶
Bases: AbstractTransform
Swap the values of pairs of elemengs in the data n-times with replacement.
This transform swaps the values of pairs of elements in the data n-times with replacement. E.g if the data is [1, 2, 3, 4, 5] and swap_numbers is 2, the output could be [2, 1, 4, 3, 5].
Parameters:
-
swap_numbers
(float
, default:1
) –Number of swaps to perform
-
seed
(int
, default:42
) –Random seed for reproducibility
Methods:
-
transform
–Swap the values of two random elements in the data.
-
transform_all
–Swap the values of two random elements in the data n times.
Source code in src/stimulus/data/transforming/transforms.py
389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
transform ¶
Swap the values of two random elements in the data.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data
Source code in src/stimulus/data/transforming/transforms.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
transform_all ¶
Swap the values of two random elements in the data n times.
Parameters:
-
data
(list
) –the data to be transformed
Source code in src/stimulus/data/transforming/transforms.py
420 421 422 423 424 425 426 427 428 429 |
|
UniformTextMasker ¶
Bases: AbstractNoiseGenerator
Mask characters in text.
This noise generators replace characters with a masking character with a given probability.
Methods:
-
transform
–adds character masking to a single data point
-
transform_all
–adds character masking to a list of data points
Parameters:
-
probability
(float
, default:0.1
) –Probability of masking each character
-
mask
(str
, default:'*'
) –Character to use for masking
-
seed
(int
, default:42
) –Random seed for reproducibility
Source code in src/stimulus/data/transforming/transforms.py
118 119 120 121 122 123 124 125 126 127 128 129 |
|
transform ¶
Adds character masking to the data.
Parameters:
-
data
(str
) –the data to be transformed
Returns:
-
transformed_data
(str
) –the transformed data point
Source code in src/stimulus/data/transforming/transforms.py
131 132 133 134 135 136 137 138 139 140 141 |
|
transform_all ¶
Adds character masking to multiple data points using multiprocessing.
Parameters:
-
data
(list
) –the data to be transformed
Returns:
-
transformed_data
(list
) –the transformed data points
Source code in src/stimulus/data/transforming/transforms.py
143 144 145 146 147 148 149 150 151 152 153 154 155 |
|