Skip to content

Commit

Permalink
allow for new digitalevents creation
Browse files Browse the repository at this point in the history
zm711 committed Nov 1, 2023
1 parent 7f5aebb commit 4954bbc
Showing 2 changed files with 29 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/spikeanalysis/stimulus_data.py
Original file line number Diff line number Diff line change
@@ -463,7 +463,7 @@ def save_events(self):
for dig_channel, event_type in digital_events.items():
assert (
"stim" in event_type.keys()
), f"Mst provide name for each stim using the the set_stimulus_name() function. Please do this for {dig_channel}"
), f"Must provide name for each stim using the the set_stimulus_name() function. Please do this for {dig_channel}"
with open("digital_events.json", "w") as write_file:
json.dump(self.digital_events, write_file, cls=NumpyEncoder)
except AttributeError:
@@ -670,23 +670,26 @@ def load_into_stimulus_data(self, stim: StimulusData, new_stim_key: str, in_plac
assert isinstance(stim, StimulusData), "function is for loading into StimulusData"
try:
assert (
new_stim_key in stim.digital_events
new_stim_key not in stim.digital_events
), f"`new_stim_key` must be new key current keys are {stim.digital_events.keys()}"
except AttributeError:
warnings.warn(
"This function should be run after all other stimulus data has been processed but before setting trial groups and names"
)
stim.digital_events = {}

onsets, lengths = self._calculate_events()

if in_place:
stim.digital_events[new_stim_key] = {}
stim.digital_events[new_stim_key]["onsets"] = onsets
stim.digital_events[new_stim_key]["lengths"] = lengths
stim.digital_events[new_stim_key]["trial_groups"] = np.ones((len(onsets)))
else:
import copy

stim1 = copy.deepcopy(stim)
stim1.digital_events[new_stim_key] = {}
stim1.digital_events[new_stim_key]["onsets"] = onsets
stim1.digital_events[new_stim_key]["lengths"] = lengths
stim1.digital_events[new_stim_key]["trial_groups"] = np.ones((len(onsets)))
@@ -701,7 +704,7 @@ def _calculate_events(self) -> tuple[np.ndarray, np.ndarray]:
lengths: np.ndarray
the lengths of the events in samples"""

assert self._sample_rate, f"`sample_rate` must be set to calculate events, use `set_sample_rate()`"
assert self._sample_rate, "`sample_rate` must be set to calculate events, use `set_sample_rate()`"

timestamps = self.timestamps - self._start_timestamp
onset = np.where(np.diff(self.data) < 0)[0]
46 changes: 23 additions & 23 deletions test/test_stimulus_data.py
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ def test_json_writer(ana_stim, tmp_path):
stim1.get_raw_digital_data()
stim1.get_final_digital_data()
stim1.generate_digital_events()
stim1.set_stimulus_name({'DIGITAL-IN-01': 'testdig', 'DIGITAL-IN-02': 'testdig2'})
stim1.set_stimulus_name({"DIGITAL-IN-01": "testdig", "DIGITAL-IN-02": "testdig2"})
print(stim1._file_path)
stim1._file_path = stim1._file_path / tmp_path
print(stim1._file_path)
@@ -309,15 +309,16 @@ def test_set_stimulus_name(stim):

def test_delete_events(ana_stim):
import copy

ana_stim.digitize_analog_data(stim_length_seconds=0.1, analog_index=0, stim_name=["test"])
stim1 = copy.deepcopy(ana_stim)
stim1.get_raw_digital_data()
stim1.get_final_digital_data()
stim1.generate_digital_events()
stim1.delete_events(del_index=1, channel_name="DIGITAL-IN-01")
assert len(stim1.digital_events["DIGITAL-IN-01"]["events"]) == 20
stim1.delete_events(del_index=1, digital=False, channel_index='0')
assert len(stim1.dig_analog_events['0']['events'])==1
stim1.delete_events(del_index=1, digital=False, channel_index="0")
assert len(stim1.dig_analog_events["0"]["events"]) == 1


def test_run_all(stim):
@@ -327,41 +328,40 @@ def test_run_all(stim):
assert isinstance(stim.dig_analog_events, dict)
assert isinstance(stim.digital_events, dict)

def test_timestamp_reader(stim):

data = [0,0,0,1,1,1,0,0,0,1,1,1,0,0,0]
timestamps = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
def test_timestamp_reader(stim):
data = [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]
timestamps = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

reader = TimestampReader(data = data, timestamps=timestamps, start_timestamp=1, sample_rate=10)
reader = TimestampReader(data=data, timestamps=timestamps, start_timestamp=1, sample_rate=10)

assert reader._start_timestamp==1
assert reader._sample_rate ==10
assert isinstance(reader.data, np.ndarray), 'should save as ndarray'
assert reader._start_timestamp == 1
assert reader._sample_rate == 10
assert isinstance(reader.data, np.ndarray), "should save as ndarray"

reader.set_sample_rate(stim)
assert reader._sample_rate == 3000.
reader.set_sample_rate(sample_rate = 1)
assert reader._sample_rate == 1, 'sample rate should be reset'
assert reader._sample_rate == 3000.0
reader.set_sample_rate(sample_rate=1)
assert reader._sample_rate == 1, "sample rate should be reset"
reader.set_start_timestamp(stim)
assert reader._start_timestamp==0
reader.set_start_timestamp(start_ts = 0)
assert reader._start_timestamp == 0
reader.set_start_timestamp(start_ts=0)
assert reader._start_timestamp == 0

import copy

stim_no_event = copy.deepcopy(stim)
stim.get_raw_digital_data()
stim.get_final_digital_data()
stim.generate_digital_events()
stim1 = copy.deepcopy(stim)
reader.load_into_stimulus_data(stim=stim1, new_stim_key='testdata', in_place=True)
reader.load_into_stimulus_data(stim=stim1, new_stim_key="testdata", in_place=True)

assert 'testdata' in stim1.digital_events.keys()
assert "testdata" in stim1.digital_events.keys()

stim2 = copy.deepcopy(stim)
stim3 = reader.load_into_stimulus_data(stim=stim2, new_stim_key='testdata2', in_place=False)
assert 'testdata2' not in stim2.digital_events.keys()
assert 'testdata2' in stim3.digital_events.keys()

reader.load_into_stimulus_data(stim=stim_no_event, new_stim_key='warning_test', in_place=True)
stim3 = reader.load_into_stimulus_data(stim=stim2, new_stim_key="testdata2", in_place=False)
assert "testdata2" not in stim2.digital_events.keys()
assert "testdata2" in stim3.digital_events.keys()


reader.load_into_stimulus_data(stim=stim_no_event, new_stim_key="warning_test", in_place=True)

0 comments on commit 4954bbc

Please sign in to comment.