-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexcel_management.py
216 lines (169 loc) · 6.86 KB
/
excel_management.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import xlsxwriter
import pandas as pd
import numpy as np
from os import remove as file_remove
from BIAC_tools import send_mail, isempty
from computer_nav import make_temppath, glob_remote, checkfile_exists_remote, remove_remote
"""
Created by Jacques Stout
Set of helper functions
Currently has:
make every float an int
Calculate the average in each cell of values from multiple excel files (found in same folder)
set up the excel files for connectomes and grouping
"""
def connectomes_to_excel(connectome, index_to_struct, output_path, overwrite=False, verbose=False, sftp=None):
#df = pd.read_excel(ROI_excel, sheet_name='Sheet1')
#structure = df['Structure']
if checkfile_exists_remote(output_path, sftp=sftp):
if overwrite:
remove_remote(output_path, sftp=sftp)
else:
if verbose:
print(f'Connectome already saved at {output_path}')
return
if sftp is None:
output_tpath = output_path
else:
output_tpath = make_temppath(output_path)
workbook = xlsxwriter.Workbook(output_tpath)
worksheet = workbook.add_worksheet()
for num in np.arange(1, np.shape(connectome)[0]+1):
worksheet.write(0, num, index_to_struct[num])
worksheet.write(num, 0, index_to_struct[num])
row=0
for col, data in enumerate(connectome):
worksheet.write_column(row+1, col+1, data)
workbook.close()
if sftp is not None:
sftp.put(output_tpath, output_path)
file_remove(output_tpath)
if verbose:
print(f'Saved connectome at {output_path}')
return
def grouping_to_excel(grouping, index_to_struct, output_path, overwrite=False, verbose=False, sftp=None):
if checkfile_exists_remote(output_path):
if overwrite:
remove_remote(output_path, sftp)
else:
if verbose:
print(f'Grouping already saved at {output_path}')
return
if sftp is None:
output_tpath = output_path
else:
output_tpath = make_temppath(output_path)
workbook = xlsxwriter.Workbook(output_tpath)
worksheet = workbook.add_worksheet()
for num in np.arange(1, np.shape(grouping)[0]):
worksheet.write(0, num, index_to_struct[num])
worksheet.write(num, 0, index_to_struct[num])
row = 0
for i in np.arange(np.shape(grouping)[0]):
for j in np.arange(np.shape(grouping)[1]):
worksheet.write(i + 1, j + 1, str(grouping[i, j]))
workbook.close()
if sftp is not None:
sftp.put(output_tpath, output_path)
file_remove(output_tpath)
if verbose:
print(f'Saved grouping at {output_path}')
return
def round_array(array,rounder):
newarray = np.zeros(np.shape(array))
if np.size(np.shape(array)) == 2:
for i in np.arange(np.shape(array)[0]):
for j in np.arange(np.shape(array)[1]):
newarray[i,j] = round(array[i,j],rounder)
return(newarray)
def excel_average(files, file_outpath):
workbook = xlsxwriter.Workbook(file_outpath)
worksheet = workbook.add_worksheet()
for file in files:
df = pd.read_excel(file, sheet_name='Sheet1')
data = df.values
row = 0
if 'datasum' not in locals():
worksheet.write_row(0, 1, data[:166, 0])
worksheet.write_column(1, 0, data[:166, 0])
datasum = data[:, 1:]
else:
datasum = datasum + data[:, 1:]
numfiles = np.size(files)
dataavg = datasum / numfiles
dataavg = round_array(dataavg, 1)
for i in np.arange(np.shape(dataavg)[0]):
worksheet.write_row(i+1, 1, dataavg[i,:])
workbook.close()
#for col, data in enumerate(connectome):
# worksheet.write(row + 1, col + 1, data)
def M_grouping_excel_save(M,grouping,M_path, grouping_path, index_to_struct, verbose=False, sftp=None):
matrix_sl = np.empty(np.shape(M), dtype=object)
for i in np.arange(np.shape(matrix_sl)[0]):
for j in np.arange(np.shape(matrix_sl)[1]):
matrix_sl[i, j] = []
for key in grouping.keys():
matrix_sl[key] = grouping[key]
matrix_sl[tuple(np.flip(key))] = grouping[key]
M = np.delete(M, 0, 0)
M = np.delete(M, 0, 1)
matrix_sl = np.delete(matrix_sl, 0, 0)
matrix_sl = np.delete(matrix_sl, 0, 1)
if sftp is None:
M_tpath = M_path
grouping_tpath = grouping_path
else:
M_tpath = make_temppath(M_path)
grouping_tpath = make_temppath(grouping_path)
connectomes_to_excel(M, index_to_struct, M_tpath)
grouping_to_excel(matrix_sl, index_to_struct, grouping_tpath)
if sftp is not None:
sftp.put(M_tpath, M_path)
sftp.put(grouping_tpath, grouping_path)
file_remove(M_tpath)
file_remove(grouping_tpath)
if verbose:
txt = (f"The excelfile for connectome and grouping were saved at {M_path} and {grouping_path}")
send_mail(txt, subject="Excel save")
print(txt)
def extract_grouping(grouping_path, index_to_struct, shape=None, verbose=False, sftp=None):
#grouping_array = np.empty(shape, dtype=object)
grouping_newdic = {}
if sftp is not None:
grouping_paths = glob_remote(grouping_path, sftp)
if np.size(grouping_paths == 1):
grouping_path = grouping_paths[0]
sftp.get(grouping_path, make_temppath(grouping_path))
grouping_frame = pd.read_excel(make_temppath(grouping_path))
file_remove(make_temppath(grouping_path))
elif np.size(grouping_paths > 1):
raise Exception('too many grouping files???')
else:
raise Exception('could not find grouping_path')
else:
grouping_frame = pd.read_excel(grouping_path)
if shape is None:
shape = list(grouping_frame.shape)
shape[0] = shape[0]+1
shape = tuple(shape)
for i in np.arange(shape[0]-1):
for j in np.arange(shape[1]-1):
liststring = grouping_frame.iloc[i, j+1]
liststring = liststring.replace('[','')
liststring = liststring.replace(']','')
liststring = (liststring.split(','))
#print(liststring[-1])
#liststring.pop(-1)
if liststring[0] != '' and liststring[0] != ' ':
try:
int(liststring[-1])
except ValueError:
liststring = liststring[:-1]
liststring = [int(i) for i in liststring]
else:
liststring = []
grouping_newdic[i + 1, j + 1] = liststring
return grouping_newdic
#for key in grouping.keys():
# matrix_sl[key] = grouping[key]
# matrix_sl[tuple(np.flip(key))] = grouping[key]