From d6b20b7657d6442a01e5b02cd0a48391f9e39f1d Mon Sep 17 00:00:00 2001 From: leon Date: Wed, 6 Mar 2024 12:48:01 +0100 Subject: [PATCH] Clean repository --- org/{ => ODR}/ODR.org | 0 org/{ => ODR}/ODR_Jab.org | 0 org/{ => ODR}/ODR_multi.org | 0 org/{ => ODR}/ODR_neuro.org | 0 org/{ => ODR}/ODR_search.org | 0 org/{ => ODR}/ODR_single.org | 0 org/gpt.org | 331 ----------------------------------- org/test.org | 230 ++++++++++++++++++++++++ 8 files changed, 230 insertions(+), 331 deletions(-) rename org/{ => ODR}/ODR.org (100%) rename org/{ => ODR}/ODR_Jab.org (100%) rename org/{ => ODR}/ODR_multi.org (100%) rename org/{ => ODR}/ODR_neuro.org (100%) rename org/{ => ODR}/ODR_search.org (100%) rename org/{ => ODR}/ODR_single.org (100%) delete mode 100644 org/gpt.org create mode 100644 org/test.org diff --git a/org/ODR.org b/org/ODR/ODR.org similarity index 100% rename from org/ODR.org rename to org/ODR/ODR.org diff --git a/org/ODR_Jab.org b/org/ODR/ODR_Jab.org similarity index 100% rename from org/ODR_Jab.org rename to org/ODR/ODR_Jab.org diff --git a/org/ODR_multi.org b/org/ODR/ODR_multi.org similarity index 100% rename from org/ODR_multi.org rename to org/ODR/ODR_multi.org diff --git a/org/ODR_neuro.org b/org/ODR/ODR_neuro.org similarity index 100% rename from org/ODR_neuro.org rename to org/ODR/ODR_neuro.org diff --git a/org/ODR_search.org b/org/ODR/ODR_search.org similarity index 100% rename from org/ODR_search.org rename to org/ODR/ODR_search.org diff --git a/org/ODR_single.org b/org/ODR/ODR_single.org similarity index 100% rename from org/ODR_single.org rename to org/ODR/ODR_single.org diff --git a/org/gpt.org b/org/gpt.org deleted file mode 100644 index 9e0fef0..0000000 --- a/org/gpt.org +++ /dev/null @@ -1,331 +0,0 @@ -*** I need to write a cpp function that generates 4 vectors of size N drawn from a multivariate gaussian distribution. - -You can use the Eigen library in C++ to generate random samples from a multivariate Gaussian distribution. Here's an example code that generates four vectors of size N: - -#+begin_src -#include -#include -#include - -using Eigen::MatrixXd; -using Eigen::VectorXd; - -void generateMultivariateGaussian(int N) { - - std::random_device rd; - std::mt19937 gen(rd()); - MatrixXd cov(N, N); - VectorXd mean(N); - - // Set the covariance matrix and mean vector - cov.setRandom(); - cov = cov.selfadjointView() * cov.selfadjointView().transpose(); - mean.setRandom(); - - // Generate four random samples from the multivariate normal distribution - std::normal_distribution<> d(0, 1); - for(int i=0; i<4; i++) { - VectorXd sample(N); - MatrixXd L = cov.llt().matrixL(); - for (int j = 0; j < N; ++j) { - sample(j) = d(gen); - } - sample = mean + L * sample; - std::cout << "Sample " << i+1 << ": " << std::endl << sample << std::endl; - } -} - -int main(){ - int N = 4; - generateMultivariateGaussian(N); - return 0; -} -#+end_src - -The =generateMultivariateGaussian= function takes an integer =N= as input and generates a random multivariate normal distribution with a randomly generated covariance matrix and mean vector. The function then generates four random samples from the distribution and prints them out. - -*** I want to set the covariance - -If you want to set the covariance matrix instead of generating a random one, you can modify the =generateMultivariateGaussian= function as follows: - -#+begin_src -void generateMultivariateGaussian(int N, MatrixXd cov) { - - std::random_device rd; - std::mt19937 gen(rd()); - VectorXd mean(N); - - // Set the mean vector - mean.setRandom(); - - // Generate four random samples from the multivariate normal distribution - std::normal_distribution<> d(0, 1); - for(int i=0; i<4; i++) { - VectorXd sample(N); - MatrixXd L = cov.llt().matrixL(); - for (int j = 0; j < N; ++j) { - sample(j) = d(gen); - } - sample = mean + L * sample; - std::cout << "Sample " << i+1 << ": " << std::endl << sample << std::endl; - } -} -#+end_src - -Here, the user is required to pass in the covariance matrix as an argument to the function. The rest of the code remains the same. - -*** Show me an example - -Sure! Here's an example code that generates four vectors of size N drawn from a multivariate Gaussian distribution with a user-specified covariance matrix: - -#+begin_src cpp -#include -#include -#include - -using Eigen::MatrixXd; -using Eigen::VectorXd; - -void generateMultivariateGaussian(int N, MatrixXd cov) { - - std::random_device rd; - std::mt19937 gen(rd()); - VectorXd mean(N); - - // Set the mean vector - mean.setRandom(); - - // Generate four random samples from the multivariate normal distribution - std::normal_distribution<> d(0, 1); - for(int i=0; i<4; i++) { - VectorXd sample(N); - MatrixXd L = cov.llt().matrixL(); - for (int j = 0; j < N; ++j) { - sample(j) = d(gen); - } - sample = mean + L / sample; - std::cout << "Sample " << i+1 << ": " << std::endl << sample << std::endl; - } -} - -int main() { - int N = 3; - - // Set the covariance matrix - MatrixXd cov(N, N); - cov << 2, 1, 0, - 1, 2, 1, - 0, 1, 2; - - // Generate four random samples from a multivariate normal distribution with the specified covariance matrix - generateMultivariateGaussian(N, cov); - - return 0; -} -#+end_src - -In this example, the user sets the covariance matrix explicitly using the =MatrixXd= class from Eigen library. The covariance matrix is then passed as an argument to the =generateMultivariateGaussian= function along with the dimensionality of the distribution. - -The code generates four random samples from a multivariate normal distribution with the user-specified covariance matrix and a randomly generated mean vector using the same method as in the previous example. - -*** Can I read cov from a config.yml - -Yes! You can read the covariance matrix from a YAML configuration file using a YAML parser library like "yaml-cpp". Here's an example code that reads the covariance matrix from a YAML file called "config.yml": - -First, you need to install the "yaml-cpp" library. If you haven't installed it yet, you can install it using the following command on Ubuntu: - -#+begin_src -sudo apt-get install libyaml-cpp-dev -#+end_src - -Then, you can use the library in your C++ code as follows: - -#+begin_src cpp -#include -#include -#include -#include - -using Eigen::MatrixXd; -using Eigen::VectorXd; - -void generateMultivariateGaussian(int N, MatrixXd cov) { - - std::random_device rd; - std::mt19937 gen(rd()); - VectorXd mean(N); - - // Set the mean vector - mean.setRandom(); - - // Generate four random samples from the multivariate normal distribution - std::normal_distribution<> d(0, 1); - for(int i=0; i<4; i++) { - VectorXd sample(N); - MatrixXd L = cov.llt().matrixL(); - for (int j = 0; j < N; ++j) { - sample(j) = d(gen); - } - sample = mean + L / sample; - std::cout << "Sample " << i+1 << ": " << std::endl << sample << std::endl; - } -} - -int main() { - int N; - - // Load the configuration file - YAML::Node config = YAML::LoadFile("config.yml"); - - // Read the dimensionality of the distribution from the configuration file - N = config["dimensionality"].as(); - - // Read the covariance matrix from the configuration file - MatrixXd cov(N, N); - for(int i=0; i(); - } - } - - // Generate four random samples from a multivariate normal distribution with the specified covariance matrix - generateMultivariateGaussian(N, cov); - - return 0; -} -#+end_src - -This code reads the dimensionality of the distribution from the "dimensionality" key in the "config.yml" file. It then reads the covariance matrix from the "covariance_matrix" key in the configuration file, which is a 1D array of length N^2 representing the elements of the covariance matrix in row-major order. - -Note that for this to work, the configuration file has to be in the following format: - -#+begin_src -dimensionality: 3 -covariance_matrix: [2, 1, 0, 1, 2, 1, 0, 1, 2] - -*** I have a set of files which contains arrays of size D x N. I want to regroup them in a single dataframe. - -You can use the Pandas library in Python to read in the arrays from the files and combine them into a single Pandas dataframe. - -Here's some example code: - -#+begin_src python -import pandas as pd -import numpy as np -import glob - -# Get a list of all files with extension ".txt" in the current directory -file_list = glob.glob("*.txt") - -# Initialize an empty list to store the arrays -array_list = [] - -# Loop over all files -for file_name in file_list: - # Load the array from the file - array = np.loadtxt(file_name) - # Append the array to the list - array_list.append(array) - -# Combine all arrays into a single dataframe -df = pd.DataFrame(np.hstack(array_list)) - -# Set the column names to "col0", "col1", etc. -df.columns = [f"col{i}" for i in range(df.shape[1])] - -# Print the resulting dataframe -print(df) -#+end_src - -This code reads in all ".txt" files in the current directory, loads the arrays from the files into a list, and then combines all the arrays into a single Pandas dataframe using =np.hstack=. Finally, it sets the column names to ="col0", "col1", etc. and prints the resulting dataframe. - -Note that this code assumes that all files in the directory have the same dimensions. If the files have different dimensions, you may need to append NaN values to the end of the shorter arrays so that they can be combined into a single dataframe. You can use the =np.nan= function to initialize NaN values. - -*** I work with emacs and orgmode. I want to had a hook so that every time I evaluate a source block in org mode -it triggers org display inline image. Also I want the blocks to be evaluated without org asking for my consent. - -You can add the following lines to your Emacs init file to enable automatic display of inline images and silent evaluation of source blocks in Org mode: - -#+begin_src emacs-lisp -(setq org-startup-with-inline-images t) ;; Display inline images on startup -(setq org-confirm-babel-evaluate nil) ;; Don't prompt for confirmation when evaluating code blocks -(add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append) ;; Display inline images after executing code blocks -#+end_src - -#+RESULTS: -| org-display-inline-images | - -With these lines, every time you evaluate a source block in Org mode, the output will be displayed inline immediately without prompting you for consent. In addition, any inline images in the output will be displayed as well. - -*** I get the following error -Debugger entered--Lisp error: (error "No org-babel-execute function for nil!") - -The error message "No org-babel-execute function for nil!" usually indicates that Org mode is trying to evaluate a source block that has no language specified. To fix this error, you need to specify the language of the source block by adding a header argument to the block. - -For example, here's a correct way to specify a source block in Python: - -#+begin_src python -print("Hello, world!") -#+end_src - -And here's an incorrect way to specify a source block with no language specified: - -#+begin_src -print("Hello, world!") -#+end_src - -To fix the error, you can specify the language as follows: - -#+begin_src unknown-language -print("Hello, world!") -#+end_src - -where "unknown-language" should be replaced with the appropriate language of the source block you are trying to evaluate. - -*** I get Floating point exception (core dumped) - - -A "Floating point exception (core dumped)" error usually indicates that your program attempted an invalid floating-point operation, such as dividing by zero, taking the square root of a negative number, or creating a NaN or infinity value. - -To fix this error, you should check your code and ensure that all floating-point operations are valid and won't cause errors. For example, you can check for division by zero, NaN or infinity values, and negative values when taking the square root. - -Here are some examples of how to check for these errors: - -To check for division by zero: - -#+begin_src -if (abs(denominator) < 1e-9) { - // Handle division by zero -} -else { - // Perform the division - result = numerator / denominator; -} -#+end_src - -To check for NaN or infinity values: - -#+begin_src -if (isnan(value) || isinf(value)) { - // Handle NaN or infinity -} -else { - // Use the value -} -#+end_src - -To check for negative values when taking the square root: - -#+begin_src -if (value < 0) { - // Handle negative value -} -else { - // Take the square root - result = sqrt(value); -} -#+end_src - -You may also want to check that your input data is valid, such as checking that the input arrays are not empty or that the dimensions of the arrays match the expected values. - -*** diff --git a/org/test.org b/org/test.org new file mode 100644 index 0000000..93903cb --- /dev/null +++ b/org/test.org @@ -0,0 +1,230 @@ +#+STARTUP: fold +#+PROPERTY: header-args:ipython :results both :exports both :async yes :session odr_search :kernel dual_data + +* Notebook Settings + +#+begin_src ipython + %load_ext autoreload + %autoreload 2 + %reload_ext autoreload + + %run /home/leon/models/lif_cpp/notebooks/setup.py + %matplotlib inline + %config InlineBackend.figure_format = 'png' +#+end_src + +#+RESULTS: +: The autoreload extension is already loaded. To reload it, use: +: %reload_ext autoreload +: Python exe +: /home/leon/mambaforge/envs/dual_data/bin/python +:END: + +* Compilation +#+begin_src sh + cd $HOME/models/lif_cpp + make clean + make +#+end_src + +* Imports + +#+begin_src ipython + import warnings + # Ignore FutureWarning + warnings.simplefilter(action='ignore', category=FutureWarning) + + import sys + sys.path.insert(0, '/home/leon/models/lif_cpp') + REPO_PATH = "/home/leon/models/lif_cpp" + + import subprocess + import pandas as pd + from time import sleep + from yaml import safe_load + from scipy.stats import circmean + from run_model import run_cpp, update_conf, restore_conf + from analysis.decode import decode_bump, circcvl + + pal = [sns.color_palette("tab10")[0], sns.color_palette("tab10")[1]] +#+end_src + +#+RESULTS: + +* Helpers + +#+begin_src ipython + from joblib import Parallel, delayed + import numpy as np + + def work(name, X_str, ini, phi, X): + try: + session = "/%s_%s_%.3f_phi_%d_ini_%d/" % (name, X_str, X, phi, ini) + rate = get_data(session) + return rate[:32000, -1] + except: + return None + + def get_rates_X_parallel(name, X_list, ini_list, phi_list, X_str='Jee', n_jobs=-1): + # Create a parallel instance using n_jobs + parallel = Parallel(n_jobs=n_jobs) + + # Use delayed to set up a list of tasks + tasks = (delayed(work)(name, X_str, ini, phi, X) for ini in ini_list for phi in phi_list for X in X_list) + + # Execute the tasks in parallel + rates_data = parallel(tasks) + + # Removing None values resulted from exceptions and build the rates array + # rates_data = list(filter(None, rates_data)) + rates = np.array(rates_data) + print(rates.shape) + rates = rates.reshape(len(ini_list), len(phi_list), len(X_list), 32000, -1) + return rates +#+end_src + +#+RESULTS: + +#+begin_src ipython + def get_data(session='/'): + # Open the file in binary mode and read the data + with open('/home/leon/models/lif_cpp/data/simul/'+ session + '/rates.txt', 'rb') as f: + data = np.fromfile(f, dtype=np.float32) + # Now 'data' is a numpy array containing your data + return data.reshape(-1, 40000).T + #+end_src + +#+RESULTS: + +#+begin_src ipython + def get_rates_X(name, X_list, ini_list, phi_list, X_str='Jee'): + rates = [] + for ini in ini_list: + for phi in phi_list: + for X in X_list: + try: + session = "/%s_%s_%.2f_phi_%d_ini_%d/" % (name, X_str, X, phi, ini) + rate = get_data(session) + rates.append(rate[:32000]) + except: + pass + + rates = np.array(rates) + rates = rates.reshape(len(ini_list), len(phi_list), len(X_list), 32000, -1) + return rates +#+end_src + +#+RESULTS: + +#+begin_src ipython + def get_df_ini_X(rates, X_list, X_str='Jee'): + n_trials, n_phi, n_X, n_neurons, n_times = rates.shape + + # Create indices + # trials_ind, phi_ind, X_ind, neurons_ind, times_ind = np.indices((n_trials, n_phi, n_X, n_neurons, n_times)) + + trials_ind, phi_ind, X_ind, neurons_ind, times_ind = np.meshgrid( + np.arange(n_trials), + np.arange(n_phi), + np.arange(n_X), + np.arange(n_neurons), + np.arange(n_times), + indexing='ij' + ) + + # Construct DataFrame + df = pd.DataFrame({ + 'trial': trials_ind.flatten(), + 'phi': phi_ind.flatten(), + X_str : X_ind.flatten() * np.round((X_list[-1] - X_list[0]) / (X_list.shape[0] -1), 5) + X_list[0], + 'neuron': neurons_ind.flatten(), + 'time': times_ind.flatten() * 0.1, + 'rates': rates.flatten() + }) + + return df +#+end_src + +#+RESULTS: + +#+begin_src ipython + def load_data(name, X_list, ini_list, phi_list, X_str='Jee'): + rates = get_rates_X_parallel(name, X_list, ini_list, phi_list, X_str) + print(rates.shape) + df = get_df_ini_X(rates, X_list, X_str) + return df +#+end_src + +#+RESULTS: + +#+begin_src ipython + def get_df_code(df, X_str='Jee'): + df_code = df.groupby(['time', 'trial', 'phi', X_str])['rates'].apply(decode_bump).reset_index() + df_code[['m0', 'm1', 'phase']] = pd.DataFrame(df_code['rates'].tolist(), index=df_code.index) + df_code = df_code.drop(columns=['rates']) + + end_point = df_code[df_code.time==df_code.time.iloc[-1]] + end_point = end_point.drop(columns=['time']) + print(end_point.head()) + return df_code, end_point +#+end_src + +#+RESULTS: + +#+begin_src ipython + def run_loop_X(conf_name, name, X_str, X_list, ini_list, phi_list, axis=0): + restore_conf(REPO_PATH + '/conf/' + conf_name + '.yml', + REPO_PATH + '/conf/' + conf_name + '.yml.bak') + + conf_path = REPO_PATH + '/conf/'+ conf_name + '.yml' + + for ini in ini_list: + for phi in phi_list: + for X in X_list: + session = "%s_%s_%.3f_phi_%d_ini_%d" % (name, X_str, X, phi, ini) + data_path = REPO_PATH + '/data/simul/' + session + update_conf(REPO_PATH + '/conf/'+ conf_name, 'DATA_PATH', data_path) + update_conf(REPO_PATH + '/conf/' + conf_name, X_str, float(X), axis=axis) + update_conf(REPO_PATH + '/conf/' + conf_name, 'PHI_STIM', float(phi), axis=0) + update_conf(REPO_PATH + '/conf/' + conf_name, 'IF_REC_SPIKE', 0) + update_conf(REPO_PATH + '/conf/' + conf_name, 'T_SAVE', 1000) + + sleep(.1) + run_cpp(session, conf_path=conf_path) + sleep(.1) + + subprocess.run([REPO_PATH + '/src/mem_usage.sh']) + subprocess.run([REPO_PATH + '/src/cpu_usage.sh']) + + restore_conf(REPO_PATH + '/conf/' + conf_name + '.yml.bak', + REPO_PATH + '/conf/' + conf_name + '.yml') +#+end_src + +#+RESULTS: + +#+begin_src ipython + def get_precision(x): + + cmean = (x - circmean(x, low=-np.pi, high=np.pi)) + + cmean[cmean > np.pi] -= 2*np.pi + cmean[cmean < -np.pi] += 2*np.pi + + return cmean +#+end_src + +#+RESULTS: + +#+begin_src ipython + def plot_smooth(smooth, ax, color): + means = smooth.mean(axis=0) + ci = smooth.std(axis=0, ddof=1) * 1.96 + + # Plot + ax.plot(means) + ax.fill_between(range(smooth.shape[1]), means - ci, means + ci, alpha=0.25) + +#+end_src + +#+RESULTS: +