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
Exemple #2
0
def load_nino34_wavelet_phase(start_date, end_date, anom=True):
    raw = np.loadtxt('/home/nikola/Work/phd/data/nino34monthly.txt')
    data = []
    time = []
    for y in range(raw.shape[0]):
        for m in range(1, 13):
            dat = float(raw[y, m])
            data.append(dat)
            time.append(date(int(raw[y, 0]), m, 1).toordinal())

    g = DataField(data=np.array(data), time=np.array(time))
    g.location = "NINO3.4"
    g.select_date(start_date, end_date)

    if anom:
        g.anomalise()

    k0 = 6.  # wavenumber of Morlet wavelet used in analysis
    fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0, 2)))
    per = PERIOD * 12  # frequency of interest
    s0 = per / fourier_factor  # get scale

    wave, _, _, _ = wvlt.continous_wavelet(g.data,
                                           1,
                                           False,
                                           wvlt.morlet,
                                           dj=0,
                                           s0=s0,
                                           j1=0,
                                           k0=6.)
    phase = np.arctan2(np.imag(wave), np.real(wave))[0, :]

    return phase
def _get_amplitude(a):
    """
    Gets amplitude of yearly cycle from SAT data.
    """
    i, j, s0_amp, data = a
    if not np.any(np.isnan(data)):
        wave, _, _, _ = wvlt.continous_wavelet(data,
                                               1,
                                               False,
                                               wvlt.morlet,
                                               dj=0,
                                               s0=s0_amp,
                                               j1=0,
                                               k0=6.)  # perform wavelet
        amplitude = np.sqrt(
            np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
        amplitude = amplitude[0, :]
        phase_amp = np.arctan2(np.imag(wave), np.real(wave))
        phase_amp = phase_amp[0, :]
        # fitting oscillatory phase / amplitude to actual SAT
        reconstruction = amplitude * np.cos(phase_amp)
        fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
        m, c = np.linalg.lstsq(fit_x, data)[0]
        amplitude = m * amplitude + c
    else:
        amplitude = np.nan

    return i, j, amplitude
Exemple #4
0
def load_nino34_wavelet_phase(start_date, end_date, anom = True):
    raw = np.loadtxt('/home/nikola/Work/phd/data/nino34monthly.txt')
    data = []
    time = []
    for y in range(raw.shape[0]):
        for m in range(1,13):
            dat = float(raw[y, m])
            data.append(dat)
            time.append(date(int(raw[y, 0]), m, 1).toordinal())

    g = DataField(data = np.array(data), time = np.array(time))
    g.location = "NINO3.4"
    g.select_date(start_date, end_date)

    if anom:
        g.anomalise()

    k0 = 6. # wavenumber of Morlet wavelet used in analysis
    fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0,2)))
    per = PERIOD * 12 # frequency of interest
    s0 = per / fourier_factor # get scale

    wave, _, _, _ = wvlt.continous_wavelet(g.data, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = 6.)
    phase = np.arctan2(np.imag(wave), np.real(wave))[0, :]

    return phase
Exemple #5
0
def _cond_difference_surrogates(sg, jobq, resq):
    while jobq.get() is not None:
        difference = np.zeros((sg.lats.shape[0], sg.lons.shape[0]))
        mean_var = np.zeros_like(difference)
        sg.construct_multifractal_surrogates()
        sg.add_seasonality(mean, var, trend)
        for i in range(sg.lats.shape[0]):
            for j in range(sg.lons.shape[0]):
                wave, _, _, _ = wavelet_analysis.continous_wavelet(
                    sg.surr_data[:, i, j],
                    1,
                    False,
                    wavelet_analysis.morlet,
                    dj=0,
                    s0=s0,
                    j1=0,
                    k0=k0)  # perform wavelet
                phase = np.arctan2(
                    np.imag(wave),
                    np.real(wave))  # get phases from oscillatory modes
                for iota in range(
                        cond_means.shape[0]
                ):  # get conditional means for current phase range
                    #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
                    phase_bins = get_equidistant_bins()  # equidistant bins
                    ndx = ((phase[0, :] >= phase_bins[iota]) &
                           (phase[0, :] <= phase_bins[iota + 1]))
                    if MEANS:
                        cond_means[iota] = np.mean(g.data[ndx])
                    else:
                        cond_means[iota] = np.var(g.data[ndx], ddof=1)
                difference[i, j] = cond_means.max() - cond_means.min(
                )  # append difference to list
                mean_var[i, j] = np.mean(cond_means)
        resq.put((difference, mean_var))
