-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtils.py
80 lines (62 loc) · 2.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
import numpy as np
import copy
import math
from functools import reduce
from decimal import Decimal
import itertools
from graphviz import Digraph
def graph(df, df_lag, eng = 'dot'):
edge_style = ""
g = Digraph(engine=eng)
g.attr(ratio='fill', size='3,3')
for k, row in enumerate(df.index):
if any(df.iloc[k]) or any(df[row]):
g.node(str(k),row, shape='oval', fontsize='10', width='0', style='filled', fillcolor='#c9c9c9', color="white")
for j, col in enumerate(df.columns):
for i, row in enumerate(df[col]):
if(row):
te_val = str(np.round(row, 6))
g.edge(str(i), str(j), label=str(df_lag[df_lag.columns[j]][i]),style= edge_style, color='black')
return g
def graph_simple(df, eng = 'dot'):
edge_style = ""
g = Digraph(engine=eng)
in_graph = []
for k, row in enumerate(df.index):
if any(df.loc[row]):
g.node(str(row),row, shape='oval', fontsize='10', width='0', style='filled', fillcolor='#c9c9c9', color="gray")
in_graph.append(row)
for c, col in enumerate(df.columns):
if any(df[col]):
if col not in in_graph:
g.node(str(col), col, shape='oval', fontsize='10', width='0', style='filled', fillcolor='#c9c9c9', color="gray")
for j, col in enumerate(df.columns):
for i, row in enumerate(df.index):
if(df[col][i]):
g.edge(str(row), str(col), label=str(df.at[row,col]), style= edge_style, color='black')
return g
def graph_from_dict(dictionary, eng = 'dot'):
edge_style = ""
g = Digraph(engine=eng)
for k in dictionary.keys():
if any([k in sub for sub in dictionary.values() for key in dictionary.keys()]) or dictionary[k]:
g.node(str(k),k, shape='oval', fontsize='10', width='0', style='filled', fillcolor='#c9c9c9', color="gray")
for k, i in dictionary.items():
for it in i:
g.edge(str(it), str(k), label='',style= edge_style, color='black')
return g
def df_to_dictTree(df):
dict_tree = {}
for col in df.columns:
non_zero = df[col].nonzero()
dict_tree[col] = df[col].index[non_zero].values.tolist()
return dict_tree
def app_roll_mean(df, window):
roll = df.copy().rolling(window).mean()
roll.dropna(inplace=True)
roll = roll.round(decimals=0).copy()
roll.reset_index(drop=True, inplace=True)
return roll