- Notifications
You must be signed in to change notification settings - Fork269
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:masterChoose a base branch fromsimkarwin:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes from1 commit
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
dfed1e3 Update io.py
simkarwine0ea23d Merge pull request #1 from ehkarimi/ehkarimi-stats_file_reader_added
simkarwin84d6e04 Stats file reader functions are added
simkarwin8880a1a Update nibabel/freesurfer/io.py
simkarwin97107b7 Update nibabel/freesurfer/io.py
simkarwinc3c3e04 Merge branch 'master' into master
effigies02334f4 Merge branch 'master' into master
effigiesFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
Update io.py
there was a problem with reading the stats files
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitdfed1e38d5e6b221ef681ca6cd721a734c99476c
There are no files selected for viewing
56 changes: 55 additions & 1 deletionnibabel/freesurfer/io.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| """ Read / write FreeSurfer geometry, morphometry, label,stats,annotation formats | ||
| """ | ||
| from __future__ import annotations | ||
effigies marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| import warnings | ||
| import numpy as np | ||
| import getpass | ||
| import time | ||
| import re | ||
| import pandas as pd | ||
| from collections import OrderedDict | ||
| from ..openers import Opener | ||
| @@ -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 | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.