Exemple #6
0
def _cond_difference_surrogates(sg, g_temp, a, start_cut, jobq, resq):
    mean, var, trend = a
    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
        elif SURR_TYPE == 'AR':
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(sg.surr_data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        _, _, idx = g_temp.get_data_of_precise_length(WINDOW_LENGTH, start_cut, None, False)
        sg.surr_data = sg.surr_data[idx[0] : idx[1]]
        phase = phase[0, idx[0] : idx[1]]
        phase_bins = get_equidistant_bins() # equidistant bins
        for i in range(cond_means.shape[0]): # get conditional means for current phase range
            #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
            ndx = ((phase >= phase_bins[i]) & (phase <= phase_bins[i+1]))
            if MEANS:
                cond_means[i] = np.mean(sg.surr_data[ndx])
            else:
                cond_means[i] = np.var(sg.surr_data[ndx], ddof = 1)
        diff = (cond_means.max() - cond_means.min()) # append difference to list    
        mean_var = np.mean(cond_means)
        
        resq.put((diff, mean_var))
def _get_oscillatory_modes(a):
    """
    Gets oscillatory modes in terms of phase and amplitude from wavelet analysis for given data.
    """
    i, j, s0, data = a
    if not np.all(np.isnan(data)):
        wave, _, _, _ = wvlt.continous_wavelet(data, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = 6.)
        phase = np.arctan2(np.imag(wave), np.real(wave))
    else:
        phase = np.nan
    
    return i, j, phase
Exemple #8
0
def _reconstruction_surrs(sg, a, jobq, resq, idx):
    mean, var, trend = a

    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
        sg.add_seasonality(mean, var, trend)

        # sg.amplitude_adjust_surrogates(mean, var, trend)

        period = AMP_PERIOD * 365.25  # frequency of interest
        s0_amp = period / fourier_factor  # get scale
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            sg.surr_data,
            1,
            False,
            wavelet_analysis.morlet,
            dj=0,
            s0=s0_amp,
            j1=0,
            k0=k0)  # perform wavelet
        amplitude2 = np.sqrt(
            np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
        amplitude2 = amplitude2[0, :]
        phase_amp = np.arctan2(np.imag(wave), np.real(wave))
        phase_amp = phase_amp[0, :]

        reconstruction = amplitude2 * np.cos(phase_amp)
        fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
        m, c = np.linalg.lstsq(fit_x, sg.surr_data)[0]
        amplitude2 = m * amplitude2 + c
        # amp_to_plot_surr = amplitude2.copy()
        # amplitude2 = m * reconstruction + c

        amplitude2 = amplitude2[idx[0]:idx[1]]
        # amp_to_plot_surr = amp_to_plot_surr[idx[0] : idx[1]]
        phase_amp = phase_amp[idx[0]:idx[1]]
        sg.surr_data = sg.surr_data[idx[0]:idx[1]]
        amp_to_plot_surr = sg.surr_data.copy()

        cond_temp = np.zeros((BINS, 2))
        for i in range(cond_means.shape[0]):
            ndx = ((phase_amp >= phase_bins[i]) &
                   (phase_amp <= phase_bins[i + 1]))
            cond_temp[i, 0] = np.mean(amplitude2[ndx])
            cond_temp[i, 1] = np.mean(sg.surr_data[ndx])
        amp_diff = cond_temp[:, 0].max() - cond_temp[:, 0].min()
        data_diff = cond_temp[:, 1].max() - cond_temp[:, 1].min()

        resq.put([cond_temp, amp_diff, data_diff, data_diff])
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 _cmi_surrogates(a):
    aa, aa_surr, aa_seas, temp, temp_surr, temp_seas, scales = a
    aa_surr.construct_fourier_surrogates_spatial()
    aa_surr.add_seasonality(aa_seas[0], aa_seas[1], aa_seas[2])


    cmi1 = []
    cmi2 = []

    for sc in scales:
        period = sc # frequency of interest in months
        s0 = period / fourier_factor # get scale
        wave_temp, _, _, _ = wvlt.continous_wavelet(temp.data, 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_surr = np.arctan2(np.imag(wave_aa), np.real(wave_aa))[0, 12:-12] # get phases from oscillatory modes

        # cmi1
        tmp = []
        for tau in range(1,30):
            x, y, z = mi.get_time_series_condition([phase_temp, phase_aa_surr], tau = tau, dim_of_condition = 1, eta = 1, phase_diff = True)
            tmp.append(mi.cond_mutual_information(x, y, z, algorithm = 'EQQ2', bins = 4, log2 = False))
        cmi1.append(np.mean(np.array(tmp)))

        # cmi2
        tmp = []
        for tau in range(1,30):
            x, y, z = mi.get_time_series_condition([phase_aa_surr, phase_temp], tau = tau, dim_of_condition = 1, eta = 1, phase_diff = True)
            tmp.append(mi.cond_mutual_information(x, y, z, algorithm = 'EQQ2', bins = 4, log2 = False))
        cmi2.append(np.mean(np.array(tmp)))

    cmi1 = np.array(cmi1)
    cmi2 = np.array(cmi2)

    return cmi1, cmi2
Exemple #11
0
def _get_oscillatory_modes(a):
    """
    Gets oscillatory modes in terms of phase and amplitude from wavelet analysis for given data.
    """
    i, j, s0, data = a
    if not np.all(np.isnan(data)):
        wave, _, _, _ = wvlt.continous_wavelet(data,
                                               1,
                                               False,
                                               wvlt.morlet,
                                               dj=0,
                                               s0=s0,
                                               j1=0,
                                               k0=6.)
        phase = np.arctan2(np.imag(wave), np.real(wave))
    else:
        phase = np.nan

    return i, j, phase
def _reconstruction_surrs(sg, a, jobq, resq, idx):
    mean, var, trend = a

    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
        sg.add_seasonality(mean, var, trend)

        # sg.amplitude_adjust_surrogates(mean, var, trend)

        period = AMP_PERIOD * 365.25 # frequency of interest
        s0_amp = period / fourier_factor # get scale
        wave, _, _, _ = wavelet_analysis.continous_wavelet(sg.surr_data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0_amp, j1 = 0, k0 = k0) # perform wavelet
        amplitude2 = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
        amplitude2 = amplitude2[0, :]
        phase_amp = np.arctan2(np.imag(wave), np.real(wave))
        phase_amp = phase_amp[0, :]

        reconstruction = amplitude2 * np.cos(phase_amp)
        fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
        m, c = np.linalg.lstsq(fit_x, sg.surr_data)[0]
        amplitude2 = m * amplitude2 + c
        # amp_to_plot_surr = amplitude2.copy()
        # amplitude2 = m * reconstruction + c

        amplitude2 = amplitude2[idx[0] : idx[1]]
        # amp_to_plot_surr = amp_to_plot_surr[idx[0] : idx[1]]
        phase_amp = phase_amp[idx[0] : idx[1]]
        sg.surr_data =  sg.surr_data[idx[0] : idx[1]]
        amp_to_plot_surr = sg.surr_data.copy()

        cond_temp = np.zeros((BINS,2))
        for i in range(cond_means.shape[0]):
            ndx = ((phase_amp >= phase_bins[i]) & (phase_amp <= phase_bins[i+1]))
            cond_temp[i,0] = np.mean(amplitude2[ndx])
            cond_temp[i,1] = np.mean(sg.surr_data[ndx])
        amp_diff = cond_temp[:, 0].max() - cond_temp[:, 0].min()
        data_diff = cond_temp[:, 1].max() - cond_temp[:, 1].min()

        resq.put([cond_temp, amp_diff, data_diff, data_diff])
def _get_amplitude(a):
    """
    Gets amplitude of yearly cycle from SAT data.
    """
    i, j, s0_amp, data = a
    if not np.any(np.isnan(data)):
        wave, _, _, _ = wvlt.continous_wavelet(data, 1, False, wvlt.morlet, dj = 0, s0 = s0_amp, j1 = 0, k0 = 6.) # perform wavelet
        amplitude = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
        amplitude = amplitude[0, :]
        phase_amp = np.arctan2(np.imag(wave), np.real(wave))
        phase_amp = phase_amp[0, :]
        # fitting oscillatory phase / amplitude to actual SAT
        reconstruction = amplitude * np.cos(phase_amp)
        fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
        m, c = np.linalg.lstsq(fit_x, data)[0]
        amplitude = m * amplitude + c
    else:
        amplitude = np.nan

    return i, j, amplitude
Exemple #14
0
def _cond_difference_surrogates(sg, jobq, resq):
    while jobq.get() is not None:
        difference = np.zeros((sg.lats.shape[0], sg.lons.shape[0]))
        mean_var = np.zeros_like(difference)
        sg.construct_multifractal_surrogates()
        sg.add_seasonality(mean, var, trend)
        for i in range(sg.lats.shape[0]):
            for j in range(sg.lons.shape[0]):
                wave, _, _, _ = wavelet_analysis.continous_wavelet(sg.surr_data[:, i, j], 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
                phase = np.arctan2(np.imag(wave), np.real(wave)) # get phases from oscillatory modes
                for iota in range(cond_means.shape[0]): # get conditional means for current phase range
                    #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
                    phase_bins = get_equidistant_bins() # equidistant bins
                    ndx = ((phase[0,:] >= phase_bins[iota]) & (phase[0,:] <= phase_bins[iota+1]))
                    if MEANS:
                        cond_means[iota] = np.mean(g.data[ndx])
                    else:
                        cond_means[iota] = np.var(g.data[ndx], ddof = 1)
                difference[i, j] = cond_means.max() - cond_means.min() # append difference to list    
                mean_var[i, j] = np.mean(cond_means)
        resq.put((difference, mean_var))
Exemple #15
0
def _cond_difference_surrogates(sg, g_temp, a, start_cut, jobq, resq):
    mean, var, trend = a
    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
        elif SURR_TYPE == 'AR':
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            sg.surr_data,
            1,
            False,
            wavelet_analysis.morlet,
            dj=0,
            s0=s0,
            j1=0,
            k0=k0)  # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        _, _, idx = g_temp.get_data_of_precise_length(WINDOW_LENGTH, start_cut,
                                                      None, False)
        sg.surr_data = sg.surr_data[idx[0]:idx[1]]
        phase = phase[0, idx[0]:idx[1]]
        phase_bins = get_equidistant_bins()  # equidistant bins
        for i in range(cond_means.shape[0]
                       ):  # get conditional means for current phase range
            #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
            ndx = ((phase >= phase_bins[i]) & (phase <= phase_bins[i + 1]))
            if MEANS:
                cond_means[i] = np.mean(sg.surr_data[ndx])
            else:
                cond_means[i] = np.var(sg.surr_data[ndx], ddof=1)
        diff = (cond_means.max() - cond_means.min()
                )  # append difference to list
        mean_var = np.mean(cond_means)

        resq.put((diff, mean_var))
Exemple #16
0
def _cond_difference_surrogates(sg, a, jobq, resq):
    while jobq.get() is not None:
        cond_means_temp = np.zeros_like(cond_means)
        if MF_SURR:
            sg.construct_multifractal_surrogates()
        else:
            sg.construct_fourier_surrogates_spatial()
        sg.add_seasonality(a[0], a[1], a[2])

        wave, _, _, _ = wavelet_analysis.continous_wavelet(sg.surr_data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))

        sg.surr_data = sg.surr_data[edge_cut_ndx]
        phase = phase[0, edge_cut_ndx]

        phase_bins = get_equidistant_bins()
        for i in range(cond_means_surr.shape[0]):
            ndx = ((phase >= phase_bins[i]) & (phase <= phase_bins[i+1]))
            if MEANS:
                cond_means_temp[i] = np.mean(sg.surr_data[ndx])
            else:
                cond_means_temp[i] = np.var(sg.surr_data[ndx], ddof = 1)
         resq.put(cond_means_temp)
