-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
62 lines (49 loc) · 1.64 KB
/
utils.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
import time
import dill
import pandas as pd
from pathlib import Path
import pickle
import custom_logger
def metadata_dump():
"""
Dump metadata as dictionary ("phrog_id": "funcion") to a dill file
"""
df = pd.read_table('Data/metadata-phrog.tsv', header=0)
metadata_dict = dict(zip(df['phrog_id'], df['category']))
dill.dump(metadata_dict, open('Data/metadata_phrog.dill', 'wb'))
def read_corpus(path: Path) -> 'list[list]':
"""
Reads and returns corpus from given corpus file
"""
custom_logger.logger.info("Loading pickle with corpus")
try:
with open(path.as_posix(), 'rb') as f:
sentences = pickle.load(f)
return sentences
except (pickle.UnpicklingError, FileNotFoundError):
custom_logger.logger.critical("Incorrect or corrupted file!")
return
def read_metadata(path: Path) -> pd.DataFrame:
"""
Reads and returns metadata pickle from given metadata pickle file
"""
custom_logger.logger.info("Loading dill with phrog metadata")
try:
with open(path.as_posix(), 'rb') as in_strm:
func = dill.load(in_strm)
return func
except (dill.UnpicklingError, FileNotFoundError):
custom_logger.logger.critical("Incorrect or corrupted file!")
return
def time_this(function):
"""
Decorator to return function execution time.
"""
def wrapper(*args, **kwargs):
start = time.perf_counter()
exec = function(*args, **kwargs)
end = time.perf_counter()
runtime = end - start
custom_logger.logger.info(f"Done in {runtime:0.8f}")
return exec
return wrapper