-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep5_potentialfinder.py
More file actions
229 lines (207 loc) · 11.1 KB
/
step5_potentialfinder.py
File metadata and controls
229 lines (207 loc) · 11.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python
# coding: utf-8
#############
# @authors: Mathias Riechart, Roshan Bhandari, Abhijeet Amle, Abhimanyu
# This code is used to pre process datasets and later feed into the functions.py that actually does the
# exponential and logistic fit.
#############
from step5_potentialfinder_functions import *
global_x=[]
from os import listdir
from os.path import isfile,isdir, join
import codecs
import cchardet
import random
from io import StringIO
import csv
#import potentialfinder
import pandas as pd
def get_encoding(file,skiprows=0):
testStr = b''
count = 0
with open(file, 'rb') as x:
line = x.readline()
while line and count < 5000: #Set based on lines you'd want to check
for _ in range(0,skiprows): #read in only those rows which are later used for sampling as well
try:
next(x)
except StopIteration:
pass
testStr = testStr + line
count = count + 1
line = x.readline()
result=cchardet.detect(testStr)
if result['confidence'] is None:
return ''
if result['confidence']<1:
print('# Encoding Detection: ' + result['encoding']+' (confidence:' + str(result['confidence'])+")")
return result['encoding']
def buffered_row_count(file):
count = 0
thefile = open(file, 'rb')
while 1:
buffer = thefile.read(8192*1024)
if not buffer: break
count += buffer.count(b'\n')
thefile.close( )
return count
def read_every_nth_row_to_byte(file,n_th):
## required as pandas.read_csv with the skip function takes too much memory.
## --> first load every nth row as byte --> then load this into pandas
with open(file, 'rb') as x:
byteStr = b''
line = x.readline()
#line = line.replace(b'\n', b'')+b'\n'
#print(line)
while line: #Set based on lines you'd want to check
for _ in range(0,n_th): #read in only those rows which are later used for sampling as well
try:
next(x)
except StopIteration:
pass
byteStr = byteStr + line
line = x.readline()
#line = line.replace(b'\n', b'')+b'\n'
#print(line)
byteStr = byteStr + line
return byteStr
def get_last_analyzed_file(analyzed_files):
if isfile(analyzed_files):
analyzed_files_obj = open(analyzed_files, "r")
allfiles=analyzed_files_obj.read().splitlines()
analyzed_files_obj.close()
return allfiles[-1]
else:
return ''
def update_analyzed_files(file_to_add,analyzed_files):
analyzed_files_obj = open(analyzed_files, "a+")
analyzed_files_obj.write(file_to_add+'\n')
analyzed_files_obj.close()
n_rows=10000
path="D://Projects/Data_Mining/GCP_Bucket/sampled_data_01_25_2021"
debug=True
analyzed_files="analyzed_files.txt"
outputtablefile='outputtable_selected.csv'
skip_files=True
##read list of files that have already been analyzed (delete this file to force restart analysis on all files):
last_analyzed_file=get_last_analyzed_file(analyzed_files)
print("Last analyzed File: "+last_analyzed_file)
if last_analyzed_file=="":
firstrow=True
else:
firstrow=False
resulttable=pd.read_csv("D://Projects/Data_Mining/New_paper_work/Code/outputs_selected3/table/"+outputtablefile,sep=";")
print("TEST:: ", listdir(path))
for username in listdir(path):
print(username)
if True:
#if username in authorlist:
if True:
for datatablename in [username]:
if True:
#if datatablename in folderlist:
if True:
for filename in [join(path, username)]:
folder_and_file=join(path,username)
if isfile(folder_and_file):
if filename[-3:]!='csv':
continue
outputTableName=path+'_'+username
if path+username == last_analyzed_file.replace('\\','') or last_analyzed_file=='':
skip_files=False
if skip_files:
print("#Skipping:"+ folder_and_file)
continue
print("#Computing:"+ folder_and_file)
update_analyzed_files(folder_and_file,analyzed_files)
try:
num_lines = buffered_row_count(folder_and_file) #requires no encoding
if num_lines<=n_rows:
n_th=0
else:
n_th = round(num_lines/n_rows) # every 100th line = 1% of the lines
if num_lines == 0:
print(folder_and_file + " seems empty.")
continue
encoding=get_encoding(folder_and_file,n_th)
print("-> Encoding:"+encoding)
try:
if encoding=='':
datastr=str(read_every_nth_row_to_byte(folder_and_file,n_th))
else:
datastr=str(read_every_nth_row_to_byte(folder_and_file,n_th),encoding=encoding)
except:
print("Skipped because of encoding identification error.")
continue
separators=[',',';','|','\t']
quotechars=['"',"'"]
worked=False
for separator in separators:
for quotechar in quotechars:
if not worked:
try:
data=StringIO(datastr)
print("trying " + separator + " | " + quotechar)
sqlDFTemp = pd.read_csv(
data,
sep = separator,
encoding=encoding,
quotechar=quotechar
)#, error_bad_lines=False)
if len(sqlDFTemp.columns)==1:
raise Exception(
'###separator has not worked out if there is only 1 column')
worked=True
except:
try:
data=StringIO(datastr)
print("trying " + separator + " | " + quotechar)
sqlDFTemp = pd.read_csv(
data,
sep = None,
encoding=encoding,
engine = 'python',
error_bad_lines=False
)
worked=True
except:
pass
if worked==False:
print("skipped")
continue
#sqlDFTemp=pd.read_csv(join(path,username,datatablename,filename), sep = None, engine = 'python')
except UnicodeDecodeError:
print ("Unicode Decode Error")
except:
if debug:
raise
else:
print('error parsing file: '+join(path,username))
continue
for fraction in [1,0.5,0.25]:
temptable=find_exponents(sqlDFTemp,
fractionToAnalyze=fraction,
outputPath='outputs_selected3',
outputTable=True,
outputPlots=True,
outputTablename=outputTableName,
logToScreen=False,
columnFillThreshold=0.5,
exp_b_threshold=0.00000000002,
exp_r_s_threshold=0.7,
maxrows=n_rows,
debug=False)
#temptable["username"] = username
#temptable["datatablename"] = datatablename
if firstrow:
resulttable=temptable
firstrow=False
else:
if not temptable is None:
resulttable=resulttable.append(temptable,ignore_index=True)
try:
resulttable.to_csv("D://Projects/Data_Mining/New_paper_work/Code/outputs_selected3/table/"+outputtablefile,sep=";")
except:
print("Writing error. Resuming.")
resulttable.to_csv("D://Projects/Data_Mining/New_paper_work/Code/outputs_selected3/table/outputtable.csv",sep=";")
print("Type-1 error count: {}, Type-2 error count: {} Type-3 error count: {}, Type-4 error count: {} ".format(type_1_count, type_2_count, type_3_count, type_4_count))