Exemple #17
0
_, _, idx = g.get_data_of_precise_length(WINDOW_LENGTH,
                                         date.fromordinal(g.time[4 * y]), None,
                                         False)
first_mid_year = date.fromordinal(g.time[idx[0] + WINDOW_LENGTH / 2]).year

while end_idx < g.data.shape[0]:

    # data
    g_working.data = g.data[start_idx:end_idx].copy()
    g_working.time = g.time[start_idx:end_idx].copy()
    if np.all(np.isnan(g_working.data) == False):
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            g_working.data,
            1,
            False,
            wavelet_analysis.morlet,
            dj=0,
            s0=s0,
            j1=0,
            k0=k0)  # perform wavelet
        phase = np.arctan2(np.imag(wave),
                           np.real(wave))  # get phases from oscillatory modes
        start_cut = date(start_year + cnt * WINDOW_SHIFT, sm, sd)
        idx = g_working.get_data_of_precise_length(WINDOW_LENGTH, start_cut,
                                                   None, True)  # 16k or 13462
        print 'data ', g.get_date_from_ndx(
            start_idx), ' - ', g.get_date_from_ndx(end_idx)
        print 'cut from ', start_cut, ' to ', g_working.get_date_from_ndx(-1)
        last_mid_year = date.fromordinal(g_working.time[WINDOW_LENGTH /
                                                        2]).year
        phase = phase[0, idx[0]:idx[1]]
