-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
281 lines (201 loc) · 6.91 KB
/
utilities.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#########################################################################################################
# general python utilities #
#########################################################################################################
def divide_list_per_scalar(list, scalar):
"""
It divide each element in a list by some scalar number
Args:
list: list of elements to be divided
scalar: dividend of every element of the list
Returns:
list with each element divided
"""
return [x / (1.0*scalar) for x in list]
#########################################################################################################
# OS utilities #
#########################################################################################################
def get_full_URI(name, extension, path=None):
"""
Check if the name of the document includes its extension, and if not, it adds it
Then check if the path exists and if not, it creates it.
Args:
path_name: name of the path to be checked
name: name of the document
extension: extension that should have the document
Returns:
name with it extesion and path like "Taggers/unigram.tagger"
"""
if extension is not None:
#Check if extension has "."
if extension[:1] != ".":
extension = "." + extension
#If file name shorter than extension, add it
if len(name) <= len(extension):
name += extension
#Check if it ends with the extension
else:
if name[-len(extension):] != extension:
name += extension
if path is not None:
import os
#if necessary creates the path
if not os.path.exists(path):
os.makedirs(path)
return path + name
else:
return name
def delete_files(path, extension, check_sub_directories=False):
"""
It delete all the existing files in a path with the given extension
Args:
path: where to look
extension: extension of the files that will be deleted
check_sub_directories: if true all subfolders will be inspected
"""
#info: http://stackoverflow.com/questions/7833715/python-deleting-certain-file-extensions
import os
for root, dirs, files in os.walk(path):
#Only check the main folder except if specified by check_sub_directories
if root == path or check_sub_directories:
for currentFile in files:
if currentFile.lower().endswith(extension):
os.remove(os.path.join(root, currentFile))
def save_to_csv(doc_name, path, matrix, header=None):
"""
It saves some matrix with its header in a csv file
Args:
doc_name: name of the csv to be saved
path: where to store the csv
matrix: main info of the csv
header: header of the csv
"""
import csv
with open(path + doc_name, 'w') as f:
writer = csv.writer(f, delimiter=";", lineterminator='\n')
if header is not None:
writer.writerow(header)
writer.writerows(matrix)
def change_decimal_separator(doc_name, path, exceptions=None):
"""
It replaces all the "." in a csv for "," since decimal separator in europe is different than USA
It also allows to have some exceptions
Args:
doc_name: name of the csv to be processed
path: where it is the csv
exceptions: list of exceptions like [".txt", ".xlsx"]
"""
with open(path + doc_name, 'r') as f:
filedata = f.read()
filedata = filedata.replace('.', ',')
if exceptions is not None:
for excep in exceptions:
filedata = filedata.replace(excep.replace('.', ','), excep)
with open(path + doc_name, 'w+') as f:
f.write(filedata)
#########################################################################################################
# pickle utilities #
#########################################################################################################
PICKLE_EXTENSION = ".pkl"
def load_pickle(name, extension=PICKLE_EXTENSION, path=None):
"""
Gets a pickle object previously stored
Args:
name: name of the pickle, it is not necessary to put the extension
Returns:
pickle object read
"""
#print "Loading", get_full_URI(name, extension, path)
from cPickle import load
uri = get_full_URI(name, extension, path)
#read the pickle
with open(uri, 'rb') as f:
return load(f)
def save_pickle(object, name, extension=PICKLE_EXTENSION, path=None):
"""
Store an object into a pickle
Args:
object: object to be stored as a pickle
name: name of the pickle, it is not necessary to put the extension
"""
from cPickle import dump
uri = get_full_URI(name, extension, path)
#save the pickle
with open(uri, 'wb') as f:
dump(object, f, -1)
#########################################################################################################
# time utilities #
#########################################################################################################
def fancy_string_time_from_seconds(seconds):
"""
It converts a time in seconds into a string that is beautiful to read
Args:
seconds: time in seconds
Returns:
a fancy string like 1h 21m 42s
"""
#extract hours and minutes
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
#only print what it is not 0
if h > 0:
return "%dh %dm %02ds" % (h, m, s)
else:
if m >0 :
return "%dm %02ds" % (m, s)
else:
return "%.2fs" % s
class Timer():
"""
Object used to show times of execution
Args:
t0: time of the last measure
"""
def __init__(self):
import time
self.t0 = time.time()
def get_time(self):
"""
It gives the time passed since the last measure. It restart everytime it is asked.
Returns:
time as string rounded at 2
"""
import time
seconds = time.time() - self.t0
self.__init__()
return fancy_string_time_from_seconds(seconds)
def sleep(minutes):
"""
It sleep x minutes and keep showing how many minutes it has slept
Args:
minutes: minutes to be slept
"""
import time
minutes_waited = 0
while minutes_waited <= minutes:
print minutes_waited, "min waited"
time.sleep(60) # Delay for 1 minute (60 seconds)
minutes_waited += 1
#########################################################################################################
# pandas utilities #
#########################################################################################################
def csv_to_df(uri, delimiter=";", decimal = ","):
"""
Process a csv and gives its dataframe
Args:
uri: uri of the csv
delimiter: char that separates diferents rows
decimal: char to separete decimals
Returns:
Dataframe with the info of the csv
"""
import pandas as pd
return pd.read_csv(uri, delimiter=delimiter, decimal=decimal)
def df_to_csv(uri, df, separator=";", decimal = ","):
"""
Stores a dataframe in a csv file
Args:
uri: uri of the csv
separator: char that separates diferents rows
decimal: char to separete decimals
"""
df.to_csv(uri, sep=separator, decimal = decimal)