From 37a4433aedf709b325a4bf7a15dbafd5326d0631 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Sat, 13 Apr 2024 10:31:26 +0200 Subject: [PATCH] move tests --- .../test_transformers_compute.m | 2 +- .../test_transformers_munge.m | 111 +++++++++++++- .../test_transformers_munge_multi.m | 136 ------------------ 3 files changed, 108 insertions(+), 141 deletions(-) delete mode 100644 tests/test_transformers/test_transformers_munge_multi.m diff --git a/tests/test_transformers/test_transformers_compute.m b/tests/test_transformers/test_transformers_compute.m index dfc41d4c..1b1232a3 100644 --- a/tests/test_transformers/test_transformers_compute.m +++ b/tests/test_transformers/test_transformers_compute.m @@ -16,7 +16,7 @@ function write_definition(input, output, trans, stack) test_name = stack.name; - % write_test_definition_to_file(input, output, trans, test_name, 'compute'); + % write_test_definition_to_file(input, output, trans, test_name, 'compute'); end %% multi step diff --git a/tests/test_transformers/test_transformers_munge.m b/tests/test_transformers/test_transformers_munge.m index b23e3823..29be2bed 100644 --- a/tests/test_transformers/test_transformers_munge.m +++ b/tests/test_transformers/test_transformers_munge.m @@ -22,10 +22,6 @@ function write_definition(input, output, trans, stack, suffix) end -%% single step - -% ordered alphabetically - function test_Assign_with_target_attribute() transformers = struct('Name', 'Assign', ... @@ -145,6 +141,113 @@ function test_Replace_string_in_numeric_output() end +function test_multi_touch() + + % GIVEN + tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-TouchBefore_events.tsv'); + tsv_content = bids.util.tsvread(tsvFile); + + transformers{1} = struct('Name', 'Threshold', ... + 'Input', 'duration', ... + 'Binarize', true, ... + 'Output', 'tmp'); + transformers{2} = struct('Name', 'Replace', ... + 'Input', 'tmp', ... + 'Output', 'duration', ... + 'Attribute', 'duration', ... + 'Replace', struct('key', 1, ... + 'value', 1)); + transformers{3} = struct('Name', 'Delete', ... + 'Input', {{'tmp'}}); + + % WHEN + [new_content, json] = bids.transformers(transformers, tsv_content); + + % THEN + % TODO assert whole content + assertEqual(fieldnames(tsv_content), fieldnames(new_content)); + assertEqual(json, struct('Transformer', ['bids-matlab_' bids.internal.get_version], ... + 'Instructions', {transformers})); + +end + +function test_multi_combine_columns() + + % GIVEN + tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-FaceRepetitionBefore_events.tsv'); + tsv_content = bids.util.tsvread(tsvFile); + + transformers{1} = struct('Name', 'Filter', ... + 'Input', 'face_type', ... + 'Query', 'face_type==famous', ... + 'Output', 'Famous'); + transformers{2} = struct('Name', 'Filter', ... + 'Input', 'repetition_type', ... + 'Query', 'repetition_type==1', ... + 'Output', 'FirstRep'); + transformers{3} = struct('Name', 'And', ... + 'Input', {{'Famous', 'FirstRep'}}, ... + 'Output', 'tmp'); + transformers{4} = struct('Name', 'Replace', ... + 'Input', 'tmp', ... + 'Output', 'trial_type', ... + 'Replace', struct('key', 'tmp_1', ... + 'value', 'FamousFirstRep')); + transformers{5} = struct('Name', 'Delete', ... + 'Input', {{'tmp', 'Famous', 'FirstRep'}}); + + % WHEN + data = tsv_content; + new_content = bids.transformers(transformers, data); + + % THEN + % TODO assert whole content + assertEqual(fieldnames(tsv_content), fieldnames(new_content)); + assertEqual(unique(new_content.trial_type), {'face'}); + +end + +function test_multi_complex_filter_with_and() + + %% GIVEN + tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-FaceRepetitionBefore_events.tsv'); + tsv_content = bids.util.tsvread(tsvFile); + + transformers{1} = struct('Name', 'Filter', ... + 'Input', 'face_type', ... + 'Query', 'face_type==famous', ... + 'Output', 'Famous'); + transformers{2} = struct('Name', 'Filter', ... + 'Input', 'repetition_type', ... + 'Query', 'repetition_type==1', ... + 'Output', 'FirstRep'); + + % WHEN + data = tsv_content; + new_content = bids.transformers(transformers, data); + + % THEN + assert(all(ismember({'Famous'; 'FirstRep'}, fieldnames(new_content)))); + assertEqual(sum(strcmp(new_content.Famous, 'famous')), 52); + + if ~isempty(which('nansum')) + assertEqual(bids.internal.nansum(new_content.FirstRep), 52); + end + + %% GIVEN + transformers{3} = struct('Name', 'And', ... + 'Input', {{'Famous', 'FirstRep'}}, ... + 'Output', 'FamousFirstRep'); + + % WHEN + data = tsv_content; + new_content = bids.transformers(transformers, data); + + % THEN + assertEqual(sum(new_content.FamousFirstRep), 26); + +end + %% Helper functions function cfg = set_up() diff --git a/tests/test_transformers/test_transformers_munge_multi.m b/tests/test_transformers/test_transformers_munge_multi.m deleted file mode 100644 index 3522894e..00000000 --- a/tests/test_transformers/test_transformers_munge_multi.m +++ /dev/null @@ -1,136 +0,0 @@ -function test_suite = test_transformers_munge_multi %#ok<*STOUT> - % - - % (C) Copyright 2022 Remi Gau - - try % assignment of 'localfunctions' is necessary in Matlab >= 2016 - test_functions = localfunctions(); %#ok<*NASGU> - catch % no problem; early Matlab versions can use initTestSuite fine - end - - initTestSuite; - -end - -%% MUNGE - -%% multi step - -function test_multi_touch() - - % GIVEN - tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-TouchBefore_events.tsv'); - tsv_content = bids.util.tsvread(tsvFile); - - transformers{1} = struct('Name', 'Threshold', ... - 'Input', 'duration', ... - 'Binarize', true, ... - 'Output', 'tmp'); - transformers{2} = struct('Name', 'Replace', ... - 'Input', 'tmp', ... - 'Output', 'duration', ... - 'Attribute', 'duration', ... - 'Replace', struct('key', 1, ... - 'value', 1)); - transformers{3} = struct('Name', 'Delete', ... - 'Input', {{'tmp'}}); - - % WHEN - [new_content, json] = bids.transformers(transformers, tsv_content); - - % THEN - % TODO assert whole content - assertEqual(fieldnames(tsv_content), fieldnames(new_content)); - assertEqual(json, struct('Transformer', ['bids-matlab_' bids.internal.get_version], ... - 'Instructions', {transformers})); - -end - -function test_multi_combine_columns() - - % GIVEN - tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-FaceRepetitionBefore_events.tsv'); - tsv_content = bids.util.tsvread(tsvFile); - - transformers{1} = struct('Name', 'Filter', ... - 'Input', 'face_type', ... - 'Query', 'face_type==famous', ... - 'Output', 'Famous'); - transformers{2} = struct('Name', 'Filter', ... - 'Input', 'repetition_type', ... - 'Query', 'repetition_type==1', ... - 'Output', 'FirstRep'); - transformers{3} = struct('Name', 'And', ... - 'Input', {{'Famous', 'FirstRep'}}, ... - 'Output', 'tmp'); - transformers{4} = struct('Name', 'Replace', ... - 'Input', 'tmp', ... - 'Output', 'trial_type', ... - 'Replace', struct('key', 'tmp_1', ... - 'value', 'FamousFirstRep')); - transformers{5} = struct('Name', 'Delete', ... - 'Input', {{'tmp', 'Famous', 'FirstRep'}}); - - % WHEN - data = tsv_content; - new_content = bids.transformers(transformers, data); - - % THEN - % TODO assert whole content - assertEqual(fieldnames(tsv_content), fieldnames(new_content)); - assertEqual(unique(new_content.trial_type), {'face'}); - -end - -function test_multi_complex_filter_with_and() - - %% GIVEN - tsvFile = fullfile(dummy_data_dir(), 'sub-01_task-FaceRepetitionBefore_events.tsv'); - tsv_content = bids.util.tsvread(tsvFile); - - transformers{1} = struct('Name', 'Filter', ... - 'Input', 'face_type', ... - 'Query', 'face_type==famous', ... - 'Output', 'Famous'); - transformers{2} = struct('Name', 'Filter', ... - 'Input', 'repetition_type', ... - 'Query', 'repetition_type==1', ... - 'Output', 'FirstRep'); - - % WHEN - data = tsv_content; - new_content = bids.transformers(transformers, data); - - % THEN - assert(all(ismember({'Famous'; 'FirstRep'}, fieldnames(new_content)))); - assertEqual(sum(strcmp(new_content.Famous, 'famous')), 52); - - if ~isempty(which('nansum')) - assertEqual(bids.internal.nansum(new_content.FirstRep), 52); - end - - %% GIVEN - transformers{3} = struct('Name', 'And', ... - 'Input', {{'Famous', 'FirstRep'}}, ... - 'Output', 'FamousFirstRep'); - - % WHEN - data = tsv_content; - new_content = bids.transformers(transformers, data); - - % THEN - assertEqual(sum(new_content.FamousFirstRep), 26); - -end - -%% Helper functions - -function cfg = set_up() - cfg = set_test_cfg(); - cfg.this_path = fileparts(mfilename('fullpath')); -end - -function value = dummy_data_dir() - cfg = set_up(); - value = fullfile(cfg.this_path, '..', 'data', 'tsv_files'); -end