forked from PacktPublishing/Data-Augmentation-with-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluto_chapter_2.py
More file actions
265 lines (251 loc) · 7.56 KB
/
pluto_chapter_2.py
File metadata and controls
265 lines (251 loc) · 7.56 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
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
# create an object
# First, importing the basic library
import torch
import pandas
import numpy
import matplotlib
import pathlib
import PIL
import datetime
import sys
import psutil
# create class/object
class PacktDataAug(object):
#
# initialize the object
def __init__(self, name="Pluto", is_verbose=True,*args, **kwargs):
super(PacktDataAug, self).__init__(*args, **kwargs)
self.author = "Duc Haba"
self.version = 1.0
self.name = name
if (is_verbose):
self._ph()
self._pp("Hello from class", f"{self.__class__} Class: {self.__class__.__name__}")
self._pp("Code name", self.name)
self._pp("Author is", self.author)
self._ph()
#
return
#
# pretty print output name-value line
def _pp(self, a, b):
print("%28s : %s" % (str(a), str(b)))
return
#
# pretty print the header or footer lines
def _ph(self):
print("-" * 28, ":", "-" * 28)
return
# ---end of class
#
# Hack it! Add new decorator
# add_method() is inspired Michael Garod's blog,
# AND correction by: Филя Усков
#
import functools
def add_method(x):
def dec(z):
@functools.wraps(z)
def y(*args, **kwargs):
return z(*args, **kwargs)
setattr(x, z.__name__, y)
return z
return dec
#
pluto = PacktDataAug("Pluto")
@add_method(PacktDataAug)
def say_sys_info(self):
self._ph()
now = datetime.datetime.now()
self._pp("System time", now.strftime("%Y/%m/%d %H:%M"))
self._pp("Platform", sys.platform)
self._pp("Pluto Version (Chapter)", self.version)
v = sys.version.replace('\n', '')
self._pp("Python (3.7.10)", f'actual: {v}')
self._pp("PyTorch (1.11.0)", f'actual: {torch.__version__}')
self._pp("Pandas (1.3.5)", f'actual: {pandas.__version__}')
self._pp("PIL (9.0.0)", f'actual: {PIL.__version__}')
self._pp("Matplotlib (3.2.2)", f'actual: {matplotlib.__version__}')
#
try:
val = psutil.cpu_count()
self._pp("CPU count", val)
val = psutil.cpu_freq()
if (None != val):
val = val._asdict()
self._pp("CPU speed", f'{val["current"]/1000:.2f} GHz')
self._pp("CPU max speed", f'{val["max"]/1000:.2f} GHz')
else:
self._pp("*CPU speed", "NOT available")
except:
pass
self._ph()
return
pluto.version = 2.0
import opendatasets
#
@add_method(PacktDataAug)
def remember_kaggle_access_keys(self,username,key):
self.kaggle_username = username
self.kaggle_key = key
return
@add_method(PacktDataAug)
def _write_kaggle_credit(self):
creds = '{"username":"'+self.kaggle_username+'","key":"'+self.kaggle_key+'"}'
kdirs = ["~/.kaggle/kaggle.json", "./kaggle.json"]
#
for k in kdirs:
cred_path = pathlib.Path(k).expanduser()
cred_path.parent.mkdir(exist_ok=True)
cred_path.write_text(creds)
cred_path.chmod(0o600)
import kaggle
#
return
#
@add_method(PacktDataAug)
def fetch_kaggle_comp_data(self,cname):
#self._write_kaggle_credit() # need to run only once.
path = pathlib.Path(cname)
kaggle.api.competition_download_cli(str(path))
zipfile.ZipFile(f'{path}.zip').extractall(path)
return
#
#
@add_method(PacktDataAug)
def fetch_kaggle_dataset(self,url,dest="kaggle"):
#self._write_kaggle_credit() # need to run only once.
opendatasets.download(url,data_dir=dest)
return
import zipfile
import os
@add_method(PacktDataAug)
def fetch_df(self, csv,sep=','):
df = pandas.read_csv(csv, encoding='latin-1', sep=sep)
return df
#
@add_method(PacktDataAug)
def _fetch_larger_font(self):
heading_properties = [('font-size', '20px')]
cell_properties = [('font-size', '18px')]
dfstyle = [dict(selector="th", props=heading_properties),
dict(selector="td", props=cell_properties)]
return dfstyle
@add_method(PacktDataAug)
def build_sf_fname(self, df):
root = 'state-farm-distracted-driver-detection/imgs/train/'
df["fname"] = root + df.classname+'/'+df.img
return
# set internal counter for image to be zero, e.g. pluto0.jpg, pluto1.jpg, etc.
pluto.fname_id = 0
#
@add_method(PacktDataAug)
def _drop_image(self,canvas, fname=None,format=".jpg",dname="Data-Augmentation-with-Python/pluto_img"):
if (fname is None):
self.fname_id += 1
if not os.path.exists(dname):
os.makedirs(dname)
fn = f'{dname}/pluto{self.fname_id}{format}'
else:
fn = fname
canvas.savefig(fn, bbox_inches="tight", pad_inches=0.25)
return
#
@add_method(PacktDataAug)
def draw_batch(self,df_filenames, disp_max=10,is_shuffle=False, figsize=(16,8)):
disp_col = 5
disp_row = int(numpy.round((disp_max/disp_col)+0.4, 0))
_fns = list(df_filenames)
if (is_shuffle):
numpy.random.shuffle(_fns)
k = 0
clean_fns = []
if (len(_fns) >= disp_max):
canvas, pic = matplotlib.pyplot.subplots(disp_row,disp_col, figsize=figsize)
for i in range(disp_row):
for j in range(disp_col):
try:
im = PIL.Image.open(_fns[k])
pic[i][j].imshow(im)
pic[i][j].set_title(pathlib.Path(_fns[k]).name)
clean_fns.append(_fns[k])
except:
pic[i][j].set_title(pathlib.Path(_fns[k]).name)
k += 1
canvas.tight_layout()
self._drop_image(canvas)
canvas.show()
else:
print("**Warning: the length should be more then ", disp_max, ". The given length: ", len(_fns))
return clean_fns
@add_method(PacktDataAug)
def build_shoe_fname(self, start_path):
df = pandas.DataFrame()
for root, dirs, files in os.walk(start_path, topdown=False):
for name in files:
f = os.path.join(root, name)
p = pathlib.Path(f).parent.name
d = pandas.DataFrame({'fname': [f], 'label': [p]})
#df = df.append(d, ignore_index=True)
df = pandas.concat([df, d], ignore_index=True)
#
# clean it up
df = df.reset_index(drop=True)
return df
#
# create the same with a generic function name
@add_method(PacktDataAug)
def make_dir_dataframe(self, start_path):
return self.build_shoe_fname(start_path)
@add_method(PacktDataAug)
def print_batch_text(self,df_orig, disp_max=6, cols=["title", "description"],is_larger_font=True):
df = df_orig[cols]
with pandas.option_context("display.max_colwidth", None):
if (is_larger_font):
display(df.sample(disp_max).style.set_table_styles(self._fetch_larger_font()))
else:
display(df.sample(disp_max))
return
@add_method(PacktDataAug)
def count_word(self, df, col_dest="description"):
df['wordc'] = df[col_dest].apply(lambda x: len(x.split()))
return
@add_method(PacktDataAug)
def draw_word_count(self,df, wc='wordc',is_stack_verticle=True):
if (is_stack_verticle):
canvas, pic = matplotlib.pyplot.subplots(2,1, figsize=(8,10))
else:
canvas, pic = matplotlib.pyplot.subplots(1,2, figsize=(16,5))
df.boxplot(ax=pic[0],column=[wc],vert=False,color="black")
df[wc].hist(ax=pic[1], color="cornflowerblue", alpha=0.9)
#
title=["Description BoxPlot", "Description Histogram"]
yaxis=["Description", "Stack"]
x1 = f'Word Count: Mean: {df[wc].mean():0.2f}, Min: {df[wc].min()}, Max: {df[wc].max()}'
xaxis=[x1, "Word Count"]
#
pic[0].set_title(title[0], fontweight ="bold")
pic[1].set_title(title[1], fontweight ="bold")
pic[0].set_ylabel(yaxis[0])
pic[1].set_ylabel(yaxis[1])
pic[0].set_xlabel(xaxis[0])
pic[1].set_xlabel(xaxis[1])
#
canvas.tight_layout()
self._drop_image(canvas)
#
canvas.show()
return
import re
import spellchecker
@add_method(PacktDataAug)
def _strip_punc(self,s):
p = re.sub(r'[^\w\s]','',s)
return(p)
#
@add_method(PacktDataAug)
def check_spelling(self,df, col_dest='description'):
spell = spellchecker.SpellChecker()
df["misspelled"] = df[col_dest].apply(lambda x: spell.unknown(self._strip_punc(x).split()))
df["misspelled_count"] = df["misspelled"].apply(lambda x: len(x))
return