|
@@ -60,19 +60,35 @@ from freesurfer_stats.version import __version__
|
|
|
|
|
|
def _get_filepath_or_buffer(
|
|
|
path: typing.Union[str, pathlib.Path]
|
|
|
-) -> typing.Tuple[typing.Any, bool]:
|
|
|
+) -> typing.Tuple[
|
|
|
+ typing.Any, bool
|
|
|
+]:
|
|
|
+
|
|
|
+
|
|
|
+ if not hasattr(pandas.io.common, "get_filepath_or_buffer"):
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ io_handle = pandas.io.common.get_handle(path, "r")
|
|
|
+ return io_handle.handle, True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
(path_or_buffer, _, _, *instructions) = pandas.io.common.get_filepath_or_buffer(
|
|
|
path
|
|
|
)
|
|
|
- if instructions:
|
|
|
+ if instructions:
|
|
|
|
|
|
assert len(instructions) == 1, instructions
|
|
|
should_close = instructions[0]
|
|
|
- else:
|
|
|
+ else:
|
|
|
|
|
|
should_close = hasattr(path_or_buffer, "close")
|
|
|
return path_or_buffer, should_close
|
|
@@ -220,13 +236,15 @@ class CorticalParcellationStats:
|
|
|
def read(cls, path: typing.Union[str, pathlib.Path]) -> "CorticalParcellationStats":
|
|
|
path_or_buffer, should_close = _get_filepath_or_buffer(path)
|
|
|
stats = cls()
|
|
|
- try:
|
|
|
- if hasattr(path_or_buffer, "readline"):
|
|
|
-
|
|
|
+ try:
|
|
|
+
|
|
|
+
|
|
|
+ if isinstance(path_or_buffer, io.TextIOWrapper):
|
|
|
+ stats._read(path_or_buffer)
|
|
|
+ elif hasattr(path_or_buffer, "readline"):
|
|
|
stats._read(io.TextIOWrapper(path_or_buffer))
|
|
|
else:
|
|
|
with open(path_or_buffer, "r") as stream:
|
|
|
-
|
|
|
stats._read(stream)
|
|
|
finally:
|
|
|
if should_close:
|