Exemple #18
0
                              date(2013, 10, 1), False)
g_data = DataField()

print(
    "[%s] Wavelet analysis in progress with %d year window shifted by %d year(s)..."
    % (str(datetime.now()), WINDOW_LENGTH, WINDOW_SHIFT))
k0 = 6.  # wavenumber of Morlet wavelet used in analysis
y = 365.25  # year in days
fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0, 2)))
period = PERIOD * y  # frequency of interest
s0 = period / fourier_factor  # get scale
# wavelet - data
wave, _, _, _ = wavelet_analysis.continous_wavelet(g.data,
                                                   1,
                                                   False,
                                                   wavelet_analysis.morlet,
                                                   dj=0,
                                                   s0=s0,
                                                   j1=0,
                                                   k0=k0)  # perform wavelet
phase = np.arctan2(np.imag(wave),
                   np.real(wave))  # get phases from oscillatory modes

if AMPLITUDE:
    period = 1 * 365.25  # frequency of interest
    s0_amp = period / fourier_factor  # get scale
    wave, _, _, _ = wavelet_analysis.continous_wavelet(
        g_amp.data,
        1,
        False,
        wavelet_analysis.morlet,
        dj=0,
def _cond_difference_surrogates(sg, sg_amp, g_temp, a, a_amp, start_cut, jobq,
                                resq, ndx_seas):
    mean, var, trend = a
    if AMPLITUDE:
        mean2, var2, trend2 = a_amp
    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_multifractal_surrogates()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_fourier_surrogates_spatial()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'AR':
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
            if AMPLITUDE:
                sg_amp.construct_surrogates_with_residuals()
                sg_amp.add_seasonality(mean2[:-1, ...], var2[:-1, ...],
                                       trend2[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            sg.surr_data,
            1,
            False,
            wavelet_analysis.morlet,
            dj=0,
            s0=s0,
            j1=0,
            k0=k0)  # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        if AMPLITUDE:
            wave, _, _, _ = wavelet_analysis.continous_wavelet(
                sg_amp.surr_data,
                1,
                False,
                wavelet_analysis.morlet,
                dj=0,
                s0=s0_amp,
                j1=0,
                k0=k0)  # perform wavelet
            amplitude = np.sqrt(
                np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
            amplitude = amplitude[0, :]
            phase_amp = np.arctan2(np.imag(wave), np.real(wave))
            phase_amp = phase_amp[0, :]
            reconstruction = amplitude * np.cos(phase_amp)
            fit_x = np.vstack(
                [reconstruction,
                 np.ones(reconstruction.shape[0])]).T
            m, c = np.linalg.lstsq(fit_x, sg_amp.surr_data)[0]
            amplitude = m * amplitude + c

        if AA:
            sg.amplitude_adjust_surrogates(mean, var, trend)
        _, _, idx = g_temp.get_data_of_precise_length(WINDOW_LENGTH, start_cut,
                                                      None, False)
        sg.surr_data = sg.surr_data[idx[0]:idx[1]]
        phase = phase[0, idx[0]:idx[1]]
        if AMPLITUDE:
            amplitude = amplitude[idx[0]:idx[1]]
        if SEASON != None:
            sg.surr_data = sg.surr_data[ndx_seas]
            phase = phase[ndx_seas]
        phase_bins = get_equidistant_bins()  # equidistant bins
        cond_means_temp = np.zeros((8, ))
        for i in range(cond_means_temp.shape[0]
                       ):  # get conditional means for current phase range
            #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
            ndx = ((phase >= phase_bins[i]) & (phase <= phase_bins[i + 1]))
            if MOMENT == 'std':
                if AMPLITUDE:
                    cond_means_temp[i] = func(amplitude[ndx], ddof=1)
                else:
                    cond_means_temp[i] = func(sg.surr_data[ndx], ddof=1)
            else:
                if AMPLITUDE:
                    cond_means_temp[i] = func(amplitude[ndx])
                else:
                    cond_means_temp[i] = func(sg.surr_data[ndx])
        ma = cond_means_temp.argmax()
        mi = cond_means_temp.argmin()
        if not SAME_BINS:
            diff = (cond_means_temp[ma] - cond_means_temp[mi]
                    )  # append difference to list
        elif SAME_BINS:
            diff = (cond_means_temp[max_bin] - cond_means_temp[min_bin])
        mean_var = np.mean(cond_means_temp)

        resq.put((diff, mean_var, cond_means_temp, np.abs(ma - mi)))
Exemple #20
0
PERCENTIL = 80


g = load_station_data('../data/TG_STAID000027.txt', date(1775, 1, 1), date(2014, 1, 1), False)
g_amp = load_station_data('../data/TG_STAID000027.txt', date(1775, 1, 1), date(2014, 1, 1), False)
# g_sun = load_sunspot_data('monthssn.dat', date(1775, 1, 1), date(2014, 1, 1), False)
tg = g.copy_data()
g.anomalise()

# tg is SAT, g.data is SATA

k0 = 6. # wavenumber of Morlet wavelet used in analysis
fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0,2)))
period = 8 * 365.25 # frequency of interest
s0 = period / fourier_factor # get scale
wave, _, _, _ = wvlt.continous_wavelet(g.data, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
phase = np.arctan2(np.imag(wave), np.real(wave)) # get phases from oscillatory modes

period = 1 * 365.25 # frequency of interest
s0 = period / fourier_factor # get scale
wave, _, _, _ = wvlt.continous_wavelet(g_amp.data, 1, False, wvlt.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
amplitude = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
amplitude = amplitude[0, :]
phase_amp = np.arctan2(np.imag(wave), np.real(wave))
phase_amp = phase_amp[0, :]

# fitting oscillatory phase / amplitude to actual SAT
reconstruction = amplitude * np.cos(phase_amp)
print reconstruction.shape
fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
m, c = np.linalg.lstsq(fit_x, g_amp.data)[0]
    k0 = 6.  # wavenumber of Morlet wavelet used in analysis
    fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0, 2)))

    coherence = []
    wvlt_coherence = []
    cmi1 = []
    cmi2 = []

    for sc in scales:
        period = sc  # frequency of interest in months
        s0 = period / fourier_factor  # get scale
        wave_temp, _, _, _ = wvlt.continous_wavelet(temp.data,
                                                    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.data,
                                                  1,
                                                  False,
                                                  wvlt.morlet,
                                                  dj=0,
                                                  s0=s0,
                                                  j1=0,
                                                  k0=k0)  # perform wavelet
        # from now only monthly -- for daily, wavelet needs polishing !!
        scales = np.arange(SCALES_SPAN[0], SCALES_SPAN[-1] + 1, 1)

        k0 = 6. # wavenumber of Morlet wavelet used in analysis
        fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0,2)))

        coherence = []
        wvlt_coherence = []
        cmi1 = []
        cmi2 = []

        for sc in scales:
            period = sc # frequency of interest in months
            s0 = period / fourier_factor # get scale
            wave_temp, _, _, _ = wvlt.continous_wavelet(temp.data, 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.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))

            # 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 = 4, log2 = False))
