<commit_before>#!/usr/bin/env python

"""File: formats.py
Module containing convenience functions for formatting raw data in a way that
the gridcell module understands, and transforming positions from raw to
physical coordinates.

"""
# Copyright 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from collections import deque, Mapping
import numpy
from scipy import optimize


def sessions(positions, spike_times, param_dicts=None, info_dicts=None):
    """
    Identify separate sessions in multiple position and spike time recordings

    Parameters
    ----------
    positions : sequence
        Sequence containing the position samples corresponding to each cell.
        Each element should be a sequence similar to `(t, x, y)` or `(t, x, y,
        x2, y2)`, where `x` and `y` are arrays containing the position samples,
        `t` is an array containing the corresponding sample times, and `x2`and
        `y2` are optional arrays containing and extra set of position samples.
        Both standards may be present simultaneously.
    spike_ts : sequence
        Sequence containing the spike times corresponding to each cell.
        Each element should be an array of spike times.
    param_dicts : sequence or None, optional
        Sequence containing a dict of parameters for each cell. If None, an
        empty dict will be assigned to each cell.
    info_dicts : sequence or None, optional
        Sequence containing an info dict for each cell. If None, an empty info<commit_msg>Implement low-pass filtering for position samples.<commit_after>#!/usr/bin/env python

"""File: formats.py
Module containing convenience functions for formatting raw data in a way that
the gridcell module understands, and transforming positions from raw to
physical coordinates.

"""
# Copyright 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from collections import deque, Mapping
import numpy
from scipy import optimize, signal
from .utils import sensibly_divide
from .cells import Position


def filter_positions(t, x, y, window_time, cutoff_freq, window='hann'):
    """
    Filter a series of position samples

    The positions are interpreted by `gridcell.Position` to find missing values
    and splits between consecutive streaks of samples, before, they are filter
    using `filter_`.

    Parameters
    ----------
    t : array-like
        Time points of the samples in the signal.
    x, y : array-like
        Position samples. See `gridcell.Position` for details about how missing
        values and splits between consecutive streaks of regular samples are
        handled.
    window_time, cutoff_freq, window
        See `filter_`.

    Returns
    -------
    xf, yf : array-like, masked
        Filtered position samples. Missing values are masked.

    """
    pos = Position(t, x, y)
    data = pos.data
    tsplit, xsplit, ysplit = data['tsplit'], data['xsplit'], data['ysplit']
    xsplitf, ysplitf = [], []
    for tt, xx, yy in zip(tsplit, xsplit, ysplit):
        xsplitf.append(filter_(tt, xx, window_time, cutoff_freq, window))
        ysplitf.append(filter_(tt, yy, window_time, cutoff_freq, window))
    return numpy.ma.hstack(xsplitf), numpy.ma.hstack(ysplitf)


def filter_(t, x, window_time, cutoff_freq, window):
    """
    Filter a signal using a FIR low-pass filter

    Parameters
    ----------
    t : array-like
        Time points of the samples in the signal. Only used to extract the
        sampling frequency.
    x : array-like
        Signal to filter. If given as a masked array, the masked entries
        will be handled using normalized convolution. Boundary conditions
        are handled by treating samples beyond the boundary the same as
        masked samples.
    window_time : non-negative scalar
        Length of the filter kernel, given in the same unit as `t`. The
        actual kernel will be rounded to an odd number of samples.
    cutoff_freq : non-negative scalar
        Cutoff frequency of the filter. Must be between 0.0 and half the
        sampling frequency, and given in the reciprocal unit of `t`.
    window : string
        Windowing function to use when constructing the filter kernel. See
        `signal.firwin` for valid options.

    Returns
    -------
    xf : ndarray
        Filtered signal. Locations where only masked values contributed to
        the filtered value are masked.

    """
    if window_time < 0.0:
        raise ValueError("'window_time' must be a non-negative number")
    elif window_time == 0.0:
        return x
    fs = 1.0 / numpy.mean(numpy.diff(t))
    wl = 2 * int(0.5 * window_time * fs) + 1
    kernel = signal.firwin(wl, [cutoff_freq], pass_zero=True,
                           nyq=(0.5 * fs), window='hann')
    mask = numpy.ma.getmaskarray(x)
    xf = numpy.ma.filled(x, fill_value=0.0)
    xn = signal.convolve(xf, kernel, mode='same')
    xd = signal.convolve((~mask).astype(numpy.int_), kernel, mode='same')
    return sensibly_divide(xn, xd, masked=True)


def sessions(positions, spike_times, param_dicts=None, info_dicts=None):
    """
    Identify separate sessions in multiple position and spike time recordings

    Parameters
    ----------
    positions : sequence
        Sequence containing the position samples corresponding to each cell.
        Each element should be a sequence similar to `(t, x, y)` or `(t, x, y,
        x2, y2)`, where `x` and `y` are arrays containing the position samples,
        `t` is an array containing the corresponding sample times, and `x2`and
        `y2` are optional arrays containing and extra set of position samples.
        Both standards may be present simultaneously.
    spike_ts : sequence
        Sequence containing the spike times corresponding to each cell.
        Each element should be an array of spike times.
    param_dicts : sequence or None, optional
        Sequence containing a dict of parameters for each cell. If None, an
        empty dict will be assigned to each cell.
    info_dicts : sequence or None, optional
        Sequence containing an info dict for each cell. If None, an empty info
{'fraction_non_alphanumeric': 0.07448840381991814, 'fraction_numerical': 0.005457025920873124, 'mean_word_length': 2.999454446262957, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6848114', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1876, 'n_words': 879}