Browse Source

CorticalParcellationStats: changed type of attribute `whole_brain_measurements` to `pandas.DataFrame`

Fabian Peter Hammerle 4 years ago
parent
commit
42be0083ef
4 changed files with 23 additions and 30 deletions
  1. 6 3
      README.rst
  2. 9 5
      freesurfer_stats/__init__.py
  3. 0 19
      tests/conftest.py
  4. 8 3
      tests/test_cortical_parcellation_stats.py

+ 6 - 3
README.rst

@@ -32,10 +32,13 @@ Usage
     >>> stats.hemisphere
     'left'
     >>> stats.whole_brain_measurements['estimated_total_intracranial_volume_mm^3']
-    1670487.274486
+    0    1.670487e+06
+    Name: estimated_total_intracranial_volume_mm^3, dtype: float64
     >>> stats.whole_brain_measurements['white_surface_total_area_mm^2']
-    98553.0
-    >>> stats.structure_measurements[['Structure Name', 'Surface Area (mm^2)', 'Gray Matter Volume (mm^3)']].head()
+    0    98553
+    Name: white_surface_total_area_mm^2, dtype: int64
+    >>> stats.structure_measurements[['Structure Name', 'Surface Area (mm^2)',
+    ...                               'Gray Matter Volume (mm^3)']].head()
                 Structure Name  Surface Area (mm^2)  Gray Matter Volume (mm^3)
     0  caudalanteriorcingulate                 1472                       4258
     1      caudalmiddlefrontal                 3039                       8239

+ 9 - 5
freesurfer_stats/__init__.py

@@ -15,10 +15,13 @@ https://surfer.nmr.mgh.harvard.edu/
 'mris_anatomical_stats -th3 -mgz -cortex ../label/lh.cortex.label'
 >>> stats.hemisphere
 >>> stats.whole_brain_measurements['estimated_total_intracranial_volume_mm^3']
-1670487.274486
+0    1.670487e+06
+Name: estimated_total_intracranial_volume_mm^3, dtype: float64
 >>> stats.whole_brain_measurements['white_surface_total_area_mm^2']
-98553.0
->>> stats.structure_measurements[['Structure Name', 'Surface Area (mm^2)', 'Gray Matter Volume (mm^3)']].head()
+0    98553
+Name: white_surface_total_area_mm^2, dtype: int64
+>>> stats.structure_measurements[['Structure Name', 'Surface Area (mm^2)',
+...                               'Gray Matter Volume (mm^3)']].head()
             Structure Name  Surface Area (mm^2)  Gray Matter Volume (mm^3)
 0  caudalanteriorcingulate                 1472                       4258
 1      caudalmiddlefrontal                 3039                       8239
@@ -119,7 +122,7 @@ class CorticalParcellationStats:
             == '# Table of FreeSurfer cortical parcellation anatomical statistics'
         assert stream.readline().rstrip() == '#'
         self._read_headers(stream)
-        self.whole_brain_measurements = {}
+        self.whole_brain_measurements = pandas.DataFrame()
         line = self._read_header_line(stream)
         while not line.startswith('NTableCols'):
             key, name, value, unit \
@@ -129,7 +132,8 @@ class CorticalParcellationStats:
             column_name = self._format_column_name(name, unit)
             assert column_name not in self.whole_brain_measurements, \
                 (key, name, column_name, self.whole_brain_measurements)
-            self.whole_brain_measurements[column_name] = float(value)
+            self.whole_brain_measurements[column_name] \
+                = pandas.to_numeric([value], errors='raise')
             line = self._read_header_line(stream)
         columns = self._read_column_attributes(
             int(line[len('NTableCols '):]), stream)

+ 0 - 19
tests/conftest.py

@@ -1,22 +1,3 @@
 import os
 
-import pytest
-
 SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
-
-
-def assert_approx_equal(value_a, value_b):
-    if isinstance(value_a, dict):
-        assert isinstance(value_b, dict)
-        assert value_a.keys() == value_b.keys()
-        for key, sub_a in value_a.items():
-            assert_approx_equal(sub_a, value_b[key])
-    elif isinstance(value_a, tuple):
-        assert len(value_a) == len(value_b)
-        for sub_a, sub_b in zip(value_a, value_b):
-            assert_approx_equal(sub_a, sub_b)
-    elif isinstance(value_a, float):
-        assert isinstance(value_b, float)
-        assert value_a == pytest.approx(value_b)
-    else:
-        assert value_a == value_b

+ 8 - 3
tests/test_cortical_parcellation_stats.py

@@ -4,7 +4,7 @@ import os
 import pandas.util.testing
 import pytest
 
-from conftest import SUBJECTS_DIR, assert_approx_equal
+from conftest import SUBJECTS_DIR
 from freesurfer_stats import CorticalParcellationStats
 
 
@@ -123,8 +123,13 @@ def test_read(path, headers, hemisphere, whole_brain_measurements, structure_mea
     stats = CorticalParcellationStats.read(path)
     assert headers == stats.headers
     assert hemisphere == stats.hemisphere
-    assert_approx_equal(whole_brain_measurements,
-                        stats.whole_brain_measurements)
+    pandas.util.testing.assert_frame_equal(
+        left=pandas.DataFrame([whole_brain_measurements]),
+        right=stats.whole_brain_measurements,
+        check_like=True,  # ignore the order of index & columns
+        check_dtype=True,
+        check_names=True,
+    )
     assert list(stats.structure_measurements.columns) == [
         'structure_name',
         'number_of_vertices',