Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_sub does not work with D107A because it has a letter in its name #123

Open
jimzhang629 opened this issue Sep 24, 2024 · 0 comments
Open
Assignees

Comments

@jimzhang629
Copy link
Contributor

jimzhang629 commented Sep 24, 2024

def get_sub(inst: Signal | mne.Info | str) -> str:
    """Gets the subject from the instance

    Parameters
    ----------
    inst : Signal
        The instance to get the subject from

    Returns
    -------
    str
        The subject"""
    if isinstance(inst, Signal):
        inst = inst.info
    elif isinstance(inst, str):
        return f"{inst[0]}{int(inst[1:])}"
    out_str = inst['subject_info']['his_id'][4:]
    if len(out_str) == 1:
        return out_str
    return out_str[0] + str(int("".join(s for s in out_str if s.isdigit())))

The current implementation of get_sub (located in ieeg/viz/mri.py) will strip away the letters in a subject name, if they exist. For example, inputting D0107A will return D107, not D107A.

I propose a new implementation of get_sub below that just strips the leading zeroes. It will strip zeroes until it encounters a non-zero value. This should keep the suffix intact. For example, inputting D0107A will return D107A.

import re

def get_sub(inst: Signal | mne.Info | str) -> str:
    """Gets the subject ID from the instance, removes leading zeros from the numeric parts,
    and preserves any letters (e.g., 'D0107A' becomes 'D107A', 'D0059A' becomes 'D59A').

    Parameters
    ----------
    inst : Signal | mne.Info | str
        The instance (Signal, Info, or string) to get the subject ID from.

    Returns
    -------
    str
        The subject ID with leading zeros removed from the numeric parts (e.g., 'D0107A' becomes 'D107A').
    """
    if isinstance(inst, Signal):
        inst = inst.info
    
    # If the instance is a string, return it after processing
    if isinstance(inst, str):
        # Use regex to match and remove leading zeros from the first numeric part
        return re.sub(r'(\D*)(0*)(\d+)', lambda m: m.group(1) + m.group(3), inst)

    # Extract subject ID from 'subject_info'
    out_str = inst['subject_info']['his_id'][4:]

    # Use regex to match and remove leading zeros from the first numeric part
    return re.sub(r'(\D*)(0*)(\d+)', lambda m: m.group(1) + m.group(3), out_str)
@jimzhang629 jimzhang629 self-assigned this Sep 24, 2024
@jimzhang629 jimzhang629 changed the title get_sub does not work with D107A because it has a letter in it's name get_sub does not work with D107A because it has a letter in its name Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant