コード例 #1
0
ファイル: trace.py プロジェクト: bmerrison/morphforge
    def __getattr__(self, name):
        from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl

        if TraceMethodCtrl.has_method(self.__class__, name):
            func = TraceMethodCtrl.get_method(self.__class__, name)
            return functools.partial(func, self)
        raise AttributeError('No Such method for trace type: %s.%s'
                             % (self.__class__.__name__, name))
コード例 #2
0
    def __getattr__(self, name):
        from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl

        if TraceMethodCtrl.has_method(self.__class__, name):
            func = TraceMethodCtrl.get_method(self.__class__, name)
            return functools.partial(func, self)
        raise AttributeError('No Such method for trace type: %s.%s' %
                             (self.__class__.__name__, name))
コード例 #3
0
ファイル: trace.py プロジェクト: NeuroArchive/morphforge
    def __getattr__(self, name):
        from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl

        if TraceMethodCtrl.has_method(self.__class__, name):
            func = TraceMethodCtrl.get_method(self.__class__, name)
            return functools.partial(func, self)

        available_methods = ','.join( [s[1] for s in TraceMethodCtrl.registered_methods.keys()] )
        raise AttributeError('No Such method for trace type: %s.%s (Maybe:%s)'
                             % (self.__class__.__name__, name,available_methods))
コード例 #4
0
from morphforge.traces.traceobjpluginctrl import copy_trace_attrs


# Shifting:
#############

def _shift_pt_trace(trace, offset):
    tr_type = type(trace)
    tr_new = tr_type(
                time=trace.time_pts + offset,
                data=trace.data_pts,)
    copy_trace_attrs(trace, tr_new, comment='+(Shifted %2.2f)' % offset)
    return tr_new


TraceMethodCtrl.register(TraceFixedDT, 'shift', _shift_pt_trace)
TraceMethodCtrl.register(TraceVariableDT, 'shift', _shift_pt_trace)


# Windowing:
###############

def _window_fixed_trace(trace, time_window):

    # Dirty Pre-processing
    if isinstance(time_window, Quantity):
        assert len(time_window) == 2
        time_window = (time_window[0], time_window[1])

    # print time_window, type(time_window)
コード例 #5
0
from morphforge.traces.tracetypes import TraceVariableDT
from morphforge.traces.tracetypes import TracePiecewise
from morphforge.traces.tracetypes import TraceFixedDT


def _get_piecewise_linear_points(tr):
    x_unit = tr.pieces[0].get_min_time().units
    y_unit = tr.pieces[0].get_start_value().units

    x_points = []
    y_points = []

    for piece in tr.pieces:
        x_points.append(float(piece.get_min_time().rescale(x_unit).magnitude))
        x_points.append(float(piece.get_max_time().rescale(x_unit).magnitude))

        y_points.append(
            float(piece.get_start_value().rescale(y_unit).magnitude))
        y_points.append(float(piece.get_end_value().rescale(y_unit).magnitude))

    return (np.array(x_points) * x_unit, np.array(y_points) * y_unit)


# Plotting:
TraceMethodCtrl.register(TraceFixedDT, 'plotpoints', lambda tr:
                         (tr.time_pts, tr.data_pts))
TraceMethodCtrl.register(TraceVariableDT, 'plotpoints', lambda tr:
                         (tr.time_pts, tr.data_pts))
TraceMethodCtrl.register(TracePiecewise, 'plotpoints',
                         _get_piecewise_linear_points)
コード例 #6
0

def _fft(tr, normalise=True):
    fft_values = np.fft.fft(tr.data_pts)
    if normalise:
        fft_values /= fft_values.max()
    dt_in_s = tr.get_dt_new().rescale('s').magnitude
    ftfreq = np.fft.fftfreq(tr.data_pts.size, dt_in_s)
    return (ftfreq, fft_values)


def _psd(tr, normalise=True):
    fft_values = np.fft.fft(tr.data_pts)
    fft_values = fft_values.real()**2 + fft_values.imag()**2
    if normalise:
        fft_values /= fft_values.max()

    dt_in_s = tr.get_dt_new().rescale('s').magnitude
    ftfreq = np.fft.fftfreq(tr.data_pts.size, dt_in_s)
    return (ftfreq, fft_values)


