Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Merge 91700cf into a5aae08
Browse files Browse the repository at this point in the history
  • Loading branch information
fphammerle committed Aug 7, 2019
2 parents a5aae08 + 91700cf commit f192cbe
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 29 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,17 @@ for volume_file in ashs.HippocampalSubfieldsVolumeFile.find('/my/ashs/subjects')
print(volume_file.read_volumes_dataframe())
```

#### Intracranial Volume

```python
from freesurfer_volume_reader import ashs

for volume_file in ashs.IntracranialVolumeFile.find('/my/ashs/subjects'):
print(volume_file.subject)
print(volume_file.read_volume_mm3())
print(volume_file.read_volume_series())
```

### Freesurfer & ASHS

```sh
Expand Down
57 changes: 46 additions & 11 deletions examples/ashs.ipynb
Expand Up @@ -17,10 +17,45 @@
"SUBJECT = 'bert'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Intracranial Volume"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1234560.0 mm^3\n"
]
}
],
"source": [
"from freesurfer_volume_reader.ashs import IntracranialVolumeFile\n",
"\n",
"for volume_file in IntracranialVolumeFile.find(SUBJECTS_DIR):\n",
" if volume_file.subject == SUBJECT:\n",
" print(volume_file.read_volume_mm3(), 'mm^3')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subfield Volumes"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
Expand All @@ -31,7 +66,7 @@
" 'bert_left_corr_nogray_volumes.txt']"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -47,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -132,7 +167,7 @@
"4 PHC 2345.878 bert right nogray"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -145,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -157,7 +192,7 @@
"Name: correction, dtype: int64"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -169,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -282,7 +317,7 @@
"SUB 457.789 457.781 457.780 457.781"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -295,7 +330,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1093,10 +1128,10 @@
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7ff6715a74e0>"
"<matplotlib.axes._subplots.AxesSubplot at 0x7fb6ca200208>"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1133,7 +1168,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions examples/compare_ashs_freesurfer_hipposf.ipynb
Expand Up @@ -115,9 +115,9 @@
],
"source": [
"import os, pandas\n",
"from freesurfer_volume_reader import VolumeFile, ashs, freesurfer\n",
"from freesurfer_volume_reader import SubfieldVolumeFile, ashs, freesurfer\n",
"\n",
"def read_volume_file(volume_file: VolumeFile) -> pandas.DataFrame:\n",
"def read_volume_file(volume_file: SubfieldVolumeFile) -> pandas.DataFrame:\n",
" volume_frame = volume_file.read_volumes_dataframe()\n",
" volume_frame['source_basename'] = os.path.basename(volume_file.absolute_path)\n",
" return volume_frame\n",
Expand Down
21 changes: 12 additions & 9 deletions freesurfer_volume_reader/__init__.py
Expand Up @@ -45,20 +45,23 @@ class VolumeFile(metaclass=abc.ABCMeta):
def absolute_path(self):
raise NotImplementedError()

@abc.abstractmethod
def read_volumes_mm3(self) -> typing.Dict[str, float]:
raise NotImplementedError()

@abc.abstractmethod
def read_volumes_dataframe(self) -> pandas.DataFrame:
raise NotImplementedError()

@classmethod
def find(cls, root_dir_path: str,
filename_regex: typing.Optional[typing.Pattern] = None,
) -> typing.Iterator['VolumeFile']:
) -> typing.Iterator['SubfieldVolumeFile']:
if not filename_regex:
filename_regex = cls.FILENAME_REGEX
for dirpath, _, filenames in os.walk(root_dir_path):
for filename in filter(filename_regex.search, filenames):
yield cls(path=os.path.join(dirpath, filename))


class SubfieldVolumeFile(VolumeFile):

@abc.abstractmethod
def read_volumes_mm3(self) -> typing.Dict[str, float]:
raise NotImplementedError()

@abc.abstractmethod
def read_volumes_dataframe(self) -> pandas.DataFrame:
raise NotImplementedError()
40 changes: 39 additions & 1 deletion freesurfer_volume_reader/ashs.py
Expand Up @@ -9,6 +9,13 @@
>>> print(volume_file.subject, volume_file.hemisphere, volume_file.correction)
>>> print(volume_file.read_volumes_mm3())
>>> print(volume_file.read_volumes_dataframe())
>>> from freesurfer_volume_reader.ashs import IntracranialVolumeFile
>>>
>>> for volume_file in IntracranialVolumeFile('/my/ashs/subjects'):
>>> print(volume_file.subject)
>>> print(volume_file.read_volume_mm3())
>>> print(volume_file.read_volume_series())
"""

import os
Expand All @@ -20,7 +27,38 @@
import freesurfer_volume_reader


class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.VolumeFile):
class IntracranialVolumeFile(freesurfer_volume_reader.VolumeFile):

FILENAME_REGEX = re.compile(r'^(?P<s>\w+)_icv.txt$')

def __init__(self, path: str):
self._absolute_path = os.path.abspath(path)
filename_match = self.FILENAME_REGEX.match(os.path.basename(path))
assert filename_match, self._absolute_path
self.subject = filename_match.groupdict()['s']

@property
def absolute_path(self):
return self._absolute_path

def read_volume_mm3(self) -> float:
with open(self.absolute_path, 'r') as volume_file:
subject, icv = volume_file.read().rstrip().split(' ')
assert subject == self.subject, (subject, self.subject)
return float(icv)

def read_volume_series(self) -> pandas.Series:
return pandas.Series(
data=[self.read_volume_mm3()],
name='volume_mm^3',
index=pandas.Index(
data=[self.subject],
name='subject',
),
)


class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile):

# https://sites.google.com/site/hipposubfields/tutorial#TOC-Viewing-ASHS-Segmentation-Results
FILENAME_PATTERN = r'^(?P<s>\w+)_(?P<h>left|right)' \
Expand Down
2 changes: 1 addition & 1 deletion freesurfer_volume_reader/freesurfer.py
Expand Up @@ -20,7 +20,7 @@
import freesurfer_volume_reader


class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.VolumeFile):
class HippocampalSubfieldsVolumeFile(freesurfer_volume_reader.SubfieldVolumeFile):

# https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields
FILENAME_PATTERN = r'^(?P<h>[lr])h\.hippoSfVolumes' \
Expand Down

0 comments on commit f192cbe

Please sign in to comment.