-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
101 lines (74 loc) · 2.23 KB
/
util.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
'''
utility
'''
import os
import pickle
import numpy as np
def file_search(dirname, sub_files, skip_dir=[]):
'''
Returns the number of files in the given directory.
Parameters:
dirname (string): File path to be searched
sub_files (list): Subfiles in the directory
skip_dir (list): dirnames need to be skipped
Returns:
None
'''
filenames = os.listdir(dirname)
for filename in filenames:
full_filename = os.path.join(dirname, filename)
if os.path.isdir(full_filename): # check if file path is in dir
if full_filename.split('/')[-1] in skip_dir:
continue
else:
file_search(full_filename, sub_files, skip_dir)
else:
sub_files.append(full_filename)
def create_folder(dirname):
'''
Creates a folder with the given file path.
Parameters:
dirname (string): File path to be created i.e, full path name
Returns:
None
'''
if not os.path.exists(dirname):
os.makedirs(dirname)
else:
print(f"File path --> '{dirname}' already exists")
def save_as_csv(file_name, data):
'''
Save extracted features as CSV file.
Parameters:
file_name (string): Name to save file as e.g 'foo.csv'
data (DataFrame): Data to be saved in file
Returns:
None
'''
data.to_csv(file_name)
print(f"'{file_name}' successfully saved")
def save_as_pickle(file_name, data):
'''
Save data as pickle file.
Parameters:
file_name (string): Name to save file as e.g 'foo.pkl'
data (DataFrame): Data to be saved in file
Returns:
None
'''
data.to_pickle(file_name)
print(f"'{file_name}' successfully saved")
def save_as_numpy(file_name, data):
'''
Save data as numpy file.
Parameters:
file_name (string): Name to save file as e.g 'foo.pkl'
data: Data to be saved in file
Returns:
None
'''
np.save(file_name, data)
print(f"'{file_name}' successfully saved")
def csv_reader(file_name):
for row in open(file_name, 'r'):
yield row