import re


def parse_prob_label(label):
    """
    Parse the label into components. Currently 
    
    (name)_ndx3_ndy7_std_h0.n(sample_size)

    - The dot . separates the file name from options.
    - (name) can contain _.
    - (sample_size) must be less than the total size of the original data.
    - _ndx%d is to add %d dimensional standard Gaussian noise to X. Optional
      argument.
    - _ndy%d is to add %d dimensional standard Gaussian noise to Y. Optional
      argument.
    - _c (optional) to denote that Y is discrete (classification problem)
    - _std may or may not be present. If present, standardize the data in each 
        trial after resampling.
    - _h0 may or may not be present. If present, shuffle the sample to simulate 
        the case where H0 is true.

    Return a dictionary with keys and values.
    """
    pat = r'(\w+?)(_ndx(\d+))?(_ndy(\d+))?(_c)?(_std)?(_h0)?[.]n(\d+)'
    m = re.search(pat, label)
    if m is None:
        raise ValueError('prob_label "%s" does not follow the pattern "%s".'%(label, pat))

    fname = m.group(1)
    ndx = m.group(3) 
    ndx = 0 if ndx is None else int(ndx)

    ndy = m.group(5)
    ndy = 0 if ndy is None else int(ndy)

    is_classification = True if m.group(6) is not None else False
    is_std = True if m.group(7) is not None else False
    is_shuffled = True if m.group(8) is not None else False
    n = int(m.group(9))

    D = {'name': fname, 'ndx': ndx, 'ndy': ndy, 'is_classification':
            is_classification, 'is_std': is_std, 'is_h0': is_shuffled, 'n': n}
    return D