def test_init_args_progressbar_default(): test_args = ['etude.py', '--no-metrics'] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.progressbar_output == 'stderr' assert not args.progressbar_disabled assert args.progressbar_file == sys.stderr
def test_count_ref_set_csv_out(): presaved_file = 'tests/data/i2b2_2016_track-1_csv_out.csv' try: tmp_descriptor, tmp_file = tempfile.mkstemp() os.close(tmp_descriptor) command_line_args = [ 'etude.py', '--reference-input', 'tests/data/i2b2_2016_track-1_reference', '--reference-config', 'config/i2b2_2016_track-1.conf', '--csv-out', tmp_file, '--by-file', '--by-type', '--by-file-and-type', '--by-type-and-file', '--print-counts', '--no-metrics' ] with patch.object(sys, 'argv', command_line_args): args = etude.init_args() namespaces , document_data , patterns = \ args_and_configs.process_config( config_file = args.reference_config , score_key = args.score_key , score_values = args.score_values ) import csv with open(presaved_file, 'r') as fp: reloaded_csv = fp.read() etude.count_ref_set(this_ns=namespaces, this_dd=document_data, this_patterns=patterns, this_folder=args.reference_input, args=args, file_prefix=args.file_prefix, file_suffix=args.file_suffix[0]) with open(tmp_file, 'r') as fp: new_csv = fp.read() assert new_csv == reloaded_csv finally: os.remove(tmp_file)
def test_init_args_file_suffix_duplex(): test_args = ['etude.py', '--no-metrics', '--file-suffix', '.xyz', '.abc'] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert len(args.file_suffix) == 2 assert args.file_suffix[0] == '.xyz' assert args.file_suffix[1] == '.abc'
def test_count_ref_set_default(capsys): presaved_file = 'tests/data/i2b2_2016_track-1_reference_out/0005_gs.xml' command_line_args = [ 'etude.py', '--reference-input', 'tests/data/i2b2_2016_track-1_reference', '--reference-config', 'config/i2b2_2016_track-1.conf', '--print-counts', '--no-metrics' ] with patch.object(sys, 'argv', command_line_args): args = etude.init_args() namespaces , document_data , patterns = \ args_and_configs.process_config( config_file = args.reference_config , score_key = args.score_key , score_values = args.score_values ) with open(presaved_file, 'r') as fp: reloaded_json = json.load(fp) etude.count_ref_set(this_ns=namespaces, this_dd=document_data, this_patterns=patterns, this_folder=args.reference_input, args=args, file_prefix=args.file_prefix, file_suffix=args.file_suffix[0]) default_out, err = capsys.readouterr() expected_values = [['counts', 'n'], ['Total', '482']] for expected_values in expected_values: print(args.delim.join('{}'.format(m) for m in expected_values)) expected_out, err = capsys.readouterr() default_out = default_out.strip() expected_out = expected_out.strip() assert default_out == expected_out
def test_init_args_progressbar_none(): test_args = ['etude.py', '--no-metrics', '--progressbar-output', 'none'] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.progressbar_output == 'none' assert args.progressbar_disabled assert args.progressbar_file is None
def test_init_args_corpus_out(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '--corpus-out', '/tmp/corpusOut' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.corpus_out == '/tmp/corpusOut'
def test_init_args_verbose(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '--verbose' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.verbose == True
def test_init_args_default_f_beta_values(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.f_beta_values == []
def test_init_args_skip_chars_and_whitespace_flag(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '--ignore-whitespace', '--skip-chars', r'[z\|]' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.ignore_whitespace == True assert args.skip_chars == r'[z\|]'
def test_init_args_heed_whitespace(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '--heed-whitespace' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.ignore_whitespace == False assert args.skip_chars == None
def test_init_args_f1_and_1(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '-m', 'Precision', 'F1', 'Recall', '--f-beta-values', '1' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert 'F' not in args.metrics_list assert ['Precision', 'F1', 'Recall'] == args.metrics_list assert args.f_beta_values == ['1']
def test_default_init_args(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.reference_input == '/tmp/reference' assert args.test_input == '/tmp/test' assert args.corpus_out == None assert args.verbose == False
def test_init_args_multiple_f_beta_values(): test_args = [ 'etude.py', '--reference-input', '/tmp/reference', '--test-input', '/tmp/test', '-m', 'Precision', 'F', 'Recall', '--f-beta-values', '4', '0.34' ] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert 'F' not in args.metrics_list assert ['Precision', 'F4', 'F0.34', 'Recall'] == args.metrics_list ## Note: the beta values are reversed because of how we need ## to insert them into the metrics_list assert args.f_beta_values == ['0.34', '4']
def test_count_ref_set_by_type_and_file(capsys): presaved_file = 'tests/data/i2b2_2016_track-1_reference_out/0005_gs.xml' command_line_args = [ 'etude.py', '--reference-input', 'tests/data/i2b2_2016_track-1_reference', '--reference-config', 'config/i2b2_2016_track-1.conf', '--by-type', '--by-file', '--print-counts', '--no-metrics' ] with patch.object(sys, 'argv', command_line_args): args = etude.init_args() namespaces , document_data , patterns = \ args_and_configs.process_config( config_file = args.reference_config , score_key = args.score_key , score_values = args.score_values ) with open(presaved_file, 'r') as fp: reloaded_json = json.load(fp) etude.count_ref_set(this_ns=namespaces, this_dd=document_data, this_patterns=patterns, this_folder=args.reference_input, args=args, file_prefix=args.file_prefix, file_suffix=args.file_suffix[0]) default_out, err = capsys.readouterr() expected_values = [['counts', 'n'], ['Total', '482'], ['0005_gs.xml', '36'], ['0016_gs.xml', '54'], ['0267_gs.xml', '63'], ['0273_gs.xml', '35'], ['0389_gs.xml', '40'], ['0475_gs.xml', '46'], ['0617_gs.xml', '38'], ['0709_gs.xml', '45'], ['0982_gs.xml', '100'], ['0992_gs.xml', '25'], ['Age', '92'], ['DateTime', '124'], ['HCUnit', '76'], ['OtherGeo', '5'], ['OtherID', '7'], ['OtherOrg', '21'], ['Patient', '19'], ['PhoneFax', '6'], ['Provider', '64'], ['SSN', '0'], ['StateCountry', '33'], ['StreetCity', '29'], ['Zip', '4'], ['eAddress', '2']] for expected_values in expected_values: print(args.delim.join('{}'.format(m) for m in expected_values)) expected_out, err = capsys.readouterr() default_out = default_out.strip() expected_out = expected_out.strip() assert default_out == expected_out
def test_init_args_file_suffix_simplex_dash(): test_args = ['etude.py', '--no-metrics', '--file-suffix', ' -03.xyz'] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert len(args.file_suffix) == 1 assert args.file_suffix[0] == '-03.xyz'
def test_init_args_file_prefix_simplex(): test_args = ['etude.py', '--no-metrics', '--file-prefix', '.xyz'] with patch.object(sys, 'argv', test_args): args = etude.init_args() assert args.file_prefix == '.xyz'