TraceMethodCtrl.register(TraceFixedDT,
                         'fft',
                         _fft,
                         can_fallback_to_fixed_trace=True)
TraceMethodCtrl.register(TraceFixedDT,
                         'psd',
                         _psd,
                         can_fallback_to_fixed_trace=True)
コード例 #7
0
import numpy as np
from morphforge.traces.tracetypes import TraceVariableDT
from morphforge.traces.tracetypes import TracePiecewise
from morphforge.traces.tracetypes import TraceFixedDT
import copy

def _clone_fixed(tr):
    tr_new = TraceFixedDT( 
            time = np.copy( tr.time_pts_np) * tr.time_units,
            data = np.copy( tr.data_pts_np) * tr.data_units )
    copy_trace_attrs(tr, tr_new, comment='+(cloned)')
    return tr_new

def _clone_variable(tr):
    tr_new = TraceVariableDT(
            time = np.copy( tr.time_pts_np) * tr.time_units,
            data = np.copy( tr.data_pts_np) * tr.data_units )
    copy_trace_attrs(tr, tr_new, comment='+(cloned)')
    return tr_new

def _clone_piecewise(tr):
    tr_new = TracePiecewise(pieces = [copy.copy(piece) for piece in tr.pieces])
    copy_trace_attrs(tr, tr_new, comment='+(cloned)')
    return tr_new


TraceMethodCtrl.register(TraceFixedDT,    'clone', _clone_fixed)
TraceMethodCtrl.register(TraceVariableDT, 'clone', _clone_variable)
TraceMethodCtrl.register(TracePiecewise,  'clone', _clone_piecewise)

コード例 #8
0
from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl

import numpy as np
from morphforge.traces.tracetypes import TraceVariableDT
from morphforge.traces.tracetypes import TracePiecewise
from morphforge.traces.tracetypes import TraceFixedDT


def _get_piecewise_linear_points(tr):
    x_unit = tr.pieces[0].get_min_time().units
    y_unit = tr.pieces[0].get_start_value().units

    x_points = []
    y_points = []

    for piece in tr.pieces:
        x_points.append(float(piece.get_min_time().rescale(x_unit).magnitude))
        x_points.append(float(piece.get_max_time().rescale(x_unit).magnitude))

        y_points.append(float(piece.get_start_value().rescale(y_unit).magnitude))
        y_points.append(float(piece.get_end_value().rescale(y_unit).magnitude))

    return (np.array(x_points) * x_unit, np.array(y_points) * y_unit)


# Plotting:
TraceMethodCtrl.register(TraceFixedDT,    'plotpoints', lambda tr: (tr.time_pts, tr.data_pts))
TraceMethodCtrl.register(TraceVariableDT, 'plotpoints', lambda tr: (tr.time_pts, tr.data_pts))
TraceMethodCtrl.register(TracePiecewise,  'plotpoints', _get_piecewise_linear_points)

コード例 #9
0
# Shifting:
#############


def _shift_pt_trace(trace, offset):
    tr_type = type(trace)
    tr_new = tr_type(
        time=trace.time_pts + offset,
        data=trace.data_pts,
    )
    copy_trace_attrs(trace, tr_new, comment='+(Shifted %2.2f)' % offset)
    return tr_new


TraceMethodCtrl.register(TraceFixedDT, 'shift', _shift_pt_trace)
TraceMethodCtrl.register(TraceVariableDT, 'shift', _shift_pt_trace)

# Windowing:
###############


def _window_fixed_trace(trace, time_window):

    # Dirty Pre-processing
    if isinstance(time_window, Quantity):
        assert len(time_window) == 2
        time_window = (time_window[0], time_window[1])

    # print time_window, type(time_window)
