# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # # - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 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.trace_methods_ctrl import CloneTrace, CopyTraceAttrs from morphforge.traces.trace_methods_ctrl import TraceMethodCtrl from morphforge.traces import Trace_FixedDT, Trace_VariableDT, Trace_Piecewise from MMtrace_conversion import TraceConverter, TraceApproximator # Conversion to: FixedDT: ######################### TraceMethodCtrl.register(Trace_FixedDT, 'convert_to_fixed', lambda tr,dt: CopyTraceAttrs(trOld=tr, trNew=TraceConverter.RebaseToFixedDT(tr,dt) ) ) TraceMethodCtrl.register(Trace_VariableDT, 'convert_to_fixed', lambda tr,dt: CopyTraceAttrs(trOld=tr, trNew=TraceConverter.RebaseToFixedDT(tr,dt) ) ) TraceMethodCtrl.register(Trace_Piecewise, 'convert_to_fixed', lambda tr,dt: CopyTraceAttrs(trOld=tr, trNew=TraceConverter.RebaseToFixedDT(tr,dt) ) ) # Conversion to VariableDT: ########################### TraceMethodCtrl.register(Trace_FixedDT, 'convert_to_variable', lambda tr: CopyTraceAttrs(trOld=tr, trNew=TraceConverter.reduce_to_variable_dt_trace() ) ) TraceMethodCtrl.register(Trace_VariableDT, 'convert_to_variable', lambda tr: CopyTraceAttrs(trOld=tr, trNew=TraceConverter.reduce_to_variable_dt_trace() ) ) # MISSING: PIECEWISE # Conversion to Piecewise: ########################## TraceMethodCtrl.register(Trace_FixedDT, 'convert_to_piecewise', TraceApproximator.FitPiecewiseLinearTrace ) # MISSING: VariableDT
# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 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.trace_methods_ctrl import TraceMethodCtrl, CloneTrace from morphforge.traces import Trace_FixedDT, Trace_VariableDT, Trace_Piecewise import numpy as np # Mean, rms, stddev, variance functions: ############################## # For FixedDT traces, these are simple: TraceMethodCtrl.register(Trace_FixedDT, 'mean', lambda tr: np.mean(tr._data) ) TraceMethodCtrl.register(Trace_FixedDT, 'stddev', lambda tr: np.std(tr._data) ) TraceMethodCtrl.register(Trace_FixedDT, 'var', lambda tr: np.var(tr._data) ) TraceMethodCtrl.register(Trace_FixedDT, 'rms', lambda tr: np.sqrt(np.mean(tr._data**2)) ) # For VariableDT traces def variableDTMean(tr): import scipy.integrate # Calculate the mean with simpsons rule: integral = scipy.integrate.simps(y = tr._data.magnitude,x= tr._time.rescale('s').magnitude ) mean = integral / tr.getDuration().rescale('s').magnitude * tr._data.units #mean_safe = tr.convert_to_fixed(unit("0.1:ms")).mean() #print mean_safe, mean #if np.fabs(mean-mean_safe) #assert False, 'Check this' return mean
cutoff_frequency.rescale("Hz") import scipy.signal frequency_hz = 1 / float(self.getDTNew().rescale('s')) n_frq_Hz = frequency_hz / 2.0 cuttoff_norm = cutoff_frequency / n_frq_Hz b, a = scipy.signal.filter_design.butter(filterorder, cuttoff_norm) filteredsignal = scipy.signal.lfilter(b, a, self._data.magnitude) return CloneTrace(self, data=filteredsignal * self._data.units, comment="+(Butterworth Filtered)" ) TraceMethodCtrl.register(Trace_FixedDT, 'filterbutterworth', butterworthfilter, can_fallback_to_fixed_trace=True ) def besselfilter(self, filterorder, cutoff_frequency): cutoff_frequency.rescale("Hz") import scipy.signal frequency_hz = 1 / float(self.getDTNew().rescale('s')) n_frq_Hz = frequency_hz / 2.0 cuttoff_norm = cutoff_frequency / n_frq_Hz print "CutoffNorm:",cuttoff_norm b, a = scipy.signal.filter_design.bessel(filterorder, cuttoff_norm) filteredsignal = scipy.signal.lfilter(b, a, self._data.magnitude)
# - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 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.trace_methods_ctrl import TraceMethodCtrl from morphforge.traces import Trace_FixedDT import numpy as np def fft(tr, normalise=True): ft = np.fft.fft( tr._data ) if normalise: ft /= ft.max() ftfreq = np.fft.fftfreq( tr._data.size, tr.getDTNew().rescale("s").magnitude ) return ftfreq, ft def psd(tr, normalise=True): ft = np.fft.fft( tr._data ) ft = ft.real()**2 + ft.imag()**2 if normalise: ft /= ft.max() ftfreq = np.fft.fftfreq( tr._data.size, tr.getDTNew().rescale("s").magnitude ) return ftfreq, ft TraceMethodCtrl.register(Trace_FixedDT, 'fft', fft, can_fallback_to_fixed_trace=True ) TraceMethodCtrl.register(Trace_FixedDT, 'psd', fft, can_fallback_to_fixed_trace=True )
import numpy import numpy as np from quantities.quantity import Quantity from morphforge.traces.trace_methods_ctrl import TraceMethodCtrl from morphforge.traces.trace_methods_ctrl import CloneTrace # Shifting: ############# def ShiftPtTrace(trace, offset): return CloneTrace(trace, time=trace._time + offset, data=trace._data, comment="+ Shifted %2.2f" % offset) TraceMethodCtrl.register(Trace_FixedDT, 'shift', ShiftPtTrace) TraceMethodCtrl.register(Trace_VariableDT, 'shift', ShiftPtTrace) # Windowing: ############### def WindowFixedTrace(trace, timeWindow): #Dirty Pre-processing if isinstance(timeWindow, Quantity): assert len(timeWindow) == 2 timeWindow = (timeWindow[0], timeWindow[1]) #print timeWindow, type(timeWindow)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ------------------------------------------------------------------------------- from morphforge.traces.trace_methods_ctrl import TraceMethodCtrl import numpy as np from morphforge.traces.tracetypes import Trace_VariableDT, Trace_Piecewise, Trace_FixedDT def getPiecewiseLinearPoints(tr): xUnit = tr._pieces[0].getMinTime().units yUnit = tr._pieces[0].getStartValue().units xPoints = [] yPoints = [] for p in tr._pieces: xPoints.append(float(p.getMinTime().rescale(xUnit).magnitude)) xPoints.append(float(p.getMaxTime().rescale(xUnit).magnitude)) yPoints.append(float(p.getStartValue().rescale(yUnit).magnitude)) yPoints.append(float(p.getEndValue().rescale(yUnit).magnitude)) return np.array(xPoints) * xUnit, np.array(yPoints) * yUnit # Plotting: TraceMethodCtrl.register(Trace_FixedDT, "plotpoints", lambda tr: (tr._time, tr._data)) TraceMethodCtrl.register(Trace_VariableDT, "plotpoints", lambda tr: (tr._time, tr._data)) TraceMethodCtrl.register(Trace_Piecewise, "plotpoints", getPiecewiseLinearPoints)