def Lorenz96_SDEmodel(Dim=1,
                      n_samp=1,
                      t_max=1000,
                      poly_order=3,
                      basetag='',
                      SavePath='./results/'):
    """Gives a generative model of a state variable of Lorenz using optimal transport.
    

    Args:
        Dim: number of state variables to be modelled 
        n_samp: sampling interval, original dt=0.1
        t_max: the length of time series used for training, max possible is 10000
        poly_order: order of polynomial used in transport

    Returns:
        saves data from the generative model

    """

    Tag = basetag + 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str(
        n_samp)

    if not os.path.exists(SavePath):
        print('create path ...')
        os.makedirs(SavePath)

    # We have already computed the time-series
    # here we only load and subsample the data

    LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat')
    x0, t0 = LorenzData['y'], LorenzData['t']
    dt = t0[1] - t0[0]
    n_max = int(t_max / dt) + 1
    x0 = x0[:n_max:n_samp, 0:Dim]
    t0 = t0[:n_max:n_samp]

    T = tm.compute_transport_map(x0, polynomial_order=poly_order)

    TMCoeffs(T, polynomial_order=poly_order
             )  # extract and plot the coefficients (just for display)

    q0 = T(x0)
    Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt)

    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=0.1)
    x_model = tm.TMinverse(q_model, T)  # trnasform back to the y-space

    sio.savemat(
        SavePath + 'LorenzModel_' + Tag[:], {
            'x_model': x_model,
            'x_train': x0,
            't_model': t_model,
            'q': q0,
            'q_model': q_model,
            't_train': t0
        })
def Lorenz96_SDEmodel():
    """Models a state variable of Lorenz using optimal transport.
    
    The code
    1- loads the data,
    2- computes the transport map to standard normal distiburion,
    3- identifies linear stochastic oscillators with same spectra
    4- computes some data from oscillators and pull them back under the transport map.
    """

    Dim = 1
    n_samp = 1  # sampling interval (dt=0.1)
    t_max = 1000
    poly_order = 3  # degree of polynomial used for transport map

    Tag = 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str(n_samp)
    SavePath = './results/'
    if not os.path.exists(SavePath):
        print('create path ...')
        os.makedirs(SavePath)

    # We have already computed the time-series
    # here we only load and subsample the data

    LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat')
    x0, t0 = LorenzData['y'], LorenzData['t']
    dt = t0[1] - t0[0]
    n_max = int(t_max / dt) + 1
    x0 = x0[:n_max:n_samp, 0:Dim]
    t0 = t0[:n_max:n_samp]

    T = tm.compute_transport_map(x0, polynomial_order=poly_order)

    TMCoeffs(T, polynomial_order=poly_order
             )  # extract and plot the coefficients (just for display)

    q0 = T(x0)
    Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt)

    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=0.1)
    x_model = tm.TMinverse(q_model, T)  # trnasform back to the y-space

    sio.savemat(
        SavePath + 'LorenzModel_' + Tag[:], {
            'x_model': x_model,
            'x_train': x0,
            't_model': t_model,
            'q': q0,
            'q_model': q_model,
            't_train': t0
        })
def Lorenz96_SDEmodel():
    # modeling Lorenz 96 model as a SDE

    Dim = 1  # how many state variables we use as output
    n_samp = 1  # sampling interval (dt=0.1)
    t_max = 1000  # length of data
    poly_order = 3  # degree of polynomial used for transport map

    # create path for figures
    Tag = 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str(n_samp)
    SavePath = './results/'
    if not os.path.exists(SavePath):
        print('create path ...')
        os.makedirs(SavePath)

    # load and subsample the data
    LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat')
    x0, t0 = LorenzData['y'], LorenzData['t']
    dt = t0[1] - t0[0]
    n_max = int(t_max / dt) + 1
    x0 = x0[:n_max:n_samp, 0:Dim]
    t0 = t0[:n_max:n_samp]

    # compute the transport map
    T = tm.compute_transport_map(x0, polynomial_order=poly_order)

    # extract the coefficients (just for display)
    TMCoeffs(T, polynomial_order=poly_order)

    # transform the data and find the SDEs
    q0 = T(x0)
    Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt)

    # generate trajectory of those SDEs
    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=0.1)

    # trnasform back to the y-space
    x_model = tm.TMinverse(q_model, T)

    # save data
    sio.savemat(
        SavePath + 'LorenzModel_' + Tag[:], {
            'x_model': x_model,
            'x_train': x0,
            't_model': t_model,
            'q': q0,
            'q_model': q_model,
            't_train': t0
        })
