<commit_before>'''
Transformations that primarily involve numerical computation on variables.
'''

import numpy as np
import pandas as pd
from bids.utils import listify
from .base import Transformation
from bids.analysis import hrf
from bids.variables import SparseRunVariable,  DenseRunVariable


class Convolve(Transformation):
    """Convolve the input variable with an HRF.

    Args:
        var (Variable): The variable to convolve.
        model (str): The name of the HRF model to apply. Must be one of 'spm',
            'glover', or 'fir'.
        derivative (bool): Whether or not to include the temporal derivative.
        dispersion (bool): Whether or not to include the dispersion derivative.
        fir_delays (iterable): A list or iterable of delays to use if model is
            'fir' (ignored otherwise). Spacing between delays must be fixed.
        sampling_rate (float): Rate to downsample variable to after convolution
            in Hz, if var is a SparseRunVariable.

    Note: Uses the HRF convolution functions implemented in nistats.
    """

    _input_type = 'variable'
    _return_type = 'variable'

    def _transform(self, var, model='spm', derivative=False, dispersion=False,
                   fir_delays=None):

        model = model.lower()

        df = var.to_df(entities=False)

        if isinstance(var, SparseRunVariable):
            sampling_rate = 10
            resample_frames = np.arange(
                0, var.get_duration(), 1/sampling_rate)
        else:
            resample_frames = df['onset'].values
            sampling_rate = var.sampling_rate

        vals = df[['onset', 'duration', 'amplitude']].values.T

        if model in ['spm', 'glover']:
            if derivative:
                model += ' + derivative'
            if dispersion:
                model += ' + dispersion'
        elif model != 'fir':
            raise ValueError("Model must be one of 'spm', 'glover', or 'fir'.")

        oversampling = np.ceil(
            1 / np.ediff1d(var.onset).min() * (1/sampling_rate))

        convolved = hrf.compute_regressor(
            vals, model, resample_frames, fir_delays=fir_delays, min_onset=0,
            oversampling=oversampling
            )

        return DenseRunVariable(
            name=var.name, values=convolved[0], run_info=var.run_info,
            source=var.source, sampling_rate=sampling_rate)<commit_msg>Take min of duration, and do future division<commit_after>'''
Transformations that primarily involve numerical computation on variables.
'''
from __future__ import division
import numpy as np
import pandas as pd
from bids.utils import listify
from .base import Transformation
from bids.analysis import hrf
from bids.variables import SparseRunVariable,  DenseRunVariable


class Convolve(Transformation):
    """Convolve the input variable with an HRF.

    Args:
        var (Variable): The variable to convolve.
        model (str): The name of the HRF model to apply. Must be one of 'spm',
            'glover', or 'fir'.
        derivative (bool): Whether or not to include the temporal derivative.
        dispersion (bool): Whether or not to include the dispersion derivative.
        fir_delays (iterable): A list or iterable of delays to use if model is
            'fir' (ignored otherwise). Spacing between delays must be fixed.
        sampling_rate (float): Rate to downsample variable to after convolution
            in Hz, if var is a SparseRunVariable.

    Note: Uses the HRF convolution functions implemented in nistats.
    """

    _input_type = 'variable'
    _return_type = 'variable'

    def _transform(self, var, model='spm', derivative=False, dispersion=False,
                   fir_delays=None):

        model = model.lower()

        df = var.to_df(entities=False)

        if isinstance(var, SparseRunVariable):
            sampling_rate = 10
            resample_frames = np.arange(
                0, var.get_duration(), 1/sampling_rate)
        else:
            resample_frames = df['onset'].values
            sampling_rate = var.sampling_rate

        vals = df[['onset', 'duration', 'amplitude']].values.T

        if model in ['spm', 'glover']:
            if derivative:
                model += ' + derivative'
            if dispersion:
                model += ' + dispersion'
        elif model != 'fir':
            raise ValueError("Model must be one of 'spm', 'glover', or 'fir'.")

        oversampling = np.ceil(
            1 / min([np.ediff1d(var.onset).min(), var.duration.min()])
            * (1/sampling_rate))

        convolved = hrf.compute_regressor(
            vals, model, resample_frames, fir_delays=fir_delays, min_onset=0,
            oversampling=oversampling
            )

        return DenseRunVariable(
            name=var.name, values=convolved[0], run_info=var.run_info,
            source=var.source, sampling_rate=sampling_rate)
{'fraction_non_alphanumeric': 0.09051987767584098, 'fraction_numerical': 0.003669724770642202, 'mean_word_length': 2.4259776536312847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2387459', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1324, 'n_words': 441}