Exemplo n.º 1
0
def run_workflow(conn_name, connectome, path_res_conn, path_io, path_res_sim, path_res_tsk, \
                 bin=False, input_nodes=None, output_nodes=None, readout_modules=None, \
                 scores_file=None, iter_id=None, iter_conn=True, iter_io=False, iter_sim=False, \
                 encode=True, decode=True, **kwargs):
    """
        Runs the full reservoir pipeline: loads and scales connectivity matrix,
        generates input/output data for the task, simulates reservoir states,
        and trains the readout module.

        Parameters
        ----------
        conn_name: str, {'consensus', 'rand_mio'}
            Specifies the name of the connectivity matrix file. 'consensus' for
            reliability and spintest analyses, and 'rand_mio' for significance
            analysis.

        connectome: str, {human_500, human_250}
            Specifies the scale of the conenctome

        path_res_conn : str
            Path to conenctivity matrix

        path_io : str
            Path to simulation results

        path_res_sim : str
            Path to simulation results

        path_res_tsk : str
            Path to task scores

        bin : bool
            If True, the binary matrix will be used

        input,output nodes: (N,) list or numpy.darray
            List or array that indicates the indexes of the input and output
            nodes in the recurrent network.
            N: number of input,output nodes in the network

        readout_modules: (N, ) numpy.darray
            Array that indicates the module at which each output node belongs
            to. Modules can be int or str.
            N: number of output nodes

        scores_file : str, {'functional', 'cytoarch'}
            Name of the partition used

        iter_id : int
            Number/name of the iteration

        iter_{conn,io,sim} : bool
            If True, specific instances (i.e., connectivity, input/output data,
            network states) related to the iteration indicated by iter_id will
            be used.

        encode,decode : bool
            If True, encoding,decoding will run
    """

    # --------------------------------------------------------------------------------------------------------------------
    # DEFINE FILE NAMES
    # ----------------------------------------------------------------------------------------------------------------------

    # define file connectivity data
    if np.logical_and(iter_id is not None, iter_conn):
        conn_file = conn_name + '_' + str(iter_id) + '.npy'
    else:
        conn_file = conn_name + '.npy'

    # define file I/O data
    if np.logical_and(iter_id is not None, iter_io):
        input_file = 'inputs_' + str(iter_id) + '.npy'
        output_file = 'outputs_' + str(iter_id) + '.npy'
    else:
        input_file = 'inputs.npy'
        output_file = 'outputs.npy'

    # define file simulation data (reservoir states)
    if np.logical_and(iter_id is not None, iter_sim):
        res_states_file = 'reservoir_states_' + str(iter_id) + '.npy'
    else:
        res_states_file = 'reservoir_states.npy'

    # define file encoding/decoding scores data
    if np.logical_and(iter_id is not None, scores_file is not None):
        encoding_file = scores_file + '_encoding_score_' + str(
            iter_id) + '.csv'
        decoding_file = scores_file + '_decoding_score_' + str(
            iter_id) + '.csv'

    elif np.logical_and(iter_id is not None, scores_file is None):
        encoding_file = 'encoding_score_' + str(iter_id) + '.csv'
        decoding_file = 'decoding_score_' + str(iter_id) + '.csv'

    elif np.logical_and(iter_id is None, scores_file is not None):
        encoding_file = scores_file + '_encoding_score.csv'
        decoding_file = scores_file + '_decoding_score.csv'

    else:
        encoding_file = 'encoding_score.csv'
        decoding_file = 'decoding_score.csv'

    # --------------------------------------------------------------------------------------------------------------------
    # IMPORT CONNECTIVITY DATA
    # ----------------------------------------------------------------------------------------------------------------------

    # load connectivity data
    conn = np.load(os.path.join(path_res_conn, conn_file))
    ctx = np.load(
        os.path.join(DATA_DIR, 'cortical', 'cortical_' + connectome + '.npy'))

    # scale weights [0,1]
    if bin: conn = conn.astype(bool).astype(int)
    else: conn = (conn - conn.min()) / (conn.max() - conn.min())

    # normalize by the spectral radius
    ew, _ = eigh(conn)
    conn = conn / np.max(ew)
    n_nodes = len(conn)

    # select input nodes
    if input_nodes is None: input_nodes = np.where(ctx == 0)[0]
    if output_nodes is None: output_nodes = np.where(ctx == 1)[0]

    # --------------------------------------------------------------------------------------------------------------------
    # CREATE I/O DATA FOR TASK
    # ----------------------------------------------------------------------------------------------------------------------
    if not os.path.exists(os.path.join(path_io, input_file)):

        io_kwargs = {'time_len': 2050}

        inputs, outputs = io.get_io_data(task=TASK,
                                         task_ref=TASK_REF,
                                         **io_kwargs)

        np.save(os.path.join(path_io, input_file), inputs)
        np.save(os.path.join(path_io, output_file), outputs)

    # --------------------------------------------------------------------------------------------------------------------
    # NETWORK SIMULATION - LINEAR MODEL
    # ----------------------------------------------------------------------------------------------------------------------
    alphas = tasks.get_default_alpha_values(SPEC_TASK)
    if not os.path.exists(os.path.join(path_res_sim, res_states_file)):

        input_train, input_test = np.load(os.path.join(path_io, input_file))

        # create input connectivity matrix - depends on the shape of the input
        w_in = np.zeros((input_train.shape[1], len(conn)))
        w_in[input_nodes, :] = FACTOR

        reservoir_states_train = sim_lnm.run_sim(
            w_in=w_in,
            w=conn,
            inputs=input_train,
            alphas=alphas,
        )

        reservoir_states_test = sim_lnm.run_sim(
            w_in=w_in,
            w=conn,
            inputs=input_test,
            alphas=alphas,
        )

        reservoir_states = [(rs_train, rs_test) for rs_train, rs_test in zip(
            reservoir_states_train, reservoir_states_test)]
        np.save(os.path.join(path_res_sim, res_states_file),
                reservoir_states,
                allow_pickle=False)

    # --------------------------------------------------------------------------------------------------------------------
    # IMPORT I/O DATA FOR TASK
    # ----------------------------------------------------------------------------------------------------------------------
    reservoir_states = np.load(os.path.join(path_res_sim, res_states_file),
                               allow_pickle=True)
    reservoir_states = reservoir_states[:, :, :, output_nodes]
    reservoir_states = reservoir_states.squeeze()
    reservoir_states = np.split(reservoir_states,
                                len(reservoir_states),
                                axis=0)
    reservoir_states = [rs.squeeze() for rs in reservoir_states]

    outputs = np.load(os.path.join(path_io, output_file))

    # --------------------------------------------------------------------------------------------------------------------
    # PERFORM TASK - ENCODERS
    # ----------------------------------------------------------------------------------------------------------------------
    try:
        if np.logical_and(
                encode,
                not os.path.exists(os.path.join(path_res_tsk, encoding_file))):

            print('\nEncoding: ')
            df_encoding = coding.encoder(
                task=SPEC_TASK,
                target=outputs,
                reservoir_states=reservoir_states,
                readout_modules=readout_modules,
                alphas=alphas,
            )

            df_encoding = df_encoding.rename(columns={'module': 'class'},
                                             copy=False)
            df_encoding.to_csv(os.path.join(path_res_tsk, encoding_file))
    except:
        pass

    # --------------------------------------------------------------------------------------------------------------------
    # PERFORM TASK - DECODERS
    # ----------------------------------------------------------------------------------------------------------------------
    try:
        if np.logical_and(
                decode,
                not os.path.exists(os.path.join(path_res_tsk, decoding_file))):

            # binarize cortical adjacency matrix
            conn_bin = conn.copy()[np.ix_(
                np.where(ctx == 1)[0],
                np.where(ctx == 1)[0])].astype(bool).astype(int)

            print('\nDecoding: ')
            df_decoding = coding.decoder(
                task=SPEC_TASK,
                target=outputs,
                reservoir_states=reservoir_states,
                readout_modules=readout_modules,
                bin_conn=conn_bin,
                alphas=alphas,
            )

            df_decoding = df_decoding.rename(columns={'module': 'class'},
                                             copy=False)
            df_decoding.to_csv(os.path.join(path_res_tsk, decoding_file))

    except:
        pass

    # delete reservoir states to release memory storage
    if iter_sim: os.remove(os.path.join(path_res_sim, res_states_file))
