-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrend_annotate.py
More file actions
153 lines (128 loc) · 4.98 KB
/
trend_annotate.py
File metadata and controls
153 lines (128 loc) · 4.98 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
from scipy import stats
import pandas as pd
from rdflib import Literal, URIRef, BNode
from rdflib.namespace import RDF
#from calc_gaps_slopes import gap_calc,trend_calc,monotonic_pred,mod_collector
s=URIRef("http://example.com/app#display-lab")
p=URIRef('http://example.com/slowmo#IsAboutMeasure')
def trend_annotate(input_graph,s13,latest_measure_df,comparator_bnode):
back_up_df=latest_measure_df
s14=s13
p14=URIRef('http://purl.obolibrary.org/obo/RO_0000091')
latest_measure_df=latest_measure_df.reset_index(drop=True)
idx= latest_measure_df.groupby(['measure'])['month'].nlargest(2) .reset_index()
l=idx['level_1'].tolist()
latest_measure_df = latest_measure_df[latest_measure_df.index.isin(l)]
latest_measure_df = latest_measure_df.reset_index(drop=True)
trend_sign=latest_measure_df["Performance_Rate"][1]-latest_measure_df["Performance_Rate"][0]
#back_up_df.to_csv('slope22.csv')
#print(trend_sign)
if trend_sign<0:
ac=BNode(latest_measure_df["measure"][0])
av=comparator_bnode
# #annotate goal comparator
o14=BNode()
input_graph.add((s14,p14,o14))
#input_graph=annotate_goal_negative_trend(input_graph,o14,ac,av)
trend_sign1="negative"
number=find_number(back_up_df,trend_sign1)
idx= back_up_df.groupby(['measure'])['month'].nlargest(number) .reset_index()
l=idx['level_1'].tolist()
measure_df = back_up_df[back_up_df.index.isin(l)]
out = latest_measure_df.groupby('measure').apply(theil_reg, xcol='month', ycol='Performance_Rate')
df_1=out[0]
df_1 = df_1.reset_index()
df_1 = df_1.rename({0:"performance_trend_slope"}, axis=1)
trend_slope=df_1["performance_trend_slope"][0]
# print(trend_slope)
# print(number)
input_graph=annotate_negative_trend(input_graph,o14,ac,av,trend_slope,number)
if trend_sign>0:
ac=BNode(latest_measure_df["measure"][0])
av=comparator_bnode
# #annotate goal comparator
o14=BNode()
input_graph.add((s14,p14,o14))
trend_sign1="positive"
number=find_number(back_up_df,trend_sign1)
idx= back_up_df.groupby(['measure'])['month'].nlargest(number) .reset_index()
l=idx['level_1'].tolist()
measure_df = back_up_df[back_up_df.index.isin(l)]
out = latest_measure_df.groupby('measure').apply(theil_reg, xcol='month', ycol='Performance_Rate')
df_1=out[0]
df_1 = df_1.reset_index()
df_1 = df_1.rename({0:"performance_trend_slope"}, axis=1)
trend_slope=df_1["performance_trend_slope"][0]
input_graph=annotate_positive_trend(input_graph,o14,ac,av,trend_slope,number)
#a=insert_annotate(input_graph)
return input_graph
def annotate_negative_trend(a,s16,measure_Name,o16,trend_slope,number):
p15=RDF.type
o15=URIRef('http://purl.obolibrary.org/obo/PSDO_0000100')
a.add((s16,p15,o15))
p16=URIRef('http://example.com/slowmo#RegardingComparator')
a.add((s16,p16,o16))
p17=URIRef('http://example.com/slowmo#RegardingMeasure')
o17=measure_Name
a.add((s16,p17,o17))
trend_slope=Literal(trend_slope)
number=Literal(number)
p18=URIRef('http://example.com/slowmo#PerformanceTrendSlope')
o18=trend_slope
a.add((s16,p18,o18))
p18=URIRef('http://example.com/slowmo#numberofmonths')
o18=number
a.add((s16,p18,o18))
return a
def annotate_positive_trend(a,s16,measure_Name,o16,trend_slope,number):
p15=RDF.type
o15=URIRef('http://purl.obolibrary.org/obo/PSDO_0000099')
a.add((s16,p15,o15))
p16=URIRef('http://example.com/slowmo#RegardingComparator')
a.add((s16,p16,o16))
p17=URIRef('http://example.com/slowmo#RegardingMeasure')
o17=measure_Name
a.add((s16,p17,o17))
trend_slope=Literal(trend_slope)
number=Literal(number)
p18=URIRef('http://example.com/slowmo#PerformanceTrendSlope')
o18=trend_slope
a.add((s16,p18,o18))
p18=URIRef('http://example.com/slowmo#numberofmonths')
o18=number
a.add((s16,p18,o18))
return a
def find_number(backup_df,trend_sign1):
if(trend_sign1=="negative"):
x=2
lista=[]
lista=backup_df["Performance_Rate"].tolist()
count=0
y=-1
z=y-1
for x in range(len(lista)):
if lista[z]<=lista[y]:
return count
if(lista[z]>lista[y]):
count=count+1
y=y-1
z=y-1
return count
if(trend_sign1=="positive"):
x=2
lista=[]
lista=backup_df["Performance_Rate"].tolist()
count=0
y=-1
z=y-1
for x in range(len(lista)):
if lista[z]>=lista[y]:
return count
if(lista[z]<lista[y]):
count=count+1
y=y-1
z=y-1
return count
def theil_reg(df, xcol, ycol):
model = stats.theilslopes(df[ycol],df[xcol])
return pd.Series(model)