Skip to content

Fix for the probabilityLeft of some of the bugged BiasedCW sessions #67

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

Open
wants to merge 3 commits into
base: main
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
136 changes: 136 additions & 0 deletions src/iblphotometry/BCW_bug_solution_KB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""
KB 2025-05-25
Solution proposed on Oct2024

Code to fix the BCW bug in the IBL photometry sessions

This code is used to fix the bug in the labelling of some of the photometry BCW sessions - the task ran correctly, but the
labelling was incorrect for the probabilityLeft:
- BCW block 1 - 50/50 = correct
- BCW block 2 - 20/80 or 80/20 = correct
- BCW block 3 - 80/20 or 20/80 = incorrect - it starts shifting 0.2 and 0.8 for each consecutive trial

"""

#%%
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from one.api import ONE #always after the imports
one = ONE(cache_rest=None, mode='remote')

#functions
def get_eid():
# eids = one.search(project='ibl_fibrephotometry')
# use example eid with bug
eid = 'd3ac8f32-cfba-4f48-b513-9d998ab0ae72'
ref = one.eid2ref(eid)
print(eid)
print(ref)
try:
# Try to load the trials directly
a = one.load_object(eid, 'trials')
trials = a.to_df()
trials['trialNumber'] = range(1, len(trials) + 1)
trials["mouse"] = ref.subject
trials["date"] = ref.date
trials["eid"] = eid
df_trials = trials
except:

Check failure on line 39 in src/iblphotometry/BCW_bug_solution_KB.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E722)

src/iblphotometry/BCW_bug_solution_KB.py:39:5: E722 Do not use bare `except`
print("session not found")

return df_trials, eid

df_trials, eid = get_eid()

plt.plot(df_trials.probabilityLeft, alpha=0.5)


#%%
""" LOAD TRIALS """
def load_trials_updated(eid=eid):
trials = one.load_object(eid, 'trials')
ref = one.eid2ref(eid)
subject = ref.subject
session_date = str(ref.date)
if len(trials['intervals'].shape) == 2:
trials['intervals_0'] = trials['intervals'][:, 0]
trials['intervals_1'] = trials['intervals'][:, 1]
del trials['intervals'] # Remove original nested array
df_trials = pd.DataFrame(trials)
idx = 2
new_col = df_trials['contrastLeft'].fillna(df_trials['contrastRight'])
df_trials.insert(loc=idx, column='allContrasts', value=new_col)
# create allSContrasts
df_trials['allSContrasts'] = df_trials['allContrasts']
df_trials.loc[df_trials['contrastRight'].isna(), 'allSContrasts'] = df_trials['allContrasts'] * -1
df_trials.insert(loc=3, column='allSContrasts', value=df_trials.pop('allSContrasts'))
df_trials[["subject", "date", "eid"]] = [subject, session_date, eid]
df_trials["reactionTime"] = df_trials["firstMovement_times"] - df_trials["stimOnTrigger_times"]
df_trials["responseTime"] = df_trials["response_times"] - df_trials["stimOnTrigger_times"]
df_trials["quiescenceTime"] = df_trials["stimOnTrigger_times"] - df_trials["intervals_0"]
df_trials["trialTime"] = df_trials["intervals_1"] - df_trials["intervals_0"]

try:
dataset_task_settings = one.load_dataset(eid, '_iblrig_taskSettings.raw.json')
values = dataset_task_settings.get('LEN_BLOCKS', 'Key not found')
# values gives the block length
# example for eid = 'd3ac8f32-cfba-4f48-b513-9d998ab0ae72'
# values = [90, 41, 65, 62, 30, 31, 64, 82, 33, 80, 70, 62, 40, 70, 22, 72, 60, 22, 30, 53, 51, 28, 31, 28, 22, 41, 72, 72, 51, 70, 24, 30, 55, 78, 39, 53, 23, 53, 25, 21, 48, 97]


values_sum = np.cumsum(values)

# Initialize a new column 'probL' with NaN values
df_trials['probL'] = np.nan

# Set the first block (first `values_sum[0]` rows) to 0.5
df_trials.loc[:values_sum[0]-1, 'probL'] = 0.5


df_trials.loc[values_sum[0]:values_sum[1]-1, 'probL'] = df_trials.loc[values_sum[0], 'probabilityLeft']

previous_value = df_trials.loc[values_sum[1]-1, 'probabilityLeft']


# Iterate over the blocks starting from values_sum[1]
for i in range(1, len(values_sum)-1):
print("i = ", i)
start_idx = values_sum[i]
end_idx = values_sum[i+1]-1
print("start and end _idx = ", start_idx, end_idx)

# Assign the block value based on the previous one
if previous_value == 0.2:
current_value = 0.8
else:
current_value = 0.2
print("current value = ", current_value)


# Set the 'probL' values for the current block
df_trials.loc[start_idx:end_idx, 'probL'] = current_value

# Update the previous_value for the next block
previous_value = current_value

# Handle any remaining rows after the last value_sum block
if len(df_trials) > values_sum[-1]:
df_trials.loc[values_sum[-1] + 1:, 'probL'] = previous_value

# plt.plot(df_trials.probabilityLeft, alpha=0.5)
# plt.plot(df_trials.probL, alpha=0.5)
# plt.title(f'behavior_{subject}_{session_date}_{eid}')
# plt.show()
except:

Check failure on line 125 in src/iblphotometry/BCW_bug_solution_KB.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E722)

src/iblphotometry/BCW_bug_solution_KB.py:125:5: E722 Do not use bare `except`
pass

df_trials["trialNumber"] = range(1, len(df_trials) + 1)
return df_trials, subject, session_date

df_trials, subject, session_date = load_trials_updated(eid)

plt.plot(df_trials.probabilityLeft, alpha=0.5) #bug
plt.plot(df_trials.probL, alpha=0.85, color='red') #bug solved

# %%
83 changes: 83 additions & 0 deletions src/iblphotometry/BCW_bugged_eids_list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
eid
488cb758-888a-4664-bc8e-2e0624507ce4
61073b6e-842e-440e-bf59-b3197065bfa1
24415d91-8a3a-440b-beee-2d4388a4b908
f416af34-121d-419c-8d27-4e95f8648012
b46d38e4-cba5-4d2f-b975-93542a452317
9c86d1db-cb3f-4bfe-bdd4-450c25b1b779
385d2bc7-02e6-4d65-b20a-68ea9bd85e7c
56044f43-8550-4b36-ae5d-0a32e627c0fd
65b43acb-afa5-4590-a400-f7f4c79788ab
cd9d071e-c798-4900-891f-b65640ec22b1
25ff1634-88e8-47bf-9f3a-f527d0f94444
5104ba1a-9c35-470a-8f6d-69a07349e19b
d3ac8f32-cfba-4f48-b513-9d998ab0ae72
4f9da1a0-5465-410b-83f4-e852cfe61107
40a2c7c5-bdce-4aae-ab2f-e088d82e58d3
8996a0d0-3b23-4d7e-92a4-8272b4bb5a9f
be3208c9-43de-44dc-bdc6-ff8963464f98
5b7c1f53-0c33-4ac3-8838-2926f46abf13
490f4be9-de15-4251-aae4-2e059bbc03c9
cb7994bf-f063-46ea-bd05-aa6e9a2fc7ef
274c4683-ef2a-471a-841f-af634446ec8d
e69221c7-b533-42dc-8631-000279a45a70
55d7f234-cc47-44a6-bb36-086c854eff00
57c0be0d-5010-4257-bab5-dbf7eca081f3
4b42f52f-3e60-41f6-869a-465ee241d0e8
86e88a0d-a637-4bf4-a215-ef6013277234
cb21401e-dfe6-4e81-b6d6-df14107a45c3
5e847644-4e5b-4074-bb48-8218c0fa76fd
cc0b6954-3030-488e-8d10-a1ba421d655a
9fac6cf0-8dc6-456d-9dd5-48be737df408
85764273-c611-491f-b4f5-de127dde1cf1
3df9f2f6-708a-48aa-b033-050750e62102
c9961dce-e106-49b7-8892-9d7dc22675be
4590ea45-e39a-45cd-925c-542d4a5eb97b
5ca99111-68bd-40cf-a582-7956a9057b0d
708a3e0e-d8d7-43a8-b182-6a4192e4dc80
7757b591-7188-4418-8a9c-ae6cf17cb82c
a54d305f-2089-47ac-8e75-134db4fd3ed9
6aefe272-b19a-41df-9adc-5af6cedb8dab
e7c37ff1-e3ee-4e82-813f-f408af91730c
d226644e-d10b-4142-9e5e-17aa91f378d6
8404a92c-65d1-4923-9efd-13155ec1c8db
48e60716-5850-4262-b9cd-24ffb6a7189f
09d25ccb-b0b5-4e89-81ff-974128934390
82114940-6291-4e2c-8d3e-4f3b73c081de
272cfbd8-d1b7-4a3b-aa13-dfc514667b46
87809791-48fe-47fd-9973-03ddb860f8cd
b5e94727-4842-4e81-830a-2b48b60ecf0b
46fe69ff-d001-4608-a15e-d5e029c14fc3
31ca8cb2-92a7-4608-a998-104ec5db1c54
89b8ef70-e620-49c2-a0f7-09890ba9fc0e
beae9e93-2826-4205-b9f9-376dd785dad0
9f19fbe9-766b-4324-b2bf-a55c7131949b
4be05d77-d7fd-40f4-a60a-0b9269f4bcb1
628d24c4-6305-4125-b5bd-4723a03a5be8
fc8963f9-7282-4a05-a4a6-41ce0a1541df
8062e7d8-f770-47bc-94f5-d02691d8b02a
820ece0e-aa62-44fe-a881-dbd51e49748e
0f5f08e1-b2b6-4306-b4de-c89b1156581d
c4dcc2bd-e701-4a28-bfaa-d796320201bd
0e951224-308e-4f91-96c7-58414d335f10
60246d4e-2386-443a-a5d6-94ac015aaf06
382c9380-a2f7-411f-949d-a6df6c00747f
f0eb3ff8-873e-4bce-8082-7c8e28b47edb
950565f4-1054-4a58-b5c9-8fa47ff42fe6
0b842869-94c4-4248-a302-a31859136ad2
65aa40c1-3400-4717-8854-4cfba85b5899
e316594f-bcdb-42ba-b8e8-68d80a456a34
67231166-8980-4146-ad6d-3d7bee327ccf
76f443fb-a06d-4ffd-9725-23d5c0d40386
9e8c2697-f5f7-42f6-b483-5b9af9c1652e
537a8269-3cdb-4738-9b0f-1134cd4e3d0d
160cff05-de6b-4070-9eaa-0bbbcb0a71d5
66d51ecb-b6a0-4293-84be-d4ccfb84ada8
8cfa1e92-3e7f-409f-8347-01a36e98b27f
f36ae1d4-20e0-47ee-b4bd-9649810d87e0
a2e65766-0f40-46dd-91cf-b56540cb033b
4783a2d1-2761-4c66-93fe-71b310b49261
a4df6acb-bf2c-4362-80c1-a1a207aaf86e
69cb5d57-109a-4455-b469-9cc235812cfb
df899930-8172-400a-a7f0-07061382a81b
714faf78-b4a5-4aff-9504-6f270dd139be
Loading