def _cond_difference_surrogates(sg, sg_amp, a, a2, jobq, resq):
    mean, var, trend = a
    mean2, var2, trend2 = a2
    last_mid_year = first_mid_year
    cond_means_out = np.zeros((8,))
    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_multifractal_surrogates()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_fourier_surrogates_spatial()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'AR':
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
            if AMPLITUDE:
                sg_amp.construct_surrogates_with_residuals()
                sg_amp.add_seasonality(mean2[:-1, ...], var2[:-1, ...], trend2[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(sg.surr_data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        if AMPLITUDE:
            wave, _, _, _ = wavelet_analysis.continous_wavelet(sg_amp.surr_data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0_amp, j1 = 0, k0 = k0) # perform wavelet
            amplitude = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
            amplitude = amplitude[0, :]
            phase_amp = np.arctan2(np.imag(wave), np.real(wave))
            phase_amp = phase_amp[0, :]

            # fitting oscillatory phase / amplitude to actual SAT
            reconstruction = amplitude * np.cos(phase_amp)
            fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
            m, c = np.linalg.lstsq(fit_x, sg_amp.surr_data)[0]
            amplitude = m * amplitude + c

        if SURR_TYPE == 'AR':
            sg.surr_data = sg.surr_data[main_cut_ndx[:-1]]
            if AMPLITUDE:
                amplitude = amplitude[main_cut_ndx[:-1]]
        else:
            sg.surr_data = sg.surr_data[main_cut_ndx]
            if AMPLITUDE:
                amplitude = amplitude[main_cut_ndx]
        phase = phase[0, main_cut_ndx]
        phase_bins = get_equidistant_bins() # equidistant bins
        cnt = 0

        difference_surr = []
        meanvar_surr = []

        start = 0
        end = WINDOW_LENGTH
        
        while end < sg.surr_data.shape[0]:
            cnt += 1
            surr_temp = sg.surr_data[start : end].copy()
            phase_temp = phase[start : end].copy()
            if AMPLITUDE:
                amp_temp = amplitude[start : end].copy()
            last_mid_year += 1
            for i in range(cond_means.shape[0]): # get conditional means for current phase range
                #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
                ndx = ((phase_temp >= phase_bins[i]) & (phase_temp <= phase_bins[i+1]))
                if MEANS:
                    if AMPLITUDE:
                        cond_means[i] = np.mean(amp_temp[ndx])
                    else:
                        cond_means[i] = np.mean(surr_temp[ndx])
                else:
                    if AMPLITUDE:
                        cond_means[i] = np.var(amp_temp[ndx], ddof = 1)
                    else:
                        cond_means[i] = np.var(surr_temp[ndx], ddof = 1)
            if PHASE_ANALYSIS_YEAR == last_mid_year:
                cond_means_out = cond_means.copy()
            difference_surr.append(cond_means.max() - cond_means.min()) # append difference to list    
            meanvar_surr.append(np.mean(cond_means))
            
            start = g.find_date_ndx(date(y1 + cnt*WINDOW_SHIFT, 7, 28))
            end = start + WINDOW_LENGTH
        
        resq.put((np.array(difference_surr), np.array(meanvar_surr), cond_means_out))


print("[%s] Wavelet analysis in progress with %d year window shifted by %d year(s)..." % (str(datetime.now()), WINDOW_LENGTH, WINDOW_SHIFT))
k0 = 6. # wavenumber of Morlet wavelet used in analysis
y = 365.25 # year in days
fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0,2)))
period = PERIOD * y # frequency of interest
s0 = period / fourier_factor # get scale 