コード例 #10
0
def _butterworthfilter(tr, filterorder, cutoff_frequency):
    cutoff_frequency.rescale('Hz')
    import scipy.signal
    frequency_hz = 1 / float(tr.get_dt_new().rescale('s'))
    n_frq_hz = frequency_hz / 2.0

    cuttoff_norm = cutoff_frequency / n_frq_hz
    (coeff_num, coeff_denom) = scipy.signal.filter_design.butter(filterorder, cuttoff_norm)
    filteredsignal = scipy.signal.lfilter(coeff_num, coeff_denom, tr.data_pts_np)

    tr_new = TraceFixedDT(time=tr.time_pts, data=filteredsignal * tr.data_unit,)
    copy_trace_attrs(tr, tr_new, comment="+(Butterworth Filtered)" )
    return tr_new


TraceMethodCtrl.register(TraceFixedDT, 'filterbutterworth', _butterworthfilter, can_fallback_to_fixed_trace=True)




def _besselfilter(tr, filterorder, cutoff_frequency):
    cutoff_frequency.rescale('Hz')
    import scipy.signal
    frequency_hz = 1 / float(tr.get_dt_new().rescale('s'))
    n_frq_hz = frequency_hz / 2.0

    cuttoff_norm = cutoff_frequency / n_frq_hz
    (coeff_num, coeff_denom) = scipy.signal.filter_design.bessel(filterorder, cuttoff_norm)
    filteredsignal = scipy.signal.lfilter(coeff_num, coeff_denom, tr.data_pts_np)

    time_shift = tr.get_dt_new() * max(len(coeff_denom), len(coeff_num))
コード例 #11
0
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------

from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl, copy_trace_attrs
from morphforge.traces import TraceFixedDT, TraceVariableDT, TracePiecewise
import numpy as np

# Mean, rms, stddev, variance functions:
##############################

