Пример #1
0
def est_field_strength(txt):

    fields = []

    for ptn in field_ptn:

        matches = rex_flex(ptn, txt, fun=re.finditer)

        for match in matches:

            short_context, _ = rex_ctx(match=match, txt=txt, nchar=50)

            if not re.search(mag_ptn, short_context, re.I):
                continue

            # Skip if significance test; easy to confuse with e.g.
            # "3.0 T, p < 0.05"
            post_context, _ = rex_ctx(match, txt, nchar_pre=0, nchar_post=50)
            if rex_flex(sig_ptn, post_context):
                continue

            field_str = match.groupdict()['field']

            # Try to cast field strength to float
            try:
                field = float(field_str)
            except (ValueError, TypeError):
                continue

            if MIN_FIELD_STRENGTH <= field <= MAX_FIELD_STRENGTH:

                context, span = rex_ctx(match, txt)
                group = match.group()

                fields.append({
                    'value': field,
                    'context': context,
                    'group': group,
                    'span': span,
                })

    return fields
Пример #2
0
def est_smooth_kernel(txt):

    # Initialize FWHM
    kernels = []

    ctxt = re.sub(r'[():=]', '', txt)

    for ptn in smooth_spatial_ptn:

        matches = rex_flex(ptn, ctxt, re.finditer)

        for match in matches:

            kernel_str = match.groupdict()['kernel']

            # Handle ...
            six_match = re.search('(\d+?)6(\d+?)6(\d+)', kernel_str)
            if six_match:
                if len(set(six_match.groups())) == 1:
                    kernel_str = six_match.groups()[-1]

            # Skip if match preceded by number; prevents matches on input like
            # "6 .5 mm fwhm"
            context_back, _ = rex_ctx(match, ctxt, nchar_pre=10, nchar_post=0)
            before_group = context_back.replace(match.group(), '')
            if re.search(r'[.\d]\s+$', before_group):
                continue

            # Skip if not float
            try:
                kernel = float(kernel_str)
            except (ValueError, TypeError):
                continue

            # Skip if implausible
            if kernel > MAX_FWHM:
                continue

            context, span = rex_ctx(match, ctxt)
            group = match.group()
            kernels.append({
                'value': kernel,
                'context': context,
                'group': group,
                'span': span,
            })

    return kernels
Пример #3
0
def est_smooth_kernel(txt):

    # Initialize FWHM
    kernels = []

    ctxt = re.sub(r'[():=]', '', txt)

    for ptn in smooth_spatial_ptn:

        matches = rex_flex(ptn, ctxt, re.finditer)

        for match in matches:

            kernel_str = match.groupdict()['kernel']

            # Handle ...
            six_match = re.search('(\d+?)6(\d+?)6(\d+)', kernel_str)
            if six_match:
                if len(set(six_match.groups())) == 1:
                    kernel_str = six_match.groups()[-1]

            # Skip if match preceded by number; prevents matches on input like
            # "6 .5 mm fwhm"
            context_back, _ = rex_ctx(match, ctxt, nchar_pre=10, nchar_post=0)
            before_group = context_back.replace(match.group(), '')
            if re.search(r'[.\d]\s+$', before_group):
                continue

            # Skip if not float
            try:
                kernel = float(kernel_str)
            except (ValueError, TypeError):
                continue

            # Skip if implausible
            if kernel > MAX_FWHM:
                continue

            context, span = rex_ctx(match, ctxt)
            group = match.group()
            kernels.append({
                'value': kernel,
                'context': context,
                'group': group,
                'span': span,
            })

    return kernels
Пример #4
0
 def test_match(self):
     match = rex_flex(self.pattern, self.text)
     result = rex_ctx(match, self.text)
     assert_equal(result[0], self.text)
     assert_equal(result[1], ((4, 7)))
Пример #5
0
 def test_pad(self):
     """  """
     result = rex_flex('\Wfoo\W', 'foo')
     assert_true(result)
Пример #6
0
 def test_finditer(self):
     result = rex_flex(r'foo', 'foo foo foo', fun=re.finditer)
     assert_equal(len(list(result)), 3)
Пример #7
0
 def test_search_rex_ignore_flags(self):
     """  """
     result = rex_flex(re.compile('foo'), 'FOO')
     assert_false(result)
Пример #8
0
 def test_search_rex(self):
     result = rex_flex(re.compile(r'foo', re.I), 'foo')
     assert_equal(bool(result), bool(re.search(r'foo', 'foo')))
Пример #9
0
 def test_search_pattern(self):
     result = rex_flex(r'foo', 'foo')
     assert_equal(bool(result), bool(re.search(r'foo', 'foo')))
Пример #10
0
 def test_search_pattern(self):
     result = rex_flex(r'foo', 'foo')
     assert_equal(
         bool(result),
         bool(re.search(r'foo', 'foo'))
     )
Пример #11
0
 def test_match(self):
     match = rex_flex(self.pattern, self.text)
     result = rex_ctx(match, self.text)
     assert_equal(result[0], self.text)
     assert_equal(result[1], ((4, 7)))
Пример #12
0
 def test_pad(self):
     """  """
     result = rex_flex('\Wfoo\W', 'foo')
     assert_true(result)
Пример #13
0
 def test_finditer(self):
     result = rex_flex(r'foo', 'foo foo foo', fun=re.finditer)
     assert_equal(
         len(list(result)),
         3
     )
Пример #14
0
 def test_search_rex_ignore_flags(self):
     """  """
     result = rex_flex(re.compile('foo'), 'FOO')
     assert_false(result)
