Пример #1
0
    def __call__(self, tstart, tend, varname=None):
        """
        Calls the filter class
        """
        if tstart == -1:
            self.tstep = np.arange(0, self.Nt, 1)
        else:
            self.tstep = self.getTstep(tstart, tend)

        if not varname == None:
            self.variable = varname

        self.loadData()

        # Load the data into a time series object (this has a filter method)
        T = timeseries(self.time[self.tstep], np.swapaxes(
            self.data, 0,
            -1))  # Time dimension needs to be last (filtfilt may have a bug)

        dataout = T.filt(self.cutoff_dt,
                         btype=self.ftype,
                         axis=-1,
                         order=self.order)

        return np.swapaxes(dataout, -1,
                           0)  # Return with the dimensions in the right order
Пример #2
0
def dataflow(X, y=None, cmd_plot=False):
    '''
	Primary function responsible for predictions and GUI output from a pre-processed file.
	Returns signals used for plotting of features as well as generated summary statistics.
	'''
    epochs = epochs_from_prep(X.copy(),
                              None,
                              settings.EPOCH_LENGTH,
                              settings.OVERLAP_FACTOR,
                              settings.SAMPLE_RATE,
                              filter=False,
                              removal=True)
    epochs = dataset(epochs, shuffle=False, exclude_ptt=False,
                     only_rwa=True).epochs
    epochs = gru(load_graph=True, path=settings.BEST_MODEL).predict(epochs)
    epochs.sort(key=lambda x: x.index_start, reverse=False)
    yhat, timecol = reconstruct(X, epochs)
    full = epochs_from_prep(X,
                            None,
                            settings.EPOCH_LENGTH,
                            settings.OVERLAP_FACTOR,
                            settings.SAMPLE_RATE,
                            filter=False,
                            removal=False)
    full.sort(key=lambda x: x.index_start, reverse=False)
    wake, nrem, rem, illegal = timeseries(full)
    summary = summary_statistics(timecol, yhat, wake, nrem, rem, illegal)
    X, y, mask = make_features(X, y, settings.SAMPLE_RATE, removal=False)
    X = transpose(X)
    ss = X[6].copy().astype(float)
    for i, _ in enumerate(ss):
        if X[7, i]:
            ss[i] = 2.0
        elif X[5, i]:
            ss[i] = 0.0
    data = X[0] / settings.SAMPLE_RATE, [X[1], X[2], X[3], X[4], ss, yhat], [
        'RR', 'RWA', 'PTT', 'PWA', 'Sleep stage', 'Arousals'
    ], region(X[5]), region(X[7]), None, None, int(X[0, -1] /
                                                   settings.SAMPLE_RATE)
    if cmd_plot:
        d = list(data)
        if y is not None:
            d[1] += [y]
            d[2] += ['y']
            d[2][5] = 'yhat'
        plot_results(*d)
    return data, summary
Пример #3
0
 def __call__(self,tstart,tend,varname=None):
     """
     Calls the filter class
     """
     if tstart == -1:
         self.tstep=np.arange(0,self.Nt,1)
     else:
         self.tstep=self.getTstep(tstart,tend)
     
     if not varname == None:
         self.variable = varname
     
     self.loadData()
     
     # Load the data into a time series object (this has a filter method)
     T = timeseries(self.time[self.tstep],np.swapaxes(self.data,0,-1)) # Time dimension needs to be last (filtfilt may have a bug)
             
     dataout = T.filt(self.cutoff_dt,btype=self.ftype,axis=-1,order=self.order)  
     
     return np.swapaxes(dataout,-1,0) # Return with the dimensions in the right order
