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

Commit

Permalink
CorticalParcellationStats.read: support pathlib.Path, http, s3 etc. v…
Browse files Browse the repository at this point in the history
…ia pandas.io.common.get_filepath_or_buffer
  • Loading branch information
fphammerle committed May 6, 2020
1 parent 79627eb commit e1cf4fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `CorticalParcellationStats.read` support `pathlib.Path`, `"http://…"`, `"https://…"`, `"s3://…"` etc.
via `pandas.io.common.get_filepath_or_buffer`
(https://github.com/fphammerle/freesurfer-stats/issues/6)

## [1.1.0] - 2020-05-06
### Added
Expand Down
19 changes: 16 additions & 3 deletions freesurfer_stats/__init__.py
Expand Up @@ -47,6 +47,8 @@
"""

import datetime
import io
import pathlib
import re
import typing

Expand Down Expand Up @@ -161,9 +163,20 @@ def _read(self, stream: typing.TextIO) -> None:
.apply(pandas.to_numeric, errors='ignore')

@classmethod
def read(cls, path: str) -> 'CorticalParcellationStats':
def read(cls, path: typing.Union[str, pathlib.Path]) -> "CorticalParcellationStats":
# support http, s3 & gcs
# https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/common.py#L171
path_or_buffer, _, _, should_close = pandas.io.common.get_filepath_or_buffer(
path
)
stats = cls()
with open(path, 'r') as stream:
if hasattr(path_or_buffer, "readline"):
# pylint: disable=protected-access
stats._read(stream)
stats._read(io.TextIOWrapper(path_or_buffer))
else:
with open(path_or_buffer, "r") as stream:
# pylint: disable=protected-access
stats._read(stream)
if should_close:
path_or_buffer.close()
return stats
23 changes: 23 additions & 0 deletions tests/test_cortical_parcellation_stats.py
Expand Up @@ -17,6 +17,7 @@
"""
import datetime
import os
import pathlib

import pandas.util.testing
import pytest
Expand Down Expand Up @@ -166,3 +167,25 @@ def test_read(path, headers, hemisphere, whole_brain_measurements, structural_me
check_dtype=True,
check_names=True,
)


@pytest.mark.parametrize(
"path_str",
[os.path.join(SUBJECTS_DIR, "fabian", "stats", "lh.aparc.DKTatlas.stats.short"),],
)
def test_read_pathlib(path_str: str):
stats_str = CorticalParcellationStats.read(path_str)
stats_pathlib = CorticalParcellationStats.read(pathlib.Path(path_str))
assert stats_str.headers == stats_pathlib.headers


@pytest.mark.parametrize(
"url",
[
"https://raw.githubusercontent.com/fphammerle/freesurfer-stats"
"/master/tests/subjects/fabian/stats/rh.aparc.stats"
],
)
def test_read_https(url: str):
stats = CorticalParcellationStats.read(url)
assert stats.headers["generating_program"] == "mris_anatomical_stats"

0 comments on commit e1cf4fd

Please sign in to comment.