Skip to content
Merged
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
4 changes: 2 additions & 2 deletions +ndr/+reader/base.m
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
error(['Do not know how to handle different sampling rates across channels.']);
end;
t0t1 = ndr_reader_base_obj.t0_t1(epochstreams, epoch_select);
t = ndr.time.fun.samples2times(s, t0t1{1}(1), sr_unique);
t = ndr.time.fun.samples2times(s, t0t1{1}, sr_unique);
end; % samples2times()

function s = times2samples(ndr_reader_base_obj, channeltype, channel, epochstreams, epoch_select, t)
Expand All @@ -279,7 +279,7 @@
error(['Do not know how to handle different sampling rates across channels.']);
end;
t0t1 = ndr_reader_base_obj.t0_t1(epochstreams, epoch_select);
s = ndr.time.fun.times2samples(t, t0t1{1}(1), sr_unique);
s = ndr.time.fun.times2samples(t, t0t1{1}, sr_unique);
end; % times2samples()
end; % methods

Expand Down
2 changes: 1 addition & 1 deletion .github/badges/tests.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions tools/tests/+ndr/+unittest/+reader/TestIntanRhd.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
classdef TestIntanRhd < matlab.unittest.TestCase
% TESTINTANRHD - Unit tests for ndr.reader.intan_rhd

methods (Test)
function testSamples2Times(testCase)
% 1. Setup
reader = ndr.reader.intan_rhd();
ndr_path = ndr.fun.ndrpath();
rhd_file = fullfile(ndr_path, 'example_data', 'example.rhd');

epochstreams = {rhd_file};
epoch_select = 1;
channeltype = 'ai';
channel = 1;

% 2. Get expected T0 and T1 from the reader itself
% This ensures we are testing the internal consistency of the reader
% and specifically the fix in ndr.reader.base.samples2times/times2samples
t0t1 = reader.t0_t1(epochstreams, epoch_select);
t0 = t0t1{1}(1);
t1 = t0t1{1}(2);
sr = reader.samplerate(epochstreams, epoch_select, channeltype, channel);

% 3. Test samples2times
% Test regular sample
s = [1, 100, 1000];
t = reader.samples2times(channeltype, channel, epochstreams, epoch_select, s);

expected_t = (s - 1) / sr + t0;
testCase.verifyEqual(t, expected_t, 'AbsTol', 1e-9);

% Test Inf (should be t1)
% This triggers the code path: t(g) = t0_t1(2);
% If the fix is not applied (passing scalar instead of vector), this would fail or error.
s_inf = Inf;
t_inf = reader.samples2times(channeltype, channel, epochstreams, epoch_select, s_inf);
testCase.verifyEqual(t_inf, t1, 'AbsTol', 1e-9);

% 4. Test times2samples
% Test regular time
t_in = [t0, t0 + 0.1];
s_out = reader.times2samples(channeltype, channel, epochstreams, epoch_select, t_in);

expected_s = 1 + round((t_in - t0) * sr);
testCase.verifyEqual(s_out, expected_s, 'AbsTol', 1e-9);

% Test Inf time
% In times2samples.m: s(g) = 1+sr*diff(t0_t1);
t_inf_in = Inf;
s_inf_out = reader.times2samples(channeltype, channel, epochstreams, epoch_select, t_inf_in);

expected_s_inf = 1 + sr * (t1 - t0);
testCase.verifyEqual(s_inf_out, expected_s_inf, 'AbsTol', 1e-9);

end

function testReadChannels(testCase)
% Verify that reading channels works and matches ndr.format.intan output logic
reader = ndr.reader.intan_rhd();
ndr_path = ndr.fun.ndrpath();
rhd_file = fullfile(ndr_path, 'example_data', 'example.rhd');

epochstreams = {rhd_file};
epoch_select = 1;
channeltype = 'ai';
channel = 1;

% Read a small chunk
s0 = 1;
s1 = 100;

data = reader.readchannels_epochsamples(channeltype, channel, epochstreams, epoch_select, s0, s1);

% Verify dimensions
testCase.verifyEqual(size(data, 1), s1 - s0 + 1);
testCase.verifyEqual(size(data, 2), 1);

% We can also compare against direct format read if needed, but the above confirms basic reader function
% and the primary goal is covering samples2times fix.

% Verify getting header
header = ndr.format.intan.read_Intan_RHD2000_header(rhd_file);
testCase.verifyNotEmpty(header);
end
end
end