cond_means = np.zeros((8,))

def get_equidistant_bins():
    return np.array(np.linspace(-np.pi, np.pi, 9))
    
wave, _, _, _ = wavelet_analysis.continous_wavelet(g.data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
phase = np.arctan2(np.imag(wave), np.real(wave)) # get phases from oscillatory modes

if AMPLITUDE:
    s0_amp = (1 * y) / fourier_factor
    wave, _, _, _ = wavelet_analysis.continous_wavelet(g_amp.data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0_amp, j1 = 0, k0 = k0) # perform wavelet
    amplitude = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
    amplitude = amplitude[0, :]
    phase_amp = np.arctan2(np.imag(wave), np.real(wave))
    phase_amp = phase_amp[0, :]

    # fitting oscillatory phase / amplitude to actual SAT
    reconstruction = amplitude * np.cos(phase_amp)
    fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
    m, c = np.linalg.lstsq(fit_x, g_amp.data)[0]
    amplitude = m * amplitude + c
def _cond_difference_surrogates(sg, sg_amp, g_temp, a, a_amp, start_cut, jobq, resq, ndx_seas):
    mean, var, trend = a
    if AMPLITUDE:
        mean2, var2, trend2 = a_amp
    while jobq.get() is not None:
        if SURR_TYPE == "MF":
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_multifractal_surrogates()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == "FT":
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_fourier_surrogates_spatial()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == "AR":
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
            if AMPLITUDE:
                sg_amp.construct_surrogates_with_residuals()
                sg_amp.add_seasonality(mean2[:-1, ...], var2[:-1, ...], trend2[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            sg.surr_data, 1, False, wavelet_analysis.morlet, dj=0, s0=s0, j1=0, k0=k0
        )  # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        if AMPLITUDE:
            wave, _, _, _ = wavelet_analysis.continous_wavelet(
                sg_amp.surr_data, 1, False, wavelet_analysis.morlet, dj=0, s0=s0_amp, j1=0, k0=k0
            )  # perform wavelet
            amplitude = np.sqrt(np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
            amplitude = amplitude[0, :]
            phase_amp = np.arctan2(np.imag(wave), np.real(wave))
            phase_amp = phase_amp[0, :]
            reconstruction = amplitude * np.cos(phase_amp)
            fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
            m, c = np.linalg.lstsq(fit_x, sg_amp.surr_data)[0]
            amplitude = m * amplitude + c

        if AA:
            sg.amplitude_adjust_surrogates(mean, var, trend)
        _, _, idx = g_temp.get_data_of_precise_length(WINDOW_LENGTH, start_cut, None, False)
        sg.surr_data = sg.surr_data[idx[0] : idx[1]]
        phase = phase[0, idx[0] : idx[1]]
        if AMPLITUDE:
            amplitude = amplitude[idx[0] : idx[1]]
        if SEASON != None:
            sg.surr_data = sg.surr_data[ndx_seas]
            phase = phase[ndx_seas]
        phase_bins = get_equidistant_bins()  # equidistant bins
        cond_means_temp = np.zeros((8,))
        for i in range(cond_means_temp.shape[0]):  # get conditional means for current phase range
            # phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
            ndx = (phase >= phase_bins[i]) & (phase <= phase_bins[i + 1])
            if MOMENT == "std":
                if AMPLITUDE:
                    cond_means_temp[i] = func(amplitude[ndx], ddof=1)
                else:
                    cond_means_temp[i] = func(sg.surr_data[ndx], ddof=1)
            else:
                if AMPLITUDE:
                    cond_means_temp[i] = func(amplitude[ndx])
                else:
                    cond_means_temp[i] = func(sg.surr_data[ndx])
        ma = cond_means_temp.argmax()
        mi = cond_means_temp.argmin()
        if not SAME_BINS:
            diff = cond_means_temp[ma] - cond_means_temp[mi]  # append difference to list
        elif SAME_BINS:
            diff = cond_means_temp[max_bin] - cond_means_temp[min_bin]
        mean_var = np.mean(cond_means_temp)

        resq.put((diff, mean_var, cond_means_temp, np.abs(ma - mi)))
Exemple #26
0
cond_means = np.zeros((8,))


def get_equidistant_bins():
    return np.array(np.linspace(-np.pi, np.pi, 9))
    

d, m, year = g.extract_day_month_year()
difference = np.zeros((g.lats.shape[0], g.lons.shape[0]))
mean_var = np.zeros_like(difference)

for i in range(g.lats.shape[0]):
    for j in range(g.lons.shape[0]):
        
        wave, _, _, _ = wavelet_analysis.continous_wavelet(g.data[:,i,j], 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave)) # get phases from oscillatory modes
        for iota in range(cond_means.shape[0]): # get conditional means for current phase range
            #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
            phase_bins = get_equidistant_bins() # equidistant bins
            ndx = ((phase[0,:] >= phase_bins[iota]) & (phase[0,:] <= phase_bins[iota+1]))
            if MEANS:
                cond_means[iota] = np.mean(g.data[ndx])
            else:
                cond_means[iota] = np.var(g.data[ndx], ddof = 1)
        difference[i, j] = cond_means.max() - cond_means.min() # append difference to list    
        mean_var[i, j] = np.mean(cond_means)
        

print("[%s] Wavelet analysis done. Now computing wavelet for MF surrogates in parallel..." % str(datetime.now()))
surrogates_difference = np.zeros([num_surr] + list(difference.shape))
Exemple #27
0
    g.anomalise()
    g_for_avg.anomalise()

    g_for_avg.select_date(date(1775, 1, 1), date(PAST_UNTIL, 1, 1))

if not USE_SURR:
    year = 365.25
    # get average extremes
    k0 = 6.  # wavenumber of Morlet wavelet used in analysis
    fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0, 2)))
    period = PERIOD * year  # frequency of interest
    s0 = period / fourier_factor  # get scale
    wave, _, _, _ = wvlt.continous_wavelet(g_for_avg.data,
                                           1,
                                           False,
                                           wvlt.morlet,
                                           dj=0,
                                           s0=s0,
                                           j1=0,
                                           k0=k0)  # perform wavelet
    phase = np.arctan2(np.imag(wave),
                       np.real(wave))  # get phases from oscillatory modes

    avg_ndx = g_for_avg.select_date(date(1779, 1, 1),
                                    date(PAST_UNTIL - 4, 1, 1))
    phase = phase[0, avg_ndx]
    tg_avg_sat = tg_avg_sat[avg_ndx]

    # sigma
    sigma = np.std(tg_avg_sat, axis=0, ddof=1)

    avg_bins = np.zeros((8, 2))  # bin no. x result no. (hot / cold extremes)
    g_data = DataField()
