72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
import openpyxl
|
||
import csv
|
||
import os.path
|
||
import glob
|
||
|
||
|
||
def read_value(patient_dir, atlas, data_type):
|
||
folder_name = os.path.basename(patient_dir)
|
||
csv_file_path = os.path.join(patient_dir, f"{data_type}_{atlas}_result", "result_atlas.csv")
|
||
|
||
result = [folder_name]
|
||
with open(csv_file_path, mode='r') as data_csv_file:
|
||
csv_reader = csv.DictReader(data_csv_file)
|
||
for row in csv_reader:
|
||
mean = float(row["meanValue"])
|
||
result.append(mean)
|
||
|
||
return result
|
||
|
||
|
||
def read_header(patient_dir, atlas, data_type):
|
||
header = ['编号']
|
||
|
||
csv_file_path = os.path.join(patient_dir, f"{data_type}_{atlas}_result", "result_atlas.csv")
|
||
|
||
with open(csv_file_path, mode='r') as data_csv_file:
|
||
csv_reader = csv.DictReader(data_csv_file)
|
||
for row in csv_reader:
|
||
region_name = row["Chinese Name"]
|
||
header.append(region_name)
|
||
|
||
return header
|
||
|
||
|
||
def write_to_work_sheet(work_sheet, patient_list, atlas, data_type):
|
||
if len(patient_list) == 0:
|
||
return
|
||
|
||
header = read_header(patient_list[0], atlas, data_type)
|
||
work_sheet.append(header)
|
||
|
||
for patient_dir in patient_list:
|
||
print(data_type, atlas, patient_dir)
|
||
patient_data = read_value(patient_dir, atlas, data_type)
|
||
|
||
work_sheet.append(patient_data)
|
||
|
||
|
||
def main():
|
||
# 选择导出的数据类型,请尝试增加VOL类型,并修改read_value、read_header函数以支持读取体积
|
||
data_types = ['corr-CBF', 'ATT', 'ACBV', 'CBF1', 'CBF2', 'CBF3', 'CBF4', 'CBF5']
|
||
# 选择导出后的图谱
|
||
atlas_list = ['AnImage_WholeBrain', 'AnImage_BrainLobes', 'AnImage_AAL3']
|
||
# 选择输出的位置
|
||
output_file = r'..\Data\csv-extract.xlsx'
|
||
# 选择输入的数据
|
||
patient_list = glob.glob(r'..\Data\csv-data\*')
|
||
# 创建新的excel workbook
|
||
work_book = openpyxl.Workbook()
|
||
# 删除默认worksheet
|
||
work_book.remove(work_book.active)
|
||
|
||
for data_type in data_types:
|
||
for atlas in atlas_list:
|
||
work_sheet = work_book.create_sheet(title=f'{atlas}_{data_type}')
|
||
write_to_work_sheet(work_sheet, patient_list, atlas, data_type)
|
||
|
||
# 保存workbook到xlsx文件
|
||
work_book.save(output_file)
|
||
|
||
|
||
main()
|