-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskStorage.py
152 lines (133 loc) · 6.51 KB
/
DiskStorage.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
import pandas as pd
import os.path
import os
from os import listdir
from gensim.models import Word2Vec
from keras.models import load_model
from SessionLogger import SessionLogger
from DiskStorageMisc import DiskStorageMisc
class DiskStorage:
pickle_ext = '.pickle'
model_ext = '.model'
h5_model_ext = '.h5'
# expects identifier and session identifier
# returns corresponding file path
@staticmethod
def get_file_path_pickle(identifier, session_id, create_sub_dirs=0, root_path=None):
data_path = DiskStorageMisc.get_session_data_path(session_id)
identifier = DiskStorageMisc.get_identifier_path(identifier, create_sub_dirs=create_sub_dirs, root_path=root_path)
return os.path.join(data_path, identifier + DiskStorage.pickle_ext)
# expects identifier and session identifier
# returns corresponding file path
@staticmethod
def get_file_path_model(identifier, session_id, create_sub_dirs=0, root_path=None):
data_path = DiskStorageMisc.get_session_data_path(session_id)
identifier = DiskStorageMisc.get_identifier_path(identifier, create_sub_dirs=create_sub_dirs, root_path=root_path)
return os.path.join(data_path, identifier + DiskStorage.model_ext)
# expects identifier and session identifier
# returns corresponding file path
@staticmethod
def get_file_path_h5_model(identifier, session_id, create_sub_dirs=0, root_path=None):
data_path = DiskStorageMisc.get_session_data_path(session_id)
identifier = DiskStorageMisc.get_identifier_path(identifier, create_sub_dirs=create_sub_dirs, root_path=root_path)
return os.path.join(data_path, identifier + DiskStorage.h5_model_ext)
# expects pandas data frame, data frame identifier and session identifier
# stores pandas data frame on disk with specified data frame identifier as name
@staticmethod
def store_pd_frame(data_frame, identifier, session_id):
DiskStorageMisc.create_data_folder(session_id)
data_frame.to_pickle(DiskStorage.get_file_path_pickle(identifier, session_id, create_sub_dirs=1, root_path=DiskStorageMisc.get_session_data_path(session_id)))
# expects identifier and session identifier
# returns corresponding pandas data frame
@staticmethod
def load_pd_frame(identifier, session_id):
fp = DiskStorage.get_file_path_pickle(identifier, session_id)
if os.path.isfile(fp):
return pd.read_pickle(fp)
else:
return pd.DataFrame()
# expects identifier and session identifier
# deletes corresponding pandas data frame from disk
@staticmethod
def delete_pd_frame(identifier, session_id):
path = DiskStorage.get_file_path_pickle(identifier, session_id)
if os.path.exists(path):
os.remove(path)
SessionLogger.log('Data frame \'' + identifier + '\' has been deleted.')
# model, model identifier and session identifier
# stores model on disk with specified model identifier as name
@staticmethod
def store_model(model, identifier, session_id):
DiskStorageMisc.create_data_folder(session_id)
model.save(DiskStorage.get_file_path_model(identifier, session_id, create_sub_dirs=1, root_path=DiskStorageMisc.get_session_data_path(session_id)))
# expects identifier and session identifier
# returns corresponding model
@staticmethod
def load_model(identifier, session_id):
return Word2Vec.load(DiskStorage.get_file_path_model(identifier, session_id))
# expects identifier and session identifier
# deletes corresponding model from disk
@staticmethod
def delete_model(identifier, session_id):
path = DiskStorage.get_file_path_model(identifier, session_id)
if os.path.exists(path):
os.remove(path)
SessionLogger.log('Vector Model \'' + identifier + '\' has been deleted.')
# model, model identifier and session identifier
# stores model on disk with specified model identifier as name
@staticmethod
def store_h5_model(model, identifier, session_id):
DiskStorageMisc.create_data_folder(session_id)
model.save(DiskStorage.get_file_path_h5_model(identifier, session_id, create_sub_dirs=1, root_path=DiskStorageMisc.get_session_data_path(session_id)))
# expects identifier and session identifier
# returns corresponding model
@staticmethod
def load_h5_model(identifier, session_id):
return load_model(DiskStorage.get_file_path_h5_model(identifier, session_id))
# expects identifier and session identifier
# deletes corresponding model from disk
@staticmethod
def delete_h5_model(identifier, session_id):
path = DiskStorage.get_file_path_h5_model(identifier, session_id)
if os.path.exists(path):
os.remove(path)
SessionLogger.log('Classification Model \'' + identifier + '\' has been deleted.')
# expects path to folder
# deletes all files from folder
@staticmethod
def delete_from_folder(path):
DiskStorageMisc.delete_from_folder(path)
# expects session identifier
# deletes all data from session
@staticmethod
def delete_session_data(session_id):
DiskStorageMisc.delete_session_data(session_id)
# expects location within the session location (i.e. a directory)
# deletes location from session
@staticmethod
def delete_location(location, session_id):
session_path = DiskStorageMisc.get_session_path(session_id)
location_path = os.path.join(session_path, location)
DiskStorageMisc.delete_from_folder(location_path)
SessionLogger.log('Location \'' + location + '\' has been deleted.')
# expects location within the session location (i.e. a directory) and the session id
# returns a list of all identifiers from the location
@staticmethod
def list_ids(location, session_id):
session_path = DiskStorageMisc.get_session_path(session_id)
location_path = os.path.join(session_path, location)
potential_ids = listdir(location_path)
ids = list()
for pot_id in potential_ids:
if os.path.isfile(os.path.join(location_path, pot_id)):
f_parts = pot_id.split('.')
idx = 0
identifier = ''
for part in f_parts:
if idx == 0:
identifier = part
if 0 < idx < len(f_parts)-1:
identifier = identifier + '.' + part
idx = idx + 1
ids.append(identifier)
return ids