Esempio n. 4
0
def ExtrapolateTails(idx_target=0):
    """
    inputs:     idx_target --->  0:U-velocity, 1:V-velocity and 2:Temperature

    the data structure for NorthPole_data.mat:
    y(:,1:3) the time series of respectively U, V and T wavelet coefficients on top of NorthPole (target variables)
    y(:,4:30) the time series of covariates most correlated with U at North Pole
    y(:,58:84) the time series of covariates most correlated with T at North Pole
    npc=27 is the number of covariates for each target variable
    yp is the periodic part of y
    yc is the chaotic part of y
    y=yp+yc
    
    """
    years_train = 2  # how many years of training data
    start_year = 0  # when does the training data start
    polynomial_order = 2  # order of Polynomial Tranport Map

    Tag = 'NorthPole_var' + str(idx_target) + '_r' + str(
        polynomial_order) + '_tyrs' + str(years_train) + '_syrs' + str(
            start_year)
    print('simulation tag: ' + Tag)
    num_core = 20  # cores for parallel computing of inverse maps
    ntrials = 5  # max number of covariates used
    nsamp = int(74 * 365 * 4)  # number of samples for extrapolation

    # data matters
    TimeSeriesData = sio.loadmat('./thehood/NorthPole_data.mat')
    Y, n_covar = TimeSeriesData['yc'], TimeSeriesData['npc'][0, 0] - 1
    idx_train = int(years_train * 365 * 4)
    idx_start = int(start_year * 365 * 4)
    Y_train = Y[idx_start:idx_start + idx_train, :]

    # variable matters
    idx_help = 3 + np.arange(int(idx_target * n_covar),
                             int((idx_target + 1) * n_covar))
    yn_train = Y_train[:, idx_target]
    yn_truth = Y[:, idx_target]

    SavePath = './results/'
    if not os.path.exists(SavePath):
        print('create path ...')
        os.makedirs(SavePath)

    # now choose a set of variables and do extrapolation
    yns_model = np.zeros((nsamp, ntrials))

    for dim in range(0, ntrials):
        print(20 * '=')
        print('coupling with ' + str(dim) + ' variables')
        y = np.concatenate(
            (Y_train[:, idx_target:idx_target + 1], Y_train[:,
                                                            idx_help[0:dim]]),
            axis=1)
        y = np.fliplr(y)  # bc we want the coupling to improve y1

        # compute the transportmap
        MPIsetup = None
        # if dim>3:  # if MPI of transportmaps is installed
        #     MPItest=[2,2,4,4]
        S = tm.compute_transport_map(y,
                                     polynomial_order=polynomial_order,
                                     MPIsetup=MPIsetup)

        # generate a big sample
        q = tm.Generate_SND_sample(y.shape[1], n=nsamp)

        y_model = tm.TMinverse(q, S, num_core=num_core)
        yn_model = y_model[:, -1]

        # save ymodel to a larger data-set
        yns_model[:, dim] = yn_model

        # we do the saving in each loop bc TM sometimes diverges
        sio.savemat(
            './results/' + Tag, {
                'y_truth': yn_truth,
                'y_train': yn_train,
                'ys_model': yns_model,
                'n_completed': dim
            })
Esempio n. 5
0
def Cavity_SDEmodeling():
    Dim = 10
    n_samp, t_max = 10, 2500
    poly_order = 2

    # data tag
    Tag = 'production_T' + str(t_max) + '/'
    print('data tag= ' + Tag)
    print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' +
          str(t_max))

    ## save path
    SavePath = './results/'
    if not os.path.exists(SavePath):
        os.makedirs(SavePath)

    # loading the data
    CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat')
    SPOD_coords, t_coords = CavityData['y'], CavityData['t']
    SPOD_coords = np.real(SPOD_coords[0:Dim, :])
    SPOD_coords = np.swapaxes(SPOD_coords, 0, 1)
    n_max = int(t_max / (t_coords[1] - t_coords[0]))

    y_train = SPOD_coords[:n_max:n_samp, :]  # training data
    t_train = t_coords[:n_max:n_samp]
    dt = t_train[1] - t_train[0]
    print('train data size=' + str(y_train.shape))

    # form the map and transform data
    T = tm.compute_transport_map(y_train,
                                 polynomial_order=poly_order,
                                 MPIsetup=None)
    q_train = T(y_train)  # training data for q

    # # save the transport map
    # file_h = open(SavePath+'CTM.dll', 'wb')
    # dill.dump(T,file_h)
    # file_h.close()

    # model the q dynamics
    Oscillator_Params = sm.SystemID_spec_match(q_train, dt=dt)

    # generate trajectory of those SDEs
    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=dt)

    # # model data
    # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params)

    # compute inverse
    print('computing the exact inverse ...')
    y_model = tm.TMinverse(q_model, T, num_core=20)

    # save model data for MATLAB
    sio.savemat(
        SavePath + 'Cavity_modal_' + Tag[:-1], {
            'y_model': y_model,
            'y_train': y_train,
            't_model': t_model,
            't_train': t_train,
            'q_model': q_model,
            'q_train': q_train
        })

    # compute the poitwsie stat in truth and model
    PointwiseStats4cavity(y_train, y_model, SPOD_coords, Tag)

    return Tag[:-1]
