예제 #1
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
예제 #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 test_nchar_post_zero(self):
        """Regression test. Check for bug by which `nchar_post=0` is equivalent
        to `nchar_post=None`.

        """
        result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_post=0)
        assert_equal(result[0], 'foo bar')
        assert_equal(result[1], (4, 7))
예제 #4
0
 def test_nchar_post(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_post=2)
     assert_equal(
         result[0],
         'foo bar b'
     )
     assert_equal(
         result[1],
         (4, 7)
     )
예제 #5
0
 def test_nchar_pre(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_pre=2)
     assert_equal(
         result[0],
         'o bar baz'
     )
     assert_equal(
         result[1],
         (4, 7)
     )
예제 #6
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
예제 #7
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
예제 #8
0
    def test_nchar_post_zero(self):
        """Regression test. Check for bug by which `nchar_post=0` is equivalent
        to `nchar_post=None`.

        """
        result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_post=0)
        assert_equal(
            result[0],
            'foo bar'
        )
        assert_equal(
            result[1],
            (4, 7)
        )
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
예제 #10
0
 def test_nchar_post(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_post=2)
     assert_equal(result[0], 'foo bar b')
     assert_equal(result[1], (4, 7))
예제 #11
0
 def test_nchar_pre(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern, nchar_pre=2)
     assert_equal(result[0], 'o bar baz')
     assert_equal(result[1], (4, 7))
예제 #12
0
 def test_nomatch(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern)
     assert_equal(result[0], self.text)
     assert_equal(result[1], (4, 7))
예제 #13
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)))
예제 #14
0
 def test_nomatch(self):
     result = rex_ctx(txt=self.text, ptn=self.pattern)
     assert_equal(result[0], self.text)
     assert_equal(result[1], (4, 7))
예제 #15
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)))
예제 #16
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