|
6 | 6 | import numpy as np
|
7 | 7 | import getpass
|
8 | 8 | import time
|
9 |
| -import re |
10 |
| -import pandas as pd |
| 9 | +import re |
11 | 10 |
|
12 | 11 | from collections import OrderedDict
|
13 | 12 | from ..openers import Opener
|
@@ -626,53 +625,52 @@ def _serialize_volume_info(volume_info):
|
626 | 625 | f'{key:6s} = {val[0]:.10g} {val[1]:.10g} {val[2]:.10g}\n'.encode('utf-8'))
|
627 | 626 | return b''.join(strings)
|
628 | 627 |
|
| 628 | +def read_stats_file(file_path): |
| 629 | + """Extracts stats from stats files except '*curv.stats' files |
629 | 630 |
|
630 |
| -class StatsFileReader: |
631 |
| - |
632 |
| - @staticmethod |
633 |
| - def read_stats_file(file_path): |
634 |
| - """Extracts stats from stats files except '*curv.stats' files |
635 |
| -
|
636 |
| - Parameters |
637 |
| - ---------- |
638 |
| - file_path: str, required |
639 |
| -
|
640 |
| - Returns |
641 |
| - ------- |
642 |
| -
|
643 |
| - """ |
644 |
| - with open(file_path, 'r') as f: |
645 |
| - for line in f: |
646 |
| - if re.findall(r'ColHeaders .*', line): |
647 |
| - parameters = line.split() |
648 |
| - break |
649 |
| - f.close() |
650 |
| - stats = np.loadtxt(file_path, comments='#', dtype=str) |
651 |
| - df_stats = pd.DataFrame(stats, columns=parameters[2:]) |
652 |
| - df_stats.set_index('StructName', drop=True, inplace=True) |
653 |
| - return df_stats |
654 |
| - |
655 |
| - @staticmethod |
656 |
| - def read_stats_file_both_hemispheres(file_path: str): |
657 |
| - """Extracts stats data of both hemisphers and merges them |
658 |
| -
|
659 |
| - Parameters |
660 |
| - ---------- |
661 |
| - file_path: str, required |
662 |
| - Path of the stats file belong to left (lh) or right(rh) hemisphere |
663 |
| -
|
664 |
| - Returns |
665 |
| - ------- |
666 |
| - df_both_hemispheres: pd.DataFrame |
667 |
| - Stats data of both hemisphers |
668 |
| -
|
669 |
| - Examples |
670 |
| - -------- |
671 |
| - >>> df_stats_a2009 = StatsFileReader.read_stats_file(r'lh.aparc.a2009s.stats') |
672 |
| -
|
673 |
| - """ |
674 |
| - df_left = StatsFileReader.read_stats_file(file_path.replace('rh', 'lh')) |
675 |
| - df_right = StatsFileReader.read_stats_file(file_path.replace('lh', 'rh')) |
676 |
| - df_both_hemispheres = pd.merge(df_left, df_right, suffixes=('_lh', '_rh'), how='outer', left_index=True, |
677 |
| - right_index=True) |
678 |
| - return df_both_hemispheres |
| 631 | + Parameters |
| 632 | + ---------- |
| 633 | + file_path: str, required |
| 634 | +
|
| 635 | + Examples |
| 636 | + -------- |
| 637 | + >>> stats_a2009, column_names = read_stats_file(r'lh.aparc.a2009s.stats') |
| 638 | +
|
| 639 | + """ |
| 640 | + with open(file_path, 'r') as f: |
| 641 | + for line in f: |
| 642 | + if re.findall(r'ColHeaders .*', line): |
| 643 | + parameters = line.split() |
| 644 | + break |
| 645 | + f.close() |
| 646 | + stats = np.loadtxt(file_path, comments='#', dtype=str) |
| 647 | + column_names = parameters[2:] |
| 648 | + return stats, column_names |
| 649 | + |
| 650 | + |
| 651 | +def read_stats_file_both_hemispheres(file_path: str): |
| 652 | + """Extracts stats data of both hemispheres and merges them |
| 653 | +
|
| 654 | + Parameters |
| 655 | + ---------- |
| 656 | + file_path: str, required |
| 657 | + Path of the stats file belong to left (lh) or right(rh) hemisphere |
| 658 | +
|
| 659 | + Returns |
| 660 | + ------- |
| 661 | + stats_both_hemispheres: ndarray |
| 662 | + Stats data of both hemisphers |
| 663 | + column_naems: ndarray |
| 664 | + Name of columns |
| 665 | +
|
| 666 | + Examples |
| 667 | + -------- |
| 668 | + >>> stats_a2009, column_names = read_stats_file_both_hemispheres(r'lh.aparc.a2009s.stats') |
| 669 | +
|
| 670 | + """ |
| 671 | + stats_left, columns_left = read_stats_file(file_path.replace('rh', 'lh')) |
| 672 | + stats_right, columns_right = read_stats_file(file_path.replace('lh', 'rh')) |
| 673 | + stats_both_hemispheres = np.concatenate((stats_left, stats_right[:, 1:]), axis=1) |
| 674 | + column_names = [col_name + '_left' for col_name in columns_left] + [col_name + '_right' for col_name in |
| 675 | + columns_right[1:]] |
| 676 | + return stats_both_hemispheres, column_names |
0 commit comments