Skip to content

Commit

Permalink
Fix: too many samples returned for ranged async data
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasstraka committed Aug 13, 2021
1 parent 7457cbb commit f4e6658
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pyDmdReader/_dmd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,16 @@ def __get_ranged_sweeps(self, ch_config, start_time, end_time) -> Tuple[np.array
block_max_samples = last_sample - first_sample + 1

raw_timestamps, raw_values, next_sample = self.__get_single_sweep(ch_config, first_sample, block_max_samples)
data = np.r_[data, raw_values]
timestamps = np.r_[timestamps, raw_timestamps]

if len(raw_timestamps) > 0:
if raw_timestamps[-1] <= actual_end_time:
timestamps = np.r_[timestamps, raw_timestamps]
data = np.r_[data, raw_values]
else:
# Do not use sample values outside the requested range
last_valid = np.argmax(raw_timestamps > actual_end_time)
timestamps = np.r_[timestamps, raw_timestamps[:last_valid]]
data = np.r_[data, raw_values[:(last_valid * ch_config.max_sample_dimension)]]

first_sample = next_sample

Expand Down
Binary file added tests/data/syncasync.dmd
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/test_loadfile_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest

SIMPLE_DMD = os.path.join(os.path.dirname(__file__), "data/simple.dmd")
SYNCASYNC_DMD = os.path.join(os.path.dirname(__file__), "data/syncasync.dmd")
INTERNAL_DMD = 'DMD_DEMO_FILE'

def test_check_invalid_name():
Expand Down Expand Up @@ -149,3 +150,27 @@ def test_timerange_simple():
assert round(data.index[-1], 2) == 0.4

dmd.close()

def test_async():
dmd = DmdReader(SYNCASYNC_DMD)
# The file has one sweeps [0 - 0.633] but a recording offset to acquisition start
all_data_sync = dmd.read_dataframe(dmd.channel_names[0])
all_data_async = dmd.read_dataframe(dmd.channel_names[1])
assert len(all_data_sync) == 29315
assert len(all_data_async) == 293
assert all_data_sync.index[0] == 0
assert round(all_data_sync.index[-1], 3) == round(dmd.measurement_duration, 3)
assert round(all_data_async.index[0], 2) == 0.01 # Async average with 100 Hz
assert round(all_data_async.index[-1], 2) == 2.93

rng_data_sync = dmd.read_dataframe(dmd.channel_names[0], start_time=0.5, end_time=1.5)
rng_data_async = dmd.read_dataframe(dmd.channel_names[1], start_time=0.5, end_time=1.5)
assert len(rng_data_sync) == 10001
assert len(rng_data_async) == 101

assert round(rng_data_sync.index[0], 3) == 0.5
assert round(rng_data_sync.index[-1], 3) == 1.5
assert round(rng_data_async.index[0], 2) == 0.5
assert round(rng_data_async.index[-1], 2) == 1.5

dmd.close()

0 comments on commit f4e6658

Please sign in to comment.