Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add linting rules for pandas #764

Open
wants to merge 6 commits into
base: iblrigv8dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iblrig/base_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ def compute_reward_time(self, amount_ul: float | None = None) -> float:
Valve opening time in seconds.
"""
amount_ul = self.task_params.REWARD_AMOUNT_UL if amount_ul is None else amount_ul
return self.valve.values.ul2ms(amount_ul) / 1e3
return self.valve.values.ul2ms(amount_ul) / 1e3 # noqa: PD011

def valve_open(self, reward_valve_time):
"""
Expand Down
8 changes: 4 additions & 4 deletions iblrig/online_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def update_graphics(self, pupdate: float | None = None):
if self.data.psychometrics.loc[p]['count'].sum() == 0:
continue
# update psychometric curves
iok = ~np.isnan(self.data.psychometrics.loc[p]['choice'].values.astype(np.float32))
iok = ~np.isnan(self.data.psychometrics.loc[p]['choice'].to_numpy().astype(np.float32))
xval = self.data.psychometrics.loc[p].index[iok]
h.curve_psych[p][0].set(xdata=xval, ydata=self.data.psychometrics.loc[p]['choice'][iok])
h.curve_reaction[p][0].set(xdata=xval, ydata=self.data.psychometrics.loc[p]['response_time'][iok])
Expand Down Expand Up @@ -391,9 +391,9 @@ def display_full_jsonable(self, jsonable_file: Path | str):
self.data.water_delivered = trials_table.reward_amount.sum()
# init the last trials table
it = self.data.last_trials.index[-np.minimum(self.data.ntrials, NTRIALS_PLOT) :]
self.data.last_trials.loc[it, 'correct'] = trials_table.trial_correct.iloc[-NTRIALS_PLOT:].values
self.data.last_trials.loc[it, 'signed_contrast'] = trials_table.signed_contrast.iloc[-NTRIALS_PLOT:].values
self.data.last_trials.loc[it, 'response_time'] = trials_table.response_time.iloc[-NTRIALS_PLOT:].values
self.data.last_trials.loc[it, 'correct'] = trials_table.trial_correct.iloc[-NTRIALS_PLOT:].to_numpy()
self.data.last_trials.loc[it, 'signed_contrast'] = trials_table.signed_contrast.iloc[-NTRIALS_PLOT:].to_numpy()
self.data.last_trials.loc[it, 'response_time'] = trials_table.response_time.iloc[-NTRIALS_PLOT:].to_numpy()
self.data.last_trials.loc[it, 'stim_on'] = np.array(
[bpod_data[i]['States timestamps']['stim_on'][0][0] for i in np.arange(-it.size, 0)]
)
Expand Down
6 changes: 3 additions & 3 deletions iblrig/test/tasks/test_biased_choice_world_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def test_task(self, reward_set: np.ndarray | None = None):
first_trial=pd.NamedAgg(column='block_trial_num', aggfunc='first'),
)
# test that the first block is 90 trials
assert df_blocks['count'].values[0] == 90
assert df_blocks['count'].to_numpy()[0] == 90
# make all first block trials were reset to 0
assert np.all(df_blocks['first_trial'] == 0)
# test that the first block has 50/50 probability
assert df_blocks['stim_probability_left'].values[0] == 0.5
assert df_blocks['stim_probability_left'].to_numpy()[0] == 0.5
# make sure that all subsequent blocks alternate between 0.2 and 0.8 left probability
assert np.all(np.isclose(np.abs(np.diff(df_blocks['stim_probability_left'].values[1:])), 0.6))
assert np.all(np.isclose(np.abs(np.diff(df_blocks['stim_probability_left'].to_numpy()[1:])), 0.6))
# assert the the trial outcomes are within 0.3 of the generating probability
np.testing.assert_array_less(np.abs(df_blocks['position'] - df_blocks['stim_probability_left']), 0.4)
np.testing.assert_array_equal(np.unique(task.trials_table['reward_amount']), reward_set)
Expand Down
4 changes: 2 additions & 2 deletions iblrig/test/tasks/test_passive_choice_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_fixtures(self) -> None:

# The task stimuli replays consist of 300 stimulus presentations ordered randomly.
assert len(f) == 300
assert f.stim_type.iloc[:10].nunique() > 1
assert f.stim_type.iloc[:10].nunique() > 1 # noqa: PD101
assert set(f.stim_type.unique()) == {'G', 'N', 'T', 'V'}

# 180 gabor patches with 300 ms duration
Expand All @@ -52,7 +52,7 @@ def test_fixtures(self) -> None:
for position in positions:
counts = f[(f.stim_type == 'G') & (f.position == position) & (f.contrast != 0.0)].contrast.value_counts()
assert set(counts.keys()) == {0.0625, 0.125, 0.25, 1.0}
assert all([v == 20 for v in counts.values])
assert all([v == 20 for v in counts.to_numpy()])
assert len(f[f.stim_type == 'V']) == 40
assert len(f[f.stim_type == 'T']) == 40
assert len(f[f.stim_type == 'N']) == 40
Expand Down
2 changes: 1 addition & 1 deletion iblrig/test/tasks/test_training_choice_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_task(self):
contrast_set = np.array([0.5, 1.0])

np.testing.assert_equal(contrasts['contrast'].values, contrast_set)
normalized_counts = np.abs(nt / contrast_set.size - contrasts['count'].values)
normalized_counts = np.abs(nt / contrast_set.size - contrasts['count'].to_numpy())
normalized_counts = normalized_counts * probas / np.sum(probas)
normalized_counts = normalized_counts / (nt / contrast_set.size)
np.testing.assert_array_less(normalized_counts, 0.33)
Expand Down
2 changes: 1 addition & 1 deletion iblrig/test/test_choice_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def count_contrasts(pc):
df = pd.DataFrame(data=pc, columns=['angle', 'contrast', 'proba'])
df['signed_contrasts'] = df['contrast'] * np.sign(df['angle'])
c = df.groupby('signed_contrasts')['signed_contrasts'].count() / pc.shape[0]
return c.values
return c.to_numpy()

def test_default(self):
np.random.seed(7816)
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ select = [
"SIM", # flake8-simplify
"UP", # pyupgrade
"D", # pydocstyle
"PD", # pandas-vet
]
ignore = [
# TODO: The following entries should be gradually removed / fixed:
Expand All @@ -159,6 +160,9 @@ ignore = [
"D205", # 1 blank line required between summary line and description
"D400", # First line should end with a period
"D401", # First line of docstring should be in imperative mood
"PD002", # Checks for inplace=True usages in pandas function and method calls
"PD008", # Checks for uses of .at on Pandas objects
"PD901", # Avoid using the generic variable name `df` for DataFrames
]

[tool.ruff.lint.per-file-ignores]
Expand Down
Loading