def Cavity_SDEmodeling():
    """Models a state variable of Lorenz using optimal transport.
    
    The code
    1- loads the SPOD mode and coordiante data,
    2- computes the transport map to standard normal distiburion,
    3- identifies linear stochastic oscillators with same spectra
    4- computes a trajectory from oscillators and pull them back under the transport map.
    5- computes pointwise stats for cavity
    """

    Dim = 10
    n_samp, t_max = 10, 2500
    poly_order = 2

    # data tag
    Tag = 'production_T' + str(t_max) + '/'
    print('data tag= ' + Tag)
    print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' +
          str(t_max))

    ## save path
    SavePath = './results/'
    if not os.path.exists(SavePath):
        os.makedirs(SavePath)

    # We have already computed the SPOD of cavity
    # here we only load and subsample the data

    CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat')
    SPOD_coords, t_coords = CavityData['y'], CavityData['t']
    SPOD_coords = np.real(SPOD_coords[0:Dim, :])
    SPOD_coords = np.swapaxes(SPOD_coords, 0, 1)
    n_max = int(t_max / (t_coords[1] - t_coords[0]))

    y_train = SPOD_coords[:n_max:n_samp, :]  # training data
    t_train = t_coords[:n_max:n_samp]
    dt = t_train[1] - t_train[0]
    print('train data size=' + str(y_train.shape))

    # form the map and transform data
    T = tm.compute_transport_map(y_train,
                                 polynomial_order=poly_order,
                                 MPIsetup=None)
    q_train = T(y_train)  # training data for q

    # # save the transport map
    # file_h = open(SavePath+'CTM.dll', 'wb')
    # dill.dump(T,file_h)
    # file_h.close()

    Oscillator_Params = sm.SystemID_spec_match(q_train,
                                               dt=dt)  # model the q dynamics

    # generate trajectory of those SDEs
    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=dt)

    # # model data
    # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params)

    # compute inverse
    print('computing the exact inverse ...')
    y_model = tm.TMinverse(q_model, T, num_core=20)

    # save model data for MATLAB
    sio.savemat(
        SavePath + 'Cavity_modal_' + Tag[:-1], {
            'y_model': y_model,
            'y_train': y_train,
            't_model': t_model,
            't_train': t_train,
            'q_model': q_model,
            'q_train': q_train
        })

    # compute the poitwsie stat in truth and model
    PointwiseStats4cavity(y_train, y_model, SPOD_coords, Tag)

    return Tag[:-1]
Esempio n. 7
0
def Cavity_SDEmodeling(n_samp: int = 10,
                       t_max: float = 2500,
                       poly_order: int = 2,
                       Dim=10,
                       basetag='production',
                       SavePath='./results/'):
    """Models a cavity SPOD dynamics using optimal transport and spectral matching.

    Args:
        n_samp: sampling rate of time series, original dt =.01
        t_max: length of time series, max is 10000
        poly_order: order of polynomial used in the transport
        Dim: how many SPOD coordiantes are modeled 
        basetag: tag describing the putpose, e.g. production, analysis, etc.
        SavePath: folder path for saving the model from data


    Returns:
        the saved-data tag
    """

    # data tag
    Tag = basetag + '_T' + str(t_max) + '_Dim=' + str(Dim) + '_ns' + str(
        n_samp) + '_po' + str(poly_order)
    print('data tag= ' + Tag)
    print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' +
          str(t_max))

    ## save path
    if not os.path.exists(SavePath):
        os.makedirs(SavePath)

    # We have already computed the SPOD of cavity
    # here we only load and subsample the data
    CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat')
    SPOD_coords, t_coords = CavityData['y'], CavityData['t']
    SPOD_coords = np.real(SPOD_coords[0:Dim, :])
    SPOD_coords = np.swapaxes(SPOD_coords, 0, 1)
    n_max = int(t_max / (t_coords[1] - t_coords[0]))

    y_train = SPOD_coords[:n_max:n_samp, :]  # training data
    t_train = t_coords[:n_max:n_samp]
    dt = t_train[1] - t_train[0]
    print('training data size=' + str(y_train.shape))

    # form the map and transform data
    T = tm.compute_transport_map(y_train,
                                 polynomial_order=poly_order,
                                 MPIsetup=None)
    q_train = T(y_train)  # training data for q

    # # save the transport map
    # file_h = open(SavePath+'CTM.dll', 'wb')
    # dill.dump(T,file_h)
    # file_h.close()

    # model the q dynamics
    Oscillator_Params = sm.SystemID_spec_match(q_train, dt=dt)

    # generate trajectory of those SDEs
    t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params,
                                                 T=10000,
                                                 dt=dt)

    # # model data
    # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params)

    # compute inverse
    print('computing the exact inverse ...')
    y_model = tm.TMinverse(q_model, T, num_core=20)

    # save model data for MATLAB
    sio.savemat(
        SavePath + 'Cavity_modal_' + Tag, {
            'y_model': y_model,
            'y_train': y_train,
            't_model': t_model,
            't_train': t_train,
            'q_model': q_model,
            'q_train': q_train
        })

    # compute the poitwsie stat in truth and model
    PointwiseStats4cavity(y_train,
                          y_model,
                          SPOD_coords,
                          Tag,
                          SavePath=SavePath)

    return Tag