def _get_mutual_inf_EQQ(a): """ Gets mutual information using EQQ algorithm for given data. """ i, j, ph1, ph2 = a return i, j, MI.mutual_information(ph1, ph2, algorithm = 'EQQ2', bins = 4, log2 = False)
def _coherency_surrogates(a): aa_surr, aa_seas, scales, temp = a aa_surr.construct_fourier_surrogates_spatial() # aa_surr.construct_surrogates_with_residuals() aa_surr.add_seasonality(aa_seas[0], aa_seas[1], aa_seas[2]) coherence = [] wvlt_coherence = [] for sc in scales: period = sc # frequency of interest in months s0 = period / fourier_factor # get scale wave_temp, _, _, _ = wvlt.continous_wavelet(temp, 1, False, wvlt.morlet, dj=0, s0=s0, j1=0, k0=k0) # perform wavelet phase_temp = np.arctan2( np.imag(wave_temp), np.real(wave_temp))[0, 12:-12] # get phases from oscillatory modes wave_aa, _, _, _ = wvlt.continous_wavelet(aa_surr.surr_data, 1, False, wvlt.morlet, dj=0, s0=s0, j1=0, k0=k0) # perform wavelet phase_aa = np.arctan2( np.imag(wave_aa), np.real(wave_aa))[0, 12:-12] # get phases from oscillatory modes # mutual information coherence coherence.append( mi.mutual_information(phase_aa, phase_temp, algorithm='EQQ2', bins=8, log2=False)) # wavelet coherence w1 = np.complex(0, 0) w2 = w1 w3 = w1 for i in range(12, aa.time.shape[0] - 12): w1 += wave_aa[0, i] * np.conjugate(wave_temp[0, i]) w2 += wave_aa[0, i] * np.conjugate(wave_aa[0, i]) w3 += wave_temp[0, i] * np.conjugate(wave_temp[0, i]) w1 /= np.sqrt(np.abs(w2) * np.abs(w3)) wvlt_coherence.append(np.abs(w1)) coherence = np.array(coherence) wvlt_coherence = np.array(wvlt_coherence) return coherence, wvlt_coherence
def _get_autocoherence(a): i, j, avg, ts = a coh = [] for tau in range(1, avg): ts0 = ts[tau:].copy() ts1 = ts[:-tau].copy() coh.append(mutual_information(ts0, ts1, algorithm = 'EQQ2', bins = 4, log2 = False)) return i, j, np.mean(np.array(coh))
def _get_autocoherence(a): i, j, avg, ts = a coh = [] for tau in range(1, avg): ts0 = ts[tau:].copy() ts1 = ts[:-tau].copy() coh.append( mutual_information(ts0, ts1, algorithm='EQQ2', bins=4, log2=False)) return i, j, np.mean(np.array(coh))
def _get_automutual_info(a): """ Gets automutual information function. """ i, j, to, ph = a result = [] for tau in range(1,to): result.append(MI.mutual_information(ph[tau:], ph[:-tau], algorithm = 'EQQ2', bins = 4, log2 = False)) return i, j, np.array(result)
def _process_matrix(self, jobq, resq): while True: a = jobq.get() # get queued input if a is None: # if it is None, we are finished, put poison pill to resq break # break infinity cycle else: i, j, ph1, ph2, method = a # compute stuff if method == "MPC": # get phase diff diff = ph1 - ph2 # compute mean phase coherence coh = np.power(np.mean(np.cos(diff)), 2) + np.power(np.mean(np.sin(diff)), 2) resq.put((i, j, coh)) elif method == "MIEQQ": resq.put((i, j, MI.mutual_information(ph1, ph2, algorithm = 'EQQ2', bins = 4, log2 = False))) elif method == "MIKNN": resq.put((i, j, MI.knn_mutual_information(ph1, ph2, k = 32, dualtree = True))) elif method == "MIGAU": corr = np.corrcoef([ph1, ph2])[0, 1] mi = -0.5 * np.log(1 - np.power(corr, 2)) if corr < 1. else 0 resq.put((i, j, mi)) elif method == "COV": resq.put((i, j, np.cov(ph1, ph2, ddof = 1)[0,1])) elif method == "CORR": resq.put((i, j, st.pearsonr(ph1, ph2)[0])) elif method == "WCOH": # input field must be wave from wavelet!!!! w1 = np.complex(0, 0) w2 = w1; w3 = w1 for t in range(0, self.time.shape[0]): w1 += ph1[t] * np.conjugate(ph2[t]) w2 += ph1[t] * np.conjugate(ph1[t]) w3 += ph2[t] * np.conjugate(ph2[t]) w1 /= np.sqrt(np.abs(w2) * np.abs(w3)) resq.put((i, j, np.abs(w1))) elif method[0] == 'L': p = int(method[1]) res = 0 for t in range(ph1.shape[0]): res += np.power(np.abs(ph1[t] - ph2[t]), p) resq.put((i, j, np.power(res, 1./p)))
def _coherency_surrogates(a): aa_surr, aa_seas, scales, temp = a aa_surr.construct_fourier_surrogates_spatial() aa_surr.amplitude_adjust_surrogates(aa_seas[0], aa_seas[1], aa_seas[2]) # aa_surr.construct_surrogates_with_residuals() # aa_surr.construct_surrogates_with_residuals() # aa_surr.add_seasonality(aa_seas[0][:-1], aa_seas[1][:-1], aa_seas[2][:-1]) coherence = [] wvlt_coherence = [] for sc in scales: temp.wavelet(sc, period_unit='d', save_wave=True) phase_temp = temp.phase.copy() wave_temp = temp.wave.copy() phase_aa, _, wave_aa = temp.wavelet(sc, period_unit='d', ts=aa_surr.surr_data, save_wave=True) # mutual information coherence coherence.append( mi.mutual_information(phase_aa, phase_temp, algorithm='EQQ2', bins=8, log2=False)) # corr = np.corrcoef([phase_aa, phase_temp])[0, 1] # coherence.append(-0.5 * np.log(1 - np.power(corr, 2))) # wavelet coherence w1 = np.complex(0, 0) w2 = w1 w3 = w1 for i in range(12, aa.time.shape[0] - 12): w1 += wave_aa[i] * np.conjugate(wave_temp[i]) w2 += wave_aa[i] * np.conjugate(wave_aa[i]) w3 += wave_temp[i] * np.conjugate(wave_temp[i]) w1 /= np.sqrt(np.abs(w2) * np.abs(w3)) wvlt_coherence.append(np.abs(w1)) coherence = np.array(coherence) wvlt_coherence = np.array(wvlt_coherence) return coherence, wvlt_coherence
def _coherency_surrogates(a): aa_surr, aa_seas, scales, temp = a aa_surr.construct_fourier_surrogates_spatial() aa_surr.amplitude_adjust_surrogates(aa_seas[0], aa_seas[1], aa_seas[2]) # aa_surr.construct_surrogates_with_residuals() # aa_surr.construct_surrogates_with_residuals() # aa_surr.add_seasonality(aa_seas[0][:-1], aa_seas[1][:-1], aa_seas[2][:-1]) coherence = [] wvlt_coherence = [] for sc in scales: temp.wavelet(sc, period_unit = 'd', save_wave = True) phase_temp = temp.phase.copy() wave_temp = temp.wave.copy() phase_aa, _, wave_aa = temp.wavelet(sc, period_unit = 'd', ts = aa_surr.surr_data, save_wave = True) # mutual information coherence coherence.append(mi.mutual_information(phase_aa, phase_temp, algorithm = 'EQQ2', bins = 8, log2 = False)) # corr = np.corrcoef([phase_aa, phase_temp])[0, 1] # coherence.append(-0.5 * np.log(1 - np.power(corr, 2))) # wavelet coherence w1 = np.complex(0, 0) w2 = w1; w3 = w1 for i in range(12,aa.time.shape[0] - 12): w1 += wave_aa[i] * np.conjugate(wave_temp[i]) w2 += wave_aa[i] * np.conjugate(wave_aa[i]) w3 += wave_temp[i] * np.conjugate(wave_temp[i]) w1 /= np.sqrt(np.abs(w2) * np.abs(w3)) wvlt_coherence.append(np.abs(w1)) coherence = np.array(coherence) wvlt_coherence = np.array(wvlt_coherence) return coherence, wvlt_coherence
def _coherency_surrogates(a): aa_surr, aa_seas, scales, temp = a aa_surr.construct_fourier_surrogates_spatial() # aa_surr.construct_surrogates_with_residuals() aa_surr.add_seasonality(aa_seas[0], aa_seas[1], aa_seas[2]) coherence = [] wvlt_coherence = [] for sc in scales: period = sc # frequency of interest in months s0 = period / fourier_factor # get scale wave_temp, _, _, _ = wvlt.continous_wavelet(temp, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet phase_temp = np.arctan2(np.imag(wave_temp), np.real(wave_temp))[0, 12:-12] # get phases from oscillatory modes wave_aa, _, _, _ = wvlt.continous_wavelet(aa_surr.surr_data, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet phase_aa = np.arctan2(np.imag(wave_aa), np.real(wave_aa))[0, 12:-12] # get phases from oscillatory modes # mutual information coherence coherence.append(mi.mutual_information(phase_aa, phase_temp, algorithm = 'EQQ2', bins = 8, log2 = False)) # wavelet coherence w1 = np.complex(0, 0) w2 = w1; w3 = w1 for i in range(12,aa.time.shape[0] - 12): w1 += wave_aa[0, i] * np.conjugate(wave_temp[0, i]) w2 += wave_aa[0, i] * np.conjugate(wave_aa[0, i]) w3 += wave_temp[0, i] * np.conjugate(wave_temp[0, i]) w1 /= np.sqrt(np.abs(w2) * np.abs(w3)) wvlt_coherence.append(np.abs(w1)) coherence = np.array(coherence) wvlt_coherence = np.array(wvlt_coherence) return coherence, wvlt_coherence
cmi2 = [] for sc in scales: temp.wavelet(sc, period_unit='d', save_wave=True) phase_temp = temp.phase.copy() wave_temp = temp.wave.copy() aa.wavelet(sc, period_unit='d', save_wave=True) phase_aa = aa.phase.copy() # get phases from oscillatory modes wave_aa = aa.wave.copy() # mutual information coherence coherence.append( mi.mutual_information(phase_aa, phase_temp, algorithm='EQQ2', bins=8, log2=False)) # corr = np.corrcoef([phase_aa, phase_temp])[0, 1] # coherence.append(-0.5 * np.log(1 - np.power(corr, 2))) # # cmi1 # # plt.figure() # tmp = [] # for tau in range(1,30): # x, y, z = mi.get_time_series_condition([phase_temp, phase_aa], tau = tau, dim_of_condition = 1, eta = 0, phase_diff = True) # tmp.append(mi.cond_mutual_information(x, y, z, algorithm = 'EQQ2', bins = 8, log2 = False)) # cmi1.append(np.mean(np.array(tmp))) # # plt.plot(tmp, label = "1->2") # # cmi2
from src.data_class import load_station_data from src.mutual_information import mutual_information import numpy as np from datetime import date import matplotlib.pyplot as plt g = load_station_data('../data/TG_STAID000027.txt', date(1958,1,1), date(2015,11,1), False) g.get_data_of_precise_length(14976, start_date = date(1958, 1, 1), apply_to_data = True) mi = [mutual_information(g.data, g.data)] for d in np.arange(1,401,1): mi.append(mutual_information(g.data[d:], g.data[:-d])) plt.plot(mi) plt.show() # # g.wavelet(1, period_unit = 'y') # # reconstruction = g.amplitude * np.cos(g.phase) # # fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T # # m, c = np.linalg.lstsq(fit_x, g.data)[0] # # amplitude = m * g.amplitude + c # g.wavelet(8, period_unit = 'y') # g.anomalise()
coherence = [] wvlt_coherence = [] cmi1 = [] cmi2 = [] for sc in scales: temp.wavelet(sc, period_unit = 'd', save_wave = True) phase_temp = temp.phase.copy() wave_temp = temp.wave.copy() aa.wavelet(sc, period_unit = 'd', save_wave = True) phase_aa = aa.phase.copy() # get phases from oscillatory modes wave_aa = aa.wave.copy() # mutual information coherence coherence.append(mi.mutual_information(phase_aa, phase_temp, algorithm = 'EQQ2', bins = 8, log2 = False)) # corr = np.corrcoef([phase_aa, phase_temp])[0, 1] # coherence.append(-0.5 * np.log(1 - np.power(corr, 2))) # # cmi1 # # plt.figure() # tmp = [] # for tau in range(1,30): # x, y, z = mi.get_time_series_condition([phase_temp, phase_aa], tau = tau, dim_of_condition = 1, eta = 0, phase_diff = True) # tmp.append(mi.cond_mutual_information(x, y, z, algorithm = 'EQQ2', bins = 8, log2 = False)) # cmi1.append(np.mean(np.array(tmp))) # # plt.plot(tmp, label = "1->2") # # cmi2 # tmp = [] # for tau in range(1,30):
from src.data_class import load_station_data from src.mutual_information import mutual_information import numpy as np from datetime import date import matplotlib.pyplot as plt g = load_station_data('../data/TG_STAID000027.txt', date(1958, 1, 1), date(2015, 11, 1), False) g.get_data_of_precise_length(14976, start_date=date(1958, 1, 1), apply_to_data=True) mi = [mutual_information(g.data, g.data)] for d in np.arange(1, 401, 1): mi.append(mutual_information(g.data[d:], g.data[:-d])) plt.plot(mi) plt.show() # # g.wavelet(1, period_unit = 'y') # # reconstruction = g.amplitude * np.cos(g.phase) # # fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T # # m, c = np.linalg.lstsq(fit_x, g.data)[0] # # amplitude = m * g.amplitude + c # g.wavelet(8, period_unit = 'y') # g.anomalise() # def get_equidistant_bins(): # return np.array(np.linspace(-np.pi, np.pi, 9))