def test_eemd_noiseSeed(self): T = np.linspace(0, 1, 100) S = np.sin(2 * np.pi * T + 4**T) + np.cos((T - 0.4)**2) # Compare up to machine epsilon cmpMachEps = lambda x, y: np.abs(x - y) <= 2 * np.finfo(x.dtype).eps config = {"processes": 1} eemd = EEMD(trials=10, **config) # First run random seed eIMF1 = eemd(S) # Second run with defined seed, diff than first eemd.noise_seed(12345) eIMF2 = eemd(S) # Extremly unlikely to have same seed, thus different results msg_false = "Different seeds, expected different outcomes" if eIMF1.shape == eIMF2.shape: self.assertFalse(np.all(cmpMachEps(eIMF1, eIMF2)), msg_false) # Third run with same seed as with 2nd eemd.noise_seed(12345) eIMF3 = eemd(S) # Using same seeds, thus expecting same results msg_true = "Used same seed, expected same results" self.assertTrue(np.all(cmpMachEps(eIMF2, eIMF3)), msg_true)
def nonlinear_trend_test(trend): #Use surrogate method to test for EEMD trend significance t = len(trend) ts = trend.reshape(1, -1) #reshape into 2d array for surrogate methods ts_sur = timeseries.surrogates.Surrogates( ts) #generate an instance of surrogate class sur_list = [ ts ] #original time series is the first item in the surrogate list # Assign EEMD to `eemd` variable eemd = EEMD() eemd.noise_seed(12345) #detect extrema using parabolic method emd = eemd.EMD emd.extrema_detection = "parabol" for i in range(19): #0.05 significance level for one-sided test sur = ts_sur.refined_AAFT_surrogates( ts, 100 ) #Return surrogates using the iteratively refined amplitude adjusted Fourier transform method. #detrend surrogate eIMFs_n = eemd.eemd(sur.flatten(), np.arange(t)) sur_trend = eIMFs_n[-1] sur_list.append(sur_trend) ta_list = [timeAsymmetry(ts.flatten()) for ts in sur_list] #test statistic: time asymmetry return np.array(ta_list)
def test_eemd_noiseSeed(self): T = np.linspace(0, 1, 100) S = np.sin(2*np.pi*T+ 4**T) + np.cos( (T-0.4)**2) # Compare up to machine epsilon cmpMachEps = lambda x, y: np.abs(x-y)<=2*np.finfo(x.dtype).eps config = {"processes": 1} eemd = EEMD(trials=10, **config) # First run random seed eIMF1 = eemd(S) # Second run with defined seed, diff than first eemd.noise_seed(12345) eIMF2 = eemd(S) # Extremly unlikely to have same seed, thus different results msg_false = "Different seeds, expected different outcomes" if eIMF1.shape == eIMF2.shape: self.assertFalse(np.all(cmpMachEps(eIMF1,eIMF2)), msg_false) # Third run with same seed as with 2nd eemd.noise_seed(12345) eIMF3 = eemd(S) # Using same seeds, thus expecting same results msg_true = "Used same seed, expected same results" self.assertTrue(np.all(cmpMachEps(eIMF2,eIMF3)), msg_true)
def Signal(): global E_imfNo E_imfNo = np.zeros(50, dtype=np.int) # EEMD options max_imf = 7 """ 信号参数: N:采样频率500Hz tMin:采样开始时间 tMax:采样结束时间 2*np.pi """ N = 500 tMin, tMax = 0, 2 * np.pi T = np.linspace(tMin, tMax, N) # 信号S:是多个信号叠加信号 S = 3 * np.sin(4 * T) + 4 * np.cos(9 * T) + np.sin(8.11 * T + 1.2) # EEMD计算 eemd = EEMD() eemd.trials = 50 eemd.noise_seed(12345) E_IMFs = eemd.eemd(S) imfNo = E_IMFs.shape[0] # Plot results in a grid c = np.floor(np.sqrt(imfNo + 1)) r = np.ceil((imfNo + 1) / c) plt.ioff() plt.subplot(r, c, 1) plt.plot(T, S, 'r') plt.xlim((tMin, tMax)) plt.title("Original signal") i = 1 for imf in E_IMFs: plt.subplot(len(E_IMFs), 1, i) plt.plot(imf) i += 1 # for num in range(imfNo): # plt.subplot(r, c, num + 2) # plt.plot(T, E_IMFs[num], 'g') # plt.xlim((tMin, tMax)) # plt.title("Imf " + str(num + 1)) plt.text(0, 0, str(format(i, '.4f')), style='italic', ha='center', wrap=True) plt.save("haha.jpg")
def prepare_data(): con = engine.connect() # con.execute("truncate DOdataPrecictOnly") # con.execute("INSERT INTO DOdataPrecictOnly(Date,`Dissolved Oxygen`) ( SELECT Date,`Dissolved Oxygen` FROM DOdata order by Date ASC)") rv = con.execute("select `Dissolved Oxygen` from DOdataPrecictOnly") do = [] for i in rv: print(i) do.append(i[0]) DO = [] for i in range(0, len(do)): DO.append([do[i]]) scaler_DO = MinMaxScaler(feature_range=(0, 1)) DO = scaler_DO.fit_transform(DO) # DO = isolutionforest(DO) eemd = EEMD() eemd.noise_seed(12345) imfs = eemd.eemd(DO.reshape(-1), None, 8) con.close() return imfs, scaler_DO
values = dataset.values groups = [0, 1, 2, 3] # fig, axs = plt.subplots(1) df = pd.DataFrame(dataset) # 整体数据的全部字典类型 do = df['Dissolved Oxygen'] # 返回溶解氧那一列,用字典的方式 DO = [] for i in range(0, len(do)): DO.append([do[i]]) scaler_DO = MinMaxScaler(feature_range=(0, 1)) DO = scaler_DO.fit_transform(DO) eemd = EEMD() eemd.noise_seed(12345) imfs = eemd.eemd(DO.reshape(-1), None, 8) c = int(len(do) * .85) lookback_window = 6 imfs_prediction = [] # i = 1 # for imf in imfs: # plt.subplot(len(imfs), 1, i) # plt.plot(imf) # i += 1 # # plt.savefig('res/result_imf.png') # plt.show() test = np.zeros([len(do) - c - lookback_window, 1])
def protected_mask(path_nc4, path_protected_shp, lats, lons, mean_file): #read data and variables f = netCDF4.Dataset(path_nc4) f.set_auto_mask(False) #missing value = -9999.0 LAI = f.variables['LAI'] time = f.variables['time'] dates = num2date(time[:], time.units) latt, lonn = f.variables['lat'][:], f.variables['lon'][:] #get actual lon/lat range latbounds = list(latt[(latt <= lats[1]) & (latt >= lats[0])]) lonbounds = list(lonn[(lonn <= lons[1]) & (lonn >= lons[0])]) #create map polygon grid, rows, cols = create_grid(lonbounds[0], latbounds[0], lonbounds[-1], latbounds[-1]) #read proctected shape file points_shape_map = gpd.read_file(path_protected_shp) points_shape_map.to_crs(epsg=4326, inplace=True) #identify grid cells in the shape file intersect with polygons data = [] for index, protected in points_shape_map.iterrows(): for index2, cell in grid.iterrows(): if protected['geometry'].intersects(cell['geometry']): data.append({'geometry': cell['geometry']}) df = gpd.GeoDataFrame(data, columns=['geometry']) #and drop duplicates by convert to wkb df["geometry"] = df["geometry"].apply(lambda geom: geom.wkb) df = df.drop_duplicates(["geometry"]) # convert back to shapely geometry df["geometry"] = df["geometry"].apply(lambda geom: shapely.wkb.loads(geom)) #map back to lat,lon list #extract all coordinates from geometry column g = [i for i in df.geometry] #map all polygons to coordinates all_coords = [mapping(item)["coordinates"] for item in g] #loop through all coordinates to find corresponding lat, lon tuple list_protected = [] for coords in all_coords: idx_tup = coords[0][0] idx_lat = np.where(latt == idx_tup[1])[0][0] idx_lon = np.where(lonn == idx_tup[0])[0][0] list_protected.append([idx_lat, idx_lon]) #get all monthly data, perform detrending using EEMD, and perform surrogate test for significance # Assign EEMD to `eemd` variable eemd = EEMD() eemd.noise_seed(12345) #detect extrema using parabolic method emd = eemd.EMD emd.extrema_detection = "parabol" #initial result dataframe lai_monthly_mkt, lai_monthly_cat = [], [] for i in range(12): t = mean_file.shape[1] #get time-length, lat length, lon length lai_cat = np.zeros( (len(latt), len(lonn))) #initiate global mask for categorization lai_mkt = np.zeros( (len(latt), len(lonn))) #initiate global mask for stipling significance for coords in list_protected: lat, lon = coords[0], coords[1] lai_monthly = mean_file[ i, :, lat, lon] #subset per month per lat lon, using the calculated mean_file if lai_monthly.all() < 0: #missing data present lai_cat[lat, lon] = 0 lai_mkt[lat, lon] = 0 else: eIMFs_n = eemd.eemd(lai_monthly, np.arange(t)) trend = eIMFs_n[-1] if check_increasing(trend): lai_cat[lat, lon] = 1 #test for significance: ta_list = nonlinear_trend_test(trend) if np.argmax(ta_list) == 0: lai_mkt[lat, lon] = 1 elif check_decreasing(trend): lai_cat[lat, lon] = -1 #test for significance: ta_list = nonlinear_trend_test(trend) if np.argmin(ta_list) == 0: lai_mkt[lat, lon] = -1 else: lai_cat[ lat, lon] = 5 #flag out inflection shape trend series if any lai_monthly_mkt.append(lai_mkt) lai_monthly_cat.append(lai_cat) #convert the result list back to arrays lai_monthly_mkt = np.array(lai_monthly_mkt) lai_monthly_cat = np.array(lai_monthly_cat) # subset the region of interest by lon lat indexes # latitude lower and upper index latli = np.argmin(np.abs(latt - lats[0])) latui = np.argmin(np.abs(latt - lats[1])) # longitude lower and upper index lonli = np.argmin(np.abs(lonn - lons[0])) lonui = np.argmin(np.abs(lonn - lons[1])) lai_cat = lai_monthly_cat[:, latli:latui, lonli:lonui] lai_mkt = lai_monthly_mkt[:, latli:latui, lonli:lonui] # Write the array to disk with open('cat_sg3_ken_sur.txt', 'w') as outfile: outfile.write('# Array shape: {0}\n'.format(lai_cat.shape)) for data_slice in lai_cat: np.savetxt(outfile, data_slice, fmt='%-7.2f') outfile.write('# New slice\n') # Write the array to disk with open('mkt_sg3_ken_sur.txt', 'w') as outfile: outfile.write('# Array shape: {0}\n'.format(lai_mkt.shape)) for data_slice in lai_mkt: np.savetxt(outfile, data_slice, fmt='%-7.2f') outfile.write('# New slice\n') #print out any infletion point index print(np.where(lai_cat == 5)) return lai_cat, lai_mkt, latbounds, lonbounds, '20 SG3 models mean'