Skip to content

Commit

Permalink
add test for new class
Browse files Browse the repository at this point in the history
  • Loading branch information
zm711 committed Nov 1, 2023
1 parent c97a490 commit 7f5aebb
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions test/test_stimulus_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

from spikeanalysis.stimulus_data import StimulusData
from spikeanalysis.stimulus_data import TimestampReader


@pytest.fixture
Expand Down Expand Up @@ -115,6 +116,10 @@ def test_json_writer(ana_stim, tmp_path):
stim1 = ana_stim
stim1.digitize_analog_data(stim_length_seconds=0.1)
print(stim1.dig_analog_events)
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'})
print(stim1._file_path)
stim1._file_path = stim1._file_path / tmp_path
print(stim1._file_path)
Expand Down Expand Up @@ -302,12 +307,17 @@ def test_set_stimulus_name(stim):
assert stim.digital_events["DIGITAL-IN-01"]["stim"] == "TEST"


def test_delete_events(stim):
stim.get_raw_digital_data()
stim.get_final_digital_data()
stim.generate_digital_events()
stim.delete_events(del_index=1, channel_name="DIGITAL-IN-01")
assert len(stim.digital_events["DIGITAL-IN-01"]["events"]) == 20
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


def test_run_all(stim):
Expand All @@ -316,3 +326,42 @@ def test_run_all(stim):
assert stim.analog_data.any()
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]

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'

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'
reader.set_start_timestamp(stim)
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)

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)


0 comments on commit 7f5aebb

Please sign in to comment.