def test_stats_table_to_numpy_raises_on_invalied_type_count(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'aseg.stats') stats = bl.stat(stats_file) types = [np.string_, np.string_] # wrong number of types! with pytest.raises(ValueError) as exc_info: numpy_data = st.stats_table_to_numpy(stats, types) assert 'Length of type_list' in str(exc_info.value) assert 'must match number of' in str(exc_info.value)
def test_measures_to_numpy(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'aseg.stats') stats = bl.stat(stats_file) numpy_measures, measure_names = st.measures_to_numpy(stats['measures']) assert numpy_measures.shape == (22, ) assert numpy_measures.shape[0] == len(stats['measures']) assert measure_names[0] == ('BrainSeg', 'BrainSegVol') assert numpy_measures[0] == pytest.approx(1243340.0, 0.01) assert np.dtype(numpy_measures[0]) == np.float_
def test_header_line_raises_on_both_missing(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) del stats['table_meta_data']['column_info_'] del stats['table_meta_data']['ColHeaders'] with pytest.raises(ValueError) as exc_info: hdr_string = st._header_line(stats['table_meta_data']) assert 'Could not determine header line' in str(exc_info.value)
def test_header_line(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) hdr_string = st._header_line(stats['table_meta_data']) assert hdr_string == '\t'.join([ 'StructName', 'NumVert', 'SurfArea', 'GrayVol', 'ThickAvg', 'ThickStd', 'MeanCurv', 'GausCurv', 'FoldInd', 'CurvInd' ])
def test_sorted_header_indices(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) header_indices = st._sorted_header_indices(stats['table_meta_data']) assert len(header_indices) == 10 assert header_indices[0] == '1' assert header_indices[5] == '6' assert header_indices[9] == '10'
def test_stats_table_to_numpy_aseg_stats(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'aseg.stats') stats = bl.stat(stats_file) types = st.typelist_for_aseg_stats() numpy_data = st.stats_table_to_numpy(stats, types) assert len(numpy_data) == 10 # 10 columns assert 'Index' in numpy_data assert 'SegId' in numpy_data for column_name in numpy_data: assert len(numpy_data[column_name]) == 45 # 45 rows
def test_stats_table_to_numpy_aparc_dktatlas_stats(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) types = st.typelist_for_aparc_atlas_stats() numpy_data = st.stats_table_to_numpy(stats, types) assert len(numpy_data) == 10 # 10 columns assert 'StructName' in numpy_data assert 'NumVert' in numpy_data for column_name in numpy_data: assert len(numpy_data[column_name]) == 31
def test_header_line_missing_column_info_warns(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) del stats['table_meta_data']['column_info_'] with pytest.warns(UserWarning, match='Stats data is missing some header data'): hdr_string = st._header_line(stats['table_meta_data']) assert hdr_string == '\t'.join([ 'StructName', 'NumVert', 'SurfArea', 'GrayVol', 'ThickAvg', 'ThickStd', 'MeanCurv', 'GausCurv', 'FoldInd', 'CurvInd' ])
def test_measures_to_numpy_subset(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'aseg.stats') stats = bl.stat(stats_file) requested_measures = [('BrainSeg', 'BrainSegVol'), ('SurfaceHoles', 'SurfaceHoles')] numpy_measures, measure_names = st.measures_to_numpy( stats['measures'], requested_measures=requested_measures) assert numpy_measures.shape == (2, ) assert numpy_measures.shape[0] == len(requested_measures) assert measure_names[0] == ('BrainSeg', 'BrainSegVol') assert measure_names[1] == ('SurfaceHoles', 'SurfaceHoles') assert numpy_measures[0] == pytest.approx(1243340.0, 0.01) assert numpy_measures[1] == pytest.approx(29, 0.01)
def test_header_line_does_not_raise_on_broken_col_headers(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) stats['table_meta_data'][ 'ColHeaders'] = 1 # ruin data: cannot split an int with pytest.warns(UserWarning, match='Stats data is missing some header data'): hdr_string = st._header_line(stats['table_meta_data']) assert hdr_string == '\t'.join([ 'StructName', 'NumVert', 'SurfArea', 'GrayVol', 'ThickAvg', 'ThickStd', 'MeanCurv', 'GausCurv', 'FoldInd', 'CurvInd' ])
def test_header_line_inconsistent_warns(): stats_file = os.path.join(TEST_DATA_DIR, 'subject1', 'stats', 'lh.aparc.DKTatlas.stats') stats = bl.stat(stats_file) stats['table_meta_data'][ 'ColHeaders'] = 'StructName NumVertBROKEN SurfArea GrayVol ThickAvg ThickStd MeanCurv GausCurv FoldInd CurvInd' # mess with data, add 'BROKEN' to 2nd header column with pytest.warns( UserWarning, match= 'Stats data regarding table header is inconsistent between ColHeaders and TableCol->ColHeader entries' ): hdr_string = st._header_line(stats['table_meta_data']) assert hdr_string == '\t'.join([ 'StructName', 'NumVert', 'SurfArea', 'GrayVol', 'ThickAvg', 'ThickStd', 'MeanCurv', 'GausCurv', 'FoldInd', 'CurvInd' ])