Skip to content

Commit

Permalink
update load_gpio_maxwell (#53)
Browse files Browse the repository at this point in the history
* update load_gpio_maxwell

* Print warnings to stderr.

---------

Co-authored-by: DailyDreaming <[email protected]>
  • Loading branch information
surygeng and DailyDreaming authored Dec 6, 2023
1 parent c758b47 commit b0dfeaa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/braingeneers/data/datasets_electrophysiology.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ def load_windows(metadata, exp, window_centers, window_sz, dtype=np.float16,

# Check if window is out of bounds
if window[0] < 0 or window[1] > dataset_length:
print("Window out of bounds, inserting zeros for window",window)
print("Window out of bounds, inserting zeros for window", window)
try:
data_temp = np.zeros((data_temp.shape[0],window_sz),dtype=dtype)
except Exception as e:
print(e)
print(e, file=sys.stderr)
data_temp = load_window(metadata, exp, window, dtype=dtype, channels=channels)
else:
data_temp = load_window(metadata, exp, window, dtype=dtype, channels=channels)
Expand Down Expand Up @@ -659,14 +659,14 @@ def load_stims_maxwell(uuid: str, metadata_ephys_exp: dict = None, experiment_st
return df

except FileNotFoundError:
print(f'\tThere seems to be no stim log file for this experiment! :(')
print(f'\tThere seems to be no stim log file for this experiment! :(', file=sys.stderr)
return None
except OSError:
print(f'\tThere seems to be no stim log file (on s3) for this experiment! :(')
print(f'\tThere seems to be no stim log file (on s3) for this experiment! :(', file=sys.stderr)
return None


def load_gpio_maxwell(dataset_path, fs=20000):
def load_gpio_maxwell(dataset_path, fs=20000.0):
"""
Loads the GPIO events for optogenetics stimulation.
:param dataset_path: a local or a s3 path
Expand All @@ -675,10 +675,12 @@ def load_gpio_maxwell(dataset_path, fs=20000):
"""
with smart_open.open(dataset_path, 'rb') as f:
with h5py.File(f, 'r') as dataset:
assert 'bits' in dataset.keys(), 'No GPIO event in the dataset!'
if 'bits' not in dataset.keys():
print('No GPIO event in the dataset!', file=sys.stderr)
return np.array([])
bits_dataset = list(dataset['bits'])
bits_dataframe = [bits_dataset[i][0] for i in range(len(bits_dataset))]
rec_startframe = dataset['raw'][-1, 0] << 16 | dataset['raw'][-2, 0]
bits_dataframe = [bits_dataset[i][0] for i in range(len(bits_dataset))]
rec_startframe = dataset['sig'][-1, 0] << 16 | dataset['sig'][-2, 0]
if len(bits_dataframe) % 2 == 0:
stim_pairs = (np.array(bits_dataframe) - rec_startframe).reshape(len(bits_dataframe) // 2, 2)
return stim_pairs / fs
Expand Down Expand Up @@ -1228,7 +1230,7 @@ def _axion_generate_per_block_metadata(filename: str):

fid.seek(start + int(obj.length.item()), 0)
if fid.tell() != start + obj.length:
print('Unexpected Channel array length')
print('Unexpected Channel array length', file=sys.stderr)

elif obj.type == 3:
continue
Expand Down
19 changes: 19 additions & 0 deletions src/braingeneers/data/datasets_electrophysiology_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ def test_modify_maxwell_metadata(self):

assert modified_metadata == expected_metadata

@skip_unittest_if_offline
def test_load_gpio_maxwell(self):
""" Read gpio event for Maxwell V1 file"""
data_1 = "s3://braingeneers/ephys/" \
"2023-04-02-hc328_rec/original/data/" \
"2023_04_02_hc328_0.raw.h5"
data_2 = "s3://braingeneers/ephys/" \
"2023-04-04-e-hc328_hckcr1-2_040423_recs/original/data/" \
"hc3.28_hckcr1_chip8787_plated4.4_rec4.4.raw.h5"
data_3 = "s3://braingeneers/ephys/" \
"2023-04-04-e-hc328_hckcr1-2_040423_recs/original/data/" \
"2023_04_04_hc328_hckcr1-2_3.raw.h5"
gpio_1 = ephys.load_gpio_maxwell(data_1)
gpio_2 = ephys.load_gpio_maxwell(data_2)
gpio_3 = ephys.load_gpio_maxwell(data_3)
self.assertEqual(gpio_1.shape, (1, 2))
self.assertEqual(gpio_2.shape, (0,))
self.assertEqual(gpio_3.shape, (29,))


class MEArecReaderTests(unittest.TestCase):
"""The fake reader test."""
Expand Down

0 comments on commit b0dfeaa

Please sign in to comment.