else:
    for i in range(len(STATIONS)):
        locals()['g' + str(i)] = load_station_data(STATIONS[i], date(1924,1,15), date(2013,10,1), True)
        if AMPLITUDE:
            locals()['g_amp' + str(i)] = load_station_data(STATIONS[i], date(1924,1,15), date(2013, 10, 1), False)
        locals()['g_data' + str(i)] = DataField()

k0 = 6. # wavenumber of Morlet wavelet used in analysis
y = 365.25 # year in days
fourier_factor = (4 * np.pi) / (k0 + np.sqrt(2 + np.power(k0,2)))
period = PERIOD * y # frequency of interest
s0 = period / fourier_factor # get scale 
# wavelet - data    
if STATIONS == None:
    wave, _, _, _ = wavelet_analysis.continous_wavelet(g.data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0, j1 = 0, k0 = k0) # perform wavelet
    phase = np.arctan2(np.imag(wave), np.real(wave)) # get phases from oscillatory modes

    if AMPLITUDE:
        period = 1 * 365.25 # frequency of interest
        s0_amp = period / fourier_factor # get scale
        wave, _, _, _ = wavelet_analysis.continous_wavelet(g_amp.data, 1, False, wavelet_analysis.morlet, dj = 0, s0 = s0_amp, j1 = 0, k0 = k0) # perform wavelet
        amplitude = np.sqrt(np.power(np.real(wave),2) + np.power(np.imag(wave),2))
        amplitude = amplitude[0, :]
        phase_amp = np.arctan2(np.imag(wave), np.real(wave))
        phase_amp = phase_amp[0, :]

        # fitting oscillatory phase / amplitude to actual SAT
        reconstruction = amplitude * np.cos(phase_amp)
        fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
        m, c = np.linalg.lstsq(fit_x, g_amp.data)[0]
