-
Notifications
You must be signed in to change notification settings - Fork 7
/
myutils.py
144 lines (115 loc) · 4.76 KB
/
myutils.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
# Google sheet API
import csv
import os
import sys
from datetime import datetime
import cv2
import gspread
import matplotlib.pyplot as plt
import numpy as np
from oauth2client.service_account import ServiceAccountCredentials
def create_log_dir_by_date(parent_dir=".", log_dir="logs"):
"""
Create a log directory based on date.
Args:
parent_dir (str, optional): parent directory. Defaults to '.'.
log_dir (str, optional): log directory. Defaults to 'logs'.
Returns:
logs_date_dir (str): log directory based on date.
"""
today = datetime.today().strftime("%Y-%m-%d")
logs_date_dir = os.path.join(parent_dir, log_dir, today)
if os.path.isdir(logs_date_dir) == False:
os.makedirs(logs_date_dir, exist_ok=True)
return logs_date_dir
def upload_df_to_GoogleSheet(
df,
csv_path,
sheetName=None,
json_keyfile_name="skit_GoogleCloud.json",
spreadsheetName="CSV-to-Google-Sheet",
verbose=False,
):
"""
Upload the dataframe to Google sheet online .
Args:
df (pd.DataFrame): dataframe of the metrics of each model. If sheetName is not None, also upload the dataframe to Google sheet as csv file. (probably redundant since we read the csv file in the next step)
csv_path (str): path to the input csv file.
sheetName (str, optional): sheet name for Google sheet. Defaults to None.
json_keyfile_name (str, optional): json key file name. Defaults to 'skit_GoogleCloud.json'. Replace it by your credential file name here.
spreadsheetName (str, optional): name for the Goole SpreadSheet. Defaults to 'CSV-to-Google-Sheet'.
verbose (bool, optional): option to pring logs. Defaults to False.
"""
# Upload the dataframe to Google sheet
scope = [
"https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
]
# Authorize the client
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_keyfile_name, scope)
client = gspread.authorize(credentials)
# Open a spreadsheet
spreadsheet = client.open(spreadsheetName)
# List all worksheets
worksheet_list = spreadsheet.worksheets()
if verbose:
print("all worksheets in the spreadsheet: ", worksheet_list)
# Create a sheet if it doesn't exist
if sheetName not in [worksheet.title for worksheet in worksheet_list]:
print(f"Create a new worksheet {sheetName}")
worksheet = spreadsheet.add_worksheet(title=sheetName, rows="100", cols="20")
# Update the value
spreadsheet.values_update(
sheetName, params={"valueInputOption": "USER_ENTERED"}, body={"values": list(csv.reader(open(csv_path)))}
)
print(f"Successfully uploaded the dataframe to Google sheet {spreadsheetName} -> {sheetName}")
def print_dict(d):
for k, v in d.items():
print(k, v)
def print_dict_l2(d):
for subdic_key in d:
print("sud_dict key", subdic_key)
for k, v in d[subdic_key].items():
print(k, v)
def normalize_im(im):
arr = np.asarray(im)
arr = (arr - np.min(arr)) / (np.max(arr) - np.min(arr))
return arr
def equalize_this(image_src, with_plot=False, gray_scale=False, convert2gray=True, clipLimit=2.0, tileGridLength=8):
if len(image_src.shape) == 3:
gray_scale = False
if not gray_scale:
if convert2gray:
if np.max(image_src) <= 1:
image_src = image_src * 255
image_src = cv2.cvtColor(image_src.astype(np.uint8), cv2.COLOR_RGB2GRAY)
# image_eq = cv2.equalizeHist(image_src)
clahe = cv2.createCLAHE(clipLimit=clipLimit, tileGridSize=(tileGridLength, tileGridLength))
image_eq = clahe.apply(image_src)
cmap_val = "gray"
else:
r_image, g_image, b_image = cv2.split(image_src)
print("r_image", type(r_image))
print(r_image.shape, r_image.dtype)
r_image_eq = cv2.equalizeHist(r_image.astype(np.uint8))
g_image_eq = cv2.equalizeHist(g_image.astype(np.uint8))
b_image_eq = cv2.equalizeHist(b_image.astype(np.uint8))
image_eq = cv2.merge((r_image_eq, g_image_eq, b_image_eq))
cmap_val = None
else:
image_eq = cv2.equalizeHist(image_src)
cmap_val = "gray"
if with_plot:
fig = plt.figure(figsize=(10, 20))
ax1 = fig.add_subplot(2, 2, 1)
ax1.axis("off")
ax1.title.set_text("Original")
ax2 = fig.add_subplot(2, 2, 2)
ax2.axis("off")
ax2.title.set_text("Equalized")
ax1.imshow(image_src, cmap=cmap_val)
ax2.imshow(image_eq, cmap=cmap_val)
return image_eq
return image_eq