def run_workflow(conn_name, connectome, path_res_conn, path_io, path_res_sim, path_res_tsk, \
                 bin=False, input_nodes=None, output_nodes=None, class_labels=None, class_mapp=None, \
                 scores_file=None, iter_id=None, iter_conn=True, iter_io=False, iter_sim=False, \
                 encode=True, decode=True, **kwargs):

    # --------------------------------------------------------------------------------------------------------------------
    # DEFINE FILE NAMES
    # ----------------------------------------------------------------------------------------------------------------------

    # define file connectivity data
    if np.logical_and(iter_id is not None, iter_conn):
        conn_file = conn_name + '_' + str(iter_id) + '.npy'
    else:
        conn_file = conn_name + '.npy'

    # define file I/O data
    if np.logical_and(iter_id is not None, iter_io):
        input_file = 'inputs_' + str(iter_id) + '.npy'
        output_file = 'outputs_' + str(iter_id) + '.npy'
    else:
        input_file = 'inputs.npy'
        output_file = 'outputs.npy'

    # define file simulation data (reservoir states)
    if np.logical_and(iter_id is not None, iter_sim):
        res_states_file = 'reservoir_states_' + str(iter_id) + '.npy'
    else:
        res_states_file = 'reservoir_states.npy'

    # define file encoding/decoding scores data
    if np.logical_and(iter_id is not None, scores_file is not None):
        encoding_file = scores_file + '_encoding_score_' + str(
            iter_id) + '.csv'
        decoding_file = scores_file + '_decoding_score_' + str(
            iter_id) + '.csv'

    elif np.logical_and(iter_id is not None, scores_file is None):
        encoding_file = 'encoding_score_' + str(iter_id) + '.csv'
        decoding_file = 'decoding_score_' + str(iter_id) + '.csv'

    elif np.logical_and(iter_id is None, scores_file is not None):
        encoding_file = scores_file + '_encoding_score.csv'
        decoding_file = scores_file + '_decoding_score.csv'

    else:
        encoding_file = 'encoding_score.csv'
        decoding_file = 'decoding_score.csv'

    # --------------------------------------------------------------------------------------------------------------------
    # IMPORT CONNECTIVITY DATA
    # ----------------------------------------------------------------------------------------------------------------------

    # load connectivity data
    conn = np.load(os.path.join(path_res_conn, conn_file))
    ctx = np.load(
        os.path.join(DATA_DIR, 'cortical', 'cortical_' + connectome + '.npy'))
    # conn = conn[np.ix_(np.where(ctx == 1)[0], np.where(ctx == 1)[0])]

    # scale weights [0,1]
    if bin: conn = conn.astype(bool).astype(int)
    else: conn = (conn - conn.min()) / (conn.max() - conn.min())

    # normalize by the spectral radius
    ew, _ = eigh(conn)
    conn = conn / np.max(ew)
    n_nodes = len(conn)

    # select input nodes
    if input_nodes is None: input_nodes = np.where(ctx == 0)[0]

    # --------------------------------------------------------------------------------------------------------------------
    # CREATE I/O DATA FOR TASK
    # ----------------------------------------------------------------------------------------------------------------------
    if not os.path.exists(os.path.join(path_io, input_file)):

        io_kwargs = {
            'time_len': 2050,
            'step_len': 20,
            'bias': 0.5,
            'n_repeats': 3
        }

        inputs, outputs = io.get_io_data(task=TASK,
                                         task_ref=TASK_REF,
                                         n_nodes=n_nodes,
                                         input_nodes=input_nodes,
                                         **io_kwargs)

        np.save(os.path.join(path_io, input_file), inputs)
        np.save(os.path.join(path_io, output_file), outputs)

    # --------------------------------------------------------------------------------------------------------------------
    # NETWORK SIMULATION - LINEAR MODEL
    # ----------------------------------------------------------------------------------------------------------------------
    if not os.path.exists(os.path.join(path_res_sim, res_states_file)):

        input_train, input_test = np.load(os.path.join(path_io, input_file))

        reservoir_states_train = sim_lnm.run_sim(
            conn=conn,
            input_nodes=input_nodes,
            inputs=input_train,
            factor=FACTOR,
            task=SPEC_TASK,
        )

        reservoir_states_test = sim_lnm.run_sim(
            conn=conn,
            input_nodes=input_nodes,
            inputs=input_test,
            factor=FACTOR,
            task=SPEC_TASK,
        )

        reservoir_states = [(rs_train, rs_test) for rs_train, rs_test in zip(
            reservoir_states_train, reservoir_states_test)]
        np.save(os.path.join(path_res_sim, res_states_file),
                reservoir_states,
                allow_pickle=False)

    # --------------------------------------------------------------------------------------------------------------------
    # IMPORT I/O DATA FOR TASK
    # ----------------------------------------------------------------------------------------------------------------------
    reservoir_states = np.load(os.path.join(path_res_sim, res_states_file),
                               allow_pickle=True)
    reservoir_states = reservoir_states[:, :, :, np.where(ctx == 1)[0]]
    reservoir_states = reservoir_states.squeeze()
    reservoir_states = np.split(reservoir_states,
                                len(reservoir_states),
                                axis=0)
    reservoir_states = [rs.squeeze() for rs in reservoir_states]

    outputs = np.load(os.path.join(path_io, output_file))

    # --------------------------------------------------------------------------------------------------------------------
    # PERFORM TASK - ENCODERS
    # ----------------------------------------------------------------------------------------------------------------------
    # try:
    if np.logical_and(
            encode,
            not os.path.exists(os.path.join(path_res_tsk, encoding_file))):

        print('\nEncoding: ')
        df_encoding = coding.encoder(task=SPEC_TASK,
                                     target=outputs.copy(),
                                     reservoir_states=reservoir_states.copy(),
                                     output_nodes=output_nodes,
                                     class_labels=class_labels,
                                     class_mapp=class_mapp,
                                     **kwargs_pttn_recog)

        df_encoding.to_csv(os.path.join(path_res_tsk, encoding_file))
    # except:
    #     pass

    # --------------------------------------------------------------------------------------------------------------------
    # PERFORM TASK - DECODERS
    # ----------------------------------------------------------------------------------------------------------------------
    try:
        if np.logical_and(
                decode,
                not os.path.exists(os.path.join(path_res_tsk, decoding_file))):

            # binarize cortical adjacency matrix
            conn_bin = conn.copy()[np.ix_(
                np.where(ctx == 1)[0],
                np.where(ctx == 1)[0])].astype(bool).astype(int)

            print('\nDecoding: ')
            df_decoding = coding.decoder(
                task=SPEC_TASK,
                target=outputs.copy(),
                reservoir_states=reservoir_states.copy(),
                output_nodes=output_nodes,
                class_labels=class_labels,
                class_mapp=class_mapp,
                bin_conn=conn_bin,
                **kwargs_pttn_recog)

            df_decoding.to_csv(os.path.join(path_res_tsk, decoding_file))
    except:
        pass

    # delete reservoir states to release memory storage
    if iter_sim: os.remove(os.path.join(path_res_sim, res_states_file))