# For FixedDT traces, these are simple:
TraceMethodCtrl.register(TraceFixedDT, 'mean', lambda tr: np.mean(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, 'stddev',
                         lambda tr: np.std(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, 'var', lambda tr: np.var(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, 'rms',
                         lambda tr: np.sqrt(np.mean(tr.data_pts**2)))

# For VariableDT traces


def _variabledt_mean(tr):
    import scipy.integrate
    # Calculate the mean with simpsons rule:
    integral = scipy.integrate.simps(y=tr.data_pts_np, x=tr.time_pts_s)
    mean = integral / tr.get_duration().rescale('s').magnitude * tr.data_unit
    return mean
コード例 #12
0
from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl
from morphforge.traces import TraceFixedDT

import numpy as np


def _fft(tr, normalise=True):
    fft_values = np.fft.fft(tr.data_pts)
    if normalise:
        fft_values /= fft_values.max()
    dt_in_s = tr.get_dt_new().rescale('s').magnitude
    ftfreq = np.fft.fftfreq(tr.data_pts.size, dt_in_s)
    return (ftfreq, fft_values)


def _psd(tr, normalise=True):
    fft_values = np.fft.fft(tr.data_pts)
    fft_values = fft_values.real() ** 2 + fft_values.imag() ** 2
    if normalise:
        fft_values /= fft_values.max()

    dt_in_s = tr.get_dt_new().rescale('s').magnitude
    ftfreq = np.fft.fftfreq(tr.data_pts.size, dt_in_s)
    return (ftfreq, fft_values)


TraceMethodCtrl.register(TraceFixedDT, 'fft', _fft, can_fallback_to_fixed_trace=True)
TraceMethodCtrl.register(TraceFixedDT, 'psd', _psd, can_fallback_to_fixed_trace=True)

コード例 #13
0
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------

from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl
from morphforge.traces import TraceFixedDT
from morphforge.traces.tracetypes import TracePiecewise, PieceWiseComponentVisitor


def _integrate_pointbased(tr, ):
    import scipy.integrate
    return scipy.integrate.simps(y=tr.data_pts_np, x=tr.time_pts_np) * tr.data_unit * tr.time_unit

TraceMethodCtrl.register(TraceFixedDT, 'integrate', _integrate_pointbased)





class PiecewiseIntegrator(PieceWiseComponentVisitor):

    @classmethod
    def visit_linear(cls, o):
        return 0.5 * (o.time_window[1]-o.time_window[0]) * (o.x1-o.x0)

    @classmethod
    def visit_flat(cls, o ):
        return (o.time_window[1]-o.time_window[0]) * o.x
コード例 #14
0
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------

from morphforge.traces.traceobjpluginctrl import copy_trace_attrs
from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl
from morphforge.traces import TraceFixedDT
from morphforge.traces import TraceVariableDT
from morphforge.traces import TracePiecewise
from MMtrace_conversion import TraceConverter, TraceApproximator

# Conversion to: FixedDT:
#########################
TraceMethodCtrl.register(
    TraceFixedDT, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(
        tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))
TraceMethodCtrl.register(
    TraceVariableDT, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(
        tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))
TraceMethodCtrl.register(
    TracePiecewise, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(
        tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))

# Conversion to VariableDT:
###########################
TraceMethodCtrl.register(
    TraceFixedDT, 'convert_to_variable', lambda tr, eps: copy_trace_attrs(
        tr_old=tr,
        tr_new=TraceConverter.reduce_to_variable_dt_trace(original_trace=tr,
                                                          epsilon=eps)))
コード例 #15
0
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------


from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl, copy_trace_attrs
from morphforge.traces import TraceFixedDT, TraceVariableDT, TracePiecewise
import numpy as np

# Mean, rms, stddev, variance functions:
##############################

# For FixedDT traces, these are simple:
TraceMethodCtrl.register(TraceFixedDT, "mean", lambda tr: np.mean(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, "stddev", lambda tr: np.std(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, "var", lambda tr: np.var(tr.data_pts))
TraceMethodCtrl.register(TraceFixedDT, "rms", lambda tr: np.sqrt(np.mean(tr.data_pts ** 2)))

# For VariableDT traces


def _variabledt_mean(tr):
    import scipy.integrate

    # Calculate the mean with simpsons rule:
    integral = scipy.integrate.simps(y=tr.data_pts_np, x=tr.time_pts_s)
    mean = integral / tr.get_duration().rescale("s").magnitude * tr.data_unit
    return mean
コード例 #16
0
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------

from morphforge.traces.traceobjpluginctrl import copy_trace_attrs
from morphforge.traces.traceobjpluginctrl import TraceMethodCtrl
from morphforge.traces import TraceFixedDT
from morphforge.traces import TraceVariableDT
from morphforge.traces import TracePiecewise
from MMtrace_conversion import TraceConverter, TraceApproximator

# Conversion to: FixedDT:
#########################
TraceMethodCtrl.register(TraceFixedDT, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))
TraceMethodCtrl.register(TraceVariableDT, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))
TraceMethodCtrl.register(TracePiecewise, 'convert_to_fixed', lambda tr, dt: copy_trace_attrs(tr_old=tr, tr_new=TraceConverter.rebase_to_fixed_dt(tr, dt)))




# Conversion to VariableDT:
###########################
TraceMethodCtrl.register(
        TraceFixedDT,
        'convert_to_variable', 
        lambda tr, eps: copy_trace_attrs(tr_old=tr, tr_new=TraceConverter.reduce_to_variable_dt_trace(original_trace=tr, epsilon=eps)))

TraceMethodCtrl.register(
        TraceVariableDT,
コード例 #17
0
from morphforge.traces import TraceFixedDT
#import numpy as np
#import quantities as pq
#import operator
from morphforge.traces.tracetypes import TracePiecewise, PieceWiseComponentVisitor
#from morphforge.traces.tracetypes.tracepiecewise import TracePieceFunctionLinear
#from morphforge.traces.tracetypes.tracepiecewise import TracePieceFunctionFlat


def _integrate_pointbased(tr, ):
    import scipy.integrate
    return scipy.integrate.simps(
        y=tr.data_pts_np, x=tr.time_pts_np) * tr.data_unit * tr.time_unit


TraceMethodCtrl.register(TraceFixedDT, 'integrate', _integrate_pointbased)


class PiecewiseIntegrator(PieceWiseComponentVisitor):
    @classmethod
    def visit_linear(cls, o):
        return 0.5 * (o.time_window[1] - o.time_window[0]) * (o.x1 - o.x0)

    @classmethod
    def visit_flat(cls, o):
        return (o.time_window[1] - o.time_window[0]) * o.x


def _integrate_piecewise(tr):
    pieces = [PiecewiseIntegrator.visit(piece) for piece in tr.pieces]
    tot = None