Skip to content

Commit

Permalink
Merge pull request #8 from ryanharvey1/7-add-github-workflow
Browse files Browse the repository at this point in the history
fixes for github workflow tests
  • Loading branch information
ryanharvey1 authored Sep 11, 2024
2 parents 1d98e45 + d97f688 commit abd7c27
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 44 deletions.
12 changes: 5 additions & 7 deletions neuro_py/ensemble/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
This implementation was written in Feb 2019.
Please e-mail me if you have comments, doubts, bug reports or criticism (Vítor, [email protected] / [email protected]).
"""
import warnings

import numpy as np

import warnings
from typing import Tuple, Union

from scipy import stats
from sklearn.decomposition import FastICA
from sklearn.decomposition import PCA
import numpy as np
from lazy_loader import attach as _attach
from scipy import stats
from sklearn.decomposition import PCA, FastICA

__all__ = (
"toyExample",
Expand Down Expand Up @@ -227,7 +225,7 @@ def runPatterns(

if np.isnan(significance.nassemblies):
return None, significance, None

if significance.nassemblies < 1:
warnings.warn("no assembly detected")

Expand Down
14 changes: 5 additions & 9 deletions neuro_py/ensemble/assembly_reactivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
from neuro_py.io import loading
from neuro_py.session.locate_epochs import compress_repeated_epochs, find_pre_task_post

__all__ = (
"AssemblyReact",
)
__all__ = ("AssemblyReact",)
__getattr__, __dir__, __all__ = _attach(f"{__name__}", submodules=__all__)
del _attach

Expand Down Expand Up @@ -226,7 +224,7 @@ def get_weights(self, epoch=None):

# check if st has any neurons
if self.st.isempty:
self.patterns = []
self.patterns = None
return

if epoch is not None:
Expand All @@ -235,7 +233,7 @@ def get_weights(self, epoch=None):
bst = self.st.bin(ds=self.weight_dt).data

if (bst == 0).all():
self.patterns = []
self.patterns = None
else:
patterns, _, _ = assembly.runPatterns(
bst,
Expand Down Expand Up @@ -293,7 +291,7 @@ def plot(
if not hasattr(self, "patterns"):
return "run get_weights first"
else:
if self.patterns == []:
if self.patterns is None:
return None, None
if plot_members:
self.find_members()
Expand Down Expand Up @@ -352,9 +350,7 @@ def plot(

def n_assemblies(self):
if hasattr(self, "patterns"):
if self.patterns == []:
return 0
elif self.patterns is None:
if self.patterns is None:
return 0
return self.patterns.shape[0]

Expand Down
5 changes: 3 additions & 2 deletions neuro_py/lfp/theta_cycles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
from typing import Union
from typing import Union, Tuple

import nelpy as nel
import numpy as np
Expand Down Expand Up @@ -154,11 +154,12 @@ def save_theta_cycles(

def get_theta_cycles(
basepath: str,
theta_freq: tuple[int] = (6, 10),
theta_freq: Tuple[int] = (6, 10),
lowpass: int = 48,
detection_params: Union[dict, None] = None,
ch: Union[int, None] = None,
):
# import bycycle, hidden import to avoid mandatory dependency
from bycycle import Bycycle

# load lfp as memmap
Expand Down
38 changes: 22 additions & 16 deletions neuro_py/process/precession_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import numba
import numpy as np
import pycircstat as pcs
import pyfftw
import scipy as sp

from lazy_loader import attach as _attach
from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d
from scipy.signal import find_peaks

import neuro_py.stats.circ_stats as pcs

__all__ = (
"corrcc",
Expand All @@ -26,6 +26,7 @@
# https://github.com/seqasim/human_precession/blob/main/Precession_utils.py
# https://doi.org/10.1016/j.cell.2021.04.017


def corrcc(alpha1, alpha2, axis=None):
"""
Circular correlation coefficient for two circular random variables.
Expand Down Expand Up @@ -57,7 +58,7 @@ def corrcc(alpha1, alpha2, axis=None):
n = len(alpha1)

