Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reading freesurfer stats files correctly#1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
simkarwin wants to merge7 commits intonipy:master
base:master
Choose a base branch
Loading
fromsimkarwin:master
Open
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Update io.py
there was a problem with reading the stats files
  • Loading branch information
@simkarwin
simkarwin authoredDec 10, 2022
commitdfed1e38d5e6b221ef681ca6cd721a734c99476c
56 changes: 55 additions & 1 deletionnibabel/freesurfer/io.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
""" Read / write FreeSurfer geometry, morphometry, label, annotation formats
""" Read / write FreeSurfer geometry, morphometry, label,stats,annotation formats
"""
from __future__ import annotations

import warnings
import numpy as np
import getpass
import time
import re
import pandas as pd

from collections import OrderedDict
from ..openers import Opener
Expand DownExpand Up@@ -622,3 +625,54 @@ def _serialize_volume_info(volume_info):
strings.append(
f'{key:6s} = {val[0]:.10g} {val[1]:.10g} {val[2]:.10g}\n'.encode('utf-8'))
return b''.join(strings)


class StatsFileReader:

@staticmethod
def read_stats_file(file_path):
"""Extracts stats from stats files except '*curv.stats' files

Parameters
----------
file_path: str, required

Returns
-------

"""
with open(file_path, 'r') as f:
for line in f:
if re.findall(r'ColHeaders .*', line):
parameters = line.split()
break
f.close()
stats = np.loadtxt(file_path, comments='#', dtype=str)
df_stats = pd.DataFrame(stats, columns=parameters[2:])
df_stats.set_index('StructName', drop=True, inplace=True)
return df_stats

@staticmethod
def read_stats_file_both_hemispheres(file_path: str):
"""Extracts stats data of both hemisphers and merges them

Parameters
----------
file_path: str, required
Path of the stats file belong to left (lh) or right(rh) hemisphere

Returns
-------
df_both_hemispheres: pd.DataFrame
Stats data of both hemisphers

Examples
--------
>>> df_stats_a2009 = StatsFileReader.read_stats_file(r'lh.aparc.a2009s.stats')

"""
df_left = StatsFileReader.read_stats_file(file_path.replace('rh', 'lh'))
df_right = StatsFileReader.read_stats_file(file_path.replace('lh', 'rh'))
df_both_hemispheres = pd.merge(df_left, df_right, suffixes=('_lh', '_rh'), how='outer', left_index=True,
right_index=True)
return df_both_hemispheres

[8]ページ先頭

©2009-2025 Movatter.jp