def _cond_difference_surrogates(sg, sg_amp, a, a2, jobq, resq):
    mean, var, trend = a
    mean2, var2, trend2 = a2
    last_mid_year = first_mid_year
    cond_means_out = np.zeros((8, ))
    while jobq.get() is not None:
        if SURR_TYPE == 'MF':
            sg.construct_multifractal_surrogates()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_multifractal_surrogates()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'FT':
            sg.construct_fourier_surrogates_spatial()
            sg.add_seasonality(mean, var, trend)
            if AMPLITUDE:
                sg_amp.construct_fourier_surrogates_spatial()
                sg_amp.add_seasonality(mean2, var2, trend2)
        elif SURR_TYPE == 'AR':
            sg.construct_surrogates_with_residuals()
            sg.add_seasonality(mean[:-1, ...], var[:-1, ...], trend[:-1, ...])
            if AMPLITUDE:
                sg_amp.construct_surrogates_with_residuals()
                sg_amp.add_seasonality(mean2[:-1, ...], var2[:-1, ...],
                                       trend2[:-1, ...])
        wave, _, _, _ = wavelet_analysis.continous_wavelet(
            sg.surr_data,
            1,
            False,
            wavelet_analysis.morlet,
            dj=0,
            s0=s0,
            j1=0,
            k0=k0)  # perform wavelet
        phase = np.arctan2(np.imag(wave), np.real(wave))
        if AMPLITUDE:
            wave, _, _, _ = wavelet_analysis.continous_wavelet(
                sg_amp.surr_data,
                1,
                False,
                wavelet_analysis.morlet,
                dj=0,
                s0=s0_amp,
                j1=0,
                k0=k0)  # perform wavelet
            amplitude = np.sqrt(
                np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
            amplitude = amplitude[0, :]
            phase_amp = np.arctan2(np.imag(wave), np.real(wave))
            phase_amp = phase_amp[0, :]

            # fitting oscillatory phase / amplitude to actual SAT
            reconstruction = amplitude * np.cos(phase_amp)
            fit_x = np.vstack(
                [reconstruction,
                 np.ones(reconstruction.shape[0])]).T
            m, c = np.linalg.lstsq(fit_x, sg_amp.surr_data)[0]
            amplitude = m * amplitude + c

        if SURR_TYPE == 'AR':
            sg.surr_data = sg.surr_data[main_cut_ndx[:-1]]
            if AMPLITUDE:
                amplitude = amplitude[main_cut_ndx[:-1]]
        else:
            sg.surr_data = sg.surr_data[main_cut_ndx]
            if AMPLITUDE:
                amplitude = amplitude[main_cut_ndx]
        phase = phase[0, main_cut_ndx]
        phase_bins = get_equidistant_bins()  # equidistant bins
        cnt = 0

        difference_surr = []
        meanvar_surr = []

        start = 0
        end = WINDOW_LENGTH

        while end < sg.surr_data.shape[0]:
            cnt += 1
            surr_temp = sg.surr_data[start:end].copy()
            phase_temp = phase[start:end].copy()
            if AMPLITUDE:
                amp_temp = amplitude[start:end].copy()
            last_mid_year += 1
            for i in range(cond_means.shape[0]
                           ):  # get conditional means for current phase range
                #phase_bins = get_equiquantal_bins(phase_temp) # equiquantal bins
                ndx = ((phase_temp >= phase_bins[i]) &
                       (phase_temp <= phase_bins[i + 1]))
                if MEANS:
                    if AMPLITUDE:
                        cond_means[i] = np.mean(amp_temp[ndx])
                    else:
                        cond_means[i] = np.mean(surr_temp[ndx])
                else:
                    if AMPLITUDE:
                        cond_means[i] = np.var(amp_temp[ndx], ddof=1)
                    else:
                        cond_means[i] = np.var(surr_temp[ndx], ddof=1)
            if PHASE_ANALYSIS_YEAR == last_mid_year:
                cond_means_out = cond_means.copy()
            difference_surr.append(
                cond_means.max() -
                cond_means.min())  # append difference to list
            meanvar_surr.append(np.mean(cond_means))

            start = g.find_date_ndx(date(y1 + cnt * WINDOW_SHIFT, 7, 28))
            end = start + WINDOW_LENGTH

        resq.put((np.array(difference_surr), np.array(meanvar_surr),
                  cond_means_out))
Exemple #30
0
result = dict()

start_cut = date(1779, 1, 1)
# _, _, idx = g_amp.get_data_of_precise_length('16k', start_cut, None, False)
idx = g_amp.select_date(start_cut, date(2010, 1, 1), apply_to_data=False)
fields = [g_amp, g_amp_sata]

for i in range(len(AMP_PERIODS)):
    period = AMP_PERIODS[i] * 365.25  # frequency of interest
    s0_amp = period / fourier_factor  # get scale
    wave, _, _, _ = wavelet_analysis.continous_wavelet(
        fields[i].data,
        1,
        False,
        wavelet_analysis.morlet,
        dj=0,
        s0=s0_amp,
        j1=0,
        k0=k0)  # perform wavelet
    amplitude = np.sqrt(
        np.power(np.real(wave), 2) + np.power(np.imag(wave), 2))
    amplitude = amplitude[0, :]
    phase_amp = np.arctan2(np.imag(wave), np.real(wave))
    phase_amp = phase_amp[0, :]

    # fitting oscillatory phase / amplitude to actual SAT
    reconstruction = amplitude * np.cos(phase_amp)
    fit_x = np.vstack([reconstruction, np.ones(reconstruction.shape[0])]).T
    m, c = np.linalg.lstsq(fit_x, fields[i].data)[0]
    amplitude = m * amplitude + c
def _cmi_surrogates(a):
    aa, aa_surr, aa_seas, temp, temp_surr, temp_seas, scales = a
    aa_surr.construct_fourier_surrogates_spatial()
    aa_surr.add_seasonality(aa_seas[0], aa_seas[1], aa_seas[2])

    cmi1 = []
    cmi2 = []

    for sc in scales:
        period = sc  # frequency of interest in months
        s0 = period / fourier_factor  # get scale
        wave_temp, _, _, _ = wvlt.continous_wavelet(temp.data,
                                                    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_surr = np.arctan2(
            np.imag(wave_aa),
            np.real(wave_aa))[0, 12:-12]  # get phases from oscillatory modes

        # cmi1
        tmp = []
        for tau in range(1, 30):
            x, y, z = mi.get_time_series_condition([phase_temp, phase_aa_surr],
                                                   tau=tau,
                                                   dim_of_condition=1,
                                                   eta=1,
                                                   phase_diff=True)
            tmp.append(
                mi.cond_mutual_information(x,
                                           y,
                                           z,
                                           algorithm='EQQ2',
                                           bins=4,
                                           log2=False))
        cmi1.append(np.mean(np.array(tmp)))

        # cmi2
        tmp = []
        for tau in range(1, 30):
            x, y, z = mi.get_time_series_condition([phase_aa_surr, phase_temp],
                                                   tau=tau,
                                                   dim_of_condition=1,
                                                   eta=1,
                                                   phase_diff=True)
            tmp.append(
                mi.cond_mutual_information(x,
                                           y,
                                           z,
                                           algorithm='EQQ2',
                                           bins=4,
                                           log2=False))
        cmi2.append(np.mean(np.array(tmp)))

    cmi1 = np.array(cmi1)
    cmi2 = np.array(cmi2)

    return cmi1, cmi2