forked from kaiiyer/UBA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
210 lines (175 loc) · 6.08 KB
/
Copy pathdataset.py
File metadata and controls
210 lines (175 loc) · 6.08 KB
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
'''
Copyright 2019-Present The OpenUB Platform Authors
This file is part of the OpenUB Platform library.
The OpenUB Platform is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The OpenUB Platform is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the OpenUB Platform. If not, see <http://www.gnu.org/licenses/>.
'''
'''
@name dataset
@description purposed with dataset management, and interaction
'''
import logging
import pandas as pd
import numpy as np
from typing import Dict, Tuple, Sequence, List
'''
@name PreSplitRecord
@description before split_record
'''
class PreSplitRecord:
def __init__(self, function):
logging.info("PreSplitRecord constructor")
self.function = function
def __call__(self, *args) -> List:
logging.warning("PreSplitRecord")
logging.error("PreSplitRecord args: "+str(args))
logging.warning("PreSplitRecord args len: "+str(len(args)))
record: str = args[0]
sep: str = args[1]
parser_result: list = self.function(args[0], record, sep)
return parser_result
'''
@name Parser
@description parse raw dataset to cleaneddataset
'''
class Parser():
def __init__(self):
logging.info("Parser init: "+str(self))
'''
@name split_record
@description, take in a record/row, and
return an list of strings. @static because we only need one, and
we don't need to keep a parser object in memory
'''
#@staticmethod
@PreSplitRecord
def split_record(self, record: str, sep: str) -> List:
logging.info("splitting record")
split_result = record.split(sep)
logging.warning("split record: "+str(split_result))
return split_result
class DataFrame():
def __init__(self, df):
self.data = df
'''
@name DatasetPrior
@description take beh
'''
class DatasetLogPrior:
def __init__(self, function):
self.function = function
'''
@name DatasetLogPrior.__call__
@description call before read_from_disk, after dataframe is
assigned to class's public memory
'''
def __call__(self, *args) -> None:
log_message = args[1] # last param
logging.info("[DatasetLogPrior Log Message] "+log_message)
logging.warning("args[0]: "+str(args[0]))
self.function(args[0])
'''
@name Dataset (parent)
@description dataset class is the parent class
'''
class Dataset(Parser):
file_location: str = "blank_file_location";
location_type: str = "blank_file_type";
def __init__(self, type):
super().__init__()
logging.info("Dataset constructor, type of ["+type+"]")
'''
Dataset get dataframe
'''
def get_dataframe(self):
logging.info("Inside Dataset.get_dataframe()")
return self.dataframe;
'''
@name CSV - child of Dataset
@description to handle csv files
'''
class CSV(Dataset):
def __init__(self, parent_folder, folder, location_type):
#call to Dataset class
super().__init__("CSV") # becomes Dataset instance
self.file_location = parent_folder+"/"+folder
self.location_type = location_type
'''
- if the data is NOT in hadoop, read with pandas
- if the data is in hadoop, read with sparkcsv
'''
def read(self) -> None:
logging.info("Reading CSV")
if self.location_type == "disk":
self.read_from_disk(self, "Reading from disk for CSV")
else:
raise Exception("location_type "+self.location_type+" is not supported in CSV")
'''
@name get_size
@description fetch the size of a pandas-based CSV, but still inherit
'''
def get_size(self) -> Tuple:
logging.info("get_size()")
df = super().get_dataframe() # fetch underlying dataframe from parent
return df.data.shape
'''
@name read_from_disk
@description read from disk, and set to objects dataframe
'''
@DatasetLogPrior
def read_from_disk(self) -> None:
logging.info("Trying: "+str(self.file_location))
## TODO: get columns from config
df = pd.read_csv(self.file_location+"/bluecoat.log",
sep=r' ',
engine='python',
header=0,
error_bad_lines=False,
warn_bad_lines=False)
# TODO: Parse class, will parse each row
logging.warning("columns: "+str(df.columns)+":"+str(df.shape))
'''
foo = lambda x: pd.Series([ i for i inself.split_record(x, ' ') ])
# apply the parser to each record
rev = df["date"].head(10).apply(foo)
'''
logging.info( "Dataframe shape: ["+str(df.shape)+"]" )
logging.error( df.describe() )
self.dataframe = DataFrame( df )
'''
@name Dataset_Session
@description instance of using a dataset
'''
class DatasetSession():
def __init__(self, type):
logging.info("dataset session")
self.dataset_type: str = type
'''
@name read_csv
@description load the csv into the dataset sessions's dataset object
'''
def read_csv(self, data_folder: str, folder: str, location_type: str) -> None:
logging.info("Dataset_Session: read_csv")
self.dataset = CSV(data_folder, folder, location_type)
self.dataset.read() # load into class dataset field, read from child class, not parent
'''
@name get_size
@description get size of dataset_session's dataset object
'''
def get_size(self) -> Tuple:
logging.warning("Getting Dataset size...")
return self.dataset.get_size()
'''
@name get_dataset
@description get size of dataset_session's dataset object
'''
def get_dataset(self) -> Dataset:
return self.dataset