-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrownfunk.py
131 lines (95 loc) · 3 KB
/
brownfunk.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Jacob Brown
# Email [email protected]
# Date: 2020-04-14
# Last Modified: 2020-07-02
""" helpful functions defined by Jacob Brown """
# timer
import time
import shutil
def timer(state="S"):
global startTimer
"""" timer(), timer("e") to start and stop timer """
if state.lower() == "s":
print("timer started")
startTimer = time.time() # start the timer from import
elif state.lower() == "e":
duration = time.time() - startTimer
duration = round(duration)
string = (
"\n..........................\n"
" Time elapsed: {} sec"
"\n..........................\n".format(duration)
)
print(string)
else:
stop("state not found, s or e only.")
# write csv
def write_csv(list_file, path):
""" Write list to csv """
with open(path, "w") as f:
writer = csv.writer(f, delimiter=",")
for i in list_file:
if isinstance(i, list):
writer.writerow(i) # multi-column
else:
writer.writerow([i]) # single column
### save a text file without a new line at the end
def saveTxt(dirfile, listToSave, sep="\n"):
with open(dirfile, "w") as f:
for num, val in enumerate(listToSave):
if num == len(listToSave) - 1:
f.write(val)
else:
f.write(val + sep)
def open_csv(file):
import csv
""" open a csv into a list format """
tmp = [] # initialise the list
with open(file, "r") as f:
reader = csv.reader(f)
for row in reader:
tmp.append(row) # add row to list
return tmp
# sub process wrapper
def subprocessToList(command, returnError=False):
""" wrapper for a subprocess command """
p = subprocess.Popen(
[command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
out, er = p.communicate()
out_string = out.decode()
files = out_string.replace("\t", ":").split("\n")
files.remove("")
if returnError:
return files, er
else:
return files
def groupBy(nestedList):
"""group by within a nested list
index 0 key, index 1 value"""
# group by
data = np.array([int(i[1]) for i in nestedList])
groups = np.array([i[0] for i in nestedList])
groups_un = np.unique(groups)
sums = sc.ndimage.sum(data, groups, groups_un)
# create list and save
store = []
for i in range(0, len(sums)):
store.append([groups_un[i], sums[i]])
return store
def renameFileRecursion(fileIn):
counter = 1
while os.path.exists(fileIn):
file, extention = os.path.splitext(fileIn)
if counter > 1:
file = file[0:len(file)-2]
fileIn = file +'_' + str(counter) + extention
counter = counter + 1
return fileIn
def moveFile(fileFrom, fileTo):
# rename if needed
fileTo = renameFileRecursion(fileTo)
# move
shutil.move(fileFrom, fileTo)a