Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
BalzaniEdoardo committed Apr 5, 2024
1 parent d5a8f6d commit 7aebc36
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
32 changes: 21 additions & 11 deletions pynapple/core/ts_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def __getattr__(self, name):
return self._metadata[name]
else:
# If the attribute is not part of the metadata, raise AttributeError
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)

def __setitem__(self, key, value):
if not self._initialized:
Expand Down Expand Up @@ -234,17 +236,21 @@ def __getitem__(self, key):
if key.ndim != 1:
raise IndexError("Only 1-dimensional boolean indices are allowed!")
if len(key) != self.__len__():
raise IndexError("Boolean index length must be equal to the number of Ts in the group! "
f"The number of Ts is {self.__len__()}, but the bolean array"
f"has length {len(key)} instead!")
raise IndexError(
"Boolean index length must be equal to the number of Ts in the group! "
f"The number of Ts is {self.__len__()}, but the bolean array"
f"has length {len(key)} instead!"
)
key = np.asarray(self.keys())[key]
return self._ts_group_from_keys(key)

def _ts_group_from_keys(self, keys):
metadata = self._metadata.loc[np.sort(keys), self._metadata.columns.drop("rate")]
metadata = self._metadata.loc[
np.sort(keys), self._metadata.columns.drop("rate")
]
return TsGroup(
{k: self[k] for k in keys}, time_support=self.time_support, **metadata
)
{k: self[k] for k in keys}, time_support=self.time_support, **metadata
)

def __repr__(self):
cols = self._metadata.columns.drop("rate")
Expand Down Expand Up @@ -325,8 +331,10 @@ def _check_metadata_colum_names(self, *args, **kwargs):
invalid_cols += [k]

if invalid_cols:
raise ValueError(f"Invalid metadata name(s) {invalid_cols}. Metadata name must differ from "
f"TsGroup attribute names!")
raise ValueError(
f"Invalid metadata name(s) {invalid_cols}. Metadata name must differ from "
f"TsGroup attribute names!"
)

def set_info(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -416,8 +424,10 @@ def set_info(self, *args, **kwargs):
else:
not_set.append({k: v})
if not_set:
raise TypeError(f"Cannot set the following metadata:\n{not_set}.\nMetadata columns provided must be "
f"of type `panda.Series`, `tuple`, `list`, or `numpy.ndarray`.")
raise TypeError(
f"Cannot set the following metadata:\n{not_set}.\nMetadata columns provided must be "
f"of type `panda.Series`, `tuple`, `list`, or `numpy.ndarray`."
)

def get_info(self, key):
"""
Expand Down
2 changes: 1 addition & 1 deletion pynapple/io/interface_npz.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def load(self):
"index",
"d",
"rate",
"keys"
"keys",
}:
tmp = self.file[k]
if len(tmp) == len(tsgroup):
Expand Down
4 changes: 4 additions & 0 deletions tests/npzfilestest/tsd2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"time": "2024-04-05 11:47:12.805723",
"info": "Test description"
}
23 changes: 23 additions & 0 deletions tmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pynapple as nap
import numpy as np
import pandas as pd

n_unit = 100
df = pd.DataFrame()
df["isi_violations_ratio"] = np.random.uniform(0, 0.7, size=n_unit)
df["firing_rate"] = np.random.uniform(0, 80, size=n_unit)
df["presence_ratio"] = np.random.uniform(0.7, 1., size=n_unit)
df["device_name"] = [f"Probe{k%4}" for k in range(n_unit)]



dd = {k: np.sort(np.random.uniform(size=n_unit)) for k in range(n_unit)}
units = nap.TsGroup(dd)
units.set_info(df)

pass_qc = units[(units.isi_violations_ratio < 0.5) &
(units.firing_rate > 0.1) &
(units.presence_ratio > 0.95) &
(units.device_name == 'Probe1')]
#pass_qc["new"] = np.arange(len(pass_qc))
pass_qc[1.1] = np.arange(len(pass_qc)) + 10

0 comments on commit 7aebc36

Please sign in to comment.