# center data on circular mean
alpha1_centered, alpha2_centered = pcs.descriptive.center(alpha1, alpha2, axis=axis)
alpha1_centered, alpha2_centered = pcs.center(alpha1, alpha2, axis=axis)

num = np.sum(np.sin(alpha1_centered) * np.sin(alpha2_centered), axis=axis)
den = np.sqrt(
Expand Down Expand Up @@ -114,14 +115,14 @@ def corrcc_uniform(alpha1, alpha2, axis=None):
n = len(alpha1)

# center data on circular mean
alpha1_centered, alpha2_centered = pcs.descriptive.center(alpha1, alpha2, axis=axis)
alpha1_centered, alpha2_centered = pcs.center(alpha1, alpha2, axis=axis)

# One of the sample means is not well defined due to uniform distribution of data
# so take the difference of the resultant vector length for the sum and difference
# of the alphas
num = pcs.descriptive.resultant_vector_length(
num = pcs.resultant_vector_length(
alpha1 - alpha2
) - pcs.descriptive.resultant_vector_length(alpha1 + alpha2)
) - pcs.resultant_vector_length(alpha1 + alpha2)
den = 2 * np.sqrt(
np.sum(np.sin(alpha1_centered) ** 2, axis=axis)
* np.sum(np.sin(alpha2_centered) ** 2, axis=axis)
Expand Down Expand Up @@ -206,8 +207,8 @@ def myfun1(p):
linear_circ = np.mod(abs(sl) * lin, 2 * np.pi)

# # marginal distributions:
p1, z1 = pcs.tests.rayleigh(circ)
p2, z2 = pcs.tests.rayleigh(linear_circ)
p1, z1 = pcs.rayleigh(circ)
p2, z2 = pcs.rayleigh(linear_circ)

# circular-linear correlation:
if (p1 > 0.5) | (p2 > 0.5):
Expand Down Expand Up @@ -297,7 +298,6 @@ def pcorrelate(t, u, bins):


def fast_acf(counts, width, bin_width, cut_peak=True):

"""
Super fast ACF function relying on numba (above).
Expand Down Expand Up @@ -339,7 +339,6 @@ def fast_acf(counts, width, bin_width, cut_peak=True):


def acf_power(acf, norm=True):

"""
Compute the power spectrum of the signal by computing the FFT of the autocorrelation.
Expand Down Expand Up @@ -384,9 +383,8 @@ def nonspatial_phase_precession(
norm: bool = True,
psd_lims: list = [0.65, 1.55],
upsample: int = 4,
smooth_sigma=1
smooth_sigma=1,
):

"""
Compute the nonspatial spike-LFP relationship modulation index.
Expand Down Expand Up @@ -425,7 +423,9 @@ def nonspatial_phase_precession(
)

frequencies = np.interp(
np.arange(0, len(frequencies), 1/upsample), np.arange(0, len(frequencies)), frequencies
np.arange(0, len(frequencies), 1 / upsample),
np.arange(0, len(frequencies)),
frequencies,
)

freqs_of_interest = np.intersect1d(
Expand All @@ -436,7 +436,7 @@ def nonspatial_phase_precession(
psd = acf_power(acf, norm=norm)

# upsample 2x psd
psd = np.interp(np.arange(0, len(psd), 1/upsample), np.arange(0, len(psd)), psd)
psd = np.interp(np.arange(0, len(psd), 1 / upsample), np.arange(0, len(psd)), psd)
# smooth psd with gaussian filter
psd = gaussian_filter1d(psd, smooth_sigma)

Expand All @@ -445,7 +445,13 @@ def nonspatial_phase_precession(

# make sure there is a peak
if ~np.any(all_peaks):
return np.nan, np.nan, psd[freqs_of_interest], frequencies[freqs_of_interest], acf
return (
np.nan,
np.nan,
psd[freqs_of_interest],
frequencies[freqs_of_interest],
acf,
)

max_peak = np.max(psd[freqs_of_interest][all_peaks])
max_idx = [all_peaks[np.argmax(psd[freqs_of_interest][all_peaks])]]
Expand Down
Loading

0 comments on commit abd7c27

Please sign in to comment.