encoders ¶
This file contains encoders classes for encoding various types of data.
Classes:
-
AbstractEncoder
–Abstract class for encoders.
-
NumericEncoder
–Encoder for float/int data.
-
NumericRankEncoder
–Encoder for float/int data that encodes the data based on their rank.
-
StrClassificationEncoder
–A string classification encoder that converts lists of strings into numeric labels using scikit-learn.
-
TextOneHotEncoder
–One hot encoder for text data.
AbstractEncoder ¶
Bases: ABC
Abstract class for encoders.
Encoders are classes that encode the raw data into torch.tensors. Different encoders provide different encoding methods. Different encoders may take different types of data as input.
Methods:
-
encode
–encodes a single data point
-
encode_all
–encodes a list of data points into a torch.tensor
-
encode_multiprocess
–encodes a list of data points using multiprocessing
-
decode
–decodes a single data point
Methods:
-
decode
–Decode a single data point.
-
encode
–Encode a single data point.
-
encode_all
–Encode a list of data points.
decode abstractmethod
¶
Decode a single data point.
This is an abstract method, child classes should overwrite it.
Parameters:
-
data
(Any
) –a single encoded data point
Returns:
-
decoded_data_point
(Any
) –the decoded data point
Source code in src/stimulus/data/encoding/encoders.py
58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
encode abstractmethod
¶
Encode a single data point.
This is an abstract method, child classes should overwrite it.
Parameters:
-
data
(Any
) –a single data point
Returns:
-
encoded_data_point
(Any
) –the encoded data point
Source code in src/stimulus/data/encoding/encoders.py
30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
encode_all abstractmethod
¶
Encode a list of data points.
This is an abstract method, child classes should overwrite it.
Parameters:
Returns:
-
encoded_data
(Tensor
) –encoded data points
Source code in src/stimulus/data/encoding/encoders.py
44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
NumericEncoder ¶
NumericEncoder(dtype: dtype = float32)
Bases: AbstractEncoder
Encoder for float/int data.
Attributes:
-
dtype
(dtype
) –The data type of the encoded data. Default = torch.float32 (32-bit floating point)
Parameters:
-
dtype
(dtype
, default:float32
) –the data type of the encoded data. Default = torch.float (32-bit floating point)
Methods:
-
decode
–Decodes the data.
-
encode
–Encodes the data.
-
encode_all
–Encodes the data.
Source code in src/stimulus/data/encoding/encoders.py
309 310 311 312 313 314 315 |
|
decode ¶
Decodes the data.
Parameters:
-
data
(Tensor
) –the encoded data
Returns:
Source code in src/stimulus/data/encoding/encoders.py
349 350 351 352 353 354 355 356 357 358 |
|
encode ¶
encode(data: float) -> Tensor
Encodes the data.
This method takes as input a single data point, should be mappable to a single output.
Parameters:
-
data
(float
) –a single data point
Returns:
-
encoded_data_point
(Tensor
) –the encoded data point
Source code in src/stimulus/data/encoding/encoders.py
317 318 319 320 321 322 323 324 325 326 327 328 |
|
encode_all ¶
Encodes the data.
This method takes as input a list of data points, or a single float, and returns a torch.tensor.
Parameters:
Returns:
-
encoded_data
(Tensor
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
NumericRankEncoder ¶
NumericRankEncoder(*, scale: bool = False)
Bases: AbstractEncoder
Encoder for float/int data that encodes the data based on their rank.
Attributes:
-
scale
(bool
) –whether to scale the ranks to be between 0 and 1. Default = False
Methods:
-
encode
–encodes a single data point
-
encode_all
–encodes a list of data points into a torch.tensor
-
decode
–decodes a single data point
-
_check_input_dtype
–checks if the input data is int or float data
Parameters:
-
scale
(bool
, default:False
) –whether to scale the ranks to be between 0 and 1. Default = False
Methods:
-
decode
–Returns an error since decoding does not make sense without encoder information, which is not yet supported.
-
encode
–Returns an error since encoding a single float does not make sense.
-
encode_all
–Encodes the data.
Source code in src/stimulus/data/encoding/encoders.py
478 479 480 481 482 483 484 |
|
decode ¶
Returns an error since decoding does not make sense without encoder information, which is not yet supported.
Source code in src/stimulus/data/encoding/encoders.py
514 515 516 |
|
encode ¶
encode(data: Any) -> Tensor
Returns an error since encoding a single float does not make sense.
Source code in src/stimulus/data/encoding/encoders.py
486 487 488 |
|
encode_all ¶
Encodes the data.
This method takes as input a list of data points, and returns the ranks of the data points. The ranks are normalized to be between 0 and 1, when scale is set to True.
Parameters:
Returns:
-
encoded_data
(Tensor
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
StrClassificationEncoder ¶
StrClassificationEncoder(*, scale: bool = False)
Bases: AbstractEncoder
A string classification encoder that converts lists of strings into numeric labels using scikit-learn.
When scale is set to True, the labels are scaled to be between 0 and 1.
Attributes:
-
scale
(bool
) –Whether to scale the labels to be between 0 and 1. Default = False
Methods:
-
encode
–str) -> int: Raises a NotImplementedError, as encoding a single string is not meaningful in this context.
-
encode_all
–list[str]) -> torch.tensor: Encodes an entire list of string data into a numeric representation using LabelEncoder and returns a torch tensor. Ensures that the provided data items are valid strings prior to encoding.
-
decode
–Any) -> Any: Raises a NotImplementedError, as decoding is not supported with the current design.
-
_check_dtype
–list[str]) -> None: Validates that all items in the data list are strings, raising a ValueError otherwise.
Parameters:
-
scale
(bool
, default:False
) –whether to scale the labels to be between 0 and 1. Default = False
Methods:
-
decode
–Returns an error since decoding does not make sense without encoder information, which is not yet supported.
-
encode
–Returns an error since encoding a single string does not make sense.
-
encode_all
–Encodes the data.
Source code in src/stimulus/data/encoding/encoders.py
406 407 408 409 410 411 412 |
|
decode ¶
Returns an error since decoding does not make sense without encoder information, which is not yet supported.
Source code in src/stimulus/data/encoding/encoders.py
446 447 448 |
|
encode ¶
Returns an error since encoding a single string does not make sense.
Parameters:
-
data
(str
) –a single string
Source code in src/stimulus/data/encoding/encoders.py
414 415 416 417 418 419 420 |
|
encode_all ¶
Encodes the data.
This method takes as input a list of data points, should be mappable to a single output, using LabelEncoder from scikit learn and returning a numpy array. For more info visit : https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html
Parameters:
Returns:
-
encoded_data
(tensor
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
TextOneHotEncoder ¶
TextOneHotEncoder(
alphabet: str = "acgt",
*,
convert_lowercase: bool = False,
padding: bool = False
)
Bases: AbstractEncoder
One hot encoder for text data.
NOTE encodes based on the given alphabet If a character c is not in the alphabet, c will be represented by a vector of zeros.
Attributes:
-
alphabet
(str
) –the alphabet to one hot encode the data with.
-
convert_lowercase
(bool
) –whether to convert the sequence and alphabet to lowercase. Default is False.
-
padding
(bool
) –whether to pad the sequences with zeros. Default is False.
-
encoder
(OneHotEncoder
) –preprocessing.OneHotEncoder object initialized with self.alphabet
Methods:
-
encode
–encodes a single data point
-
encode_all
–encodes a list of data points into a numpy array
-
encode_multiprocess
–encodes a list of data points using multiprocessing
-
decode
–decodes a single data point
-
_sequence_to_array
–transforms a sequence into a numpy array
Parameters:
-
alphabet
(str
, default:'acgt'
) –the alphabet to one hot encode the data with.
Raises:
-
TypeError
–If the input alphabet is not a string.
Methods:
-
decode
–Decodes one-hot encoded tensor back to sequences.
-
encode
–One hot encodes a single sequence.
-
encode_all
–Encodes a list of sequences.
-
encode_multiprocess
–Encodes a list of sequences using multiprocessing.
Source code in src/stimulus/data/encoding/encoders.py
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 |
|
decode ¶
Decodes one-hot encoded tensor back to sequences.
Parameters:
-
data
(Tensor
) –2D or 3D tensor of one-hot encoded sequences - 2D shape: (sequence_length, alphabet_size) - 3D shape: (batch_size, sequence_length, alphabet_size)
NOTE that when decoding 3D shape tensor, it assumes all sequences have the same length.
Returns:
Raises:
-
TypeError
–If the input data is not a 2D or 3D tensor
Source code in src/stimulus/data/encoding/encoders.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
encode ¶
encode(data: str) -> Tensor
One hot encodes a single sequence.
Takes a single string sequence and returns a torch tensor of shape (sequence_length, alphabet_length). The returned tensor corresponds to the one hot encoding of the sequence. Unknown characters are represented by a vector of zeros.
Parameters:
-
data
(str
) –single sequence
Returns:
-
encoded_data_point
(Tensor
) –one hot encoded sequence
Raises:
-
TypeError
–If the input data is not a string.
Examples:
>>> encoder = TextOneHotEncoder(alphabet="acgt")
>>> encoder.encode("acgt")
tensor([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
>>> encoder.encode("acgtn")
tensor([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]])
>>> encoder = TextOneHotEncoder(alphabet="ACgt")
>>> encoder.encode("acgt")
tensor([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
>>> encoder.encode("ACgt")
tensor([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
Source code in src/stimulus/data/encoding/encoders.py
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 |
|
encode_all ¶
Encodes a list of sequences.
Takes a list of string sequences and returns a torch tensor of shape (number_of_sequences, sequence_length, alphabet_length). The returned tensor corresponds to the one hot encoding of the sequences. Unknown characters are represented by a vector of zeros.
Parameters:
Returns:
-
encoded_data
(Tensor
) –one hot encoded sequences
Raises:
-
TypeError
–If the input data is not a list or a string.
-
ValueError
–If all sequences do not have the same length when padding is False.
Examples:
>>> encoder = TextOneHotEncoder(alphabet="acgt")
>>> encoder.encode_all(["acgt", "acgtn"])
tensor([[[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]], // this is padded with zeros
[[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]]])
Source code in src/stimulus/data/encoding/encoders.py
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 |
|
encode_multiprocess ¶
Encodes a list of sequences using multiprocessing.
Source code in src/stimulus/data/encoding/encoders.py
195 196 197 198 |
|