-
Notifications
You must be signed in to change notification settings - Fork 1
Implement samples2times and times2samples for axon_abf #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d95e381
Implement samples2times and times2samples for axon_abf to handle gaps
google-labs-jules[bot] 2be83f2
Update GitHub badges
github-actions[bot] 5bf3839
Implement samples2times and times2samples for axon_abf with unittest
google-labs-jules[bot] 02d7e24
Implement samples2times and times2samples for axon_abf with robust un…
google-labs-jules[bot] 8e35777
Update GitHub badges
github-actions[bot] 9f9b18b
Convert intan incomplete file test to unittest class
google-labs-jules[bot] d34b900
Update GitHub badges
github-actions[bot] 3a9c3a4
Enforce column output in axon_abf and fix unittest vectors
google-labs-jules[bot] 42a35ae
Update GitHub badges
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions
120
tools/tests/+ndr/+unittest/+format/+intan/TestIntanIncompleteFile.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| classdef TestIntanIncompleteFile < matlab.unittest.TestCase | ||
|
|
||
| methods (Test) | ||
| function testReadIncompleteFile(testCase) | ||
| % testReadIncompleteFile - Test reading an Intan RHD file with incomplete data blocks | ||
| % | ||
| % This test creates a dummy .rhd file with a partial data block and checks if | ||
| % ndr.format.intan.read_Intan_RHD2000_datafile handles it gracefully (ignoring the partial block). | ||
|
|
||
| % Create a dummy header | ||
| sample_rate = 20000; | ||
| num_samples_per_data_block = 60; | ||
|
|
||
| header = struct(); | ||
| header.fileinfo = struct(... | ||
| 'dirname', '.', ... | ||
| 'filename', 'test_incomplete_file',... | ||
| 'filesize', 0, ... % will be updated later | ||
| 'magic_number', hex2dec('c6912702'),... | ||
| 'data_file_main_version_number', 1,... | ||
| 'data_file_secondary_version_number', 2,... | ||
| 'eval_board_mode',0,... | ||
| 'reference_channel','',... | ||
| 'num_samples_per_data_block',num_samples_per_data_block,... | ||
| 'notes',struct('note1','','note2','','note3','')); | ||
| header.fileinfo.headersize = 100; | ||
|
|
||
| header.frequency_parameters = struct( ... | ||
| 'amplifier_sample_rate', sample_rate, ... | ||
| 'aux_input_sample_rate', sample_rate / 4, ... | ||
| 'supply_voltage_sample_rate', sample_rate / num_samples_per_data_block, ... | ||
| 'board_adc_sample_rate', sample_rate, ... | ||
| 'board_dig_in_sample_rate', sample_rate, ... | ||
| 'desired_dsp_cutoff_frequency', 0, ... | ||
| 'actual_dsp_cutoff_frequency', 0, ... | ||
| 'dsp_enabled', 0, ... | ||
| 'desired_lower_bandwidth', 0, ... | ||
| 'actual_lower_bandwidth', 0, ... | ||
| 'desired_upper_bandwidth', 0, ... | ||
| 'actual_upper_bandwidth', 0, ... | ||
| 'notch_filter_frequency', 0, ... | ||
| 'desired_impedance_test_frequency', 0, ... | ||
| 'actual_impedance_test_frequency', 0 ); | ||
|
|
||
| % Define data structure for data channels. | ||
| channel_struct = struct( ... | ||
| 'native_channel_name', 'dummy', ... | ||
| 'custom_channel_name', 'dummy', ... | ||
| 'native_order', 0, ... | ||
| 'custom_order', 0, ... | ||
| 'board_stream', 0, ... | ||
| 'chip_channel', 0, ... | ||
| 'port_name', 'dummy', ... | ||
| 'port_prefix', 'dummy', ... | ||
| 'port_number', 0, ... | ||
| 'electrode_impedance_magnitude', 0, ... | ||
| 'electrode_impedance_phase', 0 ); | ||
|
|
||
| empty_channel_struct = struct( ... | ||
| 'native_channel_name', {}, ... | ||
| 'custom_channel_name', {}, ... | ||
| 'native_order', {}, ... | ||
| 'custom_order', {}, ... | ||
| 'board_stream', {}, ... | ||
| 'chip_channel', {}, ... | ||
| 'port_name', {}, ... | ||
| 'port_prefix', {}, ... | ||
| 'port_number', {}, ... | ||
| 'electrode_impedance_magnitude', {}, ... | ||
| 'electrode_impedance_phase', {} ); | ||
|
|
||
|
|
||
| header.amplifier_channels = channel_struct; | ||
| header.amplifier_channels.native_channel_name = 'A-000'; | ||
| header.aux_input_channels = empty_channel_struct; | ||
| header.supply_voltage_channels = empty_channel_struct; | ||
| header.board_adc_channels = channel_struct; | ||
| header.board_adc_channels.native_channel_name = 'ANALOG-IN-00'; | ||
| header.board_dig_in_channels = empty_channel_struct; | ||
| header.board_dig_out_channels = empty_channel_struct; | ||
| header.num_temp_sensor_channels = 0; | ||
|
|
||
|
|
||
| % Create a dummy file | ||
| filename = [tempname '.rhd']; | ||
|
|
||
| % Ensure cleanup on failure | ||
| cleanupObj = onCleanup(@() delete(filename)); | ||
|
|
||
| fid = fopen(filename, 'w'); | ||
| testCase.assertNotEqual(fid, -1, 'Could not open test file.'); | ||
|
|
||
| fwrite(fid, zeros(1, header.fileinfo.headersize), 'uint8'); | ||
|
|
||
| % Calculate bytes per block | ||
| % Intan_RHD2000_blockinfo requires filename, but mostly uses header logic or reads file if header incomplete? | ||
| % We'll assume it works as in the original script. | ||
| [~, bytes_per_block] = ndr.format.intan.Intan_RHD2000_blockinfo(filename, header); | ||
|
|
||
| % Write one and a half blocks of data | ||
| dummy_data = zeros(1, floor(bytes_per_block * 1.5), 'uint8'); | ||
| fwrite(fid, dummy_data, 'uint8'); | ||
|
|
||
| fclose(fid); | ||
|
|
||
| s = dir(filename); | ||
| header.fileinfo.filesize = s.bytes; | ||
|
|
||
| % 2. Call ndr.format.intan.read_Intan_RHD2000_datafile to read the file. | ||
| % We expect it to succeed without error. | ||
|
|
||
| [data,total_samples,total_time,blockinfo] = ndr.format.intan.read_Intan_RHD2000_datafile(filename, header, 'adc', 1, 0, inf); | ||
|
|
||
| % 3. Verify results | ||
| testCase.verifyEqual(total_samples, 60, 'total_samples should be 60 (1 complete block).'); | ||
|
|
||
| % If total_samples is correct, we can assume it handled the incomplete block correctly. | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| classdef MockAxonAbf < ndr.reader.axon_abf | ||
| % MOCKAXONABF - Mock class for testing ndr.reader.axon_abf | ||
| % | ||
| % This class inherits from ndr.reader.axon_abf and overrides | ||
| % readchannels_epochsamples to return a predefined time vector. | ||
| % This allows testing samples2times and times2samples without a real file. | ||
|
|
||
| properties | ||
| TimeVector | ||
| end | ||
|
|
||
| methods | ||
| function obj = MockAxonAbf(t_vec) | ||
| % Constructor | ||
| % t_vec: Column vector of time points | ||
| obj.TimeVector = t_vec; | ||
| end | ||
|
|
||
| function data = readchannels_epochsamples(obj, channeltype, channel, epochstreams, epoch_select, s0, s1) | ||
| % Mock implementation returns TimeVector for 'time' channel | ||
|
|
||
| % Check channeltype | ||
| if iscell(channeltype) | ||
| ctype = channeltype{1}; | ||
| else | ||
| ctype = channeltype; | ||
| end | ||
|
|
||
| if strcmp(ctype, 'time') | ||
| % Return the full time vector (ignoring s0, s1 as samples2times calls with -inf, inf) | ||
| % In a real scenario, we would slice, but for testing the gap logic, | ||
| % samples2times requests everything. | ||
| data = obj.TimeVector(:); | ||
| else | ||
| error('MockAxonAbf only supports reading time channel.'); | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| classdef TestAxonAbf < matlab.unittest.TestCase | ||
| % TESTAXONABF - Unit tests for ndr.reader.axon_abf | ||
| % | ||
| % Verifies the functionality of samples2times and times2samples | ||
| % using a mock reader to simulate gaps in recording. | ||
|
|
||
| methods (Test) | ||
| function testSamples2Times(testCase) | ||
| % Define a time vector with a gap (simulating concatenated sweeps) | ||
| % Sweep 1: 0, 0.1, 0.2 | ||
| % Sweep 2: 0.4, 0.5 (Gap of 0.2 between 0.2 and 0.4, assuming dt=0.1) | ||
| t_vec = [0; 0.1; 0.2; 0.4; 0.5]; | ||
| reader = ndr.unittest.reader.MockAxonAbf(t_vec); | ||
|
|
||
| % Test exact samples - Use Column Vector for s | ||
| s = [1; 2; 3; 4; 5]; | ||
| t = reader.samples2times('ai', 1, {}, 1, s); | ||
|
|
||
| % Expect column output | ||
| expected_t = t_vec(s); | ||
|
|
||
| testCase.verifyEqual(t, expected_t, 'AbsTol', 1e-9); | ||
|
|
||
| % Test interpolation within a sweep | ||
| s_interp = 1.5; % Scalar | ||
| t_interp = reader.samples2times('ai', 1, {}, 1, s_interp); | ||
| expected = 0.05; | ||
| testCase.verifyEqual(t_interp, expected, 'AbsTol', 1e-9); | ||
|
|
||
| % Test interpolation across gap | ||
| % s=3 is t=0.2, s=4 is t=0.4 | ||
| % s=3.5 should linearly interpolate to 0.3 | ||
| t_gap = reader.samples2times('ai', 1, {}, 1, 3.5); | ||
| testCase.verifyEqual(t_gap, 0.3, 'AbsTol', 1e-9); | ||
| end | ||
|
|
||
| function testTimes2Samples(testCase) | ||
| t_vec = [0; 0.1; 0.2; 0.4; 0.5]; | ||
| reader = ndr.unittest.reader.MockAxonAbf(t_vec); | ||
|
|
||
| % Test exact times - Use Column Vector | ||
| t = [0; 0.1; 0.2; 0.4; 0.5]; | ||
| s = reader.times2samples('ai', 1, {}, 1, t); | ||
| expected_s = [1; 2; 3; 4; 5]; | ||
| % Verify s matches expected_s (column vector) | ||
| testCase.verifyEqual(s, expected_s); | ||
|
|
||
| % Test interpolation & rounding | ||
| % t=0.05 -> s=1.5 -> round to 2 | ||
| s_round = reader.times2samples('ai', 1, {}, 1, 0.05); | ||
| testCase.verifyEqual(s_round, 2); | ||
|
|
||
| % t=0.04 -> s=1.4 -> round to 1 | ||
| s_round = reader.times2samples('ai', 1, {}, 1, 0.04); | ||
| testCase.verifyEqual(s_round, 1); | ||
|
|
||
| % Test across gap | ||
| % t=0.3 -> s=3.5 -> round to 4 | ||
| s_gap = reader.times2samples('ai', 1, {}, 1, 0.3); | ||
| testCase.verifyEqual(s_gap, 4); | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Code Analyzer
To improve performance, use 'isscalar' instead of length comparison. Note