Пример #4
0
def tide_pred_correc(modfile,
                     lon,
                     lat,
                     time,
                     dbfile,
                     ID,
                     z=None,
                     conlist=None):
    """
    Performs a tidal prediction at all points in [lon,lat] at times in vector [time]

    Applies an amplitude and phase correction based on a time series
    """
    raise Exception("RH: Not translated!")

    from timeseries import timeseries, loadDBstation

    print('Calculating tidal correction factors from time series...')
    # Load using the timeseries module
    t0 = datetime.strftime(time[0], '%Y%m%d.%H%M%S')
    t1 = datetime.strftime(time[-1], '%Y%m%d.%H%M%S')
    dt = time[1] - time[0]

    print(t0, t1, dt.total_seconds())
    timeinfo = (t0, t1, dt.total_seconds())
    TS, meta = loadDBstation(dbfile,
                             ID,
                             'waterlevel',
                             timeinfo=timeinfo,
                             filttype='low',
                             cutoff=2 * 3600,
                             output_meta=True)
    lonpt = meta['longitude']
    latpt = meta['latitude']
    print(lonpt, latpt)

    # Extract the OTIS tide prediction
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(
        modfile, lonpt, latpt)
    h_amp = np.abs(h_re + 1j * h_im)[:, 0]
    h_phs = np.angle(h_re + 1j * h_im)[:, 0]

    # Harmonic analysis of observation time series
    amp, phs, frq, frqnames, htide = TS.tidefit(frqnames=conlist)
    TS_harm = timeseries(time, htide)
    residual = TS.y - htide

    # Calculate the amp and phase corrections
    dphs = phs - h_phs + np.pi
    damp = amp / h_amp

    # Extract the data along the specified points
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(
        modfile, lon, lat, z=z, conlist=conlist)

    h_amp = np.abs(h_re + 1j * h_im)
    h_phs = np.angle(h_re + 1j * h_im)
    u_amp = np.abs(u_re + 1j * u_im)
    u_phs = np.angle(u_re + 1j * u_im)
    v_amp = np.abs(v_re + 1j * v_im)
    v_phs = np.angle(v_re + 1j * v_im)

    # Initialise the output arrays
    sz = lon.shape
    nx = np.prod(sz)
    nt = time.shape[0]

    h = np.zeros((nt, nx))
    u = np.zeros((nt, nx))
    v = np.zeros((nt, nx))

    # Rebuild the time series
    #tsec=TS_harm.tsec - TS_harm.tsec[0]
    tsec = othertime.SecondsSince(time, basetime=time[0])
    print(tsec[0])
    for nn, om in enumerate(omega):
        for ii in range(0, nx):
            h[:,
              ii] += damp[nn] * h_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (h_phs[nn, ii] + dphs[nn]))
            u[:,
              ii] += damp[nn] * u_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (u_phs[nn, ii] + dphs[nn]))
            v[:,
              ii] += damp[nn] * v_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (v_phs[nn, ii] + dphs[nn]))

    szo = (nt, ) + sz
    return h.reshape(szo), u.reshape(szo), v.reshape(szo), residual
Пример #5
0
def tide_pred_correc(modfile,lon,lat,time,dbfile,ID,z=None,conlist=None):
    """
    Performs a tidal prediction at all points in [lon,lat] at times in vector [time]
    
    Applies an amplitude and phase correction based on a time series
    """
    from timeseries import timeseries, loadDBstation
    
    print 'Calculating tidal correction factors from time series...'
    # Load using the timeseries module
    t0 = datetime.strftime(time[0],'%Y%m%d.%H%M%S')
    t1 = datetime.strftime(time[-1],'%Y%m%d.%H%M%S')
    dt = time[1]-time[0]
    
    print t0, t1, dt.total_seconds()
    timeinfo = (t0,t1,dt.total_seconds())
    TS,meta = loadDBstation(dbfile,ID,'waterlevel',timeinfo=timeinfo,filttype='low',cutoff=2*3600,output_meta=True)
    lonpt=meta['longitude']
    latpt=meta['latitude']
    print lonpt,latpt
    
    # Extract the OTIS tide prediction
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile,lonpt,latpt)
    h_amp = np.abs(h_re+1j*h_im)[:,0]
    h_phs = np.angle(h_re+1j*h_im)[:,0]
    
    # Harmonic analysis of observation time series
    amp, phs, frq, frqnames, htide = TS.tidefit(frqnames=conlist)
    TS_harm = timeseries(time,htide)
    residual = TS.y - htide
    
    # Calculate the amp and phase corrections
    dphs = phs - h_phs + np.pi
    damp = amp/h_amp

    # Extract the data along the specified points
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile,lon,lat,z=z,conlist=conlist)
    
    h_amp = np.abs(h_re+1j*h_im)
    h_phs = np.angle(h_re+1j*h_im)
    u_amp = np.abs(u_re+1j*u_im)
    u_phs = np.angle(u_re+1j*u_im)
    v_amp = np.abs(v_re+1j*v_im)
    v_phs = np.angle(v_re+1j*v_im)
        
    # Initialise the output arrays
    sz = lon.shape
    nx = np.prod(sz)
    nt = time.shape[0]

    h=np.zeros((nt,nx))
    u=np.zeros((nt,nx))
    v=np.zeros((nt,nx))
    
    # Rebuild the time series
    #tsec=TS_harm.tsec - TS_harm.tsec[0]
    tsec = othertime.SecondsSince(time,basetime=time[0])
    print tsec[0]
    for nn,om in enumerate(omega):
        for ii in range(0,nx):
            h[:,ii] += damp[nn]*h_amp[nn,ii] * np.cos(om*tsec - (h_phs[nn,ii] + dphs[nn]))
            u[:,ii] += damp[nn]*u_amp[nn,ii] * np.cos(om*tsec - (u_phs[nn,ii] + dphs[nn]))
            v[:,ii] += damp[nn]*v_amp[nn,ii] * np.cos(om*tsec - (v_phs[nn,ii] + dphs[nn]))
            
    szo = (nt,)+sz
    return h.reshape(szo), u.reshape(szo), v.reshape(szo), residual
Пример #6
0
    for i in range(1, rnum + 1):
        if (np.where(atlas == i) is not np.nan):
            #print i
            positions.append(np.where(atlas == i))
    return positions


