Shortcuts

Source code for torcheeg.transforms.label.string

import re

from typing import List, Union, Dict

from ..base_transform import LabelTransform


[docs]class StringToInt(LabelTransform): r''' Identify numbers in strings and convert strings to numbers. If there is no number in the string, the output corresponding to the string is 0. .. code-block:: python from torcheeg import transforms t = transforms.StringToInt() t(y='None')['y'] >>> 0 t = transforms.StringToInt() t(y='sub001')['y'] >>> 1 :obj:`StringToInt` allows converting a list of strings to a list of numbers with the same conversion behavior as a single string. .. code-block:: python t = transforms.StringToInt() t(y=['sub001', '4'])['y'] >>> 1, 4 .. automethod:: __call__ ''' def __init__(self): super(StringToInt, self).__init__()
[docs] def __call__(self, *args, y: Union[str, List[str]], **kwargs) -> Union[int, List[int]]: r''' Args: label (str): The input label or list of labels. Returns: int: The output label or list of labels after binarization. ''' return super().__call__(*args, y=y, **kwargs)
def apply(self, y: Union[str, List[str]], **kwargs) -> Union[int, List[int]]: if isinstance(y, list): return [self.opt(i) for i in y] return self.opt(y) def opt(self, y: str) -> int: if not isinstance(y, str): return y nums = re.findall(r"\d+", y) if len(nums) == 0: return 0 return int(nums[0])
Read the Docs v: latest
Versions
latest
stable
v1.1.2
v1.1.1
v1.1.0
v1.0.11
v1.0.10
v1.0.9
v1.0.8.post1
v1.0.8
v1.0.7
v1.0.6
v1.0.4
v1.0.3
v1.0.2
v1.0.1
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources