### Install documentation dependencies Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Installs dependencies required for building the documentation. ```shell uv pip install --group=docs --editable . ``` -------------------------------- ### Installation Source: https://github.com/raphaelvallat/antropy/blob/master/README.rst Instructions for installing AntroPy using pip, uv, and conda. ```shell # pip pip install antropy # uv uv pip install antropy # conda conda install -c conda-forge antropy ``` -------------------------------- ### Install in editable mode with test dependencies Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Installs the project in editable mode with test dependencies using uv. ```shell git clone https://github.com//antropy.git cd antropy uv pip install --group=test --editable . ``` -------------------------------- ### Development Installation Source: https://github.com/raphaelvallat/antropy/blob/master/README.rst Instructions for cloning the repository and installing AntroPy in editable mode for development. ```shell git clone https://github.com/raphaelvallat/antropy.git cd antropy uv pip install --group=test --editable . pytest --verbose ``` -------------------------------- ### Entropy Measures Quick Start Source: https://github.com/raphaelvallat/antropy/blob/master/docs/index.md Example Python code demonstrating the usage of various entropy measures from the AntroPy library, including permutation entropy, spectral entropy, SVD entropy, approximate entropy, sample entropy, Hjorth parameters, zero-crossing count, and Lempel-Ziv complexity. The output of each function call is also provided. ```python import numpy as np import antropy as ant np.random.seed(1234567) x = np.random.normal(size=3000) print(ant.perm_entropy(x, normalize=True)) print(ant.spectral_entropy(x, sf=100, method='welch', normalize=True)) print(ant.svd_entropy(x, normalize=True)) print(ant.app_entropy(x)) print(ant.sample_entropy(x)) print(ant.hjorth_params(x)) # mobility in samples⁻¹ print(ant.hjorth_params(x, sf=100)) # mobility in Hz print(ant.num_zerocross(x)) print(ant.lziv_complexity('01111000011001', normalize=True)) ``` ```default 0.9995 # perm_entropy (0 = regular, 1 = random) 0.9941 # spectral_entropy (0 = pure tone, 1 = white noise) 0.9999 # svd_entropy 2.0152 # app_entropy 2.1986 # sample_entropy (1.4313, 1.2153) # hjorth (mobility, complexity) (143.1339, 1.2153) # hjorth with sf=100 Hz 1531 # num_zerocross 1.3598 # lziv_complexity (normalized) ``` -------------------------------- ### Entropy Measures Example Source: https://github.com/raphaelvallat/antropy/blob/master/README.rst Example usage of various entropy measures in AntroPy. ```python import numpy as np import antropy as ant np.random.seed(1234567) x = np.random.normal(size=3000) print(ant.perm_entropy(x, normalize=True)) print(ant.spectral_entropy(x, sf=100, method='welch', normalize=True)) print(ant.svd_entropy(x, normalize=True)) print(ant.app_entropy(x)) print(ant.sample_entropy(x)) print(ant.hjorth_params(x)) # mobility in samples⁻¹ print(ant.hjorth_params(x, sf=100)) # mobility in Hz print(ant.num_zerocross(x)) print(ant.lziv_complexity('01111000011001', normalize=True)) ``` -------------------------------- ### Fractal Dimension Example Source: https://github.com/raphaelvallat/antropy/blob/master/README.rst Example usage of fractal dimension measures in AntroPy. ```python print(ant.petrosian_fd(x)) print(ant.katz_fd(x)) print(ant.higuchi_fd(x)) print(ant.detrended_fluctuation(x)) ``` -------------------------------- ### Fractal dimension Source: https://github.com/raphaelvallat/antropy/blob/master/docs/index.md Examples of calculating fractal dimensions using different methods. ```python print(ant.petrosian_fd(x)) print(ant.katz_fd(x)) print(ant.higuchi_fd(x)) print(ant.detrended_fluctuation(x)) ``` ```default 1.0311 # petrosian_fd 5.9543 # katz_fd 2.0037 # higuchi_fd (≈ 2 for white noise) 0.4790 # DFA alpha (≈ 0.5 for white noise) ``` -------------------------------- ### N-D arrays Source: https://github.com/raphaelvallat/antropy/blob/master/README.rst Example of using antropy functions with N-D arrays and an axis argument for multi-channel data processing. ```python import numpy as np import antropy as ant # 4 channels × 3000 samples X = np.random.normal(size=(4, 3000)) pe = ant.perm_entropy(X, normalize=True, axis=-1) # shape (4,) mob, com = ant.hjorth_params(X, sf=256, axis=-1) # shape (4,) each nzc = ant.num_zerocross(X, normalize=True, axis=-1) # shape (4,) se = ant.spectral_entropy(X, sf=256, normalize=True) # shape (4,) ``` -------------------------------- ### Build HTML documentation Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Cleans previous builds and builds the HTML documentation using make. ```shell make -C docs clean make -C docs html ``` -------------------------------- ### Import Libraries Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_noise.ipynb Imports necessary libraries for numerical operations, data manipulation, plotting, entropy calculation, and noise generation. ```python import numpy as np import pandas as pd import seaborn as sns import antropy as ant import matplotlib.pyplot as plt import stochastic.processes.noise as sn sns.set(font_scale=1.25) ``` -------------------------------- ### Import Libraries Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Imports necessary libraries for numerical operations, data manipulation, entropy calculations, and plotting. ```python import numpy as np import pandas as pd import antropy as ant import seaborn as sns import matplotlib.pyplot as plt sns.set(font_scale=1.25) ``` -------------------------------- ### Describe Metrics Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Calculates and displays the minimum, median, and maximum values for each computed metric. ```python # Describe df.agg(["min", "median", "max"]).round(2).T ``` -------------------------------- ### Set Hodgkin-Huxley parameters Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_membrane_potential.ipynb Initializes the parameters and initial conditions for the Hodgkin-Huxley model simulation, including stimulus properties and neuronal constants. ```python # Set Hodgin-Huxley parameters A = 10 # nA t_start = 100 # ms t_stop = 400 # ms g_Na = 120 # mS/cm^2 g_K = 36 # mS/cm^2 g_L = 0.3 # mS/cm^2 Cm = 1 # F/cm^2 E_Na = 50 # mV E_K = -77 # mV E_L = -54.4 # mV y0 = [-65.0, 0.0529, 0.5916, 0.3177] # initial conditions t = np.arange(0.0, 500.0, 0.1) # time domain ``` -------------------------------- ### Run tests Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Executes the project's tests using pytest. ```shell pytest --verbose ``` -------------------------------- ### Generate Synthetic Data Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Generates 1000 1Hz sine waves with increasing noise levels and plots the first and last generated signals. ```python # Generate 1000 1Hz sines with increasing noise np.random.seed(123) N = 1000 sf = 100 n_sines = 100 noises_factor = np.linspace(0, 1, n_sines) noises = np.random.rand(n_sines, N) sines = np.zeros(shape=(n_sines, N)) for i in range(n_sines): sines[i] = np.sin(2 * np.pi * np.arange(N) / sf) + noises_factor[i] * noises[i, :) # Plot the first and last sines fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 6), sharex=True, sharey=False) ax1.plot(sines[0]) ax2.plot(sines[-1]); ``` -------------------------------- ### Check code style Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Checks code style and formatting using ruff. ```shell uvx ruff check uvx ruff format --check ``` -------------------------------- ### Create a branch for your changes Source: https://github.com/raphaelvallat/antropy/blob/master/docs/contributing.md Creates a new git branch for feature development. ```shell git checkout -b my-feature ``` -------------------------------- ### Plot Histograms Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Generates histograms for each metric to visualize their distributions. ```python # Distribution df.hist(figsize=(6, 7), layout=(3, 3), sharey=True) plt.tight_layout() ``` -------------------------------- ### Correlation Heatmap Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Visualizes the pairwise correlation between all computed metrics using a heatmap. ```python plt.figure(figsize=(8, 8)) sns.heatmap( df.corr().round(2), annot=True, vmin=-1, vmax=1, cbar=False, cmap="Spectral_r", square=True ); ``` -------------------------------- ### Compute Entropy Metrics Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Computes various entropy and fractal dimension metrics for each generated sine wave and stores them in a Pandas DataFrame. ```python # Compute the entropy metrics df = [] for i in range(n_sines): df.append( { "PermEnt": ant.perm_entropy(sines[i], order=3, normalize=True), "SVDEnt": ant.svd_entropy(sines[i], order=3, normalize=True), "SpecEnt": ant.spectral_entropy(sines[i], sf, normalize=True), "AppEnt": ant.app_entropy(sines[i], order=2), "SampleEnt": ant.sample_entropy(sines[i], order=2), "PetrosianFD": ant.petrosian_fd(sines[i]), "KatzFD": ant.katz_fd(sines[i]), "HiguchiFD": ant.higuchi_fd(sines[i]), "DFA": ant.detrended_fluctuation(sines[i]), }, ) df = pd.DataFrame(df) df.head().round(3) ``` -------------------------------- ### Clustermap Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_sine.ipynb Generates a clustermap to visualize the relationships between the metrics, with rows not clustered and data scaled. ```python sns.clustermap(df, row_cluster=False, standard_scale=1); ``` -------------------------------- ### Generate and plot voltage traces with noise Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_membrane_potential.ipynb Generates multiple voltage traces by adding increasing levels of Gaussian noise to the baseline Hodgkin-Huxley model output and plots the first and last traces to visualize the effect of noise. ```python # Generate 100 voltage traces with increasing noise np.random.seed(42) n_simulations = 100 noise_factor = np.linspace(0, 10, n_simulations) neural_noise = np.random.randn(n_simulations, t.size) traces = np.zeros((n_simulations, t.size)) for i in range(n_simulations): traces[i] = Vm + noise_factor[i] * neural_noise[i, :] # Plot the first and last voltage trace fig, ax = plt.subplots( 2, 1, sharex=True, sharey=False, gridspec_kw={"height_ratios": [3, 1]}, figsize=(8, 5) ) ax[0].plot(t, traces[0], label=f"noise factor = {noise_factor[0]}", zorder=2) ax[0].plot(t, traces[-1], label=f"noise factor = {noise_factor[-1]}", zorder=1) ax[0].set_ylabel("$V_m$ [mV]") ax[0].legend() ax[1].plot(t, I, "C2") ax[1].set_xlabel("$t$ [ms]") ax[1].set_ylabel("$I$ [nA]") ``` -------------------------------- ### Generate Time-Series with Increasing B Exponent Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_noise.ipynb Generates time-series data with varying 'B' exponents, simulating different types of noise (e.g., violet, white, Brownian). It then plots the first, center, and last generated time-series. ```python # Generate time-series with increasing B exponent betas = np.arange(-2, 2.1, 0.1) n_ts = betas.size n_samples = 1000 sf = 10 ts = np.empty((n_ts, n_samples + 1)) for i, b in enumerate(betas): rng = np.random.default_rng(42) ts[i] = sn.ColoredNoise(beta=b, rng=rng).sample(n_samples) # Plot the first, center and last time-series fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 10), sharex=True, sharey=False) ax1.plot(ts[0]) # Violet noise ax2.plot(ts[int(n_ts / 2)]) # White noise ax3.plot(ts[-1]); # Brownian noise ``` -------------------------------- ### Solve the ODE system and determine sampling frequency Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_membrane_potential.ipynb Solves the ordinary differential equations of the Hodgkin-Huxley model using the specified parameters and calculates a sampling frequency based on the number of detected spikes. ```python # Solve the ODE system I = synaptic_current(t, A, t_start, t_stop) params = (A, t_start, t_stop, g_Na, g_K, g_L, Cm, E_Na, E_K, E_L) Vm = odeint(hodgkin_huxley_model, y0, t, args=params)[:, 0] # Count the number of spikes... thresh = -40 n_spikes = find_peaks(Vm, height=thresh)[0].size # ... and define a sampling frequency sf = (n_spikes + 1) * 2 ``` -------------------------------- ### Hodgkin-Huxley model differential equations Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_membrane_potential.ipynb Implements the core differential equations for the Hodgkin-Huxley model, describing the change in membrane potential and gating variables over time. ```python def hodgkin_huxley_model(y0, t, A, t_start, t_stop, *args): """Return the change in state variables of the neuron. Parameters ---------- y0 : numpy.ndarray or list or tuple Initial conditions for resting membrane potential and `m`, `h` and `n` kinetic variables t : numpy.ndarray Sequence of time points for which to solve for state variables A : number Stimulus amplitude [nA] t_start : number Time point marking the start of stimulus input [ms] t_stop : number Time point marking the end of stimulus input [ms] args : list g_Na : number Sodium (Na) maximum conductance [mS/cm^2] g_K : number Postassium (K) maximum conductance [mS/cm^2] g_L : number Leak maximum conductance [mS/cm^2] Cm : number Membrane capacitance [F/cm^2] E_Na : number Sodium (Na) reversal potentials [mV] E_K : number Postassium (K) reversal potentials [mV] E_L : number Leak reversal potentials [mV] Returns ------- numpy.ndarray State variables dynamics of shape (`t.size`, 4) """ Vm, m, h, n = y0 if args: g_Na, g_K, g_L, Cm, E_Na, E_K, E_L = args else: g_Na, g_K, g_L, Cm, E_Na, E_K, E_L = 120, 36, 0.3, 50, -77, -54.4 dVmdt = ( synaptic_current(t, A, t_start, t_stop) - g_Na * m**3 * h * (Vm - E_Na) - g_K * n**4 * (Vm - E_K) - g_L * (Vm - E_L) ) / Cm dmdt = ( 0.1 * (Vm + 40) / (1.0 - np.exp(-(Vm + 40) / 10.0)) * (1.0 - m) - 4 * np.exp(-(Vm + 65) / 18.0) * m ) dhdt = 0.07 * np.exp(-(Vm + 65) / 20) * (1 - h) - 1 / (1.0 + np.exp(-(Vm + 35) / 10.0)) * h dndt = ( 0.01 * (Vm + 55) / (1.0 - np.exp(-(Vm + 55) / 10)) * (1 - n) - 0.125 * np.exp(-(Vm + 65) / 80) * n ) return np.hstack([dVmdt, dmdt, dhdt, dndt]) ``` -------------------------------- ### Synaptic current function Source: https://github.com/raphaelvallat/antropy/blob/master/notebooks/complexity_corr_membrane_potential.ipynb Defines a function to model a direct current input to the neuron, characterized by an amplitude and start/stop times. ```python def synaptic_current(t, A, t_start, t_stop): """Input direct current value. Parameters ---------- t : number or numpy.ndarray Time [ms] A : number Stimulus amplitude [nA] t_start : number Time point marking the start of stimulus input [ms] t_stop : number Time point marking the end of stimulus input [ms] Returns ------- float or numpy.ndarray Current in time """ return A * (t > t_start) - A * (t > t_stop) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.