-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea.py
More file actions
166 lines (122 loc) · 3.02 KB
/
area.py
File metadata and controls
166 lines (122 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 10 14:08:43 2025
@author: Leela Srinivasan
Functions to read and manipulate AFNI SurfClust outputs
"""
import os
import sys
import re
import pandas as pd
def get_fs_path(subj, deriv_dir):
"""
Parameters
----------
subj : str
subject with FreeSurfer recon-all run
Returns
-------
subj_fs_dir : str
path to subject specific FreeSurfer folder.
session : str
clinical/altclinical.
"""
for file in os.listdir(deriv_dir):
if subj+'_' in file:
subj_fs_dir=os.path.join(deriv_dir,file)
if 'alt' in file:
session='ses-altclinical'
else:
session='ses-clinical'
return subj_fs_dir, session
def list_to_textfile(path, input_list):
"""
Parameters
----------
path : str
desired path.
input_list : list
desired list.
Returns
-------
None.
"""
with open(path, 'w') as f:
for line in input_list:
f.write(f"{line}\n")
def get_subject_info(f):
"""
Parameters
----------
f : str
path to file.
Returns
-------
subject_info : list
list of subject info.
"""
col=pd.read_csv(f, header=None)[0]
subject_info=[eval(x) for x in col]
return subject_info
def split_row(input_string):
"""
Parameters
----------
input_string : str
AFNI Column header string.
Returns
-------
list
AFNI Column names, split from original header.
"""
return [x for x in re.split(' ', input_string) if x not in ['', ' ']]
def verify_clusters(f):
"""
Parameters
----------
f : str
Path to txt file output.
Returns
-------
bool
True if clusters exist, False if not.
"""
with open(f, "r") as text_file:
contents=text_file.readlines()
if contents[0]=='Empty cluster list.\n':
return False
return True
def afnisummary_to_df(f):
"""
Parameters
----------
f : str
Path to txt file output.
Returns
-------
df : df
df containing table information from AFNI text report.
total_voxels : str
String integer value containing total PVS voxels in the nifti volume.
"""
#Read table, skip opening lines and remove unwanted hashed lines, preserving summary footer
table=pd.read_csv(f, delimiter='\t', skiprows=25)
#Append split strings to new df with corresponding columns
df=pd.DataFrame(columns=split_row(table.columns[0]))
for i in range(0, len(table)):
df.loc[i]=split_row(table.loc[i, :].values[0])
return df
def df_to_csv(df, outname):
"""
Parameters
----------
df : df
df containing table information from AFNI text report.
outname : str
path to output file..
Returns
-------
None.
"""
df.to_csv(outname)