<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def detect_Massimini2004(dat_orig, s_freq, time, opts):
 """Slow wave detection based on Massimini et al., 2004. Parameters dat_orig : ndarray (dtype='float') vector with the data for one channel s_freq : float sampling frequency time : ndarray (dtype='float') vector with the time points for each sample opts : instance of 'DetectSlowWave' 'det_filt' : dict parameters for 'butter', 'duration' : tuple of float min and max duration of SW 'min_ptp' : float min peak-to-peak amplitude 'trough_duration' : tuple of float min and max duration of first half-wave (trough) Returns ------- list of dict list of detected SWs float SW density, per 30-s epoch References Massimini, M. et al. J Neurosci 24(31) 6862-70 (2004). """

    if opts.invert:
        dat_orig = -dat_orig

    dat_det = transform_signal(dat_orig, s_freq, 'double_butter', 
                               opts.det_filt)
    above_zero = detect_events(dat_det, 'above_thresh', value=0.)

    sw_in_chan = []
    if above_zero is not None:
        troughs = within_duration(above_zero, time, opts.trough_duration)
        #lg.info('troughs within duration: ' + str(troughs.shape))

        if troughs is not None:
            troughs = select_peaks(dat_det, troughs, opts.max_trough_amp)
            #lg.info('troughs deep enough: ' + str(troughs.shape))

            if troughs is not None:
                events = _add_halfwave(dat_det, troughs, s_freq, opts)
                #lg.info('SWs high enough: ' + str(events.shape))

                if len(events):
                    events = within_duration(events, time, opts.duration)
                    events = remove_straddlers(events, time, s_freq)
                    #lg.info('SWs within duration: ' + str(events.shape))

                    sw_in_chan = make_slow_waves(events, dat_det, time, s_freq)

    if len(sw_in_chan) == 0:
        lg.info('No slow wave found')

    return sw_in_chan