"""
get pair-wise regional pearson correlation
"""
#def regionalPP(regiontimeseries):

test = True
fmrispaths, datadirectory = getFnamelist(test)

apath = '/media/guou8j/Elements/Xinyu_Guo/data/atlas/aal_roi_atlas.nii.gz'
regionnum = 160
np.set_printoptions(threshold=np.nan)
pos = getRegionPosition(apath, regionnum)

filenum = len(fmrispaths)
for i in range(filenum):
    fmridata = t.timeseries(fmrispaths[i])
    print fmridata.datashape()
    rts = fmridata.getRegionTimeseries(pos)

#for i in range(200):
#    print i
#    print rts[i].shape
print np.mean(rts[0], axis=0)
Пример #7
0
    def _vertically_interpolate(self, save_path):
        for xy_point in self._model_points:
            vertically_interpolated_timeseries = []
            heights = [poynt.height for poynt in xy_point._xyz_points]

            # Ensure sorted assumption is satisfied
            assert np.array_equal(heights, np.sort(heights))

            if 'polynomial' in self._vertical_interpolation_techniques:
                for degree in range(1, len(xy_point._xyz_points)):
                    polynomial_interpolated_series = \
                        vif.polynomial(self._desired_point,
                                       xy_point._xyz_points,
                                       degree=degree)

                    if polynomial_interpolated_series is not None:
                        polynomial_interpolated_series.name = \
                            'vert_poly_deg{0}'.format(str(degree))
                        vertically_interpolated_timeseries.\
                            append(timeseries.
                                   timeseries(polynomial_interpolated_series))

            if 'nn' in self._vertical_interpolation_techniques:
                nn_interpolated_series = \
                    vif.nearest_neighbor(self._desired_point,
                                         xy_point._xyz_points)

                if nn_interpolated_series is not None:
                    nn_interpolated_series.name = 'vert_nn'
                    vertically_interpolated_timeseries.\
                        append(timeseries.timeseries(nn_interpolated_series))

            if 'stability_adjusted_log_law' in self._vertical_interpolation_techniques:
                stability_corrected_log_law_series = \
                    vif.stability_corrected_log_law(self._desired_point,
                                                    xy_point._xyz_points,
                                                    xy_point.get_timeseries_with_attribute('stability'),
                                                    xy_point.surface_roughness,
                                                    xy_point.displacement_height)

                if stability_corrected_log_law_series is not None:
                    stability_corrected_log_law_series.name = \
                        'stability_corrected_log_law_series'
                    vertically_interpolated_timeseries.\
                        append(timeseries.
                               timeseries(stability_corrected_log_law_series))

            if 'neutral_log_law' in self._vertical_interpolation_techniques:
                neutral_log_law_series =\
                    vif.neutral_log_law(self._desired_point,
                                        xy_point._xyz_points,
                                        xy_point.surface_roughness,
                                        xy_point.displacement_height)

                if neutral_log_law_series is not None:
                    neutral_log_law_series.name = 'neutral_log_law_series'
                    vertically_interpolated_timeseries.\
                        append(timeseries.timeseries(neutral_log_law_series))

            if 'stability_adjusted_power_law' in self._vertical_interpolation_techniques:
                stability_corrected_power_law_series =\
                    vif.stability_corrected_power_law(self._desired_point,
                                                      xy_point._xyz_points)
                if stability_corrected_power_law_series is not None:
                    stability_corrected_power_law_series.name =\
                        'stability_corrected_power_law_series'
                    vertically_interpolated_timeseries.\
                        append(timeseries.timeseries(stability_corrected_power_law_series))

            if 'neutral_power_law' in self._vertical_interpolation_techniques:
                neutral_power_law_series =\
                    vif.neutral_power_law(self._desired_point,
                                          xy_point._xyz_points)
                if neutral_power_law_series is not None:
                    neutral_power_law_series.name = 'neutral_power_law_series'
                    vertically_interpolated_timeseries.\
                        append(timeseries.timeseries(neutral_power_law_series))

            ground_truth = \
                self._desired_point.get_timeseries_with_attribute('ws')

            if ground_truth is not None:
                ground_truth.name = "ground_truth"
                vertically_interpolated_timeseries.append(ground_truth)

            # Create a new XY Point that will contain the time series
            # associated with the vertical interpolation
            model_transform_point = points.XYPoint(xy_point.lat, xy_point.lon,
                                                   'model')
            model_transform_point.xyz_points = \
                points.XYZPoint(xy_point.lat,
                                xy_point.lon,
                                self._desired_point.height,
                                'model',
                                gid=self._desired_point._gid)
            model_transform_point.\
                xyz_points.\
                set_timeseries(vertically_interpolated_timeseries)

            self._model_transformed.append(model_transform_point)

        # Signal that the vertical interpolation has been completed
        self._to_vertically_interpolate = False
        return