Пример #15
0
 def test_search_rex(self):
     result = rex_flex(re.compile(r'foo', re.I), 'foo')
     assert_equal(
         bool(result),
         bool(re.search(r'foo', 'foo'))
     )
Пример #16
0
def est_highpass_cutoff(txt):
    """

    """
    cutoffs = []

    ctxt = re.sub('[():=]', '', txt)

    for ptn in highpass_filter_bool_ptn:

        matches = rex_flex(ptn, ctxt, re.finditer)

        for match in matches:

            context, span = rex_ctx(match, ctxt)

            # Skip if negative patterns match
            stop = False
            for neg_ptn in highpass_filter_neg_ptn:
                if rex_flex(neg_ptn, context):
                    stop = True
                    break
            if stop:
                continue

            # Skip if wide-range negative patterns match
            context_wide, _ = rex_ctx(match, ctxt, nchar=500)
            for neg_ptn in highpass_filter_wide_neg_ptn:
                if rex_flex(neg_ptn, context_wide):
                    stop = True
                    break
            if stop:
                continue

            context_search, _ = rex_ctx(match, ctxt, nchar_pre=0)

            # Match on fraction pattern (e.g. 1 / 128 Hz)
            matches = rex_flex(
                highpass_filter_values_fraction, context_search,
                fun=re.finditer
            )
            matches = list(matches)

            if len(matches) == 1:

                match = matches[0]
                group = match.group()

                numerator = match.groupdict()['num']
                denominator = match.groupdict()['dnm']

                if (numerator.startswith('0') and not numerator.startswith('0.')) or \
                        (denominator.startswith('0') and not denominator.startswith('0.')):
                    continue

                try:
                    numerator = float(numerator)
                    denominator = float(denominator)
                except (ValueError, TypeError):
                    continue

                # Avoid zero-division errors
                if numerator == 0 or denominator == 0:
                    continue

                cutoff = numerator / denominator

                units = match.groupdict()['units']
                if units.lower() == 'hz':
                    cutoff = 1 / cutoff

                cutoffs.append({
                    'value': cutoff,
                    'context': context,
                    'group': group,
                    'span': span,
                })

                # Stop if fraction matches
                continue

            # Match on single-value pattern (e.g. 0.05 Hz)
            matches = rex_flex(
                highpass_filter_values, context_search, fun=re.finditer
            )
            matches = list(matches)

            if len(matches) == 1:

                match = matches[0]
                group = match.group()

                cutoff = match.groupdict()['cutoff']

                if cutoff.startswith('0') and not cutoff.startswith('0.'):
                    continue

                try:
                    cutoff = float(cutoff)
                except (ValueError, TypeError):
                    continue

                if cutoff == 0:
                    continue

                units = match.groupdict()['units']
                if units.lower() == 'hz':
                    cutoff = 1 / cutoff

                cutoffs.append({
                    'value': cutoff,
                    'context': context,
                    'group': group,
                    'span': span,
                })

    return cutoffs
Пример #17
0
def est_highpass_cutoff(txt):
    """

    """
    cutoffs = []

    ctxt = re.sub(r'[():=]', '', txt)

    for ptn in highpass_filter_bool_ptn:

        matches = rex_flex(ptn, ctxt, re.finditer)

        for match in matches:

            context, span = rex_ctx(match, ctxt)

            # Skip if negative patterns match
            stop = False
            for neg_ptn in highpass_filter_neg_ptn:
                if rex_flex(neg_ptn, context):
                    stop = True
                    break
            if stop:
                continue

            # Skip if wide-range negative patterns match
            context_wide, _ = rex_ctx(match, ctxt, nchar=500)
            for neg_ptn in highpass_filter_wide_neg_ptn:
                if rex_flex(neg_ptn, context_wide):
                    stop = True
                    break
            if stop:
                continue

            context_search, _ = rex_ctx(match, ctxt, nchar_pre=0)

            # Match on fraction pattern (e.g. 1 / 128 Hz)
            matches = rex_flex(highpass_filter_values_fraction,
                               context_search,
                               fun=re.finditer)
            matches = list(matches)

            if len(matches) == 1:

                match = matches[0]
                group = match.group()

                numerator = match.groupdict()['num']
                denominator = match.groupdict()['dnm']

                if (numerator.startswith('0') and not numerator.startswith('0.')) or \
                        (denominator.startswith('0') and not denominator.startswith('0.')):
                    continue

                try:
                    numerator = float(numerator)
                    denominator = float(denominator)
                except (ValueError, TypeError):
                    continue

                # Avoid zero-division errors
                if numerator == 0 or denominator == 0:
                    continue

                cutoff = numerator / denominator

                units = match.groupdict()['units']
                if units.lower() == 'hz':
                    cutoff = 1 / cutoff

                cutoffs.append({
                    'value': cutoff,
                    'context': context,
                    'group': group,
                    'span': span,
                })

                # Stop if fraction matches
                continue

            # Match on single-value pattern (e.g. 0.05 Hz)
            matches = rex_flex(highpass_filter_values,
                               context_search,
                               fun=re.finditer)
            matches = list(matches)

            if len(matches) == 1:

                match = matches[0]
                group = match.group()

                cutoff = match.groupdict()['cutoff']

                if cutoff.startswith('0') and not cutoff.startswith('0.'):
                    continue

                try:
                    cutoff = float(cutoff)
                except (ValueError, TypeError):
                    continue

                if cutoff == 0:
                    continue

                units = match.groupdict()['units']
                if units.lower() == 'hz':
                    cutoff = 1 / cutoff

                cutoffs.append({
                    'value': cutoff,
                    'context': context,
                    'group': group,
                    'span': span,
                })

    return cutoffs