forked from LessWrong2/Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
58 lines (42 loc) · 1.87 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
import datetime
import pytz
import logging
from functools import wraps
import configparser
import pathlib
def get_config_field(section, field):
config = configparser.ConfigParser()
project_root = pathlib.Path(__file__).parent.absolute()
config_filename = project_root / 'config.ini'
config.read(config_filename)
return config[section][field]
def print_and_log(*args):
logging.basicConfig(filename=get_config_field('LOGGING', 'file'), level=logging.DEBUG)
print(*args)
message = ' '.join([str(arg) for arg in args])
logging.debug(message)
def timed(func):
"""This decorator prints the start, end, and execution time for the decorated function."""
@wraps(func)
def wrapper(*args, **kwargs):
def get_local_time(tz='US/Pacific'):
return datetime.datetime.now(pytz.timezone('US/Pacific'))
start = get_local_time()
print_and_log('{} started at {}'.format(func.__name__, start.strftime('%Y-%m-%d %H:%M:%S')))
result = func(*args, **kwargs)
end = get_local_time()
print_and_log('{} finished at {}'.format(func.__name__, start.strftime('%Y-%m-%d %H:%M:%S')))
print_and_log("{} ran in {}\n".format(func.__name__, end - start))
return result
return wrapper
def mem_and_info(df):
"""Convenience function to display the memory usage and data types of dataframes during development"""
print((df.memory_usage(deep=True) / 2 ** 20).sum().round(1))
print()
a = (df.memory_usage(deep=True) / 2 ** 20).round(1).to_frame('memory') # .to_frame('memory').merge
b = df.dtypes.to_frame('dtype')
c = df.isnull().mean().to_frame('percent_null').round(3)
print(a.merge(b, left_index=True, right_index=True)
.merge(c, left_index=True, right_index=True)
.sort_values('memory', ascending=False))
print() # creates space when running repeatedly